Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Make Staking pallet using a proper Time module. #4662

Merged
merged 15 commits into from
Mar 26, 2020
Merged

Make Staking pallet using a proper Time module. #4662

merged 15 commits into from
Mar 26, 2020

Conversation

gui1117
Copy link
Contributor

@gui1117 gui1117 commented Jan 17, 2020

(EDITED)

the staking modules make use of Time trait to get the time in millisecond, however this a wrong usage of such traits which doesn't specifies what is moment.

To solve this this PR introduce a new trait named UnixTime implemented by timestamp and used by staking.

As said by Kian it would be great to have more docs on the Time trait itself and usage it is suppose to have. But I struggle to define any documentation for this trait.

The migration is implemented in such a way that the active era start at the moment of the migration.
This is needed because even if the wrong usage of Time correctly returned a moment in millisecond there is no guarantee that this moment is since unix_epoch.

On Kusama however the old Time was indeed the millisecond since unix epoch thus we could improve kusama migration.

@gui1117 gui1117 requested a review from kianenigma as a code owner January 17, 2020 13:28
@gui1117 gui1117 added the A0-please_review Pull request needs code review. label Jan 17, 2020
frame/support/src/traits.rs Outdated Show resolved Hide resolved
@gui1117 gui1117 added A4-gotissues and removed A0-please_review Pull request needs code review. labels Jan 20, 2020
@bkchr
Copy link
Member

bkchr commented Feb 3, 2020

Any update on this?

@gui1117
Copy link
Contributor Author

gui1117 commented Feb 3, 2020

OK I made an alternative implementation, otherwise I can do now_millis but I feel like this way the moment of now is a bit more configurable.

/// Duration for a Julian year (365.25 days).
fn year() -> Self::Moment;

/// Return current moment.
fn now() -> Self::Moment;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now you are mixing some time related functionality (year()) with a moment that can be anything.

I still think we should keep this Time trait simple and say that now() will return a moment that increases between blocks, but without specifying what it is. It could also be the moment from the genesis of the chain.

I would add another trait UnixTime that clearly says that now() returns the Duration from the unix epoch. I would directly return core::time::Duration. This already brings all the functions for converting between the different time formats.

The timestamp module would implement both of these traits and return the same value for both now() calls, but as different types. Staking would require that Time implements UnixTime.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I completely agree

@gui1117 gui1117 added A3-in_progress Pull request is in progress. No review needed at this stage. and removed A4-gotissues labels Feb 3, 2020
frame/staking/src/lib.rs Outdated Show resolved Hide resolved
frame/staking/src/migration.rs Outdated Show resolved Hide resolved
@gui1117 gui1117 added A0-please_review Pull request needs code review. B1-runtimenoteworthy and removed A3-in_progress Pull request is in progress. No review needed at this stage. labels Feb 4, 2020
@gui1117 gui1117 changed the title Write documentation for Time trait Make Staking pallet using a proper Time module. Feb 4, 2020
@gnunicorn
Copy link
Contributor

please update to latest master.

@gui1117 gui1117 added A3-in_progress Pull request is in progress. No review needed at this stage. and removed A0-please_review Pull request needs code review. labels Mar 6, 2020
@gui1117 gui1117 added A1-onice and removed A3-in_progress Pull request is in progress. No review needed at this stage. labels Mar 6, 2020
@kianenigma
Copy link
Contributor

I think it should be explained in the timestamp module when another module (i.e. staking in this case) should use Time impl and and when UnixTime.

Co-Authored-By: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
@bkchr
Copy link
Member

bkchr commented Mar 9, 2020

I think it should be explained in the timestamp module when another module (i.e. staking in this case) should use Time impl and and when UnixTime.

Why? It depends on what the module requires on constraints and not what the timestamp module provides.

You could just have the requirement of time increasing which could be anything, even just the block number.

Or like staking you really need UNIX time.

@gui1117
Copy link
Contributor Author

gui1117 commented Mar 9, 2020

I think it should be explained in the timestamp module when another module (i.e. staking in this case) should use Time impl and and when UnixTime.

to be honest I struggle to know what are the guarantee of Time, maybe that it can only increase.

@gui1117 gui1117 added A0-please_review Pull request needs code review. and removed A1-onice labels Mar 9, 2020
/// Deprecated Time.
///
/// Deprecated associated type wrongly used to query Time in millisecond.
type DeprecatedTime: Time;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the main use it to be able to decode the old structure ActiveInfo<MomentOf<T>>.
But indeed I can just decode the EraIndex and not start field as I don't use it.

I updated code as such, (also fixing that StorageVersion wasn't set)

@gui1117 gui1117 added A3-in_progress Pull request is in progress. No review needed at this stage. and removed A0-please_review Pull request needs code review. labels Mar 18, 2020
@bkchr
Copy link
Member

bkchr commented Mar 24, 2020

What is your plan with this @thiolliere ?

}

impl Default for Releases {
fn default() -> Self {
Releases::V2_0_0
Releases::V3_0_0
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what to think about this default, because if we add staking in a running chain (so without executing GenesisConfig) then we must insert the storage version in the runtime upgrade.
I think a staking module without any storage version must be considered invalid now.

otherwise if a chain introduce staking module without inserting the storage version then the storage version depends on when it has been inserted, and storage version could be V2 or V3 there is no way to know it.

@gui1117
Copy link
Contributor Author

gui1117 commented Mar 25, 2020

What is your plan with this @thiolliere ?

OK as the strategy for now is only to support Kusama I updated the runtime_upgrade code.
And merged master.

The PR is now ready for review

@gui1117 gui1117 added A0-please_review Pull request needs code review. and removed A3-in_progress Pull request is in progress. No review needed at this stage. labels Mar 25, 2020
frame/timestamp/src/lib.rs Outdated Show resolved Hide resolved
@bkchr
Copy link
Member

bkchr commented Mar 26, 2020

Please merge master.

gui1117 and others added 2 commits March 26, 2020 10:18
@bkchr bkchr merged commit 43c716b into master Mar 26, 2020
@bkchr bkchr deleted the gui-time-doc branch March 26, 2020 10:04
@kianenigma
Copy link
Contributor

Needed a polkadot companion PR :(

@gui1117
Copy link
Contributor Author

gui1117 commented Mar 26, 2020

sorry done here paritytech/polkadot#941

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants