This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fetching storage proofs by light client #252
Merged
Merged
Changes from 1 commit
Commits
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -19,13 +19,15 @@ | |
| use std::sync::Arc; | ||
| use futures::IntoFuture; | ||
|
|
||
| use runtime_primitives::traits::{Block as BlockT}; | ||
| use state_machine::CodeExecutor; | ||
| use runtime_primitives::generic::BlockId; | ||
| use runtime_primitives::traits::{Block as BlockT, Header as HeaderT}; | ||
| use state_machine::{CodeExecutor, read_proof_check}; | ||
|
|
||
| use call_executor::CallResult; | ||
| use error::{Error as ClientError, Result as ClientResult}; | ||
| use error::{Error as ClientError, ErrorKind as ClientErrorKind, Result as ClientResult}; | ||
| use light::blockchain::{Blockchain, Storage as BlockchainStorage}; | ||
| use light::call_executor::check_execution_proof; | ||
| use blockchain::HeaderBackend as BlockchainHeaderBackend; | ||
|
|
||
| /// Remote call request. | ||
| #[derive(Clone, Debug, PartialEq, Eq, Hash)] | ||
|
|
@@ -38,20 +40,43 @@ pub struct RemoteCallRequest<Hash: ::std::fmt::Display> { | |
| pub call_data: Vec<u8>, | ||
| } | ||
|
|
||
| /// Remote storage read request. | ||
| #[derive(Clone, Debug, PartialEq, Eq, Hash)] | ||
| pub struct RemoteReadRequest<Hash: ::std::fmt::Display> { | ||
| /// Read at state of given block. | ||
| pub block: Hash, | ||
| /// Storage key to read. | ||
| pub key: Vec<u8>, | ||
| } | ||
|
|
||
| /// Light client data fetcher. Implementations of this trait must check if remote data | ||
| /// is correct (see FetchedDataChecker) and return already checked data. | ||
| pub trait Fetcher<Block: BlockT>: Send + Sync { | ||
| /// Remote storage read future. | ||
| type RemoteReadResult: IntoFuture<Item=Option<Vec<u8>>, Error=ClientError>; | ||
| /// Remote call result future. | ||
| type RemoteCallResult: IntoFuture<Item=CallResult, Error=ClientError>; | ||
|
|
||
| /// Fetch remote storage value. | ||
| fn remote_read(&self, request: RemoteReadRequest<Block::Hash>) -> Self::RemoteReadResult; | ||
| /// Fetch remote call result. | ||
| fn remote_call(&self, request: RemoteCallRequest<Block::Hash>) -> Self::RemoteCallResult; | ||
| } | ||
|
|
||
| /// Light client remote data checker. | ||
| pub trait FetchChecker<Block: BlockT>: Send + Sync { | ||
| /// Check remote storage read proof. | ||
| fn check_read_proof( | ||
| &self, | ||
| request: &RemoteReadRequest<Block::Hash>, | ||
| remote_proof: Vec<Vec<u8>> | ||
| ) -> ClientResult<Option<Vec<u8>>>; | ||
| /// Check remote method execution proof. | ||
| fn check_execution_proof(&self, request: &RemoteCallRequest<Block::Hash>, remote_proof: Vec<Vec<u8>>) -> ClientResult<CallResult>; | ||
| fn check_execution_proof( | ||
| &self, | ||
| request: &RemoteCallRequest<Block::Hash>, | ||
| remote_proof: Vec<Vec<u8>> | ||
| ) -> ClientResult<CallResult>; | ||
| } | ||
|
|
||
| /// Remote data checker. | ||
|
|
@@ -78,11 +103,27 @@ impl<S, E, F> LightDataChecker<S, E, F> { | |
| impl<S, E, F, Block> FetchChecker<Block> for LightDataChecker<S, E, F> | ||
| where | ||
| Block: BlockT, | ||
| Block::Hash: Into<[u8; 32]>, | ||
| S: BlockchainStorage<Block>, | ||
| E: CodeExecutor, | ||
| F: Fetcher<Block>, | ||
| { | ||
| fn check_execution_proof(&self, request: &RemoteCallRequest<Block::Hash>, remote_proof: Vec<Vec<u8>>) -> ClientResult<CallResult> { | ||
| fn check_read_proof( | ||
| &self, | ||
| request: &RemoteReadRequest<Block::Hash>, | ||
| remote_proof: Vec<Vec<u8>> | ||
| ) -> ClientResult<Option<Vec<u8>>> { | ||
| let local_header = self.blockchain.header(BlockId::Hash(request.block))?; | ||
|
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. since this happens well beyond the point where the storage request is actually issued, could the header be pruned if it takes a long time to serve the request or if the header was on the verge of being pruned when the request was issued? |
||
| let local_header = local_header.ok_or_else(|| ClientErrorKind::UnknownBlock(format!("{}", request.block)))?; | ||
| let local_state_root = *local_header.state_root(); | ||
| read_proof_check(local_state_root.into(), remote_proof, &request.key).map_err(Into::into) | ||
| } | ||
|
|
||
| fn check_execution_proof( | ||
| &self, | ||
| request: &RemoteCallRequest<Block::Hash>, | ||
| remote_proof: Vec<Vec<u8>> | ||
| ) -> ClientResult<CallResult> { | ||
| check_execution_proof(&*self.blockchain, &self.executor, request, remote_proof) | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
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.
Can we wrap this line?