Repository navigation
Expand file tree
/
Copy pathAccountProof.lean
More file actions
244 lines (180 loc) · 9.93 KB
/
Copy pathAccountProof.lean
File metadata and controls
244 lines (180 loc) · 9.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
module
public import EthLib.Execution.State.Account
public import EthLib.Execution.Trie.Proof
/-!
# `State.AccountProof`
The check of an account proof, the answer of `eth_getProof`.
A light client holds a state root, and asks a node for one account and some of its storage slots.
The node answers with the fields of the account and the proof of the account in the state trie.
It adds a proof of each slot in the storage trie of the account.
The client then checks every claim against the state root alone.
[EIP-1186][eip-1186] defines the answer.
The [`AccountProof` schema][schema] of execution-apis gives its fields.
The account proof is a proof in the secured state trie, under the bytes of the address.
Its value is the RLP form of the account: nonce, balance, storage root and code hash.
Each slot proof is a proof in the secured storage trie, under the slot on 32 bytes.
Its value is the RLP form of the word.
A proof can also show that the address has no account, or that the slot is not set.
The word of a slot that is not set is the storage value `0`, so the answer must give `0`.
EIP-1186 does not say which fields the answer holds for an address without an account.
It refers the balance to `eth_getBalance`, and the nonce to `eth_getTransactionCount`.
The tests of execution-apis give such an address a [balance][balance] and a [nonce][nonce] of `0`.
They give the [word][word] `0` for a slot of it, so the answer must give `0` for each slot.
An address without an account has no storage, so the proofs of those slots are not read.
No source fixes which code hash or storage root the answer gives for an absent account.
This specification decides to accept two readings of each, and to reject any other value.
One reading is the zero hash, which [EIP-1052][eip-1052] gives as the code hash of no account.
The other reading is the value of an account with no code and no storage.
The tests give such an address the [empty code][code], whose hash is the hash of the empty string.
EIP-1186 also gives that code hash for an account without code.
The storage root of an account without storage is the root of the empty trie.
[eip-1186]: https://github.com/ethereum/EIPs/blob/46976b2a9bf99f727fb87dc8c809e3a3cdc6b445/EIPS/eip-1186.md
[schema]: https://github.com/ethereum/execution-apis/blob/3284ee80c3da3e516c53782dde75157cccf1ebdc/src/schemas/state.yaml
[balance]: https://github.com/ethereum/execution-apis/blob/3284ee80c3da3e516c53782dde75157cccf1ebdc/tests/eth_getBalance/get-balance-unknown-account.io
[nonce]: https://github.com/ethereum/execution-apis/blob/3284ee80c3da3e516c53782dde75157cccf1ebdc/tests/eth_getTransactionCount/get-nonce-unknown-account.io
[word]: https://github.com/ethereum/execution-apis/blob/3284ee80c3da3e516c53782dde75157cccf1ebdc/tests/eth_getStorageAt/get-storage-unknown-account.io
[code]: https://github.com/ethereum/execution-apis/blob/3284ee80c3da3e516c53782dde75157cccf1ebdc/tests/eth_getCode/get-code-unknown-account.io
[eip-1052]: https://github.com/ethereum/EIPs/blob/46976b2a9bf99f727fb87dc8c809e3a3cdc6b445/EIPS/eip-1052.md#specification
-/
@[expose] public section
namespace EthLib.Execution.State
open EthLib.Base
open EthLib.Execution.Trie
open EthLib.Serialization.Rlp
/-- One slot of the answer: the slot on 32 bytes, the word that the answer gives, and its proof. -/
structure StorageProof where
/-- The slot, as 32 big-endian bytes. -/
key : Bytes32
/-- The word that the answer gives for the slot. -/
value : U256
/-- The encodings of the nodes on the path of the slot, from the storage root down. -/
proof : List ByteArray
/-- The answer of `eth_getProof`: the fields of one account, and the proofs of it and of some slots.
-/
structure AccountProof where
/-- The address of the account. -/
address : Address
/-- The nonce that the answer gives. -/
nonce : Nat
/-- The balance that the answer gives. -/
balance : U256
/-- The code hash that the answer gives. -/
codeHash : Hash32
/-- The storage root that the answer gives. -/
storageHash : Hash32
/-- The encodings of the nodes on the path of the address, from the state root down. -/
accountProof : List ByteArray
/-- The slots, in the order of the answer. -/
storageProof : List StorageProof
/-- Why an answer of `eth_getProof` does not hold against a state root. -/
inductive AccountProofError where
/-- The account proof does not verify against the state root. -/
| account (error : ProofError)
/-- The value under the address is not the RLP form of an account. -/
| accountEncoding
/-- The fields of the answer are not those that the account proof shows.
For an absent account, a number is not `0`, or a hash is neither zero nor the empty value. -/
| accountMismatch
/-- The proof of the slot at an index, from 0, does not verify against the storage root. -/
| slot (index : Nat) (error : ProofError)
/-- The value under the slot at an index is not the RLP form of a word. -/
| slotEncoding (index : Nat)
/-- The word that the answer gives for the slot at an index is not the one its proof shows. -/
| slotMismatch (index : Nat)
deriving DecidableEq, Repr
/-- The stable code of an error, one per constructor, for the C exports.
The codes start at 501, above the codes of the errors of a proof.
A new constructor takes a new code, so that each code keeps its meaning. -/
def AccountProofError.code : AccountProofError → UInt32
| .account _ => 501
| .accountEncoding => 502
| .accountMismatch => 503
| .slot .. => 504
| .slotEncoding _ => 505
| .slotMismatch _ => 506
/-- What the proof of a slot shows: the slot is set, or it is not. -/
inductive SlotOutcome where
/-- The slot is set, and holds the word that the answer gives. -/
| set
/-- The slot is not set, and the answer gives it the word `0`. -/
| absent
deriving DecidableEq, Repr
/-- What the proofs of an answer show. -/
inductive AccountOutcome where
/-- The account exists with the fields of the answer, and each slot is as the outcome says. -/
| present (slots : List SlotOutcome)
/-- The address has no account, the answer gives it no value, and each slot gives the word `0`. -/
| absent
deriving DecidableEq, Repr
namespace AccountProof
/-- The account that an answer gives, without its storage. -/
def account (p : AccountProof) : Account := ⟨p.nonce, p.balance, p.codeHash⟩
/-- The root of the empty trie: the hash of `0x80`, the encoding of the empty string. -/
def emptyTrieRoot (hash : ByteArray → Hash32) : Hash32 := hash ⟨#[0x80]⟩
/-- Whether the fields of an answer fit an address with no account.
The nonce and the balance are `0`.
The code hash is zero or the Keccak-256 hash of the empty string.
The storage root is zero or the root of the empty trie.
The two hashes are judged apart, so a zero code hash may come with the root of the empty trie. -/
def absentFields (hash : ByteArray → Hash32) (p : AccountProof) : Bool :=
p.nonce == 0 && p.balance == 0 &&
(p.codeHash == Vector.replicate 32 0 || p.codeHash == EMPTY_CODE_HASH) &&
(p.storageHash == Vector.replicate 32 0 || p.storageHash == emptyTrieRoot hash)
/-- Check the account of an answer against the state root.
It is `true` when the proof shows the account.
The fields and the storage root of the account must then be those of the answer.
It is `false` when the proof shows that the address has no account.
The fields of the answer must then fit an address with no account. -/
def verifyAccount (hash : ByteArray → Hash32) (stateRoot : Hash32) (p : AccountProof) :
Except AccountProofError Bool := do
let value ← (verifyProof hash stateRoot true ⟨p.address.toArray⟩ p.accountProof).mapError
.account
match value with
| some bytes =>
let (account, storageRoot) ← (decodeAccount bytes).mapError fun _ => .accountEncoding
unless account = p.account ∧ storageRoot = p.storageHash do throw .accountMismatch
return true
| none =>
unless p.absentFields hash do throw .accountMismatch
return false
/-- Check the slot at an index against the storage root.
A set slot must hold the word of the answer.
A slot that is not set is absent, and the answer must give it the word `0`. -/
def verifySlot (hash : ByteArray → Hash32) (storageRoot : Hash32) (index : Nat)
(s : StorageProof) : Except AccountProofError SlotOutcome := do
let value ← (verifyProof hash storageRoot true ⟨s.key.toArray⟩ s.proof).mapError
(.slot index)
match value with
| some bytes =>
let word ← (Codec.decode bytes : Except RlpError U256).mapError fun _ => .slotEncoding index
unless word = s.value do throw (.slotMismatch index)
return .set
| none =>
unless s.value = 0 do throw (.slotMismatch index)
return .absent
/-- Check each slot of the answer against its storage root, in order. -/
def verifySlots (hash : ByteArray → Hash32) (storageRoot : Hash32) :
Nat → List StorageProof → Except AccountProofError (List SlotOutcome)
| _, [] => pure []
| index, s :: rest => do
let outcome ← verifySlot hash storageRoot index s
return outcome :: (← verifySlots hash storageRoot (index + 1) rest)
/-- Check that each slot of an absent account gives the word `0`, in order.
The account has no storage, so the proofs of its slots are not read. -/
def verifyAbsentSlots : Nat → List StorageProof → Except AccountProofError Unit
| _, [] => pure ()
| index, s :: rest => do
unless s.value = 0 do throw (.slotMismatch index)
verifyAbsentSlots (index + 1) rest
/-- Check a whole answer of `eth_getProof` against a state root: the account, then each slot.
The slots of an existing account are checked against its storage root.
Those of an absent account must each give the word `0`. -/
def verify (hash : ByteArray → Hash32) (stateRoot : Hash32) (p : AccountProof) :
Except AccountProofError AccountOutcome := do
if ← p.verifyAccount hash stateRoot then
return .present (← verifySlots hash p.storageHash 0 p.storageProof)
else
verifyAbsentSlots 0 p.storageProof
return .absent
end AccountProof
end EthLib.Execution.State