mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 17:01:09 +00:00
Set StateBackend::Transaction to PrefixedMemoryDB (#14612)
* Yep * Try to get it working everywhere * Make `from_raw_storage` start with an empty db * More fixes! * Make everything compile * Fix `child_storage_root` * Fix after merge * Cleanups * Update primitives/state-machine/src/overlayed_changes/mod.rs Co-authored-by: Davide Galassi <davxy@datawok.net> * Review comments * Fix issues * Silence warning * FMT * Clippy --------- Co-authored-by: Davide Galassi <davxy@datawok.net>
This commit is contained in:
@@ -229,10 +229,7 @@ fn generate_runtime_api_base_structures() -> Result<TokenStream> {
|
||||
pub struct RuntimeApiImpl<Block: #crate_::BlockT, C: #crate_::CallApiAt<Block> + 'static> {
|
||||
call: &'static C,
|
||||
transaction_depth: std::cell::RefCell<u16>,
|
||||
changes: std::cell::RefCell<#crate_::OverlayedChanges>,
|
||||
storage_transaction_cache: std::cell::RefCell<
|
||||
#crate_::StorageTransactionCache<Block, C::StateBackend>
|
||||
>,
|
||||
changes: std::cell::RefCell<#crate_::OverlayedChanges<#crate_::HashingFor<Block>>>,
|
||||
recorder: std::option::Option<#crate_::ProofRecorder<Block>>,
|
||||
call_context: #crate_::CallContext,
|
||||
extensions: std::cell::RefCell<#crate_::Extensions>,
|
||||
@@ -242,8 +239,6 @@ fn generate_runtime_api_base_structures() -> Result<TokenStream> {
|
||||
impl<Block: #crate_::BlockT, C: #crate_::CallApiAt<Block>> #crate_::ApiExt<Block> for
|
||||
RuntimeApiImpl<Block, C>
|
||||
{
|
||||
type StateBackend = C::StateBackend;
|
||||
|
||||
fn execute_in_transaction<F: FnOnce(&Self) -> #crate_::TransactionOutcome<R>, R>(
|
||||
&self,
|
||||
call: F,
|
||||
@@ -305,22 +300,21 @@ fn generate_runtime_api_base_structures() -> Result<TokenStream> {
|
||||
})
|
||||
}
|
||||
|
||||
fn into_storage_changes(
|
||||
fn into_storage_changes<B: #crate_::StateBackend<#crate_::HashingFor<Block>>>(
|
||||
&self,
|
||||
backend: &Self::StateBackend,
|
||||
backend: &B,
|
||||
parent_hash: Block::Hash,
|
||||
) -> core::result::Result<
|
||||
#crate_::StorageChanges<C::StateBackend, Block>,
|
||||
#crate_::StorageChanges<Block>,
|
||||
String
|
||||
> where Self: Sized {
|
||||
let state_version = #crate_::CallApiAt::<Block>::runtime_version_at(self.call, std::clone::Clone::clone(&parent_hash))
|
||||
.map(|v| #crate_::RuntimeVersion::state_version(&v))
|
||||
.map_err(|e| format!("Failed to get state version: {}", e))?;
|
||||
|
||||
#crate_::OverlayedChanges::into_storage_changes(
|
||||
std::cell::RefCell::take(&self.changes),
|
||||
#crate_::OverlayedChanges::drain_storage_changes(
|
||||
&mut std::cell::RefCell::borrow_mut(&self.changes),
|
||||
backend,
|
||||
core::cell::RefCell::take(&self.storage_transaction_cache),
|
||||
state_version,
|
||||
)
|
||||
}
|
||||
@@ -349,7 +343,6 @@ fn generate_runtime_api_base_structures() -> Result<TokenStream> {
|
||||
transaction_depth: 0.into(),
|
||||
changes: std::default::Default::default(),
|
||||
recorder: std::default::Default::default(),
|
||||
storage_transaction_cache: std::default::Default::default(),
|
||||
call_context: #crate_::CallContext::Offchain,
|
||||
extensions: std::default::Default::default(),
|
||||
extensions_generated_for: std::default::Default::default(),
|
||||
@@ -535,7 +528,6 @@ impl<'a> ApiRuntimeImplToApiRuntimeApiImpl<'a> {
|
||||
function: (*fn_name)(version),
|
||||
arguments: params,
|
||||
overlayed_changes: &self.changes,
|
||||
storage_transaction_cache: &self.storage_transaction_cache,
|
||||
call_context: self.call_context,
|
||||
recorder: &self.recorder,
|
||||
extensions: &self.extensions,
|
||||
|
||||
@@ -66,8 +66,6 @@ fn implement_common_api_traits(block_type: TypePath, self_ty: Type) -> Result<To
|
||||
|
||||
Ok(quote!(
|
||||
impl #crate_::ApiExt<#block_type> for #self_ty {
|
||||
type StateBackend = #crate_::InMemoryBackend<#crate_::HashingFor<#block_type>>;
|
||||
|
||||
fn execute_in_transaction<F: FnOnce(&Self) -> #crate_::TransactionOutcome<R>, R>(
|
||||
&self,
|
||||
call: F,
|
||||
@@ -111,12 +109,12 @@ fn implement_common_api_traits(block_type: TypePath, self_ty: Type) -> Result<To
|
||||
unimplemented!("`proof_recorder` not implemented for runtime api mocks")
|
||||
}
|
||||
|
||||
fn into_storage_changes(
|
||||
fn into_storage_changes<B: #crate_::StateBackend<#crate_::HashingFor<#block_type>>>(
|
||||
&self,
|
||||
_: &Self::StateBackend,
|
||||
_: &B,
|
||||
_: <#block_type as #crate_::BlockT>::Hash,
|
||||
) -> std::result::Result<
|
||||
#crate_::StorageChanges<Self::StateBackend, #block_type>,
|
||||
#crate_::StorageChanges<#block_type>,
|
||||
String
|
||||
> where Self: Sized {
|
||||
unimplemented!("`into_storage_changes` not implemented for runtime api mocks")
|
||||
|
||||
@@ -456,28 +456,8 @@ pub use sp_api_proc_macro::mock_impl_runtime_apis;
|
||||
#[cfg(feature = "std")]
|
||||
pub type ProofRecorder<B> = sp_trie::recorder::Recorder<HashingFor<B>>;
|
||||
|
||||
/// A type that is used as cache for the storage transactions.
|
||||
#[cfg(feature = "std")]
|
||||
pub type StorageTransactionCache<Block, Backend> = sp_state_machine::StorageTransactionCache<
|
||||
<Backend as StateBackend<HashingFor<Block>>>::Transaction,
|
||||
HashingFor<Block>,
|
||||
>;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub type StorageChanges<SBackend, Block> = sp_state_machine::StorageChanges<
|
||||
<SBackend as StateBackend<HashingFor<Block>>>::Transaction,
|
||||
HashingFor<Block>,
|
||||
>;
|
||||
|
||||
/// Extract the state backend type for a type that implements `ProvideRuntimeApi`.
|
||||
#[cfg(feature = "std")]
|
||||
pub type StateBackendFor<P, Block> =
|
||||
<<P as ProvideRuntimeApi<Block>>::Api as ApiExt<Block>>::StateBackend;
|
||||
|
||||
/// Extract the state backend transaction type for a type that implements `ProvideRuntimeApi`.
|
||||
#[cfg(feature = "std")]
|
||||
pub type TransactionFor<P, Block> =
|
||||
<StateBackendFor<P, Block> as StateBackend<HashingFor<Block>>>::Transaction;
|
||||
pub type StorageChanges<Block> = sp_state_machine::StorageChanges<HashingFor<Block>>;
|
||||
|
||||
/// Something that can be constructed to a runtime api.
|
||||
#[cfg(feature = "std")]
|
||||
@@ -531,9 +511,6 @@ pub enum ApiError {
|
||||
/// Extends the runtime api implementation with some common functionality.
|
||||
#[cfg(feature = "std")]
|
||||
pub trait ApiExt<Block: BlockT> {
|
||||
/// The state backend that is used to store the block states.
|
||||
type StateBackend: StateBackend<HashingFor<Block>>;
|
||||
|
||||
/// Execute the given closure inside a new transaction.
|
||||
///
|
||||
/// Depending on the outcome of the closure, the transaction is committed or rolled-back.
|
||||
@@ -582,11 +559,11 @@ pub trait ApiExt<Block: BlockT> {
|
||||
/// api functions.
|
||||
///
|
||||
/// After executing this function, all collected changes are reset.
|
||||
fn into_storage_changes(
|
||||
fn into_storage_changes<B: StateBackend<HashingFor<Block>>>(
|
||||
&self,
|
||||
backend: &Self::StateBackend,
|
||||
backend: &B,
|
||||
parent_hash: Block::Hash,
|
||||
) -> Result<StorageChanges<Self::StateBackend, Block>, String>
|
||||
) -> Result<StorageChanges<Block>, String>
|
||||
where
|
||||
Self: Sized;
|
||||
|
||||
@@ -599,7 +576,7 @@ pub trait ApiExt<Block: BlockT> {
|
||||
|
||||
/// Parameters for [`CallApiAt::call_api_at`].
|
||||
#[cfg(feature = "std")]
|
||||
pub struct CallApiAtParams<'a, Block: BlockT, Backend: StateBackend<HashingFor<Block>>> {
|
||||
pub struct CallApiAtParams<'a, Block: BlockT> {
|
||||
/// The block id that determines the state that should be setup when calling the function.
|
||||
pub at: Block::Hash,
|
||||
/// The name of the function that should be called.
|
||||
@@ -607,9 +584,7 @@ pub struct CallApiAtParams<'a, Block: BlockT, Backend: StateBackend<HashingFor<B
|
||||
/// The encoded arguments of the function.
|
||||
pub arguments: Vec<u8>,
|
||||
/// The overlayed changes that are on top of the state.
|
||||
pub overlayed_changes: &'a RefCell<OverlayedChanges>,
|
||||
/// The cache for storage transactions.
|
||||
pub storage_transaction_cache: &'a RefCell<StorageTransactionCache<Block, Backend>>,
|
||||
pub overlayed_changes: &'a RefCell<OverlayedChanges<HashingFor<Block>>>,
|
||||
/// The call context of this call.
|
||||
pub call_context: CallContext,
|
||||
/// The optional proof recorder for recording storage accesses.
|
||||
@@ -626,10 +601,7 @@ pub trait CallApiAt<Block: BlockT> {
|
||||
|
||||
/// Calls the given api function with the given encoded arguments at the given block and returns
|
||||
/// the encoded result.
|
||||
fn call_api_at(
|
||||
&self,
|
||||
params: CallApiAtParams<Block, Self::StateBackend>,
|
||||
) -> Result<Vec<u8>, ApiError>;
|
||||
fn call_api_at(&self, params: CallApiAtParams<Block>) -> Result<Vec<u8>, ApiError>;
|
||||
|
||||
/// Returns the runtime version at the given block.
|
||||
fn runtime_version_at(&self, at_hash: Block::Hash) -> Result<RuntimeVersion, ApiError>;
|
||||
|
||||
Reference in New Issue
Block a user