From ff44e0ddd90dee754d680e5af9873b14b7a4c2f1 Mon Sep 17 00:00:00 2001 From: Gabriel Paradiso Date: Mon, 19 Feb 2024 15:06:32 +0100 Subject: [PATCH] fix: lint sonarqube reliability issue --- .../relay/evm/functions/coordinator_v1.go | 8 ++- .../relay/evm/functions/coordinator_v2.go | 8 ++- .../relay/evm/functions/logpoller_wrapper.go | 55 ++++++++++++------- .../evm/functions/logpoller_wrapper_test.go | 15 ++++- 4 files changed, 60 insertions(+), 26 deletions(-) diff --git a/core/services/relay/evm/functions/coordinator_v1.go b/core/services/relay/evm/functions/coordinator_v1.go index c8450e642c8..683d39ddd1a 100644 --- a/core/services/relay/evm/functions/coordinator_v1.go +++ b/core/services/relay/evm/functions/coordinator_v1.go @@ -1,9 +1,11 @@ package functions import ( + "fmt" + "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" - "github.com/pkg/errors" + "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller" "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/functions/generated/functions_coordinator_1_1_0" @@ -61,7 +63,7 @@ func (c *CoordinatorV1) LogsToRequests(requestLogs []logpoller.Log) ([]evmRelayT parsingContract, err := functions_coordinator_1_1_0.NewFunctionsCoordinator110(c.address, c.client) if err != nil { - return nil, errors.Errorf("LogsToRequests: creating a contract instance for NewFunctionsCoordinator110 parsing failed") + return nil, fmt.Errorf("LogsToRequests: creating a contract instance for NewFunctionsCoordinator110 parsing failed: %w", err) } for _, log := range requestLogs { @@ -128,7 +130,7 @@ func (c *CoordinatorV1) LogsToResponses(responseLogs []logpoller.Log) ([]evmRela parsingContract, err := functions_coordinator_1_1_0.NewFunctionsCoordinator110(c.address, c.client) if err != nil { - return nil, errors.Errorf("LogsToResponses: creating a contract instance for parsing failed") + return nil, fmt.Errorf("LogsToResponses: creating a contract instance for parsing failed: %w", err) } for _, log := range responseLogs { gethLog := log.ToGethLog() diff --git a/core/services/relay/evm/functions/coordinator_v2.go b/core/services/relay/evm/functions/coordinator_v2.go index 7e360db609c..04c02570dc2 100644 --- a/core/services/relay/evm/functions/coordinator_v2.go +++ b/core/services/relay/evm/functions/coordinator_v2.go @@ -1,9 +1,11 @@ package functions import ( + "fmt" + "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" - "github.com/pkg/errors" + "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller" "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/functions/generated/functions_coordinator" @@ -58,7 +60,7 @@ func (c *CoordinatorV2) LogsToRequests(requestLogs []logpoller.Log) ([]evmRelayT parsingContract, err := functions_coordinator.NewFunctionsCoordinator(c.address, c.client) if err != nil { - return nil, errors.Errorf("LogsToRequests: creating a contract instance for NewFunctionsCoordinator parsing failed") + return nil, fmt.Errorf("LogsToRequests: creating a contract instance for NewFunctionsCoordinator parsing failed: %w", err) } for _, log := range requestLogs { @@ -127,7 +129,7 @@ func (c *CoordinatorV2) LogsToResponses(responseLogs []logpoller.Log) ([]evmRela parsingContract, err := functions_coordinator.NewFunctionsCoordinator(c.address, c.client) if err != nil { - return nil, errors.Errorf("LogsToResponses: creating a contract instance for parsing failed") + return nil, fmt.Errorf("LogsToResponses: creating a contract instance for parsing failed: %w", err) } for _, log := range responseLogs { gethLog := log.ToGethLog() diff --git a/core/services/relay/evm/functions/logpoller_wrapper.go b/core/services/relay/evm/functions/logpoller_wrapper.go index 4cb3d8da019..a374f01fa9e 100644 --- a/core/services/relay/evm/functions/logpoller_wrapper.go +++ b/core/services/relay/evm/functions/logpoller_wrapper.go @@ -176,10 +176,10 @@ func (l *logPollerWrapper) Name() string { return l.lggr.Name() } func (l *logPollerWrapper) LatestEvents() ([]evmRelayTypes.OracleRequest, []evmRelayTypes.OracleResponse, error) { l.mu.Lock() coordinators := []Coordinator{} - if l.activeCoordinator.Address() != (common.Address{}) { + if l.activeCoordinator != nil && l.activeCoordinator.Address() != (common.Address{}) { coordinators = append(coordinators, l.activeCoordinator) } - if l.proposedCoordinator.Address() != (common.Address{}) && l.activeCoordinator != l.proposedCoordinator { + if l.proposedCoordinator != nil && l.proposedCoordinator.Address() != (common.Address{}) && l.activeCoordinator != l.proposedCoordinator { coordinators = append(coordinators, l.proposedCoordinator) } latest, err := l.logPoller.LatestBlock() @@ -385,48 +385,65 @@ func (l *logPollerWrapper) handleRouteUpdate(activeCoordinatorAddress common.Add return } - if activeCoordinatorAddress == l.activeCoordinator.Address() && proposedCoordinatorAddress == l.proposedCoordinator.Address() { + if (l.activeCoordinator != nil && l.activeCoordinator.Address() == activeCoordinatorAddress) && + (l.proposedCoordinator != nil && l.proposedCoordinator.Address() == proposedCoordinatorAddress) { l.lggr.Debug("LogPollerWrapper: no changes to routes") return } activeCoordinatorTypeAndVersion, err := l.getTypeAndVersion(activeCoordinatorAddress) if err != nil { + l.lggr.Errorf("LogPollerWrapper: failed to get active coordinatorTypeAndVersion: %w", err) return } var activeCoordinator Coordinator - if strings.Contains(activeCoordinatorTypeAndVersion, FUNCTIONS_COORDINATOR_VERSION_1_SUBSTRING) { + switch { + case strings.Contains(activeCoordinatorTypeAndVersion, FUNCTIONS_COORDINATOR_VERSION_1_SUBSTRING): activeCoordinator = NewCoordinatorV1(activeCoordinatorAddress, l.client, l.logPoller, l.lggr) - } else if strings.Contains(activeCoordinatorTypeAndVersion, FUNCTIONS_COORDINATOR_VERSION_2_SUBSTRING) { + case strings.Contains(activeCoordinatorTypeAndVersion, FUNCTIONS_COORDINATOR_VERSION_2_SUBSTRING): activeCoordinator = NewCoordinatorV2(activeCoordinatorAddress, l.client, l.logPoller, l.lggr) + default: + l.lggr.Errorf("LogPollerWrapper: Invalid active coordinator type and version: %q", activeCoordinatorTypeAndVersion) + return } - err = activeCoordinator.RegisterFilters() - if err != nil { - l.lggr.Errorw("LogPollerWrapper: Failed to register active coordinator filters", err) - return + if activeCoordinator != nil { + err = activeCoordinator.RegisterFilters() + if err != nil { + l.lggr.Errorw("LogPollerWrapper: Failed to register active coordinator filters", err) + return + } + l.activeCoordinator = activeCoordinator + l.lggr.Debugw("LogPollerWrapper: new routes", "activeCoordinator", activeCoordinator.Address().Hex()) } proposedCoordinatorTypeAndVersion, err := l.getTypeAndVersion(proposedCoordinatorAddress) if err != nil { + l.lggr.Errorf("LogPollerWrapper: failed to get proposed coordinatorTypeAndVersion: %w", err) return } + var proposedCoordinator Coordinator - if strings.Contains(proposedCoordinatorTypeAndVersion, FUNCTIONS_COORDINATOR_VERSION_1_SUBSTRING) { + switch { + // proposedCoordinatorTypeAndVersion can be empty due to an empty proposedCoordinatorAddress + case proposedCoordinatorTypeAndVersion == "": + proposedCoordinator = NewCoordinatorV1(proposedCoordinatorAddress, l.client, l.logPoller, l.lggr) + case strings.Contains(proposedCoordinatorTypeAndVersion, FUNCTIONS_COORDINATOR_VERSION_1_SUBSTRING): proposedCoordinator = NewCoordinatorV1(proposedCoordinatorAddress, l.client, l.logPoller, l.lggr) - } else if strings.Contains(proposedCoordinatorTypeAndVersion, FUNCTIONS_COORDINATOR_VERSION_2_SUBSTRING) { + case strings.Contains(proposedCoordinatorTypeAndVersion, FUNCTIONS_COORDINATOR_VERSION_2_SUBSTRING): proposedCoordinator = NewCoordinatorV2(proposedCoordinatorAddress, l.client, l.logPoller, l.lggr) - } - err = proposedCoordinator.RegisterFilters() - if err != nil { - l.lggr.Errorw("LogPollerWrapper: Failed to register proposed coordinator filters", err) - return } - l.lggr.Debugw("LogPollerWrapper: new routes", "activeCoordinator", activeCoordinator.Address().Hex(), "proposedCoordinator", proposedCoordinator.Address().Hex()) - l.activeCoordinator = activeCoordinator - l.proposedCoordinator = proposedCoordinator + if proposedCoordinator != nil { + err = proposedCoordinator.RegisterFilters() + if err != nil { + l.lggr.Errorw("LogPollerWrapper: Failed to register proposed coordinator filters", err) + return + } + l.proposedCoordinator = proposedCoordinator + l.lggr.Debugw("LogPollerWrapper: new routes", "proposedCoordinator", proposedCoordinator.Address().Hex()) + } for _, subscriber := range l.subscribers { err := subscriber.UpdateRoutes(activeCoordinator.Address(), proposedCoordinator.Address()) diff --git a/core/services/relay/evm/functions/logpoller_wrapper_test.go b/core/services/relay/evm/functions/logpoller_wrapper_test.go index ef3cba72a09..b2f196c5da6 100644 --- a/core/services/relay/evm/functions/logpoller_wrapper_test.go +++ b/core/services/relay/evm/functions/logpoller_wrapper_test.go @@ -232,7 +232,20 @@ func TestLogPollerWrapper_LatestEvents_ReorgHandlingV1(t *testing.T) { lp.On("Logs", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return([]logpoller.Log{mockedLog}, nil).Once() servicetest.Run(t, lpWrapper) - subscriber.updates.Wait() + + done := make(chan struct{}) + go func() { + subscriber.updates.Wait() + close(done) + }() + + select { + case <-done: // subscriber.updates is 0 + break + case <-time.After(5 * time.Second): // Hit timeout. + t.Log("TestLogPollerWrapper_LatestEvents_ReorgHandlingV1: timeout") + t.FailNow() + } oracleRequests, _, err := lpWrapper.LatestEvents() require.NoError(t, err)