NomicFoundation / hardhat

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

Received invalid block tag when running custom fork #1906

Closed NatPDeveloper closed 2 years ago

NatPDeveloper commented 2 years ago

I am trying to run a hardhat node custom fork with alchemy, in the past the node would accept trxs on and off. I receive the error

Received invalid block tag 12786684. Latest block number is 12786682

I'm not sure what to do about it, in the past it would just go away, but it has been 2 days and it has not gone away. I cannot use ganache-cli because it does not support the impersonate account, and it would require a complete rewrite of our unit tests which depend on defi contracts that are presetup.

Running with command:

npx hardhat node --fork https://eth-mainnet.alchemyapi.io/v2/ACTUAL_API_KEY_HERE --hostname 0.0.0.0 --fork-block-number 12786615

"hardhat": "2.6.4"
node -v
v12.22.4
npm -v
6.14.14
lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.5 LTS
Release:    18.04
Codename:   bionic

I also tried asking in the discord, but no one has any ideas.

I see a related issue here: https://github.com/nomiclabs/hardhat/issues/1257, though our frontend is not making this mistake.

Also a stack post here with no answer: https://ethereum.stackexchange.com/questions/109625/received-invalid-block-tag-87-latest-block-number-is-0

To reproduce, run the fork branch of this repo and point to a custom forked mainnet eth node: https://github.com/davidberiro/bancor-liquidity-mining/tree/fork

Additional data: Setup a simple frontend to read the state of the DappStaking contract and you should get the above error. Configure metamask to that custom fork.

alcuadrado commented 2 years ago

Hey @NatPDeveloper,

We'll take a look at this soon. Thanks for the reproduction repo, we appreciate it!

p0o commented 2 years ago

I'm also having the same error, started showing up out of nowhere!

Madeindreams commented 2 years ago

I had this issue as well. The cause was that I was starting the node with block number x but my config file fork setting was set to another block number. Do you have the same block number in your config?

NatPDeveloper commented 2 years ago

I had this issue as well. The cause was that I was starting the node with block number x but my config file fork setting was set to another block number. Do you have the same block number in your config?

nope, same in the hardhat node as it is in hardhat config for the fork chain

npx hardhat node --fork https://eth-mainnet.alchemyapi.io/v2/KEY_HERE --hostname 0.0.0.0 --fork-block-number 12786615 > op.log

    fork: {
      url: "http://IP_HERE:8545",
      forking: {
        blockNumber: 12786615,
        url: `https://eth-mainnet.alchemyapi.io/v2/${config.alchemyKey}`
      },
      blockGasLimit: 12e6
    }
Madeindreams commented 2 years ago

I had this issue as well. The cause was that I was starting the node with block number x but my config file fork setting was set to another block number. Do you have the same block number in your config?

nope, same in the hardhat node as it is in hardhat config for the fork chain

npx hardhat node --fork https://eth-mainnet.alchemyapi.io/v2/KEY_HERE --hostname 0.0.0.0 --fork-block-number 12786615 > op.log

    fork: {
      url: "http://IP_HERE:8545",
      forking: {
        blockNumber: 12786615,
        url: `https://eth-mainnet.alchemyapi.io/v2/${config.alchemyKey}`
      },
      blockGasLimit: 12e6
    }

I have removed the block number now that i'm looking at my changes.

 networks: {
      hardhat: {
          forking: {
              url: "http://127.0.0.1:8545",

          },
      },

I start the node like this;

npx hardhat node --fork http://127.0.0.1:8545 --port 7545

local: {
      url: "http://127.0.0.1:7545/",
      chainId: 31337,
  },

Maybe try like this?

I'm telling HH to fork from my geth node and start the node on the port 7545 Let me know if that is doing it.

NatPDeveloper commented 2 years ago

I had this issue as well. The cause was that I was starting the node with block number x but my config file fork setting was set to another block number. Do you have the same block number in your config?

nope, same in the hardhat node as it is in hardhat config for the fork chain

npx hardhat node --fork https://eth-mainnet.alchemyapi.io/v2/KEY_HERE --hostname 0.0.0.0 --fork-block-number 12786615 > op.log

    fork: {
      url: "http://IP_HERE:8545",
      forking: {
        blockNumber: 12786615,
        url: `https://eth-mainnet.alchemyapi.io/v2/${config.alchemyKey}`
      },
      blockGasLimit: 12e6
    }

I have removed the block number now that i'm looking at my changes.

 networks: {
      hardhat: {
          forking: {
              url: "http://127.0.0.1:8545",

          },
      },

I start the node like this;

npx hardhat node --fork http://127.0.0.1:8545 --port 7545

local: {
      url: "http://127.0.0.1:7545/",
      chainId: 31337,
  },

Maybe try like this?

I'm telling HH to fork from my geth node and start the node on the port 7545 Let me know if that is doing it.

How do I start at a specific block then? I need to start at a specific block for my unit tests.

alcuadrado commented 2 years ago

@NatPDeveloper, we tried to reproduce this using your repository, but the error wasn't triggered. Can you provide more info about how to do it?

NatPDeveloper commented 2 years ago

@NatPDeveloper, we tried to reproduce this using your repository, but the error wasn't triggered. Can you provide more info about how to do it?

We have a frontend which is using this fork chain to push transactions to. Metamask is configured for the fork chain. The error occurs when trying to fetch data from the chain.

You can see my comment above about how I'm setting up the custom fork node, then with the fork version of the repo, you can specify the fork chain at that IP address to deploy the unit tests to. Then when you try interacting with the chain, you should get this error.

alcuadrado commented 2 years ago

Can you provide a script or a concrete list of steps to reproduce it? We spent some time with your repo, in the fork branch, trying everything that was mentioned here, and we couldn't reproduce it.

NatPDeveloper commented 2 years ago

Can you provide a script or a concrete list of steps to reproduce it? We spent some time with your repo, in the fork branch, trying everything that was mentioned here, and we couldn't reproduce it.

I just tried creating a simple debug script (added to the fork branch /debug/debug.js) where we use web3 to fetch a state variable, but that's working fine for me for both RPC and ws. I'll check again with our frontend guy to see exactly what is needed to reproduce and report back as soon as I have that.

If unable to reproduce I'll close the issue and explain where our issue was. Sorry for the confusion. Thank you for the quick assessment.

NatPDeveloper commented 2 years ago

The frontend dev had the wrong contract address configured. Very sorry for the wasted time. Everything works fine now.