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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
use std::ops::Deref;

use crate::table::MPTProofType;

use serde::{Deserialize, Serialize};

use super::RlpItemType;

#[derive(Debug, Eq, PartialEq)]
pub(crate) enum StorageRowType {
    KeyS,
    ValueS,
    KeyC,
    ValueC,
    Drifted,
    Wrong,
    LongExtNodeKey,
    LongExtNodeNibbles,
    LongExtNodeValue,
    ShortExtNodeKey,
    ShortExtNodeNibbles,
    ShortExtNodeValue,
    Address,
    Key,
    Count,
}

#[derive(Debug, Eq, PartialEq)]
pub(crate) enum AccountRowType {
    KeyS,
    KeyC,
    NonceS,
    BalanceS,
    StorageS,
    CodehashS,
    NonceC,
    BalanceC,
    StorageC,
    CodehashC,
    Drifted,
    Wrong,
    LongExtNodeKey,      // only used when extension node nibbles are modified
    LongExtNodeNibbles,  // only used when extension node nibbles are modified
    LongExtNodeValue,    // only used when extension node nibbles are modified
    ShortExtNodeKey,     // only used when extension node nibbles are modified
    ShortExtNodeNibbles, // only used when extension node nibbles are modified
    ShortExtNodeValue,   // only used when extension node nibbles are modified
    Address,             // account address
    Key,                 // hashed account address
    Count,
}

#[derive(Debug, Eq, PartialEq)]
pub(crate) enum ExtensionBranchRowType {
    Mod,
    Child0,
    Child1,
    Child2,
    Child3,
    Child4,
    Child5,
    Child6,
    Child7,
    Child8,
    Child9,
    Child10,
    Child11,
    Child12,
    Child13,
    Child14,
    Child15,
    KeyS,
    ValueS,
    Nibbles,
    ValueC,
    Count,
}

#[derive(Debug, Eq, PartialEq)]
pub(crate) enum StartRowType {
    RootS,
    RootC,
    Count,
}

/// Serde for hex
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(transparent)]
pub struct Hex {
    #[serde(with = "hex::serde")]
    bytes: Vec<u8>,
}

impl From<Vec<u8>> for Hex {
    fn from(bytes: Vec<u8>) -> Self {
        Self { bytes }
    }
}

impl Deref for Hex {
    type Target = Vec<u8>;

    fn deref(&self) -> &Self::Target {
        &self.bytes
    }
}

/// MPT branch node
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct BranchNode {
    /// TODO Doc.
    pub modified_index: usize,
    /// TODO Doc.
    pub drifted_index: usize,
    /// TODO Doc.
    pub list_rlp_bytes: [Hex; 2],
}

/// MPT extension node
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ExtensionNode {
    /// TODO Doc.
    pub list_rlp_bytes: Hex,
}

/// MPT start node
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StartNode {
    /// TODO Doc.
    pub disable_preimage_check: bool,
    /// TODO Doc.
    pub proof_type: MPTProofType,
}

/// MPT extension branch node
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ExtensionBranchNode {
    /// TODO Doc.
    pub is_extension: bool,
    /// TODO Doc.
    pub(crate) is_mod_extension: [bool; 2],
    /// TODO Doc.
    pub is_placeholder: [bool; 2],
    /// TODO Doc.
    pub extension: ExtensionNode,
    /// TODO Doc.
    pub branch: BranchNode,
}

/// MPT account node
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AccountNode {
    /// Account address.
    pub address: Hex,
    /// Hashed account address.
    pub key: Hex,
    /// RLP bytes denoting the length of the whole account leaf stream.
    pub list_rlp_bytes: [Hex; 2],
    /// RLP bytes denoting the length of the RLP list denoting the value stream (containing nonce,
    /// balance storage, codehash).
    pub value_rlp_bytes: [Hex; 2],
    /// RLP bytes denoting the length of the RLP of the value stream.
    pub value_list_rlp_bytes: [Hex; 2],
    /// RLP bytes denoting the length of the RLP stream of the drifted leaf (neighbour leaf).
    /// This is only needed in the case when a new branch is created which replaces the existing
    /// leaf in the trie and this leaf drifts down into newly created branch.
    pub drifted_rlp_bytes: Hex,
    /// RLP bytes denoting the length of the RLP stream of the (wrong) leaf that has been returned
    /// by `getProof` which has the same address up to a certain nibble as the required leaf.
    /// This is only needed for some special cases of the AccountDoesNotExist proof.
    pub wrong_rlp_bytes: Hex,
    /// Denotes whether the extension node nibbles have been modified in either `S` or `C` proof.
    /// In these special cases, an additional extension node is inserted (deleted).
    pub(crate) is_mod_extension: [bool; 2],
    /// RLP bytes denoting the length of the RLP of the long and short modified extension node.
    pub(crate) mod_list_rlp_bytes: [Hex; 2],
}

