The idea of this PR is to have Millis and Seconds as different types for the compiler. This helps to never mix by error different duration units and improve the readability of some parts of the codebase.
To achieve this, we need:
A wrapper type over integers that allows us different integer-like types to tell the compiler not to mix types (check num_wrapper.rs file).
Some explicit conversions from seconds to millis that will change the value accordingly
Implement everything from BaseArithmetic to be able to use this types as normal u64 (i.e. give to pallet_timestamp's Moment the new Millis type.
How times should be used now in the code base?
If the pallet just carries a time unit, without knowing what unit is, use a generic Moment (probably most of the times a <<T as Config>::Time as Time>::Moment), as usual. You can use the trait TimeUnit to get the time as the unit you want.
If the pallet uses a moment type that specifically represents millis, seconds or days, you should use cfg_primitives::{Millis, Seconds, Days, ..}. There is no need for an associated type in the pallet.
Blockers
~It seems like parity_scale_codec doesn't allow to implement HasCompact trait required by BasicArithmetic:~
Requirement::DelayTime uses a u32 to hold seconds when it should be a u64 wrapped into Seconds type. Nevertheless, this requires a migration in an storage of pool-system.
Description
The idea of this PR is to have
Millis
andSeconds
as different types for the compiler. This helps to never mix by error different duration units and improve the readability of some parts of the codebase.To achieve this, we need:
num_wrapper.rs
file).BaseArithmetic
to be able to use this types as normalu64
(i.e. give topallet_timestamp
'sMoment
the newMillis
type.How times should be used now in the code base?
Moment
(probably most of the times a<<T as Config>::Time as Time>::Moment
), as usual. You can use the traitTimeUnit
to get the time as the unit you want.cfg_primitives::{Millis, Seconds, Days, ..}
. There is no need for an associated type in the pallet.Blockers
~It seems like
parity_scale_codec
doesn't allow to implementHasCompact
trait required byBasicArithmetic
:~~https://github.com/paritytech/parity-scale-codec/issues/613~
HasCompact
~~https://github.com/paritytech/polkadot-sdk/issues/5004~
HasCompact
~Found a solution: last message of https://github.com/paritytech/parity-scale-codec/issues/613
Remainders
Requirement::DelayTime
uses au32
to hold seconds when it should be au64
wrapped intoSeconds
type. Nevertheless, this requires a migration in an storage of pool-system.