-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hardhat reports inconsistent gas usages when running in the fork mode #739
Comments
Hi, was it possible to reproduce the bug? |
Hey @polka125, sorry for not responding this before, this issue fell through the cracks and we didn't prioritize it sooner. I'll take a look as soon as I can. |
Hi @polka125. I haven't tried to reproduce this locally yet, but one thing I noticed is that you are not setting an initial timestamp. Since this changes between executions, it's possible that that's the reason for the difference in gas usage. From what I can tell, you are executing the transactions of block Could you try that and tell me if the inconsistency persists? |
Hi @fvictorio, Thank you! |
Glad to hear that! |
Version of Hardhat
2.22.15
What happened?
What Goes Wrong
Hardhat reports inconsistent gas usage when running in fork mode. Different runs may result in different gas usages on the same set of transactions in the same order, in the state forked from the same block.
A potentially related issue has been reported before.
My Setup
Hardhat Server Process
I use Hardhat as a local
JSON_RPC
. Here is myhardhat.config.js
:Payloads
I have a
paylods.json
file containing a set of queries. The detailed description of the queries structure is deferred to the further details section. Thepayloads.json
is hosted here or find attached payloads.jsonPayloads Execution
The payloads are submitted via
POST
request using python sctiptsubmit_payloads.py
, which does the following;For iteration 1 to MAX_RUNS
forks the blockchain using
FORK_PAYLOAD
payloadSubmit each payload from the
payloads.json
file and logs the responses. The return status is ensured to be200
logs the responses to the
output_{run}.json
fileprints the hashes of the logs upon exit to the screen
The full code is as below. Note that in order to run, you need to replace
JSON_RPC_TOKEN
with a token, I use infura api:Logs Comparation
The logs show that the gas consumption may wary from run to run. Usually the first divergence happen for the line line 1360 of the logs:
{"jsonrpc": "2.0", "id": 1, "result": {"cumulativeGasUsed": "0x9264d6", ...
{"jsonrpc": "2.0", "id": 1, "result": {"cumulativeGasUsed": "0x925ebe", ...
Further Details
The bug was discovered while my colleagues and I tried to emulate the block mining process. We collected a set of real transactions (smart contracts and externaly owned accounts) and tried to get the gas usege of the block when mined.
The
payload.json
file contains the payload data to call the hardhat RPC. The payloads description:B
does not have enough balance to send a transactionTxB
, but if there is a transactionTxA
which tops up the balanceB
making the block[TxA, TxB]
a valid block. To bypass hardhat's automatic balance check (which prevents sending the txTxB
otherwise), for each transaction we do:Minimal reproduction steps
Reproducing the Bug
I managed to reproduce the bug in a container. You can find the PoC and instructions here. The reproduction instructions are:
git clone https://github.com/polka125/hardhat-bug-artifacts.git
RPC_TOKEN
docker build -t hardhat_bug .
docker run -v ./host_logs:/app/logs -it hardhat_bug
At this point you might get feeling that the docker stuck, but checkhost_logs/hardhat.log
, it accumulates the hardhat output, i.e. the process is runningfind ./host_logs -type f -print0 | sort -z | xargs -0 shasum -a 1
The last step will print hashes of the log files, the log files with different logs will be easy to locate visually as their hashes will diverge
Search terms
Gas usage, Fork mode
The text was updated successfully, but these errors were encountered: