Skip to content
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
fail-fast: false
matrix:
rust: ["stable", "beta", "nightly", "1.81"] # MSRV
rust: ["stable", "beta", "nightly", "1.85"] # MSRV
flags: ["--no-default-features", "", "--all-features"]
steps:
- uses: actions/checkout@v3
Expand All @@ -29,7 +29,7 @@ jobs:
- name: build
run: cargo build --workspace ${{ matrix.flags }}
- name: test
if: ${{ matrix.rust != '1.81' }} # MSRV
if: ${{ matrix.rust != '1.85' }} # MSRV
run: cargo test --workspace ${{ matrix.flags }}

miri:
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ description = """
Fast Merkle-Patricia Trie (MPT) state root calculator
and proof generator for prefix-sorted nibbles
"""
edition = "2021"
rust-version = "1.81"
edition = "2024"
rust-version = "1.85"
license = "MIT OR Apache-2.0"
categories = ["data-structures", "no-std"]
keywords = ["nibbles", "trie", "mpt", "merkle", "ethereum"]
Expand All @@ -32,7 +32,7 @@ all = "warn"

[lints.clippy]
all = { level = "warn", priority = -1 }
missing-const-for-fn = "allow" # TODO: https://github.com/rust-lang/rust-clippy/issues/14020
missing-const-for-fn = "allow" # TODO: https://github.com/rust-lang/rust-clippy/issues/14020
use-self = "warn"
redundant-clone = "warn"
result_large_err = "allow"
Expand Down
2 changes: 1 addition & 1 deletion benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use alloy_trie::nodes::encode_path_leaf;
use criterion::{
criterion_group, criterion_main, measurement::WallTime, BenchmarkGroup, Criterion,
BenchmarkGroup, Criterion, criterion_group, criterion_main, measurement::WallTime,
};
use nybbles::Nibbles;
use proptest::{prelude::*, strategy::ValueTree};
Expand Down
2 changes: 1 addition & 1 deletion clippy.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
msrv = "1.81"
msrv = "1.85"
4 changes: 2 additions & 2 deletions src/account.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{EMPTY_ROOT_HASH, KECCAK_EMPTY};
use alloy_primitives::{keccak256, B256, U256};
use alloy_primitives::{B256, U256, keccak256};
use alloy_rlp::{RlpDecodable, RlpEncodable};

