Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor extradata codec to unblock ccip ocr message optimization using protobuf #16402

Merged
Prev Previous commit
Next Next commit
update
  • Loading branch information
huangzhen1997 committed Feb 14, 2025
commit 6b018194576230bd29458cf29a82c023c7a5f942
43 changes: 35 additions & 8 deletions core/capabilities/ccip/ccipevm/executecodec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ccipevm

import (
"encoding/base64"
"math/big"
"math/rand"
"testing"

Expand All @@ -12,7 +13,9 @@ import (
"github.com/ethereum/go-ethereum/core"
"github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/ccipsolana"
ccipcommon "github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/common"
"github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/common/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

cciptypes "github.com/smartcontractkit/chainlink-ccip/pkg/types/ccipocr3"
Expand All @@ -27,7 +30,7 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
)

var randomExecuteReport = func(t *testing.T, d *testSetupData, chainSelector uint64) cciptypes.ExecutePluginReport {
var randomExecuteReport = func(t *testing.T, d *testSetupData, chainSelector uint64, gasLimit *big.Int, destGasAmount uint32) cciptypes.ExecutePluginReport {
const numChainReports = 10
const msgsPerReport = 10
const numTokensPerMsg = 3
Expand All @@ -41,7 +44,7 @@ var randomExecuteReport = func(t *testing.T, d *testSetupData, chainSelector uin

tokenAmounts := make([]cciptypes.RampTokenAmount, numTokensPerMsg)
for z := 0; z < numTokensPerMsg; z++ {
encodedDestExecData, err2 := abiEncodeUint32(rand.Uint32())
encodedDestExecData, err2 := abiEncodeUint32(destGasAmount)
require.NoError(t, err2)

tokenAmounts[z] = cciptypes.RampTokenAmount{
Expand All @@ -54,7 +57,7 @@ var randomExecuteReport = func(t *testing.T, d *testSetupData, chainSelector uin
}

extraArgs, err := d.contract.EncodeEVMExtraArgsV1(nil, message_hasher.ClientEVMExtraArgsV1{
GasLimit: utils.RandUint256(),
GasLimit: gasLimit,
})
assert.NoError(t, err)

Expand Down Expand Up @@ -97,19 +100,41 @@ var randomExecuteReport = func(t *testing.T, d *testSetupData, chainSelector uin

func TestExecutePluginCodecV1(t *testing.T) {
d := testSetup(t)
ExtraData := ccipcommon.NewExtraDataCodec(ccipcommon.NewExtraDataCodecParams(ExtraDataDecoder{}, ccipsolana.ExtraDataDecoder{}))
ctx := testutils.Context(t)
mockExtraDataCodec := &mocks.ExtraDataCodec{}
destGasAmount := rand.Uint32()
gasLimit := utils.RandUint256()
mockExtraDataCodec.On("DecodeTokenAmountDestExecData", mock.Anything, mock.Anything).Return(map[string]any{
"destgasamount": destGasAmount,
}, nil)
mockExtraDataCodec.On("DecodeExtraArgs", mock.Anything, mock.Anything).Return(map[string]any{
"gasLimit": utils.RandUint256(),
"accountIsWritableBitmap": gasLimit,
}, nil)

testCases := []struct {
name string
report func(report cciptypes.ExecutePluginReport) cciptypes.ExecutePluginReport
expErr bool
chainSelector uint64
destGasAmount uint32
gasLimit *big.Int
}{
{
name: "base report",
report: func(report cciptypes.ExecutePluginReport) cciptypes.ExecutePluginReport { return report },
expErr: false,
chainSelector: 5009297550715157269, // ETH mainnet chain selector
gasLimit: gasLimit,
destGasAmount: destGasAmount,
},
{
name: "base report",
report: func(report cciptypes.ExecutePluginReport) cciptypes.ExecutePluginReport { return report },
expErr: false,
chainSelector: 124615329519749607, // Solana mainnet chain selector
gasLimit: gasLimit,
destGasAmount: destGasAmount,
},
{
name: "reports have empty msgs",
Expand All @@ -120,6 +145,8 @@ func TestExecutePluginCodecV1(t *testing.T) {
},
expErr: false,
chainSelector: 5009297550715157269, // ETH mainnet chain selector
gasLimit: gasLimit,
destGasAmount: destGasAmount,
},
{
name: "reports have empty offchain token data",
Expand All @@ -130,11 +157,11 @@ func TestExecutePluginCodecV1(t *testing.T) {
},
expErr: false,
chainSelector: 5009297550715157269, // ETH mainnet chain selector
gasLimit: gasLimit,
destGasAmount: destGasAmount,
},
}

ctx := testutils.Context(t)

// Deploy the contract
transactor := evmtestutils.MustNewSimTransactor(t)
simulatedBackend := backends.NewSimulatedBackend(core.GenesisAlloc{
Expand All @@ -148,8 +175,8 @@ func TestExecutePluginCodecV1(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
codec := NewExecutePluginCodecV1(ExtraData)
report := tc.report(randomExecuteReport(t, d, tc.chainSelector))
codec := NewExecutePluginCodecV1(mockExtraDataCodec)
report := tc.report(randomExecuteReport(t, d, tc.chainSelector, tc.gasLimit, tc.destGasAmount))
bytes, err := codec.Encode(ctx, report)
if tc.expErr {
assert.Error(t, err)
Expand Down
10 changes: 6 additions & 4 deletions core/capabilities/ccip/oraclecreator/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ var extraDataCodec = ccipcommon.NewExtraDataCodec(

var plugins = map[string]plugin{
chainsel.FamilyEVM: {
CommitPluginCodec: ccipevm.NewCommitPluginCodecV1(),
ExecutePluginCodec: ccipevm.NewExecutePluginCodecV1(extraDataCodec),
ExtraArgsCodec: extraDataCodec,
MessageHasher: func(lggr logger.Logger) cciptypes.MessageHasher { return ccipevm.NewMessageHasherV1(lggr, extraDataCodec) },
CommitPluginCodec: ccipevm.NewCommitPluginCodecV1(),
ExecutePluginCodec: ccipevm.NewExecutePluginCodecV1(extraDataCodec),
ExtraArgsCodec: extraDataCodec,
MessageHasher: func(lggr logger.Logger) cciptypes.MessageHasher {
return ccipevm.NewMessageHasherV1(lggr, extraDataCodec)
},
TokenDataEncoder: ccipevm.NewEVMTokenDataEncoder(),
GasEstimateProvider: ccipevm.NewGasEstimateProvider(),
RMNCrypto: func(lggr logger.Logger) cciptypes.RMNCrypto { return ccipevm.NewEVMRMNCrypto(lggr) },
Expand Down
Loading