Understanding Blockchain State and Contract Storage: A Simplified Guide to Diffs
Blockchain technology, while revolutionary, can often seem like a maze of complex concepts and technical jargon. Among these, understanding the role of state and storage, as well as state and storage differences, or “diffs”, is crucial for anyone looking to dive deeper into how blockchain works, particularly in Ethereum and EVM-compatible chains.
Let’s break these down into simpler terms.

TL;DR – State diffs provide a macro view of the changes on the Ethereum blockchain, while storage diffs offer a micro view of changes within the storage of a specific smart contract.
1. What is storage in blockchain?
Imagine blockchain storage as a vast, global repository designed to keep track of changes in smart contract state. It’s like a colossal key-value store, where each piece of data is stored in pairs – one key and one value. In Ethereum, this storage system is using a structure called the Merkle Patricia Tree, which efficiently organizes data within smart contracts.
Each smart contract on the Ethereum network has its own dedicated storage space. Think of it as a massive map with 2^256 slots, each capable of holding 32 bytes of data. This structure is similar to blocks in traditional file systems, which efficiently manage larger memory spaces.
However, it’s important to remember two things: 1) that interacting with this storage – whether reading or writing data – comes with a cost, often significant due to the gas consumption involved, and 2) reading storage is typically not user-friendly because the key pairs are hashed to produce a non-human readable record (e.g. instead of seeing address and balance for you would see just a hexadecimal string.
2. What are storage diffs?
Storage diffs are essentially the changes that occur in the storage of a smart contract between two consecutive states. This change is the result of a transaction, or more specifically one or more calls within a transaction. These diffs are crucial in understanding how state of a contract has evolved over time. Here’s a step-by-step breakdown of how storage diffs occur:
- Initial State: When a smart contract is deployed, it starts in an initial state as defined by its constructor code. This code initializes the contract’s storage variables, setting up the contract’s initial conditions.
- Transactions and Interactions: Transactions and function calls within them, interact with the contract, providing instructions that can change the contract’s state, depending on how the contract’s code is programmed to respond to different inputs.
- Recording Changes: The Ethereum Virtual Machine (EVM) tracks these state changes during transaction execution. It logs which storage slots are altered and records their updated values. These are hashed so that they are not natively human readable.
- Final State: The EVM updates the contract’s state post-transaction processing, with this new state reflecting all changes and being recorded on the blockchain.
- Gas Costs: Changes to storage incur gas costs, fees for computational resources used, with greater complexity and size of changes leading to higher costs.
Need more details and some techy examples, continue reading our Intro to Storage Diffs (https://docs.tokenflow.live/storage-and-storage-diffs/storage-diffs-some-theory).
3. What is state in blockchain?
Think of the state in a blockchain as a snapshot of all balances, contracts, and data at a given point in time. Unlike storage, which is specific to individual smart contracts, the state encompasses the entire blockchain network. In Ethereum, the state includes account balances, smart contract code, and contract storage. It’s like the aggregate of a comprehensive ledger that records the current status of all these elements.
The state is not static; it’s dynamic and changes with every block. Each transaction in a block, and each call in a transaction, has the potential to modify the state — whether it’s a transfer of Ether, the execution of a smart contract, or the creation of a new contract.
4. What are state diffs?
Ethereum’s state transition system is the process through which the state changes, resulting in state diffs. Here’s how it works:
- Initial State: Before any transactions, the blockchain has an initial state. This state includes the existing account balances, deployed contract code, and contract storage.
- Executing Transactions: Each transaction in a block affects the state. For example, a simple Ether transfer changes account balances, while a smart contract execution might alter contract storage or create new contracts.
- Processing Changes: The Ethereum Virtual Machine (EVM) processes these transactions, applying the changes to the blockchain’s state. This processing includes complex computations, interactions with smart contracts, and alterations to account data.
- New State: After all transactions in a block are processed, a new state emerges. This state is a direct result of the transactions and is added to the blockchain as part of the new block.
Just like with storage, modifying the state incurs gas costs. These costs are calculated based on the complexity and resource requirements of the transactions. State changes, particularly those involving smart contracts, can be gas-intensive due to the computational work needed.
In Token Flow’s data, State Diffs show changes to Ether, nonce, gas etc as well as contract changes and Storage Diffs show changes to smart contract variables. A transaction (and its calls) will often result in both State and Storage diffs – for example a simple transfer of an ERC20 token will result in Storage Diffs that reflect the changed token balances for the sender and receiver, and State Diffs will reflect that a transaction was submitted (so nonce changed), and that gas was spent by the Sender to make the transaction. In contrast, a simple transfer of Ether will result in a State Diff (balance change, gas spent, nonce change) but will not result in a Storage Diff because Ether is not a token created by a smart contract.
5. Why state diffs and storage diffs matter?
Understanding state and storage diffs goes beyond mere technical knowledge; it’s a strategic necessity in blockchain development. These differences play a crucial role in various aspects:
- Contract Design and Upgrades: Deep understanding of state and storage is crucial for creating cost-effective contracts and ensuring data integrity during upgrades. They also allow analysts to systematically see if a contract has been upgraded, is critical for things like bridge contracts which hold a lot of locked value.
- Smart Contract Interactions: It’s essential for developers to understand how their contracts impact and are impacted by the state and storage. This knowledge is key to managing complex interactions and guaranteeing that contracts operate as intended in the broader blockchain ecosystem.
- Gas Optimisation and Enhanced Contract Performance: Grasping how contract interactions affect storage is vital for reducing gas consumption and improving overall performance. Efficiently using storage reduces costs and enhances contract functionality.
- Debugging and Auditing: Storage diffs offer detailed logs of changes in contract data, while state diffs capture wider blockchain modifications. Both are integral to ensuring contract reliability, security, and conformity to designed logic.
- Blockchain Integrity: State represents the current status of the blockchain. Accurate maintenance and understanding of state diffs are essential for maintaining the network’s reliability and trust.
- Blockchain Performance: Effective management of state and storage changes leads to better resource usage and boosting blockchain performance.
- Unbiased truth: Querying storage to understand what has happened on-chain is going straight to the source – it doesn’t rely on whether an event was emitted (e.g. in the case of a rebase) or whether events were excluded by the smart contract developer to save gas.
6. Token Flow’s approach to state and storage
Blockchain state diffs and contract storage diffs are included in our on-chain datasets structured for advanced analytics. Our Approach:
- Complete blockchain history: We’ve reprocessed and decoded the entire blockchain, from Genesis to the tip of the chain, capturing every state and storage change across all contracts.
- Diffs at block, transaction and call levels: As transaction complexity increases, detailed understanding of state changes becomes critical, so we tracking chang at all levels, as well as reverts that would have resulted in a diff but didn’t because the call or transaction reverted.
- Human readable storage diffs: Working with hashes makes sense for machines but not for humans. Our storage diffs, provided in a preimage form, covering hashmaps and dynamic arrays, ensuring slot and keys information is always available.
Relying on state and storage diffs for blockchain analytics is a new paradigm and it requires a change in mindset for all data analysts. The truth is that it offers much deeper, more reliable and more accurate insights into chain data you’re working on.
It’s the change you didn’t know you needed – until now.
To get started with our state and storage diff data, sign up for a free trial here.
access data