Skip to content
This repository was archived by the owner on Nov 18, 2024. It is now read-only.

Upgrade to v1.1.1 #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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,267 changes: 862 additions & 405 deletions Cargo.lock

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@ rayon = "1.10.0"
smallvec = "1.13.2"

# reth
reth-db-api = { git = "https://github.com/paradigmxyz/reth", tag = "v1.0.6" }
reth-errors = { git = "https://github.com/paradigmxyz/reth", tag = "v1.0.6" }
reth-execution-errors = { git = "https://github.com/paradigmxyz/reth", tag = "v1.0.6" }
reth-trie = { git = "https://github.com/paradigmxyz/reth", tag = "v1.0.6" }
reth-trie-db = { git = "https://github.com/paradigmxyz/reth", tag = "v1.0.6" }
reth-provider = { git = "https://github.com/paradigmxyz/reth", tag = "v1.0.6" }
reth-db-api = { git = "https://github.com/paradigmxyz/reth", tag = "v1.1.1" }
reth-errors = { git = "https://github.com/paradigmxyz/reth", tag = "v1.1.1" }
reth-execution-errors = { git = "https://github.com/paradigmxyz/reth", tag = "v1.1.1" }
reth-trie = { git = "https://github.com/paradigmxyz/reth", tag = "v1.1.1" }
reth-trie-db = { git = "https://github.com/paradigmxyz/reth", tag = "v1.1.1" }
reth-provider = { git = "https://github.com/paradigmxyz/reth", tag = "v1.1.1" }

