This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 681
fix: use adjusted time on estimate gas when latest block is being used #4287
Open
luzzif
wants to merge
1
commit into
trufflesuite:develop
Choose a base branch
from
carrot-kpi:fix/estimate-gas-timestamp
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,9 @@ import { | |
StructLog, | ||
TraceTransactionOptions, | ||
EthereumRawAccount, | ||
TraceTransactionResult | ||
TraceTransactionResult, | ||
QUANTITY, | ||
Tag | ||
} from "@ganache/ethereum-utils"; | ||
import type { InterpreterStep } from "@ethereumjs/evm"; | ||
import { decode } from "@ganache/rlp"; | ||
|
@@ -49,7 +51,10 @@ import { | |
calculateIntrinsicGas, | ||
InternalTransactionReceipt, | ||
VmTransaction, | ||
TypedTransaction | ||
TypedTransaction, | ||
LegacyTransaction, | ||
EIP2930AccessListTransaction, | ||
EIP1559FeeMarketTransaction | ||
} from "@ganache/ethereum-transaction"; | ||
import { Block, RuntimeBlock, Snapshots } from "@ganache/ethereum-block"; | ||
import { | ||
|
@@ -77,6 +82,7 @@ import { dumpTrieStorageDetails } from "./helpers/storage-range-at"; | |
import { GanacheStateManager } from "./state-manager"; | ||
import { TrieDB } from "./trie-db"; | ||
import { Trie } from "@ethereumjs/trie"; | ||
import { Ethereum } from "./api-types"; | ||
|
||
const mclInitPromise = mcl.init(mcl.BLS12_381).then(() => { | ||
mcl.setMapToMode(mcl.IRTF); // set the right map mode; otherwise mapToG2 will return wrong values. | ||
|
@@ -577,6 +583,40 @@ export default class Blockchain extends Emittery<BlockchainTypedEvents> { | |
|
||
coinbase: Address; | ||
|
||
gasEstimateBlock = async ( | ||
tx: | ||
| LegacyTransaction | ||
| EIP2930AccessListTransaction | ||
| EIP1559FeeMarketTransaction, | ||
blockNumber: QUANTITY | Ethereum.Tag = Tag.latest | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
) => { | ||
const previousBlock = await this.blocks.get(blockNumber); | ||
const previousHeader = previousBlock.header; | ||
const options = this.#options; | ||
|
||
let timestamp = previousHeader.timestamp; | ||
if (blockNumber === "latest") | ||
timestamp = Quantity.from(this.#adjustedTime(previousHeader.timestamp)); | ||
Comment on lines
+597
to
+599
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems to me that if the request is for "latest", this should be incrementing the timestamp, otherwise we use the timestamp of the previous block, rather than the "next" block. |
||
|
||
let gas = tx.gas; | ||
if (gas.isNull()) | ||
// eth_estimateGas isn't subject to regular transaction gas limits | ||
gas = options.miner.callGasLimit; | ||
|
||
return new RuntimeBlock( | ||
Quantity.from((previousHeader.number.toBigInt() || 0n) + 1n), | ||
previousHeader.parentHash, | ||
new Address(previousHeader.miner.toBuffer()), | ||
gas, | ||
previousHeader.gasUsed, | ||
timestamp, | ||
this.isPostMerge ? Quantity.Zero : options.miner.difficulty, | ||
previousHeader.totalDifficulty, | ||
this.getMixHash(previousHeader.parentHash.toBuffer()), | ||
0n // no baseFeePerGas for estimates | ||
); | ||
}; | ||
|
||
#readyNextBlock = (previousBlock: Block, timestamp?: number) => { | ||
const previousHeader = previousBlock.header; | ||
const previousNumber = previousHeader.number.toBigInt() || 0n; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it'd be nice if this were a verb, ie
createEstimateGasBlock
or something.I haven't the time to look into this further today, but I wonder if there's some value in combining the
readyNextBlock
and this function, as they are very similar (it might be that it just becomes too complicated to be worthwhile though).