mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-18 17:35:42 +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:
@@ -17,6 +17,7 @@ sp-inherents = { version = "3.0.0", default-features = false, path = "../inheren
|
||||
sp-runtime = { version = "3.0.0", default-features = false, path = "../runtime" }
|
||||
sp-std = { version = "3.0.0", default-features = false, path = "../std" }
|
||||
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
|
||||
async-trait = { version = "0.1.48", optional = true }
|
||||
|
||||
[features]
|
||||
default = [ "std" ]
|
||||
@@ -25,4 +26,5 @@ std = [
|
||||
"sp-std/std",
|
||||
"sp-inherents/std",
|
||||
"sp-runtime/std",
|
||||
"async-trait",
|
||||
]
|
||||
|
||||
@@ -23,7 +23,7 @@ use sp_std::{result::Result, prelude::*};
|
||||
|
||||
use codec::{Encode, Decode};
|
||||
use sp_inherents::{Error, InherentIdentifier, InherentData, IsFatalError};
|
||||
use sp_runtime::RuntimeString;
|
||||
use sp_runtime::{RuntimeString, traits::Header as HeaderT};
|
||||
|
||||
/// The identifier for the `uncles` inherent.
|
||||
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"uncles00";
|
||||
@@ -44,12 +44,12 @@ impl IsFatalError for InherentError {
|
||||
}
|
||||
|
||||
/// Auxiliary trait to extract uncles inherent data.
|
||||
pub trait UnclesInherentData<H: Decode> {
|
||||
pub trait UnclesInherentData<H> {
|
||||
/// Get uncles.
|
||||
fn uncles(&self) -> Result<Vec<H>, Error>;
|
||||
}
|
||||
|
||||
impl<H: Decode> UnclesInherentData<H> for InherentData {
|
||||
impl<H: HeaderT> UnclesInherentData<H> for InherentData {
|
||||
fn uncles(&self) -> Result<Vec<H>, Error> {
|
||||
Ok(self.get_data(&INHERENT_IDENTIFIER)?.unwrap_or_default())
|
||||
}
|
||||
@@ -57,36 +57,43 @@ impl<H: Decode> UnclesInherentData<H> for InherentData {
|
||||
|
||||
/// Provider for inherent data.
|
||||
#[cfg(feature = "std")]
|
||||
pub struct InherentDataProvider<F, H> {
|
||||
inner: F,
|
||||
_marker: std::marker::PhantomData<H>,
|
||||
pub struct InherentDataProvider<H> {
|
||||
uncles: Vec<H>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<F, H> InherentDataProvider<F, H> {
|
||||
pub fn new(uncles_oracle: F) -> Self {
|
||||
InherentDataProvider { inner: uncles_oracle, _marker: Default::default() }
|
||||
impl<H> InherentDataProvider<H> {
|
||||
/// Create a new inherent data provider with the given `uncles`.
|
||||
pub fn new(uncles: Vec<H>) -> Self {
|
||||
InherentDataProvider { uncles }
|
||||
}
|
||||
|
||||
/// Create a new instance that is usable for checking inherents.
|
||||
///
|
||||
/// This will always return an empty vec of uncles.
|
||||
pub fn check_inherents() -> Self {
|
||||
Self { uncles: Vec::new() }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<F, H: Encode + std::fmt::Debug> sp_inherents::ProvideInherentData for InherentDataProvider<F, H>
|
||||
where F: Fn() -> Vec<H>
|
||||
{
|
||||
fn inherent_identifier(&self) -> &'static InherentIdentifier {
|
||||
&INHERENT_IDENTIFIER
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<H: HeaderT> sp_inherents::InherentDataProvider for InherentDataProvider<H> {
|
||||
fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error> {
|
||||
let uncles = (self.inner)();
|
||||
if !uncles.is_empty() {
|
||||
inherent_data.put_data(INHERENT_IDENTIFIER, &uncles)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
inherent_data.put_data(INHERENT_IDENTIFIER, &self.uncles)
|
||||
}
|
||||
|
||||
fn error_to_string(&self, _error: &[u8]) -> Option<String> {
|
||||
Some(format!("no further information"))
|
||||
async fn try_handle_error(
|
||||
&self,
|
||||
identifier: &InherentIdentifier,
|
||||
error: &[u8],
|
||||
) -> Option<Result<(), Error>> {
|
||||
if *identifier != INHERENT_IDENTIFIER {
|
||||
return None
|
||||
}
|
||||
|
||||
let error = InherentError::decode(&mut &error[..]).ok()?;
|
||||
|
||||
Some(Err(Error::Application(Box::from(format!("{:?}", error)))))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ sp-inherents = { version = "3.0.0", default-features = false, path = "../../inhe
|
||||
sp-timestamp = { version = "3.0.0", default-features = false, path = "../../timestamp" }
|
||||
sp-consensus-slots = { version = "0.9.0", default-features = false, path = "../slots" }
|
||||
sp-consensus = { version = "0.9.0", path = "../common", optional = true }
|
||||
async-trait = { version = "0.1.48", optional = true }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
@@ -35,4 +36,5 @@ std = [
|
||||
"sp-timestamp/std",
|
||||
"sp-consensus-slots/std",
|
||||
"sp-consensus",
|
||||
"async-trait",
|
||||
]
|
||||
|
||||
@@ -19,9 +19,6 @@
|
||||
|
||||
use sp_inherents::{InherentIdentifier, InherentData, Error};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use sp_inherents::{InherentDataProviders, ProvideInherentData};
|
||||
|
||||
/// The Aura inherent identifier.
|
||||
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"auraslot";
|
||||
|
||||
@@ -31,15 +28,14 @@ pub type InherentType = sp_consensus_slots::Slot;
|
||||
/// Auxiliary trait to extract Aura inherent data.
|
||||
pub trait AuraInherentData {
|
||||
/// Get aura inherent data.
|
||||
fn aura_inherent_data(&self) ->Result<InherentType, Error>;
|
||||
fn aura_inherent_data(&self) ->Result<Option<InherentType>, Error>;
|
||||
/// Replace aura inherent data.
|
||||
fn aura_replace_inherent_data(&mut self, new: InherentType);
|
||||
}
|
||||
|
||||
impl AuraInherentData for InherentData {
|
||||
fn aura_inherent_data(&self) ->Result<InherentType, Error> {
|
||||
fn aura_inherent_data(&self) ->Result<Option<InherentType>, Error> {
|
||||
self.get_data(&INHERENT_IDENTIFIER)
|
||||
.and_then(|r| r.ok_or_else(|| "Aura inherent data not found".into()))
|
||||
}
|
||||
|
||||
fn aura_replace_inherent_data(&mut self, new: InherentType) {
|
||||
@@ -51,50 +47,59 @@ impl AuraInherentData for InherentData {
|
||||
// TODO: Remove in the future. https://github.com/paritytech/substrate/issues/8029
|
||||
#[cfg(feature = "std")]
|
||||
pub struct InherentDataProvider {
|
||||
slot_duration: std::time::Duration,
|
||||
slot: InherentType,
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl InherentDataProvider {
|
||||
pub fn new(slot_duration: std::time::Duration) -> Self {
|
||||
/// Create a new instance with the given slot.
|
||||
pub fn new(slot: InherentType) -> Self {
|
||||
Self {
|
||||
slot_duration
|
||||
slot,
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates the inherent data provider by calculating the slot from the given
|
||||
/// `timestamp` and `duration`.
|
||||
pub fn from_timestamp_and_duration(
|
||||
timestamp: sp_timestamp::Timestamp,
|
||||
duration: std::time::Duration,
|
||||
) -> Self {
|
||||
let slot = InherentType::from(
|
||||
(timestamp.as_duration().as_millis() / duration.as_millis()) as u64
|
||||
);
|
||||
|
||||
Self {
|
||||
slot,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl ProvideInherentData for InherentDataProvider {
|
||||
fn on_register(
|
||||
&self,
|
||||
providers: &InherentDataProviders,
|
||||
) ->Result<(), Error> {
|
||||
if !providers.has_provider(&sp_timestamp::INHERENT_IDENTIFIER) {
|
||||
// Add the timestamp inherent data provider, as we require it.
|
||||
providers.register_provider(sp_timestamp::InherentDataProvider)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
impl sp_std::ops::Deref for InherentDataProvider {
|
||||
type Target = InherentType;
|
||||
|
||||
fn inherent_identifier(&self) -> &'static InherentIdentifier {
|
||||
&INHERENT_IDENTIFIER
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.slot
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[async_trait::async_trait]
|
||||
impl sp_inherents::InherentDataProvider for InherentDataProvider {
|
||||
fn provide_inherent_data(
|
||||
&self,
|
||||
inherent_data: &mut InherentData,
|
||||
) ->Result<(), Error> {
|
||||
use sp_timestamp::TimestampInherentData;
|
||||
|
||||
let timestamp = inherent_data.timestamp_inherent_data()?;
|
||||
let slot = *timestamp / self.slot_duration.as_millis() as u64;
|
||||
inherent_data.put_data(INHERENT_IDENTIFIER, &slot)
|
||||
inherent_data.put_data(INHERENT_IDENTIFIER, &self.slot)
|
||||
}
|
||||
|
||||
fn error_to_string(&self, error: &[u8]) -> Option<String> {
|
||||
use codec::Decode;
|
||||
|
||||
sp_inherents::Error::decode(&mut &error[..]).map(|e| e.into_string()).ok()
|
||||
async fn try_handle_error(
|
||||
&self,
|
||||
_: &InherentIdentifier,
|
||||
_: &[u8],
|
||||
) -> Option<Result<(), Error>> {
|
||||
// There is no error anymore
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,8 +25,9 @@ sp-core = { version = "3.0.0", default-features = false, path = "../../core" }
|
||||
sp-inherents = { version = "3.0.0", default-features = false, path = "../../inherents" }
|
||||
sp-keystore = { version = "0.9.0", default-features = false, path = "../../keystore", optional = true }
|
||||
sp-runtime = { version = "3.0.0", default-features = false, path = "../../runtime" }
|
||||
sp-timestamp = { version = "3.0.0", default-features = false, path = "../../timestamp" }
|
||||
sp-timestamp = { version = "3.0.0", path = "../../timestamp", optional = true }
|
||||
serde = { version = "1.0.123", features = ["derive"], optional = true }
|
||||
async-trait = { version = "0.1.48", optional = true }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
@@ -43,6 +44,7 @@ std = [
|
||||
"sp-inherents/std",
|
||||
"sp-keystore",
|
||||
"sp-runtime/std",
|
||||
"sp-timestamp/std",
|
||||
"serde",
|
||||
"sp-timestamp",
|
||||
"async-trait",
|
||||
]
|
||||
|
||||
@@ -17,14 +17,8 @@
|
||||
|
||||
//! Inherents for BABE
|
||||
|
||||
use sp_inherents::{Error, InherentData, InherentIdentifier};
|
||||
#[cfg(feature = "std")]
|
||||
use sp_inherents::{InherentDataProviders, ProvideInherentData};
|
||||
#[cfg(feature = "std")]
|
||||
use sp_timestamp::TimestampInherentData;
|
||||
use sp_inherents::{InherentData, InherentIdentifier, Error};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use codec::Decode;
|
||||
use sp_std::result::Result;
|
||||
|
||||
/// The BABE inherent identifier.
|
||||
@@ -35,15 +29,14 @@ pub type InherentType = sp_consensus_slots::Slot;
|
||||
/// Auxiliary trait to extract BABE inherent data.
|
||||
pub trait BabeInherentData {
|
||||
/// Get BABE inherent data.
|
||||
fn babe_inherent_data(&self) -> Result<InherentType, Error>;
|
||||
fn babe_inherent_data(&self) -> Result<Option<InherentType>, Error>;
|
||||
/// Replace BABE inherent data.
|
||||
fn babe_replace_inherent_data(&mut self, new: InherentType);
|
||||
}
|
||||
|
||||
impl BabeInherentData for InherentData {
|
||||
fn babe_inherent_data(&self) -> Result<InherentType, Error> {
|
||||
fn babe_inherent_data(&self) -> Result<Option<InherentType>, Error> {
|
||||
self.get_data(&INHERENT_IDENTIFIER)
|
||||
.and_then(|r| r.ok_or_else(|| "BABE inherent data not found".into()))
|
||||
}
|
||||
|
||||
fn babe_replace_inherent_data(&mut self, new: InherentType) {
|
||||
@@ -55,39 +48,59 @@ impl BabeInherentData for InherentData {
|
||||
// TODO: Remove in the future. https://github.com/paritytech/substrate/issues/8029
|
||||
#[cfg(feature = "std")]
|
||||
pub struct InherentDataProvider {
|
||||
slot_duration: std::time::Duration,
|
||||
slot: InherentType,
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl InherentDataProvider {
|
||||
/// Constructs `Self`
|
||||
pub fn new(slot_duration: std::time::Duration) -> Self {
|
||||
Self { slot_duration }
|
||||
/// Create new inherent data provider from the given `slot`.
|
||||
pub fn new(slot: InherentType) -> Self {
|
||||
Self { slot }
|
||||
}
|
||||
|
||||
/// Creates the inherent data provider by calculating the slot from the given
|
||||
/// `timestamp` and `duration`.
|
||||
pub fn from_timestamp_and_duration(
|
||||
timestamp: sp_timestamp::Timestamp,
|
||||
duration: std::time::Duration,
|
||||
) -> Self {
|
||||
let slot = InherentType::from(
|
||||
(timestamp.as_duration().as_millis() / duration.as_millis()) as u64
|
||||
);
|
||||
|
||||
Self {
|
||||
slot,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the `slot` of this inherent data provider.
|
||||
pub fn slot(&self) -> InherentType {
|
||||
self.slot
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl ProvideInherentData for InherentDataProvider {
|
||||
fn on_register(&self, providers: &InherentDataProviders) -> Result<(), Error> {
|
||||
if !providers.has_provider(&sp_timestamp::INHERENT_IDENTIFIER) {
|
||||
// Add the timestamp inherent data provider, as we require it.
|
||||
providers.register_provider(sp_timestamp::InherentDataProvider)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
impl sp_std::ops::Deref for InherentDataProvider {
|
||||
type Target = InherentType;
|
||||
|
||||
fn inherent_identifier(&self) -> &'static InherentIdentifier {
|
||||
&INHERENT_IDENTIFIER
|
||||
}
|
||||
|
||||
fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error> {
|
||||
let timestamp = inherent_data.timestamp_inherent_data()?;
|
||||
let slot = *timestamp / self.slot_duration.as_millis() as u64;
|
||||
inherent_data.put_data(INHERENT_IDENTIFIER, &slot)
|
||||
}
|
||||
|
||||
fn error_to_string(&self, error: &[u8]) -> Option<String> {
|
||||
Error::decode(&mut &error[..]).map(|e| e.into_string()).ok()
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.slot
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[async_trait::async_trait]
|
||||
impl sp_inherents::InherentDataProvider for InherentDataProvider {
|
||||
fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error> {
|
||||
inherent_data.put_data(INHERENT_IDENTIFIER, &self.slot)
|
||||
}
|
||||
|
||||
async fn try_handle_error(
|
||||
&self,
|
||||
_: &InherentIdentifier,
|
||||
_: &[u8],
|
||||
) -> Option<Result<(), Error>> {
|
||||
// There is no error anymore
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
@@ -402,7 +402,6 @@ async fn import_many_blocks<B: BlockT, V: Verifier<B>, Transaction: Send + 'stat
|
||||
|
||||
/// A future that will always `yield` on the first call of `poll` but schedules the current task for
|
||||
/// re-execution.
|
||||
|
||||
///
|
||||
/// This is done by getting the waker and calling `wake_by_ref` followed by returning `Pending`.
|
||||
/// The next time the `poll` is called, it will return `Ready`.
|
||||
|
||||
@@ -15,18 +15,24 @@ targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
|
||||
[dependencies]
|
||||
parking_lot = { version = "0.11.1", optional = true }
|
||||
sp-std = { version = "3.0.0", default-features = false, path = "../std" }
|
||||
sp-core = { version = "3.0.0", default-features = false, path = "../core" }
|
||||
sp-runtime = { version = "3.0.0", path = "../runtime", optional = true }
|
||||
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
|
||||
thiserror = { version = "1.0.21", optional = true }
|
||||
impl-trait-for-tuples = "0.2.0"
|
||||
async-trait = { version = "0.1.30", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
futures = "0.3.9"
|
||||
|
||||
[features]
|
||||
default = [ "std" ]
|
||||
std = [
|
||||
"parking_lot",
|
||||
"sp-std/std",
|
||||
"codec/std",
|
||||
"sp-core/std",
|
||||
"thiserror",
|
||||
"sp-runtime",
|
||||
"async-trait",
|
||||
]
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2021 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use crate::{InherentData, Error, InherentIdentifier};
|
||||
use sp_runtime::traits::Block as BlockT;
|
||||
|
||||
/// Something that can create inherent data providers.
|
||||
///
|
||||
/// It is possible for the caller to provide custom arguments to the callee by setting the
|
||||
/// `ExtraArgs` generic parameter.
|
||||
///
|
||||
/// The crate already provides some convience implementations of this trait for
|
||||
/// `Box<dyn CreateInherentDataProviders>` and closures. So, it should not be required to implement
|
||||
/// this trait manually.
|
||||
#[async_trait::async_trait]
|
||||
pub trait CreateInherentDataProviders<Block: BlockT, ExtraArgs>: Send + Sync {
|
||||
/// The inherent data providers that will be created.
|
||||
type InherentDataProviders: InherentDataProvider;
|
||||
|
||||
/// Create the inherent data providers at the given `parent` block using the given `extra_args`.
|
||||
async fn create_inherent_data_providers(
|
||||
&self,
|
||||
parent: Block::Hash,
|
||||
extra_args: ExtraArgs,
|
||||
) -> Result<Self::InherentDataProviders, Box<dyn std::error::Error + Send + Sync>>;
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<F, Block, IDP, ExtraArgs, Fut> CreateInherentDataProviders<Block, ExtraArgs> for F
|
||||
where
|
||||
Block: BlockT,
|
||||
F: Fn(Block::Hash, ExtraArgs) -> Fut + Sync + Send,
|
||||
Fut: std::future::Future<Output = Result<IDP, Box<dyn std::error::Error + Send + Sync>>> + Send + 'static,
|
||||
IDP: InherentDataProvider + 'static,
|
||||
ExtraArgs: Send + 'static,
|
||||
{
|
||||
type InherentDataProviders = IDP;
|
||||
|
||||
async fn create_inherent_data_providers(
|
||||
&self,
|
||||
parent: Block::Hash,
|
||||
extra_args: ExtraArgs,
|
||||
) -> Result<Self::InherentDataProviders, Box<dyn std::error::Error + Send + Sync>> {
|
||||
(*self)(parent, extra_args).await
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<Block: BlockT, ExtraArgs: Send, IDPS: InherentDataProvider>
|
||||
CreateInherentDataProviders<Block, ExtraArgs>
|
||||
for Box<dyn CreateInherentDataProviders<Block, ExtraArgs, InherentDataProviders = IDPS>>
|
||||
{
|
||||
type InherentDataProviders = IDPS;
|
||||
|
||||
async fn create_inherent_data_providers(
|
||||
&self,
|
||||
parent: Block::Hash,
|
||||
extra_args: ExtraArgs,
|
||||
) -> Result<Self::InherentDataProviders, Box<dyn std::error::Error + Send + Sync>> {
|
||||
(**self).create_inherent_data_providers(parent, extra_args).await
|
||||
}
|
||||
}
|
||||
|
||||
/// Something that provides inherent data.
|
||||
#[async_trait::async_trait]
|
||||
pub trait InherentDataProvider: Send + Sync {
|
||||
/// Convenience function for creating [`InherentData`].
|
||||
///
|
||||
/// Basically maps around [`Self::provide_inherent_data`].
|
||||
fn create_inherent_data(&self) -> Result<InherentData, Error> {
|
||||
let mut inherent_data = InherentData::new();
|
||||
self.provide_inherent_data(&mut inherent_data)?;
|
||||
Ok(inherent_data)
|
||||
}
|
||||
|
||||
/// Provide inherent data that should be included in a block.
|
||||
///
|
||||
/// The data should be stored in the given `InherentData` structure.
|
||||
fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error>;
|
||||
|
||||
/// Convert the given encoded error to a string.
|
||||
///
|
||||
/// If the given error could not be decoded, `None` should be returned.
|
||||
async fn try_handle_error(
|
||||
&self,
|
||||
identifier: &InherentIdentifier,
|
||||
error: &[u8],
|
||||
) -> Option<Result<(), Error>>;
|
||||
}
|
||||
|
||||
#[impl_trait_for_tuples::impl_for_tuples(30)]
|
||||
#[async_trait::async_trait]
|
||||
impl InherentDataProvider for Tuple {
|
||||
for_tuples!( where #( Tuple: Send + Sync )* );
|
||||
fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error> {
|
||||
for_tuples!( #( Tuple.provide_inherent_data(inherent_data)?; )* );
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn try_handle_error(
|
||||
&self,
|
||||
identifier: &InherentIdentifier,
|
||||
error: &[u8],
|
||||
) -> Option<Result<(), Error>> {
|
||||
for_tuples!( #(
|
||||
if let Some(r) = Tuple.try_handle_error(identifier, error).await { return Some(r) }
|
||||
)* );
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -15,21 +15,149 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Provides types and traits for creating and checking inherents.
|
||||
//! Substrate inherent extrinsics
|
||||
//!
|
||||
//! Each inherent is added to a produced block. Each runtime decides on which inherents it
|
||||
//! wants to attach to its blocks. All data that is required for the runtime to create the inherents
|
||||
//! is stored in the `InherentData`. This `InherentData` is constructed by the node and given to
|
||||
//! the runtime.
|
||||
//! Inherent extrinsics are extrinsics that are inherently added to each block. However, it is up to
|
||||
//! runtime implementation to require an inherent for each block or to make it optional. Inherents
|
||||
//! are mainly used to pass data from the block producer to the runtime. So, inherents require some
|
||||
//! part that is running on the client side and some part that is running on the runtime side. Any
|
||||
//! data that is required by an inherent is passed as [`InherentData`] from the client to the runtime
|
||||
//! when the inherents are constructed.
|
||||
//!
|
||||
//! Types that provide data for inherents, should implement `InherentDataProvider` and need to be
|
||||
//! registered at `InherentDataProviders`.
|
||||
//! The process of constructing and applying inherents is the following:
|
||||
//!
|
||||
//! In the runtime, modules need to implement `ProvideInherent` when they can create and/or check
|
||||
//! inherents. By implementing `ProvideInherent`, a module is not enforced to create an inherent.
|
||||
//! A module can also just check given inherents. For using a module as inherent provider, it needs
|
||||
//! to be registered by the `construct_runtime!` macro. The macro documentation gives more
|
||||
//! information on how that is done.
|
||||
//! 1. The block producer first creates the [`InherentData`] by using the inherent data providers
|
||||
//! that are created by [`CreateInherentDataProviders`].
|
||||
//!
|
||||
//! 2. The [`InherentData`] is passed to the `inherent_extrinsics` function of the `BlockBuilder`
|
||||
//! runtime api. This will call the runtime which will create all the inherents that should be
|
||||
//! applied to the block.
|
||||
//!
|
||||
//! 3. Apply each inherent to the block like any normal extrinsic.
|
||||
//!
|
||||
//! On block import the inherents in the block are checked by calling the `check_inherents` runtime
|
||||
//! API. This will also pass an instance of [`InherentData`] which the runtime can use to validate
|
||||
//! all inherents. If some inherent data isn't required for validating an inherent, it can be
|
||||
//! omitted when providing the inherent data providers for block import.
|
||||
//!
|
||||
//! # Providing inherent data
|
||||
//!
|
||||
//! To provide inherent data from the client side, [`InherentDataProvider`] should be implemented.
|
||||
//!
|
||||
//! ```
|
||||
//! use codec::Decode;
|
||||
//! use sp_inherents::{InherentIdentifier, InherentData};
|
||||
//!
|
||||
//! // This needs to be unique for the runtime.
|
||||
//! const INHERENT_IDENTIFIER: InherentIdentifier = *b"testinh0";
|
||||
//!
|
||||
//! /// Some custom inherent data provider
|
||||
//! struct InherentDataProvider;
|
||||
//!
|
||||
//! #[async_trait::async_trait]
|
||||
//! impl sp_inherents::InherentDataProvider for InherentDataProvider {
|
||||
//! fn provide_inherent_data(
|
||||
//! &self,
|
||||
//! inherent_data: &mut InherentData,
|
||||
//! ) -> Result<(), sp_inherents::Error> {
|
||||
//! // We can insert any data that implements [`codec::Encode`].
|
||||
//! inherent_data.put_data(INHERENT_IDENTIFIER, &"hello")
|
||||
//! }
|
||||
//!
|
||||
//! /// When validating the inherents, the runtime implementation can throw errors. We support
|
||||
//! /// two error modes, fatal and non-fatal errors. A fatal error means that the block is invalid
|
||||
//! /// and this function here should return `Err(_)` to not import the block. Non-fatal errors
|
||||
//! /// are allowed to be handled here in this function and the function should return `Ok(())`
|
||||
//! /// if it could be handled. A non-fatal error is for example that a block is in the future
|
||||
//! /// from the point of view of the local node. In such a case the block import for example
|
||||
//! /// should be delayed until the block is valid.
|
||||
//! ///
|
||||
//! /// If this functions returns `None`, it means that it is not responsible for this error or
|
||||
//! /// that the error could not be interpreted.
|
||||
//! async fn try_handle_error(
|
||||
//! &self,
|
||||
//! identifier: &InherentIdentifier,
|
||||
//! mut error: &[u8],
|
||||
//! ) -> Option<Result<(), sp_inherents::Error>> {
|
||||
//! // Check if this error belongs to us.
|
||||
//! if *identifier != INHERENT_IDENTIFIER {
|
||||
//! return None;
|
||||
//! }
|
||||
//!
|
||||
//! // For demonstration purposes we are using a `String` as error type. In real
|
||||
//! // implementations it is advised to not use `String`.
|
||||
//! Some(Err(
|
||||
//! sp_inherents::Error::Application(Box::from(String::decode(&mut error).ok()?))
|
||||
//! ))
|
||||
//! }
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! In the service the relevant inherent data providers need to be passed the block production and
|
||||
//! the block import. As already highlighted above, the providers can be different between import
|
||||
//! and production.
|
||||
//!
|
||||
//! ```
|
||||
//! # use sp_runtime::testing::ExtrinsicWrapper;
|
||||
//! # use sp_inherents::{InherentIdentifier, InherentData};
|
||||
//! # use futures::FutureExt;
|
||||
//! # type Block = sp_runtime::testing::Block<ExtrinsicWrapper<()>>;
|
||||
//! # const INHERENT_IDENTIFIER: InherentIdentifier = *b"testinh0";
|
||||
//! # struct InherentDataProvider;
|
||||
//! # #[async_trait::async_trait]
|
||||
//! # impl sp_inherents::InherentDataProvider for InherentDataProvider {
|
||||
//! # fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), sp_inherents::Error> {
|
||||
//! # inherent_data.put_data(INHERENT_IDENTIFIER, &"hello")
|
||||
//! # }
|
||||
//! # async fn try_handle_error(
|
||||
//! # &self,
|
||||
//! # _: &InherentIdentifier,
|
||||
//! # _: &[u8],
|
||||
//! # ) -> Option<Result<(), sp_inherents::Error>> {
|
||||
//! # None
|
||||
//! # }
|
||||
//! # }
|
||||
//!
|
||||
//! async fn cool_consensus_block_production(
|
||||
//! // The second parameter to the trait are parameters that depend on what the caller
|
||||
//! // can provide on extra data.
|
||||
//! _: impl sp_inherents::CreateInherentDataProviders<Block, ()>,
|
||||
//! ) {
|
||||
//! // do cool stuff
|
||||
//! }
|
||||
//!
|
||||
//! async fn cool_consensus_block_import(
|
||||
//! _: impl sp_inherents::CreateInherentDataProviders<Block, ()>,
|
||||
//! ) {
|
||||
//! // do cool stuff
|
||||
//! }
|
||||
//!
|
||||
//! async fn build_service(is_validator: bool) {
|
||||
//! // For block import we don't pass any inherent data provider, because our runtime
|
||||
//! // does not need any inherent data to validate the inherents.
|
||||
//! let block_import = cool_consensus_block_import(|_parent, ()| async { Ok(()) });
|
||||
//!
|
||||
//! let block_production = if is_validator {
|
||||
//! // For block production we want to provide our inherent data provider
|
||||
//! cool_consensus_block_production(|_parent, ()| async {
|
||||
//! Ok(InherentDataProvider)
|
||||
//! }).boxed()
|
||||
//! } else {
|
||||
//! futures::future::pending().boxed()
|
||||
//! };
|
||||
//!
|
||||
//! futures::pin_mut!(block_import);
|
||||
//!
|
||||
//! futures::future::select(block_import, block_production).await;
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! # Creating the inherent
|
||||
//!
|
||||
//! As the inherents are created by the runtime, it depends on the runtime implementation on how
|
||||
//! to create the inherents. As already described above the client side passes the [`InherentData`]
|
||||
//! and expects the runtime to construct the inherents out of it. When validating the inherents,
|
||||
//! [`CheckInherentsResult`] is used to communicate the result client side.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
#![warn(missing_docs)]
|
||||
@@ -39,42 +167,34 @@ use codec::{Encode, Decode};
|
||||
use sp_std::{collections::btree_map::{BTreeMap, IntoIter, Entry}, vec::Vec};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use parking_lot::RwLock;
|
||||
mod client_side;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use std::{sync::Arc, format};
|
||||
pub use client_side::*;
|
||||
|
||||
/// An error that can occur within the inherent data system.
|
||||
#[cfg(feature = "std")]
|
||||
#[derive(Debug, Encode, Decode, thiserror::Error)]
|
||||
#[error("Inherents: {0}")]
|
||||
pub struct Error(String);
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<T: Into<String>> From<T> for Error {
|
||||
fn from(data: T) -> Error {
|
||||
Self(data.into())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl Error {
|
||||
/// Convert this error into a `String`.
|
||||
pub fn into_string(self) -> String {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
/// An error that can occur within the inherent data system.
|
||||
#[derive(Encode, sp_core::RuntimeDebug)]
|
||||
#[cfg(not(feature = "std"))]
|
||||
pub struct Error(&'static str);
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
impl From<&'static str> for Error {
|
||||
fn from(data: &'static str) -> Error {
|
||||
Self(data)
|
||||
}
|
||||
/// Errors that occur in context of inherents.
|
||||
#[derive(Debug)]
|
||||
#[cfg_attr(feature = "std", derive(thiserror::Error))]
|
||||
#[allow(missing_docs)]
|
||||
pub enum Error {
|
||||
#[cfg_attr(
|
||||
feature = "std",
|
||||
error("Inherent data already exists for identifier: {}", "String::from_utf8_lossy(_0)")
|
||||
)]
|
||||
InherentDataExists(InherentIdentifier),
|
||||
#[cfg_attr(
|
||||
feature = "std",
|
||||
error("Failed to decode inherent data for identifier: {}", "String::from_utf8_lossy(_1)")
|
||||
)]
|
||||
DecodingFailed(#[cfg_attr(feature = "std", source)] codec::Error, InherentIdentifier),
|
||||
#[cfg_attr(
|
||||
feature = "std",
|
||||
error("There was already a fatal error reported and no other errors are allowed")
|
||||
)]
|
||||
FatalErrorReported,
|
||||
#[cfg(feature = "std")]
|
||||
#[error(transparent)]
|
||||
Application(#[from] Box<dyn std::error::Error + Send + Sync>),
|
||||
}
|
||||
|
||||
/// An identifier for an inherent.
|
||||
@@ -112,7 +232,7 @@ impl InherentData {
|
||||
Ok(())
|
||||
},
|
||||
Entry::Occupied(_) => {
|
||||
Err("Inherent with same identifier already exists!".into())
|
||||
Err(Error::InherentDataExists(identifier))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -142,9 +262,7 @@ impl InherentData {
|
||||
match self.data.get(identifier) {
|
||||
Some(inherent) =>
|
||||
I::decode(&mut &inherent[..])
|
||||
.map_err(|_| {
|
||||
"Could not decode requested inherent type!".into()
|
||||
})
|
||||
.map_err(|e| Error::DecodingFailed(e, *identifier))
|
||||
.map(Some),
|
||||
None => Ok(None)
|
||||
}
|
||||
@@ -203,7 +321,7 @@ impl CheckInherentsResult {
|
||||
) -> Result<(), Error> {
|
||||
// Don't accept any other error
|
||||
if self.fatal_error {
|
||||
return Err("No other errors are accepted after an hard error!".into())
|
||||
return Err(Error::FatalErrorReported)
|
||||
}
|
||||
|
||||
if error.is_fatal_error() {
|
||||
@@ -257,118 +375,6 @@ impl PartialEq for CheckInherentsResult {
|
||||
}
|
||||
}
|
||||
|
||||
/// All `InherentData` providers.
|
||||
#[cfg(feature = "std")]
|
||||
#[derive(Clone, Default)]
|
||||
pub struct InherentDataProviders {
|
||||
providers: Arc<RwLock<Vec<Box<dyn ProvideInherentData + Send + Sync>>>>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl InherentDataProviders {
|
||||
/// Create a new instance.
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// Register an `InherentData` provider.
|
||||
///
|
||||
/// The registration order is preserved and this order will also be used when creating the
|
||||
/// inherent data.
|
||||
///
|
||||
/// # Result
|
||||
///
|
||||
/// Will return an error, if a provider with the same identifier already exists.
|
||||
pub fn register_provider<P: ProvideInherentData + Send + Sync +'static>(
|
||||
&self,
|
||||
provider: P,
|
||||
) -> Result<(), Error> {
|
||||
if self.has_provider(&provider.inherent_identifier()) {
|
||||
Err(
|
||||
format!(
|
||||
"Inherent data provider with identifier {:?} already exists!",
|
||||
&provider.inherent_identifier()
|
||||
).into()
|
||||
)
|
||||
} else {
|
||||
provider.on_register(self)?;
|
||||
self.providers.write().push(Box::new(provider));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns if a provider for the given identifier exists.
|
||||
pub fn has_provider(&self, identifier: &InherentIdentifier) -> bool {
|
||||
self.providers.read().iter().any(|p| p.inherent_identifier() == identifier)
|
||||
}
|
||||
|
||||
/// Create inherent data.
|
||||
pub fn create_inherent_data(&self) -> Result<InherentData, Error> {
|
||||
let mut data = InherentData::new();
|
||||
self.providers.read().iter().try_for_each(|p| {
|
||||
p.provide_inherent_data(&mut data)
|
||||
.map_err(|e| format!("Error for `{:?}`: {:?}", p.inherent_identifier(), e))
|
||||
})?;
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
/// Converts a given encoded error into a `String`.
|
||||
///
|
||||
/// Useful if the implementation encounters an error for an identifier it does not know.
|
||||
pub fn error_to_string(&self, identifier: &InherentIdentifier, error: &[u8]) -> String {
|
||||
let res = self.providers.read().iter().filter_map(|p|
|
||||
if p.inherent_identifier() == identifier {
|
||||
Some(
|
||||
p.error_to_string(error)
|
||||
.unwrap_or_else(|| error_to_string_fallback(identifier))
|
||||
)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
).next();
|
||||
|
||||
match res {
|
||||
Some(res) => res,
|
||||
None => format!(
|
||||
"Error while checking inherent of type \"{}\", but this inherent type is unknown.",
|
||||
String::from_utf8_lossy(identifier)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Something that provides inherent data.
|
||||
#[cfg(feature = "std")]
|
||||
pub trait ProvideInherentData {
|
||||
/// Is called when this inherent data provider is registered at the given
|
||||
/// `InherentDataProviders`.
|
||||
fn on_register(&self, _: &InherentDataProviders) -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The identifier of the inherent for that data will be provided.
|
||||
fn inherent_identifier(&self) -> &'static InherentIdentifier;
|
||||
|
||||
/// Provide inherent data that should be included in a block.
|
||||
///
|
||||
/// The data should be stored in the given `InherentData` structure.
|
||||
fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error>;
|
||||
|
||||
/// Convert the given encoded error to a string.
|
||||
///
|
||||
/// If the given error could not be decoded, `None` should be returned.
|
||||
fn error_to_string(&self, error: &[u8]) -> Option<String>;
|
||||
}
|
||||
|
||||
/// A fallback function, if the decoding of an error fails.
|
||||
#[cfg(feature = "std")]
|
||||
fn error_to_string_fallback(identifier: &InherentIdentifier) -> String {
|
||||
format!(
|
||||
"Error while checking inherent of type \"{}\", but error could not be decoded.",
|
||||
String::from_utf8_lossy(identifier)
|
||||
)
|
||||
}
|
||||
|
||||
/// Did we encounter a fatal error while checking an inherent?
|
||||
///
|
||||
/// A fatal error is everything that fails while checking an inherent error, e.g. the inherent
|
||||
@@ -382,9 +388,9 @@ pub trait IsFatalError {
|
||||
fn is_fatal_error(&self) -> bool;
|
||||
}
|
||||
|
||||
/// Auxiliary to make any given error resolve to `is_fatal_error() == true`.
|
||||
#[derive(Encode)]
|
||||
pub struct MakeFatalError<E: codec::Encode>(E);
|
||||
/// Auxiliary to make any given error resolve to `is_fatal_error() == true` for [`IsFatalError`].
|
||||
#[derive(codec::Encode)]
|
||||
pub struct MakeFatalError<E>(E);
|
||||
|
||||
impl<E: codec::Encode> From<E> for MakeFatalError<E> {
|
||||
fn from(err: E) -> Self {
|
||||
@@ -398,63 +404,6 @@ impl<E: codec::Encode> IsFatalError for MakeFatalError<E> {
|
||||
}
|
||||
}
|
||||
|
||||
/// A pallet that provides or verifies an inherent extrinsic.
|
||||
///
|
||||
/// The pallet may provide the inherent, verify an inherent, or both provide and verify.
|
||||
pub trait ProvideInherent {
|
||||
/// The call type of the pallet.
|
||||
type Call;
|
||||
/// The error returned by `check_inherent`.
|
||||
type Error: codec::Encode + IsFatalError;
|
||||
/// The inherent identifier used by this inherent.
|
||||
const INHERENT_IDENTIFIER: self::InherentIdentifier;
|
||||
|
||||
/// Create an inherent out of the given `InherentData`.
|
||||
fn create_inherent(data: &InherentData) -> Option<Self::Call>;
|
||||
|
||||
/// Determines whether this inherent is required in this block.
|
||||
///
|
||||
/// - `Ok(None)` indicates that this inherent is not required in this block. The default
|
||||
/// implementation returns this.
|
||||
///
|
||||
/// - `Ok(Some(e))` indicates that this inherent is required in this block. The
|
||||
/// `impl_outer_inherent!`, will call this function from its `check_extrinsics`.
|
||||
/// If the inherent is not present, it will return `e`.
|
||||
///
|
||||
/// - `Err(_)` indicates that this function failed and further operations should be aborted.
|
||||
///
|
||||
/// NOTE: If inherent is required then the runtime asserts that the block contains at least
|
||||
/// one inherent for which:
|
||||
/// * type is [`Self::Call`],
|
||||
/// * [`Self::is_inherent`] returns true.
|
||||
fn is_inherent_required(_: &InherentData) -> Result<Option<Self::Error>, Self::Error> { Ok(None) }
|
||||
|
||||
/// Check whether the given inherent is valid. Checking the inherent is optional and can be
|
||||
/// omitted by using the default implementation.
|
||||
///
|
||||
/// When checking an inherent, the first parameter represents the inherent that is actually
|
||||
/// included in the block by its author. Whereas the second parameter represents the inherent
|
||||
/// data that the verifying node calculates.
|
||||
///
|
||||
/// NOTE: A block can contains multiple inherent.
|
||||
fn check_inherent(_: &Self::Call, _: &InherentData) -> Result<(), Self::Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Return whether the call is an inherent call.
|
||||
///
|
||||
/// NOTE: Signed extrinsics are not inherent, but signed extrinsic with the given call variant
|
||||
/// can be dispatched.
|
||||
///
|
||||
/// # Warning
|
||||
///
|
||||
/// In FRAME, inherent are enforced to be before other extrinsics, for this reason,
|
||||
/// pallets with unsigned transactions **must ensure** that no unsigned transaction call
|
||||
/// is an inherent call, when implementing `ValidateUnsigned::validate_unsigned`.
|
||||
/// Otherwise block producer can produce invalid blocks by including them after non inherent.
|
||||
fn is_inherent(call: &Self::Call) -> bool;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -496,93 +445,34 @@ mod tests {
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct TestInherentDataProvider {
|
||||
registered: Arc<RwLock<bool>>,
|
||||
}
|
||||
|
||||
impl TestInherentDataProvider {
|
||||
fn new() -> Self {
|
||||
let inst = Self {
|
||||
registered: Default::default(),
|
||||
};
|
||||
|
||||
// just make sure
|
||||
assert!(!inst.is_registered());
|
||||
|
||||
inst
|
||||
}
|
||||
|
||||
fn is_registered(&self) -> bool {
|
||||
*self.registered.read()
|
||||
}
|
||||
}
|
||||
struct TestInherentDataProvider;
|
||||
|
||||
const ERROR_TO_STRING: &str = "Found error!";
|
||||
|
||||
impl ProvideInherentData for TestInherentDataProvider {
|
||||
fn on_register(&self, _: &InherentDataProviders) -> Result<(), Error> {
|
||||
*self.registered.write() = true;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn inherent_identifier(&self) -> &'static InherentIdentifier {
|
||||
&TEST_INHERENT_0
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl InherentDataProvider for TestInherentDataProvider {
|
||||
fn provide_inherent_data(&self, data: &mut InherentData) -> Result<(), Error> {
|
||||
data.put_data(TEST_INHERENT_0, &42)
|
||||
}
|
||||
|
||||
fn error_to_string(&self, _: &[u8]) -> Option<String> {
|
||||
Some(ERROR_TO_STRING.into())
|
||||
async fn try_handle_error(
|
||||
&self,
|
||||
_: &InherentIdentifier,
|
||||
_: &[u8],
|
||||
) -> Option<Result<(), Error>> {
|
||||
Some(Err(Error::Application(Box::from(ERROR_TO_STRING))))
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn registering_inherent_provider() {
|
||||
let provider = TestInherentDataProvider::new();
|
||||
let providers = InherentDataProviders::new();
|
||||
fn create_inherent_data() {
|
||||
let provider = TestInherentDataProvider;
|
||||
|
||||
providers.register_provider(provider.clone()).unwrap();
|
||||
assert!(provider.is_registered());
|
||||
assert!(providers.has_provider(provider.inherent_identifier()));
|
||||
|
||||
// Second time should fail
|
||||
assert!(providers.register_provider(provider.clone()).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_inherent_data_from_all_providers() {
|
||||
let provider = TestInherentDataProvider::new();
|
||||
let providers = InherentDataProviders::new();
|
||||
|
||||
providers.register_provider(provider.clone()).unwrap();
|
||||
assert!(provider.is_registered());
|
||||
|
||||
let inherent_data = providers.create_inherent_data().unwrap();
|
||||
let inherent_data = provider.create_inherent_data().unwrap();
|
||||
|
||||
assert_eq!(
|
||||
inherent_data.get_data::<u32>(provider.inherent_identifier()).unwrap().unwrap(),
|
||||
42u32
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encoded_error_to_string() {
|
||||
let provider = TestInherentDataProvider::new();
|
||||
let providers = InherentDataProviders::new();
|
||||
|
||||
providers.register_provider(provider.clone()).unwrap();
|
||||
assert!(provider.is_registered());
|
||||
|
||||
assert_eq!(
|
||||
&providers.error_to_string(&TEST_INHERENT_0, &[1, 2]), ERROR_TO_STRING
|
||||
);
|
||||
|
||||
assert!(
|
||||
providers
|
||||
.error_to_string(&TEST_INHERENT_1, &[1, 2])
|
||||
.contains("inherent type is unknown")
|
||||
inherent_data.get_data::<u32>(&TEST_INHERENT_0).unwrap().unwrap(),
|
||||
42u32,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,10 @@ sp-runtime = { version = "3.0.0", default-features = false, path = "../runtime"
|
||||
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
|
||||
sp-inherents = { version = "3.0.0", default-features = false, path = "../inherents" }
|
||||
wasm-timer = { version = "0.2", optional = true }
|
||||
thiserror = { version = "1.0.21", optional = true }
|
||||
log = { version = "0.4.8", optional = true }
|
||||
futures-timer = { version = "3.0.2", optional = true }
|
||||
async-trait = { version = "0.1.48", optional = true }
|
||||
|
||||
[features]
|
||||
default = [ "std" ]
|
||||
@@ -29,4 +33,8 @@ std = [
|
||||
"codec/std",
|
||||
"sp-inherents/std",
|
||||
"wasm-timer",
|
||||
"thiserror",
|
||||
"log",
|
||||
"futures-timer",
|
||||
"async-trait",
|
||||
]
|
||||
|
||||
@@ -20,13 +20,9 @@
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
use codec::{Encode, Decode};
|
||||
#[cfg(feature = "std")]
|
||||
use sp_inherents::ProvideInherentData;
|
||||
use sp_inherents::{InherentIdentifier, IsFatalError, InherentData};
|
||||
use sp_std::time::Duration;
|
||||
|
||||
use sp_runtime::RuntimeString;
|
||||
|
||||
/// The identifier for the `timestamp` inherent.
|
||||
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"timstap0";
|
||||
|
||||
@@ -46,9 +42,14 @@ impl Timestamp {
|
||||
}
|
||||
|
||||
/// Returns `self` as [`Duration`].
|
||||
pub fn as_duration(&self) -> Duration {
|
||||
pub fn as_duration(self) -> Duration {
|
||||
Duration::from_millis(self.0)
|
||||
}
|
||||
|
||||
/// Checked subtraction that returns `None` on an underflow.
|
||||
pub fn checked_sub(self, other: Self) -> Option<Self> {
|
||||
self.0.checked_sub(other.0).map(Self)
|
||||
}
|
||||
}
|
||||
|
||||
impl sp_std::ops::Deref for Timestamp {
|
||||
@@ -114,20 +115,22 @@ impl From<Duration> for Timestamp {
|
||||
|
||||
/// Errors that can occur while checking the timestamp inherent.
|
||||
#[derive(Encode, sp_runtime::RuntimeDebug)]
|
||||
#[cfg_attr(feature = "std", derive(Decode))]
|
||||
#[cfg_attr(feature = "std", derive(Decode, thiserror::Error))]
|
||||
pub enum InherentError {
|
||||
/// The timestamp is valid in the future.
|
||||
/// This is a non-fatal-error and will not stop checking the inherents.
|
||||
#[cfg_attr(feature = "std", error("Block will be valid at {0}."))]
|
||||
ValidAtTimestamp(InherentType),
|
||||
/// Some other error.
|
||||
Other(RuntimeString),
|
||||
/// The block timestamp is too far in the future
|
||||
#[cfg_attr(feature = "std", error("The timestamp of the block is too far in the future."))]
|
||||
TooFarInFuture,
|
||||
}
|
||||
|
||||
impl IsFatalError for InherentError {
|
||||
fn is_fatal_error(&self) -> bool {
|
||||
match self {
|
||||
InherentError::ValidAtTimestamp(_) => false,
|
||||
InherentError::Other(_) => true,
|
||||
InherentError::TooFarInFuture => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -147,43 +150,123 @@ impl InherentError {
|
||||
/// Auxiliary trait to extract timestamp inherent data.
|
||||
pub trait TimestampInherentData {
|
||||
/// Get timestamp inherent data.
|
||||
fn timestamp_inherent_data(&self) -> Result<InherentType, sp_inherents::Error>;
|
||||
fn timestamp_inherent_data(&self) -> Result<Option<InherentType>, sp_inherents::Error>;
|
||||
}
|
||||
|
||||
impl TimestampInherentData for InherentData {
|
||||
fn timestamp_inherent_data(&self) -> Result<InherentType, sp_inherents::Error> {
|
||||
fn timestamp_inherent_data(&self) -> Result<Option<InherentType>, sp_inherents::Error> {
|
||||
self.get_data(&INHERENT_IDENTIFIER)
|
||||
.and_then(|r| r.ok_or_else(|| "Timestamp inherent data not found".into()))
|
||||
}
|
||||
}
|
||||
|
||||
/// The current timestamp using the system time.
|
||||
///
|
||||
/// This timestamp is the time since the UNIX epoch.
|
||||
#[cfg(feature = "std")]
|
||||
fn current_timestamp() -> std::time::Duration {
|
||||
use wasm_timer::SystemTime;
|
||||
|
||||
let now = SystemTime::now();
|
||||
now.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.expect("Current time is always after unix epoch; qed")
|
||||
}
|
||||
|
||||
/// Provide duration since unix epoch in millisecond for timestamp inherent.
|
||||
#[cfg(feature = "std")]
|
||||
pub struct InherentDataProvider;
|
||||
pub struct InherentDataProvider {
|
||||
max_drift: InherentType,
|
||||
timestamp: InherentType,
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl ProvideInherentData for InherentDataProvider {
|
||||
fn inherent_identifier(&self) -> &'static InherentIdentifier {
|
||||
&INHERENT_IDENTIFIER
|
||||
impl InherentDataProvider {
|
||||
/// Create `Self` while using the system time to get the timestamp.
|
||||
pub fn from_system_time() -> Self {
|
||||
Self {
|
||||
max_drift: std::time::Duration::from_secs(60).into(),
|
||||
timestamp: current_timestamp().into(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create `Self` using the given `timestamp`.
|
||||
pub fn new(timestamp: InherentType) -> Self {
|
||||
Self {
|
||||
max_drift: std::time::Duration::from_secs(60).into(),
|
||||
timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
/// With the given maximum drift.
|
||||
///
|
||||
/// By default the maximum drift is 60 seconds.
|
||||
///
|
||||
/// The maximum drift is used when checking the inherents of a runtime. If the current timestamp
|
||||
/// plus the maximum drift is smaller than the timestamp in the block, the block will be rejected
|
||||
/// as being too far in the future.
|
||||
pub fn with_max_drift(mut self, max_drift: std::time::Duration) -> Self {
|
||||
self.max_drift = max_drift.into();
|
||||
self
|
||||
}
|
||||
|
||||
/// Returns the timestamp of this inherent data provider.
|
||||
pub fn timestamp(&self) -> InherentType {
|
||||
self.timestamp
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl sp_std::ops::Deref for InherentDataProvider {
|
||||
type Target = InherentType;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.timestamp
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[async_trait::async_trait]
|
||||
impl sp_inherents::InherentDataProvider for InherentDataProvider {
|
||||
fn provide_inherent_data(
|
||||
&self,
|
||||
inherent_data: &mut InherentData,
|
||||
) -> Result<(), sp_inherents::Error> {
|
||||
use wasm_timer::SystemTime;
|
||||
|
||||
let now = SystemTime::now();
|
||||
now.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.map_err(|_| {
|
||||
"Current time is before unix epoch".into()
|
||||
}).and_then(|d| {
|
||||
inherent_data.put_data(INHERENT_IDENTIFIER, &InherentType::from(d))
|
||||
})
|
||||
inherent_data.put_data(INHERENT_IDENTIFIER, &InherentType::from(self.timestamp))
|
||||
}
|
||||
|
||||
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,
|
||||
identifier: &InherentIdentifier,
|
||||
error: &[u8],
|
||||
) -> Option<Result<(), sp_inherents::Error>> {
|
||||
if *identifier != INHERENT_IDENTIFIER {
|
||||
return None
|
||||
}
|
||||
|
||||
match InherentError::try_from(&INHERENT_IDENTIFIER, error)? {
|
||||
InherentError::ValidAtTimestamp(valid) => {
|
||||
let max_drift = self.max_drift;
|
||||
let timestamp = self.timestamp;
|
||||
// halt import until timestamp is valid.
|
||||
// reject when too far ahead.
|
||||
if valid > timestamp + max_drift {
|
||||
return Some(Err(
|
||||
sp_inherents::Error::Application(Box::from(InherentError::TooFarInFuture))
|
||||
))
|
||||
}
|
||||
|
||||
let diff = valid.checked_sub(timestamp).unwrap_or_default();
|
||||
log::info!(
|
||||
target: "timestamp",
|
||||
"halting for block {} milliseconds in the future",
|
||||
diff.0,
|
||||
);
|
||||
|
||||
futures_timer::Delay::new(diff.as_duration()).await;
|
||||
|
||||
Some(Ok(()))
|
||||
},
|
||||
o => Some(Err(sp_inherents::Error::Application(Box::from(o)))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user