mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 20:27:58 +00:00
Remove requirement on Hash = H256, make Proposer return StorageChanges and Proof (#3860)
* Extend `Proposer` to optionally generate a proof of the proposal * Something * Refactor sr-api to not depend on client anymore * Fix benches * Apply suggestions from code review Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * Apply suggestions from code review * Introduce new `into_storage_changes` function * Switch to runtime api for `execute_block` and don't require `H256` anywhere in the code * Put the `StorageChanges` into the `Proposal` * Move the runtime api error to its own trait * Adds `StorageTransactionCache` to the runtime api This requires that we add `type NodeBlock = ` to the `impl_runtime_apis!` macro to work around some bugs in rustc :( * Remove `type NodeBlock` and switch to a "better" hack * Start using the transaction cache from the runtime api * Make it compile * Move `InMemory` to its own file * Make all tests work again * Return block, storage_changes and proof from Blockbuilder::bake() * Make sure that we use/set `storage_changes` when possible * Add test * Fix deadlock * Remove accidentally added folders * Introduce `RecordProof` as argument type to be more explicit * Update client/src/client.rs Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * Update primitives/state-machine/src/ext.rs Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * Integrates review feedback * Remove `unsafe` usage * Update client/block-builder/src/lib.rs Co-Authored-By: Benjamin Kampmann <ben@gnunicorn.org> * Update client/src/call_executor.rs * Bump versions Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com> Co-authored-by: Benjamin Kampmann <ben.kampmann@googlemail.com>
This commit is contained in:
@@ -22,35 +22,13 @@ use sp_consensus::{
|
||||
BlockImportParams, BlockImport, BlockOrigin, Error as ConsensusError,
|
||||
ForkChoiceStrategy,
|
||||
};
|
||||
use hash_db::Hasher;
|
||||
use sp_runtime::Justification;
|
||||
use sp_runtime::traits::{Block as BlockT};
|
||||
use sp_runtime::generic::BlockId;
|
||||
use sp_core::Blake2Hasher;
|
||||
use codec::alloc::collections::hash_map::HashMap;
|
||||
|
||||
/// Extension trait for a test client.
|
||||
pub trait ClientExt<Block: BlockT>: Sized {
|
||||
/// Import block to the chain. No finality.
|
||||
fn import(&self, origin: BlockOrigin, block: Block)
|
||||
-> Result<(), ConsensusError>;
|
||||
|
||||
/// Import a block and make it our best block if possible.
|
||||
fn import_as_best(&self, origin: BlockOrigin, block: Block)
|
||||
-> Result<(), ConsensusError>;
|
||||
|
||||
/// Import a block and finalize it.
|
||||
fn import_as_final(&self, origin: BlockOrigin, block: Block)
|
||||
-> Result<(), ConsensusError>;
|
||||
|
||||
/// Import block with justification, finalizes block.
|
||||
fn import_justified(
|
||||
&self,
|
||||
origin: BlockOrigin,
|
||||
block: Block,
|
||||
justification: Justification
|
||||
) -> Result<(), ConsensusError>;
|
||||
|
||||
/// Finalize a block.
|
||||
fn finalize_block(
|
||||
&self,
|
||||
@@ -62,96 +40,34 @@ pub trait ClientExt<Block: BlockT>: Sized {
|
||||
fn genesis_hash(&self) -> <Block as BlockT>::Hash;
|
||||
}
|
||||
|
||||
impl<B, E, RA, Block> ClientExt<Block> for Client<B, E, Block, RA>
|
||||
where
|
||||
B: sc_client_api::backend::Backend<Block, Blake2Hasher>,
|
||||
E: sc_client::CallExecutor<Block, Blake2Hasher>,
|
||||
for<'r> &'r Self: BlockImport<Block, Error=ConsensusError>,
|
||||
Block: BlockT<Hash=<Blake2Hasher as Hasher>::Out>,
|
||||
{
|
||||
fn import(&self, origin: BlockOrigin, block: Block)
|
||||
-> Result<(), ConsensusError>
|
||||
{
|
||||
let (header, extrinsics) = block.deconstruct();
|
||||
let import = BlockImportParams {
|
||||
origin,
|
||||
header,
|
||||
justification: None,
|
||||
post_digests: vec![],
|
||||
body: Some(extrinsics),
|
||||
finalized: false,
|
||||
auxiliary: Vec::new(),
|
||||
fork_choice: ForkChoiceStrategy::LongestChain,
|
||||
allow_missing_state: false,
|
||||
import_existing: false,
|
||||
};
|
||||
/// Extension trait for a test client around block importing.
|
||||
pub trait ClientBlockImportExt<Block: BlockT>: Sized {
|
||||
/// Import block to the chain. No finality.
|
||||
fn import(&mut self, origin: BlockOrigin, block: Block) -> Result<(), ConsensusError>;
|
||||
|
||||
BlockImport::import_block(&mut (&*self), import, HashMap::new()).map(|_| ())
|
||||
}
|
||||
/// Import a block and make it our best block if possible.
|
||||
fn import_as_best(&mut self, origin: BlockOrigin, block: Block) -> Result<(), ConsensusError>;
|
||||
|
||||
fn import_as_best(&self, origin: BlockOrigin, block: Block)
|
||||
-> Result<(), ConsensusError>
|
||||
{
|
||||
let (header, extrinsics) = block.deconstruct();
|
||||
let import = BlockImportParams {
|
||||
origin,
|
||||
header,
|
||||
justification: None,
|
||||
post_digests: vec![],
|
||||
body: Some(extrinsics),
|
||||
finalized: false,
|
||||
auxiliary: Vec::new(),
|
||||
fork_choice: ForkChoiceStrategy::Custom(true),
|
||||
allow_missing_state: false,
|
||||
import_existing: false,
|
||||
};
|
||||
|
||||
BlockImport::import_block(&mut (&*self), import, HashMap::new()).map(|_| ())
|
||||
}
|
||||
|
||||
fn import_as_final(&self, origin: BlockOrigin, block: Block)
|
||||
-> Result<(), ConsensusError>
|
||||
{
|
||||
let (header, extrinsics) = block.deconstruct();
|
||||
let import = BlockImportParams {
|
||||
origin,
|
||||
header,
|
||||
justification: None,
|
||||
post_digests: vec![],
|
||||
body: Some(extrinsics),
|
||||
finalized: true,
|
||||
auxiliary: Vec::new(),
|
||||
fork_choice: ForkChoiceStrategy::Custom(true),
|
||||
allow_missing_state: false,
|
||||
import_existing: false,
|
||||
};
|
||||
|
||||
BlockImport::import_block(&mut (&*self), import, HashMap::new()).map(|_| ())
|
||||
}
|
||||
/// Import a block and finalize it.
|
||||
fn import_as_final(&mut self, origin: BlockOrigin, block: Block)
|
||||
-> Result<(), ConsensusError>;
|
||||
|
||||
/// Import block with justification, finalizes block.
|
||||
fn import_justified(
|
||||
&self,
|
||||
&mut self,
|
||||
origin: BlockOrigin,
|
||||
block: Block,
|
||||
justification: Justification,
|
||||
) -> Result<(), ConsensusError> {
|
||||
let (header, extrinsics) = block.deconstruct();
|
||||
let import = BlockImportParams {
|
||||
origin,
|
||||
header,
|
||||
justification: Some(justification),
|
||||
post_digests: vec![],
|
||||
body: Some(extrinsics),
|
||||
finalized: true,
|
||||
auxiliary: Vec::new(),
|
||||
fork_choice: ForkChoiceStrategy::LongestChain,
|
||||
allow_missing_state: false,
|
||||
import_existing: false,
|
||||
};
|
||||
|
||||
BlockImport::import_block(&mut (&*self), import, HashMap::new()).map(|_| ())
|
||||
}
|
||||
justification: Justification
|
||||
) -> Result<(), ConsensusError>;
|
||||
}
|
||||
|
||||
impl<B, E, RA, Block> ClientExt<Block> for Client<B, E, Block, RA>
|
||||
where
|
||||
B: sc_client_api::backend::Backend<Block>,
|
||||
E: sc_client::CallExecutor<Block>,
|
||||
Self: BlockImport<Block, Error = ConsensusError>,
|
||||
Block: BlockT,
|
||||
{
|
||||
fn finalize_block(
|
||||
&self,
|
||||
id: BlockId<Block>,
|
||||
@@ -164,3 +80,175 @@ impl<B, E, RA, Block> ClientExt<Block> for Client<B, E, Block, RA>
|
||||
self.block_hash(0.into()).unwrap().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
/// This implementation is required, because of the weird api requirements around `BlockImport`.
|
||||
impl<Block: BlockT, T, Transaction> ClientBlockImportExt<Block> for std::sync::Arc<T>
|
||||
where for<'r> &'r T: BlockImport<Block, Error = ConsensusError, Transaction = Transaction>
|
||||
{
|
||||
fn import(&mut self, origin: BlockOrigin, block: Block) -> Result<(), ConsensusError> {
|
||||
let (header, extrinsics) = block.deconstruct();
|
||||
let import = BlockImportParams {
|
||||
origin,
|
||||
header,
|
||||
justification: None,
|
||||
post_digests: vec![],
|
||||
body: Some(extrinsics),
|
||||
storage_changes: None,
|
||||
finalized: false,
|
||||
auxiliary: Vec::new(),
|
||||
fork_choice: ForkChoiceStrategy::LongestChain,
|
||||
allow_missing_state: false,
|
||||
import_existing: false,
|
||||
};
|
||||
|
||||
BlockImport::import_block(self, import, HashMap::new()).map(|_| ())
|
||||
}
|
||||
|
||||
fn import_as_best(&mut self, origin: BlockOrigin, block: Block) -> Result<(), ConsensusError> {
|
||||
let (header, extrinsics) = block.deconstruct();
|
||||
let import = BlockImportParams {
|
||||
origin,
|
||||
header,
|
||||
justification: None,
|
||||
post_digests: vec![],
|
||||
body: Some(extrinsics),
|
||||
storage_changes: None,
|
||||
finalized: false,
|
||||
auxiliary: Vec::new(),
|
||||
fork_choice: ForkChoiceStrategy::Custom(true),
|
||||
allow_missing_state: false,
|
||||
import_existing: false,
|
||||
};
|
||||
|
||||
BlockImport::import_block(self, import, HashMap::new()).map(|_| ())
|
||||
}
|
||||
|
||||
fn import_as_final(&mut self, origin: BlockOrigin, block: Block) -> Result<(), ConsensusError> {
|
||||
let (header, extrinsics) = block.deconstruct();
|
||||
let import = BlockImportParams {
|
||||
origin,
|
||||
header,
|
||||
justification: None,
|
||||
post_digests: vec![],
|
||||
body: Some(extrinsics),
|
||||
storage_changes: None,
|
||||
finalized: true,
|
||||
auxiliary: Vec::new(),
|
||||
fork_choice: ForkChoiceStrategy::Custom(true),
|
||||
allow_missing_state: false,
|
||||
import_existing: false,
|
||||
};
|
||||
|
||||
BlockImport::import_block(self, import, HashMap::new()).map(|_| ())
|
||||
}
|
||||
|
||||
fn import_justified(
|
||||
&mut self,
|
||||
origin: BlockOrigin,
|
||||
block: Block,
|
||||
justification: Justification,
|
||||
) -> Result<(), ConsensusError> {
|
||||
let (header, extrinsics) = block.deconstruct();
|
||||
let import = BlockImportParams {
|
||||
origin,
|
||||
header,
|
||||
justification: Some(justification),
|
||||
post_digests: vec![],
|
||||
body: Some(extrinsics),
|
||||
storage_changes: None,
|
||||
finalized: true,
|
||||
auxiliary: Vec::new(),
|
||||
fork_choice: ForkChoiceStrategy::LongestChain,
|
||||
allow_missing_state: false,
|
||||
import_existing: false,
|
||||
};
|
||||
|
||||
BlockImport::import_block(self, import, HashMap::new()).map(|_| ())
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, E, RA, Block: BlockT> ClientBlockImportExt<Block> for Client<B, E, Block, RA>
|
||||
where
|
||||
Self: BlockImport<Block, Error = ConsensusError>,
|
||||
{
|
||||
fn import(&mut self, origin: BlockOrigin, block: Block) -> Result<(), ConsensusError> {
|
||||
let (header, extrinsics) = block.deconstruct();
|
||||
let import = BlockImportParams {
|
||||
origin,
|
||||
header,
|
||||
justification: None,
|
||||
post_digests: vec![],
|
||||
body: Some(extrinsics),
|
||||
storage_changes: None,
|
||||
finalized: false,
|
||||
auxiliary: Vec::new(),
|
||||
fork_choice: ForkChoiceStrategy::LongestChain,
|
||||
allow_missing_state: false,
|
||||
import_existing: false,
|
||||
};
|
||||
|
||||
BlockImport::import_block(self, import, HashMap::new()).map(|_| ())
|
||||
}
|
||||
|
||||
fn import_as_best(&mut self, origin: BlockOrigin, block: Block) -> Result<(), ConsensusError> {
|
||||
let (header, extrinsics) = block.deconstruct();
|
||||
let import = BlockImportParams {
|
||||
origin,
|
||||
header,
|
||||
justification: None,
|
||||
post_digests: vec![],
|
||||
body: Some(extrinsics),
|
||||
storage_changes: None,
|
||||
finalized: false,
|
||||
auxiliary: Vec::new(),
|
||||
fork_choice: ForkChoiceStrategy::Custom(true),
|
||||
allow_missing_state: false,
|
||||
import_existing: false,
|
||||
};
|
||||
|
||||
BlockImport::import_block(self, import, HashMap::new()).map(|_| ())
|
||||
}
|
||||
|
||||
fn import_as_final(&mut self, origin: BlockOrigin, block: Block) -> Result<(), ConsensusError> {
|
||||
let (header, extrinsics) = block.deconstruct();
|
||||
let import = BlockImportParams {
|
||||
origin,
|
||||
header,
|
||||
justification: None,
|
||||
post_digests: vec![],
|
||||
body: Some(extrinsics),
|
||||
storage_changes: None,
|
||||
finalized: true,
|
||||
auxiliary: Vec::new(),
|
||||
fork_choice: ForkChoiceStrategy::Custom(true),
|
||||
allow_missing_state: false,
|
||||
import_existing: false,
|
||||
};
|
||||
|
||||
BlockImport::import_block(self, import, HashMap::new()).map(|_| ())
|
||||
}
|
||||
|
||||
fn import_justified(
|
||||
&mut self,
|
||||
origin: BlockOrigin,
|
||||
block: Block,
|
||||
justification: Justification,
|
||||
) -> Result<(), ConsensusError> {
|
||||
let (header, extrinsics) = block.deconstruct();
|
||||
let import = BlockImportParams {
|
||||
origin,
|
||||
header,
|
||||
justification: Some(justification),
|
||||
post_digests: vec![],
|
||||
body: Some(extrinsics),
|
||||
storage_changes: None,
|
||||
finalized: true,
|
||||
auxiliary: Vec::new(),
|
||||
fork_choice: ForkChoiceStrategy::LongestChain,
|
||||
allow_missing_state: false,
|
||||
import_existing: false,
|
||||
};
|
||||
|
||||
BlockImport::import_block(self, import, HashMap::new()).map(|_| ())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,11 +34,10 @@ pub use sp_core::{Blake2Hasher, traits::BareCryptoStorePtr};
|
||||
pub use sp_runtime::{Storage, StorageChild};
|
||||
pub use sp_state_machine::ExecutionStrategy;
|
||||
|
||||
pub use self::client_ext::ClientExt;
|
||||
pub use self::client_ext::{ClientExt, ClientBlockImportExt};
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::collections::HashMap;
|
||||
use hash_db::Hasher;
|
||||
use sp_core::storage::{well_known_keys, ChildInfo};
|
||||
use sp_runtime::traits::Block as BlockT;
|
||||
use sc_client::LocalCallExecutor;
|
||||
@@ -71,36 +70,20 @@ pub struct TestClientBuilder<Executor, Backend, G: GenesisInit> {
|
||||
keystore: Option<BareCryptoStorePtr>,
|
||||
}
|
||||
|
||||
impl<Block, Executor, G: GenesisInit> Default for TestClientBuilder<
|
||||
Executor,
|
||||
Backend<Block>,
|
||||
G,
|
||||
> where
|
||||
Block: BlockT<Hash=<Blake2Hasher as Hasher>::Out>,
|
||||
{
|
||||
impl<Block: BlockT, Executor, G: GenesisInit> Default
|
||||
for TestClientBuilder<Executor, Backend<Block>, G> {
|
||||
fn default() -> Self {
|
||||
Self::with_default_backend()
|
||||
}
|
||||
}
|
||||
|
||||
impl<Block, Executor, G: GenesisInit> TestClientBuilder<
|
||||
Executor,
|
||||
Backend<Block>,
|
||||
G,
|
||||
> where
|
||||
Block: BlockT<Hash=<Blake2Hasher as Hasher>::Out>,
|
||||
{
|
||||
impl<Block: BlockT, Executor, G: GenesisInit> TestClientBuilder<Executor, Backend<Block>, G> {
|
||||
/// Create new `TestClientBuilder` with default backend.
|
||||
pub fn with_default_backend() -> Self {
|
||||
let backend = Arc::new(Backend::new_test(std::u32::MAX, std::u64::MAX));
|
||||
Self::with_backend(backend)
|
||||
}
|
||||
|
||||
/// Give access to the underlying backend of these clients
|
||||
pub fn backend(&self) -> Arc<Backend<Block>> {
|
||||
self.backend.clone()
|
||||
}
|
||||
|
||||
/// Create new `TestClientBuilder` with default backend and pruning window size
|
||||
pub fn with_pruning_window(keep_blocks: u32) -> Self {
|
||||
let backend = Arc::new(Backend::new_test(keep_blocks, 0));
|
||||
@@ -132,6 +115,11 @@ impl<Executor, Backend, G: GenesisInit> TestClientBuilder<Executor, Backend, G>
|
||||
&mut self.genesis_init
|
||||
}
|
||||
|
||||
/// Give access to the underlying backend of these clients
|
||||
pub fn backend(&self) -> Arc<Backend> {
|
||||
self.backend.clone()
|
||||
}
|
||||
|
||||
/// Extend child storage
|
||||
pub fn add_child_storage(
|
||||
mut self,
|
||||
@@ -177,9 +165,9 @@ impl<Executor, Backend, G: GenesisInit> TestClientBuilder<Executor, Backend, G>
|
||||
>,
|
||||
sc_client::LongestChain<Backend, Block>,
|
||||
) where
|
||||
Executor: sc_client::CallExecutor<Block, Blake2Hasher>,
|
||||
Backend: sc_client_api::backend::Backend<Block, Blake2Hasher>,
|
||||
Block: BlockT<Hash=<Blake2Hasher as Hasher>::Out>,
|
||||
Executor: sc_client::CallExecutor<Block>,
|
||||
Backend: sc_client_api::backend::Backend<Block>,
|
||||
Block: BlockT,
|
||||
{
|
||||
|
||||
let storage = {
|
||||
@@ -237,8 +225,8 @@ impl<E, Backend, G: GenesisInit> TestClientBuilder<
|
||||
) where
|
||||
I: Into<Option<NativeExecutor<E>>>,
|
||||
E: sc_executor::NativeExecutionDispatch,
|
||||
Backend: sc_client_api::backend::Backend<Block, Blake2Hasher>,
|
||||
Block: BlockT<Hash=<Blake2Hasher as Hasher>::Out>,
|
||||
Backend: sc_client_api::backend::Backend<Block>,
|
||||
Block: BlockT,
|
||||
{
|
||||
let executor = executor.into().unwrap_or_else(||
|
||||
NativeExecutor::new(WasmExecutionMethod::Interpreted, None)
|
||||
|
||||
Reference in New Issue
Block a user