Run cargo fmt on the whole code base (#9394)

* Run cargo fmt on the whole code base

* Second run

* Add CI check

* Fix compilation

* More unnecessary braces

* Handle weights

* Use --all

* Use correct attributes...

* Fix UI tests

* AHHHHHHHHH

* 🤦

* Docs

* Fix compilation

* 🤷

* Please stop

* 🤦 x 2

* More

* make rustfmt.toml consistent with polkadot

Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
Bastian Köcher
2021-07-21 16:32:32 +02:00
committed by GitHub
parent d451c38c1c
commit 7b56ab15b4
1010 changed files with 53339 additions and 51208 deletions
@@ -17,16 +17,14 @@
//! Block import helpers.
use sp_runtime::traits::{Block as BlockT, DigestItemFor, Header as HeaderT, NumberFor, HashFor};
use sp_runtime::{Justification, Justifications};
use serde::{Serialize, Deserialize};
use std::borrow::Cow;
use std::collections::HashMap;
use std::sync::Arc;
use std::any::Any;
use serde::{Deserialize, Serialize};
use sp_runtime::{
traits::{Block as BlockT, DigestItemFor, HashFor, Header as HeaderT, NumberFor},
Justification, Justifications,
};
use std::{any::Any, borrow::Cow, collections::HashMap, sync::Arc};
use crate::Error;
use crate::import_queue::CacheKeyId;
use crate::{import_queue::CacheKeyId, Error};
/// Block import result.
#[derive(Debug, PartialEq, Eq)]
@@ -88,8 +86,8 @@ impl ImportResult {
if aux.needs_justification {
justification_sync_link.request_justification(hash, number);
}
}
_ => {}
},
_ => {},
}
}
}
@@ -154,9 +152,7 @@ pub struct ImportedState<B: BlockT> {
impl<B: BlockT> std::fmt::Debug for ImportedState<B> {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
fmt.debug_struct("ImportedState")
.field("block", &self.block)
.finish()
fmt.debug_struct("ImportedState").field("block", &self.block).finish()
}
}
@@ -226,12 +222,10 @@ pub struct BlockImportParams<Block: BlockT, Transaction> {
impl<Block: BlockT, Transaction> BlockImportParams<Block, Transaction> {
/// Create a new block import params.
pub fn new(
origin: BlockOrigin,
header: Block::Header,
) -> Self {
pub fn new(origin: BlockOrigin, header: Block::Header) -> Self {
Self {
origin, header,
origin,
header,
justifications: None,
post_digests: Vec::new(),
body: None,
@@ -273,7 +267,9 @@ impl<Block: BlockT, Transaction> BlockImportParams<Block, Transaction> {
///
/// Actually this just sets `StorageChanges::Changes` to `None` and makes rustc think that `Self` now
/// uses a different transaction type.
pub fn clear_storage_changes_and_mutate<Transaction2>(self) -> BlockImportParams<Block, Transaction2> {
pub fn clear_storage_changes_and_mutate<Transaction2>(
self,
) -> BlockImportParams<Block, Transaction2> {
// Preserve imported state.
let state_action = match self.state_action {
StateAction::ApplyChanges(StorageChanges::Import(state)) =>
@@ -305,14 +301,15 @@ impl<Block: BlockT, Transaction> BlockImportParams<Block, Transaction> {
let (k, v) = self.intermediates.remove_entry(key).ok_or(Error::NoIntermediate)?;
v.downcast::<T>().or_else(|v| {
self.intermediates.insert(k, v);
Err(Error::InvalidIntermediate)
self.intermediates.insert(k, v);
Err(Error::InvalidIntermediate)
})
}
/// Get a reference to a given intermediate.
pub fn intermediate<T: 'static>(&self, key: &[u8]) -> Result<&T, Error> {
self.intermediates.get(key)
self.intermediates
.get(key)
.ok_or(Error::NoIntermediate)?
.downcast_ref::<T>()
.ok_or(Error::InvalidIntermediate)
@@ -320,7 +317,8 @@ impl<Block: BlockT, Transaction> BlockImportParams<Block, Transaction> {
/// Get a mutable reference to a given intermediate.
pub fn intermediate_mut<T: 'static>(&mut self, key: &[u8]) -> Result<&mut T, Error> {
self.intermediates.get_mut(key)
self.intermediates
.get_mut(key)
.ok_or(Error::NoIntermediate)?
.downcast_mut::<T>()
.ok_or(Error::InvalidIntermediate)
@@ -353,8 +351,8 @@ pub trait BlockImport<B: BlockT> {
#[async_trait::async_trait]
impl<B: BlockT, Transaction> BlockImport<B> for crate::import_queue::BoxBlockImport<B, Transaction>
where
Transaction: Send + 'static,
where
Transaction: Send + 'static,
{
type Error = crate::error::Error;
type Transaction = Transaction;
@@ -381,10 +379,10 @@ impl<B: BlockT, Transaction> BlockImport<B> for crate::import_queue::BoxBlockImp
#[async_trait::async_trait]
impl<B: BlockT, T, E: std::error::Error + Send + 'static, Transaction> BlockImport<B> for Arc<T>
where
for<'r> &'r T: BlockImport<B, Error = E, Transaction = Transaction>,
T: Send + Sync,
Transaction: Send + 'static,
where
for<'r> &'r T: BlockImport<B, Error = E, Transaction = Transaction>,
T: Send + Sync,
Transaction: Send + 'static,
{
type Error = E;
type Transaction = Transaction;
@@ -18,9 +18,9 @@
//! Block announcement validation.
use crate::BlockStatus;
use futures::FutureExt as _;
use sp_runtime::{generic::BlockId, traits::Block};
use std::{error::Error, future::Future, pin::Pin, sync::Arc};
use futures::FutureExt as _;
/// A type which provides access to chain information.
pub trait Chain<B: Block> {
@@ -92,6 +92,7 @@ impl<B: Block> BlockAnnounceValidator<B> for DefaultBlockAnnounceValidator {
} else {
Ok(Validation::Success { is_new_best: false })
}
}.boxed()
}
.boxed()
}
}
@@ -16,8 +16,8 @@
// limitations under the License.
//! Error types in Consensus
use sp_version::RuntimeVersion;
use sp_core::ed25519::Public;
use sp_version::RuntimeVersion;
use std::error;
/// Result type alias.
@@ -58,8 +58,10 @@ pub enum Error {
#[error("Message sender {0:?} is not a valid authority")]
InvalidAuthority(Public),
/// Authoring interface does not match the runtime.
#[error("Authoring for current \
runtime is not supported. Native ({native}) cannot author for on-chain ({on_chain}).")]
#[error(
"Authoring for current \
runtime is not supported. Native ({native}) cannot author for on-chain ({on_chain})."
)]
IncompatibleAuthoringRuntime { native: RuntimeVersion, on_chain: RuntimeVersion },
/// Authoring interface does not match the runtime.
#[error("Authoring for current runtime is not supported since it has no version.")]
@@ -81,7 +83,7 @@ pub enum Error {
ChainLookup(String),
/// Signing failed
#[error("Failed to sign using key: {0:?}. Reason: {1}")]
CannotSign(Vec<u8>, String)
CannotSign(Vec<u8>, String),
}
impl core::convert::From<Public> for Error {
@@ -18,7 +18,7 @@
//! Block evaluation and evaluation errors.
use codec::Encode;
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, One, CheckedConversion};
use sp_runtime::traits::{Block as BlockT, CheckedConversion, Header as HeaderT, One};
// This is just a best effort to encode the number. None indicated that it's too big to encode
// in a u128.
@@ -48,15 +48,13 @@ pub fn evaluate_initial<Block: BlockT>(
parent_hash: &<Block as BlockT>::Hash,
parent_number: <<Block as BlockT>::Header as HeaderT>::Number,
) -> Result<()> {
let encoded = Encode::encode(proposal);
let proposal = Block::decode(&mut &encoded[..])
.map_err(|e| Error::BadProposalFormat(e))?;
let proposal = Block::decode(&mut &encoded[..]).map_err(|e| Error::BadProposalFormat(e))?;
if *parent_hash != *proposal.header().parent_hash() {
return Err(Error::WrongParentHash {
expected: format!("{:?}", *parent_hash),
got: format!("{:?}", proposal.header().parent_hash())
got: format!("{:?}", proposal.header().parent_hash()),
})
}
@@ -28,14 +28,17 @@
use std::collections::HashMap;
use sp_runtime::{Justifications, traits::{Block as BlockT, Header as _, NumberFor}};
use sp_runtime::{
traits::{Block as BlockT, Header as _, NumberFor},
Justifications,
};
use crate::{
error::Error as ConsensusError,
block_import::{
BlockImport, BlockOrigin, BlockImportParams, ImportedAux, JustificationImport, ImportResult,
BlockCheckParams, ImportedState, StateAction,
BlockCheckParams, BlockImport, BlockImportParams, BlockOrigin, ImportResult, ImportedAux,
ImportedState, JustificationImport, StateAction,
},
error::Error as ConsensusError,
metrics::Metrics,
};
pub use basic_queue::BasicQueue;
@@ -43,18 +46,19 @@ pub use basic_queue::BasicQueue;
/// A commonly-used Import Queue type.
///
/// This defines the transaction type of the `BasicQueue` to be the transaction type for a client.
pub type DefaultImportQueue<Block, Client> = BasicQueue<Block, sp_api::TransactionFor<Client, Block>>;
pub type DefaultImportQueue<Block, Client> =
BasicQueue<Block, sp_api::TransactionFor<Client, Block>>;
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
>;
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>;
pub type BoxJustificationImport<B> =
Box<dyn JustificationImport<B, Error = ConsensusError> + Send + Sync>;
/// Maps to the Origin used by the network.
pub type Origin = libp2p::PeerId;
@@ -115,7 +119,7 @@ pub trait ImportQueue<B: BlockT>: Send {
who: Origin,
hash: B::Hash,
number: NumberFor<B>,
justifications: Justifications
justifications: Justifications,
);
/// Polls for actions to perform on the network.
///
@@ -133,10 +137,18 @@ pub trait Link<B: BlockT>: Send {
&mut self,
_imported: usize,
_count: usize,
_results: Vec<(Result<BlockImportResult<NumberFor<B>>, BlockImportError>, B::Hash)>
) {}
_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) {}
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>) {}
}
@@ -180,7 +192,11 @@ pub async fn import_single_block<B: BlockT, V: Verifier<B>, Transaction: Send +
}
/// Single block import function with metering.
pub(crate) async fn import_single_block_metered<B: BlockT, V: Verifier<B>, Transaction: Send + 'static>(
pub(crate) async fn import_single_block_metered<
B: BlockT,
V: Verifier<B>,
Transaction: Send + 'static,
>(
import_handle: &mut impl BlockImport<B, Transaction = Transaction, Error = ConsensusError>,
block_origin: BlockOrigin,
block: IncomingBlock<B>,
@@ -207,60 +223,61 @@ pub(crate) async fn import_single_block_metered<B: BlockT, V: Verifier<B>, Trans
let hash = header.hash();
let parent_hash = header.parent_hash().clone();
let import_handler = |import| {
match import {
Ok(ImportResult::AlreadyInChain) => {
trace!(target: "sync", "Block already in chain {}: {:?}", number, hash);
Ok(BlockImportResult::ImportedKnown(number, peer.clone()))
},
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))
}
}
let import_handler = |import| match import {
Ok(ImportResult::AlreadyInChain) => {
trace!(target: "sync", "Block already in chain {}: {:?}", number, hash);
Ok(BlockImportResult::ImportedKnown(number, peer.clone()))
},
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_handler(import_handle.check_block(BlockCheckParams {
hash,
number,
parent_hash,
allow_missing_state: block.allow_missing_state,
import_existing: block.import_existing,
}).await)? {
match import_handler(
import_handle
.check_block(BlockCheckParams {
hash,
number,
parent_hash,
allow_missing_state: block.allow_missing_state,
import_existing: block.import_existing,
})
.await,
)? {
BlockImportResult::ImportedUnknown { .. } => (),
r => return Ok(r), // Any other successful result means that the block is already imported.
}
let started = wasm_timer::Instant::now();
let (mut import_block, maybe_keys) = verifier.verify(
block_origin,
header,
justifications,
block.body
).await.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);
}
if let Some(metrics) = metrics.as_ref() {
metrics.report_verification(false, started.elapsed());
}
BlockImportError::VerificationFailed(peer.clone(), msg)
})?;
let (mut import_block, maybe_keys) = verifier
.verify(block_origin, header, justifications, block.body)
.await
.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);
}
if let Some(metrics) = metrics.as_ref() {
metrics.report_verification(false, started.elapsed());
}
BlockImportError::VerificationFailed(peer.clone(), msg)
})?;
if let Some(metrics) = metrics.as_ref() {
metrics.report_verification(true, started.elapsed());
@@ -15,20 +15,25 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use std::{pin::Pin, time::Duration, marker::PhantomData};
use futures::{prelude::*, task::Context, task::Poll};
use futures::{
prelude::*,
task::{Context, Poll},
};
use futures_timer::Delay;
use sp_runtime::{Justification, Justifications, traits::{Block as BlockT, Header as HeaderT, NumberFor}};
use sp_utils::mpsc::{TracingUnboundedSender, tracing_unbounded, TracingUnboundedReceiver};
use prometheus_endpoint::Registry;
use sp_runtime::{
traits::{Block as BlockT, Header as HeaderT, NumberFor},
Justification, Justifications,
};
use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender};
use std::{marker::PhantomData, pin::Pin, time::Duration};
use crate::{
block_import::BlockOrigin,
import_queue::{
BlockImportResult, BlockImportError, Verifier, BoxBlockImport,
BoxJustificationImport, ImportQueue, Link, Origin,
IncomingBlock, import_single_block_metered,
buffered_link::{self, BufferedLinkSender, BufferedLinkReceiver},
buffered_link::{self, BufferedLinkReceiver, BufferedLinkSender},
import_single_block_metered, BlockImportError, BlockImportResult, BoxBlockImport,
BoxJustificationImport, ImportQueue, IncomingBlock, Link, Origin, Verifier,
},
metrics::Metrics,
};
@@ -85,24 +90,20 @@ impl<B: BlockT, Transaction: Send + 'static> BasicQueue<B, Transaction> {
spawner.spawn_essential_blocking("basic-block-import-worker", future.boxed());
Self {
justification_sender,
block_import_sender,
result_port,
_phantom: PhantomData,
}
Self { justification_sender, block_import_sender, result_port, _phantom: PhantomData }
}
}
impl<B: BlockT, Transaction: Send> ImportQueue<B> for BasicQueue<B, Transaction> {
fn import_blocks(&mut self, origin: BlockOrigin, blocks: Vec<IncomingBlock<B>>) {
if blocks.is_empty() {
return;
return
}
trace!(target: "sync", "Scheduling {} blocks for import", blocks.len());
let res =
self.block_import_sender.unbounded_send(worker_messages::ImportBlocks(origin, blocks));
let res = self
.block_import_sender
.unbounded_send(worker_messages::ImportBlocks(origin, blocks));
if res.is_err() {
log::error!(
@@ -145,7 +146,12 @@ mod worker_messages {
use super::*;
pub struct ImportBlocks<B: BlockT>(pub BlockOrigin, pub Vec<IncomingBlock<B>>);
pub struct ImportJustification<B: BlockT>(pub Origin, pub B::Hash, pub NumberFor<B>, pub Justification);
pub struct ImportJustification<B: BlockT>(
pub Origin,
pub B::Hash,
pub NumberFor<B>,
pub Justification,
);
}
/// The process of importing blocks.
@@ -164,7 +170,8 @@ async fn block_import_process<B: BlockT, Transaction: Send + 'static>(
delay_between_blocks: Duration,
) {
loop {
let worker_messages::ImportBlocks(origin, blocks) = match block_import_receiver.next().await {
let worker_messages::ImportBlocks(origin, blocks) = match block_import_receiver.next().await
{
Some(blocks) => blocks,
None => {
log::debug!(
@@ -182,7 +189,8 @@ async fn block_import_process<B: BlockT, Transaction: Send + 'static>(
&mut verifier,
delay_between_blocks,
metrics.clone(),
).await;
)
.await;
result_sender.blocks_processed(res.imported, res.block_count, res.results);
}
@@ -214,11 +222,7 @@ impl<B: BlockT> BlockImportWorker<B> {
let (block_import_sender, block_import_port) =
tracing_unbounded("mpsc_import_queue_worker_blocks");
let mut worker = BlockImportWorker {
result_sender,
justification_import,
metrics,
};
let mut worker = BlockImportWorker { result_sender, justification_import, metrics };
let delay_between_blocks = Duration::default();
@@ -248,29 +252,26 @@ impl<B: BlockT> BlockImportWorker<B> {
target: "block-import",
"Stopping block import because result channel was closed!",
);
return;
return
}
// Make sure to first process all justifications
while let Poll::Ready(justification) = futures::poll!(justification_port.next()) {
match justification {
Some(ImportJustification(who, hash, number, justification)) => {
worker
.import_justification(who, hash, number, justification)
.await
}
Some(ImportJustification(who, hash, number, justification)) =>
worker.import_justification(who, hash, number, justification).await,
None => {
log::debug!(
target: "block-import",
"Stopping block import because justification channel was closed!",
);
return;
}
return
},
}
}
if let Poll::Ready(()) = futures::poll!(&mut block_import_process) {
return;
return
}
// All futures that we polled are now pending.
@@ -310,13 +311,10 @@ impl<B: BlockT> BlockImportWorker<B> {
};
if let Some(metrics) = self.metrics.as_ref() {
metrics
.justification_import_time
.observe(started.elapsed().as_secs_f64());
metrics.justification_import_time.observe(started.elapsed().as_secs_f64());
}
self.result_sender
.justification_imported(who, &hash, number, success);
self.result_sender.justification_imported(who, &hash, number, success);
}
}
@@ -382,7 +380,8 @@ async fn import_many_blocks<B: BlockT, V: Verifier<B>, Transaction: Send + 'stat
block,
verifier,
metrics.clone(),
).await
)
.await
};
if let Some(metrics) = metrics.as_ref() {
@@ -604,7 +603,7 @@ mod tests {
block_on(futures::future::poll_fn(|cx| {
while link.events.len() < 9 {
match Future::poll(Pin::new(&mut worker), cx) {
Poll::Pending => {}
Poll::Pending => {},
Poll::Ready(()) => panic!("import queue worker should not conclude."),
}
@@ -36,13 +36,15 @@
//! std::task::Poll::Pending::<()>
//! });
//! ```
//!
use crate::import_queue::{BlockImportError, BlockImportResult, Link, Origin};
use futures::prelude::*;
use sp_runtime::traits::{Block as BlockT, NumberFor};
use sp_utils::mpsc::{TracingUnboundedSender, TracingUnboundedReceiver, tracing_unbounded};
use std::{pin::Pin, task::Context, task::Poll};
use crate::import_queue::{Origin, Link, BlockImportResult, BlockImportError};
use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender};
use std::{
pin::Pin,
task::{Context, Poll},
};
/// Wraps around an unbounded channel from the `futures` crate. The sender implements `Link` and
/// can be used to buffer commands, and the receiver can be used to poll said commands and transfer
@@ -70,15 +72,17 @@ impl<B: BlockT> BufferedLinkSender<B> {
impl<B: BlockT> Clone for BufferedLinkSender<B> {
fn clone(&self) -> Self {
BufferedLinkSender {
tx: self.tx.clone(),
}
BufferedLinkSender { tx: self.tx.clone() }
}
}
/// Internal buffered message.
enum BlockImportWorkerMsg<B: BlockT> {
BlocksProcessed(usize, usize, Vec<(Result<BlockImportResult<NumberFor<B>>, BlockImportError>, B::Hash)>),
BlocksProcessed(
usize,
usize,
Vec<(Result<BlockImportResult<NumberFor<B>>, BlockImportError>, B::Hash)>,
),
JustificationImported(Origin, B::Hash, NumberFor<B>, bool),
RequestJustification(B::Hash, NumberFor<B>),
}
@@ -88,9 +92,11 @@ impl<B: BlockT> Link<B> for BufferedLinkSender<B> {
&mut self,
imported: usize,
count: usize,
results: Vec<(Result<BlockImportResult<NumberFor<B>>, BlockImportError>, B::Hash)>
results: Vec<(Result<BlockImportResult<NumberFor<B>>, BlockImportError>, B::Hash)>,
) {
let _ = self.tx.unbounded_send(BlockImportWorkerMsg::BlocksProcessed(imported, count, results));
let _ = self
.tx
.unbounded_send(BlockImportWorkerMsg::BlocksProcessed(imported, count, results));
}
fn justification_imported(
@@ -98,14 +104,16 @@ impl<B: BlockT> Link<B> for BufferedLinkSender<B> {
who: Origin,
hash: &B::Hash,
number: NumberFor<B>,
success: bool
success: bool,
) {
let msg = BlockImportWorkerMsg::JustificationImported(who, hash.clone(), number, success);
let _ = self.tx.unbounded_send(msg);
}
fn request_justification(&mut self, hash: &B::Hash, number: NumberFor<B>) {
let _ = self.tx.unbounded_send(BlockImportWorkerMsg::RequestJustification(hash.clone(), number));
let _ = self
.tx
.unbounded_send(BlockImportWorkerMsg::RequestJustification(hash.clone(), number));
}
}
@@ -23,28 +23,28 @@
// This provides "unused" building blocks to other crates
#![allow(dead_code)]
// our error-chain could potentially blow up otherwise
#![recursion_limit="128"]
#![recursion_limit = "128"]
#[macro_use] extern crate log;
#[macro_use]
extern crate log;
use std::sync::Arc;
use std::time::Duration;
use std::{sync::Arc, time::Duration};
use sp_runtime::{
generic::BlockId, traits::{Block as BlockT, DigestFor, NumberFor, HashFor},
};
use futures::prelude::*;
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, DigestFor, HashFor, NumberFor},
};
use sp_state_machine::StorageProof;
pub mod block_import;
pub mod block_validation;
pub mod error;
pub mod block_import;
mod select_chain;
pub mod import_queue;
pub mod evaluation;
pub mod import_queue;
mod metrics;
mod select_chain;
pub use self::error::Error;
pub use block_import::{
@@ -52,10 +52,10 @@ pub use block_import::{
ImportResult, ImportedAux, ImportedState, JustificationImport, JustificationSyncLink,
StateAction, StorageChanges,
};
pub use select_chain::SelectChain;
pub use sp_state_machine::Backend as StateBackend;
pub use import_queue::DefaultImportQueue;
pub use select_chain::SelectChain;
pub use sp_inherents::InherentData;
pub use sp_state_machine::Backend as StateBackend;
/// Block status.
#[derive(Debug, PartialEq, Eq)]
@@ -80,7 +80,9 @@ pub trait Environment<B: BlockT> {
type Proposer: Proposer<B> + Send + 'static;
/// A future that resolves to the proposer.
type CreateProposer: Future<Output = Result<Self::Proposer, Self::Error>>
+ Send + Unpin + 'static;
+ Send
+ Unpin
+ 'static;
/// Error which can occur upon creation.
type Error: From<Error> + std::fmt::Debug + 'static;
@@ -96,7 +98,8 @@ pub struct Proposal<Block: BlockT, Transaction, Proof> {
/// Proof that was recorded while building the block.
pub proof: Proof,
/// The storage changes while building this block.
pub storage_changes: sp_state_machine::StorageChanges<Transaction, HashFor<Block>, NumberFor<Block>>,
pub storage_changes:
sp_state_machine::StorageChanges<Transaction, HashFor<Block>, NumberFor<Block>>,
}
/// Error that is returned when [`ProofRecording`] requested to record a proof,
@@ -179,8 +182,7 @@ pub trait Proposer<B: BlockT> {
/// The transaction type used by the backend.
type Transaction: Default + Send + 'static;
/// Future that resolves to a committed proposal with an optional proof.
type Proposal:
Future<Output = Result<Proposal<B, Self::Transaction, Self::Proof>, Self::Error>>
type Proposal: Future<Output = Result<Proposal<B, Self::Transaction, Self::Proof>, Self::Error>>
+ Send
+ Unpin
+ 'static;
@@ -233,11 +235,19 @@ pub trait SyncOracle {
pub struct NoNetwork;
impl SyncOracle for NoNetwork {
fn is_major_syncing(&mut self) -> bool { false }
fn is_offline(&mut self) -> bool { false }
fn is_major_syncing(&mut self) -> bool {
false
}
fn is_offline(&mut self) -> bool {
false
}
}
impl<T> SyncOracle for Arc<T> where T: ?Sized, for<'r> &'r T: SyncOracle {
impl<T> SyncOracle for Arc<T>
where
T: ?Sized,
for<'r> &'r T: SyncOracle,
{
fn is_major_syncing(&mut self) -> bool {
<&T>::is_major_syncing(&mut &**self)
}
@@ -277,13 +287,10 @@ impl<T: sp_version::GetRuntimeVersion<Block>, Block: BlockT> CanAuthorWith<Block
fn can_author_with(&self, at: &BlockId<Block>) -> Result<(), String> {
match self.0.runtime_version(at) {
Ok(version) => self.0.native_version().can_author_with(&version),
Err(e) => {
Err(format!(
"Failed to get runtime version at `{}` and will disable authoring. Error: {}",
at,
e,
))
}
Err(e) => Err(format!(
"Failed to get runtime version at `{}` and will disable authoring. Error: {}",
at, e,
)),
}
}
}
@@ -18,12 +18,13 @@
//! Metering tools for consensus
use prometheus_endpoint::{
register, U64, Registry, PrometheusError, Opts, CounterVec, Histogram, HistogramVec, HistogramOpts
register, CounterVec, Histogram, HistogramOpts, HistogramVec, Opts, PrometheusError, Registry,
U64,
};
use sp_runtime::traits::{Block as BlockT, NumberFor};
use crate::import_queue::{BlockImportResult, BlockImportError};
use crate::import_queue::{BlockImportError, BlockImportResult};
/// Generic Prometheus metrics for common consensus functionality.
#[derive(Clone)]
@@ -40,36 +41,29 @@ impl Metrics {
import_queue_processed: register(
CounterVec::new(
Opts::new("import_queue_processed_total", "Blocks processed by import queue"),
&["result"] // 'success or failure
&["result"], // 'success or failure
)?,
registry,
)?,
block_verification_time: register(
HistogramVec::new(
HistogramOpts::new(
"block_verification_time",
"Time taken to verify blocks",
),
HistogramOpts::new("block_verification_time", "Time taken to verify blocks"),
&["result"],
)?,
registry,
)?,
block_verification_and_import_time: register(
Histogram::with_opts(
HistogramOpts::new(
"block_verification_and_import_time",
"Time taken to verify and import blocks",
),
)?,
Histogram::with_opts(HistogramOpts::new(
"block_verification_and_import_time",
"Time taken to verify and import blocks",
))?,
registry,
)?,
justification_import_time: register(
Histogram::with_opts(
HistogramOpts::new(
"justification_import_time",
"Time taken to import justifications",
),
)?,
Histogram::with_opts(HistogramOpts::new(
"justification_import_time",
"Time taken to import justifications",
))?,
registry,
)?,
})
@@ -82,7 +76,7 @@ impl Metrics {
let label = match result {
Ok(_) => "success",
Err(BlockImportError::IncompleteHeader(_)) => "incomplete_header",
Err(BlockImportError::VerificationFailed(_,_)) => "verification_failed",
Err(BlockImportError::VerificationFailed(_, _)) => "verification_failed",
Err(BlockImportError::BadBlock(_)) => "bad_block",
Err(BlockImportError::MissingState) => "missing_state",
Err(BlockImportError::UnknownParent) => "unknown_parent",
@@ -90,15 +84,13 @@ impl Metrics {
Err(BlockImportError::Other(_)) => "failed",
};
self.import_queue_processed.with_label_values(
&[label]
).inc();
self.import_queue_processed.with_label_values(&[label]).inc();
}
pub fn report_verification(&self, success: bool, time: std::time::Duration) {
self.block_verification_time.with_label_values(
&[if success { "success" } else { "verification_failed" }]
).observe(time.as_secs_f64());
self.block_verification_time
.with_label_values(&[if success { "success" } else { "verification_failed" }])
.observe(time.as_secs_f64());
}
pub fn report_verification_and_import(&self, time: std::time::Duration) {
@@ -18,7 +18,6 @@
use crate::error::Error;
use sp_runtime::traits::{Block as BlockT, NumberFor};
/// The SelectChain trait defines the strategy upon which the head is chosen
/// if multiple forks are present for an opaque definition of "best" in the
/// specific chain build.