Professional Documents
Culture Documents
An Introduction
The Ethereum Architecture
› “Ethereum is designed to be a
general-purpose programmable Block 1 Block 2 Block 3
blockchain that runs a virtual
machine capable of executing code of
arbitrary and unbounded complexity.” Consensus Layer
Transaction pool
Node
Accounts
Contract account & Externally-owned account
UTXO vs Account Model
https://ethereum.stackexchange.com/questions/15
288/ethereum-merkle-tree-explanation/15294
Account-based Model
› Similar to how we work in the traditional banking world. Each account has states
before and after a transaction takes place.
Example:
https://etherscan.io/tx/0xe75277702021aef3473910b
0c61c73e4e9ce1d4f8dfb9e605c674f7f63025250
Account-based Model
› The Ethereum blockchain is a record of:
– Transactions
– Account states and smart contracts
Private Key
*Ethereum runs Proof-of-Stake (PoS) now instead of Proof-of-Work (PoW). Miners are now validators.
EOA to EOA
*Ethereum runs Proof-of-Stake (PoS) now instead of Proof-of-Work (PoW). Miners are now validators.
EOA to CA
Smart Contract
The Ethereum Architecture
› “Ethereum is designed to be a
general-purpose programmable Block 1 Block 2 Block 3
blockchain that runs a virtual
machine capable of executing code of
arbitrary and unbounded complexity.” Consensus Layer
Transaction pool
Node
Smart Contract Example
Turing Completeness
https://www.youtube.com/watch?v=dNRDvLACg5Q
Smart Contract Example
17
Ethereum Virtual Machine
Example of bytecode: https://etherscan.io/address/0xf0155486a14539f784739be1c02e93f28eb8e960#code
18
The Halting Problem
There is no way for a computer program to
determine if it will ever stop executing.
If there is such a program, the halting problem would not even exist.
The Halting Problem
How do we determine if a code would ever stop executing?
› If a smart contract does not terminate, the network will come into a stand
still.
What is Gas? A metering mechanism to ensure that smart
contracts will stop execution eventually.
https://etherscan.io/address/0xdac17f958d2ee523a2206206994597c13d831ec7#code
What is Gas?
https://medium.com/sunnya97/understanding-ether-vs-gas-82ce2f1dc560
What is Gas?
› A transaction must specify the maximum amount of gas the sender (the person
who invokes the smart contract) will use to run the smart contract (gasLimit).
› The sender also needs to specify how much he is willing to pay for one unit of
gas (gasPrice).
› The higher the gasPrice, the faster the transaction will be included in the
blockchain.
https://ethgasstation.info/index.php
Gas
› What if you did not specify enough gas to complete the contract
execution?
– Out-of-Gas exception: Contract execution terminated; Gas given to miner
https://etherscan.io/tx/0x229212224c1970a06b04649944b5905d9a7f734d557274f4d3d40b9d5ff60c38
› What if you specified too much gas for the contract execution?
– Unutilised gas refunded to the sender
Invoke smart contract • Runs a smart contract and execute the contract code
Structure of a Transaction
Nonce • Keeps track of the number of transaction originated from the sender
gasLimit • Maximum amount of gas that the sender is willing to use for this transaction
If the withdrawal amount is 0.01 ether (0x2386f26fc10000), the data will be:
2e1a7d4d000000000000000000000000000000000000000000000000002386f26fc10000
Ethereum Consensus
Proof-of-Work; transitioned to Proof-of-Stake in 2022
Consensus
› Proof-of-Work using Ethash – a memory-hard consensus protocol.
• Initial size ~ 1GB
• Updated every 30,000
› A high-level overview: blocks (~ 125 hours)
– Compute a seed by hashing the previous seed.
› First seed is a hash of 32 bytes of zeroes.
– Compute a pseudorandom cache from the seed.
– Generate a dataset from the cache.
– Keccak256(block header + nonce).
– Use the above value to select a slice in the dataset.
– Hash the slice, block header, and nonce, and check if the resulting hash value satisfies
a difficulty level.
› Successful miners will receive ethers as mining reward in addition to the gas
fees.
The Ethash Algorithm
› Notice that Ethereum block header
hashes displayed in the explorer do
not need to start with zeros.
› Bitcoin:
hash(block header, nonce) < TARGET.
› Ethereum:
Ethash(hash(block header), nonce) <
TARGET.
https://etherscan.io/block/0x93863ebda51e7ab6da13de
fabd92d35b0c3ec43b67e7cf04267a1b946ca7031b
Consensus
› Proof-of-Stake: Ethereum 2.0
› A high-level overview:
– Requires users to stake 32 ETH to become a validator in the network.
– Validator can create new blocks with a set of transactions.
– Validators are algorithmically chosen by the beacon chain to propose new blocks.
– The beacon chain manages the validators: registers their stake deposits, issues their rewards and
penalties.
– If a validator isn't chosen to propose a new block, they'll have to attest to another validator's
proposal.
– The beacon chain records the attestations.
– So long as 2/3 of the validators agree, the block is finalised.
https://beaconscan.com/
Ommers/Uncles (retired)
https://etherscan.io/uncles
98
97 is my parent
95 96 97
98
97 is my parent
97 is my ommer
(I will include 97)
95 96 97
95 96 97 98
97
Ommers/Uncles (retired)
Rb is the current block mining reward
Mining reward =
Number of ommers
𝑅𝑏 + 𝑅𝑏
32
95 96 97 98
Mining reward = 97
1
𝑅𝑏 + (Ommer block position − current block position)𝑅𝑏
8
Ommers/Uncles (retired)
› Mining reward of 1st ommer is 7/8 of mining rewards.
› Each block can have a maximum of two ommers. Miners always build on the
“heavier” chain.
– “Weight” concept (Ethereum) replaces the “length” concept (Bitcoin)
› Transactions in ommers are not included as blockchain information; they are just
there for the selection of the chain that is consensually accepted
Ommers/Uncles (retired)
95 96 97 98 99 100
97 95
Ommers/Uncles (retired)
Let Rb be the current block reward.
Number of ommers
𝑅𝑏 + 𝑅𝑏
32
1
𝑅𝑏 + (Ommer block position − current block position)𝑅𝑏
8
Is always a negative value – the further the ommer is
from current block, the lesser the ommer reward will be
Ommers/Uncles (retired)
Why is this necessary?
› Decentralises the network – allow weaker miners who do not discover blocks
that are added to the main chain to also profit.
› The aim is to keep the user base large for security purpose.
The Oracles
Provide external data for smart contract execution
Creates
Validators
https://etherscan.io/address/0x06012c8cf97b
ead5deae237070f9587f8e7a266d#code
dApps (decentralised Applications)
Webpage interacts with smart contract using
javascript (Web3.JS)
Contract written in
Solidity.
› In order to maintain consensus, EVM execution must be totally deterministic and based
only on the shared context of the Ethereum state and signed transactions.
› Hence, Oracles must be trusted (behave honestly) and authenticated (verified identity).
https://github.com/ethereum/yellowpaper
Blocks
Block
› Note: In addition, a block also contains ommer block headers and a series of transactions in ommer block.
› A Bloom filter is a string of zeros and ones. To add an element X into the bloom filter,
apply k different hash functions to X. Based on the result, set the k positions in the Bloom
filter to be ‘1’.
› The length of Bloom filter, selection of k, number of elements to be added will affect the
false positive rate.
Telling you that an element is a member when it is not!
Block
parentHash Keccak256 hash of parent block’s header
ommersHash Keccak256 hash of ommers
beneficiary The “coinbase transaction” equivalence of Bitcoin
stateRoot Keccak256 hash of account state
transactionsRoot Keccak256 hash of transactions in the block
receiptsRoot Keccak256 hash of receipts for transactions in the block
logsBloom Bloom filter for transactions
Difficulty PoW difficulty
number Number of ancestor blocks
gasLimit Gas limit per block
gasUsed Gas used by transactions in the block
timestamp Block’s inception time
An Ethereum block size is limited by the
extraData Arbitrary data (32 bytes or less)
amount of gas that a block can contain.
mixHash For PoW Why?
nonce For PoW
TEMPORARY
FORKS
All nodes has
Block P as the
latest block.
TEMPORARY
FORKS
Left hand side
found Block A as
the next block.