Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkouv committed Jun 28, 2024
1 parent 1d801bc commit e72d323
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 18 deletions.
51 changes: 33 additions & 18 deletions core/services/ocr3/plugins/ccipevm/commitcodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ccipevm
import (
"context"
"fmt"
"math/big"
"strings"

"github.com/ethereum/go-ethereum/accounts/abi"
Expand All @@ -17,13 +18,19 @@ import (
// CommitPluginCodec is a codec for encoding and decoding commit plugin reports.
// Compatible with:
// - "EVM2EVMMultiOffRamp 1.6.0-dev"
type CommitPluginCodec struct{}
type CommitPluginCodec struct {
commitReportAcceptedEventInputs abi.Arguments
}

func NewCommitPluginCodec() *CommitPluginCodec {
return &CommitPluginCodec{}
abiParsed, err := abi.JSON(strings.NewReader(evm_2_evm_multi_offramp.EVM2EVMMultiOffRampABI))
if err != nil {
panic(fmt.Errorf("parse multi offramp abi: %s", err))
}
eventInputs := abihelpers.MustGetEventInputs("CommitReportAccepted", abiParsed)
return &CommitPluginCodec{commitReportAcceptedEventInputs: eventInputs}
}

// todo: performance optimize, test
func (c *CommitPluginCodec) Encode(ctx context.Context, report cciptypes.CommitPluginReport) ([]byte, error) {
merkleRoots := make([]evm_2_evm_multi_offramp.EVM2EVMMultiOffRampMerkleRoot, 0, len(report.MerkleRoots))
for _, root := range report.MerkleRoots {
Expand Down Expand Up @@ -71,31 +78,39 @@ func (c *CommitPluginCodec) Encode(ctx context.Context, report cciptypes.CommitP
MerkleRoots: merkleRoots,
}

abiParsed, err := abi.JSON(strings.NewReader(evm_2_evm_multi_offramp.EVM2EVMMultiOffRampABI))
if err != nil {
return nil, fmt.Errorf("parse ABI: %w", err)
}
eventInputs := abihelpers.MustGetEventInputs("CommitReportAccepted", abiParsed)
return eventInputs.PackValues([]interface{}{evmReport})
return c.commitReportAcceptedEventInputs.PackValues([]interface{}{evmReport})
}

// todo: performance optimize, test
func (c *CommitPluginCodec) Decode(ctx context.Context, bytes []byte) (cciptypes.CommitPluginReport, error) {
abiParsed, err := abi.JSON(strings.NewReader(evm_2_evm_multi_offramp.EVM2EVMMultiOffRampABI))
if err != nil {
return cciptypes.CommitPluginReport{}, fmt.Errorf("parse ABI: %w", err)
}
eventInputs := abihelpers.MustGetEventInputs("CommitReportAccepted", abiParsed)

unpacked, err := eventInputs.Unpack(bytes)
unpacked, err := c.commitReportAcceptedEventInputs.Unpack(bytes)
if err != nil {
return cciptypes.CommitPluginReport{}, err
}
if len(unpacked) != 1 {
return cciptypes.CommitPluginReport{}, fmt.Errorf("expected 1 argument, got %d", len(unpacked))
}

commitReport, is := unpacked[0].(evm_2_evm_multi_offramp.EVM2EVMMultiOffRampCommitReport)
commitReport, is := unpacked[0].(struct {
PriceUpdates struct {
TokenPriceUpdates []struct {
SourceToken common.Address `json:"sourceToken"`
UsdPerToken *big.Int `json:"usdPerToken"`
} `json:"tokenPriceUpdates"`
GasPriceUpdates []struct {
DestChainSelector uint64 `json:"destChainSelector"`
UsdPerUnitGas *big.Int `json:"usdPerUnitGas"`
} `json:"gasPriceUpdates"`
} `json:"priceUpdates"`
MerkleRoots []struct {
SourceChainSelector uint64 `json:"sourceChainSelector"`
Interval struct {
Min uint64 `json:"min"`
Max uint64 `json:"max"`
} `json:"interval"`
MerkleRoot [32]uint8 `json:"merkleRoot"`
} `json:"merkleRoots"`
})

if !is {
return cciptypes.CommitPluginReport{},
fmt.Errorf("expected EVM2EVMMultiOffRampCommitReport, got %T", unpacked[0])
Expand Down
79 changes: 79 additions & 0 deletions core/services/ocr3/plugins/ccipevm/commitcodec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package ccipevm

import (
"math/rand"
"testing"

"github.com/smartcontractkit/libocr/offchainreporting2plus/types"
"github.com/stretchr/testify/require"

cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
)

var randomReport = cciptypes.CommitPluginReport{
MerkleRoots: []cciptypes.MerkleRootChain{
{
ChainSel: cciptypes.ChainSelector(rand.Uint64()),
SeqNumsRange: cciptypes.NewSeqNumRange(
cciptypes.SeqNum(rand.Uint64()),
cciptypes.SeqNum(rand.Uint64()),
),
MerkleRoot: utils.RandomBytes32(),
},
{
ChainSel: cciptypes.ChainSelector(rand.Uint64()),
SeqNumsRange: cciptypes.NewSeqNumRange(
cciptypes.SeqNum(rand.Uint64()),
cciptypes.SeqNum(rand.Uint64()),
),
MerkleRoot: utils.RandomBytes32(),
},
},
PriceUpdates: cciptypes.PriceUpdates{
TokenPriceUpdates: []cciptypes.TokenPrice{
{
TokenID: types.Account(utils.RandomAddress().String()),
Price: cciptypes.NewBigInt(utils.RandUint256()),
},
},
GasPriceUpdates: []cciptypes.GasPriceChain{
{GasPrice: cciptypes.NewBigInt(utils.RandUint256()), ChainSel: cciptypes.ChainSelector(rand.Uint64())},
{GasPrice: cciptypes.NewBigInt(utils.RandUint256()), ChainSel: cciptypes.ChainSelector(rand.Uint64())},
{GasPrice: cciptypes.NewBigInt(utils.RandUint256()), ChainSel: cciptypes.ChainSelector(rand.Uint64())},
},
},
}

func TestCommitPluginCodec(t *testing.T) {
commitCodec := NewCommitPluginCodec()
ctx := testutils.Context(t)
encodedReport, err := commitCodec.Encode(ctx, randomReport)
require.NoError(t, err)
decodedReport, err := commitCodec.Decode(ctx, encodedReport)
require.NoError(t, err)
require.Equal(t, randomReport, decodedReport)
}

func BenchmarkCommitPluginCodec_Encode(b *testing.B) {
commitCodec := NewCommitPluginCodec()
ctx := testutils.Context(b)

for i := 0; i < b.N; i++ {
_, err := commitCodec.Encode(ctx, randomReport)
require.NoError(b, err)
}
}

func BenchmarkCommitPluginCodec_Decode(b *testing.B) {
commitCodec := NewCommitPluginCodec()
ctx := testutils.Context(b)
encodedReport, err := commitCodec.Encode(ctx, randomReport)
require.NoError(b, err)

for i := 0; i < b.N; i++ {
_, err := commitCodec.Decode(ctx, encodedReport)
require.NoError(b, err)
}
}

0 comments on commit e72d323

Please sign in to comment.