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:
Bastian Köcher
2020-01-10 10:48:32 +01:00
committed by GitHub
parent 74d6e660c6
commit fd6b29dd2c
140 changed files with 4860 additions and 3339 deletions
+193 -105
View File
@@ -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(|_| ())
}
}