From e368b2d1582a1394c6d8ac1a2574963aa3500390 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi <shawntabrizi@gmail.com> Date: Wed, 3 Oct 2018 18:32:43 +0100 Subject: [PATCH] Updating Moment to support Mul/Div with BlockNumber (#868) * Updating Moment to support Mul/Div with BlockNumber Issue #261 * Fixed whitespace and commented code --- substrate/srml/session/src/lib.rs | 9 +++++---- substrate/srml/timestamp/src/lib.rs | 8 ++++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/substrate/srml/session/src/lib.rs b/substrate/srml/session/src/lib.rs index 4e2103a3dc2..07d83507726 100644 --- a/substrate/srml/session/src/lib.rs +++ b/substrate/srml/session/src/lib.rs @@ -43,10 +43,11 @@ extern crate srml_system as system; extern crate srml_timestamp as timestamp; use rstd::prelude::*; -use primitives::traits::{Zero, One, OnFinalise, Convert, As}; +use primitives::traits::{Zero, One, OnFinalise, Convert}; use runtime_support::{StorageValue, StorageMap}; use runtime_support::dispatch::Result; use system::ensure_signed; +use rstd::ops::Mul; /// A session has changed. pub trait OnSessionChange<T> { @@ -211,9 +212,9 @@ impl<T: Trait> Module<T> { /// Get the time that should have elapsed over a session if everything was working perfectly. pub fn ideal_session_duration() -> T::Moment { - let block_period = <timestamp::Module<T>>::block_period(); - let session_length = <T::Moment as As<T::BlockNumber>>::sa(Self::length()); - session_length * block_period + let block_period: T::Moment = <timestamp::Module<T>>::block_period(); + let session_length: T::BlockNumber = Self::length(); + Mul::<T::BlockNumber>::mul(block_period, session_length) } /// Number of blocks remaining in this session, not counting this one. If the session is diff --git a/substrate/srml/timestamp/src/lib.rs b/substrate/srml/timestamp/src/lib.rs index 38107025a46..fc602d4fa66 100644 --- a/substrate/srml/timestamp/src/lib.rs +++ b/substrate/srml/timestamp/src/lib.rs @@ -54,15 +54,19 @@ extern crate parity_codec as codec; use runtime_support::{StorageValue, Parameter}; use runtime_support::dispatch::Result; -use runtime_primitives::traits::{OnFinalise, SimpleArithmetic, As, Zero}; +use runtime_primitives::traits::{OnFinalise, SimpleArithmetic, Zero}; use system::ensure_inherent; +use rstd::ops::{Mul, Div}; + +#[cfg(any(feature = "std", test))] +use runtime_primitives::traits::As; pub trait Trait: consensus::Trait + system::Trait { /// The position of the required timestamp-set extrinsic. const TIMESTAMP_SET_POSITION: u32; /// Type used for expressing timestamp. - type Moment: Parameter + Default + SimpleArithmetic + As<Self::BlockNumber>; + type Moment: Parameter + Default + SimpleArithmetic + Mul<Self::BlockNumber, Output = Self::Moment> + Div<Self::BlockNumber, Output = Self::Moment>; } decl_module! { -- GitLab