-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
feat(trie): use branch node hash masks in sparse trie #13135
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
70bd801
feat(trie): collect branch node hash masks when calculating a proof
shekhirin 69d4d4e
feat(trie): collect branch node hash masks when calculating a proof
shekhirin b074c5e
Merge remote-tracking branch 'origin/alexey/trie-proofs-branch-node-h…
shekhirin b1424b1
use hash builder updates
shekhirin 2692787
use hash builder updates
shekhirin d5b9b3d
Merge remote-tracking branch 'origin/alexey/trie-proofs-branch-node-h…
shekhirin 63a6238
Revert "use hash builder updates"
shekhirin ee30277
fix tests
shekhirin 139edf4
check hash mask
shekhirin 6cf4140
do not use split
shekhirin 1374f44
introduce a flag
shekhirin b25005a
extend branch node hash masks
shekhirin bf0d3bf
Merge remote-tracking branch 'origin/alexey/trie-proofs-branch-node-h…
shekhirin 56314c0
add logs, remove error
shekhirin 77c0615
revert trie node iter changes
shekhirin 58ea42d
more logs
shekhirin aae8fe6
fix hash mask checking
shekhirin 01b0ad3
Merge remote-tracking branch 'origin/main' into alexey/proofs-branch-…
shekhirin 8fb259f
update comment
shekhirin 88d8c56
return reveal_account and reveal_storage_slot
shekhirin 2bb382c
add a panic comment
shekhirin 074ebfc
add a comment about hash mask
shekhirin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,12 +103,17 @@ impl<F: BlindedProviderFactory> SparseStateTrie<F> { | |
} | ||
|
||
/// Reveal unknown trie paths from provided leaf path and its proof for the account. | ||
/// | ||
/// Panics if trie updates retention is enabled. | ||
/// | ||
/// NOTE: This method does not extensively validate the proof. | ||
pub fn reveal_account( | ||
&mut self, | ||
account: B256, | ||
proof: impl IntoIterator<Item = (Nibbles, Bytes)>, | ||
) -> SparseStateTrieResult<()> { | ||
assert!(!self.retain_updates); | ||
|
||
if self.is_account_revealed(&account) { | ||
return Ok(()); | ||
} | ||
|
@@ -121,13 +126,14 @@ impl<F: BlindedProviderFactory> SparseStateTrie<F> { | |
let trie = self.state.reveal_root_with_provider( | ||
self.provider_factory.account_node_provider(), | ||
root_node, | ||
None, | ||
self.retain_updates, | ||
)?; | ||
|
||
// Reveal the remaining proof nodes. | ||
for (path, bytes) in proof { | ||
let node = TrieNode::decode(&mut &bytes[..])?; | ||
trie.reveal_node(path, node)?; | ||
trie.reveal_node(path, node, None)?; | ||
} | ||
|
||
// Mark leaf path as revealed. | ||
|
@@ -137,13 +143,18 @@ impl<F: BlindedProviderFactory> SparseStateTrie<F> { | |
} | ||
|
||
/// Reveal unknown trie paths from provided leaf path and its proof for the storage slot. | ||
/// | ||
/// Panics if trie updates retention is enabled. | ||
/// | ||
/// NOTE: This method does not extensively validate the proof. | ||
pub fn reveal_storage_slot( | ||
&mut self, | ||
account: B256, | ||
slot: B256, | ||
proof: impl IntoIterator<Item = (Nibbles, Bytes)>, | ||
) -> SparseStateTrieResult<()> { | ||
assert!(!self.retain_updates); | ||
|
||
if self.is_storage_slot_revealed(&account, &slot) { | ||
return Ok(()); | ||
} | ||
|
@@ -156,13 +167,14 @@ impl<F: BlindedProviderFactory> SparseStateTrie<F> { | |
let trie = self.storages.entry(account).or_default().reveal_root_with_provider( | ||
self.provider_factory.storage_node_provider(account), | ||
root_node, | ||
None, | ||
self.retain_updates, | ||
)?; | ||
|
||
// Reveal the remaining proof nodes. | ||
for (path, bytes) in proof { | ||
let node = TrieNode::decode(&mut &bytes[..])?; | ||
trie.reveal_node(path, node)?; | ||
trie.reveal_node(path, node, None)?; | ||
} | ||
|
||
// Mark leaf path as revealed. | ||
|
@@ -186,34 +198,48 @@ impl<F: BlindedProviderFactory> SparseStateTrie<F> { | |
let trie = self.state.reveal_root_with_provider( | ||
self.provider_factory.account_node_provider(), | ||
root_node, | ||
multiproof.branch_node_hash_masks.get(&Nibbles::default()).copied(), | ||
self.retain_updates, | ||
)?; | ||
|
||
// Reveal the remaining proof nodes. | ||
for (path, bytes) in account_nodes { | ||
let node = TrieNode::decode(&mut &bytes[..])?; | ||
trace!(target: "trie::sparse", ?path, ?node, "Revealing account node"); | ||
trie.reveal_node(path, node)?; | ||
let hash_mask = if let TrieNode::Branch(_) = node { | ||
multiproof.branch_node_hash_masks.get(&path).copied() | ||
} else { | ||
None | ||
}; | ||
|
||
trace!(target: "trie::sparse", ?path, ?node, ?hash_mask, "Revealing account node"); | ||
trie.reveal_node(path, node, hash_mask)?; | ||
} | ||
} | ||
|
||
for (account, storage_subtree) in multiproof.storages { | ||
let storage_subtree = storage_subtree.subtree.into_nodes_sorted(); | ||
let mut storage_nodes = storage_subtree.into_iter().peekable(); | ||
let subtree = storage_subtree.subtree.into_nodes_sorted(); | ||
let mut nodes = subtree.into_iter().peekable(); | ||
|
||
if let Some(root_node) = self.validate_root_node(&mut storage_nodes)? { | ||
if let Some(root_node) = self.validate_root_node(&mut nodes)? { | ||
// Reveal root node if it wasn't already. | ||
let trie = self.storages.entry(account).or_default().reveal_root_with_provider( | ||
self.provider_factory.storage_node_provider(account), | ||
root_node, | ||
storage_subtree.branch_node_hash_masks.get(&Nibbles::default()).copied(), | ||
self.retain_updates, | ||
)?; | ||
|
||
// Reveal the remaining proof nodes. | ||
for (path, bytes) in storage_nodes { | ||
for (path, bytes) in nodes { | ||
let node = TrieNode::decode(&mut &bytes[..])?; | ||
trace!(target: "trie::sparse", ?account, ?path, ?node, "Revealing storage node"); | ||
trie.reveal_node(path, node)?; | ||
let hash_mask = if let TrieNode::Branch(_) = node { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks like we could use suggestion for followup |
||
storage_subtree.branch_node_hash_masks.get(&path).copied() | ||
} else { | ||
None | ||
}; | ||
|
||
trace!(target: "trie::sparse", ?account, ?path, ?node, ?hash_mask, "Revealing storage node"); | ||
trie.reveal_node(path, node, hash_mask)?; | ||
} | ||
} | ||
} | ||
|
@@ -394,27 +420,27 @@ mod tests { | |
use rand::{rngs::StdRng, Rng, SeedableRng}; | ||
use reth_primitives_traits::Account; | ||
use reth_trie::{updates::StorageTrieUpdates, HashBuilder, TrieAccount, EMPTY_ROOT_HASH}; | ||
use reth_trie_common::proof::ProofRetainer; | ||
use reth_trie_common::{proof::ProofRetainer, StorageMultiProof, TrieMask}; | ||
|
||
#[test] | ||
fn validate_proof_first_node_not_root() { | ||
fn validate_root_node_first_node_not_root() { | ||
let sparse = SparseStateTrie::default(); | ||
let proof = [(Nibbles::from_nibbles([0x1]), Bytes::from([EMPTY_STRING_CODE]))]; | ||
assert_matches!( | ||
sparse.validate_root_node(&mut proof.into_iter().peekable()), | ||
sparse.validate_root_node(&mut proof.into_iter().peekable(),), | ||
Err(SparseStateTrieError::InvalidRootNode { .. }) | ||
); | ||
} | ||
|
||
#[test] | ||
fn validate_proof_invalid_proof_with_empty_root() { | ||
fn validate_root_node_invalid_proof_with_empty_root() { | ||
let sparse = SparseStateTrie::default(); | ||
let proof = [ | ||
(Nibbles::default(), Bytes::from([EMPTY_STRING_CODE])), | ||
(Nibbles::from_nibbles([0x1]), Bytes::new()), | ||
]; | ||
assert_matches!( | ||
sparse.validate_root_node(&mut proof.into_iter().peekable()), | ||
sparse.validate_root_node(&mut proof.into_iter().peekable(),), | ||
Err(SparseStateTrieError::InvalidRootNode { .. }) | ||
); | ||
} | ||
|
@@ -429,6 +455,7 @@ mod tests { | |
|
||
let mut sparse = SparseStateTrie::default(); | ||
assert_eq!(sparse.state, SparseTrie::Blind); | ||
|
||
sparse.reveal_account(Default::default(), proofs.into_inner()).unwrap(); | ||
assert_eq!(sparse.state, SparseTrie::revealed_empty()); | ||
} | ||
|
@@ -443,6 +470,7 @@ mod tests { | |
|
||
let mut sparse = SparseStateTrie::default(); | ||
assert!(sparse.storages.is_empty()); | ||
|
||
sparse | ||
.reveal_storage_slot(Default::default(), Default::default(), proofs.into_inner()) | ||
.unwrap(); | ||
|
@@ -477,13 +505,15 @@ mod tests { | |
slot_path_1.clone(), | ||
slot_path_2.clone(), | ||
])); | ||
storage_hash_builder.add_leaf(slot_path_1.clone(), &alloy_rlp::encode_fixed_size(&value_1)); | ||
storage_hash_builder.add_leaf(slot_path_2.clone(), &alloy_rlp::encode_fixed_size(&value_2)); | ||
storage_hash_builder.add_leaf(slot_path_1, &alloy_rlp::encode_fixed_size(&value_1)); | ||
storage_hash_builder.add_leaf(slot_path_2, &alloy_rlp::encode_fixed_size(&value_2)); | ||
|
||
let storage_root = storage_hash_builder.root(); | ||
let proof_nodes = storage_hash_builder.take_proof_nodes(); | ||
let storage_proof_1 = proof_nodes.matching_nodes_sorted(&slot_path_1); | ||
let storage_proof_2 = proof_nodes.matching_nodes_sorted(&slot_path_2); | ||
let storage_proof_nodes = storage_hash_builder.take_proof_nodes(); | ||
let storage_branch_node_hash_masks = HashMap::from_iter([ | ||
(Nibbles::default(), TrieMask::new(0b010)), | ||
(Nibbles::from_nibbles([0x1]), TrieMask::new(0b11)), | ||
]); | ||
|
||
let address_1 = b256!("1000000000000000000000000000000000000000000000000000000000000000"); | ||
let address_path_1 = Nibbles::unpack(address_1); | ||
|
@@ -504,16 +534,41 @@ mod tests { | |
|
||
let root = hash_builder.root(); | ||
let proof_nodes = hash_builder.take_proof_nodes(); | ||
let proof_1 = proof_nodes.matching_nodes_sorted(&address_path_1); | ||
let proof_2 = proof_nodes.matching_nodes_sorted(&address_path_2); | ||
|
||
let mut sparse = SparseStateTrie::default().with_updates(true); | ||
sparse.reveal_account(address_1, proof_1).unwrap(); | ||
sparse.reveal_account(address_2, proof_2).unwrap(); | ||
sparse.reveal_storage_slot(address_1, slot_1, storage_proof_1.clone()).unwrap(); | ||
sparse.reveal_storage_slot(address_1, slot_2, storage_proof_2.clone()).unwrap(); | ||
sparse.reveal_storage_slot(address_2, slot_1, storage_proof_1).unwrap(); | ||
sparse.reveal_storage_slot(address_2, slot_2, storage_proof_2).unwrap(); | ||
sparse | ||
.reveal_multiproof( | ||
HashMap::from_iter([ | ||
(address_1, HashSet::from_iter([slot_1, slot_2])), | ||
(address_2, HashSet::from_iter([slot_1, slot_2])), | ||
]), | ||
MultiProof { | ||
account_subtree: proof_nodes, | ||
branch_node_hash_masks: HashMap::from_iter([( | ||
Nibbles::from_nibbles([0x1]), | ||
TrieMask::new(0b00), | ||
)]), | ||
storages: HashMap::from_iter([ | ||
( | ||
address_1, | ||
StorageMultiProof { | ||
root, | ||
subtree: storage_proof_nodes.clone(), | ||
branch_node_hash_masks: storage_branch_node_hash_masks.clone(), | ||
}, | ||
), | ||
( | ||
address_2, | ||
StorageMultiProof { | ||
root, | ||
subtree: storage_proof_nodes, | ||
branch_node_hash_masks: storage_branch_node_hash_masks, | ||
}, | ||
), | ||
]), | ||
}, | ||
) | ||
.unwrap(); | ||
|
||
assert_eq!(sparse.root(), Some(root)); | ||
|
||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'd prefer to keep this methods, they are still useful. mb with
debug_assert!(!self.updates_enabled)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fair, useful at least for tests