Skip to content

Commit

Permalink
num-wrapper implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Jul 11, 2024
1 parent 3fb84f8 commit f18650a
Show file tree
Hide file tree
Showing 9 changed files with 600 additions and 38 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions libs/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ sp-core = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }
cfg-utils = { workspace = true }

# substrate frame dependencies
frame-support = { workspace = true }
Expand Down Expand Up @@ -57,6 +58,7 @@ std = [
"sp-std/std",
"staging-xcm-executor/std",
"staging-xcm/std",
"cfg-utils/std",
]
runtime-benchmarks = [
"frame-support/runtime-benchmarks",
Expand All @@ -65,11 +67,13 @@ runtime-benchmarks = [
"pallet-membership/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"staging-xcm-executor/runtime-benchmarks",
"cfg-utils/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"pallet-collective/try-runtime",
"pallet-membership/try-runtime",
"sp-runtime/try-runtime",
"cfg-utils/try-runtime",
]
32 changes: 19 additions & 13 deletions libs/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,20 @@ pub mod types {

/// The type for outbound LP message nonces.
pub type OutboundMessageNonce = u64;

/// The type to represent milliseconds
pub type Millis = cfg_utils::time::Millis<u64>;

/// The type to represent seconds
pub type Seconds = cfg_utils::time::Seconds<u64>;
}

/// Common constants for all runtimes
pub mod constants {
use frame_support::weights::{constants::WEIGHT_REF_TIME_PER_SECOND, Weight};
use sp_runtime::Perbill;

use super::types::{Balance, BlockNumber};
use super::types::{Balance, BlockNumber, Millis, Seconds};

/// This determines the average expected block time that we are targeting.
/// Blocks will be produced at a minimum duration defined by
Expand All @@ -174,24 +180,24 @@ pub mod constants {
/// slot_duration()`.
///
/// Change this to adjust the block time.
pub const MILLISECS_PER_BLOCK: u64 = 12000;
pub const SLOT_DURATION: u64 = MILLISECS_PER_BLOCK;
pub const MILLISECS_PER_BLOCK: Millis = Millis::from(12000);
pub const SLOT_DURATION: Millis = MILLISECS_PER_BLOCK;

// Time is measured by number of blocks.
pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber);
pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK.inner as BlockNumber);
pub const HOURS: BlockNumber = MINUTES * 60;
pub const DAYS: BlockNumber = HOURS * 24;

/// Milliseconds per day
pub const MILLISECS_PER_DAY: u64 = SECONDS_PER_DAY * 1000;

// Seconds units
pub const SECONDS_PER_MINUTE: u64 = 60;
pub const SECONDS_PER_HOUR: u64 = SECONDS_PER_MINUTE * 60;
pub const SECONDS_PER_DAY: u64 = SECONDS_PER_HOUR * 24;
pub const SECONDS_PER_WEEK: u64 = SECONDS_PER_DAY * 7;
pub const SECONDS_PER_MONTH: u64 = SECONDS_PER_DAY * 30;
pub const SECONDS_PER_YEAR: u64 = SECONDS_PER_DAY * 365;
pub const SECONDS_PER_MINUTE: Seconds = Seconds::from(60);
pub const SECONDS_PER_HOUR: Seconds = SECONDS_PER_MINUTE.mul(60);
pub const SECONDS_PER_DAY: Seconds = SECONDS_PER_HOUR.mul(24);
pub const SECONDS_PER_WEEK: Seconds = SECONDS_PER_DAY.mul(7);
pub const SECONDS_PER_MONTH: Seconds = SECONDS_PER_DAY.mul(30);
pub const SECONDS_PER_YEAR: Seconds = SECONDS_PER_DAY.mul(365);

/// Milliseconds per day
pub const MILLISECS_PER_DAY: Millis = SECONDS_PER_DAY.into_millis();

/// We assume that ~5% of the block weight is consumed by `on_initialize`
/// handlers. This is used to limit the maximal weight of a single
Expand Down
4 changes: 4 additions & 0 deletions libs/traits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ sp-arithmetic = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }
strum = { workspace = true }
cfg-utils = { workspace = true }

