-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlib.rs
More file actions
169 lines (145 loc) · 6.39 KB
/
lib.rs
File metadata and controls
169 lines (145 loc) · 6.39 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
use alloy_primitives::{keccak256, map::HashMap, Address, Bytes, B256};
use alloy_rpc_types::EIP1186AccountProofResponse;
use reth_trie::{AccountProof, HashedPostState, HashedStorage, TrieAccount};
use serde::{Deserialize, Serialize};
#[cfg(feature = "execution-witness")]
mod execution_witness;
/// Module containing MPT code adapted from `zeth`.
mod mpt;
pub use mpt::Error;
use mpt::{
mpt_from_proof, parse_proof, proofs_to_tries, resolve_nodes, transition_proofs_to_tries,
MptNode,
};
/// Ethereum state trie and account storage tries.
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EthereumState {
pub state_trie: MptNode,
pub storage_tries: HashMap<B256, MptNode>,
}
impl EthereumState {
/// Builds Ethereum state tries from relevant proofs before and after a state transition.
pub fn from_transition_proofs(
state_root: B256,
parent_proofs: &HashMap<Address, AccountProof>,
proofs: &HashMap<Address, AccountProof>,
) -> Result<Self, FromProofError> {
transition_proofs_to_tries(state_root, parent_proofs, proofs)
}
/// Builds Ethereum state tries from relevant proofs from a given state.
pub fn from_proofs(
state_root: B256,
proofs: &HashMap<Address, AccountProof>,
) -> Result<Self, FromProofError> {
proofs_to_tries(state_root, proofs)
}
/// Builds Ethereum state tries from a EIP-1186 proof.
pub fn from_account_proof(proof: EIP1186AccountProofResponse) -> Result<Self, FromProofError> {
let mut storage_tries = HashMap::with_hasher(Default::default());
let mut storage_nodes = HashMap::with_hasher(Default::default());
let mut storage_root_node = MptNode::default();
for storage_proof in &proof.storage_proof {
let proof_nodes = parse_proof(&storage_proof.proof)?;
mpt_from_proof(&proof_nodes)?;
// the first node in the proof is the root
if let Some(node) = proof_nodes.first() {
storage_root_node = node.clone();
}
proof_nodes.into_iter().for_each(|node| {
storage_nodes.insert(node.reference(), node);
});
}
storage_tries
.insert(keccak256(proof.address), resolve_nodes(&storage_root_node, &storage_nodes));
let state = EthereumState {
state_trie: MptNode::from_account_proof(&proof.account_proof)?,
storage_tries,
};
Ok(state)
}
#[cfg(feature = "execution-witness")]
pub fn from_execution_witness(state: &Vec<Bytes>, pre_state_root: B256) -> Self {
let (state_trie, storage_tries) =
execution_witness::build_validated_tries(state, pre_state_root).unwrap();
Self { state_trie, storage_tries }
}
/// Mutates state based on diffs provided in [`HashedPostState`].
pub fn update(&mut self, post_state: &HashedPostState) {
for (hashed_address, account) in post_state.accounts.iter() {
match account {
Some(account) => {
let state_storage = &post_state
.storages
.get(hashed_address)
.cloned()
.unwrap_or_else(|| HashedStorage::new(false));
let storage_root = if state_storage.is_empty() {
// Preserve the existing storage root when witness lacks the storage trie.
self.state_trie
.get_rlp::<TrieAccount>(hashed_address.as_slice())
.ok()
.flatten()
.map(|existing| existing.storage_root)
.unwrap_or_else(|| {
self.storage_tries.entry(*hashed_address).or_default().hash()
})
} else {
let storage_trie = self.storage_tries.entry(*hashed_address).or_default();
if state_storage.wiped {
storage_trie.clear();
}
for (key, value) in state_storage.storage.iter() {
let key = key.as_slice();
if value.is_zero() {
storage_trie.delete(key).unwrap();
} else {
storage_trie.insert_rlp(key, *value).unwrap();
}
}
storage_trie.hash()
};
let state_account = TrieAccount {
nonce: account.nonce,
balance: account.balance,
storage_root,
code_hash: account.get_bytecode_hash(),
};
self.state_trie.insert_rlp(hashed_address.as_slice(), state_account).unwrap();
}
None => {
self.state_trie.delete(hashed_address.as_slice()).unwrap();
}
}
}
}
/// Computes the state root.
pub fn state_root(&self) -> B256 {
self.state_trie.hash()
}
}
impl core::fmt::Debug for EthereumState {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let mut ds = f.debug_struct("EthereumState");
ds.field("state_trie", &self.state_trie);
// Use BTreeMap for stable ordering when printing
let ordered: std::collections::BTreeMap<_, _> = self.storage_tries.iter().collect();
ds.field("storage_tries", &ordered);
ds.finish()
}
}
#[derive(Debug, thiserror::Error)]
pub enum FromProofError {
#[error("Node {} is not found by hash", .0)]
NodeNotFoundByHash(usize),
#[error("Node {} refrences invalid successor", .0)]
NodeHasInvalidSuccessor(usize),
#[error("Node {} cannot have children and is invalid", .0)]
NodeCannotHaveChildren(usize),
#[error("Found mismatched storage root after reconstruction \n account {}, found {}, expected {}", .0, .1, .2)]
MismatchedStorageRoot(Address, B256, B256),
#[error("Found mismatched state root after reconstruction \n found {}, expected {}", .0, .1)]
MismatchedStateRoot(B256, B256),
// todo: Should decode return a decoder error?
#[error("Error decoding proofs from bytes, {}", .0)]
DecodingError(#[from] Error),
}