hardhat icon indicating copy to clipboard operation
hardhat copied to clipboard

obselete block number used when running fork on bsc

Open kopax opened this issue 8 months ago • 3 comments

Version of Hardhat

2.22.12

We also tried to upgrade hardhat and this issue as never been resolved even in latest versions.

What happened?

We have the same problem as in here : https://github.com/NomicFoundation/hardhat/issues/6049

Tries with 2 of our RPC bsc, same issue

I can confirm there was a regression after 2.19.4, but downgrading is not an option as hardhat have outdated issue coming up, such as eth_maxPriorityFeePerGas - Method not supported

Minimal reproduction steps

Just fork the bsc chain main net and notice the issue.

We have a workaround in our package.json but we dont recommend it.

    "fork": "LAST_BLOCK_NUMBER=$(curl -sS -X POST https://bsc.example.org -H \"Content-Type: application/json\" -d '{\"jsonrpc\": \"2.0\",\"method\": \"eth_blockNumber\", \"params\": [],\"id\": 1}' | jq -r '.result') hardhat node",

and in hardhat.config.ts, we just pass LAST_BLOCK_NUMBER:

      forking: {
        blockNumber: process.env.LAST_BLOCK_NUMBER,
        url: FORKING_URL,
      },

From where does hardhat@^2.22 try to get it's latest block number? (apparently not from eth_blockNumber).

kopax avatar Apr 18 '25 11:04 kopax

Can you clarify what you mean by obsolete block number? Can you give an example?

kanej avatar Apr 22 '25 16:04 kanej

I have two RPC bsc, both have the same issue. have you tried to fork bsc ? This is unmissable.

# first attempt 

❯ yarn workspace web3 compile
Forking https://bsc-02.example.com with chainId 56
Error: The response reported error `-32000`: `missing trie node 83279b46a36a983620e43ad24792c0d3a84814eb180d2ccf3e9c1cdaebee1ae6 (path ) state 0x83279b46a36a983620e43ad24792c0d3a84814eb180d2ccf3e9c1cdaebee1ae6 is not available`. (optional data: None). Request: {"jsonrpc":"2.0","method":"eth_getTransactionCount","params":["0xa1b63480ab1912246b81d123085b189dd35ac2ea","0x2f45570"],"id":7}

# second attempt 

❯ yarn workspace web3 compile
Forking https://bsc-02.example.com with chainId 56
Error: The response reported error `-32000`: `missing trie node b466608d820aa284bffb983d24da3872ec5c89076c5fbbf236fc5c37475739f7 (path ) state 0xb466608d820aa284bffb983d24da3872ec5c89076c5fbbf236fc5c37475739f7 is not available`. (optional data: None). Request: {"jsonrpc":"2.0","method":"eth_getCode","params":["0x7a989a441468119d48dd928090799caf9852e881","0x2f45609"],"id":5}

# and finally 

❯ yarn workspace web3 compile
Forking https://bsc-02.example.com with chainId 56
Generating typings for: 68 artifacts in dir: typechain-types for target: ethers-v6
Successfully generated 208 typings!
Compiled 38 Solidity files successfully (evm target: paris).

As of today, if I dont use my workaround, I must like in this example run multiple time every hardhat commands for it to pass.

Can you clarify what you mean by obsolete block number? Can you give an example?

I gave an example workaround in the initial post,. Hardhat use an obselete blockNumber (that is not the one returned by the rpc when requesting eth_blockNumber. I could fix it by fetching the block number and setting it in the settings, otherwise, our full node does not keep that old block.

For instance, in this snippet, you can see that the 2nd fail call use block 0x2f45570, which match 49567241

Image

It is a bit old, I took this snapshot immediately after getting that error:

Image

The missing tries node error come because our full node does not keep that state.

I concluded that hardhat somehow provide an obselete blockNumber when forking the chain. I tried myself to dig into hardhat networking package but gave up, hoping this would be fixed by hardhat team.

I'd like to help further so we can remove that dirty fix and have a proper one in hardhat.

I think the best thing is that you do a fork locally using a full node bsc and see by yourself, if you need an rpc address, as mines are private, we can do that by email.

Best

kopax avatar May 13 '25 00:05 kopax

Hey @kopax, the missing trie node error indicates you forked a non-archive node (many public nodes are non-archival).

I am able to get the latest block number for bsc using quicknode:

  // ...
  networks: {
    hardhat: {
      forking: {
        url: "https://blissful-virulent-grass.bsc.quiknode.pro/xxxyyyzzz/",
      },
    },
  },

I can then get the latest block number within a test:

  it("Should print the latest block number", async function () {
    const client = await hre.viem.getPublicClient();

    const blockNumber = await client.getBlockNumber();

    console.log("Latest block number:", blockNumber);
  });

kanej avatar Jun 05 '25 11:06 kanej