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

Set StateBackend::Transaction to PrefixedMemoryDB #14612

Merged
merged 16 commits into from
Aug 17, 2023
58 changes: 7 additions & 51 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bin/node-template/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn new_partial(
FullClient,
FullBackend,
FullSelectChain,
sc_consensus::DefaultImportQueue<Block, FullClient>,
sc_consensus::DefaultImportQueue<Block>,
sc_transaction_pool::FullPool<Block, FullClient>,
(
sc_consensus_grandpa::GrandpaBlockImport<
Expand Down
2 changes: 1 addition & 1 deletion bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ pub fn new_partial(
FullClient,
FullBackend,
FullSelectChain,
sc_consensus::DefaultImportQueue<Block, FullClient>,
sc_consensus::DefaultImportQueue<Block>,
sc_transaction_pool::FullPool<Block, FullClient>,
(
impl Fn(
Expand Down
3 changes: 0 additions & 3 deletions bin/node/testing/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ pub type Client = client::Client<
kitchensink_runtime::RuntimeApi,
>;

/// Transaction for kitchensink-runtime.
pub type Transaction = sc_client_api::backend::TransactionFor<Backend, node_primitives::Block>;

/// Genesis configuration parameters for `TestClient`.
#[derive(Default)]
pub struct GenesisParameters;
Expand Down
10 changes: 2 additions & 8 deletions client/api/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,11 @@ use sp_storage::{ChildInfo, StorageData, StorageKey};

use crate::{blockchain::Backend as BlockchainBackend, UsageInfo};

pub use sp_state_machine::{Backend as StateBackend, KeyValueStates};
pub use sp_state_machine::{Backend as StateBackend, BackendTransaction, KeyValueStates};

/// Extracts the state backend type for the given backend.
pub type StateBackendFor<B, Block> = <B as Backend<Block>>::State;

/// Extracts the transaction for the given state backend.
pub type TransactionForSB<B, Block> = <B as StateBackend<HashFor<Block>>>::Transaction;

/// Extracts the transaction for the given backend.
pub type TransactionFor<B, Block> = TransactionForSB<StateBackendFor<B, Block>, Block>;

/// Describes which block import notification stream should be notified.
#[derive(Debug, Clone, Copy)]
pub enum ImportNotificationAction {
Expand Down Expand Up @@ -181,7 +175,7 @@ pub trait BlockImportOperation<Block: BlockT> {
/// Inject storage data into the database.
fn update_db_storage(
&mut self,
update: TransactionForSB<Self::State, Block>,
update: BackendTransaction<HashFor<Block>>,
) -> sp_blockchain::Result<()>;

/// Set genesis state. If `commit` is `false` the state is saved in memory, but is not written
Expand Down
9 changes: 2 additions & 7 deletions client/api/src/call_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use sp_state_machine::{OverlayedChanges, StorageProof};
use std::cell::RefCell;

use crate::execution_extensions::ExecutionExtensions;
use sp_api::{ProofRecorder, StorageTransactionCache};
use sp_api::{HashFor, ProofRecorder};

/// Executor Provider
pub trait ExecutorProvider<Block: BlockT> {
Expand Down Expand Up @@ -72,12 +72,7 @@ pub trait CallExecutor<B: BlockT>: RuntimeVersionOf {
at_hash: B::Hash,
method: &str,
call_data: &[u8],
changes: &RefCell<OverlayedChanges>,
storage_transaction_cache: Option<
&RefCell<
StorageTransactionCache<B, <Self::Backend as crate::backend::Backend<B>>::State>,
>,
>,
changes: &RefCell<OverlayedChanges<HashFor<B>>>,
proof_recorder: &Option<ProofRecorder<B>>,
call_context: CallContext,
extensions: &RefCell<Extensions>,
Expand Down
9 changes: 4 additions & 5 deletions client/api/src/in_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ use sp_runtime::{
Justification, Justifications, StateVersion, Storage,
};
use sp_state_machine::{
Backend as StateBackend, ChildStorageCollection, InMemoryBackend, IndexOperation,
StorageCollection,
Backend as StateBackend, BackendTransaction, ChildStorageCollection, InMemoryBackend,
IndexOperation, StorageCollection,
};
use std::{
collections::{HashMap, HashSet},
Expand Down Expand Up @@ -480,8 +480,7 @@ impl<Block: BlockT> backend::AuxStore for Blockchain<Block> {
pub struct BlockImportOperation<Block: BlockT> {
pending_block: Option<PendingBlock<Block>>,
old_state: InMemoryBackend<HashFor<Block>>,
new_state:
Option<<InMemoryBackend<HashFor<Block>> as StateBackend<HashFor<Block>>>::Transaction>,
new_state: Option<BackendTransaction<HashFor<Block>>>,
aux: Vec<(Vec<u8>, Option<Vec<u8>>)>,
finalized_blocks: Vec<(Block::Hash, Option<Justification>)>,
set_head: Option<Block::Hash>,
Expand Down Expand Up @@ -539,7 +538,7 @@ impl<Block: BlockT> backend::BlockImportOperation<Block> for BlockImportOperatio

fn update_db_storage(
&mut self,
update: <InMemoryBackend<HashFor<Block>> as StateBackend<HashFor<Block>>>::Transaction,
update: BackendTransaction<HashFor<Block>>,
) -> sp_blockchain::Result<()> {
self.new_state = Some(update);
Ok(())
Expand Down
24 changes: 7 additions & 17 deletions client/basic-authorship/src/basic_authorship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ where
+ Send
+ Sync
+ 'static,
C::Api:
ApiExt<Block, StateBackend = backend::StateBackendFor<B, Block>> + BlockBuilderApi<Block>,
C::Api: ApiExt<Block> + BlockBuilderApi<Block>,
{
fn init_with_now(
&mut self,
Expand Down Expand Up @@ -229,8 +228,7 @@ where
+ Send
+ Sync
+ 'static,
C::Api:
ApiExt<Block, StateBackend = backend::StateBackendFor<B, Block>> + BlockBuilderApi<Block>,
C::Api: ApiExt<Block> + BlockBuilderApi<Block>,
PR: ProofRecording,
{
type CreateProposer = future::Ready<Result<Self::Proposer, Self::Error>>;
Expand Down Expand Up @@ -269,17 +267,11 @@ where
+ Send
+ Sync
+ 'static,
C::Api:
ApiExt<Block, StateBackend = backend::StateBackendFor<B, Block>> + BlockBuilderApi<Block>,
C::Api: ApiExt<Block> + BlockBuilderApi<Block>,
PR: ProofRecording,
{
type Transaction = backend::TransactionFor<B, Block>;
type Proposal = Pin<
Box<
dyn Future<Output = Result<Proposal<Block, Self::Transaction, PR::Proof>, Self::Error>>
+ Send,
>,
>;
type Proposal =
Pin<Box<dyn Future<Output = Result<Proposal<Block, PR::Proof>, Self::Error>> + Send>>;
type Error = sp_blockchain::Error;
type ProofRecording = PR;
type Proof = PR::Proof;
Expand Down Expand Up @@ -329,8 +321,7 @@ where
+ Send
+ Sync
+ 'static,
C::Api:
ApiExt<Block, StateBackend = backend::StateBackendFor<B, Block>> + BlockBuilderApi<Block>,
C::Api: ApiExt<Block> + BlockBuilderApi<Block>,
PR: ProofRecording,
{
async fn propose_with(
Expand All @@ -339,8 +330,7 @@ where
inherent_digests: Digest,
deadline: time::Instant,
block_size_limit: Option<usize>,
) -> Result<Proposal<Block, backend::TransactionFor<B, Block>, PR::Proof>, sp_blockchain::Error>
{
) -> Result<Proposal<Block, PR::Proof>, sp_blockchain::Error> {
let propose_with_timer = time::Instant::now();
let mut block_builder =
self.client.new_block_at(self.parent_hash, inherent_digests, PR::ENABLED)?;
Expand Down
15 changes: 6 additions & 9 deletions client/block-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,18 @@ impl From<bool> for RecordProof {
/// backend to get the state of the block. Furthermore an optional `proof` is included which
/// can be used to proof that the build block contains the expected data. The `proof` will
/// only be set when proof recording was activated.
pub struct BuiltBlock<Block: BlockT, StateBackend: backend::StateBackend<HashFor<Block>>> {
pub struct BuiltBlock<Block: BlockT> {
/// The actual block that was build.
pub block: Block,
/// The changes that need to be applied to the backend to get the state of the build block.
pub storage_changes: StorageChanges<StateBackend, Block>,
pub storage_changes: StorageChanges<Block>,
/// An optional proof that was recorded while building the block.
pub proof: Option<StorageProof>,
}

impl<Block: BlockT, StateBackend: backend::StateBackend<HashFor<Block>>>
BuiltBlock<Block, StateBackend>
{
impl<Block: BlockT> BuiltBlock<Block> {
/// Convert into the inner values.
pub fn into_inner(self) -> (Block, StorageChanges<StateBackend, Block>, Option<StorageProof>) {
pub fn into_inner(self) -> (Block, StorageChanges<Block>, Option<StorageProof>) {
(self.block, self.storage_changes, self.proof)
}
}
Expand Down Expand Up @@ -145,8 +143,7 @@ impl<'a, Block, A, B> BlockBuilder<'a, Block, A, B>
where
Block: BlockT,
A: ProvideRuntimeApi<Block> + 'a,
A::Api:
BlockBuilderApi<Block> + ApiExt<Block, StateBackend = backend::StateBackendFor<B, Block>>,
A::Api: BlockBuilderApi<Block> + ApiExt<Block>,
B: backend::Backend<Block>,
{
/// Create a new instance of builder based on the given `parent_hash` and `parent_number`.
Expand Down Expand Up @@ -231,7 +228,7 @@ where
/// Returns the build `Block`, the changes to the storage and an optional `StorageProof`
/// supplied by `self.api`, combined as [`BuiltBlock`].
/// The storage proof will be `Some(_)` when proof recording was enabled.
pub fn build(mut self) -> Result<BuiltBlock<Block, backend::StateBackendFor<B, Block>>, Error> {
pub fn build(mut self) -> Result<BuiltBlock<Block>, Error> {
let header = self.api.finalize_block(self.parent_hash)?;

debug_assert_eq!(
Expand Down
11 changes: 4 additions & 7 deletions client/consensus/aura/src/import_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ where
{
async fn verify(
&mut self,
mut block: BlockImportParams<B, ()>,
) -> Result<BlockImportParams<B, ()>, String> {
mut block: BlockImportParams<B>,
) -> Result<BlockImportParams<B>, String> {
// Skip checks that include execution, if being told so or when importing only state.
//
// This is done for example when gap syncing and it is expected that the block after the gap
Expand Down Expand Up @@ -349,7 +349,7 @@ pub fn import_queue<P, Block, I, C, S, CIDP>(
telemetry,
compatibility_mode,
}: ImportQueueParams<Block, I, C, S, CIDP>,
) -> Result<DefaultImportQueue<Block, C>, sp_consensus::Error>
) -> Result<DefaultImportQueue<Block>, sp_consensus::Error>
where
Block: BlockT,
C::Api: BlockBuilderApi<Block> + AuraApi<Block, AuthorityId<P>> + ApiExt<Block>,
Expand All @@ -361,10 +361,7 @@ where
+ AuxStore
+ UsageProvider<Block>
+ HeaderBackend<Block>,
I: BlockImport<Block, Error = ConsensusError, Transaction = sp_api::TransactionFor<C, Block>>
+ Send
+ Sync
+ 'static,
I: BlockImport<Block, Error = ConsensusError> + Send + Sync + 'static,
P: Pair + Send + Sync + 'static,
P::Public: Clone + Eq + Send + Sync + Hash + Debug + Encode + Decode,
P::Signature: Encode + Decode,
Expand Down
Loading