From 09398654bf821b928fe1c8e4ba357532b29afbab Mon Sep 17 00:00:00 2001 From: Ardit Marku Date: Tue, 28 Jan 2025 13:21:50 +0200 Subject: [PATCH] Increase computation limit for Cadence tx We set the computation limit for the Cadence tx that wraps EVM txs, to its max value. We also add a validation check, for rejecting EVM txs which go beyond the max gas limit. --- models/errors/errors.go | 7 +++++++ services/requester/requester.go | 9 ++++++++- tests/web3js/eth_failure_handling_test.js | 21 ++++++++++++++++++++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/models/errors/errors.go b/models/errors/errors.go index ab067e3d7..21e72515e 100644 --- a/models/errors/errors.go +++ b/models/errors/errors.go @@ -66,6 +66,13 @@ func NewTxGasPriceTooLowError(gasPrice *big.Int) error { )) } +func NewTxGasLimitTooHighError(maxGasLimit uint64) error { + return NewInvalidTransactionError(fmt.Errorf( + "tx gas limit exceeds the max value of %d: ", + maxGasLimit, + )) +} + func NewRecoverableError(err error) error { return fmt.Errorf("%w: %w", ErrRecoverable, err) } diff --git a/services/requester/requester.go b/services/requester/requester.go index be0291af5..7382e934d 100644 --- a/services/requester/requester.go +++ b/services/requester/requester.go @@ -15,6 +15,7 @@ import ( "github.com/onflow/flow-go/fvm/evm/emulator" "github.com/onflow/flow-go/fvm/evm/offchain/query" evmTypes "github.com/onflow/flow-go/fvm/evm/types" + flowGo "github.com/onflow/flow-go/model/flow" "github.com/onflow/go-ethereum/common" gethCore "github.com/onflow/go-ethereum/core" "github.com/onflow/go-ethereum/core/txpool" @@ -43,6 +44,7 @@ var ( const minFlowBalance = 2 const blockGasLimit = 120_000_000 +const txMaxGasLimit = 50_000_000 // estimateGasErrorRatio is the amount of overestimation eth_estimateGas // is allowed to produce in order to speed up calculations. @@ -187,6 +189,10 @@ func (e *EVM) SendRawTransaction(ctx context.Context, data []byte) (common.Hash, return common.Hash{}, err } + if tx.Gas() > txMaxGasLimit { + return common.Hash{}, errs.NewTxGasLimitTooHighError(txMaxGasLimit) + } + if err := models.ValidateTransaction(tx, e.head, e.evmSigner, e.validationOptions); err != nil { return common.Hash{}, err } @@ -589,7 +595,8 @@ func (e *EVM) buildTransaction( flowTx := flow.NewTransaction(). SetScript(script). - SetReferenceBlockID(latestBlock.ID) + SetReferenceBlockID(latestBlock.ID). + SetComputeLimit(flowGo.DefaultMaxTransactionGasLimit) for _, arg := range args { if err := flowTx.AddArgument(arg); err != nil { diff --git a/tests/web3js/eth_failure_handling_test.js b/tests/web3js/eth_failure_handling_test.js index a6973a7be..6185296a6 100644 --- a/tests/web3js/eth_failure_handling_test.js +++ b/tests/web3js/eth_failure_handling_test.js @@ -1,8 +1,27 @@ const { assert } = require('chai') const helpers = require('./helpers') -const conf = require("./config") +const conf = require('./config') const web3 = conf.web3 +it('should fail when tx gas limit higher than the max value', async () => { + let receiver = web3.eth.accounts.create() + + try { + await helpers.signAndSend({ + from: conf.eoa.address, + to: receiver.address, + value: 10, + gasPrice: conf.minGasPrice, + gasLimit: 51_000_000, // max tx gas limit is 50_000_000 + }) + } catch (e) { + assert.include(e.message, 'tx gas limit exceeds the max value of 50000000') + return + } + + assert.fail('should not reach') +}) + it('should fail when nonce too low', async () => { let receiver = web3.eth.accounts.create()