How to Fork Ethereum and Replay Historical Transactions with Ganache 7 Archive Support

How to Fork Ethereum and Replay Historical Transactions with Ganache 7 Archive Support

In Ethereum, archive nodes are full nodes running on archive mode. Ethereum’s full nodes do not store the generated World State from each block verification. They only need to store information such as headers and block contents.

They need to keep this information around long enough to be able to build up the blockchain historical state (data outside of the most recent 128 blocks) on demand. Storing only small amounts of data as opposed to the entire blockchain state enables full nodes to minimize storage and computation cost as well as sync time.

Archive nodes on the other hand, in addition to all of full node’s capabilities, store terabytes of historic data, making it possible to query any block at any point in time.

Build, Test, and Learn with Ganache 7 and Infura Integration

With the latest Ganache 7 upgrade, access to Ethereum Mainnet archive-state data is a breeze, requiring no extra configuration and just a single command. You can now instantly run your own fork of Ethereum’s Mainnet on your local computer with Ganache for free, thanks to Infura’s Ethereum archive nodes.  

For example, you can now debug verified external contracts using the Truffle Debugger. Previously, you had to connect to a self-hosted geth node that runs on archive mode, or pay for access to an expensive archive node.


How does it work?

Ganache uses the zero config mainnet forking feature under the hood, allowing you to simulate having the same state as the Ethereum mainnet, but on your local machine. To run Ganache in full archive mode, use this command: ganache --fork

This creates an archive node off of the Ethereum mainnet, you can also specify what network to create an archive node off of by attaching the --fork.network <Network Name> flag.

Lets walk through a demo of making requests to blocks older than 128 blocks from head, thereby using the archive data feature.

  • Start Ganache on archive mode with ganache --fork. This creates a fork of mainnet.


  • Next, in a new terminal window, run a request that fetches the balance of an address at block 1. The eth_getBalance RPC method accepts an address as well as an optional block parameter:

curl -H 'Content-Type: application/json'   --data '{"jsonrpc":"2.0", "id": 1, "method": "eth_getBalance", "params": ["0xe5Fb31A5CaEE6a96de393bdBF89FBe65fe125Bb3", "0x1"] }' http://localhost:8545


The 0xe5Fb31A5CaEE6a96de393bdBF89FBe65fe125Bb3 parameter on the curl request above represents the account address we want to query while 0x1 specifies the block number from which we want the result.

Taking a look at etherscan, this account received 1,000 ETH from the genesis block at block 1.

Notice how we got the response:

{"id":1,"jsonrpc":"2.0","result":"0x3635c9adc5dea00000"}

The number 0x3635c9adc5dea00000 is hex representation of 1 x 10^21 (WEI) which is equal to 1,000 ETH.

Performance Test

If you’re curious to know how Ganache’s archive node performs, we ran a speed test by simulating the Convex Finance’s system shutdown method that performs a number of token transfers. Here are the results:

Tool              Initial attempt              subsequent attempt  (exact same                  request)

Ganache 7      ~ 12 minutes                      ~24 seconds

Here’s a link to the repo in case you want to run these benchmark tests on your PC. Shout out to Matt Solomon for putting the repo together.

Conclusion

To get started with Ganache 7, whether as a new user or an existing Ganache v6 user, simply download it as an NPM package with the command npm install ganache --global or follow the instructions here.

We’re curious to see how developers leverage this feature during development. Let us know! Send us a tweet @trufflesuite or open a discussion here.