diff --git a/deployment/multiclient.go b/deployment/multiclient.go index 914c1fbd9e3..f1ac2f3c310 100644 --- a/deployment/multiclient.go +++ b/deployment/multiclient.go @@ -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] diff --git a/deployment/multiclient_test.go b/deployment/multiclient_test.go index 0dbebbe3a6a..2e10c46e33f 100644 --- a/deployment/multiclient_test.go +++ b/deployment/multiclient_test.go @@ -1,6 +1,7 @@ package deployment import ( + "io/ioutil" "net/http" "net/http/httptest" "testing" @@ -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},