From 663b30f9183b1201d1bce2f84e9cc5bb4d862727 Mon Sep 17 00:00:00 2001 From: Asen Prodanov <140823798+asenslime@users.noreply.github.com> Date: Wed, 9 Aug 2023 15:26:52 +0300 Subject: [PATCH] feat(901): log node url when operation fails (#905) Signed-off-by: Asen Prodanov --- app/clients/evm/client_pool.go | 30 ++++++++++++++++++++++------- app/clients/evm/client_pool_test.go | 9 ++++++--- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/app/clients/evm/client_pool.go b/app/clients/evm/client_pool.go index 7ae28ee9..d6725055 100644 --- a/app/clients/evm/client_pool.go +++ b/app/clients/evm/client_pool.go @@ -18,6 +18,8 @@ package evm import ( "context" + "fmt" + log "github.com/sirupsen/logrus" "math/big" "github.com/ethereum/go-ethereum" @@ -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, @@ -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 } diff --git a/app/clients/evm/client_pool_test.go b/app/clients/evm/client_pool_test.go index 00cdc128..08f9cf59 100644 --- a/app/clients/evm/client_pool_test.go +++ b/app/clients/evm/client_pool_test.go @@ -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, } } @@ -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) -} \ No newline at end of file +}