Skip to content

Commit

Permalink
fix: resolve compile errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kien-rise committed Jan 20, 2025
1 parent afd676f commit b99899f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
16 changes: 8 additions & 8 deletions crates/trie/db/tests/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ fn account_and_storage_trie() {
assert!(!trie_updates
.storage_tries_ref()
.iter()
.any(|(_, u)| !u.storage_nodes_ref().is_empty() || !u.removed_nodes_ref().is_empty())); // no storage root update
.any(|(_, u)| !u.changed_nodes_ref().is_empty())); // no storage root update

assert_eq!(trie_updates.changed_nodes_ref().len(), 1);

Expand Down Expand Up @@ -610,7 +610,7 @@ fn account_trie_around_extension_node() {
&updates
.changed_nodes_ref()
.iter()
.map(|(k, v)| (k.clone(), v.clone().unwrap()))
.map(|(k, v)| (k.clone(), Some(v.clone().unwrap())))
.collect(),
);
}
Expand All @@ -633,7 +633,7 @@ fn account_trie_around_extension_node_with_dbtrie() {
.into_iter()
.map(|item| {
let (key, node) = item.unwrap();
(key.0, node)
(key.0, Some(node))
})
.collect();
assert_trie_updates(&account_updates);
Expand Down Expand Up @@ -688,7 +688,7 @@ fn storage_trie_around_extension_node() {
StorageRoot::from_tx_hashed(tx.tx_ref(), hashed_address).root_with_updates().unwrap();
assert_eq!(expected_root, got);
assert_eq!(expected_updates, updates);
assert_trie_updates(updates.storage_nodes_ref());
assert_trie_updates(updates.changed_nodes_ref());
}

fn extension_node_storage_trie<N: ProviderNodeTypes>(
Expand All @@ -715,7 +715,7 @@ fn extension_node_storage_trie<N: ProviderNodeTypes>(

let root = hb.root();
let (_, updates) = hb.split();
let trie_updates = StorageTrieUpdates::new(updates);
let trie_updates = StorageTrieUpdates::new(updates.into_iter().map(|(k, v)| (k, Some(v))));
(root, trie_updates)
}

Expand Down Expand Up @@ -743,14 +743,14 @@ fn extension_node_trie<N: ProviderNodeTypes>(
hb.root()
}

fn assert_trie_updates(account_updates: &HashMap<Nibbles, BranchNodeCompact>) {
fn assert_trie_updates(account_updates: &HashMap<Nibbles, Option<BranchNodeCompact>>) {
assert_eq!(account_updates.len(), 2);

let node = account_updates.get(&[0x3][..]).unwrap();
let expected = BranchNodeCompact::new(0b0011, 0b0001, 0b0000, vec![], None);
let expected = Some(BranchNodeCompact::new(0b0011, 0b0001, 0b0000, vec![], None));
assert_eq!(node, &expected);

let node = account_updates.get(&[0x3, 0x0, 0xA, 0xF][..]).unwrap();
let node = account_updates.get(&[0x3, 0x0, 0xA, 0xF][..]).unwrap().clone().unwrap();
assert_eq!(node.state_mask, TrieMask::new(0b101100000));
assert_eq!(node.tree_mask, TrieMask::new(0b000000000));
assert_eq!(node.hash_mask, TrieMask::new(0b001000000));
Expand Down
4 changes: 3 additions & 1 deletion crates/trie/sparse/benches/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ fn calculate_root_from_leaves_repeated(c: &mut Criterion) {
hb.root();

let (_, updates) = hb.split();
let trie_updates = StorageTrieUpdates::new(updates);
let trie_updates = StorageTrieUpdates::new(
updates.into_iter().map(|(k, v)| (k, Some(v))),
);
(init_storage, storage_updates, trie_updates)
},
|(init_storage, storage_updates, mut trie_updates)| {
Expand Down
50 changes: 41 additions & 9 deletions crates/trie/sparse/src/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,12 @@ mod tests {
};
use std::collections::BTreeMap;

fn to_updated_nodes(
changed_nodes: HashMap<Nibbles, Option<BranchNodeCompact>>,
) -> HashMap<Nibbles, BranchNodeCompact> {
changed_nodes.into_iter().filter_map(|(k, v)| v.map(|v| (k, v))).collect()
}

/// Pad nibbles to the length of a B256 hash with zeros on the left.
fn pad_nibbles_left(nibbles: Nibbles) -> Nibbles {
let mut base =
Expand Down Expand Up @@ -1471,7 +1477,14 @@ mod tests {
let sparse_updates = sparse.take_updates();

assert_eq!(sparse_root, hash_builder_root);
assert_eq!(sparse_updates.updated_nodes, hash_builder_updates.account_nodes);
assert_eq!(
hash_builder_updates.changed_nodes,
Iterator::chain(
sparse_updates.updated_nodes.into_iter().map(|(k, v)| (k, Some(v))),
sparse_updates.removed_nodes.into_iter().map(|k| (k, None))
)
.collect(),
);
assert_eq_sparse_trie_proof_nodes(&sparse, hash_builder_proof_nodes);
}

Expand Down Expand Up @@ -1502,7 +1515,14 @@ mod tests {
let sparse_updates = sparse.take_updates();

assert_eq!(sparse_root, hash_builder_root);
assert_eq!(sparse_updates.updated_nodes, hash_builder_updates.account_nodes);
assert_eq!(
sparse_updates.updated_nodes,
hash_builder_updates
.changed_nodes
.into_iter()
.filter_map(|(k, v)| v.map(|v| (k, v)))
.collect()
);
assert_eq_sparse_trie_proof_nodes(&sparse, hash_builder_proof_nodes);
}

Expand Down Expand Up @@ -1531,7 +1551,10 @@ mod tests {
let sparse_updates = sparse.take_updates();

assert_eq!(sparse_root, hash_builder_root);
assert_eq!(sparse_updates.updated_nodes, hash_builder_updates.account_nodes);
assert_eq!(
sparse_updates.updated_nodes,
to_updated_nodes(hash_builder_updates.changed_nodes)
);
assert_eq_sparse_trie_proof_nodes(&sparse, hash_builder_proof_nodes);
}

Expand Down Expand Up @@ -1570,7 +1593,7 @@ mod tests {
assert_eq!(sparse_root, hash_builder_root);
pretty_assertions::assert_eq!(
BTreeMap::from_iter(sparse_updates.updated_nodes),
BTreeMap::from_iter(hash_builder_updates.account_nodes)
BTreeMap::from_iter(to_updated_nodes(hash_builder_updates.changed_nodes))
);
assert_eq_sparse_trie_proof_nodes(&sparse, hash_builder_proof_nodes);
}
Expand Down Expand Up @@ -1606,7 +1629,10 @@ mod tests {
let sparse_updates = sparse.updates_ref();

assert_eq!(sparse_root, hash_builder_root);
assert_eq!(sparse_updates.updated_nodes, hash_builder_updates.account_nodes);
assert_eq!(
sparse_updates.updated_nodes,
to_updated_nodes(hash_builder_updates.changed_nodes)
);
assert_eq_sparse_trie_proof_nodes(&sparse, hash_builder_proof_nodes);

let (hash_builder_root, hash_builder_updates, hash_builder_proof_nodes, _) =
Expand All @@ -1623,7 +1649,10 @@ mod tests {
let sparse_updates = sparse.take_updates();

assert_eq!(sparse_root, hash_builder_root);
assert_eq!(sparse_updates.updated_nodes, hash_builder_updates.account_nodes);
assert_eq!(
sparse_updates.updated_nodes,
to_updated_nodes(hash_builder_updates.changed_nodes)
);
assert_eq_sparse_trie_proof_nodes(&sparse, hash_builder_proof_nodes);
}

Expand Down Expand Up @@ -1975,7 +2004,7 @@ mod tests {
// Assert that the sparse trie updates match the hash builder updates
pretty_assertions::assert_eq!(
sparse_updates.updated_nodes,
hash_builder_updates.account_nodes
to_updated_nodes(hash_builder_updates.changed_nodes)
);
// Assert that the sparse trie nodes match the hash builder proof nodes
assert_eq_sparse_trie_proof_nodes(&updated_sparse, hash_builder_proof_nodes);
Expand Down Expand Up @@ -2006,7 +2035,7 @@ mod tests {
// Assert that the sparse trie updates match the hash builder updates
pretty_assertions::assert_eq!(
sparse_updates.updated_nodes,
hash_builder_updates.account_nodes
to_updated_nodes(hash_builder_updates.changed_nodes)
);
// Assert that the sparse trie nodes match the hash builder proof nodes
assert_eq_sparse_trie_proof_nodes(&updated_sparse, hash_builder_proof_nodes);
Expand Down Expand Up @@ -2369,7 +2398,10 @@ mod tests {
let sparse_updates = sparse.take_updates();

assert_eq!(sparse_root, hash_builder_root);
assert_eq!(sparse_updates.updated_nodes, hash_builder_updates.account_nodes);
assert_eq!(
sparse_updates.updated_nodes,
to_updated_nodes(hash_builder_updates.changed_nodes)
);
}

#[test]
Expand Down

0 comments on commit b99899f

Please sign in to comment.