Skip to content

Commit

Permalink
Prevent nil ptr issue on evm/Relayer (#15072)
Browse files Browse the repository at this point in the history
* Prevents nil ptr issue

* Adds test validating fix

* Fixes lint
  • Loading branch information
vyzaldysanchez authored Nov 1, 2024
1 parent db8be5b commit 4e49641
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
8 changes: 4 additions & 4 deletions core/services/relay/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,14 @@ func NewRelayer(ctx context.Context, lggr logger.Logger, chain legacyevm.Chain,
capabilitiesRegistry: opts.CapabilitiesRegistry,
}

wCfg := chain.Config().EVM().Workflow()
// Initialize write target capability if configuration is defined
if chain.Config().EVM().Workflow().ForwarderAddress() != nil {
if chain.Config().EVM().Workflow().GasLimitDefault() == nil {
if wCfg.ForwarderAddress() != nil && wCfg.FromAddress() != nil {
if wCfg.GasLimitDefault() == nil {
return nil, fmt.Errorf("unable to instantiate write target as default gas limit is not set")
}

capability, err := NewWriteTarget(ctx, relayer, chain, *chain.Config().EVM().Workflow().GasLimitDefault(),
lggr)
capability, err := NewWriteTarget(ctx, relayer, chain, *wCfg.GasLimitDefault(), lggr)
if err != nil {
return nil, fmt.Errorf("failed to initialize write target: %w", err)
}
Expand Down
54 changes: 42 additions & 12 deletions core/services/relay/evm/write_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,33 @@ import (
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink-common/pkg/capabilities"
"github.com/smartcontractkit/chainlink-common/pkg/values"
"github.com/smartcontractkit/chainlink/v2/common/headtracker/mocks"
"github.com/smartcontractkit/chainlink/v2/core/capabilities/targets"
"github.com/smartcontractkit/chainlink/v2/core/internal/cltest"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest"
"github.com/smartcontractkit/chainlink/v2/core/services/relay/evm"

"github.com/smartcontractkit/chainlink-common/pkg/capabilities"
"github.com/smartcontractkit/chainlink/v2/common/headtracker/mocks"
evmcapabilities "github.com/smartcontractkit/chainlink/v2/core/capabilities"
"github.com/smartcontractkit/chainlink/v2/core/capabilities/targets"
evmclimocks "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client/mocks"
gasmocks "github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas/mocks"
pollermocks "github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller/mocks"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/txmgr"
txmmocks "github.com/smartcontractkit/chainlink/v2/core/chains/evm/txmgr/mocks"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"
evmmocks "github.com/smartcontractkit/chainlink/v2/core/chains/legacyevm/mocks"
relayevm "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm"

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/keystone/generated/forwarder"
"github.com/smartcontractkit/chainlink/v2/core/internal/cltest"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
"github.com/smartcontractkit/chainlink/v2/core/services/relay/evm"
relayevm "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm"
)

var forwardABI = types.MustGetABI(forwarder.KeystoneForwarderMetaData.ABI)
Expand Down Expand Up @@ -142,12 +141,16 @@ func TestEvmWrite(t *testing.T) {
keyStore := cltest.NewKeyStore(t, db)

lggr := logger.TestLogger(t)
cRegistry := evmcapabilities.NewRegistry(lggr)
relayer, err := relayevm.NewRelayer(testutils.Context(t), lggr, chain, relayevm.RelayerOpts{
DS: db,
CSAETHKeystore: keyStore,
CapabilitiesRegistry: evmcapabilities.NewRegistry(lggr),
CapabilitiesRegistry: cRegistry,
})
require.NoError(t, err)
registeredCapabilities, err := cRegistry.List(testutils.Context(t))
require.NoError(t, err)
require.Len(t, registeredCapabilities, 1) // WriteTarget should be added to the registry

reportID := [2]byte{0x00, 0x01}
reportMetadata := targets.ReportV1Metadata{
Expand Down Expand Up @@ -252,4 +255,31 @@ func TestEvmWrite(t *testing.T) {
_, err = capability.Execute(ctx, req)
require.Error(t, err)
})

t.Run("Relayer fails to start WriteTarget capability on missing config", func(t *testing.T) {
ctx := testutils.Context(t)
testChain := evmmocks.NewChain(t)
testCfg := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) {
c.EVM[0].Workflow.FromAddress = nil

forwarderA := testutils.NewAddress()
forwarderAddr, err2 := types.NewEIP55Address(forwarderA.Hex())
require.NoError(t, err2)
c.EVM[0].Workflow.ForwarderAddress = &forwarderAddr
})
testChain.On("Config").Return(evmtest.NewChainScopedConfig(t, testCfg))
capabilityRegistry := evmcapabilities.NewRegistry(lggr)

_, err := relayevm.NewRelayer(ctx, lggr, testChain, relayevm.RelayerOpts{
DS: db,
CSAETHKeystore: keyStore,
CapabilitiesRegistry: capabilityRegistry,
})
require.NoError(t, err)

l, err := capabilityRegistry.List(ctx)
require.NoError(t, err)

assert.Empty(t, l)
})
}

0 comments on commit 4e49641

Please sign in to comment.