Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Add verify_execution_proof to ProofProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchengxu committed Mar 6, 2022
1 parent 74824b2 commit 8ce691d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
14 changes: 14 additions & 0 deletions client/api/src/call_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,18 @@ pub trait CallExecutor<B: BlockT>: RuntimeVersionOf {
call_data: &[u8],
overlay: Option<sp_api::OverlayedChanges>,
) -> Result<(Vec<u8>, StorageProof), sp_blockchain::Error>;

/// Verify the execution of the given `method`.
///
/// No changes are made.
fn verify_execution<'a>(
&'a self,
root: sp_core::H256,
proof: StorageProof,
at: &'a BlockId<B>,
method: &'a str,
call_data: &'a [u8],
overlay: sp_api::OverlayedChanges,
runtime_code: Option<sp_core::traits::RuntimeCode<'a>>,
) -> Result<Vec<u8>, sp_blockchain::Error>;
}
12 changes: 12 additions & 0 deletions client/api/src/proof_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ pub trait ProofProvider<Block: BlockT> {
overlay: Option<sp_api::OverlayedChanges>,
) -> sp_blockchain::Result<(Vec<u8>, StorageProof)>;

/// Verify the execution proof produced by [`execution_proof`].
fn verify_execution_proof<'a>(
&'a self,
root: sp_core::H256,
proof: StorageProof,
id: &'a BlockId<Block>,
method: &'a str,
call_data: &'a [u8],
overlay: sp_api::OverlayedChanges,
runtime_code: Option<sp_core::traits::RuntimeCode<'a>>,
) -> sp_blockchain::Result<Vec<u8>>;

/// Given a `BlockId` iterate over all storage values starting at `start_keys`.
/// Last `start_keys` element contains last accessed key value.
/// With multiple `start_keys`, first `start_keys` element is
Expand Down
41 changes: 41 additions & 0 deletions client/service/src/client/call_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,47 @@ where
)
.map_err(Into::into)
}

fn verify_execution<'a>(
&self,
root: sp_core::H256,
proof: StorageProof,
at: &'a BlockId<Block>,
method: &'a str,
call_data: &'a [u8],
overlay: sp_api::OverlayedChanges,
maybe_runtime_code: Option<RuntimeCode<'a>>,
) -> sp_blockchain::Result<Vec<u8>> {
let state = self.backend.state_at(*at)?;

let trie_backend = state.as_trie_backend().ok_or_else(|| {
Box::new(sp_state_machine::ExecutionError::UnableToGenerateProof)
as Box<dyn sp_state_machine::Error>
})?;

let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(trie_backend);
let runtime_code = state_runtime_code.runtime_code().map_err(sp_blockchain::Error::RuntimeCode)?;
let runtime_code = self.check_override(runtime_code, at)?;

// TODO: unwrap_or_else and then fix the lifetime issue.
let runtime_code = match maybe_runtime_code {
Some(runtime_code) => runtime_code,
None => runtime_code
};

let mut overlay = overlay;
sp_state_machine::execution_proof_check::<sp_runtime::traits::BlakeTwo256, _, _>(
root,
proof,
&mut overlay,
&self.executor,
self.spawn_handle.clone(),
method,
call_data,
&runtime_code,
)
.map_err(Into::into)
}
}

impl<B, E, Block> RuntimeVersionOf for LocalCallExecutor<Block, B, E>
Expand Down
13 changes: 13 additions & 0 deletions client/service/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,19 @@ where
self.executor.prove_execution(id, method, call_data, overlay)
}

fn verify_execution_proof<'a>(
&self,
root: sp_core::H256,
proof: StorageProof,
id: &'a BlockId<Block>,
method: &'a str,
call_data: &'a [u8],
overlay: sp_api::OverlayedChanges,
runtime_code: Option<sp_core::traits::RuntimeCode<'a>>
) -> sp_blockchain::Result<Vec<u8>> {
self.executor.verify_execution(root, proof, id, method, call_data, overlay, runtime_code)
}

fn read_proof_collection(
&self,
id: &BlockId<Block>,
Expand Down

0 comments on commit 8ce691d

Please sign in to comment.