diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index 35e22875a9..0e28bd5d36 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -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", diff --git a/substrate/Cargo.toml b/substrate/Cargo.toml index f35cab8014..5914227962 100644 --- a/substrate/Cargo.toml +++ b/substrate/Cargo.toml @@ -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", diff --git a/substrate/bin/node/cli/Cargo.toml b/substrate/bin/node/cli/Cargo.toml index ba7568f571..0c4a9577d1 100644 --- a/substrate/bin/node/cli/Cargo.toml +++ b/substrate/bin/node/cli/Cargo.toml @@ -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" } diff --git a/substrate/bin/node/cli/src/factory_impl.rs b/substrate/bin/node/cli/src/factory_impl.rs index aaf74a0602..b47710b733 100644 --- a/substrate/bin/node/cli/src/factory_impl.rs +++ b/substrate/bin/node/cli/src/factory_impl.rs @@ -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 = ::Signer; @@ -160,9 +160,9 @@ impl RuntimeAdapter for FactoryState { 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 } diff --git a/substrate/bin/node/cli/src/service.rs b/substrate/bin/node/cli/src/service.rs index f4c0ad69f1..c627e7774f 100644 --- a/substrate/bin/node/cli/src/service.rs +++ b/substrate/bin/node/cli/src/service.rs @@ -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, diff --git a/substrate/client/consensus/aura/Cargo.toml b/substrate/client/consensus/aura/Cargo.toml index 6fc90032c4..12998608e9 100644 --- a/substrate/client/consensus/aura/Cargo.toml +++ b/substrate/client/consensus/aura/Cargo.toml @@ -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] diff --git a/substrate/client/consensus/aura/src/lib.rs b/substrate/client/consensus/aura/src/lib.rs index a6b9c11bc3..7b396a95c3 100644 --- a/substrate/client/consensus/aura/src/lib.rs +++ b/substrate/client/consensus/aura/src/lib.rs @@ -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 { diff --git a/substrate/client/consensus/babe/Cargo.toml b/substrate/client/consensus/babe/Cargo.toml index a4a92f04f0..3a903f1228 100644 --- a/substrate/client/consensus/babe/Cargo.toml +++ b/substrate/client/consensus/babe/Cargo.toml @@ -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" } diff --git a/substrate/client/consensus/babe/src/lib.rs b/substrate/client/consensus/babe/src/lib.rs index dbfd3dbfa4..5d327bcedf 100644 --- a/substrate/client/consensus/babe/src/lib.rs +++ b/substrate/client/consensus/babe/src/lib.rs @@ -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 { diff --git a/substrate/client/consensus/pow/Cargo.toml b/substrate/client/consensus/pow/Cargo.toml index 9d510cc6da..01c6d22458 100644 --- a/substrate/client/consensus/pow/Cargo.toml +++ b/substrate/client/consensus/pow/Cargo.toml @@ -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" diff --git a/substrate/client/consensus/pow/src/lib.rs b/substrate/client/consensus/pow/src/lib.rs index 57d05fcc45..cb3f23fbf5 100644 --- a/substrate/client/consensus/pow/src/lib.rs +++ b/substrate/client/consensus/pow/src/lib.rs @@ -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, C, S, Algorithm> Verifier for PowVerifier 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 { diff --git a/substrate/client/consensus/uncles/Cargo.toml b/substrate/client/consensus/uncles/Cargo.toml index 1212449a55..01e81ef198 100644 --- a/substrate/client/consensus/uncles/Cargo.toml +++ b/substrate/client/consensus/uncles/Cargo.toml @@ -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" diff --git a/substrate/client/consensus/uncles/src/lib.rs b/substrate/client/consensus/uncles/src/lib.rs index 63d4f584a3..abbd6d2b65 100644 --- a/substrate/client/consensus/uncles/src/lib.rs +++ b/substrate/client/consensus/uncles/src/lib.rs @@ -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( C: ProvideUncles + Send + Sync + 'static, SC: SelectChain + '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, diff --git a/substrate/client/finality-grandpa/Cargo.toml b/substrate/client/finality-grandpa/Cargo.toml index d8320327a9..95652dfddb 100644 --- a/substrate/client/finality-grandpa/Cargo.toml +++ b/substrate/client/finality-grandpa/Cargo.toml @@ -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"] } diff --git a/substrate/client/finality-grandpa/src/lib.rs b/substrate/client/finality-grandpa/src/lib.rs index 80d2350982..52cd436cf3 100644 --- a/substrate/client/finality-grandpa/src/lib.rs +++ b/substrate/client/finality-grandpa/src/lib.rs @@ -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 + 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; diff --git a/substrate/paint/aura/Cargo.toml b/substrate/paint/aura/Cargo.toml index 23250ac312..7260d0d59f 100644 --- a/substrate/paint/aura/Cargo.toml +++ b/substrate/paint/aura/Cargo.toml @@ -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", ] diff --git a/substrate/paint/aura/src/lib.rs b/substrate/paint/aura/src/lib.rs index 4b14c5b648..0908cb7582 100644 --- a/substrate/paint/aura/src/lib.rs +++ b/substrate/paint/aura/src/lib.rs @@ -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; - /// Replace aura inherent data. - fn aura_replace_inherent_data(&mut self, new: InherentType); -} - -impl AuraInherentData for InherentData { - fn aura_inherent_data(&self) -> result::Result { - 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 { - 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 Module { pub fn slot_duration() -> T::Moment { // we double the minimum block-period so each author can always propose within // the majority of its slot. - ::MinimumPeriod::get().saturating_mul(2.into()) + ::MinimumPeriod::get().saturating_mul(2.into()) } fn on_timestamp_set(now: T::Moment, slot_duration: T::Moment) { @@ -278,7 +205,7 @@ impl OnTimestampSet for Module { } impl ProvideInherent for Module { - type Call = timestamp::Call; + type Call = paint_timestamp::Call; type Error = MakeFatalError; const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER; @@ -289,7 +216,7 @@ impl ProvideInherent for Module { /// 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(()), }; diff --git a/substrate/paint/aura/src/mock.rs b/substrate/paint/aura/src/mock.rs index 5c55e8bdd5..82a16335da 100644 --- a/substrate/paint/aura/src/mock.rs +++ b/substrate/paint/aura/src/mock.rs @@ -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; diff --git a/substrate/paint/authorship/Cargo.toml b/substrate/paint/authorship/Cargo.toml index b53c059c3a..e360ca1e2f 100644 --- a/substrate/paint/authorship/Cargo.toml +++ b/substrate/paint/authorship/Cargo.toml @@ -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", ] diff --git a/substrate/paint/authorship/src/lib.rs b/substrate/paint/authorship/src/lib.rs index 795b0f7940..154d1b6b8e 100644 --- a/substrate/paint/authorship/src/lib.rs +++ b/substrate/paint/authorship/src/lib.rs @@ -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 { - /// Get uncles. - fn uncles(&self) -> Result, inherents::Error>; -} - -impl UnclesInherentData for InherentData { - fn uncles(&self) -> Result, inherents::Error> { - Ok(self.get_data(&INHERENT_IDENTIFIER)?.unwrap_or_default()) - } -} - -/// Provider for inherent data. -#[cfg(feature = "std")] -pub struct InherentDataProvider { - inner: F, - _marker: std::marker::PhantomData, -} - -#[cfg(feature = "std")] -impl InherentDataProvider { - pub fn new(uncles_oracle: F) -> Self { - InherentDataProvider { inner: uncles_oracle, _marker: Default::default() } - } -} - -#[cfg(feature = "std")] -impl inherents::ProvideInherentData for InherentDataProvider -where F: Fn() -> Vec -{ - 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 { - Some(format!("no further information")) - } -} +use sp_authorship::{ + INHERENT_IDENTIFIER, UnclesInherentData, +}; pub trait Trait: system::Trait { /// Find the author of a block. diff --git a/substrate/paint/babe/Cargo.toml b/substrate/paint/babe/Cargo.toml index cdbee72216..549cc44851 100644 --- a/substrate/paint/babe/Cargo.toml +++ b/substrate/paint/babe/Cargo.toml @@ -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", diff --git a/substrate/paint/babe/src/lib.rs b/substrate/paint/babe/src/lib.rs index 5bdae6ce9b..05cbcd0567 100644 --- a/substrate/paint/babe/src/lib.rs +++ b/substrate/paint/babe/src/lib.rs @@ -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; - /// Replace BABE inherent data. - fn babe_replace_inherent_data(&mut self, new: InherentType); -} - -impl BabeInherentData for InherentData { - fn babe_inherent_data(&self) -> result::Result { - 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 { - 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; diff --git a/substrate/paint/finality-tracker/Cargo.toml b/substrate/paint/finality-tracker/Cargo.toml index f11fa2c4d9..ba7e265bea 100644 --- a/substrate/paint/finality-tracker/Cargo.toml +++ b/substrate/paint/finality-tracker/Cargo.toml @@ -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", ] diff --git a/substrate/paint/finality-tracker/src/lib.rs b/substrate/paint/finality-tracker/src/lib.rs index e0721e5a14..1b69c2c870 100644 --- a/substrate/paint/finality-tracker/src/lib.rs +++ b/substrate/paint/finality-tracker/src/lib.rs @@ -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 { - /// Get finalized inherent data. - fn finalized_number(&self) -> Result; -} - -impl FinalizedInherentData for InherentData { - fn finalized_number(&self) -> Result { - 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 { - inner: F, - _marker: std::marker::PhantomData, -} - -#[cfg(feature = "std")] -impl InherentDataProvider { - pub fn new(final_oracle: F) -> Self { - InherentDataProvider { inner: final_oracle, _marker: Default::default() } - } -} - -#[cfg(feature = "std")] -impl inherents::ProvideInherentData for InherentDataProvider - where F: Fn() -> Result -{ - 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 { - 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; diff --git a/substrate/paint/timestamp/Cargo.toml b/substrate/paint/timestamp/Cargo.toml index 73485f96e1..5c9828e36d 100644 --- a/substrate/paint/timestamp/Cargo.toml +++ b/substrate/paint/timestamp/Cargo.toml @@ -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" ] diff --git a/substrate/paint/timestamp/src/lib.rs b/substrate/paint/timestamp/src/lib.rs index d6123e54a9..3aa921626e 100644 --- a/substrate/paint/timestamp/src/lib.rs +++ b/substrate/paint/timestamp/src/lib.rs @@ -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 { - if id == &INHERENT_IDENTIFIER { - ::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; -} - -impl TimestampInherentData for InherentData { - fn timestamp_inherent_data(&self) -> Result { - 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 { - 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 { - fn on_timestamp_set(moment: Moment); -} +use sp_timestamp::{ + InherentError, INHERENT_IDENTIFIER, InherentType, + OnTimestampSet, +}; /// The module configuration trait pub trait Trait: system::Trait { diff --git a/substrate/primitives/authorship/Cargo.toml b/substrate/primitives/authorship/Cargo.toml new file mode 100644 index 0000000000..1c95fed52f --- /dev/null +++ b/substrate/primitives/authorship/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "sp-authorship" +version = "2.0.0" +authors = ["Parity Technologies "] +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", +] diff --git a/substrate/primitives/authorship/src/lib.rs b/substrate/primitives/authorship/src/lib.rs new file mode 100644 index 0000000000..e381c97094 --- /dev/null +++ b/substrate/primitives/authorship/src/lib.rs @@ -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 . + +//! 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 { + /// Get uncles. + fn uncles(&self) -> Result, Error>; +} + +impl UnclesInherentData for InherentData { + fn uncles(&self) -> Result, Error> { + Ok(self.get_data(&INHERENT_IDENTIFIER)?.unwrap_or_default()) + } +} + +/// Provider for inherent data. +#[cfg(feature = "std")] +pub struct InherentDataProvider { + inner: F, + _marker: std::marker::PhantomData, +} + +#[cfg(feature = "std")] +impl InherentDataProvider { + pub fn new(uncles_oracle: F) -> Self { + InherentDataProvider { inner: uncles_oracle, _marker: Default::default() } + } +} + +#[cfg(feature = "std")] +impl sp_inherents::ProvideInherentData for InherentDataProvider +where F: Fn() -> Vec +{ + 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 { + Some(format!("no further information")) + } +} \ No newline at end of file diff --git a/substrate/primitives/consensus/aura/Cargo.toml b/substrate/primitives/consensus/aura/Cargo.toml index 20a98f9f7a..bb772cb7a7 100644 --- a/substrate/primitives/consensus/aura/Cargo.toml +++ b/substrate/primitives/consensus/aura/Cargo.toml @@ -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", ] diff --git a/substrate/primitives/consensus/aura/src/inherents.rs b/substrate/primitives/consensus/aura/src/inherents.rs new file mode 100644 index 0000000000..11a429b817 --- /dev/null +++ b/substrate/primitives/consensus/aura/src/inherents.rs @@ -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 . + +/// 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; + /// Replace aura inherent data. + fn aura_replace_inherent_data(&mut self, new: InherentType); +} + +impl AuraInherentData for InherentData { + fn aura_inherent_data(&self) ->Result { + 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 { + inherents::Error::decode(&mut &error[..]).map(|e| e.into_string()).ok() + } +} diff --git a/substrate/primitives/consensus/aura/src/lib.rs b/substrate/primitives/consensus/aura/src/lib.rs index ccf2d8e5fd..8a89452ac3 100644 --- a/substrate/primitives/consensus/aura/src/lib.rs +++ b/substrate/primitives/consensus/aura/src/lib.rs @@ -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}; diff --git a/substrate/primitives/consensus/babe/Cargo.toml b/substrate/primitives/consensus/babe/Cargo.toml index 2ee36bd6c3..bbdcbf9b66 100644 --- a/substrate/primitives/consensus/babe/Cargo.toml +++ b/substrate/primitives/consensus/babe/Cargo.toml @@ -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", ] diff --git a/substrate/primitives/consensus/babe/src/inherents.rs b/substrate/primitives/consensus/babe/src/inherents.rs new file mode 100644 index 0000000000..0ebefaaaf0 --- /dev/null +++ b/substrate/primitives/consensus/babe/src/inherents.rs @@ -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 . +//! 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; + /// Replace BABE inherent data. + fn babe_replace_inherent_data(&mut self, new: InherentType); +} + +impl BabeInherentData for InherentData { + fn babe_inherent_data(&self) -> Result { + 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 { + Error::decode(&mut &error[..]).map(|e| e.into_string()).ok() + } +} diff --git a/substrate/primitives/consensus/babe/src/lib.rs b/substrate/primitives/consensus/babe/src/lib.rs index 6ebd296961..532aaa6dc1 100644 --- a/substrate/primitives/consensus/babe/src/lib.rs +++ b/substrate/primitives/consensus/babe/src/lib.rs @@ -20,6 +20,7 @@ #![cfg_attr(not(feature = "std"), no_std)] mod digest; +pub mod inherents; use codec::{Encode, Decode}; use rstd::vec::Vec; diff --git a/substrate/primitives/finality-tracker/Cargo.toml b/substrate/primitives/finality-tracker/Cargo.toml new file mode 100644 index 0000000000..2f7ddf916b --- /dev/null +++ b/substrate/primitives/finality-tracker/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "sp-finality-tracker" +version = "2.0.0" +authors = ["Parity Technologies "] +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", +] diff --git a/substrate/primitives/finality-tracker/src/lib.rs b/substrate/primitives/finality-tracker/src/lib.rs new file mode 100644 index 0000000000..7b81a300c4 --- /dev/null +++ b/substrate/primitives/finality-tracker/src/lib.rs @@ -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 . + +//! 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 { + /// Get finalized inherent data. + fn finalized_number(&self) -> Result; +} + +impl FinalizedInherentData for InherentData { + fn finalized_number(&self) -> Result { + 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 { + inner: F, + _marker: std::marker::PhantomData, +} + +#[cfg(feature = "std")] +impl InherentDataProvider { + pub fn new(final_oracle: F) -> Self { + InherentDataProvider { inner: final_oracle, _marker: Default::default() } + } +} + +#[cfg(feature = "std")] +impl inherents::ProvideInherentData for InherentDataProvider + where F: Fn() -> Result +{ + 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 { + Some(format!("no further information")) + } +} diff --git a/substrate/primitives/timestamp/Cargo.toml b/substrate/primitives/timestamp/Cargo.toml new file mode 100644 index 0000000000..013a0340ea --- /dev/null +++ b/substrate/primitives/timestamp/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "sp-timestamp" +version = "2.0.0" +authors = ["Parity Technologies "] +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", +] diff --git a/substrate/primitives/timestamp/src/lib.rs b/substrate/primitives/timestamp/src/lib.rs new file mode 100644 index 0000000000..4afb4f9151 --- /dev/null +++ b/substrate/primitives/timestamp/src/lib.rs @@ -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 . + +//! 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 { + if id == &INHERENT_IDENTIFIER { + ::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; +} + +impl TimestampInherentData for InherentData { + fn timestamp_inherent_data(&self) -> Result { + 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 { + 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 { + fn on_timestamp_set(moment: Moment); +} \ No newline at end of file