Skip to content

Commit

Permalink
fix estimateGas (#3416)
Browse files Browse the repository at this point in the history
* fix estimateGas

* client: cleanup estimateGas

* client: fix eth_estimateGas test and improve reliability of eth_estimateGas

---------

Co-authored-by: Jochem Brouwer <jochembrouwer96@gmail.com>
  • Loading branch information
acolytec3 and jochem-brouwer committed May 10, 2024
1 parent c6aae92 commit 79a3316
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
31 changes: 26 additions & 5 deletions packages/client/src/rpc/modules/eth.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Block } from '@ethereumjs/block'
import { Hardfork } from '@ethereumjs/common'
import { BlobEIP4844Transaction, Capability, TransactionFactory } from '@ethereumjs/tx'
import {
Expand Down Expand Up @@ -28,7 +29,7 @@ import type { EthereumClient } from '../../index.js'
import type { EthProtocol } from '../../net/protocol/index.js'
import type { FullEthereumService, Service } from '../../service/index.js'
import type { RpcTx } from '../types.js'
import type { Block, JsonRpcBlock } from '@ethereumjs/block'
import type { JsonRpcBlock } from '@ethereumjs/block'
import type { Log } from '@ethereumjs/evm'
import type { Proof } from '@ethereumjs/statemanager'
import type {
Expand Down Expand Up @@ -542,11 +543,11 @@ export class Eth {
}

if (transaction.gasPrice === undefined && transaction.maxFeePerGas === undefined) {
// If no gas price or maxFeePerGas provided, use current block base fee for gas estimates
// If no gas price or maxFeePerGas provided, set maxFeePerGas to the next base fee
if (transaction.type !== undefined && parseInt(transaction.type) === 2) {
transaction.maxFeePerGas = `0x${block.header.baseFeePerGas?.toString(16)}`
transaction.maxFeePerGas = `0x${block.header.calcNextBaseFee()?.toString(16)}`
} else if (block.header.baseFeePerGas !== undefined) {
transaction.gasPrice = `0x${block.header.baseFeePerGas?.toString(16)}`
transaction.gasPrice = `0x${block.header.calcNextBaseFee()?.toString(16)}`
}
}

Expand All @@ -555,6 +556,25 @@ export class Eth {
gasLimit: transaction.gas,
}

const blockToRunOn = Block.fromBlockData(
{
header: {
parentHash: block.hash(),
number: block.header.number + BIGINT_1,
timestamp: block.header.timestamp + BIGINT_1,
baseFeePerGas: block.common.isActivatedEIP(1559)
? block.header.calcNextBaseFee()
: undefined,
},
},
{ common: vm.common, setHardfork: true }
)

vm.common.setHardforkBy({
timestamp: blockToRunOn.header.timestamp,
blockNumber: blockToRunOn.header.number,
})

const tx = TransactionFactory.fromTxData(txData, { common: vm.common, freeze: false })

// set from address
Expand All @@ -563,12 +583,13 @@ export class Eth {
tx.getSenderAddress = () => {
return from
}

const { totalGasSpent } = await vm.runTx({
tx,
skipNonce: true,
skipBalance: true,
skipBlockGasLimitValidation: true,
block,
block: blockToRunOn,
})
return `0x${totalGasSpent.toString(16)}`
}
Expand Down
11 changes: 9 additions & 2 deletions packages/client/test/rpc/eth/estimateGas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ describe(
from: address.toString(),
data: `0x${funcHash}` as PrefixedHexString,
gasLimit: bigIntToHex(BigInt(53000)),
gasPrice: bigIntToHex(BigInt(1000000000)),
}
const estimateTx = LegacyTransaction.fromTxData(estimateTxData, { freeze: false })
estimateTx.getSenderAddress = () => {
Expand Down Expand Up @@ -158,7 +159,13 @@ describe(

// Test EIP1559 tx with no maxFeePerGas
const EIP1559reqNoGas = await rpc.request(method, [
{ ...estimateTxData, type: 2, maxFeePerGas: undefined, gasLimit: undefined },
{
...estimateTxData,
type: 2,
maxFeePerGas: undefined,
gasLimit: undefined,
gasPrice: undefined,
},
])
assert.equal(
EIP1559reqNoGas.result,
Expand All @@ -168,7 +175,7 @@ describe(

// Test legacy tx with London head block
const legacyTxNoGas = await rpc.request(method, [
{ ...estimateTxData, maxFeePerGas: undefined, gasLimit: undefined },
{ ...estimateTxData, maxFeePerGas: undefined, gasLimit: undefined, gasPrice: undefined },
])
assert.equal(
legacyTxNoGas.result,
Expand Down

0 comments on commit 79a3316

Please sign in to comment.