fix(witness): branch node children decoding (#11599)

This commit is contained in:
Roman Krasiuk
2024-10-16 19:21:25 +02:00
committed by GitHub
parent 6b2ec42e48
commit 12cab204b5
2 changed files with 68 additions and 6 deletions

View File

@ -6,6 +6,8 @@ use alloy_primitives::{
Address, Bytes, B256, U256,
};
use alloy_rlp::EMPTY_STRING_CODE;
use reth_db::{cursor::DbCursorRW, tables};
use reth_db_api::transaction::DbTxMut;
use reth_primitives::{constants::EMPTY_ROOT_HASH, Account, StorageEntry};
use reth_provider::{test_utils::create_test_provider_factory, HashingWriter};
use reth_trie::{proof::Proof, witness::TrieWitness, HashedPostState, HashedStorage, StateRoot};
@ -91,3 +93,53 @@ fn includes_nodes_for_destroyed_storage_nodes() {
assert_eq!(witness.get(&keccak256(node)), Some(node));
}
}
#[test]
fn correctly_decodes_branch_node_values() {
let factory = create_test_provider_factory();
let provider = factory.provider_rw().unwrap();
let address = Address::random();
let hashed_address = keccak256(address);
let hashed_slot1 = B256::with_last_byte(1);
let hashed_slot2 = B256::with_last_byte(2);
// Insert account and slots into database
provider.insert_account_for_hashing([(address, Some(Account::default()))]).unwrap();
let mut hashed_storage_cursor =
provider.tx_ref().cursor_dup_write::<tables::HashedStorages>().unwrap();
hashed_storage_cursor
.upsert(hashed_address, StorageEntry { key: hashed_slot1, value: U256::from(1) })
.unwrap();
hashed_storage_cursor
.upsert(hashed_address, StorageEntry { key: hashed_slot2, value: U256::from(1) })
.unwrap();
let state_root = StateRoot::from_tx(provider.tx_ref()).root().unwrap();
let multiproof = Proof::from_tx(provider.tx_ref())
.multiproof(HashMap::from_iter([(
hashed_address,
HashSet::from_iter([hashed_slot1, hashed_slot2]),
)]))
.unwrap();
let witness = TrieWitness::from_tx(provider.tx_ref())
.compute(HashedPostState {
accounts: HashMap::from([(hashed_address, Some(Account::default()))]),
storages: HashMap::from([(
hashed_address,
HashedStorage::from_iter(
false,
[hashed_slot1, hashed_slot2].map(|hashed_slot| (hashed_slot, U256::from(2))),
),
)]),
})
.unwrap();
assert!(witness.contains_key(&state_root));
for node in multiproof.account_subtree.values() {
assert_eq!(witness.get(&keccak256(node)), Some(node));
}
for node in multiproof.storages.iter().flat_map(|(_, storage)| storage.subtree.values()) {
assert_eq!(witness.get(&keccak256(node)), Some(node));
}
}