mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 15:47:58 +00:00
fd6b29dd2c
* 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>
263 lines
9.4 KiB
Rust
263 lines
9.4 KiB
Rust
// Copyright 2017-2020 Parity Technologies (UK) Ltd.
|
|
// This file is part of Substrate.
|
|
|
|
// Substrate is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
|
|
// Substrate is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//! Import Queue primitive: something which can verify and import blocks.
|
|
//!
|
|
//! This serves as an intermediate and abstracted step between synchronization
|
|
//! and import. Each mode of consensus will have its own requirements for block
|
|
//! verification. Some algorithms can verify in parallel, while others only
|
|
//! sequentially.
|
|
//!
|
|
//! The `ImportQueue` trait allows such verification strategies to be
|
|
//! instantiated. The `BasicQueue` and `BasicVerifier` traits allow serial
|
|
//! queues to be instantiated simply.
|
|
|
|
use std::collections::HashMap;
|
|
use sp_runtime::{Justification, traits::{Block as BlockT, Header as _, NumberFor}};
|
|
use crate::error::Error as ConsensusError;
|
|
use crate::block_import::{
|
|
BlockImport, BlockOrigin, BlockImportParams, ImportedAux, JustificationImport, ImportResult,
|
|
BlockCheckParams, FinalityProofImport,
|
|
};
|
|
|
|
pub use basic_queue::BasicQueue;
|
|
|
|
mod basic_queue;
|
|
pub mod buffered_link;
|
|
|
|
/// Shared block import struct used by the queue.
|
|
pub type BoxBlockImport<B, Transaction> = Box<
|
|
dyn BlockImport<B, Error = ConsensusError, Transaction = Transaction> + Send + Sync
|
|
>;
|
|
|
|
/// Shared justification import struct used by the queue.
|
|
pub type BoxJustificationImport<B> = Box<dyn JustificationImport<B, Error=ConsensusError> + Send + Sync>;
|
|
|
|
/// Shared finality proof import struct used by the queue.
|
|
pub type BoxFinalityProofImport<B> = Box<
|
|
dyn FinalityProofImport<B, Error = ConsensusError> + Send + Sync
|
|
>;
|
|
|
|
/// Maps to the Origin used by the network.
|
|
pub type Origin = libp2p::PeerId;
|
|
|
|
/// Block data used by the queue.
|
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
|
pub struct IncomingBlock<B: BlockT> {
|
|
/// Block header hash.
|
|
pub hash: <B as BlockT>::Hash,
|
|
/// Block header if requested.
|
|
pub header: Option<<B as BlockT>::Header>,
|
|
/// Block body if requested.
|
|
pub body: Option<Vec<<B as BlockT>::Extrinsic>>,
|
|
/// Justification if requested.
|
|
pub justification: Option<Justification>,
|
|
/// The peer, we received this from
|
|
pub origin: Option<Origin>,
|
|
/// Allow importing the block skipping state verification if parent state is missing.
|
|
pub allow_missing_state: bool,
|
|
/// Re-validate existing block.
|
|
pub import_existing: bool,
|
|
}
|
|
|
|
/// Type of keys in the blockchain cache that consensus module could use for its needs.
|
|
pub type CacheKeyId = [u8; 4];
|
|
|
|
/// Verify a justification of a block
|
|
pub trait Verifier<B: BlockT>: Send + Sync {
|
|
/// Verify the given data and return the BlockImportParams and an optional
|
|
/// new set of validators to import. If not, err with an Error-Message
|
|
/// presented to the User in the logs.
|
|
fn verify(
|
|
&mut self,
|
|
origin: BlockOrigin,
|
|
header: B::Header,
|
|
justification: Option<Justification>,
|
|
body: Option<Vec<B::Extrinsic>>,
|
|
) -> Result<(BlockImportParams<B, ()>, Option<Vec<(CacheKeyId, Vec<u8>)>>), String>;
|
|
}
|
|
|
|
/// Blocks import queue API.
|
|
///
|
|
/// The `import_*` methods can be called in order to send elements for the import queue to verify.
|
|
/// Afterwards, call `poll_actions` to determine how to respond to these elements.
|
|
pub trait ImportQueue<B: BlockT>: Send {
|
|
/// Import bunch of blocks.
|
|
fn import_blocks(&mut self, origin: BlockOrigin, blocks: Vec<IncomingBlock<B>>);
|
|
/// Import a block justification.
|
|
fn import_justification(
|
|
&mut self,
|
|
who: Origin,
|
|
hash: B::Hash,
|
|
number: NumberFor<B>,
|
|
justification: Justification
|
|
);
|
|
/// Import block finality proof.
|
|
fn import_finality_proof(
|
|
&mut self,
|
|
who: Origin,
|
|
hash: B::Hash,
|
|
number: NumberFor<B>,
|
|
finality_proof: Vec<u8>
|
|
);
|
|
|
|
/// Polls for actions to perform on the network.
|
|
///
|
|
/// This method should behave in a way similar to `Future::poll`. It can register the current
|
|
/// task and notify later when more actions are ready to be polled. To continue the comparison,
|
|
/// it is as if this method always returned `Poll::Pending`.
|
|
fn poll_actions(&mut self, cx: &mut futures::task::Context, link: &mut dyn Link<B>);
|
|
}
|
|
|
|
/// Hooks that the verification queue can use to influence the synchronization
|
|
/// algorithm.
|
|
pub trait Link<B: BlockT>: Send {
|
|
/// Batch of blocks imported, with or without error.
|
|
fn blocks_processed(
|
|
&mut self,
|
|
_imported: usize,
|
|
_count: usize,
|
|
_results: Vec<(Result<BlockImportResult<NumberFor<B>>, BlockImportError>, B::Hash)>
|
|
) {}
|
|
/// Justification import result.
|
|
fn justification_imported(&mut self, _who: Origin, _hash: &B::Hash, _number: NumberFor<B>, _success: bool) {}
|
|
/// Request a justification for the given block.
|
|
fn request_justification(&mut self, _hash: &B::Hash, _number: NumberFor<B>) {}
|
|
/// Finality proof import result.
|
|
///
|
|
/// Even though we have asked for finality proof of block A, provider could return proof of
|
|
/// some earlier block B, if the proof for A was too large. The sync module should continue
|
|
/// asking for proof of A in this case.
|
|
fn finality_proof_imported(
|
|
&mut self,
|
|
_who: Origin,
|
|
_request_block: (B::Hash, NumberFor<B>),
|
|
_finalization_result: Result<(B::Hash, NumberFor<B>), ()>,
|
|
) {}
|
|
/// Request a finality proof for the given block.
|
|
fn request_finality_proof(&mut self, _hash: &B::Hash, _number: NumberFor<B>) {}
|
|
}
|
|
|
|
/// Block import successful result.
|
|
#[derive(Debug, PartialEq)]
|
|
pub enum BlockImportResult<N: ::std::fmt::Debug + PartialEq> {
|
|
/// Imported known block.
|
|
ImportedKnown(N),
|
|
/// Imported unknown block.
|
|
ImportedUnknown(N, ImportedAux, Option<Origin>),
|
|
}
|
|
|
|
/// Block import error.
|
|
#[derive(Debug)]
|
|
pub enum BlockImportError {
|
|
/// Block missed header, can't be imported
|
|
IncompleteHeader(Option<Origin>),
|
|
/// Block verification failed, can't be imported
|
|
VerificationFailed(Option<Origin>, String),
|
|
/// Block is known to be Bad
|
|
BadBlock(Option<Origin>),
|
|
/// Parent state is missing.
|
|
MissingState,
|
|
/// Block has an unknown parent
|
|
UnknownParent,
|
|
/// Block import has been cancelled. This can happen if the parent block fails to be imported.
|
|
Cancelled,
|
|
/// Other error.
|
|
Other(ConsensusError),
|
|
}
|
|
|
|
/// Single block import function.
|
|
pub fn import_single_block<B: BlockT, V: Verifier<B>, Transaction>(
|
|
import_handle: &mut dyn BlockImport<B, Transaction = Transaction, Error = ConsensusError>,
|
|
block_origin: BlockOrigin,
|
|
block: IncomingBlock<B>,
|
|
verifier: &mut V,
|
|
) -> Result<BlockImportResult<NumberFor<B>>, BlockImportError> {
|
|
let peer = block.origin;
|
|
|
|
let (header, justification) = match (block.header, block.justification) {
|
|
(Some(header), justification) => (header, justification),
|
|
(None, _) => {
|
|
if let Some(ref peer) = peer {
|
|
debug!(target: "sync", "Header {} was not provided by {} ", block.hash, peer);
|
|
} else {
|
|
debug!(target: "sync", "Header {} was not provided ", block.hash);
|
|
}
|
|
return Err(BlockImportError::IncompleteHeader(peer))
|
|
},
|
|
};
|
|
|
|
trace!(target: "sync", "Header {} has {:?} logs", block.hash, header.digest().logs().len());
|
|
|
|
let number = header.number().clone();
|
|
let hash = header.hash();
|
|
let parent_hash = header.parent_hash().clone();
|
|
|
|
let import_error = |e| {
|
|
match e {
|
|
Ok(ImportResult::AlreadyInChain) => {
|
|
trace!(target: "sync", "Block already in chain {}: {:?}", number, hash);
|
|
Ok(BlockImportResult::ImportedKnown(number))
|
|
},
|
|
Ok(ImportResult::Imported(aux)) => Ok(BlockImportResult::ImportedUnknown(number, aux, peer.clone())),
|
|
Ok(ImportResult::MissingState) => {
|
|
debug!(target: "sync", "Parent state is missing for {}: {:?}, parent: {:?}", number, hash, parent_hash);
|
|
Err(BlockImportError::MissingState)
|
|
},
|
|
Ok(ImportResult::UnknownParent) => {
|
|
debug!(target: "sync", "Block with unknown parent {}: {:?}, parent: {:?}", number, hash, parent_hash);
|
|
Err(BlockImportError::UnknownParent)
|
|
},
|
|
Ok(ImportResult::KnownBad) => {
|
|
debug!(target: "sync", "Peer gave us a bad block {}: {:?}", number, hash);
|
|
Err(BlockImportError::BadBlock(peer.clone()))
|
|
},
|
|
Err(e) => {
|
|
debug!(target: "sync", "Error importing block {}: {:?}: {:?}", number, hash, e);
|
|
Err(BlockImportError::Other(e))
|
|
}
|
|
}
|
|
};
|
|
match import_error(import_handle.check_block(BlockCheckParams {
|
|
hash,
|
|
number,
|
|
parent_hash,
|
|
allow_missing_state: block.allow_missing_state,
|
|
import_existing: block.import_existing,
|
|
}))? {
|
|
BlockImportResult::ImportedUnknown { .. } => (),
|
|
r => return Ok(r), // Any other successful result means that the block is already imported.
|
|
}
|
|
|
|
let (mut import_block, maybe_keys) = verifier.verify(block_origin, header, justification, block.body)
|
|
.map_err(|msg| {
|
|
if let Some(ref peer) = peer {
|
|
trace!(target: "sync", "Verifying {}({}) from {} failed: {}", number, hash, peer, msg);
|
|
} else {
|
|
trace!(target: "sync", "Verifying {}({}) failed: {}", number, hash, msg);
|
|
}
|
|
BlockImportError::VerificationFailed(peer.clone(), msg)
|
|
})?;
|
|
|
|
let mut cache = HashMap::new();
|
|
if let Some(keys) = maybe_keys {
|
|
cache.extend(keys.into_iter());
|
|
}
|
|
import_block.allow_missing_state = block.allow_missing_state;
|
|
|
|
import_error(import_handle.import_block(import_block.convert_transaction(), cache))
|
|
}
|