forked from taikoxyz/raiko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpt.rs
1409 lines (1285 loc) · 52.7 KB
/
mpt.rs
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2023 RISC Zero, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use core::{
cell::RefCell,
cmp,
fmt::{Debug, Write},
iter, mem,
};
use std::collections::BTreeMap;
use alloy_primitives::{b256, TxNumber, B256, U256};
use alloy_rlp::Encodable;
use alloy_rlp_derive::{RlpDecodable, RlpEncodable, RlpMaxEncodedLen};
use alloy_rpc_types::EIP1186AccountProofResponse;
use anyhow::{Context, Result};
use revm_primitives::{Address, HashMap};
use rlp::{Decodable, DecoderError, Prototype, Rlp};
use serde::{Deserialize, Serialize};
use thiserror::Error as ThisError;
pub type StorageEntry = (MptNode, Vec<U256>);
/// Represents an Ethereum account within the state trie.
///
/// The `StateAccount` struct encapsulates key details of an Ethereum account, including
/// its nonce, balance, storage root, and the hash of its associated bytecode. This
/// representation is used when interacting with or querying the Ethereum state trie.
#[derive(
Debug,
Clone,
PartialEq,
Eq,
Serialize,
Deserialize,
RlpEncodable,
RlpDecodable,
RlpMaxEncodedLen,
)]
pub struct StateAccount {
/// The number of transactions sent from this account's address.
pub nonce: TxNumber,
/// The current balance of the account in Wei.
pub balance: U256,
/// The root of the account's storage trie, representing all stored contract data.
pub storage_root: B256,
/// The Keccak-256 hash of the account's associated bytecode (if it's a contract).
pub code_hash: B256,
}
impl Default for StateAccount {
/// Provides default values for a [StateAccount].
///
/// The default account has a nonce of 0, a balance of 0 Wei, an empty storage root,
/// and an empty bytecode hash.
fn default() -> Self {
Self {
nonce: 0,
balance: U256::ZERO,
storage_root: EMPTY_ROOT,
code_hash: KECCAK_EMPTY,
}
}
}
pub trait RlpBytes {
/// Returns the RLP-encoding.
fn to_rlp(&self) -> Vec<u8>;
}
impl<T> RlpBytes for T
where
T: alloy_rlp::Encodable,
{
#[inline]
fn to_rlp(&self) -> Vec<u8> {
let rlp_length = self.length();
let mut out = Vec::with_capacity(rlp_length);
self.encode(&mut out);
debug_assert_eq!(out.len(), rlp_length);
out
}
}
/// Root hash of an empty trie.
pub const EMPTY_ROOT: B256 =
b256!("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421");
extern crate alloc;
/// Represents the Keccak-256 hash of an empty byte slice.
///
/// This is a constant value and can be used as a default or placeholder
/// in various cryptographic operations.
pub const KECCAK_EMPTY: B256 =
b256!("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470");
/// Computes the Keccak-256 hash of the provided data.
///
/// This function is a thin wrapper around the Keccak256 hashing algorithm
/// and is optimized for performance.
///
/// # TODO
/// - Consider switching the return type to `B256` for consistency with other parts of the
/// codebase.
#[inline]
pub fn keccak(data: impl AsRef<[u8]>) -> [u8; 32] {
// TODO: Remove this benchmarking code once performance testing is complete.
// std::hint::black_box(sha2::Sha256::digest(&data));
*alloy_primitives::utils::keccak256(data)
}
/// Represents the root node of a sparse Merkle Patricia Trie.
///
/// The "sparse" nature of this trie allows for truncation of certain unneeded parts,
/// representing them by their node hash. This design choice is particularly useful for
/// optimizing storage. However, operations targeting a truncated part will fail and
/// return an error. Another distinction of this implementation is that branches cannot
/// store values, aligning with the construction of MPTs in Ethereum.
#[derive(Clone, Debug, Default, PartialEq, Eq, Ord, PartialOrd, Serialize, Deserialize)]
pub struct MptNode {
/// The type and data of the node.
data: MptNodeData,
/// Cache for a previously computed reference of this node. This is skipped during
/// serialization.
#[serde(skip)]
cached_reference: RefCell<Option<MptNodeReference>>,
}
/// Represents custom error types for the sparse Merkle Patricia Trie (MPT).
///
/// These errors cover various scenarios that can occur during trie operations, such as
/// encountering unresolved nodes, finding values in branches where they shouldn't be, and
/// issues related to RLP (Recursive Length Prefix) encoding and decoding.
#[derive(Debug, ThisError)]
pub enum Error {
/// Triggered when an operation reaches an unresolved node. The associated `B256`
/// value provides details about the unresolved node.
#[error("reached an unresolved node: {0:#}")]
NodeNotResolved(B256),
/// Occurs when a value is unexpectedly found in a branch node.
#[error("branch node with value")]
ValueInBranch,
/// Represents errors related to the RLP encoding and decoding using the `alloy_rlp`
/// library.
#[error("RLP error")]
Rlp(#[from] alloy_rlp::Error),
/// Represents errors related to the RLP encoding and decoding, specifically legacy
/// errors.
#[error("RLP error")]
LegacyRlp(#[from] DecoderError),
}
/// Represents the various types of data that can be stored within a node in the sparse
/// Merkle Patricia Trie (MPT).
///
/// Each node in the trie can be of one of several types, each with its own specific data
/// structure. This enum provides a clear and type-safe way to represent the data
/// associated with each node type.
#[derive(Clone, Debug, Default, PartialEq, Eq, Ord, PartialOrd, Serialize, Deserialize)]
pub enum MptNodeData {
/// Represents an empty trie node.
#[default]
Null,
/// A node that can have up to 16 children. Each child is an optional boxed [MptNode].
Branch([Option<Box<MptNode>>; 16]),
/// A leaf node that contains a key and a value, both represented as byte vectors.
Leaf(Vec<u8>, Vec<u8>),
/// A node that has exactly one child and is used to represent a shared prefix of
/// several keys.
Extension(Vec<u8>, Box<MptNode>),
/// Represents a sub-trie by its hash, allowing for efficient storage of large
/// sub-tries without storing their entire content.
Digest(B256),
}
/// Represents the ways in which one node can reference another node inside the sparse
/// Merkle Patricia Trie (MPT).
///
/// Nodes in the MPT can reference other nodes either directly through their byte
/// representation or indirectly through a hash of their encoding. This enum provides a
/// clear and type-safe way to represent these references.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Ord, PartialOrd, Serialize, Deserialize)]
pub enum MptNodeReference {
/// Represents a direct reference to another node using its byte encoding. Typically
/// used for short encodings that are less than 32 bytes in length.
Bytes(Vec<u8>),
/// Represents an indirect reference to another node using the Keccak hash of its long
/// encoding. Used for encodings that are not less than 32 bytes in length.
Digest(B256),
}
/// Provides a conversion from [MptNodeData] to [MptNode].
///
/// This implementation allows for conversion from [MptNodeData] to [MptNode],
/// initializing the `data` field with the provided value and setting the
/// `cached_reference` field to `None`.
impl From<MptNodeData> for MptNode {
fn from(value: MptNodeData) -> Self {
Self {
data: value,
cached_reference: RefCell::new(None),
}
}
}
/// Provides encoding functionalities for the `MptNode` type.
///
/// This implementation allows for the serialization of an [MptNode] into its RLP-encoded
/// form. The encoding is done based on the type of node data ([MptNodeData]) it holds.
impl Encodable for MptNode {
/// Encodes the node into the provided `out` buffer.
///
/// The encoding is done using the Recursive Length Prefix (RLP) encoding scheme. The
/// method handles different node data types and encodes them accordingly.
#[inline]
fn encode(&self, out: &mut dyn alloy_rlp::BufMut) {
match &self.data {
MptNodeData::Null => {
out.put_u8(alloy_rlp::EMPTY_STRING_CODE);
}
MptNodeData::Branch(nodes) => {
alloy_rlp::Header {
list: true,
payload_length: self.payload_length(),
}
.encode(out);
for child in nodes {
match child {
Some(node) => node.reference_encode(out),
None => out.put_u8(alloy_rlp::EMPTY_STRING_CODE),
}
}
// in the MPT reference, branches have values so always add empty value
out.put_u8(alloy_rlp::EMPTY_STRING_CODE);
}
MptNodeData::Leaf(prefix, value) => {
alloy_rlp::Header {
list: true,
payload_length: self.payload_length(),
}
.encode(out);
prefix.as_slice().encode(out);
value.as_slice().encode(out);
}
MptNodeData::Extension(prefix, node) => {
alloy_rlp::Header {
list: true,
payload_length: self.payload_length(),
}
.encode(out);
prefix.as_slice().encode(out);
node.reference_encode(out);
}
MptNodeData::Digest(digest) => {
digest.encode(out);
}
}
}
/// Returns the length of the encoded node in bytes.
///
/// This method calculates the length of the RLP-encoded node. It's useful for
/// determining the size requirements for storage or transmission.
#[inline]
fn length(&self) -> usize {
let payload_length = self.payload_length();
payload_length + alloy_rlp::length_of_length(payload_length)
}
}
/// Provides decoding functionalities for the [MptNode] type.
///
/// This implementation allows for the deserialization of an RLP-encoded [MptNode] back
/// into its original form. The decoding is done based on the prototype of the RLP data,
/// ensuring that the node is reconstructed accurately.
///
/// **Note**: This implementation is still using the older RLP library and needs to be
/// migrated to `alloy_rlp` in the future.
// TODO: migrate to alloy_rlp
impl Decodable for MptNode {
/// Decodes an RLP-encoded node from the provided `rlp` buffer.
///
/// The method handles different RLP prototypes and reconstructs the `MptNode` based
/// on the encoded data. If the RLP data does not match any known prototype or if
/// there's an error during decoding, an error is returned.
fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
match rlp.prototype()? {
Prototype::Null | Prototype::Data(0) => Ok(MptNodeData::Null.into()),
Prototype::List(2) => {
let path: Vec<u8> = rlp.val_at(0)?;
let prefix = path[0];
if (prefix & (2 << 4)) == 0 {
let node: MptNode = Decodable::decode(&rlp.at(1)?)?;
Ok(MptNodeData::Extension(path, Box::new(node)).into())
} else {
Ok(MptNodeData::Leaf(path, rlp.val_at(1)?).into())
}
}
Prototype::List(17) => {
let mut node_list = Vec::with_capacity(16);
for node_rlp in rlp.iter().take(16) {
match node_rlp.prototype()? {
Prototype::Null | Prototype::Data(0) => {
node_list.push(None);
}
_ => node_list.push(Some(Box::new(Decodable::decode(&node_rlp)?))),
}
}
let value: Vec<u8> = rlp.val_at(16)?;
if value.is_empty() {
Ok(MptNodeData::Branch(node_list.try_into().unwrap()).into())
} else {
Err(DecoderError::Custom("branch node with value"))
}
}
Prototype::Data(32) => {
let bytes: Vec<u8> = rlp.as_val()?;
Ok(MptNodeData::Digest(B256::from_slice(&bytes)).into())
}
_ => Err(DecoderError::RlpIncorrectListLen),
}
}
}
/// Represents a node in the sparse Merkle Patricia Trie (MPT).
///
/// The [MptNode] type encapsulates the data and functionalities associated with a node in
/// the MPT. It provides methods for manipulating the trie, such as inserting, deleting,
/// and retrieving values, as well as utility methods for encoding, decoding, and
/// debugging.
impl MptNode {
/// Clears the trie, replacing its data with an empty node, [MptNodeData::Null].
///
/// This method effectively removes all key-value pairs from the trie.
#[inline]
pub fn clear(&mut self) {
self.data = MptNodeData::Null;
self.invalidate_ref_cache();
}
/// Decodes an RLP-encoded [MptNode] from the provided byte slice.
///
/// This method allows for the deserialization of a previously serialized [MptNode].
#[inline]
pub fn decode(bytes: impl AsRef<[u8]>) -> Result<MptNode, Error> {
rlp::decode(bytes.as_ref()).map_err(Error::from)
}
/// Retrieves the underlying data of the node.
///
/// This method provides a reference to the node's data, allowing for inspection and
/// manipulation.
#[inline]
pub fn as_data(&self) -> &MptNodeData {
&self.data
}
/// Retrieves the [MptNodeReference] reference of the node when it's referenced inside
/// another node.
///
/// This method provides a way to obtain a compact representation of the node for
/// storage or transmission purposes.
#[inline]
pub fn reference(&self) -> MptNodeReference {
self.cached_reference
.borrow_mut()
.get_or_insert_with(|| self.calc_reference())
.clone()
}
/// Computes and returns the 256-bit hash of the node.
///
/// This method provides a unique identifier for the node based on its content.
#[inline]
pub fn hash(&self) -> B256 {
match self.data {
MptNodeData::Null => EMPTY_ROOT,
_ => match self.reference() {
MptNodeReference::Digest(digest) => digest,
MptNodeReference::Bytes(bytes) => keccak(bytes).into(),
},
}
}
/// Encodes the [MptNodeReference] of this node into the `out` buffer.
fn reference_encode(&self, out: &mut dyn alloy_rlp::BufMut) {
match self.reference() {
// if the reference is an RLP-encoded byte slice, copy it directly
MptNodeReference::Bytes(bytes) => out.put_slice(&bytes),
// if the reference is a digest, RLP-encode it with its fixed known length
MptNodeReference::Digest(digest) => {
out.put_u8(alloy_rlp::EMPTY_STRING_CODE + 32);
out.put_slice(digest.as_slice());
}
}
}
/// Returns the length of the encoded [MptNodeReference] of this node.
fn reference_length(&self) -> usize {
match self.reference() {
MptNodeReference::Bytes(bytes) => bytes.len(),
MptNodeReference::Digest(_) => 1 + 32,
}
}
fn calc_reference(&self) -> MptNodeReference {
match &self.data {
MptNodeData::Null => MptNodeReference::Bytes(vec![alloy_rlp::EMPTY_STRING_CODE]),
MptNodeData::Digest(digest) => MptNodeReference::Digest(*digest),
_ => {
let encoded = alloy_rlp::encode(self);
if encoded.len() < 32 {
MptNodeReference::Bytes(encoded)
} else {
MptNodeReference::Digest(keccak(encoded).into())
}
}
}
}
/// Determines if the trie is empty.
///
/// This method checks if the node represents an empty trie, i.e., it doesn't contain
/// any key-value pairs.
#[inline]
pub fn is_empty(&self) -> bool {
matches!(&self.data, MptNodeData::Null)
}
/// Determines if the node represents a digest.
///
/// A digest is a compact representation of a sub-trie, represented by its hash.
#[inline]
pub fn is_digest(&self) -> bool {
matches!(&self.data, MptNodeData::Digest(_))
}
/// Retrieves the nibbles corresponding to the node's prefix.
///
/// Nibbles are half-bytes, and in the context of the MPT, they represent parts of
/// keys.
#[inline]
pub fn nibs(&self) -> Vec<u8> {
match &self.data {
MptNodeData::Null | MptNodeData::Branch(_) | MptNodeData::Digest(_) => vec![],
MptNodeData::Leaf(prefix, _) | MptNodeData::Extension(prefix, _) => prefix_nibs(prefix),
}
}
/// Retrieves the value associated with a given key in the trie.
///
/// If the key is not present in the trie, this method returns `None`. Otherwise, it
/// returns a reference to the associated value. If [None] is returned, the key is
/// provably not in the trie.
#[inline]
pub fn get(&self, key: &[u8]) -> Result<Option<&[u8]>, Error> {
self.get_internal(&to_nibs(key))
}
/// Retrieves the RLP-decoded value corresponding to the key.
///
/// If the key is not present in the trie, this method returns `None`. Otherwise, it
/// returns the RLP-decoded value.
#[inline]
pub fn get_rlp<T: alloy_rlp::Decodable>(&self, key: &[u8]) -> Result<Option<T>, Error> {
match self.get(key)? {
Some(mut bytes) => Ok(Some(T::decode(&mut bytes)?)),
None => Ok(None),
}
}
fn get_internal(&self, key_nibs: &[u8]) -> Result<Option<&[u8]>, Error> {
match &self.data {
MptNodeData::Null => Ok(None),
MptNodeData::Branch(nodes) => {
if let Some((i, tail)) = key_nibs.split_first() {
match nodes[*i as usize] {
Some(ref node) => node.get_internal(tail),
None => Ok(None),
}
} else {
Ok(None)
}
}
MptNodeData::Leaf(prefix, value) => {
if prefix_nibs(prefix) == key_nibs {
Ok(Some(value))
} else {
Ok(None)
}
}
MptNodeData::Extension(prefix, node) => {
if let Some(tail) = key_nibs.strip_prefix(prefix_nibs(prefix).as_slice()) {
node.get_internal(tail)
} else {
Ok(None)
}
}
MptNodeData::Digest(digest) => Err(Error::NodeNotResolved(*digest)),
}
}
/// Removes a key from the trie.
///
/// This method attempts to remove a key-value pair from the trie. If the key is
/// present, it returns `true`. Otherwise, it returns `false`.
#[inline]
pub fn delete(&mut self, key: &[u8]) -> Result<bool, Error> {
self.delete_internal(&to_nibs(key))
}
fn delete_internal(&mut self, key_nibs: &[u8]) -> Result<bool, Error> {
match &mut self.data {
MptNodeData::Null => return Ok(false),
MptNodeData::Branch(children) => {
if let Some((i, tail)) = key_nibs.split_first() {
let child = &mut children[*i as usize];
match child {
Some(node) => {
if !node.delete_internal(tail)? {
return Ok(false);
}
// if the node is now empty, remove it
if node.is_empty() {
*child = None;
}
}
None => return Ok(false),
}
} else {
return Err(Error::ValueInBranch);
}
let mut remaining = children.iter_mut().enumerate().filter(|(_, n)| n.is_some());
// there will always be at least one remaining node
let (index, node) = remaining.next().unwrap();
// if there is only exactly one node left, we need to convert the branch
if remaining.next().is_none() {
let mut orphan = node.take().unwrap();
match &mut orphan.data {
// if the orphan is a leaf, prepend the corresponding nib to it
MptNodeData::Leaf(prefix, orphan_value) => {
let new_nibs: Vec<_> =
iter::once(index as u8).chain(prefix_nibs(prefix)).collect();
self.data = MptNodeData::Leaf(
to_encoded_path(&new_nibs, true),
mem::take(orphan_value),
);
}
// if the orphan is an extension, prepend the corresponding nib to it
MptNodeData::Extension(prefix, orphan_child) => {
let new_nibs: Vec<_> =
iter::once(index as u8).chain(prefix_nibs(prefix)).collect();
self.data = MptNodeData::Extension(
to_encoded_path(&new_nibs, false),
mem::take(orphan_child),
);
}
// if the orphan is a branch or digest, convert to an extension
MptNodeData::Branch(_) | MptNodeData::Digest(_) => {
self.data = MptNodeData::Extension(
to_encoded_path(&[index as u8], false),
orphan,
);
}
MptNodeData::Null => unreachable!(),
}
}
}
MptNodeData::Leaf(prefix, _) => {
if prefix_nibs(prefix) != key_nibs {
return Ok(false);
}
self.data = MptNodeData::Null;
}
MptNodeData::Extension(prefix, child) => {
let mut self_nibs = prefix_nibs(prefix);
if let Some(tail) = key_nibs.strip_prefix(self_nibs.as_slice()) {
if !child.delete_internal(tail)? {
return Ok(false);
}
} else {
return Ok(false);
}
// an extension can only point to a branch or a digest; since it's sub trie was
// modified, we need to make sure that this property still holds
match &mut child.data {
// if the child is empty, remove the extension
MptNodeData::Null => {
self.data = MptNodeData::Null;
}
// for a leaf, replace the extension with the extended leaf
MptNodeData::Leaf(prefix, value) => {
self_nibs.extend(prefix_nibs(prefix));
self.data =
MptNodeData::Leaf(to_encoded_path(&self_nibs, true), mem::take(value));
}
// for an extension, replace the extension with the extended extension
MptNodeData::Extension(prefix, node) => {
self_nibs.extend(prefix_nibs(prefix));
self.data = MptNodeData::Extension(
to_encoded_path(&self_nibs, false),
mem::take(node),
);
}
// for a branch or digest, the extension is still correct
MptNodeData::Branch(_) | MptNodeData::Digest(_) => {}
}
}
MptNodeData::Digest(digest) => return Err(Error::NodeNotResolved(*digest)),
};
self.invalidate_ref_cache();
Ok(true)
}
/// Inserts a key-value pair into the trie.
///
/// This method attempts to insert a new key-value pair into the trie. If the
/// insertion is successful, it returns `true`. If the key already exists, it updates
/// the value and returns `false`.
#[inline]
pub fn insert(&mut self, key: &[u8], value: Vec<u8>) -> Result<bool, Error> {
assert!(!value.is_empty(), "value must not be empty");
self.insert_internal(&to_nibs(key), value)
}
/// Inserts an RLP-encoded value into the trie.
///
/// This method inserts a value that's been encoded using RLP into the trie.
#[inline]
pub fn insert_rlp(&mut self, key: &[u8], value: impl Encodable) -> Result<bool, Error> {
self.insert_internal(&to_nibs(key), value.to_rlp())
}
#[inline]
pub fn insert_rlp_encoded(&mut self, key: &[u8], value: Vec<u8>) -> Result<bool, Error> {
self.insert_internal(&to_nibs(key), value)
}
fn insert_internal(&mut self, key_nibs: &[u8], value: Vec<u8>) -> Result<bool, Error> {
match &mut self.data {
MptNodeData::Null => {
self.data = MptNodeData::Leaf(to_encoded_path(key_nibs, true), value);
}
MptNodeData::Branch(children) => {
if let Some((i, tail)) = key_nibs.split_first() {
let child = &mut children[*i as usize];
match child {
Some(node) => {
if !node.insert_internal(tail, value)? {
return Ok(false);
}
}
// if the corresponding child is empty, insert a new leaf
None => {
*child = Some(Box::new(
MptNodeData::Leaf(to_encoded_path(tail, true), value).into(),
));
}
}
} else {
return Err(Error::ValueInBranch);
}
}
MptNodeData::Leaf(prefix, old_value) => {
let self_nibs = prefix_nibs(prefix);
let common_len = lcp(&self_nibs, key_nibs);
if common_len == self_nibs.len() && common_len == key_nibs.len() {
// if self_nibs == key_nibs, update the value if it is different
if old_value == &value {
return Ok(false);
}
*old_value = value;
} else if common_len == self_nibs.len() || common_len == key_nibs.len() {
return Err(Error::ValueInBranch);
} else {
let split_point = common_len + 1;
// otherwise, create a branch with two children
let mut children: [Option<Box<MptNode>>; 16] = Default::default();
children[self_nibs[common_len] as usize] = Some(Box::new(
MptNodeData::Leaf(
to_encoded_path(&self_nibs[split_point..], true),
mem::take(old_value),
)
.into(),
));
children[key_nibs[common_len] as usize] = Some(Box::new(
MptNodeData::Leaf(to_encoded_path(&key_nibs[split_point..], true), value)
.into(),
));
let branch = MptNodeData::Branch(children);
if common_len > 0 {
// create parent extension for new branch
self.data = MptNodeData::Extension(
to_encoded_path(&self_nibs[..common_len], false),
Box::new(branch.into()),
);
} else {
self.data = branch;
}
}
}
MptNodeData::Extension(prefix, existing_child) => {
let self_nibs = prefix_nibs(prefix);
let common_len = lcp(&self_nibs, key_nibs);
if common_len == self_nibs.len() {
// traverse down for update
if !existing_child.insert_internal(&key_nibs[common_len..], value)? {
return Ok(false);
}
} else if common_len == key_nibs.len() {
return Err(Error::ValueInBranch);
} else {
let split_point = common_len + 1;
// otherwise, create a branch with two children
let mut children: [Option<Box<MptNode>>; 16] = Default::default();
children[self_nibs[common_len] as usize] = if split_point < self_nibs.len() {
Some(Box::new(
MptNodeData::Extension(
to_encoded_path(&self_nibs[split_point..], false),
mem::take(existing_child),
)
.into(),
))
} else {
Some(mem::take(existing_child))
};
children[key_nibs[common_len] as usize] = Some(Box::new(
MptNodeData::Leaf(to_encoded_path(&key_nibs[split_point..], true), value)
.into(),
));
let branch = MptNodeData::Branch(children);
if common_len > 0 {
// Create parent extension for new branch
self.data = MptNodeData::Extension(
to_encoded_path(&self_nibs[..common_len], false),
Box::new(branch.into()),
);
} else {
self.data = branch;
}
}
}
MptNodeData::Digest(digest) => return Err(Error::NodeNotResolved(*digest)),
};
self.invalidate_ref_cache();
Ok(true)
}
fn invalidate_ref_cache(&mut self) {
self.cached_reference.borrow_mut().take();
}
/// Returns the number of traversable nodes in the trie.
///
/// This method provides a count of all the nodes that can be traversed within the
/// trie.
pub fn size(&self) -> usize {
match self.as_data() {
MptNodeData::Null | MptNodeData::Digest(_) => 0,
MptNodeData::Branch(children) => {
children.iter().flatten().map(|n| n.size()).sum::<usize>() + 1
}
MptNodeData::Leaf(_, _) => 1,
MptNodeData::Extension(_, child) => child.size() + 1,
}
}
/// Formats the trie as a string list, where each line corresponds to a trie leaf.
///
/// This method is primarily used for debugging purposes, providing a visual
/// representation of the trie's structure.
pub fn debug_rlp<T: alloy_rlp::Decodable + Debug>(&self) -> Vec<String> {
// convert the nibs to hex
let nibs: String = self.nibs().iter().fold(String::new(), |mut output, n| {
let _ = write!(output, "{n:x}");
output
});
match self.as_data() {
MptNodeData::Null => vec![format!("{:?}", MptNodeData::Null)],
MptNodeData::Branch(children) => children
.iter()
.enumerate()
.flat_map(|(i, child)| {
match child {
Some(node) => node.debug_rlp::<T>(),
None => vec!["None".to_string()],
}
.into_iter()
.map(move |s| format!("{i:x} {s}"))
})
.collect(),
MptNodeData::Leaf(_, data) => {
vec![format!(
"{nibs} -> {:?}",
T::decode(&mut &data[..]).unwrap()
)]
}
MptNodeData::Extension(_, node) => node
.debug_rlp::<T>()
.into_iter()
.map(|s| format!("{nibs} {s}"))
.collect(),
MptNodeData::Digest(digest) => vec![format!("#{digest:#}")],
}
}
/// Returns the length of the RLP payload of the node.
fn payload_length(&self) -> usize {
match &self.data {
MptNodeData::Null => 0,
MptNodeData::Branch(nodes) => {
1 + nodes
.iter()
.map(|child| child.as_ref().map_or(1, |node| node.reference_length()))
.sum::<usize>()
}
MptNodeData::Leaf(prefix, value) => {
prefix.as_slice().length() + value.as_slice().length()
}
MptNodeData::Extension(prefix, node) => {
prefix.as_slice().length() + node.reference_length()
}
MptNodeData::Digest(_) => 32,
}
}
}
/// Converts a byte slice into a vector of nibbles.
///
/// A nibble is 4 bits or half of an 8-bit byte. This function takes each byte from the
/// input slice, splits it into two nibbles, and appends them to the resulting vector.
pub fn to_nibs(slice: &[u8]) -> Vec<u8> {
let mut result = Vec::with_capacity(2 * slice.len());
for byte in slice {
result.push(byte >> 4);
result.push(byte & 0xf);
}
result
}
/// Encodes a slice of nibbles into a vector of bytes, with an additional prefix to
/// indicate the type of node (leaf or extension).
///
/// The function starts by determining the type of node based on the `is_leaf` parameter.
/// If the node is a leaf, the prefix is set to `0x20`. If the length of the nibbles is
/// odd, the prefix is adjusted and the first nibble is incorporated into it.
///
/// The remaining nibbles are then combined into bytes, with each pair of nibbles forming
/// a single byte. The resulting vector starts with the prefix, followed by the encoded
/// bytes.
pub fn to_encoded_path(mut nibs: &[u8], is_leaf: bool) -> Vec<u8> {
let mut prefix = u8::from(is_leaf) * 0x20;
if nibs.len() % 2 != 0 {
prefix += 0x10 + nibs[0];
nibs = &nibs[1..];
}
iter::once(prefix)
.chain(nibs.chunks_exact(2).map(|byte| (byte[0] << 4) + byte[1]))
.collect()
}
/// Returns the length of the common prefix.
fn lcp(a: &[u8], b: &[u8]) -> usize {
for (i, (a, b)) in iter::zip(a, b).enumerate() {
if a != b {
return i;
}
}
cmp::min(a.len(), b.len())
}
fn prefix_nibs(prefix: &[u8]) -> Vec<u8> {
let (extension, tail) = prefix.split_first().unwrap();
// the first bit of the first nibble denotes the parity
let is_odd = extension & (1 << 4) != 0;
let mut result = Vec::with_capacity(2 * tail.len() + usize::from(is_odd));
// for odd lengths, the second nibble contains the first element
if is_odd {
result.push(extension & 0xf);
}
for nib in tail {
result.push(nib >> 4);
result.push(nib & 0xf);
}
result
}
/// Parses proof bytes into a vector of MPT nodes.
pub fn parse_proof(proof: &[impl AsRef<[u8]>]) -> Result<Vec<MptNode>> {
Ok(proof
.iter()
.map(MptNode::decode)
.collect::<Result<Vec<_>, _>>()?)
}
/// Creates a Merkle Patricia trie from an EIP-1186 proof.
/// For inclusion proofs the returned trie contains exactly one leaf with the value.
pub fn mpt_from_proof(proof_nodes: &[MptNode]) -> Result<MptNode> {
let mut next: Option<MptNode> = None;
for (i, node) in proof_nodes.iter().enumerate().rev() {
// there is nothing to replace for the last node
let Some(replacement) = next else {
next = Some(node.clone());
continue;
};
// the next node must have a digest reference
let MptNodeReference::Digest(ref child_ref) = replacement.reference() else {
panic!("node {} in proof is not referenced by hash", i + 1);
};
// find the child that references the next node
let resolved: MptNode = match node.as_data().clone() {
MptNodeData::Branch(mut children) => {
if let Some(child) = children.iter_mut().flatten().find(
|child| matches!(child.as_data(), MptNodeData::Digest(d) if d == child_ref),
) {
*child = Box::new(replacement);
} else {
panic!("node {i} does not reference the successor");
}
MptNodeData::Branch(children).into()
}
MptNodeData::Extension(prefix, child) => {
assert!(
matches!(child.as_data(), MptNodeData::Digest(d) if d == child_ref),
"node {i} does not reference the successor"
);
MptNodeData::Extension(prefix, Box::new(replacement)).into()
}
MptNodeData::Null | MptNodeData::Leaf(_, _) | MptNodeData::Digest(_) => {
panic!("node {i} has no children to replace");
}
};
next = Some(resolved);
}
// the last node in the proof should be the root
Ok(next.unwrap_or_default())
}
/// Verifies that the given proof is a valid proof of exclusion for the given key.
pub fn is_not_included(key: &[u8], proof_nodes: &[MptNode]) -> Result<bool> {
let proof_trie = mpt_from_proof(proof_nodes).unwrap();
// for valid proofs, the get must not fail
let value = proof_trie.get(key).unwrap();
Ok(value.is_none())
}
/// Creates a new MPT trie where all the digests contained in `node_store` are resolved.
pub fn resolve_nodes(root: &MptNode, node_store: &HashMap<MptNodeReference, MptNode>) -> MptNode {
let trie = match root.as_data() {
MptNodeData::Null | MptNodeData::Leaf(_, _) => root.clone(),
MptNodeData::Branch(children) => {
let children: Vec<_> = children
.iter()
.map(|child| {
child
.as_ref()
.map(|node| Box::new(resolve_nodes(node, node_store)))
})
.collect();
MptNodeData::Branch(children.try_into().unwrap()).into()
}
MptNodeData::Extension(prefix, target) => {
MptNodeData::Extension(prefix.clone(), Box::new(resolve_nodes(target, node_store)))
.into()
}
MptNodeData::Digest(digest) => {
if let Some(node) = node_store.get(&MptNodeReference::Digest(*digest)) {
resolve_nodes(node, node_store)
} else {
root.clone()
}
}
};
// the root hash must not change