From 7f73574bb8ab541d57a0f6a833e1ec0fce413ca3 Mon Sep 17 00:00:00 2001 From: cam-schultz Date: Mon, 10 Jun 2024 10:55:23 -0500 Subject: [PATCH] move dialwithconfig to utils --- config/config.go | 3 +- ethclient/client.go | 52 ----------------------------------- ethclient/client_test.go | 45 ------------------------------ main/main.go | 3 +- relayer/listener.go | 4 +-- vms/evm/destination_client.go | 4 +-- 6 files changed, 6 insertions(+), 105 deletions(-) delete mode 100644 ethclient/client.go delete mode 100644 ethclient/client_test.go diff --git a/config/config.go b/config/config.go index 47b842f6..91219ed7 100644 --- a/config/config.go +++ b/config/config.go @@ -17,7 +17,6 @@ import ( "github.com/ava-labs/avalanchego/utils/set" "github.com/ava-labs/awm-relayer/utils" - clientUtils "github.com/ava-labs/awm-relayer/ethclient" "github.com/ava-labs/subnet-evm/ethclient" "github.com/ava-labs/subnet-evm/precompile/contracts/warp" @@ -615,7 +614,7 @@ func (s *DestinationBlockchain) initializeWarpQuorum() error { return fmt.Errorf("invalid subnetID in configuration. error: %w", err) } - client, err := clientUtils.DialWithConfig(context.Background(), s.RPCEndpoint.BaseURL, s.RPCEndpoint.HTTPHeaders, s.RPCEndpoint.QueryParams) + client, err := utils.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) } diff --git a/ethclient/client.go b/ethclient/client.go deleted file mode 100644 index 0809a382..00000000 --- a/ethclient/client.go +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package ethclient - -import ( - "context" - "errors" - "fmt" - "net/url" - - "github.com/ava-labs/subnet-evm/ethclient" - "github.com/ava-labs/subnet-evm/rpc" -) - -var ErrInvalidEndpoint = errors.New("invalid rpc endpoint") - -// DialWithContext returns an ethclient.Client with the internal RPC client configured with the provided options. -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(httpHeaders)...) - if err != nil { - return nil, err - } - return ethclient.NewClient(client), nil -} - -// addQueryParams adds the query parameters to the url -func addQueryParams(endpoint string, queryParams map[string]string) (string, error) { - uri, err := url.ParseRequestURI(endpoint) - if err != nil { - return "", fmt.Errorf("%w: %v", ErrInvalidEndpoint, err) - } - values := uri.Query() - for key, value := range queryParams { - values.Add(key, value) - } - uri.RawQuery = values.Encode() - return uri.String(), nil -} - -// newClientOptions creates a ClientOption slice from httpHeaders -func newClientHeaderOptions(httpHeaders map[string]string) []rpc.ClientOption { - opts := make([]rpc.ClientOption, 0, len(httpHeaders)) - for key, value := range httpHeaders { - opts = append(opts, rpc.WithHeader(key, value)) - } - return opts -} diff --git a/ethclient/client_test.go b/ethclient/client_test.go deleted file mode 100644 index f31c16a6..00000000 --- a/ethclient/client_test.go +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package ethclient - -import ( - "errors" - "testing" - - "github.com/stretchr/testify/require" -) - -func TestAddQueryParams(t *testing.T) { - t.Run("NoQueryParams", func(t *testing.T) { - newurl, err := addQueryParams("https://avalabs.com", nil) - require.NoError(t, err) - require.Equal(t, "https://avalabs.com", newurl) - }) - t.Run("TwoQueryParams", func(t *testing.T) { - newurl, err := addQueryParams("https://avalabs.com", map[string]string{ - "first": "value1", - "second": "value2", - }) - require.NoError(t, err) - require.Equal(t, "https://avalabs.com?first=value1&second=value2", newurl) - }) - t.Run("InvalidEndpoint", func(t *testing.T) { - _, err := addQueryParams("invalid-endpoint", nil) - require.True(t, errors.Is(err, ErrInvalidEndpoint)) - }) -} - -func TestNewClientOptions(t *testing.T) { - t.Run("NoHttpHeaders", func(t *testing.T) { - opts := newClientHeaderOptions(nil) - require.Len(t, opts, 0) - }) - t.Run("TwoHttpHeaders", func(t *testing.T) { - opts := newClientHeaderOptions(map[string]string{ - "first": "value1", - "second": "value2", - }) - require.Len(t, opts, 2) - }) -} diff --git a/main/main.go b/main/main.go index baf3ea7c..f8ceb811 100644 --- a/main/main.go +++ b/main/main.go @@ -19,7 +19,6 @@ import ( "github.com/ava-labs/avalanchego/utils/logging" "github.com/ava-labs/awm-relayer/config" "github.com/ava-labs/awm-relayer/database" - clientUtils "github.com/ava-labs/awm-relayer/ethclient" "github.com/ava-labs/awm-relayer/peers" "github.com/ava-labs/awm-relayer/relayer" "github.com/ava-labs/awm-relayer/types" @@ -243,7 +242,7 @@ func main() { // errgroup will cancel the context when the first goroutine returns an error errGroup.Go(func() error { // Dial the eth client - ethClient, err := clientUtils.DialWithConfig( + ethClient, err := utils.DialWithConfig( context.Background(), sourceBlockchain.RPCEndpoint.BaseURL, sourceBlockchain.RPCEndpoint.HTTPHeaders, diff --git a/relayer/listener.go b/relayer/listener.go index 84dd2400..6bc72bfa 100644 --- a/relayer/listener.go +++ b/relayer/listener.go @@ -14,11 +14,11 @@ import ( "github.com/ava-labs/avalanchego/utils/logging" "github.com/ava-labs/awm-relayer/config" "github.com/ava-labs/awm-relayer/database" - clientUtils "github.com/ava-labs/awm-relayer/ethclient" "github.com/ava-labs/awm-relayer/messages" offchainregistry "github.com/ava-labs/awm-relayer/messages/off-chain-registry" "github.com/ava-labs/awm-relayer/messages/teleporter" relayerTypes "github.com/ava-labs/awm-relayer/types" + "github.com/ava-labs/awm-relayer/utils" vms "github.com/ava-labs/awm-relayer/vms" "github.com/ava-labs/subnet-evm/ethclient" "github.com/ethereum/go-ethereum/common" @@ -66,7 +66,7 @@ func NewListener( ) return nil, err } - ethWSClient, err := clientUtils.DialWithConfig( + ethWSClient, err := utils.DialWithConfig( context.Background(), sourceBlockchain.WSEndpoint.BaseURL, sourceBlockchain.WSEndpoint.HTTPHeaders, diff --git a/vms/evm/destination_client.go b/vms/evm/destination_client.go index 944cf034..6be100dc 100644 --- a/vms/evm/destination_client.go +++ b/vms/evm/destination_client.go @@ -14,7 +14,7 @@ import ( "github.com/ava-labs/avalanchego/utils/logging" avalancheWarp "github.com/ava-labs/avalanchego/vms/platformvm/warp" "github.com/ava-labs/awm-relayer/config" - clientUtils "github.com/ava-labs/awm-relayer/ethclient" + "github.com/ava-labs/awm-relayer/utils" "github.com/ava-labs/awm-relayer/vms/evm/signer" "github.com/ava-labs/subnet-evm/core/types" "github.com/ava-labs/subnet-evm/ethclient" @@ -52,7 +52,7 @@ func NewDestinationClient( destinationBlockchain *config.DestinationBlockchain, ) (*destinationClient, error) { // Dial the destination RPC endpoint - client, err := clientUtils.DialWithConfig( + client, err := utils.DialWithConfig( context.Background(), destinationBlockchain.RPCEndpoint.BaseURL, destinationBlockchain.RPCEndpoint.HTTPHeaders,