Introduction
This blog post provides a technical introduction of the Ethereum blockchain by sourcing information from credible and authoritative sources, and organizing them in a coherent fashion such that the materials can be quickly grasped by most software practitioners. All of information sources are listed in Reference section. Readers are encouraged to dive deeper by reading the source materials also. For those who have very little time or patience for technical deep dive, the next section offer a one-paragraph summary of what Ethereum blockchain is. For those who wish to dive deeper, the summary paragraph provides a “big picture” by which readers can assemble the detail information from the rest of this blog post.
Ethereum blockchain in one paragraph
Ethereum blockchain is a network of computing nodes working together to realize tamper- and censor-proof transaction processing following a common transactional protocol. The integrity and privacy of the transactions, data and programs (smart contracts) are protected by 256-bit hash encryption, with the hash values used as keys to retrieve encrypted data and programs from storage. As processing requires resources of value, such as server time and electricity, Ethereum defines ‘ether’ as a unit of cryptocurrency, and ‘gas’ as a unit of processing to facilitate exchanges of currency for work among the participants.
Ethereum as a state-machine
Ethereum blockchain can be viewed as a decentralized network of Ethereum nodes each implementing an unbounded state machine. The network’s state, called world state, is characterized by the current state of all the accounts in the blockchain. State changes are effected by transactions TX[i]
, which can be codified as follow:
S[i+1] = APPLY(S[i],TX[i])
When all transactions in a block are processed, block reward is paid to the Beneficiary, and the blockchain is considered finalized. The process is graphically illustrated below.
Transactions are processed in blocks, with each block containing a list of one or more transactions. A transaction contains the following information:
- Nonce – number of transactions sent by the sender.
- Gas price – number of wei to pay the network per unit of gas.
- Gas limit – maximum amount of gas to be used while executing a transaction.
- To – 20 character recipient address.
- Value – number of wei to be transferred to recipient of a message call.
- v, r, s – value for transaction’s signature from which the public key can be derived.
There are two types of transactions: message calls and contract creation. Most are in form of message call. When a message call is sent,
Before a transaction is executed, validation is performed by checking:
- RLP is well-formed
- Transaction signature is valid
- Transaction nonce is valid
- Gas_limit set is sufficient for the transaction
- Sender’s account balance is sufficient to pay for the up-front cost
Successful validation leads to the execution of the transaction.
Ether and gas
“Ether” (ETH) is the main internal crypto-currency of Ethereum, and is used to pay transaction fees to account for the real-world processing cost. There are other denominations, smallest being a wei:
Gas is the fundamental cost unit used by Ethereum blockchain to quantify operations cost. Gas and ETH are related by gas_price, which is the number of wei to pay for unit of gas. Generally the entity that creates the transaction set gas_price and gas_limit; and the miners determine which transaction to process first, and they usually choose transactions that pay higher gas_price first. If gas_limit is reached before processing is done, the unfinished transaction is returned to the pool. A receipt is produced after each transaction is processed and linked to the block’s Receipt Root.
What’s an account?
In Ethereum, there are two types of accounts: externally owned accounts, controlled by private keys, and contract accounts, controlled by their contract code. An externally owned account has no code, and one can send messages from an externally owned account by creating and signing a transaction. In a contract account, every time the contract account receives a message its code activates, allowing it to read and write to internal storage and send other messages or create contracts in turn. The entirety of accounts in the blockchain defines the world state of the blockchain.
Each account has an address, and the account state is made of four variables:
- nonce – number of transactions sent from this address.
- balance – the amount of Wei owned by this account.
- storage_root – 256-bit hash of root node of a Merkle Patricia Tree that encodes the storage contents of the account.
- code_hash – 256-bit hash of the EVM code of this account’s contract.
Storage and Merkle Patricia Tree
In Ethereum blockchain, storage of hash-encrypted contents is usually done using an off-chain data base application capable of efficient lookup of byte array when given a hash value using the Merkle Patricia Tree algorithm. The Inter-Planetary File System (IPFS) is such data base application commonly used with Ethereum blockchain. The storage_root and code_hash in the account data structure are used to lookup stored data and EVM code from the data base.
In a Merkle Patricia Tree, each node in the tree is either a branch node, or a leaf node. Each branch node can have up to 16 child branches; while each leaf node holds the storage. A hash value is used to look up a leaf node storage–each nibble in the hash value represent a 16-way branch node. Each nibble represent successive level of descent down the tree until the leaf node corresponding to the hash value is reached. Below figure shows the Merkle-Patricia Tree search structure.
What’s in a block?
A block consists of a 15-element block header, 1-element Ommers (uncle) block header, and a transaction list. The block header contains the following 15 elements:
- Parent hash – 256-bit hash of parent block header.
- Ommers hash – 256-bit hash of uncle block header.
- Beneficiary – 20-byte address to transfer block reward.
- State root – 256-bit hash of root node of state trie after the block is processed.
- Transaction root – 256-bit hash of root node of transaction trie after the block is processed.
- Receipts root – 256-bit hash of root node of receipt trie.
- Logs bloom – bloom filter.
- Difficulty – difficulty of the block calculated from previous block’s difficulty and its timestamp; generally more ancestor blocks, higher the difficulty.
- Number – number of ancestor blocks behind the current block.
- Gas limit – current max gas expenditure per block.
- Gas used – total gas used by processing this block.
- Timestamp – Unix time at the block’s inception.
- Extra data – 32 byte or less byte array containing any extra data relevant to this block.
- Mix hash – 32-byte has that verifies sufficient compute has been done on this block.
- Nonce – 8-byte has that verifies a sufficient amount of computation has been done on this block (part of proof-of-work)
Ommers block header also has 15-element like the standard header above. The reason for the Ommers block is discussed next.
Ommers (uncle) block
Ommers block, more commonly referred to as uncle block, is used when two miners in the blockchain network process the “next” block at the same time. In this case, the chain forks. Take for instance where two miners, A and B, working on a next block to be linked to block #100. If both miners introduce to the network their respective “next” block at the same time, and both blocks are legitimate and valid. This is a very rare event, but if happened the chain should fork, and both branches maintained until one of them become longer. By convention, the longer chain becomes the valid chain, while the other becomes invalid.
Ethereum allows up to 7 levels of uncle blocks which would correspond to 1 orphan block and 6 stale blocks after it. Uncle block are rewarded less than the standard block, calculated according to the following formula:
([Uncle block number] + 8 – [Block number]) * [Ethereum reward] / 8
In other words, if the standard block reward is 3 ETH, the uncle reward would be 1/8th less.
Thank you for the article. It was very i formative! 🙂