-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Create opaque struct for StorageProof. #3834
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,10 +16,7 @@ | |
|
|
||
| //! Methods that light client could use to execute runtime calls. | ||
|
|
||
| use std::{ | ||
| collections::HashSet, sync::Arc, panic::UnwindSafe, result, | ||
| cell::RefCell, rc::Rc, | ||
| }; | ||
| use std::{sync::Arc, panic::UnwindSafe, result, cell::RefCell, rc::Rc}; | ||
|
|
||
| use codec::{Encode, Decode}; | ||
| use primitives::{ | ||
|
|
@@ -31,7 +28,8 @@ use sr_primitives::{ | |
| }; | ||
| use state_machine::{ | ||
| self, Backend as StateBackend, OverlayedChanges, ExecutionStrategy, create_proof_check_backend, | ||
| execution_proof_check_on_trie_backend, ExecutionManager, ChangesTrieTransaction, | ||
| execution_proof_check_on_trie_backend, ExecutionManager, ChangesTrieTransaction, StorageProof, | ||
| merge_storage_proofs, | ||
| }; | ||
| use hash_db::Hasher; | ||
|
|
||
|
|
@@ -179,7 +177,7 @@ impl<Block, B, Local> CallExecutor<Block, Blake2Hasher> for | |
| _changes: &mut OverlayedChanges, | ||
| _method: &str, | ||
| _call_data: &[u8] | ||
| ) -> ClientResult<(Vec<u8>, Vec<Vec<u8>>)> { | ||
| ) -> ClientResult<(Vec<u8>, StorageProof)> { | ||
| Err(ClientError::NotAvailableOnLightClient) | ||
| } | ||
|
|
||
|
|
@@ -198,7 +196,7 @@ pub fn prove_execution<Block, S, E>( | |
| executor: &E, | ||
| method: &str, | ||
| call_data: &[u8], | ||
| ) -> ClientResult<(Vec<u8>, Vec<Vec<u8>>)> | ||
| ) -> ClientResult<(Vec<u8>, StorageProof)> | ||
| where | ||
| Block: BlockT<Hash=H256>, | ||
| S: StateBackend<Blake2Hasher>, | ||
|
|
@@ -218,11 +216,7 @@ pub fn prove_execution<Block, S, E>( | |
|
|
||
| // execute method + record execution proof | ||
| let (result, exec_proof) = executor.prove_at_trie_state(&trie_state, &mut changes, method, call_data)?; | ||
| let total_proof = init_proof.into_iter() | ||
| .chain(exec_proof.into_iter()) | ||
| .collect::<HashSet<_>>() | ||
| .into_iter() | ||
| .collect(); | ||
| let total_proof = merge_storage_proofs(vec![init_proof, exec_proof]); | ||
|
Contributor
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. vec allocation can be avoided here, especially since merge_storage_proofs takes an iterator as input.
Contributor
Author
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. How exactly do you recommend creating this iterator? I think something like
Contributor
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. I do not have better solution than using 'chain', I know some people prefer using
Contributor
Author
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. This really doesn't seem like a good reason to sacrifice readability to me.
Contributor
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. Of course it is just an alloc among lot of others, I think crazy iter expression are rather common in the code base, but not abusing them is probably a good thing. Regarding the other comment where we grow memory, you also can use a different approach, I know some poeple like this kind of api, I never really use it but it may be good here. struct ProofMerger(Hashset<StorageProof>);
impl ProofMerger {
fn add(self, proof: StorageProof) {
self.0.extends(proof);
self
}
fn merge(self) -> StorageProof {
StorageProof { trie_nodes: self.0.into_iter().collect() }
}
}in this case would look like let total_proof = ProofMerger(init_proof)
.add(exec_proof)
.merge();
Contributor
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. Note that it is really only nitpicking, should not prevent merging this PR for me. |
||
|
|
||
| Ok((result, total_proof)) | ||
| } | ||
|
|
@@ -234,7 +228,7 @@ pub fn prove_execution<Block, S, E>( | |
| pub fn check_execution_proof<Header, E, H>( | ||
| executor: &E, | ||
| request: &RemoteCallRequest<Header>, | ||
| remote_proof: Vec<Vec<u8>>, | ||
| remote_proof: StorageProof, | ||
| ) -> ClientResult<Vec<u8>> | ||
| where | ||
| Header: HeaderT, | ||
|
|
@@ -258,7 +252,7 @@ pub fn check_execution_proof<Header, E, H>( | |
| fn check_execution_proof_with_make_header<Header, E, H, MakeNextHeader: Fn(&Header) -> Header>( | ||
| executor: &E, | ||
| request: &RemoteCallRequest<Header>, | ||
| remote_proof: Vec<Vec<u8>>, | ||
| remote_proof: StorageProof, | ||
| make_next_header: MakeNextHeader, | ||
| ) -> ClientResult<Vec<u8>> | ||
| where | ||
|
|
@@ -382,7 +376,7 @@ mod tests { | |
| _overlay: &mut OverlayedChanges, | ||
| _method: &str, | ||
| _call_data: &[u8] | ||
| ) -> Result<(Vec<u8>, Vec<Vec<u8>>), ClientError> { | ||
| ) -> Result<(Vec<u8>, StorageProof), ClientError> { | ||
| unreachable!() | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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 prefer the old version where we did not cat vecs: here we are eating memory. Maybe an iterator version of merge_storage_proofs will get the best of both world.
Actually merge_storage_proofs takes an iterator as input, so there is maybe something todo, does not seems super easy though since it neends to transform for_each_cht_group to an iterator.
Edit: may not be worth addressing.