From ea47c96c9219654c7aea8bdc5e893df022874915 Mon Sep 17 00:00:00 2001 From: tomasmik Date: Wed, 21 Jul 2021 10:44:00 +0300 Subject: [PATCH] Adjust interfaces used to wrap the clients --- client/bc.go | 28 ++++++++++++++++++++++----- client/ethmulticlient.go | 13 +++++++++---- client/ethmulticlient_test.go | 36 +++++++++++++++++------------------ 3 files changed, 50 insertions(+), 27 deletions(-) diff --git a/client/bc.go b/client/bc.go index 70b3f37..4735388 100644 --- a/client/bc.go +++ b/client/bc.go @@ -186,11 +186,16 @@ type EtherClient interface { SendTransaction(ctx context.Context, tx *types.Transaction) error } -// EthClientGetter wraps any eth clients. +// EthClientGetter wraps any eth client. type EthClientGetter interface { // Client returns a client to use for making call to the eth blockchain. Client() EtherClient +} +// AddressableEthClientGetter wraps any eth client and is able to return the address +// used to create the client and the client itself. +type AddressableEthClientGetter interface { + EthClientGetter // Address returns an address which was used to create a particular client Address() string } @@ -200,10 +205,9 @@ type DefaultEthClientGetter struct { address string } -func NewDefaultEthClientGetter(address string, cl EtherClient) EthClientGetter { +func NewDefaultEthClientGetter(cl EtherClient) EthClientGetter { return &DefaultEthClientGetter{ - client: cl, - address: address, + client: cl, } } @@ -211,6 +215,20 @@ func (eth *DefaultEthClientGetter) Client() EtherClient { return eth.client } -func (eth *DefaultEthClientGetter) Address() string { +type DefaultAddressableEthClientGetter struct { + address string + *DefaultEthClientGetter +} + +func NewDefaultAddressableEthClientGetter(address string, cl EtherClient) AddressableEthClientGetter { + return &DefaultAddressableEthClientGetter{ + DefaultEthClientGetter: &DefaultEthClientGetter{ + client: cl, + }, + address: address, + } +} + +func (eth *DefaultAddressableEthClientGetter) Address() string { return eth.address } diff --git a/client/ethmulticlient.go b/client/ethmulticlient.go index 9b39781..8678914 100644 --- a/client/ethmulticlient.go +++ b/client/ethmulticlient.go @@ -37,7 +37,7 @@ type EthMultiClient struct { timeout time.Duration // clients holds all the possible clients to call. - clients []EthClientGetter + clients []AddressableEthClientGetter // notifyDown is an optional channel. // @@ -56,7 +56,7 @@ type safeChannel struct { type doFunc func(ctx context.Context, c EtherClient) // NewEthMultiClient creates a new multi clients eth client. -func NewEthMultiClient(defaulTimeout time.Duration, clients []EthClientGetter) (*EthMultiClient, error) { +func NewEthMultiClient(defaulTimeout time.Duration, clients []AddressableEthClientGetter) (*EthMultiClient, error) { if len(clients) == 0 { return nil, errors.New("expected more than 0 clients to use") } @@ -70,11 +70,16 @@ func NewEthMultiClient(defaulTimeout time.Duration, clients []EthClientGetter) ( }, nil } +// Client implements the EthClientGetter interface and returns itself as a EtherClient. +func (c *EthMultiClient) Client() EtherClient { + return c +} + // NewEthMultiClientNotifyDown creates a new multi clients eth client. // // Channel `notifications` must be given and will be used to push notifications to the // client if any nodes go down. The channel is closed when before the clients are closed. -func NewEthMultiClientNotifyDown(defaulTimeout time.Duration, clients []EthClientGetter, notifications chan<- string) (*EthMultiClient, error) { +func NewEthMultiClientNotifyDown(defaulTimeout time.Duration, clients []AddressableEthClientGetter, notifications chan<- string) (*EthMultiClient, error) { if len(clients) == 0 { return nil, errors.New("expected more than 0 clients to use") } @@ -557,7 +562,7 @@ func (c *EthMultiClient) ReorderClients(addresses []string) error { return fmt.Errorf("can't reorder: given %d addresses to use when reordering but have %d clients", len(addresses), len(c.clients)) } - newClients := make([]EthClientGetter, len(c.clients)) + newClients := make([]AddressableEthClientGetter, len(c.clients)) for i, addr := range addresses { found := false for _, cl := range c.clients { diff --git a/client/ethmulticlient_test.go b/client/ethmulticlient_test.go index 9725266..5bfcdae 100644 --- a/client/ethmulticlient_test.go +++ b/client/ethmulticlient_test.go @@ -28,7 +28,7 @@ import ( func Test_EthMultiClient(t *testing.T) { t.Run("no clients produces an error", func(t *testing.T) { - _, err := NewEthMultiClient(time.Second, []EthClientGetter{}) + _, err := NewEthMultiClient(time.Second, []AddressableEthClientGetter{}) assert.Error(t, err) }) @@ -38,8 +38,8 @@ func Test_EthMultiClient(t *testing.T) { return big.NewInt(1), nil }, } - getter := NewDefaultEthClientGetter("", cl) - multi, err := NewEthMultiClient(time.Second, []EthClientGetter{getter}) + getter := NewDefaultAddressableEthClientGetter("", cl) + multi, err := NewEthMultiClient(time.Second, []AddressableEthClientGetter{getter}) assert.NoError(t, err) ctx, cancel := context.WithTimeout(context.TODO(), time.Second*2) @@ -62,8 +62,8 @@ func Test_EthMultiClient(t *testing.T) { return big.NewInt(1), nil }, } - getter := NewDefaultEthClientGetter("", cl) - multi, err := NewEthMultiClient(time.Second*2, []EthClientGetter{getter}) + getter := NewDefaultAddressableEthClientGetter("", cl) + multi, err := NewEthMultiClient(time.Second*2, []AddressableEthClientGetter{getter}) assert.NoError(t, err) chainID, err := multi.ChainID(context.TODO()) @@ -83,10 +83,10 @@ func Test_EthMultiClient(t *testing.T) { return big.NewInt(1), nil }, } - getter := NewDefaultEthClientGetter("", cl) - getter2 := NewDefaultEthClientGetter("", cl2) + getter := NewDefaultAddressableEthClientGetter("", cl) + getter2 := NewDefaultAddressableEthClientGetter("", cl2) - multi, err := NewEthMultiClient(time.Second, []EthClientGetter{getter, getter2}) + multi, err := NewEthMultiClient(time.Second, []AddressableEthClientGetter{getter, getter2}) assert.NoError(t, err) ctx, cancel := context.WithTimeout(context.TODO(), time.Second*2) @@ -111,10 +111,10 @@ func Test_EthMultiClient(t *testing.T) { return big.NewInt(2), nil }, } - getter := NewDefaultEthClientGetter("", cl) - getter2 := NewDefaultEthClientGetter("", cl2) + getter := NewDefaultAddressableEthClientGetter("", cl) + getter2 := NewDefaultAddressableEthClientGetter("", cl2) - multi, err := NewEthMultiClient(time.Second, []EthClientGetter{getter, getter2}) + multi, err := NewEthMultiClient(time.Second, []AddressableEthClientGetter{getter, getter2}) assert.NoError(t, err) ctx, cancel := context.WithTimeout(context.TODO(), time.Second/4) @@ -139,8 +139,8 @@ func Test_EthMultiClient(t *testing.T) { return big.NewInt(2), nil }, } - getter := NewDefaultEthClientGetter("first", cl) - getter2 := NewDefaultEthClientGetter("second", cl2) + getter := NewDefaultAddressableEthClientGetter("first", cl) + getter2 := NewDefaultAddressableEthClientGetter("second", cl2) notificationReceived := make(chan struct{}) notify := make(chan string, 0) @@ -156,7 +156,7 @@ func Test_EthMultiClient(t *testing.T) { } }() - multi, err := NewEthMultiClientNotifyDown(time.Second, []EthClientGetter{getter, getter2}, notify) + multi, err := NewEthMultiClientNotifyDown(time.Second, []AddressableEthClientGetter{getter, getter2}, notify) assert.NoError(t, err) ctx, cancel := context.WithTimeout(context.TODO(), time.Second/4) @@ -195,11 +195,11 @@ func Test_EthMultiClient(t *testing.T) { return big.NewInt(2), nil }, } - getter := NewDefaultEthClientGetter("first", cl) - getter2 := NewDefaultEthClientGetter("second", cl2) - getter3 := NewDefaultEthClientGetter("third", cl3) + getter := NewDefaultAddressableEthClientGetter("first", cl) + getter2 := NewDefaultAddressableEthClientGetter("second", cl2) + getter3 := NewDefaultAddressableEthClientGetter("third", cl3) - multi, err := NewEthMultiClient(time.Second*2, []EthClientGetter{getter, getter2, getter3}) + multi, err := NewEthMultiClient(time.Second*2, []AddressableEthClientGetter{getter, getter2, getter3}) assert.NoError(t, err) order := multi.CurrentClientOrder()