-
Notifications
You must be signed in to change notification settings - Fork 0
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
Get Proof implementation #2128
base: main
Are you sure you want to change the base?
Get Proof implementation #2128
Changes from all commits
f24b8f0
449d5f4
f4729a0
2bee946
90ade3d
8706408
c58b2c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,19 @@ use crate::{ | |
transaction::EvmGas, | ||
}; | ||
|
||
#[derive(Debug, Default, Clone, Serialize, Deserialize)] | ||
pub struct StorageProof { | ||
pub key: B256, | ||
pub value: Vec<u8>, | ||
pub proof: Vec<Vec<u8>>, | ||
} | ||
#[derive(Debug, Default, Clone, Serialize, Deserialize)] | ||
pub struct Proof { | ||
pub account: Account, | ||
pub account_proof: Vec<Vec<u8>>, | ||
pub storage_proofs: Vec<StorageProof>, | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
/// The state of the blockchain, consisting of: | ||
/// - state - a database of Map<Address, Map<key,value>> | ||
|
@@ -382,6 +395,43 @@ impl State { | |
&bincode::serialize(&account)?, | ||
)?) | ||
} | ||
|
||
pub fn get_proof(&self, address: Address, storage_keys: &[B256]) -> Result<Proof> { | ||
if !self.has_account(address)? { | ||
return Ok(Proof::default()); | ||
}; | ||
|
||
// get_proof() requires &mut so clone state and don't mutate the origin | ||
let mut state = self.clone(); | ||
state.root_hash()?; | ||
let account = state.get_account(address)?; | ||
|
||
let account_proof = state | ||
.accounts | ||
.get_proof(Self::account_key(address).as_slice())?; | ||
|
||
let mut storage_trie = state.get_account_trie(address)?; | ||
storage_trie.root_hash()?; | ||
|
||
let storage_proofs = { | ||
let mut storage_proofs = Vec::new(); | ||
for key in storage_keys { | ||
let key = Self::account_storage_key(address, *key); | ||
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. @DrZoltanFazekas Are we happy that this exposes our trie encoding as part of our external API (and thus makes it hard to ever change it)? I ask because I don't think we are encoding things into the trie the same way as Ethereum does and thus there's no guarantee that what we're doing is sensible. |
||
let Some(value) = storage_trie.get(key.as_slice())? else { | ||
continue; | ||
}; | ||
let proof = storage_trie.get_proof(key.as_slice())?; | ||
storage_proofs.push(StorageProof { proof, key, value }); | ||
} | ||
storage_proofs | ||
}; | ||
|
||
Ok(Proof { | ||
account, | ||
account_proof, | ||
storage_proofs, | ||
}) | ||
} | ||
} | ||
|
||
pub mod contract_addr { | ||
|
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.
Why not make this
&mut self
instead? You already make a clone of it in the API implementation (by calling.at_root
).