Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix trackId=>Origin conversion, support submit for root track #2084

Merged
merged 15 commits into from Feb 17, 2023

Conversation

4meta5
Copy link
Contributor

@4meta5 4meta5 commented Feb 8, 2023

Fixes referenda::submit for root track. We were previously relying on the TryFrom<TrackId = u16> impl for custom origins, but this did not support the root origin/track.

We replace TryFrom<TrackId = u16> with a derived FromStr impl and use this in conjunction with Runtime::Tracks::tracks() to get the TrackId => Origin mapping.

If Runtime::Tracks::tracks() grows big enough, we will change it back to use a TryFrom impl.

@4meta5 4meta5 added A0-pleasereview Pull request needs code review. B7-runtimenoteworthy Changes should be noted in any runtime-upgrade release notes D3-trivial PR contains trivial changes in a runtime directory that do not require an audit not-breaking Does not need to be mentioned in breaking changes labels Feb 8, 2023
@@ -278,6 +275,21 @@ where
Ok(())
}

// Helper function for submit precompile functions
fn track_id_to_origin(track_id: u16) -> EvmResult<Box<OriginOf<Runtime>>> {
let origin: OriginOf<Runtime> = if track_id == 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

How do we know track_id 0 is root ? That seems a very dangerous shortcut.

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 have not seen an OpenGov config with trackId 0 not corresponding to root origin. Moreover because it is a special origin, there is no other way to express this mapping.

Copy link
Contributor Author

@4meta5 4meta5 Feb 10, 2023

Choose a reason for hiding this comment

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

I'll add an integrity test to the runtimes to ensure trackId 0 is root but this is the best we can do.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think there should be a way to do that properly. It is too prone to error if one day we change the tracks

Copy link
Collaborator

Choose a reason for hiding this comment

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

@girazoki or @librelois to suggest some ideas ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Root is not part of the OpenGov origins, so whatever you do you have to map it manually somewhere.

To be sure of the match at compile time, we can create a public constant ROOT_TRACK_ID in a common crate and use it in runtime/.../tracks.rs and in the precompiles

Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not make this part of the From<GovOrigin> instead of hardcoding? I will take a closer look at whether we can do something more clever, but at least this should probably be part of the runtime impl

Copy link
Collaborator

@girazoki girazoki Feb 13, 2023

Choose a reason for hiding this comment

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

Additionally we could add a check that the origin matches the track through track_for inside the precompile

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Additionally we could add a check that the origin matches the track through track_for inside the precompile

I like this idea of using Tracks::tracks_for so I'm doing this now and will ping once ready for review.

Copy link
Contributor Author

@4meta5 4meta5 Feb 14, 2023

Choose a reason for hiding this comment

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

@girazoki @librelois I did a significant refactor to use Runtime::Tracks::tracks() in the precompile and runtime. This removes the TryFrom<u16> impl for CustomOrigin and adds a FromStr impl for CustomOrigin.

Also added an integration test to ensure all trackIds in TRACKS_DATA either correspond to the root origin or a custom origin.

@4meta5 4meta5 changed the title Fix submit for root track Support submit for root track Feb 14, 2023
@4meta5 4meta5 added A3-inprogress Pull request is in progress. No review needed at this stage. and removed A0-pleasereview Pull request needs code review. labels Feb 14, 2023
2 => Ok(Origin::GeneralAdmin),
3 => Ok(Origin::ReferendumCanceller),
4 => Ok(Origin::ReferendumKiller),
fn try_from(value: String) -> Result<Origin, ()> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I suggest to use strum derive macro EnumString to autogenerate this implementation:

https://docs.rs/strum/latest/strum/derive.EnumString.html

@4meta5
Copy link
Contributor Author

4meta5 commented Feb 14, 2023

  • integration test for moon{base, river} to ensure all $track.name in TRACKS_DATA correspond to either "root" or successfully convert FromStr for custom origins

@4meta5 4meta5 added A0-pleasereview Pull request needs code review. and removed A3-inprogress Pull request is in progress. No review needed at this stage. labels Feb 15, 2023
@4meta5 4meta5 changed the title Support submit for root track Fix trackId=>Origin conversion, support submit for root track Feb 16, 2023
@4meta5 4meta5 merged commit bbb0deb into master Feb 17, 2023
@4meta5 4meta5 deleted the amar-fix-submit-for-root-track-id branch February 17, 2023 13:16
imstar15 pushed a commit to OAK-Foundation/moonbeam that referenced this pull request May 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A0-pleasereview Pull request needs code review. B7-runtimenoteworthy Changes should be noted in any runtime-upgrade release notes D3-trivial PR contains trivial changes in a runtime directory that do not require an audit not-breaking Does not need to be mentioned in breaking changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants