Skip to content

Commit

Permalink
ethclient: change eth client factory args
Browse files Browse the repository at this point in the history
The factory definition is changed and everywhere it is used
  • Loading branch information
najeal committed May 13, 2024
1 parent 7ebb710 commit f768549
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 21 deletions.
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ func (s *DestinationBlockchain) initializeWarpQuorum() error {
return fmt.Errorf("invalid subnetID in configuration. error: %w", err)
}

client, err := ethclient.DialWithConfig(context.Background(), ethclient.Config(s.RPCEndpoint))
client, err := ethclient.DialWithConfig(context.Background(), s.RPCEndpoint.BaseURL, s.RPCEndpoint.HTTPHeaders, s.RPCEndpoint.QueryParams)
if err != nil {
return fmt.Errorf("failed to dial destination blockchain %s: %w", blockchainID, err)
}
Expand Down
12 changes: 3 additions & 9 deletions ethclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,13 @@ import (

var ErrInvalidEndpoint = errors.New("invalid rpc endpoint")

type Config struct {
BaseURL string
QueryParams map[string]string
HTTPHeaders map[string]string
}

// DialWithContext returns an ethclient.Client with the internal RPC client configured with the provided options.
func DialWithConfig(ctx context.Context, cfg Config) (ethclient.Client, error) {
url, err := addQueryParams(cfg.BaseURL, cfg.QueryParams)
func DialWithConfig(ctx context.Context, baseURL string, httpHeaders, queryParams map[string]string) (ethclient.Client, error) {
url, err := addQueryParams(baseURL, queryParams)
if err != nil {
return nil, err
}
client, err := rpc.DialOptions(ctx, url, newClientHeaderOptions(cfg.HTTPHeaders)...)
client, err := rpc.DialOptions(ctx, url, newClientHeaderOptions(httpHeaders)...)
if err != nil {
return nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion relayer/application_relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,9 @@ func (r *applicationRelayer) calculateStartingBlockHeight(processHistoricalBlock
func (r *applicationRelayer) setProcessedBlockHeightToLatest() (uint64, error) {
ethClient, err := ethclient.DialWithConfig(
context.Background(),
ethclient.Config(r.sourceBlockchain.RPCEndpoint),
r.sourceBlockchain.RPCEndpoint.BaseURL,
r.sourceBlockchain.RPCEndpoint.HTTPHeaders,
r.sourceBlockchain.RPCEndpoint.QueryParams,
)
if err != nil {
r.logger.Error(
Expand Down
4 changes: 3 additions & 1 deletion vms/evm/destination_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ func NewDestinationClient(logger logging.Logger, destinationBlockchain *config.D
// Dial the destination RPC endpoint
client, err := ethclient.DialWithConfig(
context.Background(),
ethclient.Config(destinationBlockchain.RPCEndpoint),
destinationBlockchain.RPCEndpoint.BaseURL,
destinationBlockchain.RPCEndpoint.HTTPHeaders,
destinationBlockchain.RPCEndpoint.QueryParams,
)
if err != nil {
logger.Error(
Expand Down
14 changes: 7 additions & 7 deletions vms/evm/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ var warpFilterQuery = interfaces.FilterQuery{

// subscriber implements Subscriber
type subscriber struct {
nodeWSEndpoint ethclient.Config
nodeRPCEndpoint ethclient.Config
nodeWSEndpoint config.APIConfig
nodeRPCEndpoint config.APIConfig
blockchainID ids.ID
logsChan chan vmtypes.WarpLogInfo
evmLog <-chan types.Log
Expand All @@ -58,7 +58,7 @@ type subscriber struct {
logger logging.Logger

// seams for mock injection:
dial func(ctx context.Context, cfg ethclient.Config) (evmethclient.Client, error)
dial func(ctx context.Context, url string, httpHeaders, queryParams map[string]string) (evmethclient.Client, error)
}

// NewSubscriber returns a subscriber
Expand All @@ -75,8 +75,8 @@ func NewSubscriber(logger logging.Logger, subnetInfo config.SourceBlockchain) *s
logs := make(chan vmtypes.WarpLogInfo, maxClientSubscriptionBuffer)

return &subscriber{
nodeWSEndpoint: ethclient.Config(subnetInfo.WSEndpoint),
nodeRPCEndpoint: ethclient.Config(subnetInfo.RPCEndpoint),
nodeWSEndpoint: subnetInfo.WSEndpoint,
nodeRPCEndpoint: subnetInfo.RPCEndpoint,
blockchainID: blockchainID,
logger: logger,
logsChan: logs,
Expand Down Expand Up @@ -141,7 +141,7 @@ func (s *subscriber) ProcessFromHeight(height *big.Int, done chan bool) {
s.logger.Error("cannot process logs from nil height")
done <- false
}
ethClient, err := s.dial(context.Background(), s.nodeWSEndpoint)
ethClient, err := s.dial(context.Background(), s.nodeWSEndpoint.BaseURL, s.nodeWSEndpoint.HTTPHeaders, s.nodeWSEndpoint.QueryParams)
if err != nil {
s.logger.Error("failed to dial eth client", zap.Error(err))
done <- false
Expand Down Expand Up @@ -273,7 +273,7 @@ func (s *subscriber) Subscribe(maxResubscribeAttempts int) error {
func (s *subscriber) dialAndSubscribe() error {
// Dial the configured source chain endpoint
// This needs to be a websocket
ethClient, err := s.dial(context.Background(), s.nodeWSEndpoint)
ethClient, err := s.dial(context.Background(), s.nodeWSEndpoint.BaseURL, s.nodeWSEndpoint.HTTPHeaders, s.nodeWSEndpoint.QueryParams)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions vms/evm/subscriber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/awm-relayer/config"
"github.com/ava-labs/awm-relayer/ethclient"
mock_ethclient "github.com/ava-labs/awm-relayer/vms/evm/mocks"
"github.com/ava-labs/subnet-evm/core/types"
evmethclient "github.com/ava-labs/subnet-evm/ethclient"
Expand All @@ -33,7 +32,7 @@ func makeSubscriberWithMockEthClient(t *testing.T) (*subscriber, *mock_ethclient

mockEthClient := mock_ethclient.NewMockClient(gomock.NewController(t))
subscriber := NewSubscriber(logger, sourceSubnet)
subscriber.dial = func(_ctx context.Context, cfg ethclient.Config) (evmethclient.Client, error) {
subscriber.dial = func(_ctx context.Context, _url string, _httpHeaders, _queryParams map[string]string) (evmethclient.Client, error) {
return mockEthClient, nil
}

Expand Down

0 comments on commit f768549

Please sign in to comment.