Skip to content

Commit

Permalink
feat: Add configurable timeout to rpc endpoints (#28)
Browse files Browse the repository at this point in the history
* feat: Add configurable timeout to rpc endpoints

* Update README.md

Co-authored-by: Joonas Lehtimäki <[email protected]>

* Update README.md

Co-authored-by: Joonas Lehtimäki <[email protected]>

---------

Co-authored-by: Joonas Lehtimäki <[email protected]>
  • Loading branch information
kayano and jlehtimaki authored Nov 27, 2023
1 parent 8b74016 commit 1bf89eb
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ rpc:
- chainName: archwaytestnet
chainId: constantine-3
url: https://rpc.constantine.archway.tech:443
timeout: 2s

github:
org: archway-network
Expand All @@ -33,6 +34,9 @@ accounts:
During startup it fetches IBC paths from github based on provided config.
If env var GITHUB_TOKEN is provided it will be used to make authenticated requests to GitHub API.
Using provided RPC endpoints it gets clients expiration dates for fetched paths.
Each RCP endpoint can have a different timeout specified.
If env var GLOBAL_RPC_TIMEOUT (default 5s) is provided, it specifies the timeout for endpoints
without having it defined.
For provided accounts it fetches wallet balances using endpoints defined in rpc list.
Expand Down
9 changes: 8 additions & 1 deletion pkg/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@ type Info struct {
ChainID string
RPCAddr string
ClientID string
Timeout string
}

func PrepChain(info Info) (*relayer.Chain, error) {
logger := zap.NewNop()

timeout := rpcTimeout
if info.Timeout != "" {
timeout = info.Timeout
}

providerConfig := cosmos.CosmosProviderConfig{
ChainID: info.ChainID,
Timeout: rpcTimeout,
Timeout: timeout,
KeyringBackend: keyringBackend,
RPCAddr: info.RPCAddr,
}
Expand Down
13 changes: 10 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ type RPC struct {
ChainName string `yaml:"chainName"`
ChainID string `yaml:"chainId"`
URL string `yaml:"url"`
Timeout string `yaml:"timeout"`
}

type Config struct {
Accounts []Account `yaml:"accounts"`
RPCs []RPC `yaml:"rpc"`
GitHub struct {
Accounts []Account `yaml:"accounts"`
GlobalRPCTimeout string `env:"GLOBAL_RPC_TIMEOUT" envDefault:"5s"`
RPCs []RPC `yaml:"rpc"`
GitHub struct {
Org string `yaml:"org"`
Repo string `yaml:"repo"`
IBCDir string `yaml:"dir"`
Expand Down Expand Up @@ -99,6 +101,7 @@ func (a *Account) GetBalance(rpcs *map[string]RPC) error {
chain, err := chain.PrepChain(chain.Info{
ChainID: (*rpcs)[a.ChainName].ChainID,
RPCAddr: (*rpcs)[a.ChainName].URL,
Timeout: (*rpcs)[a.ChainName].Timeout,
})
if err != nil {
return err
Expand All @@ -120,6 +123,10 @@ func (c *Config) GetRPCsMap() *map[string]RPC {
rpcs := map[string]RPC{}

for _, rpc := range c.RPCs {
if rpc.Timeout == "" {
rpc.Timeout = c.GlobalRPCTimeout
}

rpcs[rpc.ChainName] = rpc
}

Expand Down
8 changes: 7 additions & 1 deletion pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,27 @@ func TestGetRPCsMap(t *testing.T) {
ChainName: "archwaytestnet",
ChainID: "constantine-3",
URL: "https://rpc.constantine.archway.tech:443",
Timeout: "2s",
},
}

cfg := Config{RPCs: rpcs}
cfg := Config{
GlobalRPCTimeout: "5s",
RPCs: rpcs,
}

exp := map[string]RPC{
"archway": {
ChainName: "archway",
ChainID: "archway-1",
URL: "https://rpc.mainnet.archway.io:443",
Timeout: "5s",
},
"archwaytestnet": {
ChainName: "archwaytestnet",
ChainID: "constantine-3",
URL: "https://rpc.constantine.archway.tech:443",
Timeout: "2s",
},
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/ibc/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func GetClientsInfo(ibc *config.IBCData, rpcs *map[string]config.RPC) (ClientsIn
cdA := chain.Info{
ChainID: (*rpcs)[ibc.Chain1.ChainName].ChainID,
RPCAddr: (*rpcs)[ibc.Chain1.ChainName].URL,
Timeout: (*rpcs)[ibc.Chain1.ChainName].Timeout,
ClientID: ibc.Chain1.ClientID,
}

Expand All @@ -58,6 +59,7 @@ func GetClientsInfo(ibc *config.IBCData, rpcs *map[string]config.RPC) (ClientsIn
cdB := chain.Info{
ChainID: (*rpcs)[ibc.Chain2.ChainName].ChainID,
RPCAddr: (*rpcs)[ibc.Chain2.ChainName].URL,
Timeout: (*rpcs)[ibc.Chain2.ChainName].Timeout,
ClientID: ibc.Chain2.ClientID,
}

Expand Down Expand Up @@ -116,6 +118,7 @@ func GetChannelsInfo(ibc *config.IBCData, rpcs *map[string]config.RPC) (Channels
cdA := chain.Info{
ChainID: (*rpcs)[ibc.Chain1.ChainName].ChainID,
RPCAddr: (*rpcs)[ibc.Chain1.ChainName].URL,
Timeout: (*rpcs)[ibc.Chain1.ChainName].Timeout,
ClientID: ibc.Chain1.ClientID,
}

Expand All @@ -127,6 +130,7 @@ func GetChannelsInfo(ibc *config.IBCData, rpcs *map[string]config.RPC) (Channels
cdB := chain.Info{
ChainID: (*rpcs)[ibc.Chain2.ChainName].ChainID,
RPCAddr: (*rpcs)[ibc.Chain2.ChainName].URL,
Timeout: (*rpcs)[ibc.Chain2.ChainName].Timeout,
ClientID: ibc.Chain2.ClientID,
}

Expand Down

0 comments on commit 1bf89eb

Please sign in to comment.