/// Represents an TrieAccount in the account trie.
Expand Down Expand Up @@ -62,7 +62,7 @@ mod quantity {
#[cfg(test)]
mod tests {
use super::*;
use alloy_primitives::{hex, U256};
use alloy_primitives::{U256, hex};
use alloy_rlp::Decodable;

#[test]
Expand Down
14 changes: 5 additions & 9 deletions src/hash_builder/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//! The implementation of the hash builder.

use super::{
BranchNodeCompact, EMPTY_ROOT_HASH, Nibbles, TrieMask,
nodes::{BranchNodeRef, ExtensionNodeRef, LeafNodeRef},
proof::ProofRetainer,
BranchNodeCompact, Nibbles, TrieMask, EMPTY_ROOT_HASH,
};
use crate::{nodes::RlpNode, proof::ProofNodes, HashMap};
use crate::{HashMap, nodes::RlpNode, proof::ProofNodes};
use alloc::vec::Vec;
use alloy_primitives::{keccak256, B256};
use alloy_primitives::{B256, keccak256};
use alloy_rlp::EMPTY_STRING_CODE;
use core::cmp;
use tracing::trace;
Expand Down Expand Up @@ -183,11 +183,7 @@ impl HashBuilder {

fn current_root(&self) -> B256 {
if let Some(node_ref) = self.stack.last() {
if let Some(hash) = node_ref.as_hash() {
hash
} else {
keccak256(node_ref)
}
if let Some(hash) = node_ref.as_hash() { hash } else { keccak256(node_ref) }
} else {
EMPTY_ROOT_HASH
}
Expand Down Expand Up @@ -455,7 +451,7 @@ mod tests {
use super::*;
use crate::{nodes::LeafNode, triehash_trie_root};
use alloc::collections::BTreeMap;
use alloy_primitives::{b256, hex, U256};
use alloy_primitives::{U256, b256, hex};
use alloy_rlp::Encodable;

// Hashes the keys, RLP encodes the values, compares the trie builder with the upstream root.
Expand Down
2 changes: 1 addition & 1 deletion src/hash_builder/value.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alloc::vec::Vec;
use alloy_primitives::{hex, B256};
use alloy_primitives::{B256, hex};
use core::fmt;

/// Hash builder value.
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub use alloy_primitives::map::HashMap;
#[doc(no_inline)]
pub use nybbles::{self, Nibbles};

use alloy_primitives::{b256, B256};
use alloy_primitives::{B256, b256};

/// Root hash of an empty trie.
pub const EMPTY_ROOT_HASH: B256 =
Expand Down
6 changes: 1 addition & 5 deletions src/mask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ impl TrieMask {
/// Returns the index of the first bit set in the mask, or `None` if the mask is empty.
#[inline]
pub const fn first_set_bit_index(self) -> Option<u8> {
if self.is_empty() {
None
} else {
Some(self.0.trailing_zeros() as u8)
}
if self.is_empty() { None } else { Some(self.0.trailing_zeros() as u8) }
}

/// Set bit at a specified index.
Expand Down
8 changes: 4 additions & 4 deletions src/nodes/branch.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{super::TrieMask, RlpNode, CHILD_INDEX_RANGE};
use alloy_primitives::{hex, B256};
use alloy_rlp::{length_of_length, Buf, BufMut, Decodable, Encodable, Header, EMPTY_STRING_CODE};
use super::{super::TrieMask, CHILD_INDEX_RANGE, RlpNode};
use alloy_primitives::{B256, hex};
use alloy_rlp::{Buf, BufMut, Decodable, EMPTY_STRING_CODE, Encodable, Header, length_of_length};
use core::{fmt, ops::Range, slice::Iter};

use alloc::sync::Arc;
Expand Down Expand Up @@ -352,7 +352,7 @@ mod tests {
assert_eq!(BranchNode::decode(&mut &encoded[..]).unwrap(), branch_with_ext);

let full = BranchNode::new(
core::iter::repeat(RlpNode::word_rlp(&B256::repeat_byte(23))).take(16).collect(),
core::iter::repeat_n(RlpNode::word_rlp(&B256::repeat_byte(23)), 16).collect(),
TrieMask::new(u16::MAX),
);
let encoded = alloy_rlp::encode(&full);
Expand Down
6 changes: 3 additions & 3 deletions src/nodes/extension.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{super::Nibbles, encode_path_leaf, unpack_path_to_nibbles, RlpNode};
use alloy_primitives::{hex, Bytes};
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header};
use super::{super::Nibbles, RlpNode, encode_path_leaf, unpack_path_to_nibbles};
use alloy_primitives::{Bytes, hex};
use alloy_rlp::{BufMut, Decodable, Encodable, Header, length_of_length};
use core::fmt;

#[allow(unused_imports)]
Expand Down
6 changes: 3 additions & 3 deletions src/nodes/leaf.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{super::Nibbles, encode_path_leaf, unpack_path_to_nibbles, RlpNode};
use alloy_primitives::{hex, Bytes};
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header};
use super::{super::Nibbles, RlpNode, encode_path_leaf, unpack_path_to_nibbles};
use alloy_primitives::{Bytes, hex};
use alloy_rlp::{BufMut, Decodable, Encodable, Header, length_of_length};
use core::fmt;

#[allow(unused_imports)]
Expand Down
19 changes: 13 additions & 6 deletions src/nodes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Various branch nodes produced by the hash builder.

use alloy_primitives::{Bytes, B256};
use alloy_rlp::{Decodable, Encodable, Header, EMPTY_STRING_CODE};
use alloy_primitives::{B256, Bytes};
use alloy_rlp::{Decodable, EMPTY_STRING_CODE, Encodable, Header};
use core::ops::Range;
use nybbles::Nibbles;
use smallvec::SmallVec;
Expand Down Expand Up @@ -71,7 +71,7 @@ impl Decodable for TrieNode {
Ok(Self::EmptyRoot)
} else {
Err(alloy_rlp::Error::UnexpectedString)
}
};
}
};

Expand Down Expand Up @@ -298,7 +298,7 @@ mod tests {

// branch
let branch = TrieNode::Branch(BranchNode::new(
core::iter::repeat(RlpNode::word_rlp(&B256::repeat_byte(23))).take(16).collect(),
core::iter::repeat_n(RlpNode::word_rlp(&B256::repeat_byte(23)), 16).collect(),
TrieMask::new(u16::MAX),
));
let mut rlp = vec![];
Expand All @@ -307,13 +307,20 @@ mod tests {
rlp_node[..],
hex!("a0bed74980bbe29d9c4439c10e9c451e29b306fe74bcf9795ecf0ebbd92a220513")
);
assert_eq!(rlp, hex!("f90211a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a0171717171717171717171717171717171717171717171717171717171717171780"));
assert_eq!(
rlp,
hex!(
"f90211a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a01717171717171717171717171717171717171717171717171717171717171717a0171717171717171717171717171717171717171717171717171717171717171780"
)
);
assert_eq!(TrieNode::decode(&mut &rlp[..]).unwrap(), branch);
}

#[test]
fn hashed_encode_path_regression() {
let nibbles = Nibbles::from_nibbles(hex!("05010406040a040203030f010805020b050c04070003070e0909070f010b0a0805020301070c0a0902040b0f000f0006040a04050f020b090701000a0a040b"));
let nibbles = Nibbles::from_nibbles(hex!(
"05010406040a040203030f010805020b050c04070003070e0909070f010b0a0805020301070c0a0902040b0f000f0006040a04050f020b090701000a0a040b"
));
let path = encode_path_leaf(&nibbles, true);
let expected = hex!("351464a4233f1852b5c47037e997f1ba852317ca924bf0f064a45f2b9710aa4b");
assert_eq!(path[..], expected);
Expand Down
8 changes: 2 additions & 6 deletions src/nodes/rlp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloy_primitives::{hex, keccak256, B256};
use alloy_primitives::{B256, hex, keccak256};
use alloy_rlp::EMPTY_STRING_CODE;
use arrayvec::ArrayVec;
use core::fmt;
Expand Down Expand Up @@ -99,11 +99,7 @@ impl RlpNode {
/// Returns hash if this is an RLP-encoded hash
#[inline]
pub fn as_hash(&self) -> Option<B256> {
if self.is_hash() {
Some(B256::from_slice(&self.0[1..]))
} else {
None
}
if self.is_hash() { Some(B256::from_slice(&self.0[1..])) } else { None }
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/proof/decoded_proof_nodes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{nodes::TrieNode, proof::ProofNodes, HashMap, Nibbles};
use crate::{HashMap, Nibbles, nodes::TrieNode, proof::ProofNodes};
use alloy_primitives::Bytes;
use alloy_rlp::Decodable;
use core::ops::Deref;
Expand Down
2 changes: 1 addition & 1 deletion src/proof/decoded_retainer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{proof::DecodedProofNodes, Nibbles};
use crate::{Nibbles, proof::DecodedProofNodes};
use alloy_primitives::Bytes;

use alloc::vec::Vec;
Expand Down
2 changes: 1 addition & 1 deletion src/proof/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloy_primitives::{Bytes, B256};
use alloy_primitives::{B256, Bytes};
use core::fmt;
use nybbles::Nibbles;

Expand Down
2 changes: 1 addition & 1 deletion src/proof/retainer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{proof::ProofNodes, Nibbles};
use crate::{Nibbles, proof::ProofNodes};
use alloy_primitives::Bytes;

use alloc::vec::Vec;
Expand Down
15 changes: 8 additions & 7 deletions src/proof/verify.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! Proof verification logic.

use crate::{
nodes::{BranchNode, RlpNode, TrieNode, CHILD_INDEX_RANGE},
proof::ProofVerificationError,
EMPTY_ROOT_HASH,
nodes::{BranchNode, CHILD_INDEX_RANGE, RlpNode, TrieNode},
proof::ProofVerificationError,
};
use alloc::vec::Vec;
use alloy_primitives::{Bytes, B256};
use alloy_primitives::{B256, Bytes};
use alloy_rlp::{Decodable, EMPTY_STRING_CODE};
use core::ops::Deref;
use nybbles::Nibbles;
Expand All @@ -27,7 +27,7 @@ where
let mut proof = proof.into_iter().peekable();

// If the proof is empty or contains only an empty node, the expected value must be None.
if proof.peek().map_or(true, |node| node.as_ref() == [EMPTY_STRING_CODE]) {
if proof.peek().is_none_or(|node| node.as_ref() == [EMPTY_STRING_CODE]) {
return if root == EMPTY_ROOT_HASH {
if expected_value.is_none() {
Ok(())
Expand Down Expand Up @@ -177,7 +177,7 @@ fn process_branch(
return Ok(Some(NodeDecodingResult::Value(child_leaf.value)));
}
TrieNode::EmptyRoot => {
return Err(ProofVerificationError::UnexpectedEmptyRoot)
return Err(ProofVerificationError::UnexpectedEmptyRoot);
}
}
};
Expand All @@ -194,12 +194,13 @@ fn process_branch(
mod tests {
use super::*;
use crate::{
HashBuilder, TrieMask,
nodes::{BranchNode, ExtensionNode, LeafNode},
proof::{ProofNodes, ProofRetainer},
triehash_trie_root, HashBuilder, TrieMask,
triehash_trie_root,
};
use alloy_primitives::hex;
use alloy_rlp::{Encodable, EMPTY_STRING_CODE};
use alloy_rlp::{EMPTY_STRING_CODE, Encodable};
use core::str::FromStr;

#[test]
Expand Down
4 changes: 2 additions & 2 deletions src/root.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{HashBuilder, EMPTY_ROOT_HASH};
use crate::{EMPTY_ROOT_HASH, HashBuilder};
use alloc::vec::Vec;
use alloy_primitives::B256;
use alloy_rlp::Encodable;
Expand Down Expand Up @@ -54,7 +54,7 @@ pub use ethereum::*;
mod ethereum {
use super::*;
use crate::TrieAccount;
use alloy_primitives::{keccak256, Address, U256};
use alloy_primitives::{Address, U256, keccak256};

/// Hashes storage keys, sorts them and them calculates the root hash of the storage trie.
/// See [`storage_root_unsorted`] for more info.
Expand Down
Loading