Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,859 changes: 1,059 additions & 800 deletions Cargo.lock

Large diffs are not rendered by default.

213 changes: 109 additions & 104 deletions Cargo.toml

Large diffs are not rendered by default.

20 changes: 12 additions & 8 deletions crates/eth-sparse-mpt/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,29 @@ pub fn rlp_pointer(rlp_encode: Bytes) -> Bytes {
}

pub fn concat_path(p1: &Nibbles, p2: &[u8]) -> Nibbles {
let mut result = Nibbles::with_capacity(p1.len() + p2.len());
result.extend_from_slice_unchecked(p1);
result.extend_from_slice_unchecked(p2);
// let mut result = Nibbles::with_capacity(p1.len() + p2.len());
// result.extend_from_slice_unchecked(p1);
// result.extend_from_slice_unchecked(p2);
let p2_nibble = Nibbles::unpack(p2);
let result = p1.join(&p2_nibble);
result
}

pub fn strip_first_nibble_mut(p: &mut Nibbles) -> u8 {
let nibble = p[0];
let vec = p.as_mut_vec_unchecked();
let nibble = p.get_byte_unchecked(0);
let mut vec = p.pack().to_vec();
vec.remove(0);
p.clear();
p.extend_from_slice_unchecked(vec.as_slice());
nibble
}

#[inline]
pub fn extract_prefix_and_suffix(p1: &Nibbles, p2: &Nibbles) -> (Nibbles, Nibbles, Nibbles) {
let prefix_len = p1.common_prefix_length(p2);
let prefix = Nibbles::from_nibbles_unchecked(&p1[..prefix_len]);
let suffix1 = Nibbles::from_nibbles_unchecked(&p1[prefix_len..]);
let suffix2 = Nibbles::from_nibbles_unchecked(&p2[prefix_len..]);
let prefix = Nibbles::from_nibbles_unchecked(&p1.to_vec()[..prefix_len]);
let suffix1 = Nibbles::from_nibbles_unchecked(&p1.to_vec()[prefix_len..]);
let suffix2 = Nibbles::from_nibbles_unchecked(&p2.to_vec()[prefix_len..]);

(prefix, suffix1, suffix2)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::time::Instant;

use crate::utils::{hash_map_with_capacity, HashMap, HashSet};
use alloy_primitives::map::HashSet as AlloyHashSet;
use tracing::trace;
use tracing::{info, trace};

use alloy_primitives::{Bytes, B256};
use alloy_trie::Nibbles;
Expand Down Expand Up @@ -108,7 +108,11 @@ where
}

