Skip to content

Commit

Permalink
chore: make trie-common no-std (paradigmxyz#13473)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Dec 20, 2024
1 parent 30e8c78 commit 3966130
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ reth-tokio-util = { path = "crates/tokio-util" }
reth-tracing = { path = "crates/tracing" }
reth-transaction-pool = { path = "crates/transaction-pool" }
reth-trie = { path = "crates/trie/trie" }
reth-trie-common = { path = "crates/trie/common" }
reth-trie-common = { path = "crates/trie/common", default-features = false }
reth-trie-db = { path = "crates/trie/db" }
reth-trie-parallel = { path = "crates/trie/parallel" }
reth-trie-sparse = { path = "crates/trie/sparse" }
Expand Down
3 changes: 2 additions & 1 deletion crates/chainspec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ std = [
"alloy-rlp/std",
"reth-ethereum-forks/std",
"derive_more/std",
"reth-network-peers/std"
"reth-network-peers/std",
"reth-trie-common/std"
]
arbitrary = [
"alloy-chains/arbitrary",
Expand Down
3 changes: 2 additions & 1 deletion crates/evm/execution-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@ std = [
"serde?/std",
"reth-primitives-traits/std",
"alloy-consensus/std",
"serde_with?/std"
"serde_with?/std",
"reth-trie-common?/std"
]
3 changes: 2 additions & 1 deletion crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ std = [
"bytes/std",
"derive_more/std",
"reth-zstd-compressors?/std",
"secp256k1?/std"
"secp256k1?/std",
"reth-trie-common/std"
]
reth-codec = [
"dep:reth-codecs",
Expand Down
16 changes: 16 additions & 0 deletions crates/trie/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ serde_json.workspace = true
serde_with.workspace = true

[features]
default = ["std"]
std = [
"alloy-consensus/std",
"alloy-genesis/std",
"alloy-primitives/std",
"alloy-rlp/std",
"alloy-rpc-types-eth?/std",
"alloy-serde?/std",
"alloy-trie/std",
"bytes?/std",
"derive_more/std",
"nybbles/std",
"reth-primitives-traits/std",
"serde?/std",
"serde_with?/std"
]
eip1186 = [
"alloy-rpc-types-eth/serde",
"dep:alloy-serde",
Expand Down
1 change: 1 addition & 0 deletions crates/trie/common/src/hash_builder/state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::TrieMask;
use alloc::vec::Vec;
use alloy_trie::{hash_builder::HashBuilderValue, nodes::RlpNode, HashBuilder};
use nybbles::Nibbles;

Expand Down
3 changes: 3 additions & 0 deletions crates/trie/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
)]
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;

/// The implementation of hash builder.
pub mod hash_builder;
Expand Down
3 changes: 2 additions & 1 deletion crates/trie/common/src/nibbles.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::vec::Vec;
use derive_more::Deref;
pub use nybbles::Nibbles;

Expand Down Expand Up @@ -30,7 +31,7 @@ impl PartialEq<[u8]> for StoredNibbles {

impl PartialOrd<[u8]> for StoredNibbles {
#[inline]
fn partial_cmp(&self, other: &[u8]) -> Option<std::cmp::Ordering> {
fn partial_cmp(&self, other: &[u8]) -> Option<core::cmp::Ordering> {
self.0.as_slice().partial_cmp(other)
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/trie/common/src/prefix_set.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::Nibbles;
use alloc::{sync::Arc, vec::Vec};
use alloy_primitives::map::{B256HashMap, B256HashSet};
use std::sync::Arc;

/// Collection of mutable prefix sets.
#[derive(Clone, Default, Debug)]
Expand Down Expand Up @@ -209,7 +209,7 @@ impl PrefixSet {

impl<'a> IntoIterator for &'a PrefixSet {
type Item = &'a Nibbles;
type IntoIter = std::slice::Iter<'a, Nibbles>;
type IntoIter = core::slice::Iter<'a, Nibbles>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
Expand Down
1 change: 1 addition & 0 deletions crates/trie/common/src/proofs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Merkle trie proofs.
use crate::{Nibbles, TrieAccount};
use alloc::vec::Vec;
use alloy_consensus::constants::KECCAK_EMPTY;
use alloy_primitives::{
keccak256,
Expand Down
1 change: 1 addition & 0 deletions crates/trie/common/src/root.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Common root computation functions.
use crate::TrieAccount;
use alloc::vec::Vec;
use alloy_primitives::{keccak256, Address, B256, U256};
use alloy_rlp::Encodable;
use alloy_trie::HashBuilder;
Expand Down
1 change: 1 addition & 0 deletions crates/trie/common/src/subnode.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::BranchNodeCompact;
use alloc::vec::Vec;

/// Walker sub node for storing intermediate state root calculation state in the database.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
Expand Down
15 changes: 12 additions & 3 deletions crates/trie/common/src/updates.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{BranchNodeCompact, HashBuilder, Nibbles};
use alloc::vec::Vec;
use alloy_primitives::{
map::{B256HashMap, B256HashSet, HashMap, HashSet},
B256,
Expand Down Expand Up @@ -230,6 +231,10 @@ impl StorageTrieUpdates {
#[cfg(any(test, feature = "serde"))]
mod serde_nibbles_set {
use crate::Nibbles;
use alloc::{
string::{String, ToString},
vec::Vec,
};
use alloy_primitives::map::HashSet;
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};

Expand Down Expand Up @@ -266,13 +271,17 @@ mod serde_nibbles_set {
#[cfg(any(test, feature = "serde"))]
mod serde_nibbles_map {
use crate::Nibbles;
use alloc::{
string::{String, ToString},
vec::Vec,
};
use alloy_primitives::{hex, map::HashMap};
use core::marker::PhantomData;
use serde::{
de::{Error, MapAccess, Visitor},
ser::SerializeMap,
Deserialize, Deserializer, Serialize, Serializer,
};
use std::marker::PhantomData;

pub(super) fn serialize<S, T>(
map: &HashMap<Nibbles, T>,
Expand Down Expand Up @@ -308,7 +317,7 @@ mod serde_nibbles_map {
{
type Value = HashMap<Nibbles, T>;

fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn expecting(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
formatter.write_str("a map with hex-encoded Nibbles keys")
}

Expand Down Expand Up @@ -411,10 +420,10 @@ fn exclude_empty_from_pair<V>(
#[cfg(feature = "serde-bincode-compat")]
pub mod serde_bincode_compat {
use crate::{BranchNodeCompact, Nibbles};
use alloc::borrow::Cow;
use alloy_primitives::map::{B256HashMap, HashMap, HashSet};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_with::{DeserializeAs, SerializeAs};
use std::borrow::Cow;

/// Bincode-compatible [`super::TrieUpdates`] serde implementation.
///
Expand Down

0 comments on commit 3966130

Please sign in to comment.