Skip to content

Commit

Permalink
Merge pull request #120 from mysteriumnetwork/adjust-interfaces
Browse files Browse the repository at this point in the history
Adjust interfaces used to wrap the clients
  • Loading branch information
tomasmik authored Jul 21, 2021
2 parents 4a17208 + ea47c96 commit 96e1475
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 27 deletions.
28 changes: 23 additions & 5 deletions client/bc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -200,17 +205,30 @@ 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,
}
}

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
}
13 changes: 9 additions & 4 deletions client/ethmulticlient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand Down Expand Up @@ -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 {
Expand Down
36 changes: 18 additions & 18 deletions client/ethmulticlient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand All @@ -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)
Expand All @@ -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())
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 96e1475

Please sign in to comment.