ChainSpec extensions (#3692)

* Add some chainspec tests and make sure we validate it.

* Manual implementation of Extension + Forks definitions.

* Move chain spec to separate crate.

* Allow using ChainSpec with extensions.

* Renames.

* Implement Extension derive.

* Implement Extension for Forks.

* Support specifying fork blocks.

* make for_blocks work

* Support forks correctly.

* Add a bunch of docs.

* Make fork blocks optional.

* Add missing docs.

* Fix build.

* Use struct for check_block params.

* Fix tests?

* Clean up.
This commit is contained in:
Tomasz Drwięga
2019-09-28 19:05:36 +02:00
committed by Gavin Wood
parent c555b9bf88
commit 667ee95f5d
39 changed files with 1368 additions and 336 deletions
@@ -90,7 +90,17 @@ pub enum ForkChoiceStrategy {
Custom(bool),
}
/// Data required to import a Block
/// Data required to check validity of a Block.
pub struct BlockCheckParams<Block: BlockT> {
/// Hash of the block that we verify.
pub hash: Block::Hash,
/// Block number of the block that we verify.
pub number: NumberFor<Block>,
/// Parent hash of the block that we verify.
pub parent_hash: Block::Hash,
}
/// Data required to import a Block.
pub struct BlockImportParams<Block: BlockT> {
/// Origin of the Block
pub origin: BlockOrigin,
@@ -172,8 +182,7 @@ pub trait BlockImport<B: BlockT> {
/// Check block preconditions.
fn check_block(
&mut self,
hash: B::Hash,
parent_hash: B::Hash,
block: BlockCheckParams<B>,
) -> Result<ImportResult, Self::Error>;
/// Import a block.
@@ -192,10 +201,9 @@ impl<B: BlockT> BlockImport<B> for crate::import_queue::BoxBlockImport<B> {
/// Check block preconditions.
fn check_block(
&mut self,
hash: B::Hash,
parent_hash: B::Hash,
block: BlockCheckParams<B>,
) -> Result<ImportResult, Self::Error> {
(**self).check_block(hash, parent_hash)
(**self).check_block(block)
}
/// Import a block.
@@ -217,10 +225,9 @@ where for<'r> &'r T: BlockImport<B, Error = E>
fn check_block(
&mut self,
hash: B::Hash,
parent_hash: B::Hash,
block: BlockCheckParams<B>,
) -> Result<ImportResult, Self::Error> {
(&**self).check_block(hash, parent_hash)
(&**self).check_block(block)
}
fn import_block(
@@ -30,7 +30,7 @@ use sr_primitives::{Justification, traits::{Block as BlockT, Header as _, Number
use crate::error::Error as ConsensusError;
use crate::block_import::{
BlockImport, BlockOrigin, BlockImportParams, ImportedAux, JustificationImport, ImportResult,
FinalityProofImport,
BlockCheckParams, FinalityProofImport,
};
pub use basic_queue::BasicQueue;
@@ -194,7 +194,7 @@ pub fn import_single_block<B: BlockT, V: Verifier<B>>(
let number = header.number().clone();
let hash = header.hash();
let parent = header.parent_hash().clone();
let parent_hash = header.parent_hash().clone();
let import_error = |e| {
match e {
@@ -204,7 +204,7 @@ pub fn import_single_block<B: BlockT, V: Verifier<B>>(
},
Ok(ImportResult::Imported(aux)) => Ok(BlockImportResult::ImportedUnknown(number, aux, peer.clone())),
Ok(ImportResult::UnknownParent) => {
debug!(target: "sync", "Block with unknown parent {}: {:?}, parent: {:?}", number, hash, parent);
debug!(target: "sync", "Block with unknown parent {}: {:?}, parent: {:?}", number, hash, parent_hash);
Err(BlockImportError::UnknownParent)
},
Ok(ImportResult::KnownBad) => {
@@ -217,8 +217,7 @@ pub fn import_single_block<B: BlockT, V: Verifier<B>>(
}
}
};
match import_error(import_handle.check_block(hash, parent))? {
match import_error(import_handle.check_block(BlockCheckParams { hash, number, parent_hash }))? {
BlockImportResult::ImportedUnknown { .. } => (),
r => return Ok(r), // Any other successful result means that the block is already imported.
}
+1 -1
View File
@@ -48,7 +48,7 @@ const MAX_BLOCK_SIZE: usize = 4 * 1024 * 1024 + 512;
pub use self::error::Error;
pub use block_import::{
BlockImport, BlockOrigin, ForkChoiceStrategy, ImportedAux, BlockImportParams, ImportResult,
BlockImport, BlockOrigin, ForkChoiceStrategy, ImportedAux, BlockImportParams, BlockCheckParams, ImportResult,
JustificationImport, FinalityProofImport,
};
pub use select_chain::SelectChain;