Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiclient #15557

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will always return nil, nil

}
mc.chainName = details.ChainName
}
id, err := client.ChainID(context.Background())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we're already Dialing, we can ask for the chain ID as well

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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing assertion from test

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
Loading