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?

closed as too broad by goodvibration, Richard Horrocks, Ismael, shane, Achala Dissanayake Apr 8 at 5:24

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


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 every executed in Ethereum.

Second part, i.e. blockchain state, is basically a result of applying all the transaction 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, 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 Merke Patricia tree, path from root to leaf defines 32-byte key, and leaf data is a 32-bit value.

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.