use std::{panic::UnwindSafe, result, cell::RefCell};
use codec::{Encode, Decode};
use tp_runtime::{
generic::BlockId, traits::{Block as BlockT, HashFor},
};
use tp_state_machine::{
OverlayedChanges, ExecutionManager, ExecutionStrategy, StorageProof,
};
use tc_executor::{RuntimeVersion, NativeVersion};
use externalities::Extensions;
use tet_core::NativeOrEncoded;
use tp_api::{ProofRecorder, InitializeBlock, StorageTransactionCache};
use crate::execution_extensions::ExecutionExtensions;
pub trait ExecutorProvider<Block: BlockT> {
type Executor: CallExecutor<Block>;
fn executor(&self) -> &Self::Executor;
fn execution_extensions(&self) -> &ExecutionExtensions<Block>;
}
pub trait CallExecutor<B: BlockT> {
type Error: tp_state_machine::Error;
type Backend: crate::backend::Backend<B>;
fn call(
&self,
id: &BlockId<B>,
method: &str,
call_data: &[u8],
strategy: ExecutionStrategy,
extensions: Option<Extensions>,
) -> Result<Vec<u8>, tp_blockchain::Error>;
fn contextual_call<
'a,
IB: Fn() -> tp_blockchain::Result<()>,
EM: Fn(
Result<NativeOrEncoded<R>, Self::Error>,
Result<NativeOrEncoded<R>, Self::Error>
) -> Result<NativeOrEncoded<R>, Self::Error>,
R: Encode + Decode + PartialEq,
NC: FnOnce() -> result::Result<R, String> + UnwindSafe,
>(
&self,
initialize_block_fn: IB,
at: &BlockId<B>,
method: &str,
call_data: &[u8],
changes: &RefCell<OverlayedChanges>,
storage_transaction_cache: Option<&RefCell<
StorageTransactionCache<B, <Self::Backend as crate::backend::Backend<B>>::State>,
>>,
initialize_block: InitializeBlock<'a, B>,
execution_manager: ExecutionManager<EM>,
native_call: Option<NC>,
proof_recorder: &Option<ProofRecorder<B>>,
extensions: Option<Extensions>,
) -> tp_blockchain::Result<NativeOrEncoded<R>> where ExecutionManager<EM>: Clone;
fn runtime_version(&self, id: &BlockId<B>) -> Result<RuntimeVersion, tp_blockchain::Error>;
fn prove_at_state<S: tp_state_machine::Backend<HashFor<B>>>(
&self,
mut state: S,
overlay: &mut OverlayedChanges,
method: &str,
call_data: &[u8]
) -> Result<(Vec<u8>, StorageProof), tp_blockchain::Error> {
let trie_state = state.as_trie_backend()
.ok_or_else(||
tp_blockchain::Error::from_state(Box::new(tp_state_machine::ExecutionError::UnableToGenerateProof) as Box<_>)
)?;
self.prove_at_trie_state(trie_state, overlay, method, call_data)
}
fn prove_at_trie_state<S: tp_state_machine::TrieBackendStorage<HashFor<B>>>(
&self,
trie_state: &tp_state_machine::TrieBackend<S, HashFor<B>>,
overlay: &mut OverlayedChanges,
method: &str,
call_data: &[u8]
) -> Result<(Vec<u8>, StorageProof), tp_blockchain::Error>;
fn native_runtime_version(&self) -> Option<&NativeVersion>;
}