Skip to content
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

Add initialBlockNumber option for the node #4405

Open
mshakeg opened this issue Sep 20, 2023 · 5 comments
Open

Add initialBlockNumber option for the node #4405

mshakeg opened this issue Sep 20, 2023 · 5 comments
Labels
blocked-reason:needs-edr status:blocked Blocked by other issues or external reasons type:feature Feature request

Comments

@mshakeg
Copy link

mshakeg commented Sep 20, 2023

Describe the feature

Description

I propose a feature to allow users to specify a custom start time and initial block number for the local Hardhat network. Currently, Hardhat initializes the local development network with the current timestamp and starts from block number 0. While the network allows manipulation of the EVM time and block numbers through methods like evm_increaseTime and evm_mine, it does not offer the capability to start the network with custom initial settings let alone specifying a start time in the past i.e. less than the current unix time.

Motivation

The need for this feature arises from various testing scenarios where developers need to simulate or replay past mainnet transactions. Having control over the exact start time and block number is crucial for achieving accurate testing conditions.

For example, consider a series of transactions A, B, and C that occurred at times 1000, 1003, and 1008 respectively on mainnet. If we want to test these transactions on a local Hardhat node, the current method would be to:

  1. Start the Hardhat node.
  2. Replay transaction A at the current time
  3. Use time.increaseTo to increase the time by the time delta of tx A and B.
  4. Replay transaction B.
  5. Repeat for other transactions.

This method lacks precision and makes it cumbersome to simulate more complex scenarios where the exact start time and block number are essential.

Proposed Solution

I propose extending the configuration options for the Hardhat network to include startTime and startBlockNumber. For example:

module.exports = {
  networks: {
    hardhat: {
      startTime: 1672444800,  // Unix timestamp
      startBlockNumber: 1345678,
    },
  },
  // other configs
}

The above settings would instruct the Hardhat network to initialize itself starting from the given Unix timestamp and block number.

Search terms

node start time past

@ernaneluis
Copy link

Any updates here? Its a nice feature to have.

@fvictorio
Copy link
Member

Hardhat already supports an initialDate option for the starting timestamp, so I guess the proposal here is just about having an initialBlockNumber option, that respects that initialDate (if set), right?

@fvictorio fvictorio added status:blocked Blocked by other issues or external reasons type:feature Feature request blocked-reason:needs-edr and removed status:triaging labels Oct 17, 2023
@mshakeg
Copy link
Author

mshakeg commented Oct 17, 2023

@fvictorio thanks for pointing out the initialDate option, in that case, yes, this issue is essentially an ask for an initialBlockNumber option.

@fvictorio fvictorio changed the title Allow Custom Start Time and Initial Block Number for Hardhat Network Add initialBlockNumber option for the node Oct 18, 2023
@schaable schaable removed their assignment Oct 24, 2023
@Ermal-code
Copy link

Any updates on this?

For now I'm trying to use the initialDate option when running a local node but I get an error:

Error running JSON-RPC server: A string must be provided with a 0x-prefix, given: 0xNaN
module.exports = {
 defaultNetwork: "localHardhat",
  networks: {
    hardhat: {
    initialDate: "1699455210",
      mining: {
        auto: true,
        interval: [10000, 30000],
      },
      accounts: {
        count: 20,
        accountsBalance: "10000000000000000000",
      },
    },
  },
localHardhat: {
      url: "http://127.0.0.1:8545",
    },
  // other configs
}

@fvictorio
Copy link
Member

@Ermal-code that's a bad error message we are returning, but the underlying issue is that initialDate should be a valid date time string, for example:

    hardhat: {
      initialDate: "2023-12-16 01:15:15 PM",
    },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked-reason:needs-edr status:blocked Blocked by other issues or external reasons type:feature Feature request
Projects
Status: Backlog
Development

No branches or pull requests

5 participants