From 06531e503bb0ea0b89eeca54af1122b0bfa10be1 Mon Sep 17 00:00:00 2001 From: dimkouv Date: Thu, 15 Feb 2024 17:13:55 +0100 Subject: [PATCH 1/2] use 'latest' tag instead of using the lp --- .../batchreader/token_pool_batch_reader.go | 14 ++++---------- .../batchreader/token_pool_batch_reader_test.go | 15 ++------------- .../ccip/internal/ccipdata/v1_0_0/offramp.go | 14 ++------------ .../ccipdata/v1_0_0/offramp_reader_unit_test.go | 2 -- .../internal/ccipdata/v1_0_0/price_registry.go | 7 +------ .../ocr2/plugins/ccip/internal/pricegetter/evm.go | 9 +-------- .../plugins/ccip/internal/pricegetter/evm_test.go | 2 +- .../ocr2/plugins/ccip/internal/rpclib/evm.go | 7 +++++-- 8 files changed, 16 insertions(+), 54 deletions(-) diff --git a/core/services/ocr2/plugins/ccip/internal/ccipdata/batchreader/token_pool_batch_reader.go b/core/services/ocr2/plugins/ccip/internal/ccipdata/batchreader/token_pool_batch_reader.go index a3a7ee5e44..7be68ae958 100644 --- a/core/services/ocr2/plugins/ccip/internal/ccipdata/batchreader/token_pool_batch_reader.go +++ b/core/services/ocr2/plugins/ccip/internal/ccipdata/batchreader/token_pool_batch_reader.go @@ -18,7 +18,6 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipdata/v1_2_0" "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipdata/v1_4_0" "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/rpclib" - "github.com/smartcontractkit/chainlink/v2/core/services/pg" ) var ( @@ -92,7 +91,7 @@ func (br *EVMTokenPoolBatchedReader) GetInboundTokenPoolRateLimits(ctx context.C } } - return batchCallLatestBlockNumber[cciptypes.TokenBucketRateLimit](ctx, br.lp, br.evmBatchCaller, evmCalls) + return batchCallLatestBlockNumber[cciptypes.TokenBucketRateLimit](ctx, br.evmBatchCaller, evmCalls) } // loadTokenPoolReaders loads the token pools into the factory's cache @@ -153,16 +152,11 @@ func getBatchedTypeAndVersion(ctx context.Context, lp logpoller.LogPoller, evmBa )) } - return batchCallLatestBlockNumber[string](ctx, lp, evmBatchCaller, evmCalls) + return batchCallLatestBlockNumber[string](ctx, evmBatchCaller, evmCalls) } -func batchCallLatestBlockNumber[T any](ctx context.Context, lp logpoller.LogPoller, evmBatchCaller rpclib.EvmBatchCaller, evmCalls []rpclib.EvmCall) ([]T, error) { - latestBlock, err := lp.LatestBlock(pg.WithParentCtx(ctx)) - if err != nil { - return nil, fmt.Errorf("get latest block: %w", err) - } - - results, err := evmBatchCaller.BatchCall(ctx, uint64(latestBlock.BlockNumber), evmCalls) +func batchCallLatestBlockNumber[T any](ctx context.Context, evmBatchCaller rpclib.EvmBatchCaller, evmCalls []rpclib.EvmCall) ([]T, error) { + results, err := evmBatchCaller.BatchCall(ctx, 0, evmCalls) if err != nil { return nil, fmt.Errorf("batch call limit: %w", err) } diff --git a/core/services/ocr2/plugins/ccip/internal/ccipdata/batchreader/token_pool_batch_reader_test.go b/core/services/ocr2/plugins/ccip/internal/ccipdata/batchreader/token_pool_batch_reader_test.go index 03805d1ba3..1412809d31 100644 --- a/core/services/ocr2/plugins/ccip/internal/ccipdata/batchreader/token_pool_batch_reader_test.go +++ b/core/services/ocr2/plugins/ccip/internal/ccipdata/batchreader/token_pool_batch_reader_test.go @@ -4,13 +4,11 @@ import ( "context" "math/big" "testing" - "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" - "github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller" mocks2 "github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller/mocks" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils" "github.com/smartcontractkit/chainlink/v2/core/logger" @@ -22,18 +20,9 @@ import ( ) func TestTokenPoolFactory(t *testing.T) { - latestBlock := logpoller.LogPollerBlock{ - BlockNumber: 1231230, - BlockTimestamp: time.Now(), - FinalizedBlockNumber: 1231231, - CreatedAt: time.Now(), - } - lggr := logger.TestLogger(t) offRamp := utils.RandomAddress() lp := mocks2.NewLogPoller(t) - lp.On("LatestBlock", mock.Anything).Return(latestBlock, nil) - ctx := context.Background() remoteChainSelector := uint64(2000) batchCallerMock := rpclibmocks.NewEvmBatchCaller(t) @@ -64,8 +53,8 @@ func TestTokenPoolFactory(t *testing.T) { }) } - batchCallerMock.On("BatchCall", ctx, uint64(latestBlock.BlockNumber), mock.Anything).Return(batchCallResult, nil).Once() - batchCallerMock.On("BatchCall", ctx, uint64(latestBlock.BlockNumber), mock.Anything).Return([]rpclib.DataAndErr{{ + batchCallerMock.On("BatchCall", ctx, uint64(0), mock.Anything).Return(batchCallResult, nil).Once() + batchCallerMock.On("BatchCall", ctx, uint64(0), mock.Anything).Return([]rpclib.DataAndErr{{ Outputs: []any{rateLimits}, Err: nil, }, { diff --git a/core/services/ocr2/plugins/ccip/internal/ccipdata/v1_0_0/offramp.go b/core/services/ocr2/plugins/ccip/internal/ccipdata/v1_0_0/offramp.go index ef4c02a757..a9c494dd66 100644 --- a/core/services/ocr2/plugins/ccip/internal/ccipdata/v1_0_0/offramp.go +++ b/core/services/ocr2/plugins/ccip/internal/ccipdata/v1_0_0/offramp.go @@ -245,12 +245,7 @@ func (o *OffRamp) getDestinationTokensFromSourceTokens(ctx context.Context, toke } } - latestBlock, err := o.lp.LatestBlock(pg.WithParentCtx(ctx)) - if err != nil { - return nil, fmt.Errorf("get latest block: %w", err) - } - - results, err := o.evmBatchCaller.BatchCall(ctx, uint64(latestBlock.BlockNumber), evmCalls) + results, err := o.evmBatchCaller.BatchCall(ctx, 0, evmCalls) if err != nil { return nil, fmt.Errorf("batch call limit: %w", err) } @@ -339,12 +334,7 @@ func (o *OffRamp) getPoolsByDestTokens(ctx context.Context, tokenAddrs []common. )) } - latestBlock, err := o.lp.LatestBlock(pg.WithParentCtx(ctx)) - if err != nil { - return nil, fmt.Errorf("get latest block: %w", err) - } - - results, err := o.evmBatchCaller.BatchCall(ctx, uint64(latestBlock.FinalizedBlockNumber), evmCalls) + results, err := o.evmBatchCaller.BatchCall(ctx, 0, evmCalls) if err != nil { return nil, fmt.Errorf("batch call limit: %w", err) } diff --git a/core/services/ocr2/plugins/ccip/internal/ccipdata/v1_0_0/offramp_reader_unit_test.go b/core/services/ocr2/plugins/ccip/internal/ccipdata/v1_0_0/offramp_reader_unit_test.go index db3b4d3f8e..c1ed079964 100644 --- a/core/services/ocr2/plugins/ccip/internal/ccipdata/v1_0_0/offramp_reader_unit_test.go +++ b/core/services/ocr2/plugins/ccip/internal/ccipdata/v1_0_0/offramp_reader_unit_test.go @@ -73,8 +73,6 @@ func TestOffRampGetDestinationTokensFromSourceTokens(t *testing.T) { } lp := mocks.NewLogPoller(t) - lp.On("LatestBlock", mock.Anything).Return(logpoller.LogPollerBlock{BlockNumber: rand.Int63()}, nil) - for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { batchCaller := rpclibmocks.NewEvmBatchCaller(t) diff --git a/core/services/ocr2/plugins/ccip/internal/ccipdata/v1_0_0/price_registry.go b/core/services/ocr2/plugins/ccip/internal/ccipdata/v1_0_0/price_registry.go index 3f6424098d..73a42b5676 100644 --- a/core/services/ocr2/plugins/ccip/internal/ccipdata/v1_0_0/price_registry.go +++ b/core/services/ocr2/plugins/ccip/internal/ccipdata/v1_0_0/price_registry.go @@ -263,12 +263,7 @@ func (p *PriceRegistry) GetTokensDecimals(ctx context.Context, tokenAddresses [] } } - latestBlock, err := p.lp.LatestBlock(pg.WithParentCtx(ctx)) - if err != nil { - return nil, fmt.Errorf("get latest block: %w", err) - } - - results, err := p.evmBatchCaller.BatchCall(ctx, uint64(latestBlock.BlockNumber), evmCalls) + results, err := p.evmBatchCaller.BatchCall(ctx, 0, evmCalls) if err != nil { return nil, fmt.Errorf("batch call limit: %w", err) } diff --git a/core/services/ocr2/plugins/ccip/internal/pricegetter/evm.go b/core/services/ocr2/plugins/ccip/internal/pricegetter/evm.go index a6ae627cc3..d1046d047f 100644 --- a/core/services/ocr2/plugins/ccip/internal/pricegetter/evm.go +++ b/core/services/ocr2/plugins/ccip/internal/pricegetter/evm.go @@ -16,7 +16,6 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/config" "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipcalc" "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/rpclib" - "github.com/smartcontractkit/chainlink/v2/core/services/pg" ) const latestRoundDataMethodName = "latestRoundData" @@ -116,15 +115,9 @@ func (d *DynamicPriceGetter) TokenPricesUSD(ctx context.Context, tokens []ccipty } evmCaller := client.BatchCaller - lp := client.LP - tokensOrder := batchCallsTokensOrder[chainID] - latestBlock, err := lp.LatestBlock(pg.WithParentCtx(ctx)) - if err != nil { - return nil, fmt.Errorf("get latest block: %w", err) - } - resultsPerChain, err := evmCaller.BatchCall(ctx, uint64(latestBlock.BlockNumber), batchCalls) + resultsPerChain, err := evmCaller.BatchCall(ctx, 0, batchCalls) if err != nil { return nil, fmt.Errorf("batch call: %w", err) } diff --git a/core/services/ocr2/plugins/ccip/internal/pricegetter/evm_test.go b/core/services/ocr2/plugins/ccip/internal/pricegetter/evm_test.go index 9de3f0e7ea..a7fcaae162 100644 --- a/core/services/ocr2/plugins/ccip/internal/pricegetter/evm_test.go +++ b/core/services/ocr2/plugins/ccip/internal/pricegetter/evm_test.go @@ -349,7 +349,7 @@ func mockClientFromRound(t *testing.T, round aggregator_v3_interface.LatestRound func mockCallerFromRound(t *testing.T, round aggregator_v3_interface.LatestRoundData) *rpclibmocks.EvmBatchCaller { caller := rpclibmocks.NewEvmBatchCaller(t) - caller.On("BatchCall", mock.Anything, round.RoundId.Uint64(), mock.Anything).Return( + caller.On("BatchCall", mock.Anything, uint64(0), mock.Anything).Return( []rpclib.DataAndErr{ { Outputs: []any{round.RoundId, round.Answer, round.StartedAt, round.UpdatedAt, round.AnsweredInRound}, diff --git a/core/services/ocr2/plugins/ccip/internal/rpclib/evm.go b/core/services/ocr2/plugins/ccip/internal/rpclib/evm.go index 989b9158ac..6e2e94d63d 100644 --- a/core/services/ocr2/plugins/ccip/internal/rpclib/evm.go +++ b/core/services/ocr2/plugins/ccip/internal/rpclib/evm.go @@ -92,7 +92,10 @@ func (c *defaultEvmBatchCaller) batchCall(ctx context.Context, blockNumber uint6 return nil, fmt.Errorf("pack %s(%+v): %w", call.methodName, call.args, err) } - bn := big.NewInt(0).SetUint64(blockNumber) + blockNumStr := "latest" + if blockNumber > 0 { + blockNumStr = hexutil.EncodeBig(big.NewInt(0).SetUint64(blockNumber)) + } rpcBatchCalls[i] = rpc.BatchElem{ Method: "eth_call", @@ -102,7 +105,7 @@ func (c *defaultEvmBatchCaller) batchCall(ctx context.Context, blockNumber uint6 "to": call.contractAddress, "data": hexutil.Bytes(packedInputs), }, - hexutil.EncodeBig(bn), + blockNumStr, }, Result: &packedOutputs[i], } From 1a565abfcfb6b08f5f694508aad43acccf665df3 Mon Sep 17 00:00:00 2001 From: dimkouv Date: Thu, 15 Feb 2024 17:22:25 +0100 Subject: [PATCH 2/2] add doc --- core/services/ocr2/plugins/ccip/internal/rpclib/evm.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/services/ocr2/plugins/ccip/internal/rpclib/evm.go b/core/services/ocr2/plugins/ccip/internal/rpclib/evm.go index 6e2e94d63d..d5e2e32713 100644 --- a/core/services/ocr2/plugins/ccip/internal/rpclib/evm.go +++ b/core/services/ocr2/plugins/ccip/internal/rpclib/evm.go @@ -18,7 +18,7 @@ import ( //go:generate mockery --quiet --name EvmBatchCaller --output ./rpclibmocks --outpkg rpclibmocks --filename evm_mock.go --case=underscore type EvmBatchCaller interface { // BatchCall executes all the provided EvmCall and returns the results in the same order - // of the calls. + // of the calls. Pass blockNumber=0 to use the latest block. BatchCall(ctx context.Context, blockNumber uint64, calls []EvmCall) ([]DataAndErr, error) }