fn pad_path(mut path: Nibbles) -> B256 {
path.as_mut_vec_unchecked().resize(64, 0);
// path.to_vec().resize(64, 0);
let mut path_vec = path.pack();
path_vec.to_vec().resize(64, 0);
path.clear();
path.extend_from_slice(path_vec.as_slice());
let mut res = B256::default();
path.pack_to(res.as_mut_slice());
res
Expand Down
58 changes: 45 additions & 13 deletions crates/eth-sparse-mpt/src/v1/sparse_mpt/diff_trie/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub struct NodeCursor {

impl NodeCursor {
pub fn new(key: Nibbles, head: u64) -> Self {
let current_path = Nibbles::with_capacity(key.len());
let current_path = Nibbles::new();
Self {
current_node: head,
current_path,
Expand All @@ -75,9 +75,13 @@ impl NodeCursor {

pub fn step_into_extension(&mut self, ext: &DiffExtensionNode) {
let len = ext.key().len();
let mut path_left_vec = self.path_left.pack().to_vec();

self.current_path
.extend_from_slice_unchecked(&self.path_left[..len]);
self.path_left.as_mut_vec_unchecked().drain(..len);
.extend_from_slice_unchecked(&path_left_vec[..len]);
path_left_vec.drain(..len);
self.path_left.clear();
self.path_left.extend_from_slice(path_left_vec.as_slice());
self.current_node = ext.child.ptr();
}

Expand Down Expand Up @@ -333,9 +337,13 @@ impl DiffTrie {
.expect("other child must exist");
if branch.get_diff_child(other_child_nibble).is_none() {
let mut other_child_path = c.current_path.clone();
if let Some(l) = other_child_path.as_mut_vec_unchecked().last_mut() {
let mut other_child_path_vec = other_child_path.pack().to_vec();
if let Some(l) = other_child_path_vec.last_mut() {
*l = other_child_nibble;
}
other_child_path.clear();
other_child_path
.extend_from_slice_unchecked(other_child_path_vec.as_slice());
return Err(DeletionError::NodeNotFound(ErrSparseNodeNotFound {
path: other_child_path,
ptr: u64::MAX,
Expand Down Expand Up @@ -430,7 +438,8 @@ impl DiffTrie {
// we just replace extension node by merging its path into leaf with child_nibble
let mut new_leaf_key = ext_above.key().clone();
new_leaf_key.push(*child_nibble);
new_leaf_key.extend_from_slice_unchecked(leaf_below.key());
new_leaf_key
.extend_from_slice_unchecked(leaf_below.key().pack().as_slice());

let mut new_leaf = leaf_below;
new_leaf.changed_key = Some(new_leaf_key);
Expand All @@ -443,7 +452,8 @@ impl DiffTrie {
// we merge two extension nodes into current node with child_nibble
let ext_key = ext_above.key_mut();
ext_key.push(*child_nibble);
ext_key.extend_from_slice_unchecked(ext_below.key());
ext_key
.extend_from_slice_unchecked(ext_below.key().pack().as_slice());

ext_above.child = ext_below.child.clone();
}
Expand All @@ -469,10 +479,16 @@ impl DiffTrie {
DiffTrieNodeKind::Leaf(mut leaf_below),
) => {
// merge missing nibble into the leaf
// leaf_below
// .key_mut()
// .as_mut_vec_unchecked()
// .insert(0, *child_nibble);
let mut leaf_below_vec = leaf_below.key_mut().pack().to_vec();
leaf_below_vec.insert(0, *child_nibble);
leaf_below.key_mut().clear();
leaf_below
.key_mut()
.as_mut_vec_unchecked()
.insert(0, *child_nibble);
.extend_from_slice_unchecked(leaf_below_vec.as_slice());

let new_leaf_ptr = get_new_ptr(&mut self.ptrs);
let new_child = DiffTrieNode {
Expand All @@ -491,10 +507,16 @@ impl DiffTrie {
DiffTrieNodeKind::Extension(mut ext_below),
) => {
// merge missing nibble into the extension
// ext_below
// .key_mut()
// .as_mut_vec_unchecked()
// .insert(0, *child_nibble);
let mut ext_below_vec = ext_below.key_mut().pack().to_vec();
ext_below_vec.insert(0, *child_nibble);
ext_below.key_mut().clear();
ext_below
.key_mut()
.as_mut_vec_unchecked()
.insert(0, *child_nibble);
.extend_from_slice_unchecked(ext_below_vec.as_slice());
let new_child_ptr = get_new_ptr(&mut self.ptrs);
let new_child = DiffTrieNode {
kind: DiffTrieNodeKind::Extension(ext_below),
Expand Down Expand Up @@ -563,13 +585,23 @@ impl DiffTrie {
.expect("orphaned child existence verif");
match &mut child_below.kind {
DiffTrieNodeKind::Leaf(leaf) => {
// leaf.key_mut()
// .as_mut_vec_unchecked()
// .insert(0, child_nibble);
let mut leaf_vec = leaf.key_mut().pack().to_vec();
leaf_vec.insert(0, child_nibble);
leaf.key_mut().clear();
leaf.key_mut()
.as_mut_vec_unchecked()
.insert(0, child_nibble);
.extend_from_slice_unchecked(leaf_vec.as_slice());
child_below.rlp_pointer = None;
}
DiffTrieNodeKind::Extension(ext) => {
ext.key_mut().as_mut_vec_unchecked().insert(0, child_nibble);
// ext.key_mut().as_mut_vec_unchecked().insert(0, child_nibble);
let mut ext_vec = ext.key_mut().pack().to_vec();
ext_vec.insert(0, child_nibble);
ext.key_mut().clear();
ext.key_mut()
.extend_from_slice_unchecked(ext_vec.as_slice());
child_below.rlp_pointer = None;
}
DiffTrieNodeKind::Branch(_) => {
Expand Down
20 changes: 14 additions & 6 deletions crates/eth-sparse-mpt/src/v1/sparse_mpt/fixed_trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,12 @@ impl FixedTrie {
parent_child_idx = None;

let len = node.key.len();
current_path.extend_from_slice_unchecked(&path_left[..len]);
path_left.as_mut_vec_unchecked().drain(..len);
current_path.extend_from_slice_unchecked(&path_left.to_vec()[..len]);
// path_left.as_mut_vec_unchecked().drain(..len);
let mut path_left_vec = path_left.to_vec();
path_left_vec.drain(..len);
path_left.clear();
path_left.extend_from_slice_unchecked(path_left_vec.as_slice());

if path_left.is_empty() {
break;
Expand Down Expand Up @@ -456,10 +460,14 @@ impl FixedTrie {
// we stepped into child above so the path is the path of current child and orphan child differs
// only in last nibble
let mut path = c.current_path.clone();
path.as_mut_vec_unchecked()
.last_mut()
.map(|n| *n = orphan_nibble)
.unwrap();
// path.as_mut_vec_unchecked()
// .last_mut()
// .map(|n| *n = orphan_nibble)
// .unwrap();
let mut path_vec = path.to_vec();
path_vec.last_mut().map(|n| *n = orphan_nibble).unwrap();
path.clear();
path.extend_from_slice_unchecked(path_vec.as_slice());
missing_nodes.push(path);
}
}
Expand Down
6 changes: 5 additions & 1 deletion crates/eth-sparse-mpt/src/v2/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ impl MissingNodesFetcher {
}

fn pad_path(mut path: Nibbles) -> B256 {
path.as_mut_vec_unchecked().resize(64, 0);
// path.to_vec().resize(64, 0);
let mut path_vec = path.pack().to_vec();
path_vec.resize(64, 0);
path.clear();
path.extend_from_slice_unchecked(path_vec.as_slice());
let mut res = B256::default();
path.pack_to(res.as_mut_slice());
res
Expand Down
Loading