Skip to content

Commit

Permalink
feat: support no_std
Browse files Browse the repository at this point in the history
  • Loading branch information
yjhmelody committed Dec 22, 2023
1 parent 66da792 commit a1bfbe1
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 17 deletions.
22 changes: 15 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ repository = "https://github.com/alloy-rs/trie"
exclude = [".github/", "deny.toml", "release.toml", "rustfmt.toml"]

[dependencies]
alloy-primitives = { version = "0.5", features = ["rlp"] }
alloy-rlp = { version = "0.3", features = ["derive"] }
alloy-primitives = { version = "0.5", default-features = false, features = ["rlp"] }
alloy-rlp = { version = "0.3", default-features = false, features = ["derive"] }
derive_more = "0.99"
nybbles = "0.1"
nybbles = { version = "0.1", default-features = false }
smallvec = "1.11"
tracing = "0.1"

tracing = { version = "0.1", default-features = false }
hashbrown = { version = "0.14" }
# serde
serde = { version = "1.0", features = ["derive"], optional = true }
serde = { version = "1.0", default-features = false, features = ["derive"], optional = true }

# arbitrary
arbitrary = { version = "1.3", optional = true }
Expand All @@ -33,11 +33,19 @@ proptest = { version = "1.4", optional = true }
proptest-derive = { version = "0.4", optional = true }

[dev-dependencies]
hash-db = "0.15"
hash-db = "0.16"
plain_hasher = "0.2"
triehash = "0.8.4"

[features]
default = ["std"]
std = [
"alloy-primitives/std",
"alloy-rlp/std",
"nybbles/std",
"tracing/std",
"serde?/std",
]
serde = ["dep:serde", "alloy-primitives/serde", "nybbles/serde"]
arbitrary = [
"dep:arbitrary",
Expand Down
15 changes: 9 additions & 6 deletions src/hash_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ use super::{
BranchNodeCompact, Nibbles, TrieMask, EMPTY_ROOT_HASH,
};
use alloy_primitives::{keccak256, Bytes, B256};
use std::{
collections::{BTreeMap, HashMap},
fmt::Debug,
};
use core::cmp;
#[cfg(not(feature = "std"))]
use hashbrown::HashMap;
#[cfg(feature = "std")]
use std::collections::HashMap;
use std::{collections::BTreeMap, fmt::Debug, format, vec, vec::Vec};
use tracing::trace;

mod value;
Expand Down Expand Up @@ -79,7 +81,7 @@ impl HashBuilder {
/// Call [HashBuilder::split] to get the updates to branch nodes.
pub fn set_updates(&mut self, retain_updates: bool) {
if retain_updates {
self.updated_branch_nodes = Some(HashMap::default());
self.updated_branch_nodes = Some(HashMap::new());
}
}

Expand All @@ -101,6 +103,7 @@ impl HashBuilder {
}

/// Print the current stack of the Hash Builder.
#[cfg(feature = "std")]
pub fn print_stack(&self) {
println!("============ STACK ===============");
for item in &self.stack {
Expand Down Expand Up @@ -191,7 +194,7 @@ impl HashBuilder {
let preceding_len = self.groups.len().saturating_sub(1);

let common_prefix_len = succeeding.common_prefix_length(current.as_slice());
let len = std::cmp::max(preceding_len, common_prefix_len);
let len = cmp::max(preceding_len, common_prefix_len);
assert!(len < current.len());

trace!(
Expand Down
2 changes: 1 addition & 1 deletion src/hash_builder/proof_retainer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::Nibbles;
use alloy_primitives::Bytes;
use std::collections::BTreeMap;
use std::{collections::BTreeMap, vec::Vec};

/// Proof retainer is used to store proofs during merkle trie construction.
/// It is intended to be used within the [`HashBuilder`](crate::HashBuilder).
Expand Down
1 change: 1 addition & 0 deletions src/hash_builder/value.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use alloy_primitives::B256;
use std::{vec, vec::Vec};

/// The current value of the hash builder.
#[derive(Clone, PartialEq)]
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![deny(unused_must_use, rust_2018_idioms)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(not(feature = "std"))]
extern crate alloc as std;

pub mod nodes;
pub use nodes::BranchNodeCompact;
Expand Down
1 change: 1 addition & 0 deletions src/nodes/branch.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{super::TrieMask, rlp_node, CHILD_INDEX_RANGE};
use alloy_primitives::B256;
use alloy_rlp::{BufMut, EMPTY_STRING_CODE};
use std::vec::Vec;

/// A Branch node is only a pointer to the stack of nodes and is used to
/// create the RLP encoding of the node using masks which filter from
Expand Down
1 change: 1 addition & 0 deletions src/nodes/extension.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{super::Nibbles, rlp_node};
use alloy_rlp::{BufMut, Encodable};
use smallvec::SmallVec;
use std::vec::Vec;

/// An intermediate node that exists solely to compress the trie's paths. It contains a path segment
/// (a shared prefix of keys) and a single child pointer. Essentially, an extension node can be
Expand Down
7 changes: 5 additions & 2 deletions src/nodes/leaf.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use super::{super::Nibbles, rlp_node};
use alloy_rlp::{BufMut, Encodable};
use smallvec::SmallVec;
use std::vec::Vec;

use core::fmt;

/// A leaf node represents the endpoint or terminal node in the trie. In other words, a leaf node is
/// where actual values are stored.
Expand Down Expand Up @@ -43,8 +46,8 @@ impl Encodable for LeafNode<'_> {
}
}

impl std::fmt::Debug for LeafNode<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl fmt::Debug for LeafNode<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("LeafNode")
.field("key", &alloy_primitives::hex::encode(&self.key))
.field("value", &alloy_primitives::hex::encode(self.value))
Expand Down
3 changes: 2 additions & 1 deletion src/nodes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

use alloy_primitives::{keccak256, B256};
use alloy_rlp::EMPTY_STRING_CODE;
use std::ops::Range;
use core::ops::Range;
use std::vec::Vec;

mod branch;
pub use branch::{BranchNode, BranchNodeCompact};
Expand Down

0 comments on commit a1bfbe1

Please sign in to comment.