NomicFoundation / hardhat

Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software.
https://hardhat.org
Other
7.33k stars 1.42k forks source link

Hardhat throws a strange error when trying to call smart contract functions while using local hardhat forked network #3379

Closed archerkeyboard closed 1 year ago

archerkeyboard commented 1 year ago

Here's the code used: https://github.com/archerkeyboard/hhStuff/tree/master

Instructions to reproduce:

  1. In a cmd run npx hardhat node --fork https://bsc-dataseed2.binance.org
  2. In another terminal run npx hardhat run .\scripts\test.js --network localhost

Scenario 1 (works)

If we run command 2) right after the accounts show up from 1) It works and we deposit bnb and get wrapped bnb image

Scenario 2 (does not work)

If we wait and do the same but after 10-20 seconds, we get the following error: WARNING: Calling an account which is not a contract

image

The code is the same, only difference is the time of the execution after initializing the local hardhat network.

github-actions[bot] commented 1 year ago

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: 2e6a712a-721f-4865-8a22-63c493926a32

fvictorio commented 1 year ago

Thanks a lot for the clear reproduction steps, @archerkeyboard, that was very useful.

I believe I've found the issue: apparently, after a while, the BSC node misbehaves when you send batched RPC calls. This seems to be a bug in the BSC node.

Here is what I did, in case you want to report this to them (I don't know which channel to use for that).

Given this two calls:

curl https://bsc-dataseed2.binance.org -s -X POST -H 'Content-Type: application/json' --data '{"jsonrpc": "2.0", "id": 1, "method": "eth_getCode", "params": ["0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c", "0x165e2be"]}'
curl https://bsc-dataseed2.binance.org -s -X POST -H 'Content-Type: application/json' --data '[{"jsonrpc":"2.0", "id": 1, "method": "eth_getCode", "params": ["0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c", "0x165e2be"]}]'

The difference here is that the second is a batch request, because it's inside an array.

The first one always returns the right value (or at least most of the time, sometimes it returns an error). But the second one has a very curious behavior:

(There isn't a clear separation between the three "stages". Sometimes the right value is returned after a previous call returned 0x.)

Since Hardhat's mainnet forking relies on batched rpc calls to get the data of some accounts (it fetches the code, nonce and balance in one call), this problem means that BSC forking will not be very reliable.

I'm going to close this now because I don't think there's much we can do. If you report this problem somewhere, feel free to link this issue and/or tag me.