# revm
revm = { version = "14.0.0", features = [
revm = { version = "17.0.0", features = [
"std",
"secp256k1",
"blst",
], default-features = false }
revm-primitives = { version = "9.0.0", features = [
revm-primitives = { version = "13.0.0", features = [
"std",
], default-features = false }

# alloy
alloy-primitives = { version = "0.8.0", default-features = false, features = ["asm-keccak"] }
alloy-primitives = { version = "0.8.9", default-features = false, features = ["asm-keccak"] }
alloy-rlp = "0.3.4"
alloy-trie = { version = "0.5", default-features = false }
alloy-trie = { version = "0.7", default-features = false }

# test only dependencies but included here to be accessible from benches/
hash-db = "0.15.2"
Expand Down
16 changes: 7 additions & 9 deletions src/reth_sparse_trie/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use alloy_primitives::{Address, B256};
use change_set::prepare_change_set;
use change_set::prepare_change_set_for_prefetch;
use hash::RootHashError;
use reth_db_api::database::Database;
use reth_provider::providers::ConsistentDbView;
use reth_provider::BlockReader;
use reth_provider::DatabaseProviderFactory;
use reth_provider::ExecutionOutcome;
use std::time::Duration;
Expand Down Expand Up @@ -65,14 +65,13 @@ impl ChangedAccountData {
}

/// Prefetches data
pub fn prefetch_tries_for_accounts<'a, DB, Provider>(
consistent_db_view: ConsistentDbView<DB, Provider>,
pub fn prefetch_tries_for_accounts<'a, Provider>(
consistent_db_view: ConsistentDbView<Provider>,
shared_cache: SparseTrieSharedCache,
changed_data: impl Iterator<Item = &'a ChangedAccountData>,
) -> Result<(), SparseTrieError>
where
DB: Database,
Provider: DatabaseProviderFactory<DB> + Send + Sync,
Provider: DatabaseProviderFactory<Provider: BlockReader> + Send + Sync,
{
let change_set = prepare_change_set_for_prefetch(changed_data);

Expand All @@ -95,14 +94,13 @@ where
/// Calculate root hash for the given outcome on top of the block defined by consistent_db_view.
/// * shared_cache should be created once for each parent block and it stores fethed parts of the trie
/// It uses rayon for parallelism and the thread pool should be configured from outside.
pub fn calculate_root_hash_with_sparse_trie<DB, Provider>(
consistent_db_view: ConsistentDbView<DB, Provider>,
pub fn calculate_root_hash_with_sparse_trie<Provider>(
consistent_db_view: ConsistentDbView<Provider>,
outcome: &ExecutionOutcome,
shared_cache: SparseTrieSharedCache,
) -> (Result<B256, SparseTrieError>, SparseTrieMetrics)
where
DB: Database,
Provider: DatabaseProviderFactory<DB> + Send + Sync,
Provider: DatabaseProviderFactory<Provider: BlockReader> + Send + Sync,
{
let mut metrics = SparseTrieMetrics::default();

Expand Down
35 changes: 17 additions & 18 deletions src/reth_sparse_trie/trie_fetcher/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use crate::utils::{hash_map_with_capacity, HashMap, HashSet};
use alloy_primitives::map::HashMap as AlloyHashMap;
use alloy_primitives::map::HashSet as AlloyHashSet;

use alloy_primitives::{Bytes, B256};
use alloy_trie::Nibbles;
use rayon::prelude::*;
use reth_db_api::database::Database;
use reth_errors::ProviderError;
use reth_execution_errors::trie::StateProofError;
use reth_provider::providers::ConsistentDbView;
use reth_provider::DatabaseProviderFactory;
use reth_provider::{BlockReader, DBProvider, DatabaseProviderFactory};
use reth_trie::proof::Proof;
use reth_trie::{MultiProof as RethMultiProof, EMPTY_ROOT_HASH};
use reth_trie_db::{DatabaseHashedCursorFactory, DatabaseTrieCursorFactory};
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, Seq};
use std::collections::HashMap as StdHashMap;

use super::shared_cache::MissingNodes;

Expand Down Expand Up @@ -51,16 +52,15 @@ pub struct StorageMultiProof {
}

#[derive(Debug)]
pub struct TrieFetcher<DB, Provider> {
consistent_db_view: ConsistentDbView<DB, Provider>,
pub struct TrieFetcher<Provider> {
consistent_db_view: ConsistentDbView<Provider>,
}

impl<DB, Provider> TrieFetcher<DB, Provider>
impl<Provider> TrieFetcher<Provider>
where
DB: Database,
Provider: DatabaseProviderFactory<DB> + Send + Sync,
Provider: DatabaseProviderFactory<Provider: BlockReader> + Send + Sync,
{
pub fn new(consistent_db_view: ConsistentDbView<DB, Provider>) -> Self {
pub fn new(consistent_db_view: ConsistentDbView<Provider>) -> Self {
Self { consistent_db_view }
}

Expand All @@ -74,13 +74,12 @@ where
.into_par_iter()
.map(|targets| -> Result<MultiProof, FetchNodeError> {
let provider = self.consistent_db_view.provider_ro()?;

let proof = Proof::new(
DatabaseTrieCursorFactory::new(provider.tx_ref()),
DatabaseHashedCursorFactory::new(provider.tx_ref()),
);

let reth_multiproof = proof.with_targets(targets).multiproof()?;
let reth_multiproof = proof.multiproof(targets)?;
let result = convert_reth_multiproof(reth_multiproof, &all_requested_accounts);
Ok(result)
})
Expand All @@ -103,7 +102,7 @@ fn pad_path(mut path: Nibbles) -> B256 {

fn get_proof_targets(
missing_nodes: MissingNodes,
) -> (Vec<StdHashMap<B256, Vec<B256>>>, HashSet<B256>) {
) -> (Vec<AlloyHashMap<B256, AlloyHashSet<B256>>>, HashSet<B256>) {
// we will split all missing nodes accounts into buckets of (missing accounts / account_per_fetch)
let account_per_fetch = 5;

Expand All @@ -115,22 +114,22 @@ fn get_proof_targets(
if is_address {
all_requested_accounts.insert(hashed_address);
}
targets.insert(hashed_address, Vec::new());
targets.insert(hashed_address, AlloyHashSet::default());
}
for (account, missing_storage_nodes) in missing_nodes.storage_trie_nodes {
let hashed_address = B256::from_slice(&account);
all_requested_accounts.insert(hashed_address);
let storage_targets = targets.entry(hashed_address).or_default();
for node in missing_storage_nodes {
let node = pad_path(node);
storage_targets.push(node);
storage_targets.insert(node);
}
}

let mut result = Vec::new();
let mut result = Vec::<AlloyHashMap<B256, AlloyHashSet<B256>>>::new();
let mut iter = targets.into_iter();
loop {
let mut split_target = StdHashMap::new();
let mut split_target = AlloyHashMap::<B256, AlloyHashSet<B256>>::default();
let mut count = 0;
while let Some((target_key, target_value)) = iter.next() {
split_target.insert(target_key, target_value);
Expand Down Expand Up @@ -182,7 +181,7 @@ fn convert_reth_multiproof(
all_requested_accounts: &HashSet<B256>,
) -> MultiProof {
let mut account_subtree = Vec::with_capacity(reth_proof.account_subtree.len());
for (k, v) in reth_proof.account_subtree {
for (k, v) in reth_proof.account_subtree.into_inner() {
account_subtree.push((k, v));
}
account_subtree.sort_by_key(|a| a.0.clone());
Expand All @@ -196,7 +195,7 @@ fn convert_reth_multiproof(
}
let mut subtree = Vec::with_capacity(reth_storage_proof.subtree.len());

for (k, v) in reth_storage_proof.subtree {
for (k, v) in reth_storage_proof.subtree.into_inner() {
subtree.push((k, v));
}
subtree.sort_by_key(|a| a.0.clone());
Expand Down
12 changes: 6 additions & 6 deletions src/sparse_mpt/diff_trie/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl DiffTrie {
loop {
let node = try_get_node_mut(&mut self.nodes, c.current_node, &c.current_path)?;
match &mut node.kind {
DiffTrieNodeKind::Null => {
DiffTrieNodeKind::EmptyRoot => {
let new_node = DiffTrieNode::new_leaf(c.path_left, value);
*node = new_node;
break;
Expand Down Expand Up @@ -269,7 +269,7 @@ impl DiffTrie {
.map_err(|e| DeletionError::NodeNotFound(e))?;

match &mut node.kind {
DiffTrieNodeKind::Null => {
DiffTrieNodeKind::EmptyRoot => {
return Err(DeletionError::KeyNotFound);
}
DiffTrieNodeKind::Leaf(leaf) => {
Expand Down Expand Up @@ -359,7 +359,7 @@ impl DiffTrie {
let node = try_get_node_mut(&mut self.nodes, current_node, &Nibbles::new())
.expect("nodes must exist when walking back");
let should_remove = match &mut node.kind {
DiffTrieNodeKind::Null => unreachable!(),
DiffTrieNodeKind::EmptyRoot => unreachable!(),
DiffTrieNodeKind::Leaf(_) => {
deletion_result = NodeDeletionResult::NodeDeleted;
true
Expand Down Expand Up @@ -578,7 +578,7 @@ impl DiffTrie {
});
child_below.rlp_pointer = None;
}
DiffTrieNodeKind::Null => unreachable!(),
DiffTrieNodeKind::EmptyRoot => unreachable!(),
};
let ptr = get_new_ptr(&mut self.ptrs);
self.head = ptr;
Expand Down Expand Up @@ -610,7 +610,7 @@ impl DiffTrie {
let node = try_get_node_mut(&mut self.nodes, current_node, &empty_path)?;

match &mut node.kind {
DiffTrieNodeKind::Null | DiffTrieNodeKind::Leaf(_) => {
DiffTrieNodeKind::EmptyRoot | DiffTrieNodeKind::Leaf(_) => {
result_stack.push(node.rlp_pointer_slow());
}
DiffTrieNodeKind::Extension(extension) => {
Expand Down Expand Up @@ -682,7 +682,7 @@ impl DiffTrie {
let node = self.nodes.get(&node_ptr).expect("node not found");
let mut child_rlp = Vec::new();
let rlp_encode = match &node.kind {
DiffTrieNodeKind::Null | DiffTrieNodeKind::Leaf(_) => node.rlp_encode(&[]),
DiffTrieNodeKind::EmptyRoot | DiffTrieNodeKind::Leaf(_) => node.rlp_encode(&[]),
DiffTrieNodeKind::Extension(extension) => {
if node.rlp_pointer.is_none() && extension.child.rlp_pointer.is_none() {
let child_node = extension.child.ptr();
Expand Down
6 changes: 3 additions & 3 deletions src/sparse_mpt/diff_trie/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct DiffTrieNode {
impl DiffTrieNode {
pub fn new_null() -> Self {
Self {
kind: DiffTrieNodeKind::Null,
kind: DiffTrieNodeKind::EmptyRoot,
rlp_pointer: None,
}
}
Expand Down Expand Up @@ -133,7 +133,7 @@ impl DiffTrieNode {
encode_branch_node(&child_rlp_pointers, &mut out);
out
}
DiffTrieNodeKind::Null => {
DiffTrieNodeKind::EmptyRoot => {
let mut out = Vec::with_capacity(1);
encode_null_node(&mut out);
out
Expand All @@ -148,7 +148,7 @@ pub enum DiffTrieNodeKind {
Leaf(DiffLeafNode),
Extension(DiffExtensionNode),
Branch(DiffBranchNode),
Null,
EmptyRoot,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
20 changes: 11 additions & 9 deletions src/sparse_mpt/fixed_trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub enum FixedTrieNode {
node: Arc<FixedBranchNode>,
child_ptrs: Vec<(u8, u64)>,
},
Null,
EmptyRoot,
}

#[derive(Debug, thiserror::Error)]
Expand Down Expand Up @@ -70,7 +70,7 @@ impl FixedTrieNode {
changed_children: SmallVec::new(),
aux_bits: node.child_mask,
}),
FixedTrieNode::Null => DiffTrieNodeKind::Null,
FixedTrieNode::EmptyRoot => DiffTrieNodeKind::EmptyRoot,
};
DiffTrieNode {
kind,
Expand Down Expand Up @@ -111,7 +111,8 @@ impl From<AlloyBranchNode> for FixedBranchNode {
let rlp_data = stack_iter
.next()
.expect("stack must be the same size as mask");
children[index as usize] = Some(rlp_data.into());
// @Pending: Eval replacing Bytes for ArrayVec to avoid the copy or dig deeper into low-level.
children[index as usize] = Some(Bytes::copy_from_slice(rlp_data.as_ref()));
child_mask |= 1 << index
}
}
Expand All @@ -132,7 +133,7 @@ impl From<AlloyExtensionNode> for FixedExtensionNode {
fn from(alloy_extension_node: AlloyExtensionNode) -> Self {
Self {
key: alloy_extension_node.key,
child: alloy_extension_node.child.into(),
child: Bytes::copy_from_slice(alloy_extension_node.child.as_ref()),
}
}
}
Expand Down Expand Up @@ -199,7 +200,7 @@ impl FixedTrie {
child_ptrs,
}
}
DiffTrieNodeKind::Null => FixedTrieNode::Null,
DiffTrieNodeKind::EmptyRoot => FixedTrieNode::EmptyRoot,
};
result.nodes.insert(*ptr, fixed_node);
}
Expand All @@ -212,7 +213,7 @@ impl FixedTrie {
pub fn add_nodes(&mut self, nodes: &[(Nibbles, Bytes)]) -> Result<(), AddNodeError> {
// when adding empty proof we init try to be empty
if nodes.is_empty() && self.nodes.is_empty() {
self.nodes.insert(0, FixedTrieNode::Null);
self.nodes.insert(0, FixedTrieNode::EmptyRoot);
self.head = 0;
self.ptrs = 0;
self.nodes_inserted.insert(Nibbles::new());
Expand All @@ -234,6 +235,7 @@ impl FixedTrie {
child_ptr: None,
},
AlloyTrieNode::Leaf(node) => FixedTrieNode::Leaf(Arc::new(node.into())),
AlloyTrieNode::EmptyRoot => FixedTrieNode::EmptyRoot,
};

// here we find parent to link with this new node
Expand Down Expand Up @@ -292,7 +294,7 @@ impl FixedTrie {
current_node =
get_child_ptr(child_ptrs, nibble).ok_or(AddNodeError::InvalidInput)?;
}
FixedTrieNode::Null | FixedTrieNode::Leaf(_) => {
FixedTrieNode::EmptyRoot | FixedTrieNode::Leaf(_) => {
return Err(AddNodeError::InvalidInput)
}
}
Expand All @@ -314,7 +316,7 @@ impl FixedTrie {
assert!(get_child_ptr(child_ptrs, child_nibble).is_none());
child_ptrs.push((child_nibble, ptr));
}
FixedTrieNode::Null | FixedTrieNode::Leaf(_) => unreachable!(),
FixedTrieNode::EmptyRoot | FixedTrieNode::Leaf(_) => unreachable!(),
}
} else {
assert_eq!(self.nodes.len(), 1);
Expand Down Expand Up @@ -365,7 +367,7 @@ impl FixedTrie {
.entry(c.current_node)
.or_insert_with(|| node.create_diff_node());
match (node, &mut diff_node.kind) {
(FixedTrieNode::Null, DiffTrieNodeKind::Null) => {
(FixedTrieNode::EmptyRoot, DiffTrieNodeKind::EmptyRoot) => {
// this is empty trie, we have everything to return
return Ok(result);
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use alloy_primitives::{keccak256, Bytes, B256};
use alloy_rlp::{length_of_length, BufMut, Encodable, Header, EMPTY_STRING_CODE};
use alloy_trie::nodes::{ExtensionNodeRef, LeafNodeRef};
use alloy_trie::Nibbles;
use reth_trie::word_rlp;
use reth_trie::RlpNode;
use rustc_hash::{FxBuildHasher, FxHasher};
use serde::{Deserialize, Serialize};

Expand All @@ -21,7 +21,7 @@ pub fn rlp_pointer(rlp_encode: Bytes) -> Bytes {
if rlp_encode.len() < 32 {
rlp_encode
} else {
word_rlp(&keccak256(&rlp_encode)).into()
Bytes::copy_from_slice(RlpNode::word_rlp(&keccak256(&rlp_encode)).as_ref())
}
}

Expand Down