Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
Added a test for trie cloning (0xPolygonZero#98)
Browse files Browse the repository at this point in the history
Co-authored-by: Ben <[email protected]>
  • Loading branch information
BGluth and muursh authored Mar 11, 2024
1 parent bafd06d commit 05f8722
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions mpt_trie/src/trie_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,13 +770,14 @@ fn create_node_if_ins_val_not_hash<N, F: FnOnce(Vec<u8>) -> WrappedNode<N>>(

#[cfg(test)]
mod tests {
use std::collections::HashSet;
use std::{collections::HashSet, iter::once};

use log::debug;

use super::ValOrHash;
use crate::{
partial_trie::{Node, PartialTrie, StandardTrie},
nibbles::Nibbles,
partial_trie::{HashedPartialTrie, Node, PartialTrie, StandardTrie},
testing_utils::{
common_setup, entry, entry_with_value,
generate_n_hash_nodes_entries_for_empty_slots_in_trie,
Expand Down Expand Up @@ -877,6 +878,27 @@ mod tests {
assert_eq!(trie.get(0x1234), Some([100].as_slice()));
}

#[test]
fn cloning_a_trie_creates_two_separate_tries() {
common_setup();

assert_cloning_works_for_tries::<StandardTrie>();
assert_cloning_works_for_tries::<HashedPartialTrie>();
}

fn assert_cloning_works_for_tries<T>()
where
T: FromIterator<(Nibbles, Vec<u8>)> + PartialTrie,
{
let trie = T::from_iter(once(entry(0x1234)));
let mut cloned_trie = trie.clone();

cloned_trie.extend(once(entry(0x5678)));

assert_ne!(trie, cloned_trie);
assert_ne!(trie.hash(), cloned_trie.hash());
}

#[test]
fn mass_inserts_fixed_sized_keys_all_entries_are_retrievable() {
common_setup();
Expand Down

0 comments on commit 05f8722

Please sign in to comment.