hardhat icon indicating copy to clipboard operation
hardhat copied to clipboard

Incorrect block hash when forking other EVM chains

Open leduythuccs opened this issue 2 years ago • 3 comments

When forking other EVM chains (like Avax, Avax testnet, Aurora, Polygon, ...), the blockhash getting from eth_getBlockByNumber is not match with the RPC, the reason is:

  • In this: ForkBlockchain.ts#L272, hardhat use the blockdata to create a new Block instance. Block instance will use Ethereum formula to calculate the block hash.
  • On Avax (and many other chains), they don't follow Ethereum's block hash formula so the calculated hash result will differ from the actual hash from the RPC.

Reproduce this:

  • yarn hardhat node --fork https://api.avax-test.network/ext/bc/C/rpc
  • Fetch a random-block information:
curl -X GET \
  'localhost:8545' \
  --data-raw '{
  "method": "eth_getBlockByNumber",
  "params": [
    "0xC5CFC8",
    false
  ],
  "jsonrpc": "2.0",
  "id": 44
}'
  • That block is here: https://testnet.snowtrace.io/block/12963784, its hash is 0x454706c3214e4c4c1336dce67b57d608d6feca590a882922b2e0ca440e075bbd while hardhat is returning 0xbd00f86284b05bf7b69f04a0c3186fc548692d3664f900389487d9199957afbb

How to fix

  • I found a hacky fix is that we can override the .hash() function of the block instance, like what i did in this commit

leduythuccs avatar Sep 13 '22 06:09 leduythuccs

This issue is also being tracked on Linear.

We use Linear to manage our development process, but we keep the conversations on Github.

LINEAR-ID: 16b29507-4f6d-4de8-8804-d6ea49d712f4

github-actions[bot] avatar Sep 13 '22 06:09 github-actions[bot]

This issue is not a problem for most of the users tho. I only realized this when trying to run a subgraph with a local hardhat fork. Subgraph verifies the block hash in order to detect re-org.

leduythuccs avatar Sep 13 '22 07:09 leduythuccs

Hi @leduythuccs, thanks for reporting this.

This is a hard one because ethereumjs doesn't provide any way to set the hash. Monkey-patching is always an option, of course, but I don't think the risk is justified in this case. In any case, we really don't want to have that unnecessary hash computation (we should just use whatever the remote node returned).

fvictorio avatar Sep 20 '22 21:09 fvictorio