Hamsa Harcourt I'm Hamsa, a software engineer with a strong passion for building human-centric products. I love teaching concepts about JavaScript and the web at large.

The importance of snapshots in advanced blockchain functions

4 min read 1292

Advanced Blockchain Functions Snapshot

Since its creation, the common use cases and applications of the blockchain have shifted drastically.  Nowadays, blockchain technology plays a role in financial payments, supply chains, identity verification, insurance, and healthcare, just to name a few.

However, to stay relevant in the long term, it may become necessary for blockchain platforms to make certain changes. For example, Tron, a popular cryptocurrency, is migrating its tokens from Ethereum to its own mainnet, thereby creating the infrastructure for a completely decentralized internet that is capable of hosting decentralized apps.

Because blockchain networks are known for storing large quantities of data, it’s likely that the quantity of data kept by a node in any given blockchain will be significant and will continue to expand. This can be daunting for new nodes joining a network, which have to download data from the entire network before joining the network.

In this article, we’ll explore the importance of snapshot and reverse state in blockchain applications, learning how we can leverage them to manage our application’s storage.

Introducing snapshots

A snapshot is a file that contains the current status of a blockchain at any particular time. A snapshot captures the complete blockchain ledger, including all existing addresses and their related data, like transactions, fees, balance, metadata, and more. It is saved in a directory on your hard drive.

When a node joins a network, it must download the whole blockchain, from the genesis block to the last block generated, which can range in size from 1.5 GB to 140 GB. Snapshots, on the other hand, allow nodes to catch up with the network by obtaining only the most recent states.

For a better understanding of snapshots, consider how much labor it would take to collect the most recent states and transactions of all accounts and smart contracts on the source blockchain. Also keep in mind that as long as the network is operational, the global state will change as new transactions are received.

Blockchain snapshots: Use cases

Let’s review some common use cases of blockchain snapshots. For one, airdrops refers to the transfer of digital assets to the wallets of active members of the blockchain community, usually for free or in return for a community service. In this case, snapshots are used to seek out committed members of the community who held the tokens at a given point in time.

Token migration refers to the process of transmitting a token holder’s balance from one blockchain to an entirely different blockchain. It is used mostly during hard forks where holders of tokens in the original blockchain will be granted tokens in the new blockchain.

Altcoins, which are spinoffs or improved versions of a cryptocurrency, are created due to a perceived shortcoming of the original cryptocurrency. Before an altcoin is created, a snapshot of the cryptocurrency blockchain is taken, providing insight of the initial distribution of the original blockchain. An example of an altcoin is Bitcoin Cash, which is based off of Bitcoin.

Getting started

Ganache is a tool for developing Ethereum and Corda DApps on a local level. With Ganache, you can build, deploy, and test your DApps in a secure and predictable environment.

Using Ganache programmatically

To install Ganache, run the following command:

npm install ganache

Next, add the following code to your existing Ganache project:

const provider = ganache.provider();
const [from, to] = await provider.send("eth_accounts");
const startingBalance = BigInt(await provider.send("eth_getBalance", [from] ));

// take a snapshot
const snapshotId = await provider.send("evm_snapshot");

// send value to another account (over-simplified example)
await provider.send("eth_subscribe", ["newHeads"] );
await provider.send("eth_sendTransaction", [{from, to, value: "0xffff"}] );
await provider.once("message"); // Note: `await provider.once` is non-standard

// ensure balance has updated
const newBalance = await provider.send("eth_getBalance", [from] );
assert(BigInt(newBalance) < startingBalance);

// revert the snapshot
const isReverted = await provider.send("evm_revert", [snapshotId] );

// ensure balance has reverted
const endingBalance = await provider.send("eth_getBalance", [from] );
const isBalanceReverted = assert.strictEqual(BigInt(endingBalance), startingBalance);
console.log({isBalanceReverted: isBalanceReverted});

In the code above, we created a provider and checked the balance of an account to use as a starting reference point. Then, we take a snapshot of the state of the blockchain at the current block using the evm_snapshot RPC method; it takes no parameters and returns an ID, which we store as snapshotId.

Next, we subscribe to an an event that we named newHeads. Subscriptions are created with a regular RPC call with eth_subscribe as the method and the subscription name as the first parameter. If successful, it returns the subscription ID. For each event that matches the subscription, a notification with relevant data is sent together with the subscription ID.

Next, we send the value from one account to another, simulating activity on the blockchain. Afterwards, we confirm that the balance we had at the beginning is different from the balance we have now.

Finally, we use the evm_revert RPC method to revert the state of the blockchain to a previous snapshot. It takes a single parameter, which is the snapshot ID to revert to. If no snapshot ID is passed, it will revert to the latest snapshot. It’s important to note that after a successful evm_revert, the same snapshot ID cannot be used again.

We then confirm that our revert was successful by checking if our starting balance is the same as our current balance, meaning that the transaction we made earlier was reverted.

Using the Ganache command line

The Ganache CLI, which is part of the Truffle suite of Ethereum development tools, is the command line version of Ganache. To install the Ganache CLI, run the following command:

npm install -g ganache-cli

To confirm if the installation was successful, run the command below:

More great articles from LogRocket:

ganache-cli --version

If the Ganache CLI was installed successfully, a version number will appear on your screen. You can take a snapshot via the Ganache CLI by running the following command:

ganache-cli --database.dbPath="./data/save/" -i="5777" -d -m="YOUR_12_WORDS_HERE"

--database.dbPath specifies a path to a directory to save the chain database. If a database already exists, the Ganache CLI will initialize that chain instead of creating a new one.

-I specifies the network ID returned by the RPC method. It defaults to the current time or the network ID of the forked blockchain, if configured. -d generates deterministic addresses based on a pre-defined deterministic seed, and -m uses a specific HD wallet mnemonic to generate initial addresses.

If successful, some files will be generated and saved to the ./data/save directory. If the data is already present in that directory, it will return to the same state.


The blockchain industry is a fast paced industry. Platforms that utilize blockchain technology have to adapt at a break-neck speed in order to remain relevant. As a result, these platforms might have to migrate from one blockchain instance to another to remain competitive and secure.

Data migration on the blockchain involves copying data from the source blockchain and recreating it on the target blockchain. However, this data can be very large, emphasizing the need to reduce the blockchain download time.

A snapshot is a file that contains the state of a blockchain at any given point in time. Snapshots allow new nodes to catch-up with the network by downloading only the most recent states from the blockchain, and they are commonly used to bootstrap blockchains, sidechains, and new nodes. A snapshot file format and bootstrap procedure for Bitcoin altcoins is presented in Bitcoin Forum. I hope you enjoyed this tutorial, and be sure to leave a comment if you have any questions. Happy coding!

Join organizations like Bitso and Coinsquare who use LogRocket to proactively monitor their Web3 apps

Client-side issues that impact users’ ability to activate and transact in your apps can drastically affect your bottom line. If you’re interested in monitoring UX issues, automatically surfacing JavaScript errors, and tracking slow network requests and component load time, try LogRocket.https://logrocket.com/signup/

LogRocket is like a DVR for web and mobile apps, recording everything that happens in your web app or site. Instead of guessing why problems happen, you can aggregate and report on key frontend performance metrics, replay user sessions along with application state, log network requests, and automatically surface all errors.

Modernize how you debug web and mobile apps — .

Hamsa Harcourt I'm Hamsa, a software engineer with a strong passion for building human-centric products. I love teaching concepts about JavaScript and the web at large.

Leave a Reply