/// MPT storage node
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StorageNode {
    /// Storage key.
    pub address: Hex,
    /// Hashed storage key.
    pub key: Hex,
    /// RLP bytes denoting the length of the whole storage leaf stream.
    pub list_rlp_bytes: [Hex; 2],
    /// RLP bytes denoting the length of the value stream.
    pub value_rlp_bytes: [Hex; 2],
    /// RLP bytes denoting the length of the RLP stream of the drifted leaf (neighbour leaf).
    /// This is only needed in the case when a new branch is created which replaces the existing
    /// leaf in the trie and this leaf drifts down into newly created branch.
    pub drifted_rlp_bytes: Hex,
    /// RLP bytes denoting the length of the RLP stream of the (wrong) leaf that has been returned
    /// by `getProof` which has the same address up to a certain nibble as the required leaf.
    /// This is only needed for some special cases of the StorageDoesNotExist proof.
    pub wrong_rlp_bytes: Hex,
    /// Denotes whether the extension node nibbles have been modified in either `S` or `C` proof.
    /// In these special cases, an additional extension node is inserted (deleted).
    pub(crate) is_mod_extension: [bool; 2],
    /// RLP bytes denoting the length of the RLP of the long and short modified extension node.
    pub(crate) mod_list_rlp_bytes: [Hex; 2],
}

/// MPT node
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct Node {
    /// A node denoting the start / end of the proof.
    pub start: Option<StartNode>,
    /// A node as an abstraction of extension node and branch.
    pub extension_branch: Option<ExtensionBranchNode>,
    /// An account leaf node.
    pub account: Option<AccountNode>,
    /// A storage leaf node.
    pub storage: Option<StorageNode>,
    /// RLP substreams of the node (for example for account leaf it contains substreams for key,
    /// nonce, balance, storage, codehash, drifted key, wrong key...)
    pub values: Vec<Hex>,
    /// Streams to be hashed and verified by Keccak circuit.
    pub keccak_data: Vec<Hex>,
}

/// RLP types start
pub const NODE_RLP_TYPES_START: [RlpItemType; StartRowType::Count as usize] =
    [RlpItemType::Hash, RlpItemType::Hash];

/// RLP types branch
pub const NODE_RLP_TYPES_BRANCH: [RlpItemType; ExtensionBranchRowType::Count as usize] = [
    RlpItemType::Node,
    RlpItemType::Node,
    RlpItemType::Node,
    RlpItemType::Node,
    RlpItemType::Node,
    RlpItemType::Node,
    RlpItemType::Node,
    RlpItemType::Node,
    RlpItemType::Node,
    RlpItemType::Node,
    RlpItemType::Node,
    RlpItemType::Node,
    RlpItemType::Node,
    RlpItemType::Node,
    RlpItemType::Node,
    RlpItemType::Node,
    RlpItemType::Node,
    RlpItemType::Key,
    RlpItemType::Node,
    RlpItemType::Nibbles,
    RlpItemType::Node,
];

/// RLP types account
pub const NODE_RLP_TYPES_ACCOUNT: [RlpItemType; AccountRowType::Count as usize] = [
    RlpItemType::Key,
    RlpItemType::Key,
    RlpItemType::Value,
    RlpItemType::Value,
    RlpItemType::Hash,
    RlpItemType::Hash,
    RlpItemType::Value,
    RlpItemType::Value,
    RlpItemType::Hash,
    RlpItemType::Hash,
    RlpItemType::Key,
    RlpItemType::Key,
    RlpItemType::Key,
    RlpItemType::Nibbles,
    RlpItemType::Value,
    RlpItemType::Key,
    RlpItemType::Nibbles,
    RlpItemType::Value,
    RlpItemType::Address,
    RlpItemType::Hash,
];

/// RLP types storage
pub const NODE_RLP_TYPES_STORAGE: [RlpItemType; StorageRowType::Count as usize] = [
    RlpItemType::Key,
    RlpItemType::Value,
    RlpItemType::Key,
    RlpItemType::Value,
    RlpItemType::Key,
    RlpItemType::Key,
    RlpItemType::Key,
    RlpItemType::Nibbles,
    RlpItemType::Value,
    RlpItemType::Key,
    RlpItemType::Nibbles,
    RlpItemType::Value,
    RlpItemType::Hash,
    RlpItemType::Hash,
];