Open fp-crypto opened 1 year ago
The reason Hardhat's time is constant is that when more than N blocks are mined (I don't remember the value of N) they are not actually mined. Instead, a fake range of blocks is created and then the last ones are actually mined. This means that the hash of the latest block is kind of fake, but that's an acceptable trade-off.
There are some non-obvious things to keep in mind to implement this approach though. Happy to share them here if someone is interested in implementing this in Anvil.
What would be the way to go about implementing this @fvictorio?
Hi @fvictorio,
Following up on this issue since there hasn't been any activity for a while. Would it be possible to discuss implementing this functionality, either here or via email? We're currently using anvil extensively for testing and this functionality would save us a lot of CI time. I'm more than willing to tackle this issue if pointed in the right direction.
Cheers :tada:
Ok, this is mainly off the top of my head, so take it with a grain of salt.
The main thing here is that you obviously don't want to mine one million blocks or whatever, so what you do is to create a representation of a range of blocks that doesn't actually exist. Doing this means that the latest block won't be 100% correct (for example, you can't have a correct block hash without actually mining all the previous blocks), but that's fine for most people.
This is, broadly speaking, how Hardhat/EDR does it:
latest
block has to have a timestamp of the previous latest timestamp plus 100_000
.We have a lot of tests for this, starting here: https://github.com/NomicFoundation/edr/blob/e0c927d337671cbba061caab7ad35db627ecf183/hardhat-tests/test/internal/hardhat-network/provider/modules/hardhat.ts#L303
You can also start reading EDR code from here for more details on how this works.
I'm pretty sure that there are corners that can be cut here, but we are normally quite obsessive about being as correct as possible (e.g., correct timestamps) and this is the way we managed to do it.
Sorry for the messy explanation and hopefully that helps!
I attempted to move my testing from
hardhat node
toanvil
. Some of my testing requires mining a large number of blocks.anvil_mine
execution time seems to scale linearly relative to block count, whereashardhat_mine
withhardhat node
is nearly constant. See the following benchmarks:Anvil
Hardhat
I'm using:
anvil 0.1.0 (0e33b3e 2023-07-26T00:26:08.161934000Z)
hardhat 9.6.7
ape 0.6.14
(for testing)https://github.com/foundry-rs/foundry/blob/41bae8e6265e905f73c3f4eac14a5ba9275417a4/anvil/src/eth/api.rs#L1421-L1440