Rewrite Inherent data (#1488)

* Implement new inherent data

* Fixes compilation on wasm

* Fixes after rebase

* Switch back to generate inherent stuff by macro

* Update after rebase

* Apply suggestions from code review

Co-Authored-By: bkchr <bkchr@users.noreply.github.com>

* Fix compilation after rebase

* Address grumbles

* Remove `InherentDataProviders` from `Client`

* Update wasm files after rebase

* Address grumbles

* Fixes compilation after latest merge

* Last fix
This commit is contained in:
Bastian Köcher
2019-01-22 17:52:08 +01:00
committed by GitHub
parent b14917e63f
commit 70b1af7b1e
55 changed files with 1513 additions and 661 deletions
+2
View File
@@ -9,6 +9,7 @@ parity-codec = { version = "2.2", default-features = false }
parity-codec-derive = { version = "2.1", default-features = false }
serde = { version = "1.0", default-features = false }
substrate-primitives = { path = "../../core/primitives", default-features = false }
substrate-inherents = { path = "../../core/inherents", default-features = false }
sr-std = { path = "../../core/sr-std", default-features = false }
sr-io = { path = "../../core/sr-io", default-features = false }
sr-primitives = { path = "../../core/sr-primitives", default-features = false }
@@ -37,4 +38,5 @@ std = [
"srml-consensus/std",
"srml-timestamp/std",
"srml-staking/std",
"substrate-inherents/std",
]
+109 -19
View File
@@ -18,8 +18,6 @@
#![cfg_attr(not(feature = "std"), no_std)]
#[allow(unused_imports)]
#[macro_use]
extern crate sr_std as rstd;
#[macro_use]
@@ -31,9 +29,10 @@ extern crate srml_support as runtime_support;
extern crate sr_primitives as primitives;
extern crate srml_system as system;
extern crate srml_timestamp as timestamp;
pub extern crate srml_timestamp as timestamp;
extern crate srml_staking as staking;
extern crate substrate_primitives;
extern crate substrate_inherents as inherents;
#[cfg(test)]
extern crate srml_consensus as consensus;
@@ -48,15 +47,93 @@ extern crate lazy_static;
#[cfg(test)]
extern crate parking_lot;
use rstd::prelude::*;
use rstd::{result, prelude::*};
use runtime_support::storage::StorageValue;
use runtime_support::dispatch::Result;
use primitives::traits::{As, Zero};
use timestamp::OnTimestampSet;
#[cfg(feature = "std")]
use timestamp::TimestampInherentData;
#[cfg(feature = "std")]
use parity_codec::Decode;
use inherents::{RuntimeString, InherentIdentifier, InherentData, ProvideInherent, MakeFatalError};
#[cfg(feature = "std")]
use inherents::{InherentDataProviders, ProvideInherentData};
mod mock;
mod tests;
/// 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::Result<InherentType, RuntimeString>;
/// Replace aura inherent data.
fn aura_replace_inherent_data(&mut self, new: InherentType);
}
impl AuraInherentData for InherentData {
fn aura_inherent_data(&self) -> result::Result<InherentType, RuntimeString> {
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::Result<(), RuntimeString> {
if !providers.has_provider(&timestamp::INHERENT_IDENTIFIER) {
// Add the timestamp inherent data provider, as we require it.
providers.register_provider(timestamp::InherentDataProvider)
} else {
Ok(())
}
}
fn inherent_identifier(&self) -> &'static inherents::InherentIdentifier {
&INHERENT_IDENTIFIER
}
fn provide_inherent_data(
&self,
inherent_data: &mut InherentData,
) -> result::Result<(), RuntimeString> {
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> {
RuntimeString::decode(&mut &error[..]).map(Into::into)
}
}
/// Something which can handle Aura consensus reports.
pub trait HandleReport {
fn handle_report(report: AuraReport);
@@ -133,20 +210,6 @@ impl<T: Trait> Module<T> {
<timestamp::Module<T>>::block_period().as_().saturating_mul(2)
}
/// Verify an inherent slot that is used in a block seal against a timestamp
/// extracted from the block.
// TODO: ensure `ProvideInherent` can deal with dependencies like this.
// https://github.com/paritytech/substrate/issues/1228
pub fn verify_inherent(timestamp: T::Moment, seal_slot: u64) -> Result {
let timestamp_based_slot = timestamp.as_() / Self::slot_duration();
if timestamp_based_slot == seal_slot {
Ok(())
} else {
Err("timestamp set in block doesn't match slot in seal".into())
}
}
fn on_timestamp_set<H: HandleReport>(now: T::Moment, slot_duration: T::Moment) {
let last = Self::last();
<Self as Store>::LastTimestamp::put(now.clone());
@@ -197,3 +260,30 @@ impl<T: staking::Trait + Trait> HandleReport for StakingSlasher<T> {
);
}
}
impl<T: Trait> ProvideInherent for Module<T> {
type Call = timestamp::Call<T>;
type Error = MakeFatalError<RuntimeString>;
const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER;
fn create_inherent(_: &InherentData) -> Option<Self::Call> {
None
}
fn check_inherent(call: &Self::Call, data: &InherentData) -> result::Result<(), Self::Error> {
let timestamp = match call {
timestamp::Call::set(ref timestamp) => timestamp.clone(),
_ => return Ok(()),
};
let timestamp_based_slot = timestamp.as_() / Self::slot_duration();
let seal_slot = data.aura_inherent_data()?;
if timestamp_based_slot == seal_slot {
Ok(())
} else {
Err(RuntimeString::from("timestamp set in block doesn't match slot in seal").into())
}
}
}
-3
View File
@@ -32,7 +32,6 @@ impl_outer_origin!{
pub struct Test;
impl consensus::Trait for Test {
const NOTE_OFFLINE_POSITION: u32 = 1;
type Log = DigestItem;
type SessionKey = UintAuthorityId;
type InherentOfflineReport = ();
@@ -53,8 +52,6 @@ impl system::Trait for Test {
}
impl timestamp::Trait for Test {
const TIMESTAMP_SET_POSITION: u32 = 0;
type Moment = u64;
type OnTimestampSet = Aura;
}
+2
View File
@@ -9,6 +9,7 @@ serde = { version = "1.0", default-features = false }
parity-codec = { version = "2.2", default-features = false }
parity-codec-derive = { version = "2.1", default-features = false }
substrate-primitives = { path = "../../core/primitives", default-features = false }
substrate-inherents = { path = "../../core/inherents", default-features = false }
sr-std = { path = "../../core/sr-std", default-features = false }
sr-io = { path = "../../core/sr-io", default-features = false }
sr-primitives = { path = "../../core/sr-primitives", default-features = false }
@@ -26,4 +27,5 @@ std = [
"srml-support/std",
"sr-primitives/std",
"srml-system/std",
"substrate-inherents/std",
]
+37 -37
View File
@@ -18,8 +18,6 @@
#![cfg_attr(not(feature = "std"), no_std)]
#[allow(unused_imports)]
#[macro_use]
extern crate sr_std as rstd;
#[macro_use]
@@ -37,18 +35,19 @@ extern crate substrate_primitives;
#[cfg(test)]
extern crate sr_io as runtime_io;
extern crate substrate_inherents as inherents;
use rstd::prelude::*;
use rstd::result;
use parity_codec::Encode;
use runtime_support::{storage, Parameter};
use runtime_support::storage::StorageValue;
use runtime_support::storage::unhashed::StorageVec;
use primitives::CheckInherentError;
use primitives::traits::{
MaybeSerializeDebug, Member, ProvideInherent, Block as BlockT
};
use primitives::traits::{MaybeSerializeDebug, Member};
use substrate_primitives::storage::well_known_keys;
use system::{ensure_signed, ensure_inherent};
use inherents::{
ProvideInherent, InherentData, InherentIdentifier, RuntimeString, MakeFatalError
};
#[cfg(any(feature = "std", test))]
use substrate_primitives::Ed25519AuthorityId;
@@ -56,6 +55,12 @@ use substrate_primitives::Ed25519AuthorityId;
mod mock;
mod tests;
/// The identifier for consensus inherents.
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"offlrep0";
/// The error type used by this inherent.
pub type InherentError = RuntimeString;
struct AuthorityStorageVec<S: codec::Codec + Default>(rstd::marker::PhantomData<S>);
impl<S: codec::Codec + Default> StorageVec for AuthorityStorageVec<S> {
type Item = S;
@@ -158,9 +163,6 @@ impl<N> From<RawLog<N>> for primitives::testing::DigestItem where N: Into<Ed2551
}
pub trait Trait: system::Trait {
/// The allowed extrinsic position for `note_offline` inherent.
const NOTE_OFFLINE_POSITION: u32;
/// Type for all log entries of this module.
type Log: From<Log<Self>> + Into<system::DigestItemOf<Self>>;
@@ -206,12 +208,6 @@ decl_module! {
fn note_offline(origin, offline: <T::InherentOfflineReport as InherentOfflineReport>::Inherent) {
ensure_inherent(origin)?;
assert!(
<system::Module<T>>::extrinsic_index() == Some(T::NOTE_OFFLINE_POSITION),
"note_offline extrinsic must be at position {} in the block",
T::NOTE_OFFLINE_POSITION
);
T::InherentOfflineReport::handle_report(offline);
}
@@ -292,34 +288,38 @@ impl<T: Trait> Module<T> {
}
impl<T: Trait> ProvideInherent for Module<T> {
type Inherent = <T::InherentOfflineReport as InherentOfflineReport>::Inherent;
type Call = Call<T>;
type Error = MakeFatalError<RuntimeString>;
const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER;
fn create_inherent_extrinsics(data: Self::Inherent) -> Vec<(u32, Self::Call)> {
if <T::InherentOfflineReport as InherentOfflineReport>::is_empty(&data) {
vec![]
fn create_inherent(data: &InherentData) -> Option<Self::Call> {
if let Ok(Some(data)) =
data.get_data::<<T::InherentOfflineReport as InherentOfflineReport>::Inherent>(
&INHERENT_IDENTIFIER
)
{
if <T::InherentOfflineReport as InherentOfflineReport>::is_empty(&data) {
None
} else {
Some(Call::note_offline(data))
}
} else {
vec![(T::NOTE_OFFLINE_POSITION, Call::note_offline(data))]
None
}
}
fn check_inherent<Block: BlockT, F: Fn(&Block::Extrinsic) -> Option<&Self::Call>>(
block: &Block, expected: Self::Inherent, extract_function: &F
) -> result::Result<(), CheckInherentError> {
let noted_offline = block
.extrinsics()
.get(T::NOTE_OFFLINE_POSITION as usize)
.and_then(|xt| match extract_function(&xt) {
Some(Call::note_offline(ref x)) => Some(x),
_ => None,
});
fn check_inherent(call: &Self::Call, data: &InherentData) -> Result<(), Self::Error> {
let offline = match call {
Call::note_offline(ref offline) => offline,
_ => return Ok(()),
};
// REVIEW: perhaps we should be passing a `None` to check_inherent.
if let Some(noted_offline) = noted_offline {
<T::InherentOfflineReport as InherentOfflineReport>::check_inherent(&noted_offline, &expected)
.map_err(|e| CheckInherentError::Other(e.into()))?;
}
let expected = data
.get_data::<<T::InherentOfflineReport as InherentOfflineReport>::Inherent>(&INHERENT_IDENTIFIER)?
.ok_or(RuntimeString::from("No `offline_report` found in the inherent data!"))?;
Ok(())
<T::InherentOfflineReport as InherentOfflineReport>::check_inherent(
&offline, &expected
).map_err(|e| RuntimeString::from(e).into())
}
}
-1
View File
@@ -31,7 +31,6 @@ impl_outer_origin!{
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Test;
impl Trait for Test {
const NOTE_OFFLINE_POSITION: u32 = 1;
type Log = DigestItem;
type SessionKey = UintAuthorityId;
type InherentOfflineReport = ::InstantFinalityReportVec<()>;
+16 -4
View File
@@ -18,9 +18,10 @@
#![cfg(test)]
use primitives::{generic, testing::{self, UintAuthorityId}, traits::{OnFinalise, ProvideInherent}};
use primitives::{generic, testing::{self, UintAuthorityId}, traits::OnFinalise};
use runtime_io::with_externalities;
use mock::{Consensus, System, new_test_ext};
use inherents::{InherentData, ProvideInherent};
#[test]
fn authorities_change_logged() {
@@ -31,7 +32,13 @@ fn authorities_change_logged() {
let header = System::finalise();
assert_eq!(header.digest, testing::Digest {
logs: vec![
generic::DigestItem::AuthoritiesChange(vec![UintAuthorityId(4).into(), UintAuthorityId(5).into(), UintAuthorityId(6).into()]),
generic::DigestItem::AuthoritiesChange(
vec![
UintAuthorityId(4).into(),
UintAuthorityId(5).into(),
UintAuthorityId(6).into()
]
),
],
});
});
@@ -67,7 +74,12 @@ fn authorities_change_is_not_logged_when_changed_back_to_original() {
fn offline_report_can_be_excluded() {
with_externalities(&mut new_test_ext(vec![1, 2, 3]), || {
System::initialise(&1, &Default::default(), &Default::default());
assert!(Consensus::create_inherent_extrinsics(Vec::new()).is_empty());
assert_eq!(Consensus::create_inherent_extrinsics(vec![0]).len(), 1);
assert!(Consensus::create_inherent(&InherentData::new()).is_none());
let offline_report: Vec<u32> = vec![0];
let mut data = InherentData::new();
data.put_data(super::INHERENT_IDENTIFIER, &offline_report).unwrap();
assert!(Consensus::create_inherent(&data).is_some());
});
}
-3
View File
@@ -114,7 +114,6 @@ decl_event!(
decl_storage! {
trait Store for Module<T: Trait> as Session {
/// The current set of validators.
pub Validators get(validators) config(): Vec<T::AccountId>;
/// Current length of the session.
@@ -246,7 +245,6 @@ mod tests {
#[derive(Clone, Eq, PartialEq)]
pub struct Test;
impl consensus::Trait for Test {
const NOTE_OFFLINE_POSITION: u32 = 1;
type Log = DigestItem;
type SessionKey = UintAuthorityId;
type InherentOfflineReport = ();
@@ -265,7 +263,6 @@ mod tests {
type Log = DigestItem;
}
impl timestamp::Trait for Test {
const TIMESTAMP_SET_POSITION: u32 = 0;
type Moment = u64;
type OnTimestampSet = ();
}
-2
View File
@@ -32,7 +32,6 @@ impl_outer_origin!{
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Test;
impl consensus::Trait for Test {
const NOTE_OFFLINE_POSITION: u32 = 1;
type Log = DigestItem;
type SessionKey = UintAuthorityId;
type InherentOfflineReport = ();
@@ -63,7 +62,6 @@ impl session::Trait for Test {
type Event = ();
}
impl timestamp::Trait for Test {
const TIMESTAMP_SET_POSITION: u32 = 0;
type Moment = u64;
type OnTimestampSet = ();
}
+2
View File
@@ -12,6 +12,7 @@ srml-metadata = { path = "../metadata", default-features = false }
sr-std = { path = "../../core/sr-std", default-features = false }
sr-io = { path = "../../core/sr-io", default-features = false }
sr-primitives = { path = "../../core/sr-primitives", default-features = false }
substrate-inherents = { path = "../../core/inherents", default-features = false }
srml-support-procedural = { path = "./procedural" }
mashup = "0.1.7"
once_cell = { version = "0.1.6", default-features = false, optional = true }
@@ -32,6 +33,7 @@ std = [
"sr-std/std",
"sr-primitives/std",
"srml-metadata/std",
"substrate-inherents/std",
]
nightly = []
strict = []
+54 -36
View File
@@ -15,11 +15,11 @@
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
#[doc(hidden)]
pub use rstd::{cmp, result::Result, vec::Vec};
pub use rstd::vec::Vec;
#[doc(hidden)]
pub use runtime_primitives::{
traits::{ProvideInherent, Block as BlockT}, CheckInherentError
};
pub use runtime_primitives::traits::Block as BlockT;
#[doc(hidden)]
pub use inherents::{InherentData, ProvideInherent, CheckInherentsResult, IsFatalError};
/// Implement the outer inherent.
@@ -30,54 +30,72 @@ pub use runtime_primitives::{
/// ```nocompile
/// impl_outer_inherent! {
/// pub struct InherentData where Block = Block, UncheckedExtrinsic = UncheckedExtrinsic {
/// timestamp: Timestamp export Error as TimestampInherentError,
/// timestamp: Timestamp,
/// consensus: Consensus,
/// /// Aura module using the `Timestamp` call.
/// aura: Timestamp,
/// }
/// }
/// ```
///
/// Additional parameters after `UncheckedExtrinsic` are `Error` and `Call`.
#[macro_export]
macro_rules! impl_outer_inherent {
(
for $runtime:ident,
Block = $block:ident,
InherentData = $inherent:ty
impl Inherents where Block = $block:ident, UncheckedExtrinsic = $uncheckedextrinsic:ident
{
$( $module:ident: $module_ty:ident,)*
$( $module:ident: $call:ident, )*
}
) => {
impl $runtime {
fn check_inherents(
block: $block,
data: $inherent
) -> $crate::inherent::Result<(), $crate::inherent::CheckInherentError> {
use $crate::inherent::CheckInherentError;
trait InherentDataExt {
fn create_extrinsics(&self) ->
$crate::inherent::Vec<<$block as $crate::inherent::BlockT>::Extrinsic>;
fn check_extrinsics(&self, block: &$block) -> $crate::inherent::CheckInherentsResult;
}
impl InherentDataExt for $crate::inherent::InherentData {
fn create_extrinsics(&self) ->
$crate::inherent::Vec<<$block as $crate::inherent::BlockT>::Extrinsic> {
use $crate::inherent::ProvideInherent;
let mut inherents = Vec::new();
let mut max_valid_after = None;
$(
let res = <$module_ty as $crate::inherent::ProvideInherent>::check_inherent(
&block,
data.$module,
&|xt| match xt.function {
Call::$module_ty(ref data) => Some(data),
_ => None,
},
);
match res {
Err(CheckInherentError::ValidAtTimestamp(t)) =>
max_valid_after = $crate::inherent::cmp::max(max_valid_after, Some(t)),
res => res?
if let Some(inherent) = $module::create_inherent(self) {
inherents.push($uncheckedextrinsic::new_unsigned(
Call::$call(inherent))
);
}
)*
// once everything else has checked out, take the maximum of
// all things which are timestamp-restricted.
match max_valid_after {
Some(t) => Err(CheckInherentError::ValidAtTimestamp(t)),
None => Ok(())
inherents
}
fn check_extrinsics(&self, block: &$block) -> $crate::inherent::CheckInherentsResult {
use $crate::inherent::{ProvideInherent, IsFatalError};
let mut result = $crate::inherent::CheckInherentsResult::new();
for xt in block.extrinsics() {
if xt.is_signed().unwrap_or(false) {
break;
}
$(
match xt.function {
Call::$call(ref call) => {
if let Err(e) = $module::check_inherent(call, self) {
result.put_error(
$module::INHERENT_IDENTIFIER, &e
).expect("There is only one fatal error; qed");
if e.is_fatal_error() {
return result;
}
}
}
_ => {},
}
)*
}
result
}
}
};
+1
View File
@@ -32,6 +32,7 @@ extern crate srml_metadata;
extern crate mashup;
#[cfg_attr(test, macro_use)]
extern crate srml_support_procedural;
extern crate substrate_inherents as inherents;
#[cfg(test)]
#[macro_use]
+73 -50
View File
@@ -20,11 +20,11 @@
//! mostly for to combine data types and metadata of the included modules.
/// Construct a runtime, with the given name and the given modules.
///
///
/// The parameters here are specific types for Block, NodeBlock and InherentData
/// (TODO: describe the difference between Block and NodeBlock)
/// and the modules that are used by the runtime.
///
///
/// # Example:
///
/// ```nocompile
@@ -32,7 +32,7 @@
/// pub enum Runtime with Log(interalIdent: DigestItem<SessionKey>) where
/// Block = Block,
/// NodeBlock = runtime::Block,
/// InherentData = BasicInherentData
/// UncheckedExtrinsic = UncheckedExtrinsic
/// {
/// System: system,
/// Test: test::{default, Log(Test)},
@@ -44,10 +44,10 @@
/// The module `System: system` will expand to `System: system::{Module, Call, Storage, Event<T>, Config<T>}`.
/// The identifier `System` is the name of the module and the lower case identifier `system` is the
/// name of the Rust module/crate for this Substrate module.
///
///
/// The module `Test: test::{default, Log(Test)}` will expand to
/// `Test: test::{Module, Call, Storage, Event<T>, Config<T>, Log(Test)}`.
///
///
/// The module `Test2: test_with_long_module::{Module}` will expand to
/// `Test2: test_with_long_module::{Module}`.
///
@@ -59,9 +59,9 @@
/// - `Origin` or `Origin<T>` (if the origin is generic)
/// - `Config` or `Config<T>` (if the config is generic)
/// - `Log( $(IDENT),* )`
///
/// The
///
/// - `Inherent $( (CALL) )*` - If the module provides/can check inherents. The optional parameter
/// is for modules that use a `Call` from a different module as
/// inherent.
#[macro_export]
macro_rules! construct_runtime {
@@ -73,7 +73,7 @@ macro_rules! construct_runtime {
where
Block = $block:ident,
NodeBlock = $node_block:ty,
InherentData = $inherent:ty
UncheckedExtrinsic = $uncheckedextrinsic:ident
{
$( $rest:tt )*
}
@@ -82,7 +82,7 @@ macro_rules! construct_runtime {
$runtime;
$block;
$node_block;
$inherent;
$uncheckedextrinsic;
$log_internal < $( $log_genarg ),* >;
;
$( $rest )*
@@ -92,7 +92,7 @@ macro_rules! construct_runtime {
$runtime:ident;
$block:ident;
$node_block:ty;
$inherent:ty;
$uncheckedextrinsic:ident;
$log_internal:ident <$( $log_genarg:ty ),+>;
$(
$expanded_name:ident: $expanded_module:ident::{
@@ -120,7 +120,7 @@ macro_rules! construct_runtime {
$runtime;
$block;
$node_block;
$inherent;
$uncheckedextrinsic;
$log_internal < $( $log_genarg ),* >;
$(
$expanded_name: $expanded_module::{
@@ -148,7 +148,7 @@ macro_rules! construct_runtime {
$runtime:ident;
$block:ident;
$node_block:ty;
$inherent:ty;
$uncheckedextrinsic:ident;
$log_internal:ident <$( $log_genarg:ty ),+>;
$(
$expanded_name:ident: $expanded_module:ident::{
@@ -183,7 +183,7 @@ macro_rules! construct_runtime {
$runtime;
$block;
$node_block;
$inherent;
$uncheckedextrinsic;
$log_internal < $( $log_genarg ),* >;
$(
$expanded_name: $expanded_module::{
@@ -217,7 +217,7 @@ macro_rules! construct_runtime {
$runtime:ident;
$block:ident;
$node_block:ty;
$inherent:ty;
$uncheckedextrinsic:ident;
$log_internal:ident <$( $log_genarg:ty ),+>;
$(
$expanded_name:ident: $expanded_module:ident::{
@@ -251,7 +251,7 @@ macro_rules! construct_runtime {
$runtime;
$block;
$node_block;
$inherent;
$uncheckedextrinsic;
$log_internal < $( $log_genarg ),* >;
$(
$expanded_name: $expanded_module::{
@@ -287,7 +287,7 @@ macro_rules! construct_runtime {
$runtime:ident;
$block:ident;
$node_block:ty;
$inherent:ty;
$uncheckedextrinsic:ident;
$log_internal:ident <$( $log_genarg:ty ),+>;
$(
$name:ident: $module:ident::{
@@ -299,9 +299,9 @@ macro_rules! construct_runtime {
}
),*;
) => {
// This generates a substrate_generate_ident_name macro that will substitute
// This generates a substrate_generate_ident_name macro that will substitute
// "config-ident FooModule" => FooModuleConfig for every module included in the
// runtime.
// runtime.
mashup! {
$(
substrate_generate_ident_name["config-ident" $name] = $name Config;
@@ -370,19 +370,19 @@ macro_rules! construct_runtime {
__decl_outer_inherent!(
$runtime;
$block;
$inherent;
$uncheckedextrinsic;
;
$(
$name: $module::{ $( $modules $( <$modules_generic> )* ),* }
$name: $module::{ $( $modules $( ( $( $modules_args ),* ) )* ),* }
),*;
);
}
}
/// A macro that generates a "__decl" private macro that transforms parts of the runtime definition
/// to feed them into a public "impl" macro which accepts the format
/// to feed them into a public "impl" macro which accepts the format
/// "pub enum $name for $runtime where system = $system".
///
///
/// Used to define Event and Origin associated types.
#[macro_export]
#[doc(hidden)]
@@ -1109,23 +1109,23 @@ macro_rules! __decl_outer_inherent {
(
$runtime:ident;
$block:ident;
$inherent:ty;
$( $parsed_modules:ident :: $parsed_name:ident ),*;
$uncheckedextrinsic:ident;
$( $parsed_name:ident :: $parsed_call:ident ),*;
$name:ident: $module:ident::{
Inherent $(, $modules:ident $( <$modules_generic:ident> )* )*
Inherent $(, $modules:ident $( ( $( $modules_call:ident )* ) )* )*
}
$(, $rest_name:ident : $rest_module:ident::{
$( $rest_modules:ident $( <$rest_modules_generic:ident> )* ),*
$( $rest_modules:ident $( ( $( $rest_call:ident )* ) )* ),*
})*;
) => {
__decl_outer_inherent!(
$runtime;
$block;
$inherent;
$( $parsed_modules :: $parsed_name, )* $module::$name;
$uncheckedextrinsic;
$( $parsed_name :: $parsed_call, )* $name::$name;
$(
$rest_name: $rest_module::{
$( $rest_modules $( <$rest_modules_generic> )* ),*
$( $rest_modules $( ( $( $rest_call )* ) )* ),*
}
),*;
);
@@ -1133,24 +1133,49 @@ macro_rules! __decl_outer_inherent {
(
$runtime:ident;
$block:ident;
$inherent:ty;
$( $parsed_modules:ident :: $parsed_name:ident ),*;
$uncheckedextrinsic:ident;
$( $parsed_name:ident :: $parsed_call:ident ),*;
$name:ident: $module:ident::{
$ingore:ident $( <$ignor:ident> )* $(, $modules:ident $( <$modules_generic:ident> )* )*
Inherent ( $call:ident ) $(, $modules:ident $( ( $( $modules_call:ident )* ) )* )*
}
$(, $rest_name:ident : $rest_module:ident::{
$( $rest_modules:ident $( <$rest_modules_generic:ident> )* ),*
$( $rest_modules:ident $( ( $( $rest_call:ident )* ) )* ),*
})*;
) => {
__decl_outer_inherent!(
$runtime;
$block;
$inherent;
$( $parsed_modules :: $parsed_name ),*;
$name: $module::{ $( $modules $( <$modules_generic> )* ),* }
$uncheckedextrinsic;
$( $parsed_name :: $parsed_call, )* $name::$call;
$(
$rest_name: $rest_module::{
$( $rest_modules $( ( $( $rest_call )* ) )* ),*
}
),*;
);
};
(
$runtime:ident;
$block:ident;
$uncheckedextrinsic:ident;
$( $parsed_name:ident :: $parsed_call:ident ),*;
$name:ident: $module:ident::{
$ingore:ident $( ( $( $ignor:ident )* ) )*
$(, $modules:ident $( ( $( $modules_call:ident )* ) )* )*
}
$(, $rest_name:ident : $rest_module:ident::{
$( $rest_modules:ident $( ( $( $rest_call:ident )* ) )* ),*
})*;
) => {
__decl_outer_inherent!(
$runtime;
$block;
$uncheckedextrinsic;
$( $parsed_name :: $parsed_call ),*;
$name: $module::{ $( $modules $( ( $( $modules_call )* ) )* ),* }
$(
, $rest_name: $rest_module::{
$( $rest_modules $( <$rest_modules_generic> )* ),*
$( $rest_modules $( ( $( $rest_call )* ) )* ),*
}
)*;
);
@@ -1158,21 +1183,21 @@ macro_rules! __decl_outer_inherent {
(
$runtime:ident;
$block:ident;
$inherent:ty;
$( $parsed_modules:ident :: $parsed_name:ident ),*;
$uncheckedextrinsic:ident;
$( $parsed_name:ident :: $parsed_call:ident ),*;
$name:ident: $module:ident::{}
$(, $rest_name:ident : $rest_module:ident::{
$( $rest_modules:ident $( <$rest_modules_generic:ident> )* ),*
$( $rest_modules:ident $( ( $( $rest_call:ident )* ) )* ),*
})*;
) => {
__decl_outer_inherent!(
$runtime;
$block;
$inherent;
$( $parsed_modules :: $parsed_name ),*;
$uncheckedextrinsic;
$( $parsed_name :: $parsed_call ),*;
$(
$rest_name: $rest_module::{
$( $rest_modules $( <$rest_modules_generic> )* ),*
$( $rest_modules $( ( $( $rest_call )* ) )* ),*
}
),*;
);
@@ -1180,15 +1205,13 @@ macro_rules! __decl_outer_inherent {
(
$runtime:ident;
$block:ident;
$inherent:ty;
$( $parsed_modules:ident :: $parsed_name:ident ),*;
$uncheckedextrinsic:ident;
$( $parsed_name:ident :: $parsed_call:ident ),*;
;
) => {
impl_outer_inherent!(
for $runtime,
Block = $block,
InherentData = $inherent {
$($parsed_modules : $parsed_name,)*
impl Inherents where Block = $block, UncheckedExtrinsic = $uncheckedextrinsic {
$( $parsed_name : $parsed_call, )*
}
);
};
+4
View File
@@ -7,10 +7,12 @@ authors = ["Parity Technologies <admin@parity.io>"]
hex-literal = "0.1.0"
serde = { version = "1.0", default-features = false }
parity-codec = { version = "2.2", default-features = false }
parity-codec-derive = { version = "2.2", default-features = false }
substrate-primitives = { path = "../../core/primitives", default-features = false }
sr-std = { path = "../../core/sr-std", default-features = false }
sr-io = { path = "../../core/sr-io", default-features = false }
sr-primitives = { path = "../../core/sr-primitives", default-features = false }
substrate-inherents = { path = "../../core/inherents", default-features = false }
srml-support = { path = "../support", default-features = false }
srml-system = { path = "../system", default-features = false }
srml-consensus = { path = "../consensus", default-features = false }
@@ -28,6 +30,8 @@ std = [
"srml-consensus/std",
"serde/std",
"parity-codec/std",
"parity-codec-derive/std",
"substrate-primitives/std",
"srml-system/std",
"substrate-inherents/std",
]
+112 -35
View File
@@ -32,7 +32,6 @@
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg_attr(not(feature = "std"), macro_use)]
extern crate sr_std as rstd;
#[macro_use]
@@ -46,15 +45,95 @@ extern crate sr_primitives as runtime_primitives;
extern crate srml_system as system;
extern crate srml_consensus as consensus;
extern crate parity_codec as codec;
#[macro_use]
extern crate parity_codec_derive;
extern crate substrate_inherents as inherents;
use runtime_support::{StorageValue, Parameter};
use runtime_primitives::CheckInherentError;
use runtime_primitives::traits::{
As, SimpleArithmetic, Zero, ProvideInherent, Block as BlockT, Extrinsic
};
use runtime_primitives::traits::{As, SimpleArithmetic, Zero};
use system::ensure_inherent;
use rstd::{result, ops::{Mul, Div}, vec::Vec};
use rstd::{result, ops::{Mul, Div}, cmp};
use runtime_support::for_each_tuple;
use inherents::{RuntimeString, InherentIdentifier, ProvideInherent, IsFatalError, InherentData};
#[cfg(feature = "std")]
use inherents::ProvideInherentData;
/// 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)]
#[cfg_attr(feature = "std", derive(Debug, 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[..])
} else {
None
}
}
}
/// Auxiliary trait to extract timestamp inherent data.
pub trait TimestampInherentData {
/// Get timestamp inherent data.
fn timestamp_inherent_data(&self) -> Result<InherentType, RuntimeString>;
}
impl TimestampInherentData for InherentData {
fn timestamp_inherent_data(&self) -> Result<InherentType, RuntimeString> {
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<(), RuntimeString> {
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_secs();
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.
pub trait OnTimestampSet<Moment> {
@@ -80,11 +159,10 @@ macro_rules! impl_timestamp_set {
for_each_tuple!(impl_timestamp_set);
pub trait Trait: consensus::Trait + system::Trait {
/// The position of the required timestamp-set extrinsic.
const TIMESTAMP_SET_POSITION: u32;
/// Type used for expressing timestamp.
type Moment: Parameter + Default + SimpleArithmetic + Mul<Self::BlockNumber, Output = Self::Moment> + Div<Self::BlockNumber, Output = Self::Moment>;
type Moment: Parameter + Default + SimpleArithmetic
+ Mul<Self::BlockNumber, Output = Self::Moment>
+ Div<Self::BlockNumber, Output = Self::Moment>;
/// Something which can be notified when the timestamp is set. Set this to `()` if not needed.
type OnTimestampSet: OnTimestampSet<Self::Moment>;
}
@@ -102,11 +180,6 @@ decl_module! {
fn set(origin, #[compact] now: T::Moment) {
ensure_inherent(origin)?;
assert!(!<Self as Store>::DidUpdate::exists(), "Timestamp must be updated only once in the block");
assert!(
<system::Module<T>>::extrinsic_index() == Some(T::TIMESTAMP_SET_POSITION),
"Timestamp extrinsic must be at position {} in the block",
T::TIMESTAMP_SET_POSITION
);
assert!(
Self::now().is_zero() || now >= Self::now() + Self::block_period(),
"Timestamp must increment by at least <BlockPeriod> between sequential blocks"
@@ -152,33 +225,39 @@ impl<T: Trait> Module<T> {
}
}
impl<T: Trait> ProvideInherent for Module<T> {
type Inherent = T::Moment;
type Call = Call<T>;
fn extract_inherent_data(data: &InherentData) -> Result<InherentType, RuntimeString> {
data.get_data::<InherentType>(&INHERENT_IDENTIFIER)
.map_err(|_| RuntimeString::from("Invalid timestamp inherent data encoding."))?
.ok_or_else(|| "Timestamp inherent data is not provided.".into())
}
fn create_inherent_extrinsics(data: Self::Inherent) -> Vec<(u32, Self::Call)> {
let next_time = ::rstd::cmp::max(data, Self::now() + Self::block_period());
vec![(T::TIMESTAMP_SET_POSITION, Call::set(next_time.into()))]
impl<T: Trait> ProvideInherent for Module<T> {
type Call = Call<T>;
type Error = InherentError;
const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER;
fn create_inherent(data: &InherentData) -> Option<Self::Call> {
let data = extract_inherent_data(data).expect("Gets and decodes timestamp inherent data");
let next_time = cmp::max(As::sa(data), Self::now() + Self::block_period());
Some(Call::set(next_time.into()))
}
fn check_inherent<Block: BlockT, F: Fn(&Block::Extrinsic) -> Option<&Self::Call>>(
block: &Block, data: Self::Inherent, extract_function: &F
) -> result::Result<(), CheckInherentError> {
fn check_inherent(call: &Self::Call, data: &InherentData) -> result::Result<(), Self::Error> {
const MAX_TIMESTAMP_DRIFT: u64 = 60;
let xt = block.extrinsics().get(T::TIMESTAMP_SET_POSITION as usize)
.ok_or_else(|| CheckInherentError::Other("No valid timestamp inherent in block".into()))?;
let t = match (xt.is_signed(), extract_function(&xt)) {
(Some(false), Some(Call::set(ref t))) => t.clone(),
_ => return Err(CheckInherentError::Other("No valid timestamp inherent in block".into())),
let t = match call {
Call::set(ref t) => t.clone(),
_ => return Ok(()),
}.as_();
let data = extract_inherent_data(data).map_err(|e| InherentError::Other(e))?;
let minimum = (Self::now() + Self::block_period()).as_();
if t > data.as_() + MAX_TIMESTAMP_DRIFT {
Err(CheckInherentError::Other("Timestamp too far in future to accept".into()))
if t > data + MAX_TIMESTAMP_DRIFT {
Err(InherentError::Other("Timestamp too far in future to accept".into()))
} else if t < minimum {
Err(CheckInherentError::ValidAtTimestamp(minimum))
Err(InherentError::ValidAtTimestamp(minimum))
} else {
Ok(())
}
@@ -215,13 +294,11 @@ mod tests {
type Log = DigestItem;
}
impl consensus::Trait for Test {
const NOTE_OFFLINE_POSITION: u32 = 1;
type Log = DigestItem;
type SessionKey = UintAuthorityId;
type InherentOfflineReport = ();
}
impl Trait for Test {
const TIMESTAMP_SET_POSITION: u32 = 0;
type Moment = u64;
type OnTimestampSet = ();
}