Skip to content

Commit

Permalink
chore(deps): bump nybbles, correct some unchecked usages (#6794)
Browse files Browse the repository at this point in the history
Co-authored-by: Roman Krasiuk <rokrassyuk@gmail.com>
  • Loading branch information
DaniPopes and rkrasiuk committed Feb 26, 2024
1 parent ac36fa9 commit 285312e
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 101 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Expand Up @@ -180,7 +180,7 @@ alloy-primitives = "0.6"
alloy-dyn-abi = "0.6"
alloy-sol-types = "0.6"
alloy-rlp = "0.3"
alloy-trie = "0.2"
alloy-trie = "0.3"
alloy-rpc-types = { git = "https://github.com/alloy-rs/alloy", rev = "76c70fb" }
alloy-rpc-trace-types = { git = "https://github.com/alloy-rs/alloy", rev = "76c70fb" }
alloy-rpc-engine-types = { git = "https://github.com/alloy-rs/alloy", rev = "76c70fb" }
Expand Down Expand Up @@ -223,7 +223,7 @@ metrics = "0.21.1" # Needed for `metrics-macro` to resolve the crate using `::me
hex-literal = "0.4"
once_cell = "1.17"
syn = "2.0"
nybbles = "0.1"
nybbles = "0.2.1"
smallvec = "1.13"

# proc-macros
Expand Down
6 changes: 1 addition & 5 deletions crates/primitives/Cargo.toml
Expand Up @@ -25,7 +25,7 @@ alloy-primitives = { workspace = true, features = ["rand", "rlp"] }
alloy-rlp = { workspace = true, features = ["arrayvec"] }
alloy-trie = { workspace = true, features = ["serde"] }
ethers-core = { workspace = true, default-features = false, optional = true }
nybbles = { version = "0.1.2", features = ["serde", "rlp"] }
nybbles = { workspace = true, features = ["serde", "rlp"] }
alloy-genesis.workspace = true
alloy-eips.workspace = true
# crypto
Expand Down Expand Up @@ -125,7 +125,3 @@ harness = false
name = "trie_root"
required-features = ["arbitrary", "test-utils"]
harness = false

[[bench]]
name = "nibbles"
harness = false
65 changes: 0 additions & 65 deletions crates/primitives/benches/nibbles.rs

This file was deleted.

7 changes: 3 additions & 4 deletions crates/trie/benches/prefix_set.rs
Expand Up @@ -92,15 +92,14 @@ fn generate_test_data(size: usize) -> (Vec<Nibbles>, Vec<Nibbles>, Vec<bool>) {
let config = ProptestConfig { result_cache: basic_result_cache, ..Default::default() };
let mut runner = TestRunner::new(config);

let mut preload = vec(vec(any::<u8>(), 32), size).new_tree(&mut runner).unwrap().current();
let vec_of_nibbles = |range| vec(any_with::<Nibbles>(range), size);
let mut preload = vec_of_nibbles(32usize.into()).new_tree(&mut runner).unwrap().current();
preload.dedup();
preload.sort();
let preload = preload.into_iter().map(Nibbles::from_nibbles_unchecked).collect::<Vec<_>>();

let mut input = vec(vec(any::<u8>(), 0..=32), size).new_tree(&mut runner).unwrap().current();
let mut input = vec_of_nibbles((0..=32usize).into()).new_tree(&mut runner).unwrap().current();
input.dedup();
input.sort();
let input = input.into_iter().map(Nibbles::from_nibbles_unchecked).collect::<Vec<_>>();

let expected = input
.iter()
Expand Down
16 changes: 8 additions & 8 deletions crates/trie/src/prefix_set/mod.rs
Expand Up @@ -176,14 +176,14 @@ mod tests {
#[test]
fn test_contains_with_multiple_inserts_and_duplicates() {
let mut prefix_set = PrefixSetMut::default();
prefix_set.insert(Nibbles::from_nibbles_unchecked(b"123"));
prefix_set.insert(Nibbles::from_nibbles_unchecked(b"124"));
prefix_set.insert(Nibbles::from_nibbles_unchecked(b"456"));
prefix_set.insert(Nibbles::from_nibbles_unchecked(b"123")); // Duplicate

assert!(prefix_set.contains(b"12"));
assert!(prefix_set.contains(b"45"));
assert!(!prefix_set.contains(b"78"));
prefix_set.insert(Nibbles::from_nibbles([1, 2, 3]));
prefix_set.insert(Nibbles::from_nibbles([1, 2, 4]));
prefix_set.insert(Nibbles::from_nibbles([4, 5, 6]));
prefix_set.insert(Nibbles::from_nibbles([1, 2, 3])); // Duplicate

assert!(prefix_set.contains(&[1, 2]));
assert!(prefix_set.contains(&[4, 5]));
assert!(!prefix_set.contains(&[7, 8]));
assert_eq!(prefix_set.len(), 3); // Length should be 3 (excluding duplicate)
}
}
7 changes: 2 additions & 5 deletions crates/trie/src/trie_cursor/database_cursors.rs
Expand Up @@ -172,14 +172,11 @@ mod tests {
let mut cursor = provider.tx_ref().cursor_dup_write::<tables::StoragesTrie>().unwrap();

let hashed_address = B256::random();
let key = vec![0x2, 0x3];
let key = StoredNibblesSubKey::from(vec![0x2, 0x3]);
let value = BranchNodeCompact::new(1, 1, 1, vec![B256::random()], None);

cursor
.upsert(
hashed_address,
StorageTrieEntry { nibbles: key.clone().into(), node: value.clone() },
)
.upsert(hashed_address, StorageTrieEntry { nibbles: key.clone(), node: value.clone() })
.unwrap();

let mut cursor = DatabaseStorageTrieCursor::new(cursor, hashed_address);
Expand Down
3 changes: 3 additions & 0 deletions crates/trie/src/trie_cursor/subnode.rs
Expand Up @@ -68,18 +68,21 @@ impl CursorSubNode {
}

/// Returns the full key of the current node.
#[inline]
pub fn full_key(&self) -> &Nibbles {
&self.full_key
}

/// Returns `true` if the state flag is set for the current nibble.
#[inline]
pub fn state_flag(&self) -> bool {
self.node
.as_ref()
.map_or(true, |node| self.nibble < 0 || node.state_mask.is_bit_set(self.nibble as u8))
}

/// Returns `true` if the tree flag is set for the current nibble.
#[inline]
pub fn tree_flag(&self) -> bool {
self.node
.as_ref()
Expand Down
19 changes: 11 additions & 8 deletions crates/trie/src/walker.rs
Expand Up @@ -171,7 +171,7 @@ impl<C: TrieCursor> TrieWalker<C> {

// Check if the walker needs to backtrack to the previous level in the trie during its
// traversal.
if subnode.nibble() >= 15 || (subnode.nibble() < 0 && !allow_root_to_child_nibble) {
if subnode.nibble() >= 0xf || (subnode.nibble() < 0 && !allow_root_to_child_nibble) {
self.stack.pop();
self.move_to_next_sibling(false)?;
return Ok(())
Expand All @@ -184,10 +184,13 @@ impl<C: TrieCursor> TrieWalker<C> {
}

// Find the next sibling with state.
while subnode.nibble() < 16 {
loop {
if subnode.state_flag() {
return Ok(())
}
if subnode.nibble() == 0xf {
break
}
subnode.inc_nibble();
}

Expand Down Expand Up @@ -354,26 +357,26 @@ mod tests {

// No changes
let mut cursor = TrieWalker::new(&mut trie, Default::default());
assert_eq!(cursor.key().cloned(), Some(Nibbles::from_nibbles_unchecked([]))); // root
assert_eq!(cursor.key().cloned(), Some(Nibbles::new())); // root
assert!(cursor.can_skip_current_node); // due to root_hash
cursor.advance().unwrap(); // skips to the end of trie
assert_eq!(cursor.key().cloned(), None);

// We insert something that's not part of the existing trie/prefix.
let mut changed = PrefixSetMut::default();
changed.insert(Nibbles::from_nibbles_unchecked([0xF, 0x1]));
changed.insert(Nibbles::from_nibbles([0xF, 0x1]));
let mut cursor = TrieWalker::new(&mut trie, changed.freeze());

// Root node
assert_eq!(cursor.key().cloned(), Some(Nibbles::from_nibbles_unchecked([])));
assert_eq!(cursor.key().cloned(), Some(Nibbles::new()));
// Should not be able to skip state due to the changed values
assert!(!cursor.can_skip_current_node);
cursor.advance().unwrap();
assert_eq!(cursor.key().cloned(), Some(Nibbles::from_nibbles_unchecked([0x2])));
assert_eq!(cursor.key().cloned(), Some(Nibbles::from_nibbles([0x2])));
cursor.advance().unwrap();
assert_eq!(cursor.key().cloned(), Some(Nibbles::from_nibbles_unchecked([0x2, 0x1])));
assert_eq!(cursor.key().cloned(), Some(Nibbles::from_nibbles([0x2, 0x1])));
cursor.advance().unwrap();
assert_eq!(cursor.key().cloned(), Some(Nibbles::from_nibbles_unchecked([0x4])));
assert_eq!(cursor.key().cloned(), Some(Nibbles::from_nibbles([0x4])));

cursor.advance().unwrap();
assert_eq!(cursor.key().cloned(), None); // the end of trie
Expand Down

0 comments on commit 285312e

Please sign in to comment.