hardhat
hardhat copied to clipboard
Add `initialBlockNumber` option for the node
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:
- Start the Hardhat node.
- Replay transaction A at the current time
- Use time.increaseTo to increase the time by the time delta of tx A and B.
- Replay transaction B.
- 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
Any updates here? Its a nice feature to have.
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 thanks for pointing out the initialDate
option, in that case, yes, this issue is essentially an ask for an initialBlockNumber
option.
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
}
@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",
},