Skip to content

Commit 5fdd574

Browse files
author
bohendo
committed
add yellow-paper cheat-sheet
1 parent 45cee0b commit 5fdd574

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed

learn_evm/yellow-paper.md

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
2+
# Ethereum Yellow Paper
3+
4+
So, you want to read the yellow paper. Before we dive in, keep in mind that the yellow paper is out of date and some in the community might refer to it as being depreciated. Check out the [BRANCHES.md](https://github.com/ethereum/yellowpaper/blob/master/BRANCHES.md) file of the [`yellowpaper` repository on github](https://github.com/ethereum/yellowpaper) to stay up-to-date on how closely this document tracks the latest version of the Ethereum protocol. At the time of writing, the yellow paper is up to date with the Berlin hardfork which occurred in April 2021.
5+
6+
For a more up-to-date reference, check out the [Ethereum Specification](https://ethereum.github.io/execution-specs/autoapi/ethereum/) which features a detailed description of each opcode *for each hardfork* in addition to reference implementations written in python.
7+
8+
That said, the yellow paper is still a rich resource for ramping up on the fundamentals of the Ethereum protocol. This document aims to provide some guidance and assistance in deciphering Ethereum's flagship specification.
9+
10+
As you review the following, keep in mind that additions and corrections are welcome and are covered by bounties from Trail of Bits. Join us in #ethereum on the [Empire Hacking Slack](https://empireslacking.herokuapp.com) to discuss Ethereum specifications and tool development.
11+
12+
## Mathematical Symbols
13+
14+
One challenging part of the yellow paper, for those of us who are not so well trained in formal mathematics, is comprehending the mathematical symbols. A cheat-sheet of some of these symbols is provided below
15+
16+
- ``: there exists
17+
- ``: for all
18+
- ``: and
19+
- ``: or
20+
21+
And some more Ethereum-specific symbols:
22+
23+
- `N_{H}`: 1,150,000 aka block number at which the protocol was upgraded from homestead to frontier.
24+
- `T`: a transaction eg `T = { n: nonce, p: gasPrice, g: gasLimit, t: to, v: value, i: initBytecode, d: data }`
25+
- `S()`: returns the sender of a transaction eg `S(T) = T.from`
26+
- `Λ`: (lambda) account creation function
27+
- `KEC`: Keccak SHA-3 hash function
28+
- `RLP`: Recursive Length Prefix encoding
29+
30+
## High-level glossary
31+
32+
The following are symbols and function representations that provide a high-level description of ethereum. Many of these symbols represent a data structure, the details of which are described in subsequent sections.
33+
34+
- `σ`: ethereum world state
35+
- `B`: block
36+
- `μ`: EVM state
37+
- `A`: accumulated transaction sub-state
38+
- `I`: execution environment
39+
- `o`: output of `H(μ,I)` ie null if we're good to go or a set of data if execution should halt
40+
- `Υ(σ,T) => σ'`: the transaction-level state transition function
41+
- `Π(σ,B) => σ'`: the block-level state transition function, processes all transactions then finalizes with Ω
42+
- `Ω(B,σ) => σ`: block-finalisation state transition function
43+
- `O(σ,μ,A,I)`: one iteration of the execution cycle
44+
- `H(μ,I) => o`: outputs null while execution should continue or a series if execution should halt.
45+
46+
## Ethereum World-State: σ
47+
48+
A mapping between addresses (external or contract) and account states. Saved as a Merkle-Patricia tree whose root is recorded on the blockchain backbone.
49+
50+
```
51+
σ = [ account1={...}, account2={...},
52+
account3= {
53+
n: nonce aka number of transactions sent by account3
54+
b: balance ie number of wei account3 controls
55+
s: storage root, hash of the merkle-patricia tree that contains this accounts long-term data store
56+
c: code, hash of the EVM bytecode that controls this account. If this equals the hash of an empty string, this is a non-contract account.
57+
}, ...
58+
]
59+
```
60+
61+
## The Block: B
62+
63+
```
64+
B = Block = {
65+
H: Header = {
66+
p: parentHash,
67+
o: ommersHash,
68+
c: beneficiary,
69+
r: stateRoot,
70+
t: transactionsRoot,
71+
e: receiptsRoot,
72+
b: logsBloomFilter,
73+
d: difficulty,
74+
i: number,
75+
l: gasLimit,
76+
g: gasUsed,
77+
s: timestamp,
78+
x: extraData,
79+
m: mixHash,
80+
n: nonce,
81+
},
82+
T: Transactions = [
83+
tx1, tx2...
84+
],
85+
U: Uncle block headers = [
86+
header1, header2..
87+
],
88+
R: Transaction Receipts = [
89+
receipt_1 = {
90+
σ: root hash of the ETH state after transaction 1 finishes executing,
91+
u: cumulative gas used immediately after this tx completes,
92+
b: bloom filter,
93+
l: set of logs created while executing this tx
94+
}
95+
]
96+
}
97+
```
98+
99+
## Execution Environment: I
100+
101+
```
102+
I = Execution Environment = {
103+
a: address(this) address of the account which owns the executing code
104+
o: tx.origin original sender of the tx that initialized this execution
105+
p: tx.gasPrice price of gas
106+
d: data aka byte array of method id & args
107+
s: sender of this tx or initiator of this execution
108+
v: value send along w this execution or transaction
109+
b: byte array of machine code to be executed
110+
H: header of the current block
111+
e: current stack depth
112+
}
113+
```
114+
115+
## EVM state: μ
116+
117+
The state of the EVM during execution. This is the data structure provided by the `debug_traceTransaction` JSON RPC method, see [this page](./tracing.md) for more details about using this method to investigate transaction execution.
118+
119+
```
120+
μ = {
121+
g: gas left
122+
pc: program counter ie index into which instruction of I.b to execute next
123+
m: memory contents, lazily initialized to 2^256 zeros
124+
i: number of words in memory
125+
s: stack contents
126+
}
127+
```
128+
129+
## Accrued sub-state: A
130+
131+
The data accumulated during tx execution that needs to be available at the end to finalize the transactions state changes.
132+
133+
```
134+
A = {
135+
s: suicide set ie the accounts to delete at the end of this tx
136+
l: logs
137+
t: touched accounts
138+
r: refunds eg gas received when storage is freed
139+
}
140+
```
141+
142+
## Contract Creation
143+
144+
If we send a transaction `tx` to create a contract, `tx.to` is set to `null` and we include a `tx.init` field that contains bytecode. This is NOT the bytecode run by the contract, rather it RETURNS the bytecode run by the contract ie the `tx.init` code is run ONCE at contract creation and never again.
145+
146+
If `T.to == 0` then this is a contract creation transaction and `T.init != null`, `T.data == null`

0 commit comments

Comments
 (0)