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
@@ -22,9 +22,9 @@
//! `CompatibleDigestItem` trait to appear in public interfaces.
use crate::AURA_ENGINE_ID;
use sp_runtime::generic::DigestItem;
use codec::{Codec, Encode};
use sp_consensus_slots::Slot;
use codec::{Encode, Codec};
use sp_runtime::generic::DigestItem;
use sp_std::fmt::Debug;
/// A digest item which is usable with aura consensus.
@@ -42,9 +42,10 @@ pub trait CompatibleDigestItem<Signature>: Sized {
fn as_aura_pre_digest(&self) -> Option<Slot>;
}
impl<Signature, Hash> CompatibleDigestItem<Signature> for DigestItem<Hash> where
impl<Signature, Hash> CompatibleDigestItem<Signature> for DigestItem<Hash>
where
Signature: Codec,
Hash: Debug + Send + Sync + Eq + Clone + Codec + 'static
Hash: Debug + Send + Sync + Eq + Clone + Codec + 'static,
{
fn aura_seal(signature: Signature) -> Self {
DigestItem::Seal(AURA_ENGINE_ID, signature.encode())
@@ -16,8 +16,7 @@
// limitations under the License.
/// Contains the inherents for the AURA module
use sp_inherents::{InherentIdentifier, InherentData, Error};
use sp_inherents::{Error, InherentData, InherentIdentifier};
/// The Aura inherent identifier.
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"auraslot";
@@ -28,13 +27,13 @@ pub type InherentType = sp_consensus_slots::Slot;
/// Auxiliary trait to extract Aura inherent data.
pub trait AuraInherentData {
/// Get aura inherent data.
fn aura_inherent_data(&self) ->Result<Option<InherentType>, Error>;
fn aura_inherent_data(&self) -> Result<Option<InherentType>, Error>;
/// Replace aura inherent data.
fn aura_replace_inherent_data(&mut self, new: InherentType);
}
impl AuraInherentData for InherentData {
fn aura_inherent_data(&self) ->Result<Option<InherentType>, Error> {
fn aura_inherent_data(&self) -> Result<Option<InherentType>, Error> {
self.get_data(&INHERENT_IDENTIFIER)
}
@@ -54,9 +53,7 @@ pub struct InherentDataProvider {
impl InherentDataProvider {
/// Create a new instance with the given slot.
pub fn new(slot: InherentType) -> Self {
Self {
slot,
}
Self { slot }
}
/// Creates the inherent data provider by calculating the slot from the given
@@ -65,13 +62,10 @@ impl InherentDataProvider {
timestamp: sp_timestamp::Timestamp,
duration: std::time::Duration,
) -> Self {
let slot = InherentType::from(
(timestamp.as_duration().as_millis() / duration.as_millis()) as u64
);
let slot =
InherentType::from((timestamp.as_duration().as_millis() / duration.as_millis()) as u64);
Self {
slot,
}
Self { slot }
}
}
@@ -87,10 +81,7 @@ impl sp_std::ops::Deref for InherentDataProvider {
#[cfg(feature = "std")]
#[async_trait::async_trait]
impl sp_inherents::InherentDataProvider for InherentDataProvider {
fn provide_inherent_data(
&self,
inherent_data: &mut InherentData,
) ->Result<(), Error> {
fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error> {
inherent_data.put_data(INHERENT_IDENTIFIER, &self.slot)
}
@@ -19,9 +19,9 @@
#![cfg_attr(not(feature = "std"), no_std)]
use codec::{Encode, Decode, Codec};
use sp_std::vec::Vec;
use codec::{Codec, Decode, Encode};
use sp_runtime::ConsensusEngineId;
use sp_std::vec::Vec;
pub mod digests;
pub mod inherents;
@@ -46,7 +46,7 @@ pub mod sr25519 {
pub mod ed25519 {
mod app_ed25519 {
use sp_application_crypto::{app_crypto, key_types::AURA, ed25519};
use sp_application_crypto::{app_crypto, ed25519, key_types::AURA};
app_crypto!(ed25519, AURA);
}
@@ -22,8 +22,8 @@ use super::{
BabeEpochConfiguration, Slot, BABE_ENGINE_ID,
};
use codec::{Codec, Decode, Encode};
use sp_std::vec::Vec;
use sp_runtime::{DigestItem, RuntimeDebug};
use sp_std::vec::Vec;
use sp_consensus_vrf::schnorrkel::{Randomness, VRFOutput, VRFProof};
@@ -143,14 +143,13 @@ pub enum NextConfigDescriptor {
c: (u64, u64),
/// Value of `allowed_slots` in `BabeEpochConfiguration`.
allowed_slots: AllowedSlots,
}
},
}
impl From<NextConfigDescriptor> for BabeEpochConfiguration {
fn from(desc: NextConfigDescriptor) -> Self {
match desc {
NextConfigDescriptor::V1 { c, allowed_slots } =>
Self { c, allowed_slots },
NextConfigDescriptor::V1 { c, allowed_slots } => Self { c, allowed_slots },
}
}
}
@@ -176,8 +175,9 @@ pub trait CompatibleDigestItem: Sized {
fn as_next_config_descriptor(&self) -> Option<NextConfigDescriptor>;
}
impl<Hash> CompatibleDigestItem for DigestItem<Hash> where
Hash: Send + Sync + Eq + Clone + Codec + 'static
impl<Hash> CompatibleDigestItem for DigestItem<Hash>
where
Hash: Send + Sync + Eq + Clone + Codec + 'static,
{
fn babe_pre_digest(digest: PreDigest) -> Self {
DigestItem::PreRuntime(BABE_ENGINE_ID, digest.encode())
@@ -17,7 +17,7 @@
//! Inherents for BABE
use sp_inherents::{InherentData, InherentIdentifier, Error};
use sp_inherents::{Error, InherentData, InherentIdentifier};
use sp_std::result::Result;
@@ -64,13 +64,10 @@ impl InherentDataProvider {
timestamp: sp_timestamp::Timestamp,
duration: std::time::Duration,
) -> Self {
let slot = InherentType::from(
(timestamp.as_duration().as_millis() / duration.as_millis()) as u64
);
let slot =
InherentType::from((timestamp.as_duration().as_millis() / duration.as_millis()) as u64);
Self {
slot,
}
Self { slot }
}
/// Returns the `slot` of this inherent data provider.
+10 -23
View File
@@ -30,7 +30,7 @@ pub use sp_consensus_vrf::schnorrkel::{
use codec::{Decode, Encode};
#[cfg(feature = "std")]
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};
#[cfg(feature = "std")]
use sp_keystore::vrf::{VRFTranscriptData, VRFTranscriptValue};
use sp_runtime::{traits::Header, ConsensusEngineId, RuntimeDebug};
@@ -96,11 +96,7 @@ pub type BabeAuthorityWeight = u64;
pub type BabeBlockWeight = u32;
/// Make a VRF transcript from given randomness, slot number and epoch.
pub fn make_transcript(
randomness: &Randomness,
slot: Slot,
epoch: u64,
) -> Transcript {
pub fn make_transcript(randomness: &Randomness, slot: Slot, epoch: u64) -> Transcript {
let mut transcript = Transcript::new(&BABE_ENGINE_ID);
transcript.append_u64(b"slot number", *slot);
transcript.append_u64(b"current epoch", epoch);
@@ -110,18 +106,14 @@ pub fn make_transcript(
/// Make a VRF transcript data container
#[cfg(feature = "std")]
pub fn make_transcript_data(
randomness: &Randomness,
slot: Slot,
epoch: u64,
) -> VRFTranscriptData {
pub fn make_transcript_data(randomness: &Randomness, slot: Slot, epoch: u64) -> VRFTranscriptData {
VRFTranscriptData {
label: &BABE_ENGINE_ID,
items: vec![
("slot number", VRFTranscriptValue::U64(*slot)),
("current epoch", VRFTranscriptValue::U64(epoch)),
("chain randomness", VRFTranscriptValue::Bytes(randomness.to_vec())),
]
],
}
}
@@ -280,20 +272,15 @@ where
use digests::*;
use sp_application_crypto::RuntimeAppPublic;
let find_pre_digest = |header: &H| {
header
.digest()
.logs()
.iter()
.find_map(|log| log.as_babe_pre_digest())
};
let find_pre_digest =
|header: &H| header.digest().logs().iter().find_map(|log| log.as_babe_pre_digest());
let verify_seal_signature = |mut header: H, offender: &AuthorityId| {
let seal = header.digest_mut().pop()?.as_babe_seal()?;
let pre_hash = header.hash();
if !offender.verify(&pre_hash.as_ref(), &seal) {
return None;
return None
}
Some(())
@@ -302,7 +289,7 @@ where
let verify_proof = || {
// we must have different headers for the equivocation to be valid
if proof.first_header.hash() == proof.second_header.hash() {
return None;
return None
}
let first_pre_digest = find_pre_digest(&proof.first_header)?;
@@ -313,12 +300,12 @@ where
if proof.slot != first_pre_digest.slot() ||
first_pre_digest.slot() != second_pre_digest.slot()
{
return None;
return None
}
// both headers must have been authored by the same authority
if first_pre_digest.authority_index() != second_pre_digest.authority_index() {
return None;
return None
}
// we finally verify that the expected authority has signed both headers and
@@ -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.
@@ -19,9 +19,9 @@
#![cfg_attr(not(feature = "std"), no_std)]
use sp_std::vec::Vec;
use sp_runtime::ConsensusEngineId;
use codec::Decode;
use sp_runtime::ConsensusEngineId;
use sp_std::vec::Vec;
/// The `ConsensusEngineId` of PoW.
pub const POW_ENGINE_ID: ConsensusEngineId = [b'p', b'o', b'w', b'_'];
@@ -17,13 +17,19 @@
//! Schnorrkel-based VRF.
use codec::{Encode, Decode, EncodeLike};
use sp_std::{convert::TryFrom, prelude::*};
use sp_core::U512;
use sp_std::ops::{Deref, DerefMut};
use codec::{Decode, Encode, EncodeLike};
use schnorrkel::errors::MultiSignatureStage;
use sp_core::U512;
use sp_std::{
convert::TryFrom,
ops::{Deref, DerefMut},
prelude::*,
};
pub use schnorrkel::{SignatureError, PublicKey, vrf::{VRF_PROOF_LENGTH, VRF_OUTPUT_LENGTH}};
pub use schnorrkel::{
vrf::{VRF_OUTPUT_LENGTH, VRF_PROOF_LENGTH},
PublicKey, SignatureError,
};
/// The length of the Randomness.
pub const RANDOMNESS_LENGTH: usize = VRF_OUTPUT_LENGTH;
@@ -34,11 +40,15 @@ pub struct VRFOutput(pub schnorrkel::vrf::VRFOutput);
impl Deref for VRFOutput {
type Target = schnorrkel::vrf::VRFOutput;
fn deref(&self) -> &Self::Target { &self.0 }
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for VRFOutput {
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 }
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl Encode for VRFOutput {
@@ -47,7 +57,7 @@ impl Encode for VRFOutput {
}
}
impl EncodeLike for VRFOutput { }
impl EncodeLike for VRFOutput {}
impl Decode for VRFOutput {
fn decode<R: codec::Input>(i: &mut R) -> Result<Self, codec::Error> {
@@ -82,11 +92,15 @@ impl Ord for VRFProof {
impl Deref for VRFProof {
type Target = schnorrkel::vrf::VRFProof;
fn deref(&self) -> &Self::Target { &self.0 }
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for VRFProof {
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 }
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl Encode for VRFProof {
@@ -95,7 +109,7 @@ impl Encode for VRFProof {
}
}
impl EncodeLike for VRFProof { }
impl EncodeLike for VRFProof {}
impl Decode for VRFProof {
fn decode<R: codec::Input>(i: &mut R) -> Result<Self, codec::Error> {
@@ -113,8 +127,8 @@ impl TryFrom<[u8; VRF_PROOF_LENGTH]> for VRFProof {
}
fn convert_error(e: SignatureError) -> codec::Error {
use SignatureError::*;
use MultiSignatureStage::*;
use SignatureError::*;
match e {
EquationFalse => "Signature error: `EquationFalse`".into(),
PointDecompressionError => "Signature error: `PointDecompressionError`".into(),