How to Fork Ethereum and Replay Historical Transactions with Ganache 7 Archive Support
Tags: ganache7, truffle, web3, forkethereum
Published: 2022-01-12 04:20:19

> This post was originally published on [the Infura blog](https://blog.infura.io/fork-ethereum-replay-historical-transactions-with-ganache-7-archive-support) 

In Ethereum, archive nodes are [full nodes](https://ethereum.org/en/developers/docs/nodes-and-clients/#full-node) 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](https://github.com/trufflesuite/ganache/releases/tag/ganache%407.0.0-beta.1), 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](https://trufflesuite.com/blog/debugging-verified-external-contracts-with-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.

**1.** Start Ganache on archive mode with **`ganache --fork`**. This creates a fork of mainnet.
![Ganache 7](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/02szpd0iyogtvdzwaeac.png)

**2.** 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.
```bash
 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 1000 ETH from the genesis block at block 1.

![Etherscan image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/afc01chlksjknoszp67v.png)

Notice how we got the response: 
```bash
{"id":1,"jsonrpc":"2.0","result":"0x3635c9adc5dea00000"}
``` 
The number **0x3635c9adc5dea00000** is hex representation of 1 x 10^21 (WEI) which is equal to 1000 ETH.

### Performance Test

If you’re curious to know how Ganache’s archive node performs, I 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 attempts (same request) |
|-----------|-----------------|---------------------|
| Ganache 7 | ~ 12 minutes    | ~24 seconds         |
  
Here’s a [link](https://github.com/mds1/convex-shutdown-simulation) to the repo in case you want to run these benchmark tests on your PC. Shout out to [Matt Solomon](https://twitter.com/msolomon44/status/1466198596772978689) 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@beta --global** or follow the instructions [here](https://github.com/trufflesuite/ganache#getting-started).