Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(trie): fuzz in-memory storage nodes #10413

Merged
merged 1 commit into from
Aug 21, 2024
Merged
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
59 changes: 55 additions & 4 deletions crates/trie/db/tests/fuzz_in_memory_nodes.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use proptest::prelude::*;
use reth_db::{cursor::DbCursorRW, tables, transaction::DbTxMut};
use reth_primitives::{Account, B256, U256};
use reth_primitives::{Account, StorageEntry, B256, U256};
use reth_provider::test_utils::create_test_provider_factory;
use reth_trie::{
prefix_set::{PrefixSetMut, TriePrefixSets},
test_utils::state_root_prehashed,
test_utils::{state_root_prehashed, storage_root_prehashed},
trie_cursor::InMemoryTrieCursorFactory,
StateRoot,
updates::TrieUpdates,
StateRoot, StorageRoot,
};
use reth_trie_common::Nibbles;
use reth_trie_db::{DatabaseStateRoot, DatabaseTrieCursorFactory};
use reth_trie_db::{DatabaseStateRoot, DatabaseStorageRoot, DatabaseTrieCursorFactory};
use std::collections::BTreeMap;

proptest! {
Expand Down Expand Up @@ -60,7 +61,57 @@ proptest! {
);
assert_eq!(expected_root, state_root);
}
}

#[test]
fn fuzz_in_memory_storage_nodes(mut init_storage: BTreeMap<B256, U256>, storage_updates: [BTreeMap<B256, U256>; 10]) {
let hashed_address = B256::random();
let factory = create_test_provider_factory();
let provider = factory.provider_rw().unwrap();
let mut hashed_storage_cursor =
provider.tx_ref().cursor_write::<tables::HashedStorages>().unwrap();

// Insert init state into database
for (hashed_slot, value) in init_storage.clone() {
hashed_storage_cursor
.upsert(hashed_address, StorageEntry { key: hashed_slot, value })
.unwrap();
}

// Compute initial storage root and updates
let (_, _, mut storage_trie_nodes) =
StorageRoot::from_tx_hashed(provider.tx_ref(), hashed_address).root_with_updates().unwrap();

let mut storage = init_storage;
for mut storage_update in storage_updates {
// Insert state updates into database
let mut changes = PrefixSetMut::default();
for (hashed_slot, value) in storage_update.clone() {
hashed_storage_cursor
.upsert(hashed_address, StorageEntry { key: hashed_slot, value })
.unwrap();
changes.insert(Nibbles::unpack(hashed_slot));
}

// Compute root with in-memory trie nodes overlay
let mut trie_nodes = TrieUpdates::default();
trie_nodes.insert_storage_updates(hashed_address, storage_trie_nodes.clone());
let (storage_root, _, trie_updates) =
StorageRoot::from_tx_hashed(provider.tx_ref(), hashed_address)
.with_prefix_set(changes.freeze())
.with_trie_cursor_factory(InMemoryTrieCursorFactory::new(
DatabaseTrieCursorFactory::new(provider.tx_ref()),
&trie_nodes.into_sorted(),
))
.root_with_updates()
.unwrap();

storage_trie_nodes.extend(trie_updates);

// Verify the result
storage.append(&mut storage_update);
let expected_root = storage_root_prehashed(storage.clone());
assert_eq!(expected_root, storage_root);
}
}
}
Loading