Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrasiuk committed Sep 27, 2024
1 parent 663455f commit a2ce83a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
10 changes: 8 additions & 2 deletions crates/trie/common/src/proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::{Nibbles, TrieAccount};
use alloy_primitives::{keccak256, Address, Bytes, B256, U256};
use alloy_rlp::{encode_fixed_size, Decodable};
use alloy_rlp::{encode_fixed_size, Decodable, EMPTY_STRING_CODE};
use alloy_trie::{
nodes::TrieNode,
proof::{verify_proof, ProofNodes, ProofVerificationError},
Expand Down Expand Up @@ -88,7 +88,13 @@ pub struct StorageMultiProof {

impl Default for StorageMultiProof {
fn default() -> Self {
Self { root: EMPTY_ROOT_HASH, subtree: Default::default() }
Self {
root: EMPTY_ROOT_HASH,
subtree: ProofNodes::from_iter([(
Nibbles::default(),
Bytes::from([EMPTY_STRING_CODE]),
)]),
}
}
}

Expand Down
20 changes: 13 additions & 7 deletions crates/trie/db/tests/witness.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use alloy_primitives::{keccak256, Address};
use alloy_rlp::EMPTY_STRING_CODE;
use reth_primitives::{constants::EMPTY_ROOT_HASH, keccak256, Account, Address, Bytes, B256, U256};
use reth_primitives::{constants::EMPTY_ROOT_HASH, Account, Bytes, B256, U256};
use reth_provider::{test_utils::create_test_provider_factory, HashingWriter};
use reth_trie::{proof::Proof, witness::TrieWitness, HashedPostState, HashedStorage, StateRoot};
use reth_trie_db::{DatabaseProof, DatabaseStateRoot, DatabaseTrieWitness};
Expand All @@ -10,16 +11,21 @@ fn includes_empty_node_preimage() {
let factory = create_test_provider_factory();
let provider = factory.provider_rw().unwrap();

let address = Address::random();
let hashed_address = keccak256(address);
let hashed_slot = B256::random();

// witness includes empty state trie root node
assert_eq!(
TrieWitness::from_tx(provider.tx_ref()).compute(HashedPostState::default()).unwrap(),
TrieWitness::from_tx(provider.tx_ref())
.compute(HashedPostState {
accounts: HashMap::from([(hashed_address, Some(Account::default()))]),
storages: HashMap::default(),
})
.unwrap(),
HashMap::from([(EMPTY_ROOT_HASH, Bytes::from([EMPTY_STRING_CODE]))])
);

let address = Address::random();
let hashed_address = keccak256(address);
let hashed_slot = B256::random();

// Insert account into database
provider.insert_account_for_hashing([(address, Some(Account::default()))]).unwrap();

Expand All @@ -34,7 +40,7 @@ fn includes_empty_node_preimage() {
accounts: HashMap::from([(hashed_address, Some(Account::default()))]),
storages: HashMap::from([(
hashed_address,
HashedStorage::from_iter(false, [(hashed_slot, U256::ZERO)]),
HashedStorage::from_iter(false, [(hashed_slot, U256::from(1))]),
)]),
})
.unwrap();
Expand Down
9 changes: 7 additions & 2 deletions crates/trie/trie/src/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ where
proof: impl IntoIterator<Item = (&'b Nibbles, &'b Bytes)>,
) -> Result<BTreeMap<Nibbles, Either<B256, Vec<u8>>>, TrieWitnessError> {
let mut trie_nodes = BTreeMap::default();
for (path, encoded) in proof {
let mut proof_iter = proof.into_iter().enumerate().peekable();
while let Some((idx, (path, encoded))) = proof_iter.next() {
// Record the node in witness.
self.witness.insert(keccak256(encoded.as_ref()), encoded.clone());

Expand All @@ -226,7 +227,11 @@ where
trie_nodes.insert(next_path.clone(), Either::Right(leaf.value.clone()));
}
}
TrieNode::EmptyRoot => return Err(TrieWitnessError::UnexpectedEmptyRoot(next_path)),
TrieNode::EmptyRoot => {
if idx != 0 || proof_iter.peek().is_some() {
return Err(TrieWitnessError::UnexpectedEmptyRoot(next_path))
}
}
};
}

Expand Down

0 comments on commit a2ce83a

Please sign in to comment.