[dev-dependencies]
cfg-mocks = { workspace = true, default_features = true }
Expand All @@ -41,15 +42,18 @@ std = [
"scale-info/std",
"strum/std",
"orml-traits/std",
"cfg-utils/std",
]
runtime-benchmarks = [
"cfg-primitives/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"cfg-mocks/runtime-benchmarks",
"cfg-utils/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"cfg-primitives/try-runtime",
"sp-runtime/try-runtime",
"cfg-utils/try-runtime",
]
4 changes: 1 addition & 3 deletions libs/traits/src/interest.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cfg_primitives::SECONDS_PER_YEAR;
use cfg_primitives::{Seconds, SECONDS_PER_YEAR};
use frame_support::{
dispatch::DispatchResult,
pallet_prelude::{RuntimeDebug, TypeInfo},
Expand All @@ -14,8 +14,6 @@ use sp_runtime::{
DispatchError,
};

use crate::Seconds;

#[derive(Encode, Decode, Clone, PartialEq, Eq, TypeInfo, RuntimeDebug, MaxEncodedLen)]
pub enum CompoundingSchedule {
/// Interest compounds every second
Expand Down
26 changes: 4 additions & 22 deletions libs/traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pub mod swaps;
/// Traits related to benchmarking tooling.
pub mod benchmarking;

pub use cfg_utils::time::{IntoMillis, IntoSeconds};

/// A trait that can be used to fetch the nav and update nav for a given pool
pub trait PoolNAV<PoolId, Amount> {
type ClassId;
Expand Down Expand Up @@ -380,35 +382,15 @@ pub trait TryConvert<A, B> {
fn try_convert(a: A) -> Result<B, Self::Error>;
}

// TODO: Probably these should be in a future cfg-utils.
// Issue: https://github.com/centrifuge/centrifuge-chain/issues/1380

/// Type to represent milliseconds
pub type Millis = u64;

/// Type to represent seconds
pub type Seconds = u64;

/// Trait to obtain the time as seconds
pub trait TimeAsSecs: UnixTime {
fn now() -> Seconds {
<Self as UnixTime>::now().as_secs()
fn now() -> cfg_primitives::Seconds {
<Self as UnixTime>::now().as_secs().into()
}
}

impl<T: UnixTime> TimeAsSecs for T {}

/// Trait to convert into seconds
pub trait IntoSeconds {
fn into_seconds(self) -> Seconds;
}

impl IntoSeconds for Millis {
fn into_seconds(self) -> Seconds {
self / 1000
}
}

pub trait ValueProvider<Source, Key> {
type Value;

Expand Down
2 changes: 2 additions & 0 deletions libs/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ sp-arithmetic = { workspace = true }
sp-consensus-aura = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }
serde = { workspace = true }

[features]
default = ["std"]
Expand All @@ -39,6 +40,7 @@ std = [
"scale-info/std",
"sp-consensus-aura/std",
"hex/std",
"serde/std",
]
runtime-benchmarks = [
"frame-support/runtime-benchmarks",
Expand Down
86 changes: 86 additions & 0 deletions libs/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use parity_scale_codec::Encode;
use sp_std::cmp::min;

pub mod num_wrapper;

/// Build a fixed-size array using as many elements from `src` as possible
/// without overflowing and ensuring that the array is 0 padded in the case
/// where `src.len()` is smaller than S.
Expand Down Expand Up @@ -209,6 +211,90 @@ pub mod math {
}
}

pub mod time {
use crate::num_wrapper::NumWrapper;

/// Trait to convert into seconds
pub trait IntoSeconds {
type Seconds;
fn into_seconds(self) -> Self::Seconds;
}

/// Trait to convert into millis
pub trait IntoMillis {
type Millis;
fn into_millis(self) -> Self::Millis;
}

/// Type to distinguish NumWrapper as millis
pub struct MillisType;

/// Type to represent milliseconds
pub type Millis<T> = NumWrapper<T, MillisType>;

macro_rules! into_seconds {
($type_name:ident < $t:ty >) => {
impl $type_name<$t> {
pub const fn into_seconds(self) -> Seconds<$t> {
Seconds::from(self.inner / 1000)
}

/// Constant factor multiplication
pub const fn mul(self, other: $t) -> Self {
Self::from(self.inner * other)
}
}

impl IntoSeconds for $type_name<$t> {
type Seconds = Seconds<$t>;

fn into_seconds(self) -> Seconds<$t> {
self.into_seconds()
}
}
};
}

into_seconds!(Millis<u16>);
into_seconds!(Millis<u32>);
into_seconds!(Millis<u64>);
into_seconds!(Millis<u128>);

/// Type to distinguish NumWrapper as seconds
pub struct SecondsType;

/// Type to represent seconds
pub type Seconds<T> = NumWrapper<T, SecondsType>;

macro_rules! into_millis {
($type_name:ident < $t:ty >) => {
impl $type_name<$t> {
pub const fn into_millis(self) -> Millis<$t> {
Millis::from(self.inner.saturating_mul(1000))
}

/// Constant factor multiplication
pub const fn mul(self, other: $t) -> Self {
Self::from(self.inner * other)
}
}

impl IntoMillis for $type_name<$t> {
type Millis = Millis<$t>;

fn into_millis(self) -> Millis<$t> {
self.into_millis()
}
}
};
}

into_millis!(Seconds<u16>);
into_millis!(Seconds<u32>);
into_millis!(Seconds<u64>);
into_millis!(Seconds<u128>);
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading

0 comments on commit f18650a

Please sign in to comment.