diff --git a/substrate/core/consensus/aura/src/lib.rs b/substrate/core/consensus/aura/src/lib.rs index 964f7231b1..485d4c78d0 100644 --- a/substrate/core/consensus/aura/src/lib.rs +++ b/substrate/core/consensus/aura/src/lib.rs @@ -764,7 +764,7 @@ mod tests { } } - const SLOT_DURATION: u64 = 1; + const SLOT_DURATION: u64 = 1000; pub struct AuraTestNet { peers: Vec>, diff --git a/substrate/core/consensus/slots/src/slots.rs b/substrate/core/consensus/slots/src/slots.rs index 4e51cf0d84..98310bbf2e 100644 --- a/substrate/core/consensus/slots/src/slots.rs +++ b/substrate/core/consensus/slots/src/slots.rs @@ -57,15 +57,14 @@ impl SignedDuration { duration_now() + self.offset } else { duration_now() - self.offset - }.as_secs()) / slot_duration + }.as_millis() as u64) / slot_duration } } /// Returns the duration until the next slot, based on current duration since pub fn time_until_next(now: Duration, slot_duration: u64) -> Duration { - let remaining_full_secs = slot_duration - (now.as_secs() % slot_duration) - 1; - let remaining_nanos = 1_000_000_000 - now.subsec_nanos(); - Duration::new(remaining_full_secs, remaining_nanos) + let remaining_full_millis = slot_duration - (now.as_millis() as u64 % slot_duration) - 1; + Duration::from_millis(remaining_full_millis) } /// Information about a slot. @@ -89,7 +88,7 @@ impl SlotInfo { if now < self.ends_at { self.ends_at.duration_since(now) } else { - Duration::from_secs(0) + Duration::from_millis(0) } } } @@ -156,7 +155,7 @@ impl Stream for Slots { }; // reschedule delay for next slot. let ends_in = offset + - time_until_next(Duration::from_secs(timestamp), slot_duration); + time_until_next(Duration::from_millis(timestamp), slot_duration); let ends_at = Instant::now() + ends_in; self.inner_delay = Some(Delay::new(ends_in)); diff --git a/substrate/core/test-runtime/src/lib.rs b/substrate/core/test-runtime/src/lib.rs index cf3ac363c0..d4c1620863 100644 --- a/substrate/core/test-runtime/src/lib.rs +++ b/substrate/core/test-runtime/src/lib.rs @@ -354,7 +354,7 @@ impl srml_system::Trait for Runtime { } impl srml_timestamp::Trait for Runtime { - /// A timestamp: seconds since the unix epoch. + /// A timestamp: milliseconds since the unix epoch. type Moment = u64; type OnTimestampSet = (); type MinimumPeriod = MinimumPeriod; @@ -540,7 +540,7 @@ cfg_if! { } impl aura_primitives::AuraApi for Runtime { - fn slot_duration() -> u64 { 1 } + fn slot_duration() -> u64 { 1000 } fn authorities() -> Vec { system::authorities() } } @@ -548,7 +548,7 @@ cfg_if! { fn startup_data() -> babe_primitives::BabeConfiguration { babe_primitives::BabeConfiguration { median_required_blocks: 0, - slot_duration: 3, + slot_duration: 3000, c: (3, 10), } } @@ -731,7 +731,7 @@ cfg_if! { } impl aura_primitives::AuraApi for Runtime { - fn slot_duration() -> u64 { 1 } + fn slot_duration() -> u64 { 1000 } fn authorities() -> Vec { system::authorities() } } @@ -739,7 +739,7 @@ cfg_if! { fn startup_data() -> babe_primitives::BabeConfiguration { babe_primitives::BabeConfiguration { median_required_blocks: 0, - slot_duration: 1, + slot_duration: 1000, c: (3, 10), } } diff --git a/substrate/node-template/runtime/src/lib.rs b/substrate/node-template/runtime/src/lib.rs index eedcda4d14..7b2201c8ef 100644 --- a/substrate/node-template/runtime/src/lib.rs +++ b/substrate/node-template/runtime/src/lib.rs @@ -150,10 +150,10 @@ impl indices::Trait for Runtime { } parameter_types! { - pub const MinimumPeriod: u64 = 5; + pub const MinimumPeriod: u64 = 5000; } impl timestamp::Trait for Runtime { - /// A timestamp: seconds since the unix epoch. + /// A timestamp: milliseconds since the unix epoch. type Moment = u64; type OnTimestampSet = Aura; type MinimumPeriod = MinimumPeriod; diff --git a/substrate/node/cli/src/service.rs b/substrate/node/cli/src/service.rs index 29af557a3f..57d986cb05 100644 --- a/substrate/node/cli/src/service.rs +++ b/substrate/node/cli/src/service.rs @@ -264,7 +264,7 @@ mod tests { use consensus_common::{Environment, Proposer, BlockImportParams, BlockOrigin, ForkChoiceStrategy}; use node_primitives::DigestItem; use node_runtime::{BalancesCall, Call, UncheckedExtrinsic}; - use node_runtime::constants::{currency::CENTS, time::SECS_PER_BLOCK}; + use node_runtime::constants::{currency::CENTS, time::SLOT_DURATION}; use parity_codec::{Encode, Decode}; use primitives::{ crypto::Pair as CryptoPair, blake2_256, @@ -365,7 +365,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 * SECS_PER_BLOCK)); + inherent_data.replace_data(timestamp::INHERENT_IDENTIFIER, &(slot_num * SLOT_DURATION)); if let Some(babe_pre_digest) = babe::test_helpers::claim_slot( &*service.client(), &parent_id, diff --git a/substrate/node/executor/src/lib.rs b/substrate/node/executor/src/lib.rs index f858f4e2a8..93e31ef6fc 100644 --- a/substrate/node/executor/src/lib.rs +++ b/substrate/node/executor/src/lib.rs @@ -463,7 +463,7 @@ mod tests { vec![ CheckedExtrinsic { signed: None, - function: Call::Timestamp(timestamp::Call::set(42)), + function: Call::Timestamp(timestamp::Call::set(42 * 1000)), }, CheckedExtrinsic { signed: Some((alice(), signed_extra(0, 0))), @@ -485,7 +485,7 @@ mod tests { vec![ CheckedExtrinsic { signed: None, - function: Call::Timestamp(timestamp::Call::set(42)), + function: Call::Timestamp(timestamp::Call::set(42 * 1000)), }, CheckedExtrinsic { signed: Some((alice(), signed_extra(0, 0))), @@ -500,7 +500,7 @@ mod tests { vec![ CheckedExtrinsic { signed: None, - function: Call::Timestamp(timestamp::Call::set(52)), + function: Call::Timestamp(timestamp::Call::set(52 * 1000)), }, CheckedExtrinsic { signed: Some((bob(), signed_extra(0, 0))), @@ -528,7 +528,7 @@ mod tests { vec![ CheckedExtrinsic { signed: None, - function: Call::Timestamp(timestamp::Call::set(time)), + function: Call::Timestamp(timestamp::Call::set(time * 1000)), }, CheckedExtrinsic { signed: Some((alice(), signed_extra(nonce, 0))), @@ -784,7 +784,7 @@ mod tests { vec![ CheckedExtrinsic { signed: None, - function: Call::Timestamp(timestamp::Call::set(42)), + function: Call::Timestamp(timestamp::Call::set(42 * 1000)), }, CheckedExtrinsic { signed: Some((charlie(), signed_extra(0, 0))), @@ -983,7 +983,7 @@ mod tests { vec![ CheckedExtrinsic { signed: None, - function: Call::Timestamp(timestamp::Call::set(42)), + function: Call::Timestamp(timestamp::Call::set(42 * 1000)), }, CheckedExtrinsic { signed: Some((charlie(), signed_extra(0, 0))), @@ -1000,7 +1000,7 @@ mod tests { vec![ CheckedExtrinsic { signed: None, - function: Call::Timestamp(timestamp::Call::set(52)), + function: Call::Timestamp(timestamp::Call::set(52 * 1000)), }, CheckedExtrinsic { signed: Some((charlie(), signed_extra(1, 0))), @@ -1147,7 +1147,7 @@ mod tests { xts.insert(0, CheckedExtrinsic { signed: None, - function: Call::Timestamp(timestamp::Call::set(time)), + function: Call::Timestamp(timestamp::Call::set(time * 1000)), }); // NOTE: this is super slow. Can probably be improved. @@ -1213,7 +1213,7 @@ mod tests { vec![ CheckedExtrinsic { signed: None, - function: Call::Timestamp(timestamp::Call::set(time)), + function: Call::Timestamp(timestamp::Call::set(time * 1000)), }, CheckedExtrinsic { signed: Some((charlie(), signed_extra(nonce, 0))), diff --git a/substrate/node/primitives/src/lib.rs b/substrate/node/primitives/src/lib.rs index c8a0b1a25e..96085cf60f 100644 --- a/substrate/node/primitives/src/lib.rs +++ b/substrate/node/primitives/src/lib.rs @@ -50,7 +50,9 @@ pub type Index = u64; /// A hash of some data used by the chain. pub type Hash = primitives::H256; -/// A timestamp: seconds since the unix epoch. +/// A timestamp: milliseconds since the unix epoch. +/// `u64` is enough to represent a duration of half a billion years, when the +/// time scale is milliseconds. pub type Timestamp = u64; /// Digest item type. diff --git a/substrate/node/runtime/src/constants.rs b/substrate/node/runtime/src/constants.rs index c030f57121..c32709adf6 100644 --- a/substrate/node/runtime/src/constants.rs +++ b/substrate/node/runtime/src/constants.rs @@ -18,21 +18,25 @@ /// Money matters. pub mod currency { - use node_primitives::Balance; + use node_primitives::Balance; - pub const MILLICENTS: Balance = 1_000_000_000; - pub const CENTS: Balance = 1_000 * MILLICENTS; // assume this is worth about a cent. - pub const DOLLARS: Balance = 100 * CENTS; + pub const MILLICENTS: Balance = 1_000_000_000; + pub const CENTS: Balance = 1_000 * MILLICENTS; // assume this is worth about a cent. + pub const DOLLARS: Balance = 100 * CENTS; } /// Time. pub mod time { - use node_primitives::Moment; + use node_primitives::Moment; - pub const SECS_PER_BLOCK: Moment = 6; - pub const MINUTES: Moment = 60 / SECS_PER_BLOCK; - pub const HOURS: Moment = MINUTES * 60; - pub const DAYS: Moment = HOURS * 24; + pub const MILLISECS_PER_BLOCK: Moment = 6000; + pub const SECS_PER_BLOCK: Moment = MILLISECS_PER_BLOCK / 1000; + pub const SLOT_DURATION: Moment = 6000; + + // These time units are defined in number of blocks. + pub const MINUTES: Moment = 60 / SECS_PER_BLOCK; + pub const HOURS: Moment = MINUTES * 60; + pub const DAYS: Moment = HOURS * 24; } // CRITICAL NOTE: The system module maintains two constants: a _maximum_ block weight and a @@ -45,8 +49,8 @@ pub mod time { // the ratio that `system` module uses to find normal transaction quota. /// Fee-related. pub mod fee { - pub use runtime_primitives::Perbill; + pub use runtime_primitives::Perbill; - /// The block saturation level. Fees will be updates based on this value. - pub const TARGET_BLOCK_FULLNESS: Perbill = Perbill::from_percent(25); -} \ No newline at end of file + /// The block saturation level. Fees will be updates based on this value. + pub const TARGET_BLOCK_FULLNESS: Perbill = Perbill::from_percent(25); +} diff --git a/substrate/node/runtime/src/lib.rs b/substrate/node/runtime/src/lib.rs index 9ea7bf3c9e..02d2a91851 100644 --- a/substrate/node/runtime/src/lib.rs +++ b/substrate/node/runtime/src/lib.rs @@ -79,7 +79,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // and set impl_version to equal spec_version. If only runtime // implementation changes and behavior does not, then leave spec_version as // is and increment impl_version. - spec_version: 121, + spec_version: 122, impl_version: 122, apis: RUNTIME_API_VERSIONS, }; @@ -166,7 +166,7 @@ impl balances::Trait for Runtime { } parameter_types! { - pub const MinimumPeriod: u64 = SECS_PER_BLOCK / 2; + pub const MinimumPeriod: Moment = SLOT_DURATION / 2; } impl timestamp::Trait for Runtime { type Moment = Moment; diff --git a/substrate/srml/timestamp/src/lib.rs b/substrate/srml/timestamp/src/lib.rs index 030f825dba..46ffb6ed22 100644 --- a/substrate/srml/timestamp/src/lib.rs +++ b/substrate/srml/timestamp/src/lib.rs @@ -170,7 +170,7 @@ impl ProvideInherentData for InherentDataProvider { .map_err(|_| { "Current time is before unix epoch".into() }).and_then(|d| { - let duration: InherentType = d.as_secs(); + let duration: InherentType = d.as_millis() as u64; inherent_data.put_data(INHERENT_IDENTIFIER, &duration) }) }