mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-15 19:41:05 +00:00
Move inherents to primitives (#4126)
* Split Aura and Timestamp inherents out of paint * fixup node depedencies * move babe inherents to primitives * move authorship inherents into primitives * Move finalty tracker inherents into primitives * fix aura primitives import
This commit is contained in:
committed by
GitHub
parent
5979a8c3e5
commit
303843f483
@@ -0,0 +1,19 @@
|
||||
[package]
|
||||
name = "sp-authorship"
|
||||
version = "2.0.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
description = "Authorship primitives"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
sp-inherents = { package = "substrate-inherents", path = "../inherents", default-features = false }
|
||||
rstd = { package = "sr-std", path = "../sr-std", default-features = false }
|
||||
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] }
|
||||
|
||||
[features]
|
||||
default = [ "std" ]
|
||||
std = [
|
||||
"codec/std",
|
||||
"rstd/std",
|
||||
"sp-inherents/std",
|
||||
]
|
||||
@@ -0,0 +1,75 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Authorship Primitives
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
use rstd::{result::Result, prelude::*};
|
||||
|
||||
use codec::{Encode, Decode};
|
||||
use sp_inherents::{Error, InherentIdentifier, InherentData};
|
||||
|
||||
/// The identifier for the `uncles` inherent.
|
||||
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"uncles00";
|
||||
|
||||
/// Auxiliary trait to extract uncles inherent data.
|
||||
pub trait UnclesInherentData<H: Decode> {
|
||||
/// Get uncles.
|
||||
fn uncles(&self) -> Result<Vec<H>, Error>;
|
||||
}
|
||||
|
||||
impl<H: Decode> UnclesInherentData<H> for InherentData {
|
||||
fn uncles(&self) -> Result<Vec<H>, Error> {
|
||||
Ok(self.get_data(&INHERENT_IDENTIFIER)?.unwrap_or_default())
|
||||
}
|
||||
}
|
||||
|
||||
/// Provider for inherent data.
|
||||
#[cfg(feature = "std")]
|
||||
pub struct InherentDataProvider<F, H> {
|
||||
inner: F,
|
||||
_marker: std::marker::PhantomData<H>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<F, H> InherentDataProvider<F, H> {
|
||||
pub fn new(uncles_oracle: F) -> Self {
|
||||
InherentDataProvider { inner: uncles_oracle, _marker: Default::default() }
|
||||
}
|
||||
}
|
||||
|
||||
#[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
|
||||
}
|
||||
|
||||
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(())
|
||||
}
|
||||
}
|
||||
|
||||
fn error_to_string(&self, _error: &[u8]) -> Option<String> {
|
||||
Some(format!("no further information"))
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,8 @@ codec = { package = "parity-scale-codec", version = "1.0.0", default-features =
|
||||
rstd = { package = "sr-std", path = "../../sr-std", default-features = false }
|
||||
sr-api = { path = "../../sr-api", default-features = false }
|
||||
sr-primitives = { path = "../../sr-primitives", default-features = false }
|
||||
inherents = { package = "substrate-inherents", path = "../../inherents", default-features = false }
|
||||
sp-timestamp = { path = "../../timestamp", default-features = false }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
@@ -20,4 +22,6 @@ std = [
|
||||
"rstd/std",
|
||||
"sr-api/std",
|
||||
"sr-primitives/std",
|
||||
"inherents/std",
|
||||
"sp-timestamp/std",
|
||||
]
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/// Contains the inherents for the AURA module
|
||||
|
||||
use sp_timestamp::TimestampInherentData;
|
||||
use inherents::{InherentIdentifier, InherentData, Error};
|
||||
use rstd::result::Result;
|
||||
use codec::Decode;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use inherents::{InherentDataProviders, ProvideInherentData};
|
||||
|
||||
/// The Aura inherent identifier.
|
||||
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"auraslot";
|
||||
|
||||
/// The type of the Aura inherent.
|
||||
pub type InherentType = u64;
|
||||
|
||||
/// Auxiliary trait to extract Aura inherent data.
|
||||
pub trait AuraInherentData {
|
||||
/// Get aura inherent data.
|
||||
fn aura_inherent_data(&self) ->Result<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> {
|
||||
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) {
|
||||
self.replace_data(INHERENT_IDENTIFIER, &new);
|
||||
}
|
||||
}
|
||||
|
||||
/// Provides the slot duration inherent data for `Aura`.
|
||||
#[cfg(feature = "std")]
|
||||
pub struct InherentDataProvider {
|
||||
slot_duration: u64,
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl InherentDataProvider {
|
||||
pub fn new(slot_duration: u64) -> Self {
|
||||
Self {
|
||||
slot_duration
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[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(())
|
||||
}
|
||||
}
|
||||
|
||||
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_num = timestamp / self.slot_duration;
|
||||
inherent_data.put_data(INHERENT_IDENTIFIER, &slot_num)
|
||||
}
|
||||
|
||||
fn error_to_string(&self, error: &[u8]) -> Option<String> {
|
||||
inherents::Error::decode(&mut &error[..]).map(|e| e.into_string()).ok()
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,8 @@ use codec::{Encode, Decode, Codec};
|
||||
use rstd::vec::Vec;
|
||||
use sr_primitives::ConsensusEngineId;
|
||||
|
||||
pub mod inherents;
|
||||
|
||||
pub mod sr25519 {
|
||||
mod app_sr25519 {
|
||||
use app_crypto::{app_crypto, key_types::AURA, sr25519};
|
||||
|
||||
@@ -13,6 +13,8 @@ schnorrkel = { version = "0.8.5", features = ["preaudit_deprecated"], optional =
|
||||
slots = { package = "substrate-consensus-slots", path = "../../../client/consensus/slots", optional = true }
|
||||
sr-api = { path = "../../sr-api", default-features = false }
|
||||
sr-primitives = { path = "../../sr-primitives", default-features = false }
|
||||
sp-inherents = { package = "substrate-inherents", path = "../../inherents", default-features = false }
|
||||
sp-timestamp = { path = "../../timestamp", default-features = false }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
@@ -24,4 +26,6 @@ std = [
|
||||
"slots",
|
||||
"sr-api/std",
|
||||
"sr-primitives/std",
|
||||
"sp-inherents/std",
|
||||
"sp-timestamp/std",
|
||||
]
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
//! Inherents for BABE
|
||||
|
||||
use sp_inherents::{Error, InherentData, InherentIdentifier};
|
||||
#[cfg(feature = "std")]
|
||||
use sp_inherents::{InherentDataProviders, ProvideInherentData};
|
||||
#[cfg(feature = "std")]
|
||||
use sp_timestamp::TimestampInherentData;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use codec::Decode;
|
||||
use rstd::result::Result;
|
||||
|
||||
/// The BABE inherent identifier.
|
||||
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"babeslot";
|
||||
|
||||
/// The type of the BABE inherent.
|
||||
pub type InherentType = u64;
|
||||
/// Auxiliary trait to extract BABE inherent data.
|
||||
pub trait BabeInherentData {
|
||||
/// Get BABE inherent data.
|
||||
fn babe_inherent_data(&self) -> Result<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> {
|
||||
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) {
|
||||
self.replace_data(INHERENT_IDENTIFIER, &new);
|
||||
}
|
||||
}
|
||||
|
||||
/// Provides the slot duration inherent data for BABE.
|
||||
#[cfg(feature = "std")]
|
||||
pub struct InherentDataProvider {
|
||||
slot_duration: u64,
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl InherentDataProvider {
|
||||
/// Constructs `Self`
|
||||
pub fn new(slot_duration: u64) -> Self {
|
||||
Self { slot_duration }
|
||||
}
|
||||
}
|
||||
|
||||
#[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(())
|
||||
}
|
||||
}
|
||||
|
||||
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_number = timestamp / self.slot_duration;
|
||||
inherent_data.put_data(INHERENT_IDENTIFIER, &slot_number)
|
||||
}
|
||||
|
||||
fn error_to_string(&self, error: &[u8]) -> Option<String> {
|
||||
Error::decode(&mut &error[..]).map(|e| e.into_string()).ok()
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
mod digest;
|
||||
pub mod inherents;
|
||||
|
||||
use codec::{Encode, Decode};
|
||||
use rstd::vec::Vec;
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
[package]
|
||||
name = "sp-finality-tracker"
|
||||
version = "2.0.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false }
|
||||
inherents = { package = "substrate-inherents", path = "../../primitives/inherents", default-features = false }
|
||||
rstd = { package = "sr-std", path = "../../primitives/sr-std", default-features = false }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = [
|
||||
"codec/std",
|
||||
"rstd/std",
|
||||
"inherents/std",
|
||||
]
|
||||
@@ -0,0 +1,76 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! SRML module that tracks the last finalized block, as perceived by block authors.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
use inherents::{InherentIdentifier, InherentData, Error};
|
||||
use codec::Decode;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use codec::Encode;
|
||||
|
||||
/// The identifier for the `finalnum` inherent.
|
||||
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"finalnum";
|
||||
|
||||
/// Auxiliary trait to extract finalized inherent data.
|
||||
pub trait FinalizedInherentData<N: Decode> {
|
||||
/// Get finalized inherent data.
|
||||
fn finalized_number(&self) -> Result<N, Error>;
|
||||
}
|
||||
|
||||
impl<N: Decode> FinalizedInherentData<N> for InherentData {
|
||||
fn finalized_number(&self) -> Result<N, Error> {
|
||||
self.get_data(&INHERENT_IDENTIFIER)
|
||||
.and_then(|r| r.ok_or_else(|| "Finalized number inherent data not found".into()))
|
||||
}
|
||||
}
|
||||
|
||||
/// Provider for inherent data.
|
||||
#[cfg(feature = "std")]
|
||||
pub struct InherentDataProvider<F, N> {
|
||||
inner: F,
|
||||
_marker: std::marker::PhantomData<N>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<F, N> InherentDataProvider<F, N> {
|
||||
pub fn new(final_oracle: F) -> Self {
|
||||
InherentDataProvider { inner: final_oracle, _marker: Default::default() }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<F, N: Encode> inherents::ProvideInherentData for InherentDataProvider<F, N>
|
||||
where F: Fn() -> Result<N, Error>
|
||||
{
|
||||
fn inherent_identifier(&self) -> &'static InherentIdentifier {
|
||||
&INHERENT_IDENTIFIER
|
||||
}
|
||||
|
||||
fn provide_inherent_data(
|
||||
&self,
|
||||
inherent_data: &mut InherentData,
|
||||
) -> Result<(), Error> {
|
||||
(self.inner)()
|
||||
.and_then(|n| inherent_data.put_data(INHERENT_IDENTIFIER, &n))
|
||||
}
|
||||
|
||||
fn error_to_string(&self, _error: &[u8]) -> Option<String> {
|
||||
Some(format!("no further information"))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
[package]
|
||||
name = "sp-timestamp"
|
||||
version = "2.0.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
sr-api = { path = "../sr-api", default-features = false }
|
||||
rstd = { package = "sr-std", path = "../sr-std", default-features = false }
|
||||
sr-primitives = { path = "../sr-primitives", default-features = false }
|
||||
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] }
|
||||
inherents = { package = "substrate-inherents", path = "../inherents", default-features = false }
|
||||
impl-trait-for-tuples = "0.1.3"
|
||||
|
||||
[features]
|
||||
default = [ "std" ]
|
||||
std = [
|
||||
"sr-api/std",
|
||||
"rstd/std",
|
||||
"sr-primitives/std",
|
||||
"codec/std",
|
||||
"inherents/std",
|
||||
]
|
||||
@@ -0,0 +1,115 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Substrate core types and inherents for timestamps.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
use codec::Encode;
|
||||
#[cfg(feature = "std")]
|
||||
use codec::Decode;
|
||||
#[cfg(feature = "std")]
|
||||
use inherents::ProvideInherentData;
|
||||
use inherents::{InherentIdentifier, ProvideInherent, IsFatalError, InherentData};
|
||||
|
||||
use sr_primitives::RuntimeString;
|
||||
|
||||
/// The identifier for the `timestamp` inherent.
|
||||
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"timstap0";
|
||||
/// The type of the inherent.
|
||||
pub type InherentType = u64;
|
||||
|
||||
/// Errors that can occur while checking the timestamp inherent.
|
||||
#[derive(Encode, sr_primitives::RuntimeDebug)]
|
||||
#[cfg_attr(feature = "std", derive(Decode))]
|
||||
pub enum InherentError {
|
||||
/// The timestamp is valid in the future.
|
||||
/// This is a non-fatal-error and will not stop checking the inherents.
|
||||
ValidAtTimestamp(InherentType),
|
||||
/// Some other error.
|
||||
Other(RuntimeString),
|
||||
}
|
||||
|
||||
impl IsFatalError for InherentError {
|
||||
fn is_fatal_error(&self) -> bool {
|
||||
match self {
|
||||
InherentError::ValidAtTimestamp(_) => false,
|
||||
InherentError::Other(_) => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl InherentError {
|
||||
/// Try to create an instance ouf of the given identifier and data.
|
||||
#[cfg(feature = "std")]
|
||||
pub fn try_from(id: &InherentIdentifier, data: &[u8]) -> Option<Self> {
|
||||
if id == &INHERENT_IDENTIFIER {
|
||||
<InherentError as codec::Decode>::decode(&mut &data[..]).ok()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Auxiliary trait to extract timestamp inherent data.
|
||||
pub trait TimestampInherentData {
|
||||
/// Get timestamp inherent data.
|
||||
fn timestamp_inherent_data(&self) -> Result<InherentType, inherents::Error>;
|
||||
}
|
||||
|
||||
impl TimestampInherentData for InherentData {
|
||||
fn timestamp_inherent_data(&self) -> Result<InherentType, inherents::Error> {
|
||||
self.get_data(&INHERENT_IDENTIFIER)
|
||||
.and_then(|r| r.ok_or_else(|| "Timestamp inherent data not found".into()))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub struct InherentDataProvider;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl ProvideInherentData for InherentDataProvider {
|
||||
fn inherent_identifier(&self) -> &'static InherentIdentifier {
|
||||
&INHERENT_IDENTIFIER
|
||||
}
|
||||
|
||||
fn provide_inherent_data(
|
||||
&self,
|
||||
inherent_data: &mut InherentData,
|
||||
) -> Result<(), inherents::Error> {
|
||||
use std::time::SystemTime;
|
||||
|
||||
let now = SystemTime::now();
|
||||
now.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.map_err(|_| {
|
||||
"Current time is before unix epoch".into()
|
||||
}).and_then(|d| {
|
||||
let duration: InherentType = d.as_millis() as u64;
|
||||
inherent_data.put_data(INHERENT_IDENTIFIER, &duration)
|
||||
})
|
||||
}
|
||||
|
||||
fn error_to_string(&self, error: &[u8]) -> Option<String> {
|
||||
InherentError::try_from(&INHERENT_IDENTIFIER, error).map(|e| format!("{:?}", e))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// A trait which is called when the timestamp is set.
|
||||
#[impl_trait_for_tuples::impl_for_tuples(30)]
|
||||
pub trait OnTimestampSet<Moment> {
|
||||
fn on_timestamp_set(moment: Moment);
|
||||
}
|
||||
Reference in New Issue
Block a user