Skip to content

Commit

Permalink
add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkouv committed Jul 10, 2024
1 parent 85e1e50 commit 89d286f
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 6 deletions.
8 changes: 4 additions & 4 deletions core/services/ocr3/plugins/ccipevm/commitcodec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
)

var randomReport = func() cciptypes.CommitPluginReport {
var randomCommitReport = func() cciptypes.CommitPluginReport {
return cciptypes.CommitPluginReport{
MerkleRoots: []cciptypes.MerkleRootChain{
{
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestCommitPluginCodecV1(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
report := tc.report(randomReport())
report := tc.report(randomCommitReport())
commitCodec := NewCommitPluginCodecV1()
ctx := testutils.Context(t)
encodedReport, err := commitCodec.Encode(ctx, report)
Expand All @@ -115,7 +115,7 @@ func BenchmarkCommitPluginCodecV1_Encode(b *testing.B) {
commitCodec := NewCommitPluginCodecV1()
ctx := testutils.Context(b)

rep := randomReport()
rep := randomCommitReport()
for i := 0; i < b.N; i++ {
_, err := commitCodec.Encode(ctx, rep)
require.NoError(b, err)
Expand All @@ -125,7 +125,7 @@ func BenchmarkCommitPluginCodecV1_Encode(b *testing.B) {
func BenchmarkCommitPluginCodecV1_Decode(b *testing.B) {
commitCodec := NewCommitPluginCodecV1()
ctx := testutils.Context(b)
encodedReport, err := commitCodec.Encode(ctx, randomReport())
encodedReport, err := commitCodec.Encode(ctx, randomCommitReport())
require.NoError(b, err)

for i := 0; i < b.N; i++ {
Expand Down
126 changes: 124 additions & 2 deletions core/services/ocr3/plugins/ccipevm/executecodec_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,129 @@
package ccipevm

import "testing"
import (
rand2 "crypto/rand"
"math/rand"
"testing"

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"
"github.com/stretchr/testify/assert"
)

var randomExecuteReport = func(t *testing.T) cciptypes.ExecutePluginReport {
const numChainReports = 100
const msgsPerReport = 50
const numTokensPerMsg = 20

chainReports := make([]cciptypes.ExecutePluginReportSingleChain, numChainReports)
for i := 0; i < numChainReports; i++ {
reportMessages := make([]cciptypes.Message, msgsPerReport)
for j := 0; j < msgsPerReport; j++ {
data, err := cciptypes.NewBytesFromString(utils.RandomAddress().String())
assert.NoError(t, err)

tokenAmounts := make([]cciptypes.RampTokenAmount, numTokensPerMsg)
for z := 0; z < numTokensPerMsg; z++ {
tokenAmounts[z] = cciptypes.RampTokenAmount{
SourcePoolAddress: utils.RandomAddress().Bytes(),
DestTokenAddress: utils.RandomAddress().Bytes(),
ExtraData: data,
Amount: cciptypes.NewBigInt(utils.RandUint256()),
}
}

reportMessages[j] = cciptypes.Message{
Header: cciptypes.RampMessageHeader{
MessageID: utils.RandomBytes32(),
SourceChainSelector: cciptypes.ChainSelector(rand.Uint64()),
DestChainSelector: cciptypes.ChainSelector(rand.Uint64()),
SequenceNumber: cciptypes.SeqNum(rand.Uint64()),
Nonce: rand.Uint64(),
MsgHash: utils.RandomBytes32(),
OnRamp: utils.RandomAddress().Bytes(),
},
Sender: utils.RandomAddress().Bytes(),
Data: data,
Receiver: utils.RandomAddress().Bytes(),
ExtraArgs: data,
FeeToken: utils.RandomAddress().Bytes(),
FeeTokenAmount: cciptypes.NewBigInt(utils.RandUint256()),
TokenAmounts: tokenAmounts,
}
}

tokenData := make([][][]byte, numTokensPerMsg)
for j := 0; j < msgsPerReport; j++ {
tokenData[j] = make([][]byte, numTokensPerMsg)
for z := 0; z < numTokensPerMsg; z++ {
tokenData[z] = make([][]byte, 32)
for k := 0; k < 32; k++ {
_, err := rand2.Read(tokenData[z][k])
assert.NoError(t, err)
}
}
}

chainReports[i] = cciptypes.ExecutePluginReportSingleChain{
SourceChainSelector: cciptypes.ChainSelector(rand.Uint64()),
Messages: reportMessages,
OffchainTokenData: tokenData,
Proofs: []cciptypes.Bytes32{utils.RandomBytes32(), utils.RandomBytes32()},
ProofFlagBits: cciptypes.NewBigInt(utils.RandUint256()),
}
}

return cciptypes.ExecutePluginReport{ChainReports: chainReports}
}

func TestExecutePluginCodecV1(t *testing.T) {
t.Skip("Skipping test")
testCases := []struct {
name string
report func(report cciptypes.ExecutePluginReport) cciptypes.ExecutePluginReport
expErr bool
}{
{
name: "base report",
report: func(report cciptypes.ExecutePluginReport) cciptypes.ExecutePluginReport { return report },
expErr: false,
},
{
name: "reports have empty msgs",
report: func(report cciptypes.ExecutePluginReport) cciptypes.ExecutePluginReport {
report.ChainReports[0].Messages = nil
report.ChainReports[4].Messages = []cciptypes.Message{}
return report
},
expErr: false,
},
{
name: "reports have empty offchain token data",
report: func(report cciptypes.ExecutePluginReport) cciptypes.ExecutePluginReport {
report.ChainReports[0].OffchainTokenData = [][][]byte{}
report.ChainReports[4].OffchainTokenData[1] = [][]byte{}
return report
},
expErr: false,
},
}

ctx := testutils.Context(t)

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
codec := NewExecutePluginCodecV1()
report := tc.report(randomExecuteReport(t))
bytes, err := codec.Encode(ctx, report)
if tc.expErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)

decodedReport, err := codec.Decode(ctx, bytes)
assert.NoError(t, err)
assert.Equal(t, report, decodedReport)
})
}
}

0 comments on commit 89d286f

Please sign in to comment.