Skip to content

Commit

Permalink
feat(901): log node url when operation fails (#905)
Browse files Browse the repository at this point in the history
Signed-off-by: Asen Prodanov <[email protected]>
  • Loading branch information
asenslime authored Aug 9, 2023
1 parent db69907 commit 663b30f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
30 changes: 23 additions & 7 deletions app/clients/evm/client_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package evm

import (
"context"
"fmt"
log "github.com/sirupsen/logrus"
"math/big"

"github.com/ethereum/go-ethereum"
Expand All @@ -28,13 +30,17 @@ import (
)

type ClientPool struct {
clients []client.EVM
retries int
clients []client.EVM
clientsConfigs []config.Evm
retries int
logger *log.Entry
}

func NewClientPool(c config.EvmPool, chainId uint64) *ClientPool {
logger := config.GetLoggerFor(fmt.Sprintf("EVM Client Pool"))
nodeURLs := c.NodeUrls
clients := make([]client.EVM, 0, len(nodeURLs))
clientsConfigs := make([]config.Evm, 0, len(nodeURLs))
for _, nodeURL := range nodeURLs {
configEvm := config.Evm{
BlockConfirmations: c.BlockConfirmations,
Expand All @@ -45,28 +51,38 @@ func NewClientPool(c config.EvmPool, chainId uint64) *ClientPool {
MaxLogsBlocks: c.MaxLogsBlocks,
}
clients = append(clients, NewClient(configEvm, chainId))
clientsConfigs = append(clientsConfigs, configEvm)
}

retry := len(clients) * 3

return &ClientPool{
clients: clients,
retries: retry,
clients: clients,
clientsConfigs: clientsConfigs,
retries: retry,
logger: logger,
}
}

func (cp *ClientPool) getClient(idx int) client.EVM {
return cp.clients[idx%len(cp.clients)]
func (cp *ClientPool) getClient(idx int) (client.EVM, config.Evm) {
clientIndex := idx % len(cp.clients)
configIndex := idx % len(cp.clientsConfigs)
return cp.clients[clientIndex], cp.clientsConfigs[configIndex]
}

func (cp *ClientPool) retryOperation(operation func(client.EVM) (interface{}, error)) (interface{}, error) {
var err error
for i := 0; i < cp.retries; i++ {
client := cp.getClient(i)
client, clientConfig := cp.getClient(i)
result, e := operation(client)
if e == nil {
return result, nil
}

cp.logger.WithFields(log.Fields{
"nodeUrl": clientConfig.NodeUrl,
"retries": i,
}).Warn("retry operation failed")
err = e
}

Expand Down
9 changes: 6 additions & 3 deletions app/clients/evm/client_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ var (
func setupCP() {
setup()
evmList := make([]client.EVM, 0)
clientConfigs := []config.Evm{config.Evm{NodeUrl: "testurl"}}
evmList = append(evmList, c)

cp = &ClientPool{
clients: evmList,
retries: retries,
clients: evmList,
clientsConfigs: clientConfigs,
logger: config.GetLoggerFor("client_pool_test_logger"),
retries: retries,
}
}

Expand Down Expand Up @@ -444,4 +447,4 @@ func TestClientPool_CallContract(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, expectedResult, actualResult)
mocks.MEVMCoreClient.AssertNumberOfCalls(t, "CallContract", 3)
}
}

0 comments on commit 663b30f

Please sign in to comment.