Please help me out with these questions.

1)Where does the contract stores the data?

2)when we access the value of a variable form of a contract, how does it retrieve the latest value?

3)how is the new value of a variable is linked to the original contract? what happens to fetch it?


2 Answers 2


Short answer

Every smart contract deployed into Ethereum blockchain has its own personal database, known as smart contract storage. This database is able to store 32-byte values addressed by 32-byte keys. Smart contract may write into its storage via SSTORE opcode and read from it via SLOAD.

Long answer

Each Ethereum node, excluding so called "light" nodes, maintains a large database holding blockchain data. Basically, this data consists of two parts: block data, and blockchain state.

First part, i.e. block data, is basically a collection of all live blocks ever mined. Live block is a block that is part of a blockchain, i.e. that wasn't dropped during chain reorganization. Nodes download blocks from each other, so this information is easily available to everyone. Each block contains an ordered list of transactions, and, as long as blocks are ordered themselves, block data could be considered as one big log of all transactions ever executed in Ethereum.

Second part, i.e. blockchain state, is basically a result of applying all the transactions in proper order to the well-known initial state. Blockchain state contains information such as account balances, byte code of deployed smart contracts, and, among other things, storage of all live smart contracts.

Each block includes hash of blockchain state as it should be after applying all the transactions from this block. Thus, each node may validate new block by applying block's transaction to the blockchain state corresponding to the previous block, calculating hash of the resulting blockchain state, and comparing to the hash included into the new block. This guarantees that all nodes execute all transactions in the same way, even if they use different software.

Usually, nodes do not directly exchange blockchain state, but in "fast" synchronization mode node is able to download snapshot of blockchain state from other nodes. Though, this happens only once, i.e. when "fast" node initializes its database.

So for your questions:

  1. Smart contract stores data in its own private part of blockchain state, every non-light node has a copy of this state, and, while usually not directly exchanging blockchain states, nodes use hashes to make sure their copies of blockchain state are in sync.
  2. When smart contract accesses its state variable, node, that executes corresponding transaction, reads value of this variable from local copy of blockchain state.
  3. Blockchain state is organized as a so called Merkle Patricia tree, where path from root to the leaf defines Ethereum address, and leaf data contains address state. For smart contract addresses, leaf data contains current balance, nonce, smart contract byte code, and a reference to a separate Merkle Patricia tree holding storage of this smart contract. In this another Merkle Patricia tree, path from root to leaf defines 32-byte key, and leaf data is a 32-byte value.
  • In your short answer, you specify the smart contract database is able to store 32-byte values addressed by 32-byte keys. But in item #3 of your conclusion, you reference 32-byte keys addressing 32-bit values. Could you clarify? Mar 30, 2021 at 12:54
  • @JimRubenstein It was just a typo Mar 31, 2021 at 15:48
  • @JimRubenstein - would be great if you please help out / mention the correct phrase
    – sqlchild
    Jun 11, 2022 at 23:52
  • @sqlchild 32 byte keys/values is correct. Jun 20, 2022 at 14:56

1) Smart-contract stores data in a block of blockchain if that data (variable) was declared as "storage". 2) I think smart contract finds the latest value by seeking for address of that variable and fetch it 3) -

Not the answer you're looking for? Browse other questions tagged or ask your own question.