diff --git a/core/capabilities/ccip/ccipevm/gas_helpers.go b/core/capabilities/ccip/ccipevm/gas_helpers.go new file mode 100644 index 0000000000..41acb2a15f --- /dev/null +++ b/core/capabilities/ccip/ccipevm/gas_helpers.go @@ -0,0 +1,89 @@ +package ccipevm + +import ( + "math" + + cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3" +) + +const ( + EvmAddressLengthBytes = 20 + EvmWordBytes = 32 + CalldataGasPerByte = 16 + TokenAdminRegistryWarmupCost = 2_500 + TokenAdminRegistryPoolLookupGas = 100 + // WARM_ACCESS_COST TokenAdminRegistry + 700 + // CALL cost for TokenAdminRegistry + 2_100 // COLD_SLOAD_COST loading the pool address + SupportsInterfaceCheck = 2600 + // because the receiver will be untouched initially + 30_000*3 // supportsInterface of ERC165Checker library performs 3 static-calls of 30k gas each + PerTokenOverheadGas = TokenAdminRegistryPoolLookupGas + + SupportsInterfaceCheck + + 200_000 + // releaseOrMint using callWithExactGas + 50_000 // transfer using callWithExactGas + RateLimiterOverheadGas = 2_100 + // COLD_SLOAD_COST for accessing token bucket + 5_000 // SSTORE_RESET_GAS for updating & decreasing token bucket + ConstantMessagePartBytes = 10 * 32 // A message consists of 10 abi encoded fields 32B each (after encoding) + ExecutionStateProcessingOverheadGas = 2_100 + // COLD_SLOAD_COST for first reading the state + 20_000 + // SSTORE_SET_GAS for writing from 0 (untouched) to non-zero (in-progress) + 100 //# SLOAD_GAS = WARM_STORAGE_READ_COST for rewriting from non-zero (in-progress) to non-zero (success/failure) +) + +func NewGasEstimateProvider() EstimateProvider { + return EstimateProvider{} +} + +type EstimateProvider struct { +} + +// CalculateMerkleTreeGas estimates the merkle tree gas based on number of requests +func (gp EstimateProvider) CalculateMerkleTreeGas(numRequests int) uint64 { + if numRequests == 0 { + return 0 + } + merkleProofBytes := (math.Ceil(math.Log2(float64(numRequests))))*32 + (1+2)*32 // only ever one outer root hash + return uint64(merkleProofBytes * CalldataGasPerByte) +} + +// return the size of bytes for msg tokens +func bytesForMsgTokens(numTokens int) int { + // token address (address) + token amount (uint256) + return (EvmAddressLengthBytes + EvmWordBytes) * numTokens +} + +// CalculateMessageMaxGas computes the maximum gas overhead for a message. +func (gp EstimateProvider) CalculateMessageMaxGas(msg cciptypes.Message) uint64 { + numTokens := len(msg.TokenAmounts) + var data []byte = msg.Data + dataLength := len(data) + + // TODO: update interface to return error? + // Although this decoding should never fail. + messageGasLimit, err := decodeExtraArgsV1V2(msg.ExtraArgs) + if err != nil { + panic(err) + } + + messageBytes := ConstantMessagePartBytes + + bytesForMsgTokens(numTokens) + + dataLength + + messageCallDataGas := uint64(messageBytes * CalldataGasPerByte) + + // Rate limiter only limits value in tokens. It's not called if there are no + // tokens in the message. The same goes for the admin registry, it's only loaded + // if there are tokens, and it's only loaded once. + rateLimiterOverhead := uint64(0) + adminRegistryOverhead := uint64(0) + if numTokens >= 1 { + rateLimiterOverhead = RateLimiterOverheadGas + adminRegistryOverhead = TokenAdminRegistryWarmupCost + } + + return messageGasLimit.Uint64() + + messageCallDataGas + + ExecutionStateProcessingOverheadGas + + SupportsInterfaceCheck + + adminRegistryOverhead + + rateLimiterOverhead + + PerTokenOverheadGas*uint64(numTokens) +} diff --git a/core/capabilities/ccip/oraclecreator/inprocess.go b/core/capabilities/ccip/oraclecreator/inprocess.go index 6616d35675..1088f31466 100644 --- a/core/capabilities/ccip/oraclecreator/inprocess.go +++ b/core/capabilities/ccip/oraclecreator/inprocess.go @@ -8,6 +8,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/ccipevm" evmconfig "github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/configs/evm" "github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/ocrimpls" + "github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/superfakes" cctypes "github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/types" "github.com/ethereum/go-ethereum/common" @@ -310,6 +311,8 @@ func (i *inprocessOracleCreator) CreatePluginOracle(pluginType cctypes.PluginTyp ccipevm.NewExecutePluginCodecV1(), ccipevm.NewMessageHasherV1(), i.homeChainReader, + ccipevm.NewGasEstimateProvider(), // TODO: this works for evm only, how about non-evm? + superfakes.NewNilTokenDataReader(), contractReaders, chainWriters, ) diff --git a/core/capabilities/ccip/superfakes/token_data_reader.go b/core/capabilities/ccip/superfakes/token_data_reader.go new file mode 100644 index 0000000000..ff6a88076c --- /dev/null +++ b/core/capabilities/ccip/superfakes/token_data_reader.go @@ -0,0 +1,23 @@ +package superfakes + +import ( + "context" + + "github.com/smartcontractkit/chainlink-ccip/execute/exectypes" + "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3" +) + +// NewNilTokenDataReader returns a new nilTokenDataReader. +// This token data reader always returns nil for the token data. +func NewNilTokenDataReader() exectypes.TokenDataReader { + return &nilTokenDataReader{} +} + +type nilTokenDataReader struct{} + +// ReadTokenData implements exectypes.TokenDataReader. +func (t *nilTokenDataReader) ReadTokenData(ctx context.Context, srcChain ccipocr3.ChainSelector, num ccipocr3.SeqNum) (r [][]byte, err error) { + return nil, nil +} + +var _ exectypes.TokenDataReader = (*nilTokenDataReader)(nil) diff --git a/core/scripts/go.mod b/core/scripts/go.mod index 52fd0e57f3..63fbdc30af 100644 --- a/core/scripts/go.mod +++ b/core/scripts/go.mod @@ -274,7 +274,7 @@ require ( github.com/sethvargo/go-retry v0.2.4 // indirect github.com/shirou/gopsutil v3.21.11+incompatible // indirect github.com/shirou/gopsutil/v3 v3.24.3 // indirect - github.com/smartcontractkit/chainlink-ccip v0.0.0-20240806142433-6ae40054db92 // indirect + github.com/smartcontractkit/chainlink-ccip v0.0.0-20240812095514-98c545c89255 // indirect github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20240710121324-3ed288aa9b45 // indirect github.com/smartcontractkit/chainlink-data-streams v0.0.0-20240718160222-2dc0c8136bfa // indirect github.com/smartcontractkit/chainlink-feeds v0.0.0-20240710170203-5b41615da827 // indirect diff --git a/core/scripts/go.sum b/core/scripts/go.sum index d32e7ddd71..a58f204ebb 100644 --- a/core/scripts/go.sum +++ b/core/scripts/go.sum @@ -1070,8 +1070,8 @@ github.com/smartcontractkit/chain-selectors v1.0.21 h1:KCR9SA7PhOexaBzFieHoLv1Wo github.com/smartcontractkit/chain-selectors v1.0.21/go.mod h1:d4Hi+E1zqjy9HqMkjBE5q1vcG9VGgxf5VxiRHfzi2kE= github.com/smartcontractkit/chainlink-automation v1.0.4 h1:iyW181JjKHLNMnDleI8umfIfVVlwC7+n5izbLSFgjw8= github.com/smartcontractkit/chainlink-automation v1.0.4/go.mod h1:u4NbPZKJ5XiayfKHD/v3z3iflQWqvtdhj13jVZXj/cM= -github.com/smartcontractkit/chainlink-ccip v0.0.0-20240806142433-6ae40054db92 h1:KKqMibl8CcKSyrSkVVfgGEOuC6Cn3vFQK8yMQA6FpxA= -github.com/smartcontractkit/chainlink-ccip v0.0.0-20240806142433-6ae40054db92/go.mod h1:wl0SRmyJ2ARxz+4Eho6nEXsmWFXxTSYpvcb30AxwKyE= +github.com/smartcontractkit/chainlink-ccip v0.0.0-20240812095514-98c545c89255 h1:jomg9y16IB1yAeGJgxOUUzwMrBswcf+/jprkSv02Go0= +github.com/smartcontractkit/chainlink-ccip v0.0.0-20240812095514-98c545c89255/go.mod h1:/ZWraCBaDDgaIN1prixYcbVvIk/6HeED9+8zbWQ+TMo= github.com/smartcontractkit/chainlink-common v0.2.1-0.20240717132349-ee5af9b79834 h1:pTf4xdcmiWBqWZ6rTy2RMTDBzhHk89VC1pM7jXKQztI= github.com/smartcontractkit/chainlink-common v0.2.1-0.20240717132349-ee5af9b79834/go.mod h1:fh9eBbrReCmv31bfz52ENCAMa7nTKQbdhb2B3+S2VGo= github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20240710121324-3ed288aa9b45 h1:NBQLtqk8zsyY4qTJs+NElI3aDFTcAo83JHvqD04EvB0= diff --git a/go.mod b/go.mod index efc4f413d3..2d556efea7 100644 --- a/go.mod +++ b/go.mod @@ -74,7 +74,7 @@ require ( github.com/shopspring/decimal v1.4.0 github.com/smartcontractkit/chain-selectors v1.0.21 github.com/smartcontractkit/chainlink-automation v1.0.4 - github.com/smartcontractkit/chainlink-ccip v0.0.0-20240806142433-6ae40054db92 + github.com/smartcontractkit/chainlink-ccip v0.0.0-20240812095514-98c545c89255 github.com/smartcontractkit/chainlink-common v0.2.1-0.20240717132349-ee5af9b79834 github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20240710121324-3ed288aa9b45 github.com/smartcontractkit/chainlink-data-streams v0.0.0-20240718160222-2dc0c8136bfa diff --git a/go.sum b/go.sum index 1c4f247f0e..552e95dba8 100644 --- a/go.sum +++ b/go.sum @@ -1027,8 +1027,8 @@ github.com/smartcontractkit/chain-selectors v1.0.21 h1:KCR9SA7PhOexaBzFieHoLv1Wo github.com/smartcontractkit/chain-selectors v1.0.21/go.mod h1:d4Hi+E1zqjy9HqMkjBE5q1vcG9VGgxf5VxiRHfzi2kE= github.com/smartcontractkit/chainlink-automation v1.0.4 h1:iyW181JjKHLNMnDleI8umfIfVVlwC7+n5izbLSFgjw8= github.com/smartcontractkit/chainlink-automation v1.0.4/go.mod h1:u4NbPZKJ5XiayfKHD/v3z3iflQWqvtdhj13jVZXj/cM= -github.com/smartcontractkit/chainlink-ccip v0.0.0-20240806142433-6ae40054db92 h1:KKqMibl8CcKSyrSkVVfgGEOuC6Cn3vFQK8yMQA6FpxA= -github.com/smartcontractkit/chainlink-ccip v0.0.0-20240806142433-6ae40054db92/go.mod h1:wl0SRmyJ2ARxz+4Eho6nEXsmWFXxTSYpvcb30AxwKyE= +github.com/smartcontractkit/chainlink-ccip v0.0.0-20240812095514-98c545c89255 h1:jomg9y16IB1yAeGJgxOUUzwMrBswcf+/jprkSv02Go0= +github.com/smartcontractkit/chainlink-ccip v0.0.0-20240812095514-98c545c89255/go.mod h1:/ZWraCBaDDgaIN1prixYcbVvIk/6HeED9+8zbWQ+TMo= github.com/smartcontractkit/chainlink-common v0.2.1-0.20240717132349-ee5af9b79834 h1:pTf4xdcmiWBqWZ6rTy2RMTDBzhHk89VC1pM7jXKQztI= github.com/smartcontractkit/chainlink-common v0.2.1-0.20240717132349-ee5af9b79834/go.mod h1:fh9eBbrReCmv31bfz52ENCAMa7nTKQbdhb2B3+S2VGo= github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20240710121324-3ed288aa9b45 h1:NBQLtqk8zsyY4qTJs+NElI3aDFTcAo83JHvqD04EvB0= diff --git a/integration-tests/go.mod b/integration-tests/go.mod index b521a60bea..a22fa1611b 100644 --- a/integration-tests/go.mod +++ b/integration-tests/go.mod @@ -384,7 +384,7 @@ require ( github.com/shirou/gopsutil/v3 v3.24.3 // indirect github.com/shoenig/go-m1cpu v0.1.6 // indirect github.com/sirupsen/logrus v1.9.3 // indirect - github.com/smartcontractkit/chainlink-ccip v0.0.0-20240806142433-6ae40054db92 // indirect + github.com/smartcontractkit/chainlink-ccip v0.0.0-20240812095514-98c545c89255 // indirect github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20240710121324-3ed288aa9b45 // indirect github.com/smartcontractkit/chainlink-data-streams v0.0.0-20240718160222-2dc0c8136bfa // indirect github.com/smartcontractkit/chainlink-feeds v0.0.0-20240710170203-5b41615da827 // indirect diff --git a/integration-tests/go.sum b/integration-tests/go.sum index 2d783f2a41..d67d937c8a 100644 --- a/integration-tests/go.sum +++ b/integration-tests/go.sum @@ -1392,8 +1392,8 @@ github.com/smartcontractkit/chain-selectors v1.0.21 h1:KCR9SA7PhOexaBzFieHoLv1Wo github.com/smartcontractkit/chain-selectors v1.0.21/go.mod h1:d4Hi+E1zqjy9HqMkjBE5q1vcG9VGgxf5VxiRHfzi2kE= github.com/smartcontractkit/chainlink-automation v1.0.4 h1:iyW181JjKHLNMnDleI8umfIfVVlwC7+n5izbLSFgjw8= github.com/smartcontractkit/chainlink-automation v1.0.4/go.mod h1:u4NbPZKJ5XiayfKHD/v3z3iflQWqvtdhj13jVZXj/cM= -github.com/smartcontractkit/chainlink-ccip v0.0.0-20240806142433-6ae40054db92 h1:KKqMibl8CcKSyrSkVVfgGEOuC6Cn3vFQK8yMQA6FpxA= -github.com/smartcontractkit/chainlink-ccip v0.0.0-20240806142433-6ae40054db92/go.mod h1:wl0SRmyJ2ARxz+4Eho6nEXsmWFXxTSYpvcb30AxwKyE= +github.com/smartcontractkit/chainlink-ccip v0.0.0-20240812095514-98c545c89255 h1:jomg9y16IB1yAeGJgxOUUzwMrBswcf+/jprkSv02Go0= +github.com/smartcontractkit/chainlink-ccip v0.0.0-20240812095514-98c545c89255/go.mod h1:/ZWraCBaDDgaIN1prixYcbVvIk/6HeED9+8zbWQ+TMo= github.com/smartcontractkit/chainlink-common v0.2.1-0.20240717132349-ee5af9b79834 h1:pTf4xdcmiWBqWZ6rTy2RMTDBzhHk89VC1pM7jXKQztI= github.com/smartcontractkit/chainlink-common v0.2.1-0.20240717132349-ee5af9b79834/go.mod h1:fh9eBbrReCmv31bfz52ENCAMa7nTKQbdhb2B3+S2VGo= github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20240710121324-3ed288aa9b45 h1:NBQLtqk8zsyY4qTJs+NElI3aDFTcAo83JHvqD04EvB0= diff --git a/integration-tests/load/go.mod b/integration-tests/load/go.mod index 9e9d66acc1..f68a195b96 100644 --- a/integration-tests/load/go.mod +++ b/integration-tests/load/go.mod @@ -40,7 +40,7 @@ require ( github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect - github.com/smartcontractkit/chainlink-ccip v0.0.0-20240806142433-6ae40054db92 // indirect + github.com/smartcontractkit/chainlink-ccip v0.0.0-20240812095514-98c545c89255 // indirect github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20240710121324-3ed288aa9b45 // indirect k8s.io/apimachinery v0.30.2 // indirect ) diff --git a/integration-tests/load/go.sum b/integration-tests/load/go.sum index cca6735675..4eb84feff3 100644 --- a/integration-tests/load/go.sum +++ b/integration-tests/load/go.sum @@ -1374,8 +1374,8 @@ github.com/smartcontractkit/chain-selectors v1.0.21 h1:KCR9SA7PhOexaBzFieHoLv1Wo github.com/smartcontractkit/chain-selectors v1.0.21/go.mod h1:d4Hi+E1zqjy9HqMkjBE5q1vcG9VGgxf5VxiRHfzi2kE= github.com/smartcontractkit/chainlink-automation v1.0.4 h1:iyW181JjKHLNMnDleI8umfIfVVlwC7+n5izbLSFgjw8= github.com/smartcontractkit/chainlink-automation v1.0.4/go.mod h1:u4NbPZKJ5XiayfKHD/v3z3iflQWqvtdhj13jVZXj/cM= -github.com/smartcontractkit/chainlink-ccip v0.0.0-20240806142433-6ae40054db92 h1:KKqMibl8CcKSyrSkVVfgGEOuC6Cn3vFQK8yMQA6FpxA= -github.com/smartcontractkit/chainlink-ccip v0.0.0-20240806142433-6ae40054db92/go.mod h1:wl0SRmyJ2ARxz+4Eho6nEXsmWFXxTSYpvcb30AxwKyE= +github.com/smartcontractkit/chainlink-ccip v0.0.0-20240812095514-98c545c89255 h1:jomg9y16IB1yAeGJgxOUUzwMrBswcf+/jprkSv02Go0= +github.com/smartcontractkit/chainlink-ccip v0.0.0-20240812095514-98c545c89255/go.mod h1:/ZWraCBaDDgaIN1prixYcbVvIk/6HeED9+8zbWQ+TMo= github.com/smartcontractkit/chainlink-common v0.2.1-0.20240717132349-ee5af9b79834 h1:pTf4xdcmiWBqWZ6rTy2RMTDBzhHk89VC1pM7jXKQztI= github.com/smartcontractkit/chainlink-common v0.2.1-0.20240717132349-ee5af9b79834/go.mod h1:fh9eBbrReCmv31bfz52ENCAMa7nTKQbdhb2B3+S2VGo= github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20240710121324-3ed288aa9b45 h1:NBQLtqk8zsyY4qTJs+NElI3aDFTcAo83JHvqD04EvB0=