1

I am really confused about how ERC20 Token Balances are stored on the chain. It appears that all balance data are stored as an member mapping variable:

contract TestCoin is ERC20Interface {
    ...
    mapping(address => uint) balances;
    ...

I am wondering how many copies are stored on the Blockchain. Say balances contains 10K records and the highest block number is 100. Now I make a token transfer and 2 records in balances changed and we get a new balances, and this transfer is confirmed by a new block 101.

 <------- [Block 100] <------- [Block 101]
              ^                    ^
              |                    |
           balance1             balance2

Where does the balance is stored? Is it possible to retrieve balances in block 100? (I guess we must be able to do so, because the blockchain could rollback).

I am using geth/web3/eth.

TylerH
  • 20,799
  • 66
  • 75
  • 101
KurtZ
  • 11
  • 2

1 Answers1

1

The Ethereum blockchain has the notion of "state", where every contract (and address) has a state, and every block creates a delta (change) to this state. To get the current balance, you have to scan the whole blockchain.

So if I send you 50 ether, the block that will contain this transaction will effectively cause a -50 Eth in my account and 50 Eth to yours.

Same applies to that mapping you show there. The mapping contains only relevant addresses (addresses that are involved), and a delta in these addresses when tokens are sent to them in each block, when a relevant transaction happens.

Ths is the difference between an "accounting model", which Ethereum uses, and a UTXO model, which Bitcoin uses.

The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
  • 1
    Thank you for the answer. I understand how ETH works. Here I am talking about ERC20 tokens, which store the entire account/balance in the smart contract as a mapping. – KurtZ Apr 02 '18 at 03:36