Skip to content

Commit

Permalink
fix: lint sonarqube reliability issue
Browse files Browse the repository at this point in the history
  • Loading branch information
agparadiso committed Feb 19, 2024
1 parent fad8945 commit ff44e0d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 26 deletions.
8 changes: 5 additions & 3 deletions core/services/relay/evm/functions/coordinator_v1.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 5 additions & 3 deletions core/services/relay/evm/functions/coordinator_v2.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
55 changes: 36 additions & 19 deletions core/services/relay/evm/functions/logpoller_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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())
Expand Down
15 changes: 14 additions & 1 deletion core/services/relay/evm/functions/logpoller_wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ff44e0d

Please sign in to comment.