From 47da13e861975ca9a10880cb5861a4ee3756c00b Mon Sep 17 00:00:00 2001 From: "Abdelrahman Soliman (Boda)" <2677789+asoliman92@users.noreply.github.com> Date: Wed, 20 Nov 2024 16:18:14 +0200 Subject: [PATCH] CCIPReader pricing tests (#15264) * Add Test_GetChainFeePriceUpdates (#15240) Add Test_GetChainFeePriceUpdates * Bind real contracts per chain in CCIPReader test (#15282) Add functionality to bind more contracts to different chains (using same simulated backend for simplicity) in ccipreader tester. LinkPriceUSD test GetMedianDataAvailabilityGasConfig * Test_GetWrappedNativeTokenPriceUSD (#15316) Test_GetWrappedNativeTokenPriceUSD * comments --- .../ccipreader/ccipreader_test.go | 387 ++++++++++++++++-- 1 file changed, 352 insertions(+), 35 deletions(-) diff --git a/core/capabilities/ccip/ccip_integration_tests/ccipreader/ccipreader_test.go b/core/capabilities/ccip/ccip_integration_tests/ccipreader/ccipreader_test.go index 150a51f93fb..5f5754acbde 100644 --- a/core/capabilities/ccip/ccip_integration_tests/ccipreader/ccipreader_test.go +++ b/core/capabilities/ccip/ccip_integration_tests/ccipreader/ccipreader_test.go @@ -2,6 +2,7 @@ package ccipreader import ( "context" + "encoding/hex" "math/big" "sort" "testing" @@ -17,23 +18,28 @@ import ( "go.uber.org/zap/zapcore" "golang.org/x/exp/maps" + readermocks "github.com/smartcontractkit/chainlink-ccip/mocks/pkg/contractreader" cciptypes "github.com/smartcontractkit/chainlink-ccip/pkg/types/ccipocr3" + "github.com/smartcontractkit/chainlink-common/pkg/codec" "github.com/smartcontractkit/chainlink-common/pkg/types" "github.com/smartcontractkit/chainlink-common/pkg/utils/tests" + evmconfig "github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/configs/evm" + "github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets" "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/ccip_reader_tester" + "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/fee_quoter" + "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/router" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm" evmtypes "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/types" - readermocks "github.com/smartcontractkit/chainlink-ccip/mocks/pkg/contractreader" "github.com/smartcontractkit/chainlink-ccip/pkg/consts" "github.com/smartcontractkit/chainlink-ccip/pkg/contractreader" ccipreaderpkg "github.com/smartcontractkit/chainlink-ccip/pkg/reader" @@ -47,6 +53,14 @@ const ( chainD = cciptypes.ChainSelector(4) ) +var ( + defaultGasPrice = assets.GWei(10) + InitialLinkPrice = e18Mult(20) + InitialWethPrice = e18Mult(4000) + linkAddress = utils.RandomAddress() + wethAddress = utils.RandomAddress() +) + func TestCCIPReader_CommitReportsGTETimestamp(t *testing.T) { ctx := testutils.Context(t) @@ -67,15 +81,20 @@ func TestCCIPReader_CommitReportsGTETimestamp(t *testing.T) { }, } + sb, auth := setupSimulatedBackendAndAuth(t) onRampAddress := utils.RandomAddress() - s := testSetup(ctx, t, chainD, chainD, nil, cfg, map[cciptypes.ChainSelector][]types.BoundContract{ + s := testSetup(ctx, t, chainD, chainD, nil, cfg, nil, map[cciptypes.ChainSelector][]types.BoundContract{ chainS1: { { Address: onRampAddress.Hex(), Name: consts.ContractNameOnRamp, }, }, - }) + }, + true, + sb, + auth, + ) tokenA := common.HexToAddress("123") const numReports = 5 @@ -189,7 +208,8 @@ func TestCCIPReader_ExecutedMessageRanges(t *testing.T) { }, } - s := testSetup(ctx, t, chainD, chainD, nil, cfg, nil) + sb, auth := setupSimulatedBackendAndAuth(t) + s := testSetup(ctx, t, chainD, chainD, nil, cfg, nil, nil, true, sb, auth) _, err := s.contract.EmitExecutionStateChanged( s.auth, @@ -274,7 +294,8 @@ func TestCCIPReader_MsgsBetweenSeqNums(t *testing.T) { }, } - s := testSetup(ctx, t, chainS1, chainD, nil, cfg, nil) + sb, auth := setupSimulatedBackendAndAuth(t) + s := testSetup(ctx, t, chainS1, chainD, nil, cfg, nil, nil, true, sb, auth) _, err := s.contract.EmitCCIPMessageSent(s.auth, uint64(chainD), ccip_reader_tester.InternalEVM2AnyRampMessage{ Header: ccip_reader_tester.InternalRampMessageHeader{ @@ -375,7 +396,8 @@ func TestCCIPReader_NextSeqNum(t *testing.T) { }, } - s := testSetup(ctx, t, chainD, chainD, onChainSeqNums, cfg, nil) + sb, auth := setupSimulatedBackendAndAuth(t) + s := testSetup(ctx, t, chainD, chainD, onChainSeqNums, cfg, nil, nil, true, sb, auth) seqNums, err := s.reader.NextSeqNum(ctx, []cciptypes.ChainSelector{chainS1, chainS2, chainS3}) assert.NoError(t, err) @@ -402,7 +424,8 @@ func TestCCIPReader_GetExpectedNextSequenceNumber(t *testing.T) { }, } - s := testSetup(ctx, t, chainS1, chainD, nil, cfg, nil) + sb, auth := setupSimulatedBackendAndAuth(t) + s := testSetup(ctx, t, chainS1, chainD, nil, cfg, nil, nil, true, sb, auth) _, err := s.contract.SetDestChainSeqNr(s.auth, uint64(chainD), 10) require.NoError(t, err) @@ -452,7 +475,8 @@ func TestCCIPReader_Nonces(t *testing.T) { }, } - s := testSetup(ctx, t, chainD, chainD, nil, cfg, nil) + sb, auth := setupSimulatedBackendAndAuth(t) + s := testSetup(ctx, t, chainD, chainD, nil, cfg, nil, nil, true, sb, auth) // Add some nonces. for chain, addrs := range nonces { @@ -479,6 +503,276 @@ func TestCCIPReader_Nonces(t *testing.T) { } } +func Test_GetChainFeePriceUpdates(t *testing.T) { + ctx := testutils.Context(t) + sb, auth := setupSimulatedBackendAndAuth(t) + feeQuoter := deployFeeQuoterWithPrices(t, auth, sb, chainS1) + + s := testSetup(ctx, t, chainD, chainD, nil, evmconfig.DestReaderConfig, + map[cciptypes.ChainSelector][]types.BoundContract{ + chainD: { + { + Address: feeQuoter.Address().String(), + Name: consts.ContractNameFeeQuoter, + }, + }, + }, + nil, + false, + sb, + auth, + ) + + updates := s.reader.GetChainFeePriceUpdate(ctx, []cciptypes.ChainSelector{chainS1, chainS2}) + // only chainS1 has a bound contract + require.Len(t, updates, 1) + require.Equal(t, defaultGasPrice.ToInt(), updates[chainS1].Value.Int) +} + +func Test_LinkPriceUSD(t *testing.T) { + ctx := testutils.Context(t) + sb, auth := setupSimulatedBackendAndAuth(t) + feeQuoter := deployFeeQuoterWithPrices(t, auth, sb, chainS1) + + s := testSetup(ctx, t, chainD, chainD, nil, evmconfig.DestReaderConfig, + map[cciptypes.ChainSelector][]types.BoundContract{ + chainD: { + { + Address: feeQuoter.Address().String(), + Name: consts.ContractNameFeeQuoter, + }, + }, + }, + nil, + false, + sb, + auth, + ) + + linkPriceUSD, err := s.reader.LinkPriceUSD(ctx) + require.NoError(t, err) + require.NotNil(t, linkPriceUSD.Int) + require.Equal(t, InitialLinkPrice, linkPriceUSD.Int) +} + +func Test_GetMedianDataAvailabilityGasConfig(t *testing.T) { + ctx := testutils.Context(t) + + sb, auth := setupSimulatedBackendAndAuth(t) + + // All fee quoters using same auth and simulated backend for simplicity + feeQuoter1 := deployFeeQuoterWithPrices(t, auth, sb, chainD) + feeQuoter2 := deployFeeQuoterWithPrices(t, auth, sb, chainD) + feeQuoter3 := deployFeeQuoterWithPrices(t, auth, sb, chainD) + feeQuoters := []*fee_quoter.FeeQuoter{feeQuoter1, feeQuoter2, feeQuoter3} + + // Update the dest chain config for each fee quoter + for i, fq := range feeQuoters { + destChainCfg := defaultFeeQuoterDestChainConfig() + //nolint:gosec // disable G115 + destChainCfg.DestDataAvailabilityOverheadGas = uint32(100 + i) + //nolint:gosec // disable G115 + destChainCfg.DestGasPerDataAvailabilityByte = uint16(200 + i) + //nolint:gosec // disable G115 + destChainCfg.DestDataAvailabilityMultiplierBps = uint16(1 + i) + _, err := fq.ApplyDestChainConfigUpdates(auth, []fee_quoter.FeeQuoterDestChainConfigArgs{ + { + DestChainSelector: uint64(chainD), + DestChainConfig: destChainCfg, + }, + }) + sb.Commit() + require.NoError(t, err) + } + + s := testSetup(ctx, t, chainD, chainD, nil, evmconfig.DestReaderConfig, map[cciptypes.ChainSelector][]types.BoundContract{ + chainS1: { + { + Address: feeQuoter1.Address().String(), + Name: consts.ContractNameFeeQuoter, + }, + }, + chainS2: { + { + Address: feeQuoter2.Address().String(), + Name: consts.ContractNameFeeQuoter, + }, + }, + chainS3: { + { + Address: feeQuoter3.Address().String(), + Name: consts.ContractNameFeeQuoter, + }, + }, + }, nil, + false, + sb, + auth, + ) + + daConfig, err := s.reader.GetMedianDataAvailabilityGasConfig(ctx) + require.NoError(t, err) + + // Verify the results + require.Equal(t, uint32(101), daConfig.DestDataAvailabilityOverheadGas) + require.Equal(t, uint16(201), daConfig.DestGasPerDataAvailabilityByte) + require.Equal(t, uint16(2), daConfig.DestDataAvailabilityMultiplierBps) +} + +func Test_GetWrappedNativeTokenPriceUSD(t *testing.T) { + ctx := testutils.Context(t) + sb, auth := setupSimulatedBackendAndAuth(t) + feeQuoter := deployFeeQuoterWithPrices(t, auth, sb, chainS1) + + // Mock the routerContract to return a native token address + routerContract := deployRouterWithNativeToken(t, auth, sb) + + s := testSetup(ctx, t, chainD, chainD, nil, evmconfig.DestReaderConfig, + map[cciptypes.ChainSelector][]types.BoundContract{ + chainD: { + { + Address: feeQuoter.Address().String(), + Name: consts.ContractNameFeeQuoter, + }, + { + Address: routerContract.Address().String(), + Name: consts.ContractNameRouter, + }, + }, + }, + nil, + false, + sb, + auth, + ) + + prices := s.reader.GetWrappedNativeTokenPriceUSD(ctx, []cciptypes.ChainSelector{chainD, chainS1}) + + // Only chainD has reader contracts bound + require.Len(t, prices, 1) + require.Equal(t, InitialWethPrice, prices[chainD].Int) +} + +func deployRouterWithNativeToken(t *testing.T, auth *bind.TransactOpts, sb *simulated.Backend) *router.Router { + address, _, _, err := router.DeployRouter( + auth, + sb.Client(), + wethAddress, + utils.RandomAddress(), // armProxy address + ) + require.NoError(t, err) + sb.Commit() + + routerContract, err := router.NewRouter(address, sb.Client()) + require.NoError(t, err) + + return routerContract +} + +func deployFeeQuoterWithPrices(t *testing.T, auth *bind.TransactOpts, sb *simulated.Backend, destChain cciptypes.ChainSelector) *fee_quoter.FeeQuoter { + address, _, _, err := fee_quoter.DeployFeeQuoter( + auth, + sb.Client(), + fee_quoter.FeeQuoterStaticConfig{ + MaxFeeJuelsPerMsg: big.NewInt(0).Mul(big.NewInt(2e2), big.NewInt(1e18)), + LinkToken: linkAddress, + TokenPriceStalenessThreshold: uint32(24 * 60 * 60), + }, + []common.Address{auth.From}, + []common.Address{wethAddress, linkAddress}, + []fee_quoter.FeeQuoterTokenPriceFeedUpdate{}, + []fee_quoter.FeeQuoterTokenTransferFeeConfigArgs{}, + []fee_quoter.FeeQuoterPremiumMultiplierWeiPerEthArgs{}, + []fee_quoter.FeeQuoterDestChainConfigArgs{ + { + + DestChainSelector: uint64(destChain), + DestChainConfig: defaultFeeQuoterDestChainConfig(), + }, + }, + ) + + require.NoError(t, err) + sb.Commit() + + feeQuoter, err := fee_quoter.NewFeeQuoter(address, sb.Client()) + require.NoError(t, err) + + _, err = feeQuoter.UpdatePrices( + auth, fee_quoter.InternalPriceUpdates{ + GasPriceUpdates: []fee_quoter.InternalGasPriceUpdate{ + { + DestChainSelector: uint64(chainS1), + UsdPerUnitGas: defaultGasPrice.ToInt(), + }, + }, + TokenPriceUpdates: []fee_quoter.InternalTokenPriceUpdate{ + { + SourceToken: linkAddress, + UsdPerToken: InitialLinkPrice, + }, + { + SourceToken: wethAddress, + UsdPerToken: InitialWethPrice, + }, + }, + }, + ) + require.NoError(t, err) + sb.Commit() + + gas, err := feeQuoter.GetDestinationChainGasPrice(&bind.CallOpts{}, uint64(chainS1)) + require.NoError(t, err) + require.Equal(t, defaultGasPrice.ToInt(), gas.Value) + + return feeQuoter +} + +func defaultFeeQuoterDestChainConfig() fee_quoter.FeeQuoterDestChainConfig { + // https://github.com/smartcontractkit/ccip/blob/c4856b64bd766f1ddbaf5d13b42d3c4b12efde3a/contracts/src/v0.8/ccip/libraries/Internal.sol#L337-L337 + /* + ```Solidity + // bytes4(keccak256("CCIP ChainFamilySelector EVM")) + bytes4 public constant CHAIN_FAMILY_SELECTOR_EVM = 0x2812d52c; + ``` + */ + evmFamilySelector, _ := hex.DecodeString("2812d52c") + return fee_quoter.FeeQuoterDestChainConfig{ + IsEnabled: true, + MaxNumberOfTokensPerMsg: 10, + MaxDataBytes: 256, + MaxPerMsgGasLimit: 3_000_000, + DestGasOverhead: 50_000, + DefaultTokenFeeUSDCents: 1, + DestGasPerPayloadByte: 10, + DestDataAvailabilityOverheadGas: 100, + DestGasPerDataAvailabilityByte: 100, + DestDataAvailabilityMultiplierBps: 1, + DefaultTokenDestGasOverhead: 125_000, + DefaultTxGasLimit: 200_000, + GasMultiplierWeiPerEth: 1, + NetworkFeeUSDCents: 1, + ChainFamilySelector: [4]byte(evmFamilySelector), + } +} + +func setupSimulatedBackendAndAuth(t *testing.T) (*simulated.Backend, *bind.TransactOpts) { + privateKey, err := crypto.GenerateKey() + require.NoError(t, err) + + blnc, ok := big.NewInt(0).SetString("999999999999999999999999999999999999", 10) + require.True(t, ok) + + alloc := map[common.Address]ethtypes.Account{crypto.PubkeyToAddress(privateKey.PublicKey): {Balance: blnc}} + simulatedBackend := simulated.NewBackend(alloc, simulated.WithBlockGasLimit(8000000)) + + auth, err := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(1337)) + require.NoError(t, err) + auth.GasLimit = uint64(6000000) + + return simulatedBackend, auth +} + func testSetup( ctx context.Context, t *testing.T, @@ -486,25 +780,12 @@ func testSetup( destChain cciptypes.ChainSelector, onChainSeqNums map[cciptypes.ChainSelector]cciptypes.SeqNum, cfg evmtypes.ChainReaderConfig, - otherBindings map[cciptypes.ChainSelector][]types.BoundContract, + toBindContracts map[cciptypes.ChainSelector][]types.BoundContract, + toMockBindings map[cciptypes.ChainSelector][]types.BoundContract, + bindTester bool, + simulatedBackend *simulated.Backend, + auth *bind.TransactOpts, ) *testSetupData { - const chainID = 1337 - - // Generate a new key pair for the simulated account - privateKey, err := crypto.GenerateKey() - assert.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]ethtypes.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) - auth.GasLimit = uint64(0) - - // Deploy the contract address, _, _, err := ccip_reader_tester.DeployCCIPReaderTester(auth, simulatedBackend.Client()) assert.NoError(t, err) simulatedBackend.Commit() @@ -547,21 +828,47 @@ func testSetup( } contractNames := maps.Keys(cfg.Contracts) - assert.Len(t, contractNames, 1, "test setup assumes there is only one contract") cr, err := evm.NewChainReaderService(ctx, lggr, lp, headTracker, cl, cfg) require.NoError(t, err) extendedCr := contractreader.NewExtendedContractReader(cr) - err = extendedCr.Bind(ctx, []types.BoundContract{ - { - Address: address.String(), - Name: contractNames[0], - }, - }) - require.NoError(t, err) + + if bindTester { + err = extendedCr.Bind(ctx, []types.BoundContract{ + { + Address: address.String(), + Name: contractNames[0], + }, + }) + require.NoError(t, err) + } + var otherCrs = make(map[cciptypes.ChainSelector]contractreader.Extended) - for chain, bindings := range otherBindings { + for chain, bindings := range toBindContracts { + cl2 := client.NewSimulatedBackendClient(t, simulatedBackend, big.NewInt(0).SetUint64(uint64(chain))) + headTracker2 := headtracker.NewSimulatedHeadTracker(cl2, lpOpts.UseFinalityTag, lpOpts.FinalityDepth) + lp2 := logpoller.NewLogPoller(logpoller.NewORM(big.NewInt(0).SetUint64(uint64(chain)), db, lggr), + cl2, + lggr, + headTracker2, + lpOpts, + ) + require.NoError(t, lp2.Start(ctx)) + + cr2, err2 := evm.NewChainReaderService(ctx, lggr, lp2, headTracker2, cl2, cfg) + require.NoError(t, err2) + + extendedCr2 := contractreader.NewExtendedContractReader(cr2) + err2 = extendedCr2.Bind(ctx, bindings) + require.NoError(t, err2) + otherCrs[chain] = extendedCr2 + } + + for chain, bindings := range toMockBindings { + if _, ok := otherCrs[chain]; ok { + require.False(t, ok, "chain %d already exists", chain) + } m := readermocks.NewMockContractReaderFacade(t) m.EXPECT().Bind(ctx, bindings).Return(nil) ecr := contractreader.NewExtendedContractReader(m) @@ -594,6 +901,7 @@ func testSetup( lp: lp, cl: cl, reader: reader, + extendedCR: extendedCr, } } @@ -605,4 +913,13 @@ type testSetupData struct { lp logpoller.LogPoller cl client.Client reader ccipreaderpkg.CCIPReader + extendedCR contractreader.Extended +} + +func uBigInt(i uint64) *big.Int { + return new(big.Int).SetUint64(i) +} + +func e18Mult(amount uint64) *big.Int { + return new(big.Int).Mul(uBigInt(amount), uBigInt(1e18)) }