Skip to content

Commit

Permalink
Fix multiclient (#15557)
Browse files Browse the repository at this point in the history
  • Loading branch information
connorwstein authored Dec 6, 2024
1 parent 125d98c commit 074c1f2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
18 changes: 8 additions & 10 deletions deployment/multiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,15 @@ func NewMultiClient(lggr logger.Logger, rpcs []RPC, opts ...func(client *MultiCl
if err != nil {
return nil, fmt.Errorf("failed to dial ws url '%s': %w", rpc.WSURL, err)
}
// fetch chain name if not set
if mc.chainName == "" {
id, err := client.ChainID(context.Background())
if err == nil {
details, err := chainselectors.GetChainDetailsByChainIDAndFamily(id.String(), chainselectors.FamilyEVM)
if err == nil {
return nil, err
}
mc.chainName = details.ChainName
}
id, err := client.ChainID(context.Background())
if err != nil {
return nil, fmt.Errorf("failed to get chain id: %w", err)
}
details, err := chainselectors.GetChainDetailsByChainIDAndFamily(id.String(), chainselectors.FamilyEVM)
if err != nil {
return nil, fmt.Errorf("failed to lookup chain details %w", err)
}
mc.chainName = details.ChainName
clients = append(clients, client)
}
mc.Client = clients[0]
Expand Down
24 changes: 19 additions & 5 deletions deployment/multiclient_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package deployment

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
Expand All @@ -15,20 +16,33 @@ func TestMultiClient(t *testing.T) {
lggr := logger.TestLogger(t)
// Expect an error if no RPCs supplied.
s := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(http.StatusOK)
_, err := writer.Write([]byte(`{"jsonrpc":"2.0","id":1,"result":true}`))
b, err := ioutil.ReadAll(request.Body)
require.NoError(t, err)
// TODO: Helper struct somewhere for this?
if string(b) == "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"eth_chainId\"}" {
writer.WriteHeader(http.StatusOK)
// Respond with 1337
_, err = writer.Write([]byte(`{"jsonrpc":"2.0","id":1,"result":"0x539"}`))
require.NoError(t, err)
return
} else {
// Dial
writer.WriteHeader(http.StatusOK)
_, err = writer.Write([]byte(`{"jsonrpc":"2.0","id":1,"result":true}`))
require.NoError(t, err)
}
}))
defer s.Close()
_, err := NewMultiClient(lggr, []RPC{})
require.Error(t, err)

// Expect defaults to be set if not provided.
mc, err := NewMultiClient(lggr, []RPC{{WSURL: s.URL}})
require.NoError(t, err)
require.NotNil(t, mc)
assert.Equal(t, mc.RetryConfig.Attempts, uint(RPC_DEFAULT_RETRY_ATTEMPTS))
assert.Equal(t, mc.RetryConfig.Delay, RPC_DEFAULT_RETRY_DELAY)

_, err = NewMultiClient(lggr, []RPC{})
require.Error(t, err)

// Expect second client to be set as backup.
mc, err = NewMultiClient(lggr, []RPC{
{WSURL: s.URL},
Expand Down

0 comments on commit 074c1f2

Please sign in to comment.