Make CallApiAt::call_api_at work at light client (#1213)

* light client contextual method call

* fixed off-by-one in test

* newlines + commas
This commit is contained in:
Svyatoslav Nikolsky
2018-12-10 16:02:00 +03:00
committed by GitHub
parent acf1b77bcd
commit 742cb33d90
7 changed files with 341 additions and 132 deletions
+54 -15
View File
@@ -61,8 +61,8 @@ pub use changes_trie::{
key_changes, key_changes_proof, key_changes_proof_check,
prune as prune_changes_tries};
pub use overlayed_changes::OverlayedChanges;
pub use proving_backend::create_proof_check_backend_storage;
pub use trie_backend_essence::Storage;
pub use proving_backend::{create_proof_check_backend, create_proof_check_backend_storage};
pub use trie_backend_essence::{TrieBackendStorage, Storage};
pub use trie_backend::TrieBackend;
/// Default num of pages for the heap
@@ -191,6 +191,7 @@ pub enum ExecutionStrategy {
}
/// Like `ExecutionStrategy` only it also stores a handler in case of consensus failure.
#[derive(Clone)]
pub enum ExecutionManager<F> {
/// Execute with the native equivalent if it is compatible with the given wasm module; otherwise fall back to the wasm.
NativeWhenPossible,
@@ -381,14 +382,6 @@ where
}
/// Prove execution using the given state backend, overlayed changes, and call executor.
/// Produces a state-backend-specific "transaction" which can be used to apply the changes
/// to the backing store, such as the disk.
/// Execution proof is the set of all 'touched' storage DBValues from the backend.
///
/// On an error, no prospective changes are written to the overlay.
///
/// Note: changes to code will be in place if this call is made again. For running partial
/// blocks (e.g. a transaction at a time), ensure a different method is used.
pub fn prove_execution<B, H, Exec>(
backend: B,
overlay: &mut OverlayedChanges,
@@ -404,7 +397,32 @@ where
{
let trie_backend = backend.try_into_trie_backend()
.ok_or_else(|| Box::new(ExecutionError::UnableToGenerateProof) as Box<Error>)?;
let proving_backend = proving_backend::ProvingBackend::new(&trie_backend);
prove_execution_on_trie_backend(&trie_backend, overlay, exec, method, call_data)
}
/// Prove execution using the given trie backend, overlayed changes, and call executor.
/// Produces a state-backend-specific "transaction" which can be used to apply the changes
/// to the backing store, such as the disk.
/// Execution proof is the set of all 'touched' storage DBValues from the backend.
///
/// On an error, no prospective changes are written to the overlay.
///
/// Note: changes to code will be in place if this call is made again. For running partial
/// blocks (e.g. a transaction at a time), ensure a different method is used.
pub fn prove_execution_on_trie_backend<S, H, Exec>(
trie_backend: &TrieBackend<S, H>,
overlay: &mut OverlayedChanges,
exec: &Exec,
method: &str,
call_data: &[u8],
) -> Result<(Vec<u8>, Vec<Vec<u8>>), Box<Error>>
where
S: trie_backend_essence::TrieBackendStorage<H>,
H: Hasher,
Exec: CodeExecutor<H>,
H::Out: Ord + HeapSizeOf,
{
let proving_backend = proving_backend::ProvingBackend::new(trie_backend);
let (result, _, _) = execute::<H, _, changes_trie::InMemoryStorage<H>, _>(
&proving_backend,
None,
@@ -432,9 +450,31 @@ where
Exec: CodeExecutor<H>,
H::Out: Ord + HeapSizeOf,
{
let backend = proving_backend::create_proof_check_backend::<H>(root.into(), proof)?;
execute::<H, _, changes_trie::InMemoryStorage<H>, _>(&backend, None, overlay, exec, method, call_data, ExecutionStrategy::NativeWhenPossible)
.map(|(result, _, _)| result)
let trie_backend = proving_backend::create_proof_check_backend::<H>(root.into(), proof)?;
execution_proof_check_on_trie_backend(&trie_backend, overlay, exec, method, call_data)
}
/// Check execution proof on proving backend, generated by `prove_execution` call.
pub fn execution_proof_check_on_trie_backend<H, Exec>(
trie_backend: &TrieBackend<MemoryDB<H>, H>,
overlay: &mut OverlayedChanges,
exec: &Exec,
method: &str,
call_data: &[u8],
) -> Result<Vec<u8>, Box<Error>>
where
H: Hasher,
Exec: CodeExecutor<H>,
H::Out: Ord + HeapSizeOf,
{
execute::<H, _, changes_trie::InMemoryStorage<H>, _>(
trie_backend,
None, overlay,
exec,
method,
call_data,
ExecutionStrategy::NativeWhenPossible
).map(|(result, _, _)| result)
}
/// Generate storage read proof.
@@ -447,7 +487,6 @@ where
H: Hasher,
H::Out: Ord + HeapSizeOf
{
let trie_backend = backend.try_into_trie_backend()
.ok_or_else(|| Box::new(ExecutionError::UnableToGenerateProof) as Box<Error>)?;
prove_read_on_trie_backend(&trie_backend, key)