Skip to content

Commit

Permalink
[CCIP-2917] core/capabilities/ccip: use relayers instead of legacyevm (
Browse files Browse the repository at this point in the history
…#14611)

* core/capabilities/ccip: use relayers instead of legacyevm

* fix lint

* address CR comments

* fix

* goimports
  • Loading branch information
makramkd authored Oct 2, 2024
1 parent 65f1d9d commit 659b75a
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 158 deletions.
36 changes: 18 additions & 18 deletions core/capabilities/ccip/configs/evm/chain_writer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package evm

import (
"encoding/json"
"fmt"

"github.com/ethereum/go-ethereum/accounts/abi"
Expand All @@ -19,28 +18,29 @@ var (
offrampABI = evmtypes.MustGetABI(offramp.OffRampABI)
)

func MustChainWriterConfig(
fromAddress common.Address,
maxGasPrice *assets.Wei,
commitGasLimit,
execBatchGasLimit uint64,
) []byte {
rawConfig := ChainWriterConfigRaw(fromAddress, maxGasPrice, commitGasLimit, execBatchGasLimit)
encoded, err := json.Marshal(rawConfig)
if err != nil {
panic(fmt.Errorf("failed to marshal ChainWriterConfig: %w", err))
}

return encoded
}

// ChainWriterConfigRaw returns a ChainWriterConfig that can be used to transmit commit and execute reports.
func ChainWriterConfigRaw(
fromAddress common.Address,
maxGasPrice *assets.Wei,
commitGasLimit,
execBatchGasLimit uint64,
) evmrelaytypes.ChainWriterConfig {
) (evmrelaytypes.ChainWriterConfig, error) {
if fromAddress == common.HexToAddress("0x0") {
return evmrelaytypes.ChainWriterConfig{}, fmt.Errorf("fromAddress cannot be zero")
}
if maxGasPrice == nil {
return evmrelaytypes.ChainWriterConfig{}, fmt.Errorf("maxGasPrice cannot be nil")
}
if maxGasPrice.Cmp(assets.NewWeiI(0)) <= 0 {
return evmrelaytypes.ChainWriterConfig{}, fmt.Errorf("maxGasPrice must be greater than zero")
}
if commitGasLimit == 0 {
return evmrelaytypes.ChainWriterConfig{}, fmt.Errorf("commitGasLimit must be greater than zero")
}
if execBatchGasLimit == 0 {
return evmrelaytypes.ChainWriterConfig{}, fmt.Errorf("execBatchGasLimit must be greater than zero")
}

return evmrelaytypes.ChainWriterConfig{
Contracts: map[string]*evmrelaytypes.ContractConfig{
consts.ContractNameOffRamp: {
Expand All @@ -60,7 +60,7 @@ func ChainWriterConfigRaw(
},
},
MaxGasPrice: maxGasPrice,
}
}, nil
}

// mustGetMethodName panics if the method name is not found in the provided ABI.
Expand Down
103 changes: 103 additions & 0 deletions core/capabilities/ccip/configs/evm/chain_writer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package evm_test

import (
"testing"

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

"github.com/smartcontractkit/chainlink-ccip/pkg/consts"
"github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/configs/evm"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
)

func TestChainWriterConfigRaw(t *testing.T) {
tests := []struct {
name string
fromAddress common.Address
maxGasPrice *assets.Wei
commitGasLimit uint64
execBatchGasLimit uint64
expectedError string
}{
{
name: "valid input",
fromAddress: common.HexToAddress("0x1234567890abcdef1234567890abcdef12345678"),
maxGasPrice: assets.NewWeiI(1000000000),
commitGasLimit: 21000,
execBatchGasLimit: 42000,
expectedError: "",
},
{
name: "zero fromAddress",
fromAddress: common.HexToAddress("0x0"),
maxGasPrice: assets.NewWeiI(1000000000),
commitGasLimit: 21000,
execBatchGasLimit: 42000,
expectedError: "fromAddress cannot be zero",
},
{
name: "nil maxGasPrice",
fromAddress: common.HexToAddress("0x1234567890abcdef1234567890abcdef12345678"),
maxGasPrice: nil,
commitGasLimit: 21000,
execBatchGasLimit: 42000,
expectedError: "maxGasPrice cannot be nil",
},
{
name: "zero maxGasPrice",
fromAddress: common.HexToAddress("0x1234567890abcdef1234567890abcdef12345678"),
maxGasPrice: assets.NewWeiI(0),
commitGasLimit: 21000,
execBatchGasLimit: 42000,
expectedError: "maxGasPrice must be greater than zero",
},
{
name: "negative maxGasPrice",
fromAddress: common.HexToAddress("0x1234567890abcdef1234567890abcdef12345678"),
maxGasPrice: assets.NewWeiI(-1),
commitGasLimit: 21000,
execBatchGasLimit: 42000,
expectedError: "maxGasPrice must be greater than zero",
},
{
name: "zero commitGasLimit",
fromAddress: common.HexToAddress("0x1234567890abcdef1234567890abcdef12345678"),
maxGasPrice: assets.NewWeiI(1000000000),
commitGasLimit: 0,
execBatchGasLimit: 42000,
expectedError: "commitGasLimit must be greater than zero",
},
{
name: "zero execBatchGasLimit",
fromAddress: common.HexToAddress("0x1234567890abcdef1234567890abcdef12345678"),
maxGasPrice: assets.NewWeiI(1000000000),
commitGasLimit: 21000,
execBatchGasLimit: 0,
expectedError: "execBatchGasLimit must be greater than zero",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config, err := evm.ChainWriterConfigRaw(tt.fromAddress, tt.maxGasPrice, tt.commitGasLimit, tt.execBatchGasLimit)
if tt.expectedError != "" {
assert.EqualError(t, err, tt.expectedError)
} else {
assert.NoError(t, err)
assert.Equal(t,
tt.fromAddress,
config.Contracts[consts.ContractNameOffRamp].Configs[consts.MethodCommit].FromAddress)
assert.Equal(t,
tt.commitGasLimit,
config.Contracts[consts.ContractNameOffRamp].Configs[consts.MethodCommit].GasLimit)
assert.Equal(t,
tt.execBatchGasLimit,
config.Contracts[consts.ContractNameOffRamp].Configs[consts.MethodExecute].GasLimit)
assert.Equal(t,
tt.maxGasPrice,
config.MaxGasPrice)
}
})
}
}
36 changes: 12 additions & 24 deletions core/capabilities/ccip/configs/evm/contract_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,6 @@ var (
// TODO: replace with generated ABI when the contract will be defined
var rmnHomeString = "[{\"inputs\":[],\"name\":\"getAllConfigs\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"num\",\"type\":\"uint256\"}],\"name\":\"store\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]"

// MustSourceReaderConfig returns a ChainReaderConfig that can be used to read from the onramp.
// The configuration is marshaled into JSON so that it can be passed to the relayer NewContractReader() method.
func MustSourceReaderConfig() []byte {
rawConfig := SourceReaderConfig
encoded, err := json.Marshal(rawConfig)
if err != nil {
panic(fmt.Errorf("failed to marshal ChainReaderConfig into JSON: %w", err))
}

return encoded
}

// MustDestReaderConfig returns a ChainReaderConfig that can be used to read from the offramp.
// The configuration is marshaled into JSON so that it can be passed to the relayer NewContractReader() method.
func MustDestReaderConfig() []byte {
rawConfig := DestReaderConfig
encoded, err := json.Marshal(rawConfig)
if err != nil {
panic(fmt.Errorf("failed to marshal ChainReaderConfig into JSON: %w", err))
}

return encoded
}

func MergeReaderConfigs(configs ...evmrelaytypes.ChainReaderConfig) evmrelaytypes.ChainReaderConfig {
allContracts := make(map[string]evmrelaytypes.ChainContractReader)
for _, c := range configs {
Expand Down Expand Up @@ -241,6 +217,8 @@ var SourceReaderConfig = evmrelaytypes.ChainReaderConfig{
},
}

// FeedReaderConfig provides a ChainReaderConfig that can be used to read from a price feed
// that is deployed on-chain.
var FeedReaderConfig = evmrelaytypes.ChainReaderConfig{
Contracts: map[string]evmrelaytypes.ChainContractReader{
consts.ContractNamePriceAggregator: {
Expand Down Expand Up @@ -307,6 +285,16 @@ var HomeChainReaderConfigRaw = evmrelaytypes.ChainReaderConfig{
},
}

var HomeChainReaderConfig = mustMarshal(HomeChainReaderConfigRaw)

func mustMarshal(v interface{}) []byte {
b, err := json.Marshal(v)
if err != nil {
panic(err)
}
return b
}

func mustGetEventName(event string, tabi abi.ABI) string {
e, ok := tabi.Events[event]
if !ok {
Expand Down
Loading

0 comments on commit 659b75a

Please sign in to comment.