From d494a3cee9a0f279733172f13cb63db57a552a1f Mon Sep 17 00:00:00 2001 From: nogo <0xnogo@gmail.com> Date: Tue, 19 Nov 2024 17:16:31 +0400 Subject: [PATCH 1/4] first attempt --- .../usdcreader/usdcreader_test.go | 142 +++++++++++++++++- 1 file changed, 135 insertions(+), 7 deletions(-) diff --git a/core/capabilities/ccip/ccip_integration_tests/usdcreader/usdcreader_test.go b/core/capabilities/ccip/ccip_integration_tests/usdcreader/usdcreader_test.go index 2d5846f24af..924fca0f5c5 100644 --- a/core/capabilities/ccip/ccip_integration_tests/usdcreader/usdcreader_test.go +++ b/core/capabilities/ccip/ccip_integration_tests/usdcreader/usdcreader_test.go @@ -2,6 +2,8 @@ package usdcreader import ( "context" + "encoding/binary" + "fmt" "math/big" "testing" "time" @@ -11,13 +13,15 @@ import ( gethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethclient/simulated" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uber.org/zap/zapcore" sel "github.com/smartcontractkit/chain-selectors" + "github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller" + ubig "github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils/big" + "github.com/smartcontractkit/chainlink-ccip/pkg/contractreader" "github.com/smartcontractkit/chainlink-ccip/pkg/reader" cciptypes "github.com/smartcontractkit/chainlink-ccip/pkg/types/ccipocr3" @@ -27,7 +31,6 @@ import ( evmconfig "github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/configs/evm" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/headtracker" - "github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils" "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/usdc_reader_tester" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" @@ -37,6 +40,8 @@ import ( evmtypes "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/types" ) +const ChainID = 1337 + func Test_USDCReader_MessageHashes(t *testing.T) { finalityDepth := 5 @@ -201,7 +206,117 @@ func Test_USDCReader_MessageHashes(t *testing.T) { }) } } +func Benchmark_MessageHashes(b *testing.B) { + finalityDepth := 5 + + ctx := testutils.Context(b) + sourceChain := cciptypes.ChainSelector(sel.ETHEREUM_MAINNET_OPTIMISM_1.Selector) + sourceDomainCCTP := reader.CCTPDestDomains[uint64(sourceChain)] + destChain := cciptypes.ChainSelector(sel.AVALANCHE_MAINNET.Selector) + destDomainCCTP := reader.CCTPDestDomains[uint64(destChain)] + + ts := testSetup(ctx, b, sourceChain, evmconfig.USDCReaderConfig, finalityDepth) + + usdcReader, err := reader.NewUSDCMessageReader( + ctx, + logger.TestLogger(b), + map[cciptypes.ChainSelector]pluginconfig.USDCCCTPTokenConfig{ + sourceChain: { + SourceMessageTransmitterAddr: ts.contractAddr.String(), + }, + }, + map[cciptypes.ChainSelector]contractreader.ContractReaderFacade{ + sourceChain: ts.reader, + }) + require.NoError(b, err) + // add messages in log poller directly + populateDatabase(b, ts, sourceChain, sourceDomainCCTP, destDomainCCTP, 11) + + tokens := map[reader.MessageTokenID]cciptypes.RampTokenAmount{} + tokens[reader.NewMessageTokenID(1, 1)] = cciptypes.RampTokenAmount{ + ExtraData: reader.NewSourceTokenDataPayload(11, sourceDomainCCTP).ToBytes(), + } + + hashes, err1 := usdcReader.MessageHashes(ctx, sourceChain, destChain, tokens) + require.NoError(b, err1) + + fmt.Println("Hashes length: ", len(hashes)) + require.Len(b, hashes, 1) +} + +func populateDatabase(b *testing.B, + testEnv *testSetupData, + source cciptypes.ChainSelector, + sourceDomainCCTP uint32, + destDomainCCTP uint32, + startNonce uint64) { + ctx := testutils.Context(b) + + abi, err := usdc_reader_tester.USDCReaderTesterMetaData.GetAbi() + require.NoError(b, err) + + var logs []logpoller.Log + messageSentEventSig := abi.Events["MessageSent"].ID + require.NoError(b, err) + messageTransmitterAddress := testEnv.contractAddr + + for i := 0; i < 10; i++ { + nonce := startNonce + uint64(i) + + // Pack the message following exact CCTP format + var buf []byte + // _msgVersion (uint32) + buf = binary.BigEndian.AppendUint32(buf, reader.CCTPMessageVersion) + // _msgSourceDomain (uint32) + buf = binary.BigEndian.AppendUint32(buf, sourceDomainCCTP) + // _msgDestinationDomain (uint32) + buf = binary.BigEndian.AppendUint32(buf, destDomainCCTP) + // _msgNonce (uint64) + buf = binary.BigEndian.AppendUint64(buf, nonce) + // First 12 bytes of the sender address are always empty for EVM + senderBytes := [12]byte{} + buf = append(buf, senderBytes[:]...) + + // Convert to 32 bytes + var message [32]byte + copy(message[:], buf) + + // Encode the data following Solidity's encoding for bytes + data := make([]byte, 0) + + // Add two 32-byte words of padding before our data + padding := make([]byte, 64) + data = append(data, padding...) + + // Add message + data = append(data, message[:]...) + + // Create topics array with just the event signature + topics := [][]byte{ + messageSentEventSig[:], // Topic[0] is event signature + } + + // Create log entry + logs = append(logs, logpoller.Log{ + EvmChainId: ubig.New(big.NewInt(int64(uint64(source)))), + LogIndex: int64(i + 1), + BlockHash: utils.NewHash(), + BlockNumber: int64(i + 1), + BlockTimestamp: time.Now(), + EventSig: messageSentEventSig, + Topics: topics, + Address: messageTransmitterAddress, + TxHash: utils.NewHash(), + Data: data, + CreatedAt: time.Now(), + }) + } + + require.NoError(b, testEnv.orm.InsertLogs(ctx, logs)) +} + +// we might want to use batching (evm/batching or evm/batching) but might be slow func emitMessageSent(t *testing.T, testEnv *testSetupData, source, dest uint32, nonce uint64) { payload := utils.RandomBytes32() _, err := testEnv.contract.EmitMessageSent( @@ -219,9 +334,7 @@ func emitMessageSent(t *testing.T, testEnv *testSetupData, source, dest uint32, testEnv.sb.Commit() } -func testSetup(ctx context.Context, t *testing.T, readerChain cciptypes.ChainSelector, cfg evmtypes.ChainReaderConfig, depth int) *testSetupData { - const chainID = 1337 - +func testSetup(ctx context.Context, t testing.TB, readerChain cciptypes.ChainSelector, cfg evmtypes.ChainReaderConfig, depth int) *testSetupData { // Generate a new key pair for the simulated account privateKey, err := crypto.GenerateKey() assert.NoError(t, err) @@ -232,7 +345,7 @@ func testSetup(ctx context.Context, t *testing.T, readerChain cciptypes.ChainSel simulatedBackend := simulated.NewBackend(alloc, simulated.WithBlockGasLimit(0)) // Create a transactor - auth, err := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(chainID)) + auth, err := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(ChainID)) assert.NoError(t, err) auth.GasLimit = uint64(0) @@ -258,7 +371,20 @@ func testSetup(ctx context.Context, t *testing.T, readerChain cciptypes.ChainSel } cl := client.NewSimulatedBackendClient(t, simulatedBackend, big.NewInt(0).SetUint64(uint64(readerChain))) headTracker := headtracker.NewSimulatedHeadTracker(cl, lpOpts.UseFinalityTag, lpOpts.FinalityDepth) - lp := logpoller.NewLogPoller(logpoller.NewORM(big.NewInt(0).SetUint64(uint64(readerChain)), db, lggr), + orm := logpoller.NewORM(big.NewInt(0).SetUint64(uint64(readerChain)), db, lggr) + + blockHash := common.HexToHash("0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") + blockNumber := int64(42) + blockTimestamp := time.Now() + finalizedBlock := int64(41) // Assuming block 41 is the last finalized block + + // Insert the dummy block + // @mateusz-sekara: this is not inserted in the database, + err = orm.InsertBlock(ctx, blockHash, blockNumber, blockTimestamp, finalizedBlock) + require.NoError(t, err) + + lp := logpoller.NewLogPoller( + orm, cl, lggr, headTracker, @@ -285,6 +411,7 @@ func testSetup(ctx context.Context, t *testing.T, readerChain cciptypes.ChainSel auth: auth, cl: cl, reader: cr, + orm: orm, lp: lp, } } @@ -296,5 +423,6 @@ type testSetupData struct { auth *bind.TransactOpts cl client.Client reader types.ContractReader + orm logpoller.ORM lp logpoller.LogPoller } From 5d17af2778067827b34a0c2c4c880491e65a370d Mon Sep 17 00:00:00 2001 From: nogo <0xnogo@gmail.com> Date: Wed, 20 Nov 2024 17:18:58 +0400 Subject: [PATCH 2/4] benchmark of usdc query --- .../usdcreader/usdcreader_test.go | 129 +++++++++++------- 1 file changed, 80 insertions(+), 49 deletions(-) diff --git a/core/capabilities/ccip/ccip_integration_tests/usdcreader/usdcreader_test.go b/core/capabilities/ccip/ccip_integration_tests/usdcreader/usdcreader_test.go index 924fca0f5c5..6bd61111c3f 100644 --- a/core/capabilities/ccip/ccip_integration_tests/usdcreader/usdcreader_test.go +++ b/core/capabilities/ccip/ccip_integration_tests/usdcreader/usdcreader_test.go @@ -3,7 +3,6 @@ package usdcreader import ( "context" "encoding/binary" - "fmt" "math/big" "testing" "time" @@ -13,6 +12,7 @@ import ( gethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethclient/simulated" + "github.com/jmoiron/sqlx" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uber.org/zap/zapcore" @@ -21,6 +21,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller" ubig "github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils/big" + "github.com/smartcontractkit/chainlink/v2/core/utils/testutils/heavyweight" "github.com/smartcontractkit/chainlink-ccip/pkg/contractreader" "github.com/smartcontractkit/chainlink-ccip/pkg/reader" @@ -53,7 +54,7 @@ func Test_USDCReader_MessageHashes(t *testing.T) { polygonChain := cciptypes.ChainSelector(sel.POLYGON_MAINNET.Selector) polygonDomainCCTP := reader.CCTPDestDomains[uint64(polygonChain)] - ts := testSetup(ctx, t, ethereumChain, evmconfig.USDCReaderConfig, finalityDepth) + ts := testSetup(ctx, t, ethereumChain, evmconfig.USDCReaderConfig, finalityDepth, false) usdcReader, err := reader.NewUSDCMessageReader( ctx, @@ -209,40 +210,61 @@ func Test_USDCReader_MessageHashes(t *testing.T) { func Benchmark_MessageHashes(b *testing.B) { finalityDepth := 5 - ctx := testutils.Context(b) - sourceChain := cciptypes.ChainSelector(sel.ETHEREUM_MAINNET_OPTIMISM_1.Selector) - sourceDomainCCTP := reader.CCTPDestDomains[uint64(sourceChain)] - destChain := cciptypes.ChainSelector(sel.AVALANCHE_MAINNET.Selector) - destDomainCCTP := reader.CCTPDestDomains[uint64(destChain)] - - ts := testSetup(ctx, b, sourceChain, evmconfig.USDCReaderConfig, finalityDepth) + // Adding a new parameter: tokenCount + testCases := []struct { + name string + msgCount int + startNonce uint64 + tokenCount int + }{ + {"Small_Dataset", 100, 1, 5}, + {"Medium_Dataset", 10_000, 1, 10}, + {"Large_Dataset", 100_000, 1, 50}, + } - usdcReader, err := reader.NewUSDCMessageReader( - ctx, - logger.TestLogger(b), - map[cciptypes.ChainSelector]pluginconfig.USDCCCTPTokenConfig{ - sourceChain: { - SourceMessageTransmitterAddr: ts.contractAddr.String(), - }, - }, - map[cciptypes.ChainSelector]contractreader.ContractReaderFacade{ - sourceChain: ts.reader, - }) - require.NoError(b, err) + for _, tc := range testCases { + b.Run(tc.name, func(b *testing.B) { + ctx := testutils.Context(b) + sourceChain := cciptypes.ChainSelector(sel.ETHEREUM_MAINNET_OPTIMISM_1.Selector) + sourceDomainCCTP := reader.CCTPDestDomains[uint64(sourceChain)] + destChain := cciptypes.ChainSelector(sel.AVALANCHE_MAINNET.Selector) + destDomainCCTP := reader.CCTPDestDomains[uint64(destChain)] + + ts := testSetup(ctx, b, sourceChain, evmconfig.USDCReaderConfig, finalityDepth, true) + + usdcReader, err := reader.NewUSDCMessageReader( + ctx, + logger.TestLogger(b), + map[cciptypes.ChainSelector]pluginconfig.USDCCCTPTokenConfig{ + sourceChain: { + SourceMessageTransmitterAddr: ts.contractAddr.String(), + }, + }, + map[cciptypes.ChainSelector]contractreader.ContractReaderFacade{ + sourceChain: ts.reader, + }) + require.NoError(b, err) + + // Populate the database with the specified number of logs + populateDatabase(b, ts, sourceChain, sourceDomainCCTP, destDomainCCTP, tc.startNonce, tc.msgCount, finalityDepth) + + // Create a map of tokens to query for, with the specified tokenCount + tokens := make(map[reader.MessageTokenID]cciptypes.RampTokenAmount) + for i := 1; i <= tc.tokenCount; i++ { + tokens[reader.NewMessageTokenID(cciptypes.SeqNum(i), 1)] = cciptypes.RampTokenAmount{ + ExtraData: reader.NewSourceTokenDataPayload(tc.startNonce+uint64(i), sourceDomainCCTP).ToBytes(), + } + } - // add messages in log poller directly - populateDatabase(b, ts, sourceChain, sourceDomainCCTP, destDomainCCTP, 11) + b.ResetTimer() - tokens := map[reader.MessageTokenID]cciptypes.RampTokenAmount{} - tokens[reader.NewMessageTokenID(1, 1)] = cciptypes.RampTokenAmount{ - ExtraData: reader.NewSourceTokenDataPayload(11, sourceDomainCCTP).ToBytes(), + for i := 0; i < b.N; i++ { + hashes, err := usdcReader.MessageHashes(ctx, sourceChain, destChain, tokens) + require.NoError(b, err) + require.Len(b, hashes, tc.tokenCount) // Ensure the number of matches is as expected + } + }) } - - hashes, err1 := usdcReader.MessageHashes(ctx, sourceChain, destChain, tokens) - require.NoError(b, err1) - - fmt.Println("Hashes length: ", len(hashes)) - require.Len(b, hashes, 1) } func populateDatabase(b *testing.B, @@ -250,7 +272,9 @@ func populateDatabase(b *testing.B, source cciptypes.ChainSelector, sourceDomainCCTP uint32, destDomainCCTP uint32, - startNonce uint64) { + startNonce uint64, + numOfMessages int, + finalityDepth int) { ctx := testutils.Context(b) abi, err := usdc_reader_tester.USDCReaderTesterMetaData.GetAbi() @@ -261,7 +285,7 @@ func populateDatabase(b *testing.B, require.NoError(b, err) messageTransmitterAddress := testEnv.contractAddr - for i := 0; i < 10; i++ { + for i := 0; i < numOfMessages; i++ { nonce := startNonce + uint64(i) // Pack the message following exact CCTP format @@ -285,9 +309,15 @@ func populateDatabase(b *testing.B, // Encode the data following Solidity's encoding for bytes data := make([]byte, 0) - // Add two 32-byte words of padding before our data - padding := make([]byte, 64) - data = append(data, padding...) + // This says "actual data starts 32 bytes from the beginning" + offsetBytes := make([]byte, 32) + binary.BigEndian.PutUint64(offsetBytes[24:], 32) + data = append(data, offsetBytes...) + + // This is the length of our message + lengthBytes := make([]byte, 32) + binary.BigEndian.PutUint64(lengthBytes[24:], uint64(len(message))) + data = append(data, lengthBytes...) // Add message data = append(data, message[:]...) @@ -314,6 +344,7 @@ func populateDatabase(b *testing.B, } require.NoError(b, testEnv.orm.InsertLogs(ctx, logs)) + require.NoError(b, testEnv.orm.InsertBlock(ctx, utils.RandomHash(), int64(numOfMessages+finalityDepth), time.Now(), int64(numOfMessages+finalityDepth))) } // we might want to use batching (evm/batching or evm/batching) but might be slow @@ -334,7 +365,7 @@ func emitMessageSent(t *testing.T, testEnv *testSetupData, source, dest uint32, testEnv.sb.Commit() } -func testSetup(ctx context.Context, t testing.TB, readerChain cciptypes.ChainSelector, cfg evmtypes.ChainReaderConfig, depth int) *testSetupData { +func testSetup(ctx context.Context, t testing.TB, readerChain cciptypes.ChainSelector, cfg evmtypes.ChainReaderConfig, depth int, useHeavyDB bool) *testSetupData { // Generate a new key pair for the simulated account privateKey, err := crypto.GenerateKey() assert.NoError(t, err) @@ -361,7 +392,15 @@ func testSetup(ctx context.Context, t testing.TB, readerChain cciptypes.ChainSel lggr := logger.TestLogger(t) lggr.SetLogLevel(zapcore.ErrorLevel) - db := pgtest.NewSqlxDB(t) + + // Parameterize database selection + var db *sqlx.DB + if useHeavyDB { + _, db = heavyweight.FullTestDBV2(t, nil) // Use heavyweight database for benchmarks + } else { + db = pgtest.NewSqlxDB(t) // Use simple in-memory DB for tests + } + lpOpts := logpoller.Opts{ PollPeriod: time.Millisecond, FinalityDepth: int64(depth), @@ -373,16 +412,6 @@ func testSetup(ctx context.Context, t testing.TB, readerChain cciptypes.ChainSel headTracker := headtracker.NewSimulatedHeadTracker(cl, lpOpts.UseFinalityTag, lpOpts.FinalityDepth) orm := logpoller.NewORM(big.NewInt(0).SetUint64(uint64(readerChain)), db, lggr) - blockHash := common.HexToHash("0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") - blockNumber := int64(42) - blockTimestamp := time.Now() - finalizedBlock := int64(41) // Assuming block 41 is the last finalized block - - // Insert the dummy block - // @mateusz-sekara: this is not inserted in the database, - err = orm.InsertBlock(ctx, blockHash, blockNumber, blockTimestamp, finalizedBlock) - require.NoError(t, err) - lp := logpoller.NewLogPoller( orm, cl, @@ -412,6 +441,7 @@ func testSetup(ctx context.Context, t testing.TB, readerChain cciptypes.ChainSel cl: cl, reader: cr, orm: orm, + db: db, lp: lp, } } @@ -424,5 +454,6 @@ type testSetupData struct { cl client.Client reader types.ContractReader orm logpoller.ORM + db *sqlx.DB lp logpoller.LogPoller } From 5cb19cd502e96f4aeca5208d6b04a5b52077683d Mon Sep 17 00:00:00 2001 From: nogo <0xnogo@gmail.com> Date: Fri, 22 Nov 2024 11:38:39 +0400 Subject: [PATCH 3/4] addressing comment + lint --- .../usdcreader/usdcreader_test.go | 95 ++++++++++--------- 1 file changed, 51 insertions(+), 44 deletions(-) diff --git a/core/capabilities/ccip/ccip_integration_tests/usdcreader/usdcreader_test.go b/core/capabilities/ccip/ccip_integration_tests/usdcreader/usdcreader_test.go index 6bd61111c3f..10ad23b96c6 100644 --- a/core/capabilities/ccip/ccip_integration_tests/usdcreader/usdcreader_test.go +++ b/core/capabilities/ccip/ccip_integration_tests/usdcreader/usdcreader_test.go @@ -207,6 +207,16 @@ func Test_USDCReader_MessageHashes(t *testing.T) { }) } } + +// Benchmark Results: +// Benchmark_MessageHashes/Small_Dataset-14 3723 272421 ns/op 126949 B/op 2508 allocs/op +// Benchmark_MessageHashes/Medium_Dataset-14 196 6164706 ns/op 1501435 B/op 20274 allocs/op +// Benchmark_MessageHashes/Large_Dataset-14 7 163930268 ns/op 37193160 B/op 463954 allocs/op +// +// Notes: +// - Small dataset processes 3,723 iterations with 126KB memory usage per iteration. +// - Medium dataset processes 196 iterations with 1.5MB memory usage per iteration. +// - Large dataset processes only 7 iterations with ~37MB memory usage per iteration. func Benchmark_MessageHashes(b *testing.B) { finalityDepth := 5 @@ -214,7 +224,7 @@ func Benchmark_MessageHashes(b *testing.B) { testCases := []struct { name string msgCount int - startNonce uint64 + startNonce int64 tokenCount int }{ {"Small_Dataset", 100, 1, 5}, @@ -251,8 +261,9 @@ func Benchmark_MessageHashes(b *testing.B) { // Create a map of tokens to query for, with the specified tokenCount tokens := make(map[reader.MessageTokenID]cciptypes.RampTokenAmount) for i := 1; i <= tc.tokenCount; i++ { + //nolint:gosec // disable G115 tokens[reader.NewMessageTokenID(cciptypes.SeqNum(i), 1)] = cciptypes.RampTokenAmount{ - ExtraData: reader.NewSourceTokenDataPayload(tc.startNonce+uint64(i), sourceDomainCCTP).ToBytes(), + ExtraData: reader.NewSourceTokenDataPayload(uint64(tc.startNonce)+uint64(i), sourceDomainCCTP).ToBytes(), } } @@ -272,7 +283,7 @@ func populateDatabase(b *testing.B, source cciptypes.ChainSelector, sourceDomainCCTP uint32, destDomainCCTP uint32, - startNonce uint64, + startNonce int64, numOfMessages int, finalityDepth int) { ctx := testutils.Context(b) @@ -286,42 +297,6 @@ func populateDatabase(b *testing.B, messageTransmitterAddress := testEnv.contractAddr for i := 0; i < numOfMessages; i++ { - nonce := startNonce + uint64(i) - - // Pack the message following exact CCTP format - var buf []byte - // _msgVersion (uint32) - buf = binary.BigEndian.AppendUint32(buf, reader.CCTPMessageVersion) - // _msgSourceDomain (uint32) - buf = binary.BigEndian.AppendUint32(buf, sourceDomainCCTP) - // _msgDestinationDomain (uint32) - buf = binary.BigEndian.AppendUint32(buf, destDomainCCTP) - // _msgNonce (uint64) - buf = binary.BigEndian.AppendUint64(buf, nonce) - // First 12 bytes of the sender address are always empty for EVM - senderBytes := [12]byte{} - buf = append(buf, senderBytes[:]...) - - // Convert to 32 bytes - var message [32]byte - copy(message[:], buf) - - // Encode the data following Solidity's encoding for bytes - data := make([]byte, 0) - - // This says "actual data starts 32 bytes from the beginning" - offsetBytes := make([]byte, 32) - binary.BigEndian.PutUint64(offsetBytes[24:], 32) - data = append(data, offsetBytes...) - - // This is the length of our message - lengthBytes := make([]byte, 32) - binary.BigEndian.PutUint64(lengthBytes[24:], uint64(len(message))) - data = append(data, lengthBytes...) - - // Add message - data = append(data, message[:]...) - // Create topics array with just the event signature topics := [][]byte{ messageSentEventSig[:], // Topic[0] is event signature @@ -329,7 +304,7 @@ func populateDatabase(b *testing.B, // Create log entry logs = append(logs, logpoller.Log{ - EvmChainId: ubig.New(big.NewInt(int64(uint64(source)))), + EvmChainId: ubig.New(new(big.Int).SetUint64(uint64(source))), LogIndex: int64(i + 1), BlockHash: utils.NewHash(), BlockNumber: int64(i + 1), @@ -338,7 +313,7 @@ func populateDatabase(b *testing.B, Topics: topics, Address: messageTransmitterAddress, TxHash: utils.NewHash(), - Data: data, + Data: createMessageSentLogPollerData(startNonce, i, sourceDomainCCTP, destDomainCCTP), CreatedAt: time.Now(), }) } @@ -347,6 +322,39 @@ func populateDatabase(b *testing.B, require.NoError(b, testEnv.orm.InsertBlock(ctx, utils.RandomHash(), int64(numOfMessages+finalityDepth), time.Now(), int64(numOfMessages+finalityDepth))) } +func createMessageSentLogPollerData(startNonce int64, i int, sourceDomainCCTP uint32, destDomainCCTP uint32) []byte { + nonce := int(startNonce) + i + + var buf []byte + + buf = binary.BigEndian.AppendUint32(buf, reader.CCTPMessageVersion) + + buf = binary.BigEndian.AppendUint32(buf, sourceDomainCCTP) + + buf = binary.BigEndian.AppendUint32(buf, destDomainCCTP) + + buf = binary.BigEndian.AppendUint64(buf, uint64(nonce)) + + senderBytes := [12]byte{} + buf = append(buf, senderBytes[:]...) + + var message [32]byte + copy(message[:], buf) + + data := make([]byte, 0) + + offsetBytes := make([]byte, 32) + binary.BigEndian.PutUint64(offsetBytes[24:], 32) + data = append(data, offsetBytes...) + + lengthBytes := make([]byte, 32) + binary.BigEndian.PutUint64(lengthBytes[24:], uint64(len(message))) + data = append(data, lengthBytes...) + + data = append(data, message[:]...) + return data +} + // we might want to use batching (evm/batching or evm/batching) but might be slow func emitMessageSent(t *testing.T, testEnv *testSetupData, source, dest uint32, nonce uint64) { payload := utils.RandomBytes32() @@ -368,16 +376,15 @@ func emitMessageSent(t *testing.T, testEnv *testSetupData, source, dest uint32, func testSetup(ctx context.Context, t testing.TB, readerChain cciptypes.ChainSelector, cfg evmtypes.ChainReaderConfig, depth int, useHeavyDB bool) *testSetupData { // Generate a new key pair for the simulated account privateKey, err := crypto.GenerateKey() - assert.NoError(t, err) + require.NoError(t, err) // Set up the genesis account with balance blnc, ok := big.NewInt(0).SetString("999999999999999999999999999999999999", 10) assert.True(t, ok) alloc := map[common.Address]gethtypes.Account{crypto.PubkeyToAddress(privateKey.PublicKey): {Balance: blnc}} simulatedBackend := simulated.NewBackend(alloc, simulated.WithBlockGasLimit(0)) // Create a transactor - auth, err := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(ChainID)) - assert.NoError(t, err) + require.NoError(t, err) auth.GasLimit = uint64(0) address, _, _, err := usdc_reader_tester.DeployUSDCReaderTester( From 58f224609a06d334114f7fe8c98b37c7e27e97c3 Mon Sep 17 00:00:00 2001 From: nogo <0xnogo@gmail.com> Date: Sat, 23 Nov 2024 08:29:55 +0400 Subject: [PATCH 4/4] more lint fixing --- .../ccip/ccip_integration_tests/usdcreader/usdcreader_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/capabilities/ccip/ccip_integration_tests/usdcreader/usdcreader_test.go b/core/capabilities/ccip/ccip_integration_tests/usdcreader/usdcreader_test.go index 10ad23b96c6..efa4f193ed9 100644 --- a/core/capabilities/ccip/ccip_integration_tests/usdcreader/usdcreader_test.go +++ b/core/capabilities/ccip/ccip_integration_tests/usdcreader/usdcreader_test.go @@ -332,7 +332,7 @@ func createMessageSentLogPollerData(startNonce int64, i int, sourceDomainCCTP ui buf = binary.BigEndian.AppendUint32(buf, sourceDomainCCTP) buf = binary.BigEndian.AppendUint32(buf, destDomainCCTP) - + // #nosec G115 buf = binary.BigEndian.AppendUint64(buf, uint64(nonce)) senderBytes := [12]byte{}