chore: Make SparseTrie crate no_std compatible (#15786)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
kevaundray
2025-04-23 12:57:41 +01:00
committed by GitHub
parent ae3ffb90e4
commit 40f0edfc2e
11 changed files with 62 additions and 30 deletions

View File

@@ -31,7 +31,7 @@ reth-stages-api.workspace = true
reth-tasks.workspace = true
reth-trie-db.workspace = true
reth-trie-parallel.workspace = true
reth-trie-sparse.workspace = true
reth-trie-sparse = { workspace = true, features = ["std", "metrics"] }
reth-trie.workspace = true
# alloy

View File

@@ -19,7 +19,7 @@ reth-provider.workspace = true
reth-storage-errors.workspace = true
reth-trie-common.workspace = true
reth-trie-db.workspace = true
reth-trie-sparse.workspace = true
reth-trie-sparse = { workspace = true, features = ["std"] }
reth-trie.workspace = true
# alloy
@@ -56,7 +56,7 @@ tokio = { workspace = true, features = ["rt", "rt-multi-thread", "macros"] }
[features]
default = ["metrics"]
metrics = ["reth-metrics", "dep:metrics", "reth-trie/metrics"]
metrics = ["reth-metrics", "dep:metrics", "reth-trie/metrics", "reth-trie-sparse/metrics"]
test-utils = [
"reth-db-api/test-utils",
"reth-primitives-traits/test-utils",

View File

@@ -16,7 +16,7 @@ workspace = true
reth-primitives-traits.workspace = true
reth-execution-errors.workspace = true
reth-trie-common.workspace = true
reth-tracing.workspace = true
tracing.workspace = true
# alloy
alloy-primitives.workspace = true
@@ -27,8 +27,8 @@ auto_impl.workspace = true
smallvec = { workspace = true, features = ["const_new"] }
# metrics
reth-metrics.workspace = true
metrics.workspace = true
reth-metrics = { workspace = true, optional = true }
metrics = { workspace = true, optional = true }
[dev-dependencies]
reth-primitives-traits = { workspace = true, features = ["arbitrary"] }
@@ -38,6 +38,7 @@ reth-testing-utils.workspace = true
reth-trie = { workspace = true, features = ["test-utils"] }
reth-trie-common = { workspace = true, features = ["test-utils", "arbitrary"] }
reth-trie-db = { workspace = true, features = ["test-utils"] }
reth-tracing.workspace = true
arbitrary.workspace = true
assert_matches.workspace = true
@@ -50,7 +51,19 @@ rand.workspace = true
rand_08.workspace = true
[features]
default = ["std", "metrics"]
std = [
"reth-storage-api/std",
"reth-primitives-traits/std",
"reth-execution-errors/std",
"reth-trie-common/std",
"alloy-primitives/std",
"alloy-rlp/std",
"tracing/std",
]
metrics = ["dep:reth-metrics", "dep:metrics", "std"]
test-utils = [
"std",
"reth-primitives-traits/test-utils",
"reth-provider/test-utils",
"reth-trie-common/test-utils",
@@ -58,6 +71,7 @@ test-utils = [
"reth-trie/test-utils",
]
arbitrary = [
"std",
"reth-primitives-traits/arbitrary",
"reth-trie-common/arbitrary",
"alloy-primitives/arbitrary",

View File

@@ -1,6 +1,9 @@
//! The implementation of sparse MPT.
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![cfg_attr(not(feature = "std"), no_std)]
extern crate alloc;
mod state;
pub use state::*;

View File

@@ -1,5 +1,6 @@
//! Metrics for the sparse state trie
#[cfg(feature = "metrics")]
use reth_metrics::{metrics::Histogram, Metrics};
/// Metrics for the sparse state trie
@@ -16,24 +17,28 @@ pub(crate) struct SparseStateTrieMetrics {
/// Number of total storage nodes, including those that were skipped.
pub(crate) multiproof_total_storage_nodes: u64,
/// The actual metrics we will record into the histogram
#[cfg(feature = "metrics")]
pub(crate) histograms: SparseStateTrieHistograms,
}
impl SparseStateTrieMetrics {
/// Record the metrics into the histograms
pub(crate) fn record(&self) {
self.histograms
.multiproof_skipped_account_nodes
.record(self.multiproof_skipped_account_nodes as f64);
self.histograms
.multiproof_total_account_nodes
.record(self.multiproof_total_account_nodes as f64);
self.histograms
.multiproof_skipped_storage_nodes
.record(self.multiproof_skipped_storage_nodes as f64);
self.histograms
.multiproof_total_storage_nodes
.record(self.multiproof_total_storage_nodes as f64);
#[cfg(feature = "metrics")]
{
self.histograms
.multiproof_skipped_account_nodes
.record(self.multiproof_skipped_account_nodes as f64);
self.histograms
.multiproof_total_account_nodes
.record(self.multiproof_total_account_nodes as f64);
self.histograms
.multiproof_skipped_storage_nodes
.record(self.multiproof_skipped_storage_nodes as f64);
self.histograms
.multiproof_total_storage_nodes
.record(self.multiproof_total_storage_nodes as f64);
}
}
/// Increment the skipped account nodes counter by the given count
@@ -58,6 +63,7 @@ impl SparseStateTrieMetrics {
}
/// Metrics for the sparse state trie
#[cfg(feature = "metrics")]
#[derive(Metrics)]
#[metrics(scope = "sparse_state_trie")]
pub(crate) struct SparseStateTrieHistograms {

View File

@@ -3,23 +3,23 @@ use crate::{
metrics::SparseStateTrieMetrics,
RevealedSparseTrie, SparseTrie, TrieMasks,
};
use alloc::{collections::VecDeque, vec::Vec};
use alloy_primitives::{
hex,
map::{B256Map, HashMap, HashSet},
Bytes, B256,
};
use alloy_rlp::{Decodable, Encodable};
use core::fmt;
use core::{fmt, iter::Peekable};
use reth_execution_errors::{SparseStateTrieErrorKind, SparseStateTrieResult, SparseTrieErrorKind};
use reth_primitives_traits::Account;
use reth_tracing::tracing::trace;
use reth_trie_common::{
proof::ProofNodes,
updates::{StorageTrieUpdates, TrieUpdates},
MultiProof, Nibbles, RlpNode, StorageMultiProof, TrieAccount, TrieMask, TrieNode,
EMPTY_ROOT_HASH, TRIE_ACCOUNT_RLP_MAX_SIZE,
};
use std::{collections::VecDeque, iter::Peekable};
use tracing::trace;
/// Sparse state trie representing lazy-loaded Ethereum state trie.
pub struct SparseStateTrie<F: BlindedProviderFactory = DefaultBlindedProviderFactory> {

View File

@@ -1,4 +1,12 @@
use crate::blinded::{BlindedProvider, DefaultBlindedProvider, RevealedNode};
use alloc::{
borrow::Cow,
boxed::Box,
fmt,
string::{String, ToString},
vec,
vec::Vec,
};
use alloy_primitives::{
hex, keccak256,
map::{Entry, HashMap, HashSet},
@@ -6,14 +14,13 @@ use alloy_primitives::{
};
use alloy_rlp::Decodable;
use reth_execution_errors::{SparseTrieErrorKind, SparseTrieResult};
use reth_tracing::tracing::trace;
use reth_trie_common::{
prefix_set::{PrefixSet, PrefixSetMut},
BranchNodeCompact, BranchNodeRef, ExtensionNodeRef, LeafNodeRef, Nibbles, RlpNode, TrieMask,
TrieNode, CHILD_INDEX_RANGE, EMPTY_ROOT_HASH,
};
use smallvec::SmallVec;
use std::{borrow::Cow, fmt};
use tracing::trace;
/// Struct for passing around `hash_mask` and `tree_mask`
#[derive(Debug)]
@@ -653,7 +660,7 @@ impl<P> RevealedSparseTrie<P> {
/// Updates all remaining dirty nodes before calculating the root.
pub fn root(&mut self) -> B256 {
// Take the current prefix set
let mut prefix_set = std::mem::take(&mut self.prefix_set).freeze();
let mut prefix_set = core::mem::take(&mut self.prefix_set).freeze();
let rlp_node = self.rlp_node_allocate(&mut prefix_set);
if let Some(root_hash) = rlp_node.as_hash() {
root_hash
@@ -666,7 +673,7 @@ impl<P> RevealedSparseTrie<P> {
/// depth. Root node has a level of 0.
pub fn update_rlp_node_level(&mut self, depth: usize) {
// Take the current prefix set
let mut prefix_set = std::mem::take(&mut self.prefix_set).freeze();
let mut prefix_set = core::mem::take(&mut self.prefix_set).freeze();
let mut buffers = RlpNodeBuffers::default();
// Get the nodes that have changed at the given depth.
@@ -769,7 +776,7 @@ impl<P> RevealedSparseTrie<P> {
prefix_set: &mut PrefixSet,
buffers: &mut RlpNodeBuffers,
) -> RlpNode {
let starting_path = buffers.path_stack.last().map(|item| item.path.clone());
let _starting_path = buffers.path_stack.last().map(|item| item.path.clone());
'main: while let Some(RlpNodePathStackItem { level, path, mut is_in_prefix_set }) =
buffers.path_stack.pop()
@@ -777,7 +784,7 @@ impl<P> RevealedSparseTrie<P> {
let node = self.nodes.get_mut(&path).unwrap();
trace!(
target: "trie::sparse",
?starting_path,
?_starting_path,
?level,
?path,
?is_in_prefix_set,
@@ -1040,7 +1047,7 @@ impl<P> RevealedSparseTrie<P> {
trace!(
target: "trie::sparse",
?starting_path,
?_starting_path,
?level,
?path,
?node,

View File

@@ -30,7 +30,7 @@ alloy-consensus.workspace = true
alloy-trie.workspace = true
# tracing
tracing.workspace = true
tracing = { workspace = true, features = ["attributes"] }
# misc
auto_impl.workspace = true