forked from paradigmxyz/reth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(trie): account & storage proofs (paradigmxyz#5041)
- Loading branch information
Showing
12 changed files
with
557 additions
and
260 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
msrv = "1.70" | ||
ignore-interior-mutability = ["bytes::Bytes", "reth_primitives::trie::nibbles::Nibbles"] |
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
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use crate::{trie::Nibbles, Bytes}; | ||
use std::collections::BTreeMap; | ||
|
||
/// Proof retainer is used to store proofs during merkle trie construction. | ||
/// It is intended to be used within the [`HashBuilder`](crate::trie::HashBuilder). | ||
#[derive(Debug)] | ||
pub struct ProofRetainer { | ||
/// The nibbles of the target trie keys to retain proofs for. | ||
targets: Vec<Nibbles>, | ||
/// The map of retained proofs (RLP serialized trie nodes) | ||
/// with their corresponding key in the trie. | ||
proofs: BTreeMap<Nibbles, Bytes>, | ||
} | ||
|
||
impl ProofRetainer { | ||
/// Create new retainer with target nibbles. | ||
pub fn new(targets: Vec<Nibbles>) -> Self { | ||
Self { targets, proofs: Default::default() } | ||
} | ||
|
||
/// Returns `true` if the given prefix matches the retainer target. | ||
pub fn matches(&self, prefix: &Nibbles) -> bool { | ||
self.targets.iter().any(|target| target.starts_with(prefix)) | ||
} | ||
|
||
/// Returns all collected proofs. | ||
pub fn into_proofs(self) -> BTreeMap<Nibbles, Bytes> { | ||
self.proofs | ||
} | ||
|
||
/// Retain the proof if the key matches any of the targets. | ||
pub fn retain(&mut self, prefix: &Nibbles, proof: &[u8]) { | ||
if self.matches(prefix) { | ||
self.proofs.insert(prefix.clone(), Bytes::from(proof.to_vec())); | ||
} | ||
} | ||
} |
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
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
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use super::Nibbles; | ||
use crate::{keccak256, Account, Address, Bytes, B256, U256}; | ||
|
||
/// The merkle proof with the relevant account info. | ||
#[derive(PartialEq, Eq, Default, Debug)] | ||
pub struct AccountProof { | ||
/// The address associated with the account. | ||
pub address: Address, | ||
/// Account info. | ||
pub info: Option<Account>, | ||
/// Array of rlp-serialized merkle trie nodes which starting from the root node and | ||
/// following the path of the hashed address as key. | ||
pub proof: Vec<Bytes>, | ||
/// The storage trie root. | ||
pub storage_root: B256, | ||
/// Array of storage proofs as requested. | ||
pub storage_proofs: Vec<StorageProof>, | ||
} | ||
|
||
impl AccountProof { | ||
/// Create new account proof entity. | ||
pub fn new(address: Address) -> Self { | ||
Self { address, ..Default::default() } | ||
} | ||
|
||
/// Set account info, storage root and requested storage proofs. | ||
pub fn set_account( | ||
&mut self, | ||
info: Account, | ||
storage_root: B256, | ||
storage_proofs: Vec<StorageProof>, | ||
) { | ||
self.info = Some(info); | ||
self.storage_root = storage_root; | ||
self.storage_proofs = storage_proofs; | ||
} | ||
|
||
/// Set proof path. | ||
pub fn set_proof(&mut self, proof: Vec<Bytes>) { | ||
self.proof = proof; | ||
} | ||
} | ||
|
||
/// The merkle proof of the storage entry. | ||
#[derive(PartialEq, Eq, Default, Debug)] | ||
pub struct StorageProof { | ||
/// The raw storage key. | ||
pub key: B256, | ||
/// The hashed storage key nibbles. | ||
pub nibbles: Nibbles, | ||
/// The storage value. | ||
pub value: U256, | ||
/// Array of rlp-serialized merkle trie nodes which starting from the storage root node and | ||
/// following the path of the hashed storage slot as key. | ||
pub proof: Vec<Bytes>, | ||
} | ||
|
||
impl StorageProof { | ||
/// Create new storage proof from the storage slot. | ||
pub fn new(key: B256) -> Self { | ||
let nibbles = Nibbles::unpack(keccak256(key)); | ||
Self { key, nibbles, ..Default::default() } | ||
} | ||
|
||
/// Create new storage proof from the storage slot and its pre-hashed image. | ||
pub fn new_with_hashed(key: B256, hashed_key: B256) -> Self { | ||
Self { key, nibbles: Nibbles::unpack(hashed_key), ..Default::default() } | ||
} | ||
|
||
/// Create new storage proof from the storage slot and its pre-hashed image. | ||
pub fn new_with_nibbles(key: B256, nibbles: Nibbles) -> Self { | ||
Self { key, nibbles, ..Default::default() } | ||
} | ||
|
||
/// Set storage value. | ||
pub fn set_value(&mut self, value: U256) { | ||
self.value = value; | ||
} | ||
|
||
/// Set proof path. | ||
pub fn set_proof(&mut self, proof: Vec<Bytes>) { | ||
self.proof = proof; | ||
} | ||
} |
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
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
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
Oops, something went wrong.