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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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:

  1. Initial State: Before any transactions, the blockchain has an initial state. This state includes the existing account balances, deployed contract code, and contract storage.
  2. 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.
  3. 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.
  4. 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:

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:

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

More articles