mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-15 20:51:05 +00:00
Rework inherent data client side (#8526)
* Lol * Yeah * Moare * adaasda * Convert AURA to new pallet macro * AURA: Switch to `CurrentSlot` instead of `LastTimestamp` This switches AURA to use `CurrentSlot` instead of `LastTimestamp`. * Add missing file * Update frame/aura/src/migrations.rs Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com> * Remove the runtime side provide inherent code * Use correct weight * Add TODO * Remove the Inherent from AURA * 🤦 * Remove unused stuff * Update primitives authorship * Fix babe inherent data provider * Fix consensus-uncles * Fix BABE * Do some further changes to authorship primitives... :D * More work * Make it compile the happy path * Make it async! * Take hash * More stuff * Hacks * Revert "Hacks" This reverts commit cfffad88668cfdebf632a59c4fbfada001ef8251. * Fix * Make `execute_block` return the final block header * Move Aura digest stuff * Make it possible to disable equivocation checking * Fix fix fix * Some refactorings * Comment * Fixes fixes fixes * More cleanups * Some love * Better love * Make slot duration being exposed as `Duration` to the outside * Some slot info love * Add `build_aura_worker` utility function * Copy copy copy * Some stuff * Start fixing pow * Fix pow * Remove some bounds * More work * Make grandpa work * Make slots use `async_trait` * Introduce `SharedData` * Add test and fix bugs * Switch to `SharedData` * Make grandpa tests working * More Babe work * Make grandpa work * Introduce `SharedData` * Add test and fix bugs * Switch to `SharedData` * Make grandpa tests working * More Babe work * Make it async * Fix fix * Use `async_trait` in sc-consensus-slots This makes the code a little bit easier to read and also expresses that there can always only be one call at a time to `on_slot`. * Make grandpa tests compile * More Babe tests work * Fix network test * Start fixing service test * Finish service-test * Fix sc-consensus-aura * Fix fix fix * More fixes * Make everything compile *yeah* * Make manual-seal compile * More fixes * Start fixing Aura * Fix Aura tests * Fix Babe tests * Make everything compile * Move code around and switch to async_trait * Fix Babe * Docs docs docs * Move to FRAME * Fix fix fix * Make everything compile * Last cleanups * Fix integration test * Change slot usage of the timestamp * We really need to switch to `impl-trait-for-tuples` * Update primitives/inherents/src/lib.rs Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com> * Update primitives/inherents/src/lib.rs Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com> * Update primitives/inherents/src/lib.rs Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com> * Some extra logging * Remove dbg! * Update primitives/consensus/common/src/import_queue/basic_queue.rs Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com> Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>
This commit is contained in:
@@ -33,7 +33,6 @@ sp-version = { version = "3.0.0", path = "../../../primitives/version" }
|
||||
sc-consensus-slots = { version = "0.9.0", path = "../slots" }
|
||||
sp-api = { version = "3.0.0", path = "../../../primitives/api" }
|
||||
sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" }
|
||||
sp-timestamp = { version = "3.0.0", path = "../../../primitives/timestamp" }
|
||||
sp-keystore = { version = "0.9.0", path = "../../../primitives/keystore" }
|
||||
sc-telemetry = { version = "3.0.0", path = "../../telemetry" }
|
||||
prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../../utils/prometheus", version = "0.9.0"}
|
||||
@@ -43,6 +42,7 @@ async-trait = "0.1.42"
|
||||
getrandom = { version = "0.2", features = ["js"], optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
sp-timestamp = { version = "3.0.0", path = "../../../primitives/timestamp" }
|
||||
sp-keyring = { version = "3.0.0", path = "../../../primitives/keyring" }
|
||||
sp-tracing = { version = "3.0.0", path = "../../../primitives/tracing" }
|
||||
sc-executor = { version = "0.9.0", path = "../../executor" }
|
||||
|
||||
@@ -18,19 +18,15 @@
|
||||
|
||||
//! Module implementing the logic for verifying and importing AuRa blocks.
|
||||
|
||||
use crate::{
|
||||
AuthorityId, find_pre_digest, slot_author, aura_err, Error, AuraSlotCompatible, SlotDuration,
|
||||
register_aura_inherent_data_provider, authorities,
|
||||
};
|
||||
use crate::{AuthorityId, find_pre_digest, slot_author, aura_err, Error, authorities};
|
||||
use std::{
|
||||
sync::Arc, time::Duration, thread, marker::PhantomData, hash::Hash, fmt::Debug,
|
||||
collections::HashMap,
|
||||
sync::Arc, marker::PhantomData, hash::Hash, fmt::Debug, collections::HashMap,
|
||||
};
|
||||
use log::{debug, info, trace};
|
||||
use prometheus_endpoint::Registry;
|
||||
use codec::{Encode, Decode, Codec};
|
||||
use sp_consensus::{
|
||||
BlockImport, CanAuthorWith, ForkChoiceStrategy, BlockImportParams, SlotData,
|
||||
BlockImport, CanAuthorWith, ForkChoiceStrategy, BlockImportParams,
|
||||
BlockOrigin, Error as ConsensusError, BlockCheckParams, ImportResult,
|
||||
import_queue::{
|
||||
Verifier, BasicQueue, DefaultImportQueue, BoxJustificationImport,
|
||||
@@ -43,10 +39,9 @@ use sp_runtime::{generic::{BlockId, OpaqueDigestItemId}, Justifications};
|
||||
use sp_runtime::traits::{Block as BlockT, Header, DigestItemFor, Zero};
|
||||
use sp_api::ProvideRuntimeApi;
|
||||
use sp_core::crypto::Pair;
|
||||
use sp_inherents::{InherentDataProviders, InherentData};
|
||||
use sp_timestamp::InherentError as TIError;
|
||||
use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_TRACE, CONSENSUS_DEBUG, CONSENSUS_INFO};
|
||||
use sc_consensus_slots::{CheckedHeader, SlotCompatible, check_equivocation};
|
||||
use sp_inherents::{CreateInherentDataProviders, InherentDataProvider as _};
|
||||
use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_TRACE, CONSENSUS_DEBUG};
|
||||
use sc_consensus_slots::{CheckedHeader, check_equivocation, InherentDataProviderExt};
|
||||
use sp_consensus_slots::Slot;
|
||||
use sp_api::ApiExt;
|
||||
use sp_consensus_aura::{
|
||||
@@ -118,26 +113,26 @@ fn check_header<C, B: BlockT, P: Pair>(
|
||||
}
|
||||
|
||||
/// A verifier for Aura blocks.
|
||||
pub struct AuraVerifier<C, P, CAW> {
|
||||
pub struct AuraVerifier<C, P, CAW, IDP> {
|
||||
client: Arc<C>,
|
||||
phantom: PhantomData<P>,
|
||||
inherent_data_providers: InherentDataProviders,
|
||||
create_inherent_data_providers: IDP,
|
||||
can_author_with: CAW,
|
||||
check_for_equivocation: CheckForEquivocation,
|
||||
telemetry: Option<TelemetryHandle>,
|
||||
}
|
||||
|
||||
impl<C, P, CAW> AuraVerifier<C, P, CAW> {
|
||||
impl<C, P, CAW, IDP> AuraVerifier<C, P, CAW, IDP> {
|
||||
pub(crate) fn new(
|
||||
client: Arc<C>,
|
||||
inherent_data_providers: InherentDataProviders,
|
||||
create_inherent_data_providers: IDP,
|
||||
can_author_with: CAW,
|
||||
check_for_equivocation: CheckForEquivocation,
|
||||
telemetry: Option<TelemetryHandle>,
|
||||
) -> Self {
|
||||
Self {
|
||||
client,
|
||||
inherent_data_providers,
|
||||
create_inherent_data_providers,
|
||||
can_author_with,
|
||||
check_for_equivocation,
|
||||
telemetry,
|
||||
@@ -146,22 +141,22 @@ impl<C, P, CAW> AuraVerifier<C, P, CAW> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<C, P, CAW> AuraVerifier<C, P, CAW> where
|
||||
impl<C, P, CAW, IDP> AuraVerifier<C, P, CAW, IDP> where
|
||||
P: Send + Sync + 'static,
|
||||
CAW: Send + Sync + 'static,
|
||||
IDP: Send,
|
||||
{
|
||||
fn check_inherents<B: BlockT>(
|
||||
async fn check_inherents<B: BlockT>(
|
||||
&self,
|
||||
block: B,
|
||||
block_id: BlockId<B>,
|
||||
inherent_data: InherentData,
|
||||
timestamp_now: u64,
|
||||
inherent_data: sp_inherents::InherentData,
|
||||
create_inherent_data_providers: IDP::InherentDataProviders,
|
||||
) -> Result<(), Error<B>> where
|
||||
C: ProvideRuntimeApi<B>, C::Api: BlockBuilderApi<B>,
|
||||
CAW: CanAuthorWith<B>,
|
||||
IDP: CreateInherentDataProviders<B, ()>,
|
||||
{
|
||||
const MAX_TIMESTAMP_DRIFT_SECS: u64 = 60;
|
||||
|
||||
if let Err(e) = self.can_author_with.can_author_with(&block_id) {
|
||||
debug!(
|
||||
target: "aura",
|
||||
@@ -179,44 +174,20 @@ impl<C, P, CAW> AuraVerifier<C, P, CAW> where
|
||||
).map_err(|e| Error::Client(e.into()))?;
|
||||
|
||||
if !inherent_res.ok() {
|
||||
inherent_res
|
||||
.into_errors()
|
||||
.try_for_each(|(i, e)| match TIError::try_from(&i, &e) {
|
||||
Some(TIError::ValidAtTimestamp(timestamp)) => {
|
||||
// halt import until timestamp is valid.
|
||||
// reject when too far ahead.
|
||||
if timestamp > timestamp_now + MAX_TIMESTAMP_DRIFT_SECS {
|
||||
return Err(Error::TooFarInFuture);
|
||||
}
|
||||
|
||||
let diff = timestamp.saturating_sub(timestamp_now);
|
||||
info!(
|
||||
target: "aura",
|
||||
"halting for block {} seconds in the future",
|
||||
diff
|
||||
);
|
||||
telemetry!(
|
||||
self.telemetry;
|
||||
CONSENSUS_INFO;
|
||||
"aura.halting_for_future_block";
|
||||
"diff" => ?diff
|
||||
);
|
||||
thread::sleep(Duration::from_secs(diff));
|
||||
Ok(())
|
||||
},
|
||||
Some(TIError::Other(e)) => Err(Error::Runtime(e.into())),
|
||||
None => Err(Error::DataProvider(
|
||||
self.inherent_data_providers.error_to_string(&i, &e)
|
||||
)),
|
||||
})
|
||||
} else {
|
||||
Ok(())
|
||||
for (i, e) in inherent_res.into_errors() {
|
||||
match create_inherent_data_providers.try_handle_error(&i, &e).await {
|
||||
Some(res) => res.map_err(Error::Inherent)?,
|
||||
None => return Err(Error::UnknownInherentError(i)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<B: BlockT, C, P, CAW> Verifier<B> for AuraVerifier<C, P, CAW> where
|
||||
impl<B: BlockT, C, P, CAW, IDP> Verifier<B> for AuraVerifier<C, P, CAW, IDP> where
|
||||
C: ProvideRuntimeApi<B> +
|
||||
Send +
|
||||
Sync +
|
||||
@@ -229,6 +200,8 @@ impl<B: BlockT, C, P, CAW> Verifier<B> for AuraVerifier<C, P, CAW> where
|
||||
P::Public: Send + Sync + Hash + Eq + Clone + Decode + Encode + Debug + 'static,
|
||||
P::Signature: Encode + Decode,
|
||||
CAW: CanAuthorWith<B> + Send + Sync + 'static,
|
||||
IDP: CreateInherentDataProviders<B, ()> + Send + Sync,
|
||||
IDP::InherentDataProviders: InherentDataProviderExt + Send + Sync,
|
||||
{
|
||||
async fn verify(
|
||||
&mut self,
|
||||
@@ -237,16 +210,24 @@ impl<B: BlockT, C, P, CAW> Verifier<B> for AuraVerifier<C, P, CAW> where
|
||||
justifications: Option<Justifications>,
|
||||
mut body: Option<Vec<B::Extrinsic>>,
|
||||
) -> Result<(BlockImportParams<B, ()>, Option<Vec<(CacheKeyId, Vec<u8>)>>), String> {
|
||||
let mut inherent_data = self.inherent_data_providers
|
||||
.create_inherent_data()
|
||||
.map_err(|e| e.into_string())?;
|
||||
let (timestamp_now, slot_now, _) = AuraSlotCompatible.extract_timestamp_and_slot(&inherent_data)
|
||||
.map_err(|e| format!("Could not extract timestamp and slot: {:?}", e))?;
|
||||
let hash = header.hash();
|
||||
let parent_hash = *header.parent_hash();
|
||||
let authorities = authorities(self.client.as_ref(), &BlockId::Hash(parent_hash))
|
||||
.map_err(|e| format!("Could not fetch authorities at {:?}: {:?}", parent_hash, e))?;
|
||||
|
||||
let create_inherent_data_providers = self.create_inherent_data_providers
|
||||
.create_inherent_data_providers(
|
||||
parent_hash,
|
||||
(),
|
||||
)
|
||||
.await
|
||||
.map_err(|e| Error::<B>::Client(sp_blockchain::Error::Application(e)))?;
|
||||
|
||||
let mut inherent_data = create_inherent_data_providers.create_inherent_data()
|
||||
.map_err(Error::<B>::Inherent)?;
|
||||
|
||||
let slot_now = create_inherent_data_providers.slot();
|
||||
|
||||
// we add one to allow for some small drift.
|
||||
// FIXME #1019 in the future, alter this queue to allow deferring of
|
||||
// headers
|
||||
@@ -264,9 +245,10 @@ impl<B: BlockT, C, P, CAW> Verifier<B> for AuraVerifier<C, P, CAW> where
|
||||
// to check that the internally-set timestamp in the inherents
|
||||
// actually matches the slot set in the seal.
|
||||
if let Some(inner_body) = body.take() {
|
||||
inherent_data.aura_replace_inherent_data(slot);
|
||||
let block = B::new(pre_header.clone(), inner_body);
|
||||
|
||||
inherent_data.aura_replace_inherent_data(slot);
|
||||
|
||||
// skip the inherents verification if the runtime API is old.
|
||||
if self.client
|
||||
.runtime_api()
|
||||
@@ -280,8 +262,8 @@ impl<B: BlockT, C, P, CAW> Verifier<B> for AuraVerifier<C, P, CAW> where
|
||||
block.clone(),
|
||||
BlockId::Hash(parent_hash),
|
||||
inherent_data,
|
||||
*timestamp_now,
|
||||
).map_err(|e| e.to_string())?;
|
||||
create_inherent_data_providers,
|
||||
).await.map_err(|e| e.to_string())?;
|
||||
}
|
||||
|
||||
let (_, inner_body) = block.deconstruct();
|
||||
@@ -480,15 +462,15 @@ impl Default for CheckForEquivocation {
|
||||
}
|
||||
|
||||
/// Parameters of [`import_queue`].
|
||||
pub struct ImportQueueParams<'a, Block, I, C, S, CAW> {
|
||||
pub struct ImportQueueParams<'a, Block, I, C, S, CAW, CIDP> {
|
||||
/// The block import to use.
|
||||
pub block_import: I,
|
||||
/// The justification import.
|
||||
pub justification_import: Option<BoxJustificationImport<Block>>,
|
||||
/// The client to interact with the chain.
|
||||
pub client: Arc<C>,
|
||||
/// The inherent data provider, to create the inherent data.
|
||||
pub inherent_data_providers: InherentDataProviders,
|
||||
/// Something that can create the inherent data providers.
|
||||
pub create_inherent_data_providers: CIDP,
|
||||
/// The spawner to spawn background tasks.
|
||||
pub spawner: &'a S,
|
||||
/// The prometheus registry.
|
||||
@@ -497,26 +479,23 @@ pub struct ImportQueueParams<'a, Block, I, C, S, CAW> {
|
||||
pub can_author_with: CAW,
|
||||
/// Should we check for equivocation?
|
||||
pub check_for_equivocation: CheckForEquivocation,
|
||||
/// The duration of one slot.
|
||||
pub slot_duration: SlotDuration,
|
||||
/// Telemetry instance used to report telemetry metrics.
|
||||
pub telemetry: Option<TelemetryHandle>,
|
||||
}
|
||||
|
||||
/// Start an import queue for the Aura consensus algorithm.
|
||||
pub fn import_queue<'a, P, Block, I, C, S, CAW>(
|
||||
pub fn import_queue<'a, P, Block, I, C, S, CAW, CIDP>(
|
||||
ImportQueueParams {
|
||||
block_import,
|
||||
justification_import,
|
||||
client,
|
||||
inherent_data_providers,
|
||||
create_inherent_data_providers,
|
||||
spawner,
|
||||
registry,
|
||||
can_author_with,
|
||||
check_for_equivocation,
|
||||
slot_duration,
|
||||
telemetry,
|
||||
}: ImportQueueParams<'a, Block, I, C, S, CAW>
|
||||
}: ImportQueueParams<'a, Block, I, C, S, CAW, CIDP>
|
||||
) -> Result<DefaultImportQueue<Block, C>, sp_consensus::Error> where
|
||||
Block: BlockT,
|
||||
C::Api: BlockBuilderApi<Block> + AuraApi<Block, AuthorityId<P>> + ApiExt<Block>,
|
||||
@@ -538,13 +517,14 @@ pub fn import_queue<'a, P, Block, I, C, S, CAW>(
|
||||
P::Signature: Encode + Decode,
|
||||
S: sp_core::traits::SpawnEssentialNamed,
|
||||
CAW: CanAuthorWith<Block> + Send + Sync + 'static,
|
||||
CIDP: CreateInherentDataProviders<Block, ()> + Sync + Send + 'static,
|
||||
CIDP::InherentDataProviders: InherentDataProviderExt + Send + Sync,
|
||||
{
|
||||
register_aura_inherent_data_provider(&inherent_data_providers, slot_duration.slot_duration())?;
|
||||
initialize_authorities_cache(&*client)?;
|
||||
|
||||
let verifier = AuraVerifier::<_, P, _>::new(
|
||||
let verifier = AuraVerifier::<_, P, _, _>::new(
|
||||
client,
|
||||
inherent_data_providers,
|
||||
create_inherent_data_providers,
|
||||
can_author_with,
|
||||
check_for_equivocation,
|
||||
telemetry,
|
||||
|
||||
@@ -31,7 +31,8 @@
|
||||
//! NOTE: Aura itself is designed to be generic over the crypto used.
|
||||
#![forbid(missing_docs, unsafe_code)]
|
||||
use std::{
|
||||
sync::Arc, marker::PhantomData, hash::Hash, fmt::Debug, pin::Pin, convert::{TryFrom, TryInto},
|
||||
sync::Arc, marker::PhantomData, hash::Hash, fmt::Debug, pin::Pin,
|
||||
convert::{TryFrom, TryInto},
|
||||
};
|
||||
|
||||
use futures::prelude::*;
|
||||
@@ -41,7 +42,7 @@ use codec::{Encode, Decode, Codec};
|
||||
|
||||
use sp_consensus::{
|
||||
BlockImport, Environment, Proposer, CanAuthorWith, ForkChoiceStrategy, BlockImportParams,
|
||||
BlockOrigin, Error as ConsensusError, SelectChain, SlotData,
|
||||
BlockOrigin, Error as ConsensusError, SelectChain,
|
||||
};
|
||||
use sc_client_api::{backend::AuxStore, BlockOf};
|
||||
use sp_blockchain::{Result as CResult, well_known_cache_keys, ProvideCache, HeaderBackend};
|
||||
@@ -52,10 +53,11 @@ use sp_runtime::traits::{Block as BlockT, Header, DigestItemFor, Zero, Member};
|
||||
use sp_api::ProvideRuntimeApi;
|
||||
use sp_core::crypto::Pair;
|
||||
use sp_keystore::{SyncCryptoStorePtr, SyncCryptoStore};
|
||||
use sp_inherents::{InherentDataProviders, InherentData};
|
||||
use sp_timestamp::TimestampInherentData;
|
||||
use sc_consensus_slots::{SlotInfo, SlotCompatible, StorageChanges, BackoffAuthoringBlocksStrategy};
|
||||
use sp_inherents::CreateInherentDataProviders;
|
||||
use sc_telemetry::TelemetryHandle;
|
||||
use sc_consensus_slots::{
|
||||
SlotInfo, BackoffAuthoringBlocksStrategy, InherentDataProviderExt, StorageChanges,
|
||||
};
|
||||
use sp_consensus_slots::Slot;
|
||||
|
||||
mod import_queue;
|
||||
@@ -64,7 +66,7 @@ pub use sp_consensus_aura::{
|
||||
ConsensusLog, AuraApi, AURA_ENGINE_ID, digests::CompatibleDigestItem,
|
||||
inherents::{
|
||||
InherentType as AuraInherent,
|
||||
AuraInherentData, INHERENT_IDENTIFIER, InherentDataProvider,
|
||||
INHERENT_IDENTIFIER, InherentDataProvider,
|
||||
},
|
||||
};
|
||||
pub use sp_consensus::SyncOracle;
|
||||
@@ -103,24 +105,8 @@ fn slot_author<P: Pair>(slot: Slot, authorities: &[AuthorityId<P>]) -> Option<&A
|
||||
Some(current_author)
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
||||
struct AuraSlotCompatible;
|
||||
|
||||
impl SlotCompatible for AuraSlotCompatible {
|
||||
fn extract_timestamp_and_slot(
|
||||
&self,
|
||||
data: &InherentData,
|
||||
) -> Result<(sp_timestamp::Timestamp, AuraInherent, std::time::Duration), sp_consensus::Error> {
|
||||
data.timestamp_inherent_data()
|
||||
.and_then(|t| data.aura_inherent_data().map(|a| (t, a)))
|
||||
.map_err(Into::into)
|
||||
.map_err(sp_consensus::Error::InherentData)
|
||||
.map(|(x, y)| (x, y, Default::default()))
|
||||
}
|
||||
}
|
||||
|
||||
/// Parameters of [`start_aura`].
|
||||
pub struct StartAuraParams<C, SC, I, PF, SO, BS, CAW> {
|
||||
pub struct StartAuraParams<C, SC, I, PF, SO, BS, CAW, IDP> {
|
||||
/// The duration of a slot.
|
||||
pub slot_duration: SlotDuration,
|
||||
/// The client to interact with the chain.
|
||||
@@ -133,8 +119,8 @@ pub struct StartAuraParams<C, SC, I, PF, SO, BS, CAW> {
|
||||
pub proposer_factory: PF,
|
||||
/// The sync oracle that can give us the current sync status.
|
||||
pub sync_oracle: SO,
|
||||
/// The inherent data providers to create the inherent data.
|
||||
pub inherent_data_providers: InherentDataProviders,
|
||||
/// Something that can create the inherent data providers.
|
||||
pub create_inherent_data_providers: IDP,
|
||||
/// Should we force the authoring of blocks?
|
||||
pub force_authoring: bool,
|
||||
/// The backoff strategy when we miss slots.
|
||||
@@ -154,7 +140,7 @@ pub struct StartAuraParams<C, SC, I, PF, SO, BS, CAW> {
|
||||
}
|
||||
|
||||
/// Start the aura worker. The returned future should be run in a futures executor.
|
||||
pub fn start_aura<P, B, C, SC, PF, I, SO, CAW, BS, Error>(
|
||||
pub fn start_aura<P, B, C, SC, PF, I, SO, CAW, BS, Error, IDP>(
|
||||
StartAuraParams {
|
||||
slot_duration,
|
||||
client,
|
||||
@@ -162,14 +148,14 @@ pub fn start_aura<P, B, C, SC, PF, I, SO, CAW, BS, Error>(
|
||||
block_import,
|
||||
proposer_factory,
|
||||
sync_oracle,
|
||||
inherent_data_providers,
|
||||
create_inherent_data_providers,
|
||||
force_authoring,
|
||||
backoff_authoring_blocks,
|
||||
keystore,
|
||||
can_author_with,
|
||||
block_proposal_slot_portion,
|
||||
telemetry,
|
||||
}: StartAuraParams<C, SC, I, PF, SO, BS, CAW>,
|
||||
}: StartAuraParams<C, SC, I, PF, SO, BS, CAW, IDP>,
|
||||
) -> Result<impl Future<Output = ()>, sp_consensus::Error> where
|
||||
B: BlockT,
|
||||
C: ProvideRuntimeApi<B> + BlockOf + ProvideCache<B> + AuxStore + HeaderBackend<B> + Send + Sync,
|
||||
@@ -185,6 +171,8 @@ pub fn start_aura<P, B, C, SC, PF, I, SO, CAW, BS, Error>(
|
||||
SO: SyncOracle + Send + Sync + Clone,
|
||||
CAW: CanAuthorWith<B> + Send,
|
||||
BS: BackoffAuthoringBlocksStrategy<NumberFor<B>> + Send + 'static,
|
||||
IDP: CreateInherentDataProviders<B, ()> + Send,
|
||||
IDP::InherentDataProviders: InherentDataProviderExt + Send,
|
||||
{
|
||||
let worker = build_aura_worker::<P, _, _, _, _, _, _, _>(BuildAuraWorkerParams {
|
||||
client: client.clone(),
|
||||
@@ -198,18 +186,12 @@ pub fn start_aura<P, B, C, SC, PF, I, SO, CAW, BS, Error>(
|
||||
block_proposal_slot_portion,
|
||||
});
|
||||
|
||||
register_aura_inherent_data_provider(
|
||||
&inherent_data_providers,
|
||||
slot_duration.slot_duration()
|
||||
)?;
|
||||
|
||||
Ok(sc_consensus_slots::start_slot_worker::<_, _, _, _, _, AuraSlotCompatible, _, _>(
|
||||
Ok(sc_consensus_slots::start_slot_worker(
|
||||
slot_duration,
|
||||
select_chain,
|
||||
worker,
|
||||
sync_oracle,
|
||||
inherent_data_providers,
|
||||
AuraSlotCompatible,
|
||||
create_inherent_data_providers,
|
||||
can_author_with,
|
||||
))
|
||||
}
|
||||
@@ -278,8 +260,8 @@ pub fn build_aura_worker<P, B, C, PF, I, SO, BS, Error>(
|
||||
force_authoring,
|
||||
backoff_authoring_blocks,
|
||||
telemetry,
|
||||
_key_type: PhantomData::<P>,
|
||||
block_proposal_slot_portion,
|
||||
_key_type: PhantomData::<P>,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -452,8 +434,7 @@ where
|
||||
|
||||
fn proposing_remaining_duration(
|
||||
&self,
|
||||
head: &B::Header,
|
||||
slot_info: &SlotInfo,
|
||||
slot_info: &SlotInfo<B>,
|
||||
) -> std::time::Duration {
|
||||
let max_proposing = slot_info.duration.mul_f32(self.block_proposal_slot_portion.get());
|
||||
|
||||
@@ -464,11 +445,11 @@ where
|
||||
let slot_remaining = std::cmp::min(slot_remaining, max_proposing);
|
||||
|
||||
// If parent is genesis block, we don't require any lenience factor.
|
||||
if head.number().is_zero() {
|
||||
if slot_info.chain_head.number().is_zero() {
|
||||
return slot_remaining
|
||||
}
|
||||
|
||||
let parent_slot = match find_pre_digest::<B, P::Signature>(head) {
|
||||
let parent_slot = match find_pre_digest::<B, P::Signature>(&slot_info.chain_head) {
|
||||
Err(_) => return slot_remaining,
|
||||
Ok(d) => d,
|
||||
};
|
||||
@@ -509,15 +490,15 @@ enum Error<B: BlockT> {
|
||||
SlotAuthorNotFound,
|
||||
#[display(fmt = "Bad signature on {:?}", _0)]
|
||||
BadSignature(B::Hash),
|
||||
#[display(fmt = "Rejecting block too far in future")]
|
||||
TooFarInFuture,
|
||||
Client(sp_blockchain::Error),
|
||||
DataProvider(String),
|
||||
Runtime(String),
|
||||
#[display(fmt = "Slot number must increase: parent slot: {}, this slot: {}", _0, _1)]
|
||||
SlotMustIncrease(Slot, Slot),
|
||||
#[display(fmt = "Parent ({}) of {} unavailable. Cannot import", _0, _1)]
|
||||
ParentUnavailable(B::Hash, B::Hash),
|
||||
#[display(fmt = "Unknown inherent error for identifier: {}", "String::from_utf8_lossy(_0)")]
|
||||
UnknownInherentError(sp_inherents::InherentIdentifier),
|
||||
#[display(fmt = "Inherent error: {}", _0)]
|
||||
Inherent(sp_inherents::Error),
|
||||
}
|
||||
|
||||
impl<B: BlockT> std::convert::From<Error<B>> for String {
|
||||
@@ -543,21 +524,6 @@ fn find_pre_digest<B: BlockT, Signature: Codec>(header: &B::Header) -> Result<Sl
|
||||
pre_digest.ok_or_else(|| aura_err(Error::NoDigestFound))
|
||||
}
|
||||
|
||||
/// Register the aura inherent data provider, if not registered already.
|
||||
fn register_aura_inherent_data_provider(
|
||||
inherent_data_providers: &InherentDataProviders,
|
||||
slot_duration: std::time::Duration,
|
||||
) -> Result<(), sp_consensus::Error> {
|
||||
if !inherent_data_providers.has_provider(&INHERENT_IDENTIFIER) {
|
||||
inherent_data_providers
|
||||
.register_provider(InherentDataProvider::new(slot_duration))
|
||||
.map_err(Into::into)
|
||||
.map_err(sp_consensus::Error::InherentData)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn authorities<A, B, C>(client: &C, at: &BlockId<B>) -> Result<Vec<A>, ConsensusError> where
|
||||
A: Codec + Debug,
|
||||
B: BlockT,
|
||||
@@ -580,7 +546,7 @@ mod tests {
|
||||
use super::*;
|
||||
use sp_consensus::{
|
||||
NoNetwork as DummyOracle, Proposal, AlwaysCanAuthor, DisableProofRecording,
|
||||
import_queue::BoxJustificationImport,
|
||||
import_queue::BoxJustificationImport, SlotData,
|
||||
};
|
||||
use sc_network_test::{Block as TestBlock, *};
|
||||
use sp_runtime::traits::{Block as BlockT, DigestFor};
|
||||
@@ -596,6 +562,8 @@ mod tests {
|
||||
use substrate_test_runtime_client::{TestClient, runtime::{Header, H256}};
|
||||
use sc_keystore::LocalKeystore;
|
||||
use sp_application_crypto::key_types::AURA;
|
||||
use sp_inherents::InherentData;
|
||||
use sp_timestamp::InherentDataProvider as TimestampInherentDataProvider;
|
||||
|
||||
type Error = sp_blockchain::Error;
|
||||
|
||||
@@ -643,7 +611,16 @@ mod tests {
|
||||
|
||||
const SLOT_DURATION: u64 = 1000;
|
||||
|
||||
type AuraVerifier = import_queue::AuraVerifier<PeersFullClient, AuthorityPair, AlwaysCanAuthor>;
|
||||
type AuraVerifier = import_queue::AuraVerifier<
|
||||
PeersFullClient,
|
||||
AuthorityPair,
|
||||
AlwaysCanAuthor,
|
||||
Box<dyn CreateInherentDataProviders<
|
||||
TestBlock,
|
||||
(),
|
||||
InherentDataProviders = (TimestampInherentDataProvider, InherentDataProvider)
|
||||
>>
|
||||
>;
|
||||
type AuraPeer = Peer<(), PeersClient>;
|
||||
|
||||
pub struct AuraTestNet {
|
||||
@@ -668,16 +645,19 @@ mod tests {
|
||||
match client {
|
||||
PeersClient::Full(client, _) => {
|
||||
let slot_duration = slot_duration(&*client).expect("slot duration available");
|
||||
let inherent_data_providers = InherentDataProviders::new();
|
||||
register_aura_inherent_data_provider(
|
||||
&inherent_data_providers,
|
||||
slot_duration.slot_duration()
|
||||
).expect("Registers aura inherent data provider");
|
||||
|
||||
assert_eq!(slot_duration.slot_duration().as_millis() as u64, SLOT_DURATION);
|
||||
import_queue::AuraVerifier::new(
|
||||
client,
|
||||
inherent_data_providers,
|
||||
Box::new(|_, _| async {
|
||||
let timestamp = TimestampInherentDataProvider::from_system_time();
|
||||
let slot = InherentDataProvider::from_timestamp_and_duration(
|
||||
*timestamp,
|
||||
Duration::from_secs(6),
|
||||
);
|
||||
|
||||
Ok((timestamp, slot))
|
||||
}),
|
||||
AlwaysCanAuthor,
|
||||
CheckForEquivocation::Yes,
|
||||
None,
|
||||
@@ -746,19 +726,22 @@ mod tests {
|
||||
|
||||
let slot_duration = slot_duration(&*client).expect("slot duration available");
|
||||
|
||||
let inherent_data_providers = InherentDataProviders::new();
|
||||
register_aura_inherent_data_provider(
|
||||
&inherent_data_providers, slot_duration.slot_duration()
|
||||
).expect("Registers aura inherent data provider");
|
||||
|
||||
aura_futures.push(start_aura::<AuthorityPair, _, _, _, _, _, _, _, _, _>(StartAuraParams {
|
||||
aura_futures.push(start_aura::<AuthorityPair, _, _, _, _, _, _, _, _, _, _>(StartAuraParams {
|
||||
slot_duration,
|
||||
block_import: client.clone(),
|
||||
select_chain,
|
||||
client,
|
||||
proposer_factory: environ,
|
||||
sync_oracle: DummyOracle,
|
||||
inherent_data_providers,
|
||||
create_inherent_data_providers: |_, _| async {
|
||||
let timestamp = TimestampInherentDataProvider::from_system_time();
|
||||
let slot = InherentDataProvider::from_timestamp_and_duration(
|
||||
*timestamp,
|
||||
Duration::from_secs(6),
|
||||
);
|
||||
|
||||
Ok((timestamp, slot))
|
||||
},
|
||||
force_authoring: false,
|
||||
backoff_authoring_blocks: Some(BackoffAuthoringOnFinalizedHeadLagging::default()),
|
||||
keystore,
|
||||
@@ -881,13 +864,13 @@ mod tests {
|
||||
let head = client.header(&BlockId::Number(0)).unwrap().unwrap();
|
||||
|
||||
let res = futures::executor::block_on(worker.on_slot(
|
||||
head,
|
||||
SlotInfo {
|
||||
slot: 0.into(),
|
||||
timestamp: 0.into(),
|
||||
ends_at: Instant::now() + Duration::from_secs(100),
|
||||
inherent_data: InherentData::new(),
|
||||
duration: Duration::from_millis(1000),
|
||||
chain_head: head,
|
||||
block_size_limit: None,
|
||||
},
|
||||
)).unwrap();
|
||||
|
||||
@@ -26,7 +26,6 @@ serde = { version = "1.0.104", features = ["derive"] }
|
||||
sp-version = { version = "3.0.0", path = "../../../primitives/version" }
|
||||
sp-io = { version = "3.0.0", path = "../../../primitives/io" }
|
||||
sp-inherents = { version = "3.0.0", path = "../../../primitives/inherents" }
|
||||
sp-timestamp = { version = "3.0.0", path = "../../../primitives/timestamp" }
|
||||
sc-telemetry = { version = "3.0.0", path = "../../telemetry" }
|
||||
sc-keystore = { version = "3.0.0", path = "../../keystore" }
|
||||
sc-client-api = { version = "3.0.0", path = "../../api" }
|
||||
@@ -56,6 +55,7 @@ retain_mut = "0.1.2"
|
||||
async-trait = "0.1.42"
|
||||
|
||||
[dev-dependencies]
|
||||
sp-timestamp = { version = "3.0.0", path = "../../../primitives/timestamp" }
|
||||
sp-keyring = { version = "3.0.0", path = "../../../primitives/keyring" }
|
||||
sp-tracing = { version = "3.0.0", path = "../../../primitives/tracing" }
|
||||
sc-executor = { version = "0.9.0", path = "../../executor" }
|
||||
|
||||
@@ -77,7 +77,7 @@ pub use sp_consensus::SyncOracle;
|
||||
pub use sc_consensus_slots::SlotProportion;
|
||||
use std::{
|
||||
collections::HashMap, sync::Arc, u64, pin::Pin, borrow::Cow, convert::TryInto,
|
||||
time::{Duration, Instant},
|
||||
time::Duration,
|
||||
};
|
||||
use sp_consensus::{ImportResult, CanAuthorWith, import_queue::BoxJustificationImport};
|
||||
use sp_core::crypto::Public;
|
||||
@@ -89,7 +89,7 @@ use sp_runtime::{
|
||||
};
|
||||
use sp_api::{ProvideRuntimeApi, NumberFor};
|
||||
use parking_lot::Mutex;
|
||||
use sp_inherents::{InherentDataProviders, InherentData};
|
||||
use sp_inherents::{CreateInherentDataProviders, InherentDataProvider, InherentData};
|
||||
use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_TRACE, CONSENSUS_DEBUG};
|
||||
use sp_consensus::{
|
||||
BlockImport, Environment, Proposer, BlockCheckParams,
|
||||
@@ -97,7 +97,6 @@ use sp_consensus::{
|
||||
SelectChain, SlotData, import_queue::{Verifier, BasicQueue, DefaultImportQueue, CacheKeyId},
|
||||
};
|
||||
use sp_consensus_babe::inherents::BabeInherentData;
|
||||
use sp_timestamp::TimestampInherentData;
|
||||
use sc_client_api::{
|
||||
backend::AuxStore, BlockchainEvents, ProvideUncles,
|
||||
};
|
||||
@@ -110,8 +109,8 @@ use futures::prelude::*;
|
||||
use log::{debug, info, log, trace, warn};
|
||||
use prometheus_endpoint::Registry;
|
||||
use sc_consensus_slots::{
|
||||
SlotInfo, SlotCompatible, StorageChanges, CheckedHeader, check_equivocation,
|
||||
BackoffAuthoringBlocksStrategy
|
||||
SlotInfo, StorageChanges, CheckedHeader, check_equivocation,
|
||||
BackoffAuthoringBlocksStrategy, InherentDataProviderExt,
|
||||
};
|
||||
use sc_consensus_epochs::{
|
||||
descendent_query, SharedEpochChanges, EpochChangesFor, Epoch as EpochT, ViableEpochDescriptor,
|
||||
@@ -270,15 +269,19 @@ pub enum Error<B: BlockT> {
|
||||
/// Parent block has no associated weight
|
||||
#[display(fmt = "Parent block of {} has no associated weight", _0)]
|
||||
ParentBlockNoAssociatedWeight(B::Hash),
|
||||
/// Check inherents error
|
||||
#[display(fmt = "Checking inherents failed: {}", _0)]
|
||||
/// Check Inherents error
|
||||
CheckInherents(String),
|
||||
CheckInherents(sp_inherents::Error),
|
||||
/// Unhandled check inherents error
|
||||
#[display(fmt = "Checking inherents unhandled error: {}", "String::from_utf8_lossy(_0)")]
|
||||
CheckInherentsUnhandled(sp_inherents::InherentIdentifier),
|
||||
/// Create inherents error.
|
||||
#[display(fmt = "Creating inherents failed: {}", _0)]
|
||||
CreateInherents(sp_inherents::Error),
|
||||
/// Client error
|
||||
Client(sp_blockchain::Error),
|
||||
/// Runtime Api error.
|
||||
RuntimeApi(sp_api::ApiError),
|
||||
/// Runtime error
|
||||
Runtime(sp_inherents::Error),
|
||||
/// Fork tree error
|
||||
ForkTree(Box<fork_tree::Error<sp_blockchain::Error>>),
|
||||
}
|
||||
@@ -360,7 +363,7 @@ impl std::ops::Deref for Config {
|
||||
}
|
||||
|
||||
/// Parameters for BABE.
|
||||
pub struct BabeParams<B: BlockT, C, E, I, SO, SC, CAW, BS> {
|
||||
pub struct BabeParams<B: BlockT, C, E, I, SO, SC, CAW, BS, IDP> {
|
||||
/// The keystore that manages the keys of the node.
|
||||
pub keystore: SyncCryptoStorePtr,
|
||||
|
||||
@@ -381,8 +384,8 @@ pub struct BabeParams<B: BlockT, C, E, I, SO, SC, CAW, BS> {
|
||||
/// A sync oracle
|
||||
pub sync_oracle: SO,
|
||||
|
||||
/// Providers for inherent data.
|
||||
pub inherent_data_providers: InherentDataProviders,
|
||||
/// Something that can create the inherent data providers.
|
||||
pub create_inherent_data_providers: IDP,
|
||||
|
||||
/// Force authoring of blocks even if we are offline
|
||||
pub force_authoring: bool,
|
||||
@@ -408,21 +411,21 @@ pub struct BabeParams<B: BlockT, C, E, I, SO, SC, CAW, BS> {
|
||||
}
|
||||
|
||||
/// Start the babe worker.
|
||||
pub fn start_babe<B, C, SC, E, I, SO, CAW, BS, Error>(BabeParams {
|
||||
pub fn start_babe<B, C, SC, E, I, SO, CAW, BS, Error, IDP>(BabeParams {
|
||||
keystore,
|
||||
client,
|
||||
select_chain,
|
||||
env,
|
||||
block_import,
|
||||
sync_oracle,
|
||||
inherent_data_providers,
|
||||
create_inherent_data_providers,
|
||||
force_authoring,
|
||||
backoff_authoring_blocks,
|
||||
babe_link,
|
||||
can_author_with,
|
||||
block_proposal_slot_portion,
|
||||
telemetry,
|
||||
}: BabeParams<B, C, E, I, SO, SC, CAW, BS>) -> Result<
|
||||
}: BabeParams<B, C, E, I, SO, SC, CAW, BS, IDP>) -> Result<
|
||||
BabeWorker<B>,
|
||||
sp_consensus::Error,
|
||||
> where
|
||||
@@ -440,6 +443,8 @@ pub fn start_babe<B, C, SC, E, I, SO, CAW, BS, Error>(BabeParams {
|
||||
SO: SyncOracle + Send + Sync + Clone + 'static,
|
||||
CAW: CanAuthorWith<B> + Send + Sync + 'static,
|
||||
BS: BackoffAuthoringBlocksStrategy<NumberFor<B>> + Send + 'static,
|
||||
IDP: CreateInherentDataProviders<B, ()> + Send + Sync + 'static,
|
||||
IDP::InherentDataProviders: InherentDataProviderExt + Send,
|
||||
{
|
||||
const HANDLE_BUFFER_SIZE: usize = 1024;
|
||||
|
||||
@@ -461,21 +466,13 @@ pub fn start_babe<B, C, SC, E, I, SO, CAW, BS, Error>(BabeParams {
|
||||
telemetry,
|
||||
};
|
||||
|
||||
register_babe_inherent_data_provider(&inherent_data_providers, config.slot_duration())?;
|
||||
sc_consensus_uncles::register_uncles_inherent_data_provider(
|
||||
client.clone(),
|
||||
select_chain.clone(),
|
||||
&inherent_data_providers,
|
||||
)?;
|
||||
|
||||
info!(target: "babe", "👶 Starting BABE Authorship worker");
|
||||
let inner = sc_consensus_slots::start_slot_worker(
|
||||
config.0.clone(),
|
||||
select_chain,
|
||||
worker,
|
||||
sync_oracle,
|
||||
inherent_data_providers,
|
||||
babe_link.time_source,
|
||||
create_inherent_data_providers,
|
||||
can_author_with,
|
||||
);
|
||||
|
||||
@@ -813,23 +810,22 @@ where
|
||||
|
||||
fn proposing_remaining_duration(
|
||||
&self,
|
||||
parent_head: &B::Header,
|
||||
slot_info: &SlotInfo,
|
||||
slot_info: &SlotInfo<B>,
|
||||
) -> std::time::Duration {
|
||||
let max_proposing = slot_info.duration.mul_f32(self.block_proposal_slot_portion.get());
|
||||
|
||||
let slot_remaining = slot_info.ends_at
|
||||
.checked_duration_since(Instant::now())
|
||||
.checked_duration_since(std::time::Instant::now())
|
||||
.unwrap_or_default();
|
||||
|
||||
let slot_remaining = std::cmp::min(slot_remaining, max_proposing);
|
||||
|
||||
// If parent is genesis block, we don't require any lenience factor.
|
||||
if parent_head.number().is_zero() {
|
||||
if slot_info.chain_head.number().is_zero() {
|
||||
return slot_remaining
|
||||
}
|
||||
|
||||
let parent_slot = match find_pre_digest::<B>(parent_head) {
|
||||
let parent_slot = match find_pre_digest::<B>(&slot_info.chain_head) {
|
||||
Err(_) => return slot_remaining,
|
||||
Ok(d) => d.slot(),
|
||||
};
|
||||
@@ -913,27 +909,9 @@ fn find_next_config_digest<B: BlockT>(header: &B::Header)
|
||||
Ok(config_digest)
|
||||
}
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
struct TimeSource(Arc<Mutex<(Option<Duration>, Vec<(Instant, u64)>)>>);
|
||||
|
||||
impl SlotCompatible for TimeSource {
|
||||
fn extract_timestamp_and_slot(
|
||||
&self,
|
||||
data: &InherentData,
|
||||
) -> Result<(sp_timestamp::Timestamp, Slot, std::time::Duration), sp_consensus::Error> {
|
||||
trace!(target: "babe", "extract timestamp");
|
||||
data.timestamp_inherent_data()
|
||||
.and_then(|t| data.babe_inherent_data().map(|a| (t, a)))
|
||||
.map_err(Into::into)
|
||||
.map_err(sp_consensus::Error::InherentData)
|
||||
.map(|(x, y)| (x, y, self.0.lock().0.take().unwrap_or_default()))
|
||||
}
|
||||
}
|
||||
|
||||
/// State that must be shared between the import queue and the authoring logic.
|
||||
#[derive(Clone)]
|
||||
pub struct BabeLink<Block: BlockT> {
|
||||
time_source: TimeSource,
|
||||
epoch_changes: SharedEpochChanges<Block, Epoch>,
|
||||
config: Config,
|
||||
}
|
||||
@@ -951,30 +929,31 @@ impl<Block: BlockT> BabeLink<Block> {
|
||||
}
|
||||
|
||||
/// A verifier for Babe blocks.
|
||||
pub struct BabeVerifier<Block: BlockT, Client, SelectChain, CAW> {
|
||||
pub struct BabeVerifier<Block: BlockT, Client, SelectChain, CAW, CIDP> {
|
||||
client: Arc<Client>,
|
||||
select_chain: SelectChain,
|
||||
inherent_data_providers: sp_inherents::InherentDataProviders,
|
||||
create_inherent_data_providers: CIDP,
|
||||
config: Config,
|
||||
epoch_changes: SharedEpochChanges<Block, Epoch>,
|
||||
time_source: TimeSource,
|
||||
can_author_with: CAW,
|
||||
telemetry: Option<TelemetryHandle>,
|
||||
}
|
||||
|
||||
impl<Block, Client, SelectChain, CAW> BabeVerifier<Block, Client, SelectChain, CAW>
|
||||
impl<Block, Client, SelectChain, CAW, CIDP> BabeVerifier<Block, Client, SelectChain, CAW, CIDP>
|
||||
where
|
||||
Block: BlockT,
|
||||
Client: AuxStore + HeaderBackend<Block> + HeaderMetadata<Block> + ProvideRuntimeApi<Block>,
|
||||
Client::Api: BlockBuilderApi<Block> + BabeApi<Block>,
|
||||
SelectChain: sp_consensus::SelectChain<Block>,
|
||||
CAW: CanAuthorWith<Block>,
|
||||
CIDP: CreateInherentDataProviders<Block, ()>,
|
||||
{
|
||||
fn check_inherents(
|
||||
async fn check_inherents(
|
||||
&self,
|
||||
block: Block,
|
||||
block_id: BlockId<Block>,
|
||||
inherent_data: InherentData,
|
||||
create_inherent_data_providers: CIDP::InherentDataProviders,
|
||||
) -> Result<(), Error<Block>> {
|
||||
if let Err(e) = self.can_author_with.can_author_with(&block_id) {
|
||||
debug!(
|
||||
@@ -993,14 +972,15 @@ where
|
||||
).map_err(Error::RuntimeApi)?;
|
||||
|
||||
if !inherent_res.ok() {
|
||||
inherent_res
|
||||
.into_errors()
|
||||
.try_for_each(|(i, e)| {
|
||||
Err(Error::CheckInherents(self.inherent_data_providers.error_to_string(&i, &e)))
|
||||
})
|
||||
} else {
|
||||
Ok(())
|
||||
for (i, e) in inherent_res.into_errors() {
|
||||
match create_inherent_data_providers.try_handle_error(&i, &e).await {
|
||||
Some(res) => res.map_err(|e| Error::CheckInherents(e))?,
|
||||
None => return Err(Error::CheckInherentsUnhandled(i)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_and_report_equivocation(
|
||||
@@ -1085,8 +1065,8 @@ where
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<Block, Client, SelectChain, CAW> Verifier<Block>
|
||||
for BabeVerifier<Block, Client, SelectChain, CAW>
|
||||
impl<Block, Client, SelectChain, CAW, CIDP> Verifier<Block>
|
||||
for BabeVerifier<Block, Client, SelectChain, CAW, CIDP>
|
||||
where
|
||||
Block: BlockT,
|
||||
Client: HeaderMetadata<Block, Error = sp_blockchain::Error> + HeaderBackend<Block> + ProvideRuntimeApi<Block>
|
||||
@@ -1094,6 +1074,8 @@ where
|
||||
Client::Api: BlockBuilderApi<Block> + BabeApi<Block>,
|
||||
SelectChain: sp_consensus::SelectChain<Block>,
|
||||
CAW: CanAuthorWith<Block> + Send + Sync,
|
||||
CIDP: CreateInherentDataProviders<Block, ()> + Send + Sync,
|
||||
CIDP::InherentDataProviders: InherentDataProviderExt + Send + Sync,
|
||||
{
|
||||
async fn verify(
|
||||
&mut self,
|
||||
@@ -1111,46 +1093,51 @@ where
|
||||
body,
|
||||
);
|
||||
|
||||
debug!(target: "babe", "We have {:?} logs in this header", header.digest().logs().len());
|
||||
let mut inherent_data = self
|
||||
.inherent_data_providers
|
||||
.create_inherent_data()
|
||||
.map_err(Error::<Block>::Runtime)?;
|
||||
|
||||
let (_, slot_now, _) = self.time_source.extract_timestamp_and_slot(&inherent_data)
|
||||
.map_err(Error::<Block>::Extraction)?;
|
||||
|
||||
let hash = header.hash();
|
||||
let parent_hash = *header.parent_hash();
|
||||
|
||||
debug!(target: "babe", "We have {:?} logs in this header", header.digest().logs().len());
|
||||
|
||||
let create_inherent_data_providers = self
|
||||
.create_inherent_data_providers
|
||||
.create_inherent_data_providers(parent_hash, ())
|
||||
.await
|
||||
.map_err(|e| Error::<Block>::Client(sp_consensus::Error::from(e).into()))?;
|
||||
|
||||
let slot_now = create_inherent_data_providers.slot();
|
||||
|
||||
let parent_header_metadata = self.client.header_metadata(parent_hash)
|
||||
.map_err(Error::<Block>::FetchParentHeader)?;
|
||||
|
||||
let pre_digest = find_pre_digest::<Block>(&header)?;
|
||||
let epoch_changes = self.epoch_changes.shared_data();
|
||||
let epoch_descriptor = epoch_changes.epoch_descriptor_for_child_of(
|
||||
descendent_query(&*self.client),
|
||||
&parent_hash,
|
||||
parent_header_metadata.number,
|
||||
pre_digest.slot(),
|
||||
)
|
||||
let (check_header, epoch_descriptor) = {
|
||||
let epoch_changes = self.epoch_changes.shared_data();
|
||||
let epoch_descriptor = epoch_changes.epoch_descriptor_for_child_of(
|
||||
descendent_query(&*self.client),
|
||||
&parent_hash,
|
||||
parent_header_metadata.number,
|
||||
pre_digest.slot(),
|
||||
)
|
||||
.map_err(|e| Error::<Block>::ForkTree(Box::new(e)))?
|
||||
.ok_or_else(|| Error::<Block>::FetchEpoch(parent_hash))?;
|
||||
let viable_epoch = epoch_changes.viable_epoch(
|
||||
&epoch_descriptor,
|
||||
|slot| Epoch::genesis(&self.config, slot)
|
||||
).ok_or_else(|| Error::<Block>::FetchEpoch(parent_hash))?;
|
||||
let viable_epoch = epoch_changes.viable_epoch(
|
||||
&epoch_descriptor,
|
||||
|slot| Epoch::genesis(&self.config, slot)
|
||||
).ok_or_else(|| Error::<Block>::FetchEpoch(parent_hash))?;
|
||||
|
||||
// We add one to the current slot to allow for some small drift.
|
||||
// FIXME #1019 in the future, alter this queue to allow deferring of headers
|
||||
let v_params = verification::VerificationParams {
|
||||
header: header.clone(),
|
||||
pre_digest: Some(pre_digest),
|
||||
slot_now: slot_now + 1,
|
||||
epoch: viable_epoch.as_ref(),
|
||||
// We add one to the current slot to allow for some small drift.
|
||||
// FIXME #1019 in the future, alter this queue to allow deferring of headers
|
||||
let v_params = verification::VerificationParams {
|
||||
header: header.clone(),
|
||||
pre_digest: Some(pre_digest),
|
||||
slot_now: slot_now + 1,
|
||||
epoch: viable_epoch.as_ref(),
|
||||
};
|
||||
|
||||
(verification::check_header::<Block>(v_params)?, epoch_descriptor)
|
||||
};
|
||||
|
||||
match verification::check_header::<Block>(v_params)? {
|
||||
match check_header {
|
||||
CheckedHeader::Checked(pre_header, verified_info) => {
|
||||
let babe_pre_digest = verified_info.pre_digest.as_babe_pre_digest()
|
||||
.expect("check_header always returns a pre-digest digest item; qed");
|
||||
@@ -1173,6 +1160,8 @@ where
|
||||
// to check that the internally-set timestamp in the inherents
|
||||
// actually matches the slot set in the seal.
|
||||
if let Some(inner_body) = body.take() {
|
||||
let mut inherent_data = create_inherent_data_providers.create_inherent_data()
|
||||
.map_err(Error::<Block>::CreateInherents)?;
|
||||
inherent_data.babe_replace_inherent_data(slot);
|
||||
let block = Block::new(pre_header.clone(), inner_body);
|
||||
|
||||
@@ -1180,7 +1169,8 @@ where
|
||||
block.clone(),
|
||||
BlockId::Hash(parent_hash),
|
||||
inherent_data,
|
||||
)?;
|
||||
create_inherent_data_providers,
|
||||
).await?;
|
||||
|
||||
let (_, inner_body) = block.deconstruct();
|
||||
body = Some(inner_body);
|
||||
@@ -1220,22 +1210,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
/// Register the babe inherent data provider, if not registered already.
|
||||
pub fn register_babe_inherent_data_provider(
|
||||
inherent_data_providers: &InherentDataProviders,
|
||||
slot_duration: Duration,
|
||||
) -> Result<(), sp_consensus::Error> {
|
||||
debug!(target: "babe", "Registering");
|
||||
if !inherent_data_providers.has_provider(&sp_consensus_babe::inherents::INHERENT_IDENTIFIER) {
|
||||
inherent_data_providers
|
||||
.register_provider(sp_consensus_babe::inherents::InherentDataProvider::new(slot_duration))
|
||||
.map_err(Into::into)
|
||||
.map_err(sp_consensus::Error::InherentData)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// A block-import handler for BABE.
|
||||
///
|
||||
/// This scans each imported block for epoch change signals. The signals are
|
||||
@@ -1579,13 +1553,13 @@ pub fn block_import<Client, Block: BlockT, I>(
|
||||
config: Config,
|
||||
wrapped_block_import: I,
|
||||
client: Arc<Client>,
|
||||
) -> ClientResult<(BabeBlockImport<Block, Client, I>, BabeLink<Block>)> where
|
||||
) -> ClientResult<(BabeBlockImport<Block, Client, I>, BabeLink<Block>)>
|
||||
where
|
||||
Client: AuxStore + HeaderBackend<Block> + HeaderMetadata<Block, Error = sp_blockchain::Error>,
|
||||
{
|
||||
let epoch_changes = aux_schema::load_epoch_changes::<Block, _>(&*client, &config)?;
|
||||
let link = BabeLink {
|
||||
epoch_changes: epoch_changes.clone(),
|
||||
time_source: Default::default(),
|
||||
config: config.clone(),
|
||||
};
|
||||
|
||||
@@ -1616,13 +1590,13 @@ pub fn block_import<Client, Block: BlockT, I>(
|
||||
///
|
||||
/// The block import object provided must be the `BabeBlockImport` or a wrapper
|
||||
/// of it, otherwise crucial import logic will be omitted.
|
||||
pub fn import_queue<Block: BlockT, Client, SelectChain, Inner, CAW>(
|
||||
pub fn import_queue<Block: BlockT, Client, SelectChain, Inner, CAW, CIDP>(
|
||||
babe_link: BabeLink<Block>,
|
||||
block_import: Inner,
|
||||
justification_import: Option<BoxJustificationImport<Block>>,
|
||||
client: Arc<Client>,
|
||||
select_chain: SelectChain,
|
||||
inherent_data_providers: InherentDataProviders,
|
||||
create_inherent_data_providers: CIDP,
|
||||
spawner: &impl sp_core::traits::SpawnEssentialNamed,
|
||||
registry: Option<&Registry>,
|
||||
can_author_with: CAW,
|
||||
@@ -1636,15 +1610,14 @@ pub fn import_queue<Block: BlockT, Client, SelectChain, Inner, CAW>(
|
||||
Client::Api: BlockBuilderApi<Block> + BabeApi<Block> + ApiExt<Block>,
|
||||
SelectChain: sp_consensus::SelectChain<Block> + 'static,
|
||||
CAW: CanAuthorWith<Block> + Send + Sync + 'static,
|
||||
CIDP: CreateInherentDataProviders<Block, ()> + Send + Sync + 'static,
|
||||
CIDP::InherentDataProviders: InherentDataProviderExt + Send + Sync,
|
||||
{
|
||||
register_babe_inherent_data_provider(&inherent_data_providers, babe_link.config.slot_duration())?;
|
||||
|
||||
let verifier = BabeVerifier {
|
||||
select_chain,
|
||||
inherent_data_providers,
|
||||
create_inherent_data_providers,
|
||||
config: babe_link.config,
|
||||
epoch_changes: babe_link.epoch_changes,
|
||||
time_source: babe_link.time_source,
|
||||
can_author_with,
|
||||
telemetry,
|
||||
client,
|
||||
|
||||
@@ -28,7 +28,10 @@ use sp_keystore::{
|
||||
SyncCryptoStore,
|
||||
vrf::make_transcript as transcript_from_data,
|
||||
};
|
||||
use sp_consensus_babe::{AuthorityPair, Slot, AllowedSlots, make_transcript, make_transcript_data};
|
||||
use sp_consensus_babe::{
|
||||
AuthorityPair, Slot, AllowedSlots, make_transcript, make_transcript_data,
|
||||
inherents::InherentDataProvider,
|
||||
};
|
||||
use sc_consensus_slots::BackoffAuthoringOnFinalizedHeadLagging;
|
||||
use sc_block_builder::{BlockBuilder, BlockBuilderProvider};
|
||||
use sp_consensus::{
|
||||
@@ -48,6 +51,7 @@ use rand_chacha::{
|
||||
use sc_keystore::LocalKeystore;
|
||||
use sp_application_crypto::key_types::BABE;
|
||||
use futures::executor::block_on;
|
||||
use sp_timestamp::InherentDataProvider as TimestampInherentDataProvider;
|
||||
|
||||
type Item = DigestItem<Hash>;
|
||||
|
||||
@@ -235,7 +239,17 @@ type TestSelectChain = substrate_test_runtime_client::LongestChain<
|
||||
>;
|
||||
|
||||
pub struct TestVerifier {
|
||||
inner: BabeVerifier<TestBlock, PeersFullClient, TestSelectChain, AlwaysCanAuthor>,
|
||||
inner: BabeVerifier<
|
||||
TestBlock,
|
||||
PeersFullClient,
|
||||
TestSelectChain,
|
||||
AlwaysCanAuthor,
|
||||
Box<dyn CreateInherentDataProviders<
|
||||
TestBlock,
|
||||
(),
|
||||
InherentDataProviders = (TimestampInherentDataProvider, InherentDataProvider)
|
||||
>>
|
||||
>,
|
||||
mutator: Mutator,
|
||||
}
|
||||
|
||||
@@ -253,13 +267,12 @@ impl Verifier<TestBlock> for TestVerifier {
|
||||
) -> Result<(BlockImportParams<TestBlock, ()>, Option<Vec<(CacheKeyId, Vec<u8>)>>), String> {
|
||||
// apply post-sealing mutations (i.e. stripping seal, if desired).
|
||||
(self.mutator)(&mut header, Stage::PostSeal);
|
||||
self.inner.verify(dbg!(origin), header, justifications, body).await
|
||||
self.inner.verify(origin, header, justifications, body).await
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PeerData {
|
||||
link: BabeLink<TestBlock>,
|
||||
inherent_data_providers: InherentDataProviders,
|
||||
block_import: Mutex<
|
||||
Option<BoxBlockImport<TestBlock, TransactionFor<substrate_test_runtime_client::Backend, TestBlock>>>
|
||||
>,
|
||||
@@ -286,7 +299,6 @@ impl TestNetFactory for BabeTestNet {
|
||||
)
|
||||
{
|
||||
let client = client.as_full().expect("only full clients are tested");
|
||||
let inherent_data_providers = InherentDataProviders::new();
|
||||
|
||||
let config = Config::get_or_compute(&*client).expect("config available");
|
||||
let (block_import, link) = crate::block_import(
|
||||
@@ -303,7 +315,7 @@ impl TestNetFactory for BabeTestNet {
|
||||
(
|
||||
BlockImportAdapter::new(block_import),
|
||||
None,
|
||||
Some(PeerData { link, inherent_data_providers, block_import: data_block_import }),
|
||||
Some(PeerData { link, block_import: data_block_import }),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -329,10 +341,17 @@ impl TestNetFactory for BabeTestNet {
|
||||
inner: BabeVerifier {
|
||||
client: client.clone(),
|
||||
select_chain: longest_chain,
|
||||
inherent_data_providers: data.inherent_data_providers.clone(),
|
||||
create_inherent_data_providers: Box::new(|_, _| async {
|
||||
let timestamp = TimestampInherentDataProvider::from_system_time();
|
||||
let slot = InherentDataProvider::from_timestamp_and_duration(
|
||||
*timestamp,
|
||||
Duration::from_secs(6),
|
||||
);
|
||||
|
||||
Ok((timestamp, slot))
|
||||
}),
|
||||
config: data.link.config.clone(),
|
||||
epoch_changes: data.link.epoch_changes.clone(),
|
||||
time_source: data.link.time_source.clone(),
|
||||
can_author_with: AlwaysCanAuthor,
|
||||
telemetry: None,
|
||||
},
|
||||
@@ -440,7 +459,15 @@ fn run_one_test(
|
||||
client,
|
||||
env: environ,
|
||||
sync_oracle: DummyOracle,
|
||||
inherent_data_providers: data.inherent_data_providers.clone(),
|
||||
create_inherent_data_providers: Box::new(|_, _| async {
|
||||
let timestamp = TimestampInherentDataProvider::from_system_time();
|
||||
let slot = InherentDataProvider::from_timestamp_and_duration(
|
||||
*timestamp,
|
||||
Duration::from_secs(6),
|
||||
);
|
||||
|
||||
Ok((timestamp, slot))
|
||||
}),
|
||||
force_authoring: false,
|
||||
backoff_authoring_blocks: Some(BackoffAuthoringOnFinalizedHeadLagging::default()),
|
||||
babe_link: data.link.clone(),
|
||||
|
||||
@@ -24,8 +24,8 @@ use codec::Encode;
|
||||
use std::{borrow::Cow, sync::{Arc, atomic}, time::SystemTime};
|
||||
use sc_client_api::AuxStore;
|
||||
use sc_consensus_babe::{
|
||||
Config, Epoch, authorship, CompatibleDigestItem, BabeIntermediate,
|
||||
register_babe_inherent_data_provider, INTERMEDIATE_KEY, find_pre_digest,
|
||||
Config, Epoch, authorship, CompatibleDigestItem, BabeIntermediate, INTERMEDIATE_KEY,
|
||||
find_pre_digest,
|
||||
};
|
||||
use sc_consensus_epochs::{SharedEpochChanges, descendent_query, ViableEpochDescriptor, EpochHeader};
|
||||
use sp_keystore::SyncCryptoStorePtr;
|
||||
@@ -38,12 +38,12 @@ use sp_consensus_babe::{
|
||||
BabeApi, inherents::BabeInherentData, ConsensusLog, BABE_ENGINE_ID, AuthorityId,
|
||||
digests::{PreDigest, SecondaryPlainPreDigest, NextEpochDescriptor}, BabeAuthorityWeight,
|
||||
};
|
||||
use sp_inherents::{InherentDataProviders, InherentData, ProvideInherentData, InherentIdentifier};
|
||||
use sp_inherents::{InherentData, InherentDataProvider, InherentIdentifier};
|
||||
use sp_runtime::{
|
||||
traits::{DigestItemFor, DigestFor, Block as BlockT, Zero, Header},
|
||||
generic::{Digest, BlockId},
|
||||
};
|
||||
use sp_timestamp::{InherentType, InherentError, INHERENT_IDENTIFIER, TimestampInherentData};
|
||||
use sp_timestamp::{InherentType, INHERENT_IDENTIFIER, TimestampInherentData};
|
||||
|
||||
/// Provides BABE-compatible predigests and BlockImportParams.
|
||||
/// Intended for use with BABE runtimes.
|
||||
@@ -73,7 +73,6 @@ impl<B, C> BabeConsensusDataProvider<B, C>
|
||||
pub fn new(
|
||||
client: Arc<C>,
|
||||
keystore: SyncCryptoStorePtr,
|
||||
provider: &InherentDataProviders,
|
||||
epoch_changes: SharedEpochChanges<B, Epoch>,
|
||||
authorities: Vec<(AuthorityId, BabeAuthorityWeight)>,
|
||||
) -> Result<Self, Error> {
|
||||
@@ -82,10 +81,6 @@ impl<B, C> BabeConsensusDataProvider<B, C>
|
||||
}
|
||||
|
||||
let config = Config::get_or_compute(&*client)?;
|
||||
let timestamp_provider = SlotTimestampProvider::new(client.clone())?;
|
||||
|
||||
provider.register_provider(timestamp_provider)?;
|
||||
register_babe_inherent_data_provider(provider, config.slot_duration())?;
|
||||
|
||||
Ok(Self {
|
||||
config,
|
||||
@@ -131,7 +126,8 @@ impl<B, C> ConsensusDataProvider<B> for BabeConsensusDataProvider<B, C>
|
||||
type Transaction = TransactionFor<C, B>;
|
||||
|
||||
fn create_digest(&self, parent: &B::Header, inherents: &InherentData) -> Result<DigestFor<B>, Error> {
|
||||
let slot = inherents.babe_inherent_data()?;
|
||||
let slot = inherents.babe_inherent_data()?
|
||||
.ok_or_else(|| Error::StringError("No babe inherent data".into()))?;
|
||||
let epoch = self.epoch(parent, slot)?;
|
||||
|
||||
// this is a dev node environment, we should always be able to claim a slot.
|
||||
@@ -194,7 +190,8 @@ impl<B, C> ConsensusDataProvider<B> for BabeConsensusDataProvider<B, C>
|
||||
params: &mut BlockImportParams<B, Self::Transaction>,
|
||||
inherents: &InherentData
|
||||
) -> Result<(), Error> {
|
||||
let slot = inherents.babe_inherent_data()?;
|
||||
let slot = inherents.babe_inherent_data()?
|
||||
.ok_or_else(|| Error::StringError("No babe inherent data".into()))?;
|
||||
let epoch_changes = self.epoch_changes.shared_data();
|
||||
let mut epoch_descriptor = epoch_changes
|
||||
.epoch_descriptor_for_child_of(
|
||||
@@ -216,7 +213,9 @@ impl<B, C> ConsensusDataProvider<B> for BabeConsensusDataProvider<B, C>
|
||||
|
||||
if !has_authority {
|
||||
log::info!(target: "manual-seal", "authority not found");
|
||||
let slot = *inherents.timestamp_inherent_data()? / self.config.slot_duration;
|
||||
let timestamp = inherents.timestamp_inherent_data()?
|
||||
.ok_or_else(|| Error::StringError("No timestamp inherent data".into()))?;
|
||||
let slot = *timestamp / self.config.slot_duration;
|
||||
// manually hard code epoch descriptor
|
||||
epoch_descriptor = match epoch_descriptor {
|
||||
ViableEpochDescriptor::Signaled(identifier, _header) => {
|
||||
@@ -243,14 +242,14 @@ impl<B, C> ConsensusDataProvider<B> for BabeConsensusDataProvider<B, C>
|
||||
|
||||
/// Provide duration since unix epoch in millisecond for timestamp inherent.
|
||||
/// Mocks the timestamp inherent to always produce the timestamp for the next babe slot.
|
||||
struct SlotTimestampProvider {
|
||||
pub struct SlotTimestampProvider {
|
||||
time: atomic::AtomicU64,
|
||||
slot_duration: u64
|
||||
}
|
||||
|
||||
impl SlotTimestampProvider {
|
||||
/// create a new mocked time stamp provider.
|
||||
fn new<B, C>(client: Arc<C>) -> Result<Self, Error>
|
||||
/// Create a new mocked time stamp provider.
|
||||
pub fn new<B, C>(client: Arc<C>) -> Result<Self, Error>
|
||||
where
|
||||
B: BlockT,
|
||||
C: AuxStore + HeaderBackend<B> + ProvideRuntimeApi<B>,
|
||||
@@ -281,11 +280,8 @@ impl SlotTimestampProvider {
|
||||
}
|
||||
}
|
||||
|
||||
impl ProvideInherentData for SlotTimestampProvider {
|
||||
fn inherent_identifier(&self) -> &'static InherentIdentifier {
|
||||
&INHERENT_IDENTIFIER
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl InherentDataProvider for SlotTimestampProvider {
|
||||
fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), sp_inherents::Error> {
|
||||
// we update the time here.
|
||||
let duration: InherentType = self.time.fetch_add(
|
||||
@@ -296,7 +292,11 @@ impl ProvideInherentData for SlotTimestampProvider {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn error_to_string(&self, error: &[u8]) -> Option<String> {
|
||||
InherentError::try_from(&INHERENT_IDENTIFIER, error).map(|e| format!("{:?}", e))
|
||||
async fn try_handle_error(
|
||||
&self,
|
||||
_: &InherentIdentifier,
|
||||
_: &[u8],
|
||||
) -> Option<Result<(), sp_inherents::Error>> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ use sp_consensus::{
|
||||
import_queue::{Verifier, BasicQueue, CacheKeyId, BoxBlockImport},
|
||||
};
|
||||
use sp_blockchain::HeaderBackend;
|
||||
use sp_inherents::InherentDataProviders;
|
||||
use sp_inherents::CreateInherentDataProviders;
|
||||
use sp_runtime::{traits::Block as BlockT, Justifications, ConsensusEngineId};
|
||||
use sc_client_api::backend::{Backend as ClientBackend, Finalizer};
|
||||
use sc_transaction_pool::txpool;
|
||||
@@ -94,7 +94,7 @@ pub fn import_queue<Block, Transaction>(
|
||||
}
|
||||
|
||||
/// Params required to start the instant sealing authorship task.
|
||||
pub struct ManualSealParams<B: BlockT, BI, E, C: ProvideRuntimeApi<B>, A: txpool::ChainApi, SC, CS> {
|
||||
pub struct ManualSealParams<B: BlockT, BI, E, C: ProvideRuntimeApi<B>, A: txpool::ChainApi, SC, CS, CIDP> {
|
||||
/// Block import instance for well. importing blocks.
|
||||
pub block_import: BI,
|
||||
|
||||
@@ -117,12 +117,12 @@ pub struct ManualSealParams<B: BlockT, BI, E, C: ProvideRuntimeApi<B>, A: txpool
|
||||
/// Digest provider for inclusion in blocks.
|
||||
pub consensus_data_provider: Option<Box<dyn ConsensusDataProvider<B, Transaction = TransactionFor<C, B>>>>,
|
||||
|
||||
/// Provider for inherents to include in blocks.
|
||||
pub inherent_data_providers: InherentDataProviders,
|
||||
/// Something that can create the inherent data providers.
|
||||
pub create_inherent_data_providers: CIDP,
|
||||
}
|
||||
|
||||
/// Params required to start the manual sealing authorship task.
|
||||
pub struct InstantSealParams<B: BlockT, BI, E, C: ProvideRuntimeApi<B>, A: txpool::ChainApi, SC> {
|
||||
pub struct InstantSealParams<B: BlockT, BI, E, C: ProvideRuntimeApi<B>, A: txpool::ChainApi, SC, CIDP> {
|
||||
/// Block import instance for well. importing blocks.
|
||||
pub block_import: BI,
|
||||
|
||||
@@ -141,12 +141,12 @@ pub struct InstantSealParams<B: BlockT, BI, E, C: ProvideRuntimeApi<B>, A: txpoo
|
||||
/// Digest provider for inclusion in blocks.
|
||||
pub consensus_data_provider: Option<Box<dyn ConsensusDataProvider<B, Transaction = TransactionFor<C, B>>>>,
|
||||
|
||||
/// Provider for inherents to include in blocks.
|
||||
pub inherent_data_providers: InherentDataProviders,
|
||||
/// Something that can create the inherent data providers.
|
||||
pub create_inherent_data_providers: CIDP,
|
||||
}
|
||||
|
||||
/// Creates the background authorship task for the manual seal engine.
|
||||
pub async fn run_manual_seal<B, BI, CB, E, C, A, SC, CS>(
|
||||
pub async fn run_manual_seal<B, BI, CB, E, C, A, SC, CS, CIDP>(
|
||||
ManualSealParams {
|
||||
mut block_import,
|
||||
mut env,
|
||||
@@ -154,10 +154,9 @@ pub async fn run_manual_seal<B, BI, CB, E, C, A, SC, CS>(
|
||||
pool,
|
||||
mut commands_stream,
|
||||
select_chain,
|
||||
inherent_data_providers,
|
||||
consensus_data_provider,
|
||||
..
|
||||
}: ManualSealParams<B, BI, E, C, A, SC, CS>
|
||||
create_inherent_data_providers,
|
||||
}: ManualSealParams<B, BI, E, C, A, SC, CS, CIDP>
|
||||
)
|
||||
where
|
||||
A: txpool::ChainApi<Block=B> + 'static,
|
||||
@@ -171,6 +170,7 @@ pub async fn run_manual_seal<B, BI, CB, E, C, A, SC, CS>(
|
||||
CS: Stream<Item=EngineCommand<<B as BlockT>::Hash>> + Unpin + 'static,
|
||||
SC: SelectChain<B> + 'static,
|
||||
TransactionFor<C, B>: 'static,
|
||||
CIDP: CreateInherentDataProviders<B, ()>,
|
||||
{
|
||||
while let Some(command) = commands_stream.next().await {
|
||||
match command {
|
||||
@@ -189,10 +189,10 @@ pub async fn run_manual_seal<B, BI, CB, E, C, A, SC, CS>(
|
||||
env: &mut env,
|
||||
select_chain: &select_chain,
|
||||
block_import: &mut block_import,
|
||||
inherent_data_provider: &inherent_data_providers,
|
||||
consensus_data_provider: consensus_data_provider.as_ref().map(|p| &**p),
|
||||
pool: pool.clone(),
|
||||
client: client.clone(),
|
||||
create_inherent_data_providers: &create_inherent_data_providers,
|
||||
}
|
||||
).await;
|
||||
}
|
||||
@@ -215,7 +215,7 @@ pub async fn run_manual_seal<B, BI, CB, E, C, A, SC, CS>(
|
||||
/// runs the background authorship task for the instant seal engine.
|
||||
/// instant-seal creates a new block for every transaction imported into
|
||||
/// the transaction pool.
|
||||
pub async fn run_instant_seal<B, BI, CB, E, C, A, SC>(
|
||||
pub async fn run_instant_seal<B, BI, CB, E, C, A, SC, CIDP>(
|
||||
InstantSealParams {
|
||||
block_import,
|
||||
env,
|
||||
@@ -223,9 +223,8 @@ pub async fn run_instant_seal<B, BI, CB, E, C, A, SC>(
|
||||
pool,
|
||||
select_chain,
|
||||
consensus_data_provider,
|
||||
inherent_data_providers,
|
||||
..
|
||||
}: InstantSealParams<B, BI, E, C, A, SC>
|
||||
create_inherent_data_providers,
|
||||
}: InstantSealParams<B, BI, E, C, A, SC, CIDP>
|
||||
)
|
||||
where
|
||||
A: txpool::ChainApi<Block=B> + 'static,
|
||||
@@ -238,6 +237,7 @@ pub async fn run_instant_seal<B, BI, CB, E, C, A, SC>(
|
||||
E::Proposer: Proposer<B, Transaction = TransactionFor<C, B>>,
|
||||
SC: SelectChain<B> + 'static,
|
||||
TransactionFor<C, B>: 'static,
|
||||
CIDP: CreateInherentDataProviders<B, ()>,
|
||||
{
|
||||
// instant-seal creates blocks as soon as transactions are imported
|
||||
// into the transaction pool.
|
||||
@@ -261,7 +261,7 @@ pub async fn run_instant_seal<B, BI, CB, E, C, A, SC>(
|
||||
commands_stream,
|
||||
select_chain,
|
||||
consensus_data_provider,
|
||||
inherent_data_providers,
|
||||
create_inherent_data_providers,
|
||||
}
|
||||
).await
|
||||
}
|
||||
@@ -280,7 +280,6 @@ mod tests {
|
||||
use sp_transaction_pool::{TransactionPool, MaintainedTransactionPool, TransactionSource};
|
||||
use sp_runtime::generic::BlockId;
|
||||
use sp_consensus::ImportedAux;
|
||||
use sp_inherents::InherentDataProviders;
|
||||
use sc_basic_authorship::ProposerFactory;
|
||||
use sc_client_api::BlockBackend;
|
||||
|
||||
@@ -295,7 +294,6 @@ mod tests {
|
||||
let builder = TestClientBuilder::new();
|
||||
let (client, select_chain) = builder.build_with_longest_chain();
|
||||
let client = Arc::new(client);
|
||||
let inherent_data_providers = InherentDataProviders::new();
|
||||
let spawner = sp_core::testing::TaskExecutor::new();
|
||||
let pool = Arc::new(BasicPool::with_revalidation_type(
|
||||
Options::default(), true.into(), api(), None, RevalidationType::Full, spawner.clone(),
|
||||
@@ -330,7 +328,7 @@ mod tests {
|
||||
pool: pool.pool().clone(),
|
||||
commands_stream,
|
||||
select_chain,
|
||||
inherent_data_providers,
|
||||
create_inherent_data_providers: |_, _| async { Ok(()) },
|
||||
consensus_data_provider: None,
|
||||
}
|
||||
);
|
||||
@@ -367,10 +365,14 @@ mod tests {
|
||||
let builder = TestClientBuilder::new();
|
||||
let (client, select_chain) = builder.build_with_longest_chain();
|
||||
let client = Arc::new(client);
|
||||
let inherent_data_providers = InherentDataProviders::new();
|
||||
let spawner = sp_core::testing::TaskExecutor::new();
|
||||
let pool = Arc::new(BasicPool::with_revalidation_type(
|
||||
Options::default(), true.into(), api(), None, RevalidationType::Full, spawner.clone(),
|
||||
Options::default(),
|
||||
true.into(),
|
||||
api(),
|
||||
None,
|
||||
RevalidationType::Full,
|
||||
spawner.clone(),
|
||||
));
|
||||
let env = ProposerFactory::new(
|
||||
spawner.clone(),
|
||||
@@ -390,7 +392,7 @@ mod tests {
|
||||
commands_stream,
|
||||
select_chain,
|
||||
consensus_data_provider: None,
|
||||
inherent_data_providers,
|
||||
create_inherent_data_providers: |_, _| async { Ok(()) },
|
||||
}
|
||||
);
|
||||
std::thread::spawn(|| {
|
||||
@@ -442,11 +444,15 @@ mod tests {
|
||||
let builder = TestClientBuilder::new();
|
||||
let (client, select_chain) = builder.build_with_longest_chain();
|
||||
let client = Arc::new(client);
|
||||
let inherent_data_providers = InherentDataProviders::new();
|
||||
let pool_api = api();
|
||||
let spawner = sp_core::testing::TaskExecutor::new();
|
||||
let pool = Arc::new(BasicPool::with_revalidation_type(
|
||||
Options::default(), true.into(), pool_api.clone(), None, RevalidationType::Full, spawner.clone(),
|
||||
Options::default(),
|
||||
true.into(),
|
||||
pool_api.clone(),
|
||||
None,
|
||||
RevalidationType::Full,
|
||||
spawner.clone(),
|
||||
));
|
||||
let env = ProposerFactory::new(
|
||||
spawner.clone(),
|
||||
@@ -466,7 +472,7 @@ mod tests {
|
||||
commands_stream,
|
||||
select_chain,
|
||||
consensus_data_provider: None,
|
||||
inherent_data_providers,
|
||||
create_inherent_data_providers: |_, _| async { Ok(()) },
|
||||
}
|
||||
);
|
||||
std::thread::spawn(|| {
|
||||
@@ -528,7 +534,7 @@ mod tests {
|
||||
pool_api.add_block(block, true);
|
||||
pool_api.increment_nonce(Alice.into());
|
||||
|
||||
assert!(pool.submit_one(&BlockId::Number(1), SOURCE, uxt(Alice, 2)).await.is_ok());
|
||||
assert!(pool.submit_one(&BlockId::Number(1), SOURCE, uxt(Bob, 0)).await.is_ok());
|
||||
let (tx2, rx2) = futures::channel::oneshot::channel();
|
||||
assert!(sink.send(EngineCommand::SealNewBlock {
|
||||
parent_hash: Some(created_block.hash),
|
||||
|
||||
@@ -33,14 +33,14 @@ use sp_consensus::{
|
||||
use sp_blockchain::HeaderBackend;
|
||||
use std::collections::HashMap;
|
||||
use std::time::Duration;
|
||||
use sp_inherents::InherentDataProviders;
|
||||
use sp_inherents::{CreateInherentDataProviders, InherentDataProvider};
|
||||
use sp_api::{ProvideRuntimeApi, TransactionFor};
|
||||
|
||||
/// max duration for creating a proposal in secs
|
||||
pub const MAX_PROPOSAL_DURATION: u64 = 10;
|
||||
|
||||
/// params for sealing a new block
|
||||
pub struct SealBlockParams<'a, B: BlockT, BI, SC, C: ProvideRuntimeApi<B>, E, P: txpool::ChainApi> {
|
||||
pub struct SealBlockParams<'a, B: BlockT, BI, SC, C: ProvideRuntimeApi<B>, E, P: txpool::ChainApi, CIDP> {
|
||||
/// if true, empty blocks(without extrinsics) will be created.
|
||||
/// otherwise, will return Error::EmptyTransactionPool.
|
||||
pub create_empty: bool,
|
||||
@@ -62,12 +62,12 @@ pub struct SealBlockParams<'a, B: BlockT, BI, SC, C: ProvideRuntimeApi<B>, E, P:
|
||||
pub consensus_data_provider: Option<&'a dyn ConsensusDataProvider<B, Transaction = TransactionFor<C, B>>>,
|
||||
/// block import object
|
||||
pub block_import: &'a mut BI,
|
||||
/// inherent data provider
|
||||
pub inherent_data_provider: &'a InherentDataProviders,
|
||||
/// Something that can create the inherent data providers.
|
||||
pub create_inherent_data_providers: &'a CIDP,
|
||||
}
|
||||
|
||||
/// seals a new block with the given params
|
||||
pub async fn seal_block<B, BI, SC, C, E, P>(
|
||||
pub async fn seal_block<B, BI, SC, C, E, P, CIDP>(
|
||||
SealBlockParams {
|
||||
create_empty,
|
||||
finalize,
|
||||
@@ -77,11 +77,10 @@ pub async fn seal_block<B, BI, SC, C, E, P>(
|
||||
select_chain,
|
||||
block_import,
|
||||
env,
|
||||
inherent_data_provider,
|
||||
create_inherent_data_providers,
|
||||
consensus_data_provider: digest_provider,
|
||||
mut sender,
|
||||
..
|
||||
}: SealBlockParams<'_, B, BI, SC, C, E, P>
|
||||
}: SealBlockParams<'_, B, BI, SC, C, E, P, CIDP>
|
||||
)
|
||||
where
|
||||
B: BlockT,
|
||||
@@ -93,6 +92,7 @@ pub async fn seal_block<B, BI, SC, C, E, P>(
|
||||
P: txpool::ChainApi<Block=B>,
|
||||
SC: SelectChain<B>,
|
||||
TransactionFor<C, B>: 'static,
|
||||
CIDP: CreateInherentDataProviders<B, ()>,
|
||||
{
|
||||
let future = async {
|
||||
if pool.validated_pool().status().ready == 0 && !create_empty {
|
||||
@@ -109,19 +109,29 @@ pub async fn seal_block<B, BI, SC, C, E, P>(
|
||||
None => select_chain.best_chain()?
|
||||
};
|
||||
|
||||
let inherent_data_providers =
|
||||
create_inherent_data_providers
|
||||
.create_inherent_data_providers(
|
||||
parent.hash(),
|
||||
(),
|
||||
)
|
||||
.await
|
||||
.map_err(|e| Error::Other(e))?;
|
||||
|
||||
let inherent_data = inherent_data_providers.create_inherent_data()?;
|
||||
|
||||
let proposer = env.init(&parent)
|
||||
.map_err(|err| Error::StringError(format!("{:?}", err))).await?;
|
||||
let id = inherent_data_provider.create_inherent_data()?;
|
||||
let inherents_len = id.len();
|
||||
let inherents_len = inherent_data.len();
|
||||
|
||||
let digest = if let Some(digest_provider) = digest_provider {
|
||||
digest_provider.create_digest(&parent, &id)?
|
||||
digest_provider.create_digest(&parent, &inherent_data)?
|
||||
} else {
|
||||
Default::default()
|
||||
};
|
||||
|
||||
let proposal = proposer.propose(
|
||||
id.clone(),
|
||||
inherent_data.clone(),
|
||||
digest,
|
||||
Duration::from_secs(MAX_PROPOSAL_DURATION),
|
||||
None,
|
||||
@@ -139,7 +149,7 @@ pub async fn seal_block<B, BI, SC, C, E, P>(
|
||||
params.storage_changes = Some(proposal.storage_changes);
|
||||
|
||||
if let Some(digest_provider) = digest_provider {
|
||||
digest_provider.append_block_import(&parent, &mut params, &id)?;
|
||||
digest_provider.append_block_import(&parent, &mut params, &inherent_data)?;
|
||||
}
|
||||
|
||||
match block_import.import_block(params, HashMap::new()).await? {
|
||||
|
||||
@@ -27,7 +27,6 @@ log = "0.4.8"
|
||||
futures = { version = "0.3.1", features = ["compat"] }
|
||||
futures-timer = "3.0.1"
|
||||
parking_lot = "0.11.1"
|
||||
sp-timestamp = { version = "3.0.0", path = "../../../primitives/timestamp" }
|
||||
derive_more = "0.99.2"
|
||||
prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../../utils/prometheus", version = "0.9.0"}
|
||||
async-trait = "0.1.42"
|
||||
|
||||
@@ -39,7 +39,7 @@ use std::{
|
||||
sync::Arc, borrow::Cow, collections::HashMap, marker::PhantomData,
|
||||
cmp::Ordering, time::Duration,
|
||||
};
|
||||
use futures::{prelude::*, future::Either};
|
||||
use futures::{Future, StreamExt};
|
||||
use parking_lot::Mutex;
|
||||
use sc_client_api::{BlockOf, backend::AuxStore, BlockchainEvents};
|
||||
use sp_blockchain::{HeaderBackend, ProvideCache, well_known_cache_keys::Id as CacheKeyId};
|
||||
@@ -49,7 +49,7 @@ use sp_runtime::generic::{BlockId, Digest, DigestItem};
|
||||
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
|
||||
use sp_api::ProvideRuntimeApi;
|
||||
use sp_consensus_pow::{Seal, TotalDifficulty, POW_ENGINE_ID};
|
||||
use sp_inherents::{InherentDataProviders, InherentData};
|
||||
use sp_inherents::{CreateInherentDataProviders, InherentDataProvider};
|
||||
use sp_consensus::{
|
||||
BlockImportParams, BlockOrigin, ForkChoiceStrategy, SyncOracle, Environment, Proposer,
|
||||
SelectChain, Error as ConsensusError, CanAuthorWith, BlockImport, BlockCheckParams, ImportResult,
|
||||
@@ -61,7 +61,6 @@ use codec::{Encode, Decode};
|
||||
use prometheus_endpoint::Registry;
|
||||
use sc_client_api;
|
||||
use log::*;
|
||||
use sp_timestamp::{InherentError as TIError, TimestampInherentData};
|
||||
|
||||
use crate::worker::UntilImportedOrTimeout;
|
||||
|
||||
@@ -92,7 +91,12 @@ pub enum Error<B: BlockT> {
|
||||
#[display(fmt = "Creating inherents failed: {}", _0)]
|
||||
CreateInherents(sp_inherents::Error),
|
||||
#[display(fmt = "Checking inherents failed: {}", _0)]
|
||||
CheckInherents(String),
|
||||
CheckInherents(sp_inherents::Error),
|
||||
#[display(
|
||||
fmt = "Checking inherents unknown error for identifier: {:?}",
|
||||
"String::from_utf8_lossy(_0)",
|
||||
)]
|
||||
CheckInherentsUnknownError(sp_inherents::InherentIdentifier),
|
||||
#[display(fmt = "Multiple pre-runtime digests")]
|
||||
MultiplePreRuntimeDigests,
|
||||
Client(sp_blockchain::Error),
|
||||
@@ -200,18 +204,18 @@ pub trait PowAlgorithm<B: BlockT> {
|
||||
}
|
||||
|
||||
/// A block importer for PoW.
|
||||
pub struct PowBlockImport<B: BlockT, I, C, S, Algorithm, CAW> {
|
||||
pub struct PowBlockImport<B: BlockT, I, C, S, Algorithm, CAW, CIDP> {
|
||||
algorithm: Algorithm,
|
||||
inner: I,
|
||||
select_chain: S,
|
||||
client: Arc<C>,
|
||||
inherent_data_providers: sp_inherents::InherentDataProviders,
|
||||
create_inherent_data_providers: Arc<CIDP>,
|
||||
check_inherents_after: <<B as BlockT>::Header as HeaderT>::Number,
|
||||
can_author_with: CAW,
|
||||
}
|
||||
|
||||
impl<B: BlockT, I: Clone, C, S: Clone, Algorithm: Clone, CAW: Clone> Clone
|
||||
for PowBlockImport<B, I, C, S, Algorithm, CAW>
|
||||
impl<B: BlockT, I: Clone, C, S: Clone, Algorithm: Clone, CAW: Clone, CIDP> Clone
|
||||
for PowBlockImport<B, I, C, S, Algorithm, CAW, CIDP>
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
@@ -219,14 +223,14 @@ impl<B: BlockT, I: Clone, C, S: Clone, Algorithm: Clone, CAW: Clone> Clone
|
||||
inner: self.inner.clone(),
|
||||
select_chain: self.select_chain.clone(),
|
||||
client: self.client.clone(),
|
||||
inherent_data_providers: self.inherent_data_providers.clone(),
|
||||
create_inherent_data_providers: self.create_inherent_data_providers.clone(),
|
||||
check_inherents_after: self.check_inherents_after.clone(),
|
||||
can_author_with: self.can_author_with.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, I, C, S, Algorithm, CAW> PowBlockImport<B, I, C, S, Algorithm, CAW> where
|
||||
impl<B, I, C, S, Algorithm, CAW, CIDP> PowBlockImport<B, I, C, S, Algorithm, CAW, CIDP> where
|
||||
B: BlockT,
|
||||
I: BlockImport<B, Transaction = sp_api::TransactionFor<C, B>> + Send + Sync,
|
||||
I::Error: Into<ConsensusError>,
|
||||
@@ -234,6 +238,7 @@ impl<B, I, C, S, Algorithm, CAW> PowBlockImport<B, I, C, S, Algorithm, CAW> wher
|
||||
C::Api: BlockBuilderApi<B>,
|
||||
Algorithm: PowAlgorithm<B>,
|
||||
CAW: CanAuthorWith<B>,
|
||||
CIDP: CreateInherentDataProviders<B, ()>,
|
||||
{
|
||||
/// Create a new block import suitable to be used in PoW
|
||||
pub fn new(
|
||||
@@ -242,7 +247,7 @@ impl<B, I, C, S, Algorithm, CAW> PowBlockImport<B, I, C, S, Algorithm, CAW> wher
|
||||
algorithm: Algorithm,
|
||||
check_inherents_after: <<B as BlockT>::Header as HeaderT>::Number,
|
||||
select_chain: S,
|
||||
inherent_data_providers: sp_inherents::InherentDataProviders,
|
||||
create_inherent_data_providers: CIDP,
|
||||
can_author_with: CAW,
|
||||
) -> Self {
|
||||
Self {
|
||||
@@ -251,20 +256,17 @@ impl<B, I, C, S, Algorithm, CAW> PowBlockImport<B, I, C, S, Algorithm, CAW> wher
|
||||
algorithm,
|
||||
check_inherents_after,
|
||||
select_chain,
|
||||
inherent_data_providers,
|
||||
create_inherent_data_providers: Arc::new(create_inherent_data_providers),
|
||||
can_author_with,
|
||||
}
|
||||
}
|
||||
|
||||
fn check_inherents(
|
||||
async fn check_inherents(
|
||||
&self,
|
||||
block: B,
|
||||
block_id: BlockId<B>,
|
||||
inherent_data: InherentData,
|
||||
timestamp_now: u64,
|
||||
inherent_data_providers: CIDP::InherentDataProviders,
|
||||
) -> Result<(), Error<B>> {
|
||||
const MAX_TIMESTAMP_DRIFT_SECS: u64 = 60;
|
||||
|
||||
if *block.header().number() < self.check_inherents_after {
|
||||
return Ok(())
|
||||
}
|
||||
@@ -279,6 +281,9 @@ impl<B, I, C, S, Algorithm, CAW> PowBlockImport<B, I, C, S, Algorithm, CAW> wher
|
||||
return Ok(())
|
||||
}
|
||||
|
||||
let inherent_data = inherent_data_providers.create_inherent_data()
|
||||
.map_err(|e| Error::CreateInherents(e))?;
|
||||
|
||||
let inherent_res = self.client.runtime_api().check_inherents(
|
||||
&block_id,
|
||||
block,
|
||||
@@ -286,38 +291,32 @@ impl<B, I, C, S, Algorithm, CAW> PowBlockImport<B, I, C, S, Algorithm, CAW> wher
|
||||
).map_err(|e| Error::Client(e.into()))?;
|
||||
|
||||
if !inherent_res.ok() {
|
||||
inherent_res
|
||||
.into_errors()
|
||||
.try_for_each(|(i, e)| match TIError::try_from(&i, &e) {
|
||||
Some(TIError::ValidAtTimestamp(timestamp)) => {
|
||||
if timestamp > timestamp_now + MAX_TIMESTAMP_DRIFT_SECS {
|
||||
return Err(Error::TooFarInFuture);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
},
|
||||
Some(TIError::Other(e)) => Err(Error::Runtime(e)),
|
||||
None => Err(Error::CheckInherents(
|
||||
self.inherent_data_providers.error_to_string(&i, &e)
|
||||
)),
|
||||
})
|
||||
} else {
|
||||
Ok(())
|
||||
for (identifier, error) in inherent_res.into_errors() {
|
||||
match inherent_data_providers.try_handle_error(&identifier, &error).await {
|
||||
Some(res) => res.map_err(Error::CheckInherents)?,
|
||||
None => return Err(Error::CheckInherentsUnknownError(identifier)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<B, I, C, S, Algorithm, CAW> BlockImport<B> for PowBlockImport<B, I, C, S, Algorithm, CAW> where
|
||||
impl<B, I, C, S, Algorithm, CAW, CIDP> BlockImport<B>
|
||||
for PowBlockImport<B, I, C, S, Algorithm, CAW, CIDP>
|
||||
where
|
||||
B: BlockT,
|
||||
I: BlockImport<B, Transaction = sp_api::TransactionFor<C, B>> + Send + Sync,
|
||||
I::Error: Into<ConsensusError>,
|
||||
S: SelectChain<B>,
|
||||
C: ProvideRuntimeApi<B> + Send + Sync + HeaderBackend<B> + AuxStore + ProvideCache<B> + BlockOf,
|
||||
C::Api: BlockBuilderApi<B>,
|
||||
Algorithm: PowAlgorithm<B> + Send,
|
||||
Algorithm: PowAlgorithm<B> + Send + Sync,
|
||||
Algorithm::Difficulty: 'static + Send,
|
||||
CAW: CanAuthorWith<B> + Send,
|
||||
CAW: CanAuthorWith<B> + Send + Sync,
|
||||
CIDP: CreateInherentDataProviders<B, ()> + Send + Sync,
|
||||
{
|
||||
type Error = ConsensusError;
|
||||
type Transaction = sp_api::TransactionFor<C, B>;
|
||||
@@ -343,18 +342,16 @@ impl<B, I, C, S, Algorithm, CAW> BlockImport<B> for PowBlockImport<B, I, C, S, A
|
||||
let mut aux = PowAux::read::<_, B>(self.client.as_ref(), &parent_hash)?;
|
||||
|
||||
if let Some(inner_body) = block.body.take() {
|
||||
let inherent_data = self.inherent_data_providers
|
||||
.create_inherent_data().map_err(|e| e.into_string())?;
|
||||
let timestamp_now = inherent_data.timestamp_inherent_data().map_err(|e| e.into_string())?;
|
||||
|
||||
let check_block = B::new(block.header.clone(), inner_body);
|
||||
|
||||
self.check_inherents(
|
||||
check_block.clone(),
|
||||
BlockId::Hash(parent_hash),
|
||||
inherent_data,
|
||||
*timestamp_now,
|
||||
)?;
|
||||
self.create_inherent_data_providers.create_inherent_data_providers(
|
||||
parent_hash,
|
||||
(),
|
||||
).await?,
|
||||
).await?;
|
||||
|
||||
block.body = Some(check_block.deconstruct().1);
|
||||
}
|
||||
@@ -475,7 +472,7 @@ impl<B: BlockT, Algorithm> Verifier<B> for PowVerifier<B, Algorithm> where
|
||||
import_block.justifications = justifications;
|
||||
import_block.intermediates.insert(
|
||||
Cow::from(INTERMEDIATE_KEY),
|
||||
Box::new(intermediate) as Box<_>,
|
||||
Box::new(intermediate) as Box<_>
|
||||
);
|
||||
import_block.post_hash = Some(hash);
|
||||
|
||||
@@ -483,20 +480,6 @@ impl<B: BlockT, Algorithm> Verifier<B> for PowVerifier<B, Algorithm> where
|
||||
}
|
||||
}
|
||||
|
||||
/// Register the PoW inherent data provider, if not registered already.
|
||||
pub fn register_pow_inherent_data_provider(
|
||||
inherent_data_providers: &InherentDataProviders,
|
||||
) -> Result<(), sp_consensus::Error> {
|
||||
if !inherent_data_providers.has_provider(&sp_timestamp::INHERENT_IDENTIFIER) {
|
||||
inherent_data_providers
|
||||
.register_provider(sp_timestamp::InherentDataProvider)
|
||||
.map_err(Into::into)
|
||||
.map_err(sp_consensus::Error::InherentData)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// The PoW import queue type.
|
||||
pub type PowImportQueue<B, Transaction> = BasicQueue<B, Transaction>;
|
||||
|
||||
@@ -505,7 +488,6 @@ pub fn import_queue<B, Transaction, Algorithm>(
|
||||
block_import: BoxBlockImport<B, Transaction>,
|
||||
justification_import: Option<BoxJustificationImport<B>>,
|
||||
algorithm: Algorithm,
|
||||
inherent_data_providers: InherentDataProviders,
|
||||
spawner: &impl sp_core::traits::SpawnEssentialNamed,
|
||||
registry: Option<&Registry>,
|
||||
) -> Result<
|
||||
@@ -517,8 +499,6 @@ pub fn import_queue<B, Transaction, Algorithm>(
|
||||
Algorithm: PowAlgorithm<B> + Clone + Send + Sync + 'static,
|
||||
Algorithm::Difficulty: Send,
|
||||
{
|
||||
register_pow_inherent_data_provider(&inherent_data_providers)?;
|
||||
|
||||
let verifier = PowVerifier::new(algorithm);
|
||||
|
||||
Ok(BasicQueue::new(
|
||||
@@ -539,7 +519,7 @@ pub fn import_queue<B, Transaction, Algorithm>(
|
||||
///
|
||||
/// `pre_runtime` is a parameter that allows a custom additional pre-runtime digest to be inserted
|
||||
/// for blocks being built. This can encode authorship information, or just be a graffiti.
|
||||
pub fn start_mining_worker<Block, C, S, Algorithm, E, SO, CAW>(
|
||||
pub fn start_mining_worker<Block, C, S, Algorithm, E, SO, CAW, CIDP>(
|
||||
block_import: BoxBlockImport<Block, sp_api::TransactionFor<C, Block>>,
|
||||
client: Arc<C>,
|
||||
select_chain: S,
|
||||
@@ -547,7 +527,7 @@ pub fn start_mining_worker<Block, C, S, Algorithm, E, SO, CAW>(
|
||||
mut env: E,
|
||||
mut sync_oracle: SO,
|
||||
pre_runtime: Option<Vec<u8>>,
|
||||
inherent_data_providers: sp_inherents::InherentDataProviders,
|
||||
create_inherent_data_providers: CIDP,
|
||||
timeout: Duration,
|
||||
build_time: Duration,
|
||||
can_author_with: CAW,
|
||||
@@ -565,12 +545,9 @@ pub fn start_mining_worker<Block, C, S, Algorithm, E, SO, CAW>(
|
||||
E::Proposer: Proposer<Block, Transaction = sp_api::TransactionFor<C, Block>>,
|
||||
SO: SyncOracle + Clone + Send + Sync + 'static,
|
||||
CAW: CanAuthorWith<Block> + Clone + Send + 'static,
|
||||
CIDP: CreateInherentDataProviders<Block, ()>,
|
||||
{
|
||||
if let Err(_) = register_pow_inherent_data_provider(&inherent_data_providers) {
|
||||
warn!("Registering inherent data provider for timestamp failed");
|
||||
}
|
||||
|
||||
let timer = UntilImportedOrTimeout::new(client.import_notification_stream(), timeout);
|
||||
let mut timer = UntilImportedOrTimeout::new(client.import_notification_stream(), timeout);
|
||||
let worker = Arc::new(Mutex::new(MiningWorker::<Block, Algorithm, C, _> {
|
||||
build: None,
|
||||
algorithm: algorithm.clone(),
|
||||
@@ -578,81 +555,97 @@ pub fn start_mining_worker<Block, C, S, Algorithm, E, SO, CAW>(
|
||||
}));
|
||||
let worker_ret = worker.clone();
|
||||
|
||||
let task = timer.for_each(move |()| {
|
||||
let worker = worker.clone();
|
||||
let task = async move {
|
||||
loop {
|
||||
if timer.next().await.is_none() {
|
||||
break;
|
||||
}
|
||||
|
||||
if sync_oracle.is_major_syncing() {
|
||||
debug!(target: "pow", "Skipping proposal due to sync.");
|
||||
worker.lock().on_major_syncing();
|
||||
return Either::Left(future::ready(()))
|
||||
}
|
||||
if sync_oracle.is_major_syncing() {
|
||||
debug!(target: "pow", "Skipping proposal due to sync.");
|
||||
worker.lock().on_major_syncing();
|
||||
return;
|
||||
}
|
||||
|
||||
let best_header = match select_chain.best_chain() {
|
||||
Ok(x) => x,
|
||||
Err(err) => {
|
||||
let best_header = match select_chain.best_chain() {
|
||||
Ok(x) => x,
|
||||
Err(err) => {
|
||||
warn!(
|
||||
target: "pow",
|
||||
"Unable to pull new block for authoring. \
|
||||
Select best chain error: {:?}",
|
||||
err
|
||||
);
|
||||
return;
|
||||
},
|
||||
};
|
||||
let best_hash = best_header.hash();
|
||||
|
||||
if let Err(err) = can_author_with.can_author_with(&BlockId::Hash(best_hash)) {
|
||||
warn!(
|
||||
target: "pow",
|
||||
"Unable to pull new block for authoring. \
|
||||
Select best chain error: {:?}",
|
||||
err
|
||||
);
|
||||
return Either::Left(future::ready(()))
|
||||
},
|
||||
};
|
||||
let best_hash = best_header.hash();
|
||||
|
||||
if let Err(err) = can_author_with.can_author_with(&BlockId::Hash(best_hash)) {
|
||||
warn!(
|
||||
target: "pow",
|
||||
"Skipping proposal `can_author_with` returned: {} \
|
||||
Probably a node update is required!",
|
||||
err,
|
||||
);
|
||||
return Either::Left(future::ready(()))
|
||||
}
|
||||
|
||||
if worker.lock().best_hash() == Some(best_hash) {
|
||||
return Either::Left(future::ready(()))
|
||||
}
|
||||
|
||||
// The worker is locked for the duration of the whole proposing period. Within this period,
|
||||
// the mining target is outdated and useless anyway.
|
||||
|
||||
let difficulty = match algorithm.difficulty(best_hash) {
|
||||
Ok(x) => x,
|
||||
Err(err) => {
|
||||
warn!(
|
||||
target: "pow",
|
||||
"Unable to propose new block for authoring. \
|
||||
Fetch difficulty failed: {:?}",
|
||||
"Skipping proposal `can_author_with` returned: {} \
|
||||
Probably a node update is required!",
|
||||
err,
|
||||
);
|
||||
return Either::Left(future::ready(()))
|
||||
},
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
let awaiting_proposer = env.init(&best_header);
|
||||
let inherent_data = match inherent_data_providers.create_inherent_data() {
|
||||
Ok(x) => x,
|
||||
Err(err) => {
|
||||
warn!(
|
||||
target: "pow",
|
||||
"Unable to propose new block for authoring. \
|
||||
Creating inherent data failed: {:?}",
|
||||
err,
|
||||
);
|
||||
return Either::Left(future::ready(()))
|
||||
},
|
||||
};
|
||||
let mut inherent_digest = Digest::<Block::Hash>::default();
|
||||
if let Some(pre_runtime) = &pre_runtime {
|
||||
inherent_digest.push(DigestItem::PreRuntime(POW_ENGINE_ID, pre_runtime.to_vec()));
|
||||
}
|
||||
if worker.lock().best_hash() == Some(best_hash) {
|
||||
return;
|
||||
}
|
||||
|
||||
let pre_runtime = pre_runtime.clone();
|
||||
// The worker is locked for the duration of the whole proposing period. Within this period,
|
||||
// the mining target is outdated and useless anyway.
|
||||
|
||||
Either::Right(async move {
|
||||
let proposer = match awaiting_proposer.await {
|
||||
let difficulty = match algorithm.difficulty(best_hash) {
|
||||
Ok(x) => x,
|
||||
Err(err) => {
|
||||
warn!(
|
||||
target: "pow",
|
||||
"Unable to propose new block for authoring. \
|
||||
Fetch difficulty failed: {:?}",
|
||||
err,
|
||||
);
|
||||
return;
|
||||
},
|
||||
};
|
||||
|
||||
let inherent_data_providers =
|
||||
match create_inherent_data_providers.create_inherent_data_providers(best_hash, ()).await {
|
||||
Ok(x) => x,
|
||||
Err(err) => {
|
||||
warn!(
|
||||
target: "pow",
|
||||
"Unable to propose new block for authoring. \
|
||||
Creating inherent data providers failed: {:?}",
|
||||
err,
|
||||
);
|
||||
return;
|
||||
},
|
||||
};
|
||||
|
||||
let inherent_data = match inherent_data_providers.create_inherent_data() {
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
warn!(
|
||||
target: "pow",
|
||||
"Unable to propose new block for authoring. \
|
||||
Creating inherent data failed: {:?}",
|
||||
e,
|
||||
);
|
||||
return;
|
||||
},
|
||||
};
|
||||
|
||||
let mut inherent_digest = Digest::<Block::Hash>::default();
|
||||
if let Some(pre_runtime) = &pre_runtime {
|
||||
inherent_digest.push(DigestItem::PreRuntime(POW_ENGINE_ID, pre_runtime.to_vec()));
|
||||
}
|
||||
|
||||
let pre_runtime = pre_runtime.clone();
|
||||
|
||||
let proposer = match env.init(&best_header).await {
|
||||
Ok(x) => x,
|
||||
Err(err) => {
|
||||
warn!(
|
||||
@@ -694,8 +687,8 @@ pub fn start_mining_worker<Block, C, S, Algorithm, E, SO, CAW>(
|
||||
};
|
||||
|
||||
worker.lock().on_build(build);
|
||||
})
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
(worker_ret, task)
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ futures = "0.3.9"
|
||||
futures-timer = "3.0.1"
|
||||
log = "0.4.11"
|
||||
thiserror = "1.0.21"
|
||||
impl-trait-for-tuples = "0.2.1"
|
||||
async-trait = "0.1.42"
|
||||
|
||||
[dev-dependencies]
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
//! provides generic functionality for slots.
|
||||
|
||||
#![forbid(unsafe_code)]
|
||||
#![deny(missing_docs)]
|
||||
#![warn(missing_docs)]
|
||||
|
||||
mod slots;
|
||||
mod aux_schema;
|
||||
@@ -41,12 +41,13 @@ use sp_api::{ProvideRuntimeApi, ApiRef};
|
||||
use sp_arithmetic::traits::BaseArithmetic;
|
||||
use sp_consensus::{BlockImport, Proposer, SyncOracle, SelectChain, CanAuthorWith, SlotData};
|
||||
use sp_consensus_slots::Slot;
|
||||
use sp_inherents::{InherentData, InherentDataProviders};
|
||||
use sp_inherents::CreateInherentDataProviders;
|
||||
use sp_runtime::{
|
||||
generic::BlockId,
|
||||
traits::{Block as BlockT, Header, HashFor, NumberFor}
|
||||
traits::{Block as BlockT, Header as HeaderT, HashFor, NumberFor}
|
||||
};
|
||||
use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_DEBUG, CONSENSUS_WARN, CONSENSUS_INFO};
|
||||
use sp_timestamp::Timestamp;
|
||||
|
||||
/// The changes that need to applied to the storage to create the state for a block.
|
||||
///
|
||||
@@ -75,8 +76,7 @@ pub trait SlotWorker<B: BlockT, Proof> {
|
||||
/// the slot. Otherwise `None` is returned.
|
||||
async fn on_slot(
|
||||
&mut self,
|
||||
chain_head: B::Header,
|
||||
slot_info: SlotInfo,
|
||||
slot_info: SlotInfo<B>,
|
||||
) -> Option<SlotResult<B, Proof>>;
|
||||
}
|
||||
|
||||
@@ -187,21 +187,19 @@ pub trait SimpleSlotWorker<B: BlockT> {
|
||||
/// Remaining duration for proposing.
|
||||
fn proposing_remaining_duration(
|
||||
&self,
|
||||
head: &B::Header,
|
||||
slot_info: &SlotInfo,
|
||||
slot_info: &SlotInfo<B>,
|
||||
) -> Duration;
|
||||
|
||||
/// Implements [`SlotWorker::on_slot`].
|
||||
async fn on_slot(
|
||||
&mut self,
|
||||
chain_head: B::Header,
|
||||
slot_info: SlotInfo,
|
||||
slot_info: SlotInfo<B>,
|
||||
) -> Option<SlotResult<B, <Self::Proposer as Proposer<B>>::Proof>> {
|
||||
let (timestamp, slot) = (slot_info.timestamp, slot_info.slot);
|
||||
let telemetry = self.telemetry();
|
||||
let logging_target = self.logging_target();
|
||||
|
||||
let proposing_remaining_duration = self.proposing_remaining_duration(&chain_head, &slot_info);
|
||||
let proposing_remaining_duration = self.proposing_remaining_duration(&slot_info);
|
||||
|
||||
let proposing_remaining = if proposing_remaining_duration == Duration::default() {
|
||||
debug!(
|
||||
@@ -215,13 +213,13 @@ pub trait SimpleSlotWorker<B: BlockT> {
|
||||
Delay::new(proposing_remaining_duration)
|
||||
};
|
||||
|
||||
let epoch_data = match self.epoch_data(&chain_head, slot) {
|
||||
let epoch_data = match self.epoch_data(&slot_info.chain_head, slot) {
|
||||
Ok(epoch_data) => epoch_data,
|
||||
Err(err) => {
|
||||
warn!(
|
||||
target: logging_target,
|
||||
"Unable to fetch epoch data at block {:?}: {:?}",
|
||||
chain_head.hash(),
|
||||
slot_info.chain_head.hash(),
|
||||
err,
|
||||
);
|
||||
|
||||
@@ -229,7 +227,7 @@ pub trait SimpleSlotWorker<B: BlockT> {
|
||||
telemetry;
|
||||
CONSENSUS_WARN;
|
||||
"slots.unable_fetching_authorities";
|
||||
"slot" => ?chain_head.hash(),
|
||||
"slot" => ?slot_info.chain_head.hash(),
|
||||
"err" => ?err,
|
||||
);
|
||||
|
||||
@@ -237,7 +235,7 @@ pub trait SimpleSlotWorker<B: BlockT> {
|
||||
}
|
||||
};
|
||||
|
||||
self.notify_slot(&chain_head, slot, &epoch_data);
|
||||
self.notify_slot(&slot_info.chain_head, slot, &epoch_data);
|
||||
|
||||
let authorities_len = self.authorities_len(&epoch_data);
|
||||
|
||||
@@ -256,9 +254,9 @@ pub trait SimpleSlotWorker<B: BlockT> {
|
||||
return None;
|
||||
}
|
||||
|
||||
let claim = self.claim_slot(&chain_head, slot, &epoch_data)?;
|
||||
let claim = self.claim_slot(&slot_info.chain_head, slot, &epoch_data)?;
|
||||
|
||||
if self.should_backoff(slot, &chain_head) {
|
||||
if self.should_backoff(slot, &slot_info.chain_head) {
|
||||
return None;
|
||||
}
|
||||
|
||||
@@ -266,7 +264,7 @@ pub trait SimpleSlotWorker<B: BlockT> {
|
||||
target: self.logging_target(),
|
||||
"Starting authorship at slot {}; timestamp = {}",
|
||||
slot,
|
||||
timestamp,
|
||||
*timestamp,
|
||||
);
|
||||
|
||||
telemetry!(
|
||||
@@ -277,7 +275,7 @@ pub trait SimpleSlotWorker<B: BlockT> {
|
||||
"timestamp" => *timestamp,
|
||||
);
|
||||
|
||||
let proposer = match self.proposer(&chain_head).await {
|
||||
let proposer = match self.proposer(&slot_info.chain_head).await {
|
||||
Ok(p) => p,
|
||||
Err(err) => {
|
||||
warn!(
|
||||
@@ -422,94 +420,119 @@ pub trait SimpleSlotWorker<B: BlockT> {
|
||||
impl<B: BlockT, T: SimpleSlotWorker<B> + Send> SlotWorker<B, <T::Proposer as Proposer<B>>::Proof> for T {
|
||||
async fn on_slot(
|
||||
&mut self,
|
||||
chain_head: B::Header,
|
||||
slot_info: SlotInfo,
|
||||
slot_info: SlotInfo<B>,
|
||||
) -> Option<SlotResult<B, <T::Proposer as Proposer<B>>::Proof>> {
|
||||
SimpleSlotWorker::on_slot(self, chain_head, slot_info).await
|
||||
SimpleSlotWorker::on_slot(self, slot_info).await
|
||||
}
|
||||
}
|
||||
|
||||
/// Slot compatible inherent data.
|
||||
pub trait SlotCompatible {
|
||||
/// Extract timestamp and slot from inherent data.
|
||||
fn extract_timestamp_and_slot(
|
||||
&self,
|
||||
inherent: &InherentData,
|
||||
) -> Result<(sp_timestamp::Timestamp, Slot, std::time::Duration), sp_consensus::Error>;
|
||||
/// Slot specific extension that the inherent data provider needs to implement.
|
||||
pub trait InherentDataProviderExt {
|
||||
/// The current timestamp that will be found in the [`InherentData`].
|
||||
fn timestamp(&self) -> Timestamp;
|
||||
|
||||
/// The current slot that will be found in the [`InherentData`].
|
||||
fn slot(&self) -> Slot;
|
||||
}
|
||||
|
||||
impl<T, S, P> InherentDataProviderExt for (T, S, P)
|
||||
where
|
||||
T: Deref<Target = Timestamp>,
|
||||
S: Deref<Target = Slot>,
|
||||
{
|
||||
fn timestamp(&self) -> Timestamp {
|
||||
*self.0.deref()
|
||||
}
|
||||
|
||||
fn slot(&self) -> Slot {
|
||||
*self.1.deref()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S, P, R> InherentDataProviderExt for (T, S, P, R)
|
||||
where
|
||||
T: Deref<Target = Timestamp>,
|
||||
S: Deref<Target = Slot>,
|
||||
{
|
||||
fn timestamp(&self) -> Timestamp {
|
||||
*self.0.deref()
|
||||
}
|
||||
|
||||
fn slot(&self) -> Slot {
|
||||
*self.1.deref()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> InherentDataProviderExt for (T, S)
|
||||
where
|
||||
T: Deref<Target = Timestamp>,
|
||||
S: Deref<Target = Slot>,
|
||||
{
|
||||
fn timestamp(&self) -> Timestamp {
|
||||
*self.0.deref()
|
||||
}
|
||||
|
||||
fn slot(&self) -> Slot {
|
||||
*self.1.deref()
|
||||
}
|
||||
}
|
||||
|
||||
/// Start a new slot worker.
|
||||
///
|
||||
/// Every time a new slot is triggered, `worker.on_slot` is called and the future it returns is
|
||||
/// polled until completion, unless we are major syncing.
|
||||
pub fn start_slot_worker<B, C, W, T, SO, SC, CAW, Proof>(
|
||||
pub async fn start_slot_worker<B, C, W, T, SO, CAW, CIDP, Proof>(
|
||||
slot_duration: SlotDuration<T>,
|
||||
client: C,
|
||||
mut worker: W,
|
||||
mut sync_oracle: SO,
|
||||
inherent_data_providers: InherentDataProviders,
|
||||
timestamp_extractor: SC,
|
||||
create_inherent_data_providers: CIDP,
|
||||
can_author_with: CAW,
|
||||
) -> impl Future<Output = ()>
|
||||
)
|
||||
where
|
||||
B: BlockT,
|
||||
C: SelectChain<B>,
|
||||
W: SlotWorker<B, Proof>,
|
||||
SO: SyncOracle + Send,
|
||||
SC: SlotCompatible + Unpin,
|
||||
T: SlotData + Clone,
|
||||
CAW: CanAuthorWith<B> + Send,
|
||||
CIDP: CreateInherentDataProviders<B, ()> + Send,
|
||||
CIDP::InherentDataProviders: InherentDataProviderExt + Send,
|
||||
{
|
||||
let SlotDuration(slot_duration) = slot_duration;
|
||||
|
||||
// rather than use a timer interval, we schedule our waits ourselves
|
||||
let mut slots = Slots::<SC>::new(
|
||||
let mut slots = Slots::new(
|
||||
slot_duration.slot_duration(),
|
||||
inherent_data_providers,
|
||||
timestamp_extractor,
|
||||
create_inherent_data_providers,
|
||||
client,
|
||||
);
|
||||
|
||||
async move {
|
||||
loop {
|
||||
let slot_info = match slots.next_slot().await {
|
||||
Ok(slot) => slot,
|
||||
Err(err) => {
|
||||
debug!(target: "slots", "Faulty timer: {:?}", err);
|
||||
return
|
||||
},
|
||||
};
|
||||
|
||||
// only propose when we are not syncing.
|
||||
if sync_oracle.is_major_syncing() {
|
||||
debug!(target: "slots", "Skipping proposal slot due to sync.");
|
||||
continue;
|
||||
loop {
|
||||
let slot_info = match slots.next_slot().await {
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
warn!(target: "slots", "Error while polling for next slot: {:?}", e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let slot = slot_info.slot;
|
||||
let chain_head = match client.best_chain() {
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
warn!(
|
||||
target: "slots",
|
||||
"Unable to author block in slot {}. No best block header: {:?}",
|
||||
slot,
|
||||
e,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
if sync_oracle.is_major_syncing() {
|
||||
debug!(target: "slots", "Skipping proposal slot due to sync.");
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Err(err) = can_author_with.can_author_with(&BlockId::Hash(chain_head.hash())) {
|
||||
warn!(
|
||||
target: "slots",
|
||||
"Unable to author block in slot {},. `can_author_with` returned: {} \
|
||||
Probably a node update is required!",
|
||||
slot,
|
||||
err,
|
||||
);
|
||||
} else {
|
||||
worker.on_slot(chain_head, slot_info).await;
|
||||
}
|
||||
if let Err(err) = can_author_with
|
||||
.can_author_with(&BlockId::Hash(slot_info.chain_head.hash()))
|
||||
{
|
||||
warn!(
|
||||
target: "slots",
|
||||
"Unable to author block in slot {},. `can_author_with` returned: {} \
|
||||
Probably a node update is required!",
|
||||
slot_info.slot,
|
||||
err,
|
||||
);
|
||||
} else {
|
||||
let _ = worker.on_slot(slot_info).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -627,7 +650,10 @@ impl SlotProportion {
|
||||
/// to parent. If the number of skipped slots is greated than 0 this method will apply
|
||||
/// an exponential backoff of at most `2^7 * slot_duration`, if no slots were skipped
|
||||
/// this method will return `None.`
|
||||
pub fn slot_lenience_exponential(parent_slot: Slot, slot_info: &SlotInfo) -> Option<Duration> {
|
||||
pub fn slot_lenience_exponential<Block: BlockT>(
|
||||
parent_slot: Slot,
|
||||
slot_info: &SlotInfo<Block>,
|
||||
) -> Option<Duration> {
|
||||
// never give more than 2^this times the lenience.
|
||||
const BACKOFF_CAP: u64 = 7;
|
||||
|
||||
@@ -656,7 +682,10 @@ pub fn slot_lenience_exponential(parent_slot: Slot, slot_info: &SlotInfo) -> Opt
|
||||
/// to parent. If the number of skipped slots is greated than 0 this method will apply
|
||||
/// a linear backoff of at most `20 * slot_duration`, if no slots were skipped
|
||||
/// this method will return `None.`
|
||||
pub fn slot_lenience_linear(parent_slot: Slot, slot_info: &SlotInfo) -> Option<Duration> {
|
||||
pub fn slot_lenience_linear<Block: BlockT>(
|
||||
parent_slot: u64,
|
||||
slot_info: &SlotInfo<Block>,
|
||||
) -> Option<Duration> {
|
||||
// never give more than 20 times more lenience.
|
||||
const BACKOFF_CAP: u64 = 20;
|
||||
|
||||
@@ -777,20 +806,27 @@ impl<N> BackoffAuthoringBlocksStrategy<N> for () {
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use std::time::{Duration, Instant};
|
||||
use crate::{BackoffAuthoringOnFinalizedHeadLagging, BackoffAuthoringBlocksStrategy};
|
||||
use substrate_test_runtime_client::runtime::Block;
|
||||
use substrate_test_runtime_client::runtime::{Block, Header};
|
||||
use sp_api::NumberFor;
|
||||
|
||||
const SLOT_DURATION: Duration = Duration::from_millis(6000);
|
||||
|
||||
fn slot(slot: u64) -> super::slots::SlotInfo {
|
||||
fn slot(slot: u64) -> super::slots::SlotInfo<Block> {
|
||||
super::slots::SlotInfo {
|
||||
slot: slot.into(),
|
||||
duration: SLOT_DURATION,
|
||||
timestamp: Default::default(),
|
||||
inherent_data: Default::default(),
|
||||
ends_at: Instant::now(),
|
||||
chain_head: Header::new(
|
||||
1,
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
),
|
||||
block_size_limit: None,
|
||||
}
|
||||
}
|
||||
@@ -798,20 +834,20 @@ mod test {
|
||||
#[test]
|
||||
fn linear_slot_lenience() {
|
||||
// if no slots are skipped there should be no lenience
|
||||
assert_eq!(super::slot_lenience_linear(1.into(), &slot(2)), None);
|
||||
assert_eq!(super::slot_lenience_linear(1u64.into(), &slot(2)), None);
|
||||
|
||||
// otherwise the lenience is incremented linearly with
|
||||
// the number of skipped slots.
|
||||
for n in 3..=22 {
|
||||
assert_eq!(
|
||||
super::slot_lenience_linear(1.into(), &slot(n)),
|
||||
super::slot_lenience_linear(1u64.into(), &slot(n)),
|
||||
Some(SLOT_DURATION * (n - 2) as u32),
|
||||
);
|
||||
}
|
||||
|
||||
// but we cap it to a maximum of 20 slots
|
||||
assert_eq!(
|
||||
super::slot_lenience_linear(1.into(), &slot(23)),
|
||||
super::slot_lenience_linear(1u64.into(), &slot(23)),
|
||||
Some(SLOT_DURATION * 20),
|
||||
);
|
||||
}
|
||||
@@ -819,24 +855,24 @@ mod test {
|
||||
#[test]
|
||||
fn exponential_slot_lenience() {
|
||||
// if no slots are skipped there should be no lenience
|
||||
assert_eq!(super::slot_lenience_exponential(1.into(), &slot(2)), None);
|
||||
assert_eq!(super::slot_lenience_exponential(1u64.into(), &slot(2)), None);
|
||||
|
||||
// otherwise the lenience is incremented exponentially every two slots
|
||||
for n in 3..=17 {
|
||||
assert_eq!(
|
||||
super::slot_lenience_exponential(1.into(), &slot(n)),
|
||||
super::slot_lenience_exponential(1u64.into(), &slot(n)),
|
||||
Some(SLOT_DURATION * 2u32.pow((n / 2 - 1) as u32)),
|
||||
);
|
||||
}
|
||||
|
||||
// but we cap it to a maximum of 14 slots
|
||||
assert_eq!(
|
||||
super::slot_lenience_exponential(1.into(), &slot(18)),
|
||||
super::slot_lenience_exponential(1u64.into(), &slot(18)),
|
||||
Some(SLOT_DURATION * 2u32.pow(7)),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
super::slot_lenience_exponential(1.into(), &slot(19)),
|
||||
super::slot_lenience_exponential(1u64.into(), &slot(19)),
|
||||
Some(SLOT_DURATION * 2u32.pow(7)),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -20,9 +20,10 @@
|
||||
//!
|
||||
//! This is used instead of `futures_timer::Interval` because it was unreliable.
|
||||
|
||||
use super::{SlotCompatible, Slot};
|
||||
use sp_consensus::Error;
|
||||
use sp_inherents::{InherentData, InherentDataProviders};
|
||||
use super::{Slot, InherentDataProviderExt};
|
||||
use sp_consensus::{Error, SelectChain};
|
||||
use sp_inherents::{InherentData, CreateInherentDataProviders, InherentDataProvider};
|
||||
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
|
||||
|
||||
use std::time::{Duration, Instant};
|
||||
use futures_timer::Delay;
|
||||
@@ -38,19 +39,19 @@ pub fn duration_now() -> Duration {
|
||||
))
|
||||
}
|
||||
|
||||
/// Returns the duration until the next slot, based on current duration since
|
||||
pub fn time_until_next(now: Duration, slot_duration: Duration) -> Duration {
|
||||
/// Returns the duration until the next slot from now.
|
||||
pub fn time_until_next(slot_duration: Duration) -> Duration {
|
||||
let remaining_full_millis = slot_duration.as_millis()
|
||||
- (now.as_millis() % slot_duration.as_millis())
|
||||
- (duration_now().as_millis() % slot_duration.as_millis())
|
||||
- 1;
|
||||
Duration::from_millis(remaining_full_millis as u64)
|
||||
}
|
||||
|
||||
/// Information about a slot.
|
||||
pub struct SlotInfo {
|
||||
/// The slot number.
|
||||
pub struct SlotInfo<B: BlockT> {
|
||||
/// The slot number as found in the inherent data.
|
||||
pub slot: Slot,
|
||||
/// Current timestamp.
|
||||
/// Current timestamp as found in the inherent data.
|
||||
pub timestamp: sp_timestamp::Timestamp,
|
||||
/// The instant at which the slot ends.
|
||||
pub ends_at: Instant,
|
||||
@@ -58,13 +59,15 @@ pub struct SlotInfo {
|
||||
pub inherent_data: InherentData,
|
||||
/// Slot duration.
|
||||
pub duration: Duration,
|
||||
/// The chain header this slot is based on.
|
||||
pub chain_head: B::Header,
|
||||
/// Some potential block size limit for the block to be authored at this slot.
|
||||
///
|
||||
/// For more information see [`Proposer::propose`](sp_consensus::Proposer::propose).
|
||||
pub block_size_limit: Option<usize>,
|
||||
}
|
||||
|
||||
impl SlotInfo {
|
||||
impl<B: BlockT> SlotInfo<B> {
|
||||
/// Create a new [`SlotInfo`].
|
||||
///
|
||||
/// `ends_at` is calculated using `timestamp` and `duration`.
|
||||
@@ -73,6 +76,7 @@ impl SlotInfo {
|
||||
timestamp: sp_timestamp::Timestamp,
|
||||
inherent_data: InherentData,
|
||||
duration: Duration,
|
||||
chain_head: B::Header,
|
||||
block_size_limit: Option<usize>,
|
||||
) -> Self {
|
||||
Self {
|
||||
@@ -80,46 +84,55 @@ impl SlotInfo {
|
||||
timestamp,
|
||||
inherent_data,
|
||||
duration,
|
||||
chain_head,
|
||||
block_size_limit,
|
||||
ends_at: Instant::now() + time_until_next(timestamp.as_duration(), duration),
|
||||
ends_at: Instant::now() + time_until_next(duration),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A stream that returns every time there is a new slot.
|
||||
pub(crate) struct Slots<SC> {
|
||||
pub(crate) struct Slots<Block, C, IDP> {
|
||||
last_slot: Slot,
|
||||
slot_duration: Duration,
|
||||
inner_delay: Option<Delay>,
|
||||
inherent_data_providers: InherentDataProviders,
|
||||
timestamp_extractor: SC,
|
||||
create_inherent_data_providers: IDP,
|
||||
client: C,
|
||||
_phantom: std::marker::PhantomData<Block>,
|
||||
}
|
||||
|
||||
impl<SC> Slots<SC> {
|
||||
impl<Block, C, IDP> Slots<Block, C, IDP> {
|
||||
/// Create a new `Slots` stream.
|
||||
pub fn new(
|
||||
slot_duration: Duration,
|
||||
inherent_data_providers: InherentDataProviders,
|
||||
timestamp_extractor: SC,
|
||||
create_inherent_data_providers: IDP,
|
||||
client: C,
|
||||
) -> Self {
|
||||
Slots {
|
||||
last_slot: 0.into(),
|
||||
slot_duration,
|
||||
inner_delay: None,
|
||||
inherent_data_providers,
|
||||
timestamp_extractor,
|
||||
create_inherent_data_providers,
|
||||
client,
|
||||
_phantom: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<SC: SlotCompatible> Slots<SC> {
|
||||
impl<Block, C, IDP> Slots<Block, C, IDP>
|
||||
where
|
||||
Block: BlockT,
|
||||
C: SelectChain<Block>,
|
||||
IDP: CreateInherentDataProviders<Block, ()>,
|
||||
IDP::InherentDataProviders: crate::InherentDataProviderExt,
|
||||
{
|
||||
/// Returns a future that fires when the next slot starts.
|
||||
pub async fn next_slot(&mut self) -> Result<SlotInfo, Error> {
|
||||
pub async fn next_slot(&mut self) -> Result<SlotInfo<Block>, Error> {
|
||||
loop {
|
||||
self.inner_delay = match self.inner_delay.take() {
|
||||
None => {
|
||||
// schedule wait.
|
||||
let wait_dur = time_until_next(duration_now(), self.slot_duration);
|
||||
let wait_dur = time_until_next(self.slot_duration);
|
||||
Some(Delay::new(wait_dur))
|
||||
}
|
||||
Some(d) => Some(d),
|
||||
@@ -130,15 +143,39 @@ impl<SC: SlotCompatible> Slots<SC> {
|
||||
}
|
||||
// timeout has fired.
|
||||
|
||||
let inherent_data = match self.inherent_data_providers.create_inherent_data() {
|
||||
Ok(id) => id,
|
||||
Err(err) => return Err(sp_consensus::Error::InherentData(err)),
|
||||
let ends_at = Instant::now() + time_until_next(self.slot_duration);
|
||||
|
||||
let chain_head = match self.client.best_chain() {
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
log::warn!(
|
||||
target: "slots",
|
||||
"Unable to author block in slot. No best block header: {:?}",
|
||||
e,
|
||||
);
|
||||
// Let's try at the next slot..
|
||||
self.inner_delay.take();
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let result = self.timestamp_extractor.extract_timestamp_and_slot(&inherent_data);
|
||||
let (timestamp, slot, offset) = result?;
|
||||
|
||||
let inherent_data_providers = self.create_inherent_data_providers
|
||||
.create_inherent_data_providers(chain_head.hash(), ())
|
||||
.await?;
|
||||
|
||||
if Instant::now() > ends_at {
|
||||
log::warn!(
|
||||
target: "slots",
|
||||
"Creating inherent data providers took more time than we had left for the slot.",
|
||||
);
|
||||
}
|
||||
|
||||
let timestamp = inherent_data_providers.timestamp();
|
||||
let slot = inherent_data_providers.slot();
|
||||
let inherent_data = inherent_data_providers.create_inherent_data()?;
|
||||
|
||||
// reschedule delay for next slot.
|
||||
let ends_in = offset +
|
||||
time_until_next(timestamp.as_duration(), self.slot_duration);
|
||||
let ends_in = time_until_next(self.slot_duration);
|
||||
self.inner_delay = Some(Delay::new(ends_in));
|
||||
|
||||
// never yield the same slot twice.
|
||||
@@ -150,6 +187,7 @@ impl<SC: SlotCompatible> Slots<SC> {
|
||||
timestamp,
|
||||
inherent_data,
|
||||
self.slot_duration,
|
||||
chain_head,
|
||||
None,
|
||||
))
|
||||
}
|
||||
|
||||
@@ -14,9 +14,6 @@ targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
[dependencies]
|
||||
sc-client-api = { version = "3.0.0", path = "../../api" }
|
||||
sp-core = { version = "3.0.0", path = "../../../primitives/core" }
|
||||
sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" }
|
||||
sp-authorship = { version = "3.0.0", path = "../../../primitives/authorship" }
|
||||
sp-consensus = { version = "0.9.0", path = "../../../primitives/consensus/common" }
|
||||
sp-inherents = { version = "3.0.0", path = "../../../primitives/inherents" }
|
||||
log = "0.4.8"
|
||||
thiserror = "1.0.21"
|
||||
|
||||
@@ -17,51 +17,28 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
//! Uncles functionality for Substrate.
|
||||
#![forbid(unsafe_code, missing_docs)]
|
||||
|
||||
use sp_consensus::SelectChain;
|
||||
use sp_inherents::{InherentDataProviders};
|
||||
use log::warn;
|
||||
use sc_client_api::ProvideUncles;
|
||||
use sp_runtime::traits::{Block as BlockT, Header};
|
||||
use std::sync::Arc;
|
||||
use sp_authorship;
|
||||
use sp_runtime::{traits::Block as BlockT, generic::BlockId};
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error<B: BlockT> {
|
||||
#[error("Could not retrieve the block hash for block id: {0:?}")]
|
||||
NoHashForBlockId(BlockId<B>),
|
||||
}
|
||||
|
||||
/// Maximum uncles generations we may provide to the runtime.
|
||||
const MAX_UNCLE_GENERATIONS: u32 = 8;
|
||||
|
||||
/// Register uncles inherent data provider, if not registered already.
|
||||
pub fn register_uncles_inherent_data_provider<B, C, SC>(
|
||||
client: Arc<C>,
|
||||
select_chain: SC,
|
||||
inherent_data_providers: &InherentDataProviders,
|
||||
) -> Result<(), sp_consensus::Error> where
|
||||
/// Create a new [`sp_authorship::InherentDataProvider`] at the given block.
|
||||
pub fn create_uncles_inherent_data_provider<B, C>(
|
||||
client: &C,
|
||||
parent: B::Hash,
|
||||
) -> Result<sp_authorship::InherentDataProvider<B::Header>, sc_client_api::blockchain::Error> where
|
||||
B: BlockT,
|
||||
C: ProvideUncles<B> + Send + Sync + 'static,
|
||||
SC: SelectChain<B> + 'static,
|
||||
C: ProvideUncles<B>,
|
||||
{
|
||||
if !inherent_data_providers.has_provider(&sp_authorship::INHERENT_IDENTIFIER) {
|
||||
inherent_data_providers
|
||||
.register_provider(sp_authorship::InherentDataProvider::new(move || {
|
||||
{
|
||||
let chain_head = match select_chain.best_chain() {
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
warn!(target: "uncles", "Unable to get chain head: {:?}", e);
|
||||
return Vec::new();
|
||||
}
|
||||
};
|
||||
match client.uncles(chain_head.hash(), MAX_UNCLE_GENERATIONS.into()) {
|
||||
Ok(uncles) => uncles,
|
||||
Err(e) => {
|
||||
warn!(target: "uncles", "Unable to get uncles: {:?}", e);
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
}
|
||||
}))
|
||||
.map_err(|err| sp_consensus::Error::InherentData(err.into()))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
let uncles = client.uncles(parent, MAX_UNCLE_GENERATIONS.into())?;
|
||||
|
||||
Ok(sp_authorship::InherentDataProvider::new(uncles))
|
||||
}
|
||||
|
||||
@@ -44,8 +44,8 @@ use sp_runtime::{
|
||||
Justification, Justifications, BuildStorage,
|
||||
generic::{BlockId, SignedBlock, DigestItem},
|
||||
traits::{
|
||||
Block as BlockT, Header as HeaderT, Zero, NumberFor,
|
||||
HashFor, SaturatedConversion, One, DigestFor, UniqueSaturatedInto,
|
||||
Block as BlockT, Header as HeaderT, Zero, NumberFor, HashFor, SaturatedConversion, One,
|
||||
DigestFor,
|
||||
},
|
||||
};
|
||||
use sp_state_machine::{
|
||||
@@ -1149,14 +1149,20 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
|
||||
}
|
||||
|
||||
/// Gets the uncles of the block with `target_hash` going back `max_generation` ancestors.
|
||||
pub fn uncles(&self, target_hash: Block::Hash, max_generation: NumberFor<Block>) -> sp_blockchain::Result<Vec<Block::Hash>> {
|
||||
pub fn uncles(
|
||||
&self,
|
||||
target_hash: Block::Hash,
|
||||
max_generation: NumberFor<Block>,
|
||||
) -> sp_blockchain::Result<Vec<Block::Hash>> {
|
||||
let load_header = |id: Block::Hash| -> sp_blockchain::Result<Block::Header> {
|
||||
self.backend.blockchain().header(BlockId::Hash(id))?
|
||||
.ok_or_else(|| Error::UnknownBlock(format!("{:?}", id)))
|
||||
};
|
||||
|
||||
let genesis_hash = self.backend.blockchain().info().genesis_hash;
|
||||
if genesis_hash == target_hash { return Ok(Vec::new()); }
|
||||
if genesis_hash == target_hash {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
|
||||
let mut current_hash = target_hash;
|
||||
let mut current = load_header(current_hash)?;
|
||||
@@ -1164,14 +1170,20 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
|
||||
let mut ancestor = load_header(ancestor_hash)?;
|
||||
let mut uncles = Vec::new();
|
||||
|
||||
for _generation in 0u32..UniqueSaturatedInto::<u32>::unique_saturated_into(max_generation) {
|
||||
let mut generation: NumberFor<Block> = Zero::zero();
|
||||
while generation < max_generation {
|
||||
let children = self.backend.blockchain().children(ancestor_hash)?;
|
||||
uncles.extend(children.into_iter().filter(|h| h != ¤t_hash));
|
||||
current_hash = ancestor_hash;
|
||||
if genesis_hash == current_hash { break; }
|
||||
|
||||
if genesis_hash == current_hash {
|
||||
break;
|
||||
}
|
||||
|
||||
current = ancestor;
|
||||
ancestor_hash = *current.parent_hash();
|
||||
ancestor = load_header(ancestor_hash)?;
|
||||
generation += One::one();
|
||||
}
|
||||
trace!("Collected {} uncles", uncles.len());
|
||||
Ok(uncles)
|
||||
|
||||
@@ -176,8 +176,6 @@ pub struct PartialComponents<Client, Backend, SelectChain, ImportQueue, Transact
|
||||
pub import_queue: ImportQueue,
|
||||
/// A shared transaction pool.
|
||||
pub transaction_pool: Arc<TransactionPool>,
|
||||
/// A registry of all providers of `InherentData`.
|
||||
pub inherent_data_providers: sp_inherents::InherentDataProviders,
|
||||
/// Everything else that needs to be passed into the main build function.
|
||||
pub other: Other,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user