diff --git a/crates/trie/db/tests/trie.rs b/crates/trie/db/tests/trie.rs index fbb8d7aed039..487e8e04b73c 100644 --- a/crates/trie/db/tests/trie.rs +++ b/crates/trie/db/tests/trie.rs @@ -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); @@ -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(), ); } @@ -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); @@ -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( @@ -715,7 +715,7 @@ fn extension_node_storage_trie( 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) } @@ -743,14 +743,14 @@ fn extension_node_trie( hb.root() } -fn assert_trie_updates(account_updates: &HashMap) { +fn assert_trie_updates(account_updates: &HashMap>) { 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)); diff --git a/crates/trie/sparse/benches/root.rs b/crates/trie/sparse/benches/root.rs index e01f9825d8ee..15e038dac0f5 100644 --- a/crates/trie/sparse/benches/root.rs +++ b/crates/trie/sparse/benches/root.rs @@ -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)| { diff --git a/crates/trie/sparse/src/trie.rs b/crates/trie/sparse/src/trie.rs index 8f54d9454022..0de00c94be56 100644 --- a/crates/trie/sparse/src/trie.rs +++ b/crates/trie/sparse/src/trie.rs @@ -1326,6 +1326,12 @@ mod tests { }; use std::collections::BTreeMap; + fn to_updated_nodes( + changed_nodes: HashMap>, + ) -> HashMap { + 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 = @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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, _) = @@ -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); } @@ -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); @@ -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); @@ -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]