mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 03:01:07 +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
Generated
+46
-6
@@ -2701,7 +2701,6 @@ dependencies = [
|
||||
"paint-authority-discovery 0.1.0",
|
||||
"paint-balances 2.0.0",
|
||||
"paint-contracts 2.0.0",
|
||||
"paint-finality-tracker 2.0.0",
|
||||
"paint-im-online 0.1.0",
|
||||
"paint-indices 2.0.0",
|
||||
"paint-support 2.0.0",
|
||||
@@ -2712,6 +2711,8 @@ dependencies = [
|
||||
"rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.102 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sp-finality-tracker 2.0.0",
|
||||
"sp-timestamp 2.0.0",
|
||||
"sr-io 2.0.0",
|
||||
"sr-primitives 2.0.0",
|
||||
"structopt 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@@ -3134,6 +3135,7 @@ dependencies = [
|
||||
"parity-scale-codec 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.102 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sp-timestamp 2.0.0",
|
||||
"sr-io 2.0.0",
|
||||
"sr-primitives 2.0.0",
|
||||
"sr-std 2.0.0",
|
||||
@@ -3169,6 +3171,7 @@ dependencies = [
|
||||
"paint-support 2.0.0",
|
||||
"paint-system 2.0.0",
|
||||
"parity-scale-codec 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sp-authorship 2.0.0",
|
||||
"sr-io 2.0.0",
|
||||
"sr-primitives 2.0.0",
|
||||
"sr-std 2.0.0",
|
||||
@@ -3189,6 +3192,7 @@ dependencies = [
|
||||
"parity-scale-codec 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.102 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sp-timestamp 2.0.0",
|
||||
"sr-io 2.0.0",
|
||||
"sr-primitives 2.0.0",
|
||||
"sr-staking-primitives 2.0.0",
|
||||
@@ -3398,6 +3402,7 @@ dependencies = [
|
||||
"paint-system 2.0.0",
|
||||
"parity-scale-codec 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.102 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sp-finality-tracker 2.0.0",
|
||||
"sr-io 2.0.0",
|
||||
"sr-primitives 2.0.0",
|
||||
"sr-std 2.0.0",
|
||||
@@ -3765,6 +3770,7 @@ dependencies = [
|
||||
"paint-system 2.0.0",
|
||||
"parity-scale-codec 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.102 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sp-timestamp 2.0.0",
|
||||
"sr-io 2.0.0",
|
||||
"sr-primitives 2.0.0",
|
||||
"sr-std 2.0.0",
|
||||
@@ -4991,6 +4997,36 @@ name = "sourcefile"
|
||||
version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "sp-authorship"
|
||||
version = "2.0.0"
|
||||
dependencies = [
|
||||
"parity-scale-codec 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sr-std 2.0.0",
|
||||
"substrate-inherents 2.0.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sp-finality-tracker"
|
||||
version = "2.0.0"
|
||||
dependencies = [
|
||||
"parity-scale-codec 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sr-std 2.0.0",
|
||||
"substrate-inherents 2.0.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sp-timestamp"
|
||||
version = "2.0.0"
|
||||
dependencies = [
|
||||
"impl-trait-for-tuples 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parity-scale-codec 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sr-api 2.0.0",
|
||||
"sr-primitives 2.0.0",
|
||||
"sr-std 2.0.0",
|
||||
"substrate-inherents 2.0.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "spin"
|
||||
version = "0.5.2"
|
||||
@@ -5504,9 +5540,9 @@ dependencies = [
|
||||
"futures-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"futures-timer 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"paint-aura 2.0.0",
|
||||
"parity-scale-codec 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sp-timestamp 2.0.0",
|
||||
"sr-api 2.0.0",
|
||||
"sr-io 2.0.0",
|
||||
"sr-primitives 2.0.0",
|
||||
@@ -5536,10 +5572,12 @@ name = "substrate-consensus-aura-primitives"
|
||||
version = "2.0.0"
|
||||
dependencies = [
|
||||
"parity-scale-codec 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sp-timestamp 2.0.0",
|
||||
"sr-api 2.0.0",
|
||||
"sr-primitives 2.0.0",
|
||||
"sr-std 2.0.0",
|
||||
"substrate-application-crypto 2.0.0",
|
||||
"substrate-inherents 2.0.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -5557,12 +5595,12 @@ dependencies = [
|
||||
"num-bigint 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-rational 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"paint-babe 2.0.0",
|
||||
"parity-scale-codec 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"pdqselect 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"schnorrkel 0.8.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sp-timestamp 2.0.0",
|
||||
"sr-api 2.0.0",
|
||||
"sr-io 2.0.0",
|
||||
"sr-primitives 2.0.0",
|
||||
@@ -5596,11 +5634,13 @@ version = "2.0.0"
|
||||
dependencies = [
|
||||
"parity-scale-codec 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"schnorrkel 0.8.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sp-timestamp 2.0.0",
|
||||
"sr-api 2.0.0",
|
||||
"sr-primitives 2.0.0",
|
||||
"sr-std 2.0.0",
|
||||
"substrate-application-crypto 2.0.0",
|
||||
"substrate-consensus-slots 2.0.0",
|
||||
"substrate-inherents 2.0.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -5629,8 +5669,8 @@ dependencies = [
|
||||
"derive_more 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"futures-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"paint-timestamp 2.0.0",
|
||||
"parity-scale-codec 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sp-timestamp 2.0.0",
|
||||
"sr-primitives 2.0.0",
|
||||
"substrate-block-builder-runtime-api 2.0.0",
|
||||
"substrate-client-api 2.0.0",
|
||||
@@ -5674,7 +5714,7 @@ name = "substrate-consensus-uncles"
|
||||
version = "2.0.0"
|
||||
dependencies = [
|
||||
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"paint-authorship 0.1.0",
|
||||
"sp-authorship 2.0.0",
|
||||
"sr-primitives 2.0.0",
|
||||
"substrate-client-api 2.0.0",
|
||||
"substrate-consensus-common 2.0.0",
|
||||
@@ -5750,11 +5790,11 @@ dependencies = [
|
||||
"futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"futures-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"paint-finality-tracker 2.0.0",
|
||||
"parity-scale-codec 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_json 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sp-finality-tracker 2.0.0",
|
||||
"sr-api 2.0.0",
|
||||
"sr-primitives 2.0.0",
|
||||
"substrate-client 2.0.0",
|
||||
|
||||
@@ -89,6 +89,7 @@ members = [
|
||||
"paint/utility",
|
||||
"primitives/application-crypto",
|
||||
"primitives/authority-discovery",
|
||||
"primitives/authorship",
|
||||
"primitives/block-builder/runtime-api",
|
||||
"primitives/consensus/aura",
|
||||
"primitives/consensus/babe",
|
||||
@@ -98,6 +99,7 @@ members = [
|
||||
"primitives/core/debug-derive",
|
||||
"primitives/core/storage",
|
||||
"primitives/externalities",
|
||||
"primitives/finality-tracker",
|
||||
"primitives/finality-grandpa",
|
||||
"primitives/inherents",
|
||||
"primitives/keyring",
|
||||
@@ -122,6 +124,7 @@ members = [
|
||||
"primitives/sr-std",
|
||||
"primitives/sr-version",
|
||||
"primitives/state-machine",
|
||||
"primitives/timestamp",
|
||||
"primitives/transaction-pool/runtime-api",
|
||||
"primitives/trie",
|
||||
"primitives/wasm-interface",
|
||||
|
||||
@@ -39,6 +39,7 @@ babe-primitives = { package = "substrate-consensus-babe-primitives", path = "../
|
||||
grandpa_primitives = { package = "substrate-finality-grandpa-primitives", path = "../../../primitives/finality-grandpa" }
|
||||
primitives = { package = "substrate-primitives", path = "../../../primitives/core" }
|
||||
sr-primitives = { path = "../../../primitives/sr-primitives" }
|
||||
sp-timestamp = { path = "../../../primitives/timestamp", default-features = false }
|
||||
|
||||
# core dependencies
|
||||
runtime-io = { package = "sr-io", path = "../../../primitives/sr-io" }
|
||||
@@ -62,7 +63,7 @@ authority-discovery = { package = "substrate-authority-discovery", path = "../..
|
||||
# paint dependencies
|
||||
indices = { package = "paint-indices", path = "../../../paint/indices" }
|
||||
timestamp = { package = "paint-timestamp", path = "../../../paint/timestamp", default-features = false }
|
||||
finality_tracker = { package = "paint-finality-tracker", path = "../../../paint/finality-tracker", default-features = false }
|
||||
sp-finality-tracker = { path = "../../../primitives/finality-tracker", default-features = false }
|
||||
contracts = { package = "paint-contracts", path = "../../../paint/contracts" }
|
||||
system = { package = "paint-system", path = "../../../paint/system" }
|
||||
balances = { package = "paint-balances", path = "../../../paint/balances" }
|
||||
@@ -71,7 +72,6 @@ support = { package = "paint-support", path = "../../../paint/support", default-
|
||||
im_online = { package = "paint-im-online", path = "../../../paint/im-online", default-features = false }
|
||||
sr-authority-discovery = { package = "paint-authority-discovery", path = "../../../paint/authority-discovery"}
|
||||
|
||||
|
||||
# node-specific dependencies
|
||||
node-runtime = { path = "../runtime" }
|
||||
node-rpc = { path = "../rpc" }
|
||||
|
||||
@@ -35,8 +35,8 @@ use sr_primitives::{
|
||||
use transaction_factory::RuntimeAdapter;
|
||||
use transaction_factory::modes::Mode;
|
||||
use inherents::InherentData;
|
||||
use timestamp;
|
||||
use finality_tracker;
|
||||
use sp_timestamp;
|
||||
use sp_finality_tracker;
|
||||
|
||||
type AccountPublic = <Signature as Verify>::Signer;
|
||||
|
||||
@@ -160,9 +160,9 @@ impl RuntimeAdapter for FactoryState<Number> {
|
||||
let timestamp = (self.block_no as u64 + 1) * MinimumPeriod::get();
|
||||
|
||||
let mut inherent = InherentData::new();
|
||||
inherent.put_data(timestamp::INHERENT_IDENTIFIER, ×tamp)
|
||||
inherent.put_data(sp_timestamp::INHERENT_IDENTIFIER, ×tamp)
|
||||
.expect("Failed putting timestamp inherent");
|
||||
inherent.put_data(finality_tracker::INHERENT_IDENTIFIER, &self.block_no)
|
||||
inherent.put_data(sp_finality_tracker::INHERENT_IDENTIFIER, &self.block_no)
|
||||
.expect("Failed putting finalized number inherent");
|
||||
inherent
|
||||
}
|
||||
|
||||
@@ -370,8 +370,8 @@ mod tests {
|
||||
traits::Verify,
|
||||
OpaqueExtrinsic,
|
||||
};
|
||||
use timestamp;
|
||||
use finality_tracker;
|
||||
use sp_timestamp;
|
||||
use sp_finality_tracker;
|
||||
use keyring::AccountKeyring;
|
||||
use substrate_service::{AbstractService, Roles};
|
||||
use crate::service::new_full;
|
||||
@@ -484,7 +484,7 @@ mod tests {
|
||||
let mut inherent_data = inherent_data_providers
|
||||
.create_inherent_data()
|
||||
.expect("Creates inherent data.");
|
||||
inherent_data.replace_data(finality_tracker::INHERENT_IDENTIFIER, &1u64);
|
||||
inherent_data.replace_data(sp_finality_tracker::INHERENT_IDENTIFIER, &1u64);
|
||||
|
||||
let parent_id = BlockId::number(service.client().info().chain.best_number);
|
||||
let parent_header = service.client().header(&parent_id).unwrap().unwrap();
|
||||
@@ -498,7 +498,7 @@ mod tests {
|
||||
// even though there's only one authority some slots might be empty,
|
||||
// so we must keep trying the next slots until we can claim one.
|
||||
let babe_pre_digest = loop {
|
||||
inherent_data.replace_data(timestamp::INHERENT_IDENTIFIER, &(slot_num * SLOT_DURATION));
|
||||
inherent_data.replace_data(sp_timestamp::INHERENT_IDENTIFIER, &(slot_num * SLOT_DURATION));
|
||||
if let Some(babe_pre_digest) = babe::test_helpers::claim_slot(
|
||||
slot_num,
|
||||
&parent_header,
|
||||
|
||||
@@ -27,7 +27,7 @@ runtime_version = { package = "sr-version", path = "../../../primitives/sr-versi
|
||||
slots = { package = "substrate-consensus-slots", path = "../slots" }
|
||||
sr-api = { path = "../../../primitives/sr-api" }
|
||||
sr-primitives = { path = "../../../primitives/sr-primitives" }
|
||||
paint-aura = { path = "../../../paint/aura" }
|
||||
sp-timestamp = { path = "../../../primitives/timestamp" }
|
||||
substrate-telemetry = { path = "../../telemetry" }
|
||||
|
||||
[dev-dependencies]
|
||||
|
||||
@@ -56,10 +56,10 @@ use futures::prelude::*;
|
||||
use parking_lot::Mutex;
|
||||
use log::{debug, info, trace};
|
||||
|
||||
use paint_aura::{
|
||||
InherentType as AuraInherent, AuraInherentData,
|
||||
timestamp::{TimestampInherentData, InherentType as TimestampInherent, InherentError as TIError}
|
||||
use sp_timestamp::{
|
||||
TimestampInherentData, InherentType as TimestampInherent, InherentError as TIError
|
||||
};
|
||||
|
||||
use substrate_telemetry::{telemetry, CONSENSUS_TRACE, CONSENSUS_DEBUG, CONSENSUS_INFO};
|
||||
|
||||
use slots::{CheckedHeader, SlotData, SlotWorker, SlotInfo, SlotCompatible};
|
||||
@@ -69,7 +69,13 @@ use keystore::KeyStorePtr;
|
||||
|
||||
use sr_api::ApiExt;
|
||||
|
||||
pub use aura_primitives::*;
|
||||
pub use aura_primitives::{
|
||||
ConsensusLog, AuraApi, AURA_ENGINE_ID,
|
||||
inherents::{
|
||||
InherentType as AuraInherent,
|
||||
AuraInherentData, INHERENT_IDENTIFIER, InherentDataProvider,
|
||||
},
|
||||
};
|
||||
pub use consensus_common::SyncOracle;
|
||||
pub use digest::CompatibleDigestItem;
|
||||
|
||||
@@ -654,9 +660,9 @@ fn register_aura_inherent_data_provider(
|
||||
inherent_data_providers: &InherentDataProviders,
|
||||
slot_duration: u64,
|
||||
) -> Result<(), consensus_common::Error> {
|
||||
if !inherent_data_providers.has_provider(&paint_aura::INHERENT_IDENTIFIER) {
|
||||
if !inherent_data_providers.has_provider(&INHERENT_IDENTIFIER) {
|
||||
inherent_data_providers
|
||||
.register_provider(paint_aura::InherentDataProvider::new(slot_duration))
|
||||
.register_provider(InherentDataProvider::new(slot_duration))
|
||||
.map_err(Into::into)
|
||||
.map_err(consensus_common::Error::InherentData)
|
||||
} else {
|
||||
|
||||
@@ -16,9 +16,9 @@ num-traits = "0.2.8"
|
||||
runtime-version = { package = "sr-version", path = "../../../primitives/sr-version" }
|
||||
runtime-io = { package = "sr-io", path = "../../../primitives/sr-io" }
|
||||
inherents = { package = "substrate-inherents", path = "../../../primitives/inherents" }
|
||||
sp-timestamp = { path = "../../../primitives/timestamp" }
|
||||
substrate-telemetry = { path = "../../telemetry" }
|
||||
keystore = { package = "substrate-keystore", path = "../../keystore" }
|
||||
paint-babe = { path = "../../../paint/babe" }
|
||||
client-api = { package = "substrate-client-api", path = "../../api" }
|
||||
client = { package = "substrate-client", path = "../../" }
|
||||
sr-api = { path = "../../../primitives/sr-api" }
|
||||
|
||||
@@ -58,7 +58,10 @@
|
||||
|
||||
#![forbid(unsafe_code)]
|
||||
#![warn(missing_docs)]
|
||||
pub use babe_primitives::*;
|
||||
pub use babe_primitives::{
|
||||
BabeApi, ConsensusLog, BABE_ENGINE_ID, BabePreDigest, SlotNumber, BabeConfiguration,
|
||||
CompatibleDigestItem,
|
||||
};
|
||||
pub use consensus_common::SyncOracle;
|
||||
use std::{collections::HashMap, sync::Arc, u64, pin::Pin, time::{Instant, Duration}};
|
||||
use babe_primitives;
|
||||
@@ -80,10 +83,8 @@ use consensus_common::{
|
||||
self, BlockImport, Environment, Proposer, BlockCheckParams,
|
||||
ForkChoiceStrategy, BlockImportParams, BlockOrigin, Error as ConsensusError,
|
||||
};
|
||||
use paint_babe::{
|
||||
BabeInherentData,
|
||||
timestamp::{TimestampInherentData, InherentType as TimestampInherent}
|
||||
};
|
||||
use babe_primitives::inherents::BabeInherentData;
|
||||
use sp_timestamp::{TimestampInherentData, InherentType as TimestampInherent};
|
||||
use consensus_common::SelectChain;
|
||||
use consensus_common::import_queue::{Verifier, BasicQueue, CacheKeyId};
|
||||
use client_api::{
|
||||
@@ -771,9 +772,9 @@ fn register_babe_inherent_data_provider(
|
||||
slot_duration: u64,
|
||||
) -> Result<(), consensus_common::Error> {
|
||||
debug!(target: "babe", "Registering");
|
||||
if !inherent_data_providers.has_provider(&paint_babe::INHERENT_IDENTIFIER) {
|
||||
if !inherent_data_providers.has_provider(&babe_primitives::inherents::INHERENT_IDENTIFIER) {
|
||||
inherent_data_providers
|
||||
.register_provider(paint_babe::InherentDataProvider::new(slot_duration))
|
||||
.register_provider(babe_primitives::inherents::InherentDataProvider::new(slot_duration))
|
||||
.map_err(Into::into)
|
||||
.map_err(consensus_common::Error::InherentData)
|
||||
} else {
|
||||
|
||||
@@ -11,10 +11,10 @@ primitives = { package = "substrate-primitives", path = "../../../primitives/cor
|
||||
sr-primitives = { path = "../../../primitives/sr-primitives" }
|
||||
client-api = { package = "substrate-client-api", path = "../../api" }
|
||||
block-builder-api = { package = "substrate-block-builder-runtime-api", path = "../../../primitives/block-builder/runtime-api" }
|
||||
paint-timestamp = { path = "../../../paint/timestamp" }
|
||||
inherents = { package = "substrate-inherents", path = "../../../primitives/inherents" }
|
||||
pow-primitives = { package = "substrate-consensus-pow-primitives", path = "../../../primitives/consensus/pow" }
|
||||
consensus-common = { package = "substrate-consensus-common", path = "../../../primitives/consensus/common" }
|
||||
log = "0.4.8"
|
||||
futures-preview = { version = "0.3.0-alpha.19", features = ["compat"] }
|
||||
sp-timestamp = { path = "../../../primitives/timestamp" }
|
||||
derive_more = "0.15.0"
|
||||
|
||||
@@ -40,7 +40,7 @@ use block_builder_api::BlockBuilder as BlockBuilderApi;
|
||||
use sr_primitives::{Justification, RuntimeString};
|
||||
use sr_primitives::generic::{BlockId, Digest, DigestItem};
|
||||
use sr_primitives::traits::{Block as BlockT, Header as HeaderT, ProvideRuntimeApi};
|
||||
use paint_timestamp::{TimestampInherentData, InherentError as TIError};
|
||||
use sp_timestamp::{TimestampInherentData, InherentError as TIError};
|
||||
use pow_primitives::{Seal, TotalDifficulty, POW_ENGINE_ID};
|
||||
use primitives::H256;
|
||||
use inherents::{InherentDataProviders, InherentData};
|
||||
@@ -316,9 +316,9 @@ impl<B: BlockT<Hash=H256>, C, S, Algorithm> Verifier<B> for PowVerifier<B, C, S,
|
||||
pub fn register_pow_inherent_data_provider(
|
||||
inherent_data_providers: &InherentDataProviders,
|
||||
) -> Result<(), consensus_common::Error> {
|
||||
if !inherent_data_providers.has_provider(&paint_timestamp::INHERENT_IDENTIFIER) {
|
||||
if !inherent_data_providers.has_provider(&sp_timestamp::INHERENT_IDENTIFIER) {
|
||||
inherent_data_providers
|
||||
.register_provider(paint_timestamp::InherentDataProvider)
|
||||
.register_provider(sp_timestamp::InherentDataProvider)
|
||||
.map_err(Into::into)
|
||||
.map_err(consensus_common::Error::InherentData)
|
||||
} else {
|
||||
|
||||
@@ -9,7 +9,7 @@ edition = "2018"
|
||||
client-api = { package = "substrate-client-api", path = "../../api" }
|
||||
primitives = { package = "substrate-primitives", path = "../../../primitives/core" }
|
||||
sr-primitives = { path = "../../../primitives/sr-primitives" }
|
||||
paint-authorship = { path = "../../../paint/authorship" }
|
||||
sp-authorship = { path = "../../../primitives/authorship" }
|
||||
consensus_common = { package = "substrate-consensus-common", path = "../../../primitives/consensus/common" }
|
||||
inherents = { package = "substrate-inherents", path = "../../../primitives/inherents" }
|
||||
log = "0.4.8"
|
||||
|
||||
@@ -25,6 +25,7 @@ use log::warn;
|
||||
use client_api::ProvideUncles;
|
||||
use sr_primitives::traits::{Block as BlockT, Header};
|
||||
use std::sync::Arc;
|
||||
use sp_authorship;
|
||||
|
||||
/// Maximum uncles generations we may provide to the runtime.
|
||||
const MAX_UNCLE_GENERATIONS: u32 = 8;
|
||||
@@ -39,9 +40,9 @@ pub fn register_uncles_inherent_data_provider<B, C, SC>(
|
||||
C: ProvideUncles<B> + Send + Sync + 'static,
|
||||
SC: SelectChain<B> + 'static,
|
||||
{
|
||||
if !inherent_data_providers.has_provider(&paint_authorship::INHERENT_IDENTIFIER) {
|
||||
if !inherent_data_providers.has_provider(&sp_authorship::INHERENT_IDENTIFIER) {
|
||||
inherent_data_providers
|
||||
.register_provider(paint_authorship::InherentDataProvider::new(move || {
|
||||
.register_provider(sp_authorship::InherentDataProvider::new(move || {
|
||||
{
|
||||
let chain_head = match select_chain.best_chain() {
|
||||
Ok(x) => x,
|
||||
|
||||
@@ -25,7 +25,7 @@ client = { package = "substrate-client", path = "../" }
|
||||
header-metadata = { package = "substrate-header-metadata", path = "../header-metadata" }
|
||||
inherents = { package = "substrate-inherents", path = "../../primitives/inherents" }
|
||||
network = { package = "substrate-network", path = "../network" }
|
||||
paint-finality-tracker = { path = "../../paint/finality-tracker" }
|
||||
sp-finality-tracker = { path = "../../primitives/finality-tracker" }
|
||||
fg_primitives = { package = "substrate-finality-grandpa-primitives", path = "../../primitives/finality-grandpa" }
|
||||
grandpa = { package = "finality-grandpa", version = "0.9.0", features = ["derive-codec"] }
|
||||
|
||||
|
||||
@@ -70,7 +70,7 @@ use primitives::{H256, Blake2Hasher, Pair};
|
||||
use substrate_telemetry::{telemetry, CONSENSUS_INFO, CONSENSUS_DEBUG, CONSENSUS_WARN};
|
||||
use serde_json;
|
||||
|
||||
use paint_finality_tracker;
|
||||
use sp_finality_tracker;
|
||||
|
||||
use grandpa::Error as GrandpaError;
|
||||
use grandpa::{voter, BlockNumberOps, voter_set::VoterSet};
|
||||
@@ -505,9 +505,9 @@ fn register_finality_tracker_inherent_data_provider<B, E, Block: BlockT<Hash=H25
|
||||
E: CallExecutor<Block, Blake2Hasher> + Send + Sync + 'static,
|
||||
RA: Send + Sync + 'static,
|
||||
{
|
||||
if !inherent_data_providers.has_provider(&paint_finality_tracker::INHERENT_IDENTIFIER) {
|
||||
if !inherent_data_providers.has_provider(&sp_finality_tracker::INHERENT_IDENTIFIER) {
|
||||
inherent_data_providers
|
||||
.register_provider(paint_finality_tracker::InherentDataProvider::new(move || {
|
||||
.register_provider(sp_finality_tracker::InherentDataProvider::new(move || {
|
||||
#[allow(deprecated)]
|
||||
{
|
||||
let info = client.info().chain;
|
||||
|
||||
@@ -17,7 +17,9 @@ runtime-io ={ package = "sr-io", path = "../../primitives/sr-io", default-featur
|
||||
support = { package = "paint-support", path = "../support", default-features = false }
|
||||
substrate-consensus-aura-primitives = { path = "../../primitives/consensus/aura", default-features = false}
|
||||
system = { package = "paint-system", path = "../system", default-features = false }
|
||||
timestamp = { package = "paint-timestamp", path = "../timestamp", default-features = false }
|
||||
sp-timestamp = { package = "sp-timestamp", path = "../../primitives/timestamp", default-features = false }
|
||||
paint-timestamp = { package = "paint-timestamp", path = "../timestamp", default-features = false }
|
||||
|
||||
|
||||
[dev-dependencies]
|
||||
lazy_static = "1.4.0"
|
||||
@@ -37,5 +39,6 @@ std = [
|
||||
"support/std",
|
||||
"substrate-consensus-aura-primitives/std",
|
||||
"system/std",
|
||||
"timestamp/std",
|
||||
"sp-timestamp/std",
|
||||
"paint-timestamp/std",
|
||||
]
|
||||
|
||||
@@ -45,7 +45,7 @@
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
pub use timestamp;
|
||||
use paint_timestamp;
|
||||
|
||||
use rstd::{result, prelude::*};
|
||||
use codec::{Encode, Decode};
|
||||
@@ -57,90 +57,17 @@ use sr_primitives::{
|
||||
RuntimeAppPublic,
|
||||
traits::{SaturatedConversion, Saturating, Zero, Member, IsMember}, generic::DigestItem,
|
||||
};
|
||||
use timestamp::OnTimestampSet;
|
||||
#[cfg(feature = "std")]
|
||||
use timestamp::TimestampInherentData;
|
||||
use sp_timestamp::OnTimestampSet;
|
||||
use inherents::{InherentIdentifier, InherentData, ProvideInherent, MakeFatalError};
|
||||
#[cfg(feature = "std")]
|
||||
use inherents::{InherentDataProviders, ProvideInherentData};
|
||||
use substrate_consensus_aura_primitives::{AURA_ENGINE_ID, ConsensusLog, AuthorityIndex};
|
||||
use substrate_consensus_aura_primitives::{
|
||||
AURA_ENGINE_ID, ConsensusLog, AuthorityIndex,
|
||||
inherents::{INHERENT_IDENTIFIER, AuraInherentData},
|
||||
};
|
||||
|
||||
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, inherents::Error>;
|
||||
/// 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, inherents::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::Result<(), inherents::Error> {
|
||||
if !providers.has_provider(×tamp::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<(), inherents::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()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Trait: timestamp::Trait {
|
||||
pub trait Trait: paint_timestamp::Trait {
|
||||
/// The identifier type for an authority.
|
||||
type AuthorityId: Member + Parameter + RuntimeAppPublic + Default;
|
||||
}
|
||||
@@ -249,7 +176,7 @@ impl<T: Trait> Module<T> {
|
||||
pub fn slot_duration() -> T::Moment {
|
||||
// we double the minimum block-period so each author can always propose within
|
||||
// the majority of its slot.
|
||||
<T as timestamp::Trait>::MinimumPeriod::get().saturating_mul(2.into())
|
||||
<T as paint_timestamp::Trait>::MinimumPeriod::get().saturating_mul(2.into())
|
||||
}
|
||||
|
||||
fn on_timestamp_set(now: T::Moment, slot_duration: T::Moment) {
|
||||
@@ -278,7 +205,7 @@ impl<T: Trait> OnTimestampSet<T::Moment> for Module<T> {
|
||||
}
|
||||
|
||||
impl<T: Trait> ProvideInherent for Module<T> {
|
||||
type Call = timestamp::Call<T>;
|
||||
type Call = paint_timestamp::Call<T>;
|
||||
type Error = MakeFatalError<inherents::Error>;
|
||||
const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER;
|
||||
|
||||
@@ -289,7 +216,7 @@ impl<T: Trait> ProvideInherent for Module<T> {
|
||||
/// Verify the validity of the inherent using the timestamp.
|
||||
fn check_inherent(call: &Self::Call, data: &InherentData) -> result::Result<(), Self::Error> {
|
||||
let timestamp = match call {
|
||||
timestamp::Call::set(ref timestamp) => timestamp.clone(),
|
||||
paint_timestamp::Call::set(ref timestamp) => timestamp.clone(),
|
||||
_ => return Ok(()),
|
||||
};
|
||||
|
||||
|
||||
@@ -62,7 +62,7 @@ impl system::Trait for Test {
|
||||
type Version = ();
|
||||
}
|
||||
|
||||
impl timestamp::Trait for Test {
|
||||
impl paint_timestamp::Trait for Test {
|
||||
type Moment = u64;
|
||||
type OnTimestampSet = Aura;
|
||||
type MinimumPeriod = MinimumPeriod;
|
||||
|
||||
@@ -9,6 +9,7 @@ edition = "2018"
|
||||
primitives = { package = "substrate-primitives", path = "../../primitives/core", default-features = false }
|
||||
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] }
|
||||
inherents = { package = "substrate-inherents", path = "../../primitives/inherents", default-features = false }
|
||||
sp-authorship = { path = "../../primitives/authorship", default-features = false }
|
||||
rstd = { package = "sr-std", path = "../../primitives/sr-std", default-features = false }
|
||||
sr-primitives = { path = "../../primitives/sr-primitives", default-features = false }
|
||||
support = { package = "paint-support", path = "../support", default-features = false }
|
||||
@@ -27,4 +28,5 @@ std = [
|
||||
"support/std",
|
||||
"system/std",
|
||||
"runtime-io/std",
|
||||
"sp-authorship/std",
|
||||
]
|
||||
|
||||
@@ -30,57 +30,9 @@ use system::ensure_none;
|
||||
use sr_primitives::traits::{Header as HeaderT, One, Zero};
|
||||
use sr_primitives::weights::SimpleDispatchInfo;
|
||||
use inherents::{InherentIdentifier, ProvideInherent, InherentData, MakeFatalError};
|
||||
|
||||
/// 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>, inherents::Error>;
|
||||
}
|
||||
|
||||
impl<H: Decode> UnclesInherentData<H> for InherentData {
|
||||
fn uncles(&self) -> Result<Vec<H>, inherents::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> 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<(), inherents::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"))
|
||||
}
|
||||
}
|
||||
use sp_authorship::{
|
||||
INHERENT_IDENTIFIER, UnclesInherentData,
|
||||
};
|
||||
|
||||
pub trait Trait: system::Trait {
|
||||
/// Find the author of a block.
|
||||
|
||||
@@ -15,6 +15,7 @@ sr-staking-primitives = { path = "../../primitives/sr-staking-primitives", defau
|
||||
support = { package = "paint-support", path = "../support", default-features = false }
|
||||
system = { package = "paint-system", path = "../system", default-features = false }
|
||||
timestamp = { package = "paint-timestamp", path = "../timestamp", default-features = false }
|
||||
sp-timestamp = { path = "../../primitives/timestamp", default-features = false }
|
||||
session = { package = "paint-session", path = "../session", default-features = false }
|
||||
babe-primitives = { package = "substrate-consensus-babe-primitives", path = "../../primitives/consensus/babe", default-features = false }
|
||||
runtime-io ={ package = "sr-io", path = "../../primitives/sr-io", default-features = false }
|
||||
@@ -37,6 +38,7 @@ std = [
|
||||
"sr-staking-primitives/std",
|
||||
"system/std",
|
||||
"timestamp/std",
|
||||
"sp-timestamp/std",
|
||||
"inherents/std",
|
||||
"babe-primitives/std",
|
||||
"session/std",
|
||||
|
||||
@@ -21,25 +21,23 @@
|
||||
#![forbid(unused_must_use, unsafe_code, unused_variables, unused_must_use)]
|
||||
#![deny(unused_imports)]
|
||||
pub use timestamp;
|
||||
use sp_timestamp;
|
||||
|
||||
use rstd::{result, prelude::*};
|
||||
use support::{decl_storage, decl_module, traits::FindAuthor, traits::Get};
|
||||
use timestamp::OnTimestampSet;
|
||||
use sp_timestamp::OnTimestampSet;
|
||||
use sr_primitives::{generic::DigestItem, ConsensusEngineId, Perbill};
|
||||
use sr_primitives::traits::{IsMember, SaturatedConversion, Saturating, RandomnessBeacon};
|
||||
use sr_staking_primitives::{
|
||||
SessionIndex,
|
||||
offence::{Offence, Kind},
|
||||
};
|
||||
#[cfg(feature = "std")]
|
||||
use timestamp::TimestampInherentData;
|
||||
|
||||
use codec::{Encode, Decode};
|
||||
use inherents::{InherentIdentifier, InherentData, ProvideInherent, MakeFatalError};
|
||||
#[cfg(feature = "std")]
|
||||
use inherents::{InherentDataProviders, ProvideInherentData};
|
||||
use babe_primitives::{
|
||||
BABE_ENGINE_ID, ConsensusLog, BabeAuthorityWeight, NextEpochDescriptor, RawBabePreDigest,
|
||||
SlotNumber,
|
||||
SlotNumber, inherents::{INHERENT_IDENTIFIER, BabeInherentData}
|
||||
};
|
||||
pub use babe_primitives::{AuthorityId, VRF_OUTPUT_LENGTH, PUBLIC_KEY_LENGTH};
|
||||
|
||||
@@ -49,78 +47,6 @@ mod tests;
|
||||
#[cfg(all(feature = "std", test))]
|
||||
mod mock;
|
||||
|
||||
/// 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::Result<InherentType, inherents::Error>;
|
||||
/// Replace BABE inherent data.
|
||||
fn babe_replace_inherent_data(&mut self, new: InherentType);
|
||||
}
|
||||
|
||||
impl BabeInherentData for InherentData {
|
||||
fn babe_inherent_data(&self) -> result::Result<InherentType, inherents::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::Result<(), inherents::Error> {
|
||||
if !providers.has_provider(×tamp::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<(), inherents::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> {
|
||||
inherents::Error::decode(&mut &error[..]).map(|e| e.into_string()).ok()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Trait: timestamp::Trait {
|
||||
/// The amount of time, in slots, that each epoch should last.
|
||||
type EpochDuration: Get<SlotNumber>;
|
||||
|
||||
@@ -10,6 +10,7 @@ codec = { package = "parity-scale-codec", version = "1.0.0", default-features =
|
||||
inherents = { package = "substrate-inherents", path = "../../primitives/inherents", default-features = false }
|
||||
rstd = { package = "sr-std", path = "../../primitives/sr-std", default-features = false }
|
||||
sr-primitives = { path = "../../primitives/sr-primitives", default-features = false }
|
||||
sp-finality-tracker = { path = "../../primitives/finality-tracker", default-features = false }
|
||||
support = { package = "paint-support", path = "../support", default-features = false }
|
||||
paint-system = { path = "../system", default-features = false }
|
||||
impl-trait-for-tuples = "0.1.3"
|
||||
@@ -27,5 +28,6 @@ std = [
|
||||
"support/std",
|
||||
"sr-primitives/std",
|
||||
"paint-system/std",
|
||||
"sp-finality-tracker/std",
|
||||
"inherents/std",
|
||||
]
|
||||
|
||||
@@ -21,64 +21,10 @@
|
||||
use inherents::{InherentIdentifier, ProvideInherent, InherentData, MakeFatalError};
|
||||
use sr_primitives::traits::{One, Zero, SaturatedConversion};
|
||||
use rstd::{prelude::*, result, cmp, vec};
|
||||
use codec::Decode;
|
||||
use support::{decl_module, decl_storage};
|
||||
use support::traits::Get;
|
||||
use paint_system::{ensure_none, Trait as SystemTrait};
|
||||
|
||||
#[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, inherents::Error>;
|
||||
}
|
||||
|
||||
impl<N: Decode> FinalizedInherentData<N> for InherentData {
|
||||
fn finalized_number(&self) -> Result<N, inherents::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, inherents::Error>
|
||||
{
|
||||
fn inherent_identifier(&self) -> &'static InherentIdentifier {
|
||||
&INHERENT_IDENTIFIER
|
||||
}
|
||||
|
||||
fn provide_inherent_data(
|
||||
&self,
|
||||
inherent_data: &mut InherentData,
|
||||
) -> Result<(), inherents::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"))
|
||||
}
|
||||
}
|
||||
use sp_finality_tracker::{INHERENT_IDENTIFIER, FinalizedInherentData};
|
||||
|
||||
pub const DEFAULT_WINDOW_SIZE: u32 = 101;
|
||||
pub const DEFAULT_REPORT_LATENCY: u32 = 1000;
|
||||
|
||||
@@ -12,6 +12,7 @@ sr-primitives = { path = "../../primitives/sr-primitives", default-features = fa
|
||||
inherents = { package = "substrate-inherents", path = "../../primitives/inherents", default-features = false }
|
||||
support = { package = "paint-support", path = "../support", default-features = false }
|
||||
system = { package = "paint-system", path = "../system", default-features = false }
|
||||
sp-timestamp = { path = "../../primitives/timestamp", default-features = false }
|
||||
impl-trait-for-tuples = "0.1.3"
|
||||
|
||||
[dev-dependencies]
|
||||
@@ -28,4 +29,5 @@ std = [
|
||||
"support/std",
|
||||
"serde",
|
||||
"system/std",
|
||||
"sp-timestamp/std"
|
||||
]
|
||||
|
||||
@@ -91,11 +91,7 @@
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
use rstd::{result, cmp};
|
||||
use codec::Encode;
|
||||
#[cfg(feature = "std")]
|
||||
use codec::Decode;
|
||||
#[cfg(feature = "std")]
|
||||
use inherents::ProvideInherentData;
|
||||
use inherents::{ProvideInherent, InherentData, InherentIdentifier};
|
||||
use support::{Parameter, decl_storage, decl_module};
|
||||
use support::traits::{Time, Get};
|
||||
use sr_primitives::{
|
||||
@@ -106,93 +102,10 @@ use sr_primitives::{
|
||||
};
|
||||
use sr_primitives::weights::SimpleDispatchInfo;
|
||||
use system::ensure_none;
|
||||
use inherents::{InherentIdentifier, ProvideInherent, IsFatalError, InherentData};
|
||||
|
||||
/// 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);
|
||||
}
|
||||
use sp_timestamp::{
|
||||
InherentError, INHERENT_IDENTIFIER, InherentType,
|
||||
OnTimestampSet,
|
||||
};
|
||||
|
||||
/// The module configuration trait
|
||||
pub trait Trait: system::Trait {
|
||||
|
||||
@@ -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