-
Notifications
You must be signed in to change notification settings - Fork 0
feat(rust/catalyst-types): change RoleIndex to enum #283
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
base: main
Are you sure you want to change the base?
Conversation
This commit introduces RoleIndex as enum for a better static control over the logical variants of the type. closes #277
d1f4130
to
d47d69e
Compare
d57100e
to
2166c44
Compare
✅ Test Report | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing this part from the issue Replace the usage of the RoleNumber type with the new RoleId.
.
RoleNumber
structure is defined inside the rbac-registration
crate. And this type facilitates exactly the same purpose, so it's redundant to have two types for the same entity.
So you will need replace the RoleNumber
with the RoleId
inside the rbac-registration
crate and remove RoleNumber
at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Despite one comment LGTM.
Only one more thing before we will merge this PR into main.
Pls create a PR with the updating and applying this branch to the cat-gateway first, and if everything would be fine on the cat-gateway
side will merge this PR.
The role numbers are now statically defined so an invalid one cannot exist.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When working with enums in rust, this crate:
https://docs.rs/strum_macros/0.27.1/strum_macros/derive.EnumDiscriminants.html
which we already use can save a lot of boilerplate and maintenance keeping everything in-sync.
Suggest looking to see if anything other than the two place I highlight could be simplified using these derived macros.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sure that comment related to "Role Index" and "Role number" are changed
eg. This should change to Role ID
/// Role Index is not encoded correctly
InvalidRoleId(#[from] RoleIdError),
Co-authored-by: bkioshn <[email protected]>
Co-authored-by: bkioshn <[email protected]>
Co-authored-by: bkioshn <[email protected]>
Co-authored-by: bkioshn <[email protected]>
VoterDelegation = 2, | ||
/// Proposer that enabling creation, collaboration, and submission of proposals. | ||
Proposer = 3, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like this enum should have a method that returns True if the RoleId is a known variant, and false otherwise.
Which could be used by the currently removed validate_role_numbers
function below to determine if any particular role number is known without worrying about what exact role number it is.
You could use: https://docs.rs/strum/latest/strum/derive.EnumIter.html
or https://docs.rs/strum/latest/strum/derive.VariantArray.html
to achieve this without having to update the method when we add new variants.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The initial rationale was that the static list of known roles were valid. Is the valid list a subset? The previous implementation of validate_role_numbers
contemplated only roles 0 and 3; do we have a documented list somewhere of what is a valid role? For me to link here for future reference
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is the original spec of the definition of the different role id types https://github.com/input-output-hk/catalyst-CIPs/blob/x509-catalyst-role-definitions/CIP-XXXX/README.md#project-catalyst-user-role-registrations
/// Delegated representative (dRep) that vote on behalf of delegators. | ||
DelegatedRepresentative = 1, | ||
/// Voter role that delegates voting power to a chosen representative (dRep). | ||
VoterDelegation = 2, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove VoterDelegation
we are not going to use this role at this number.
So the known members are 0,1 and 3 only so far.
Co-authored-by: Alex Pozhylenkov <[email protected]>
Co-authored-by: Alex Pozhylenkov <[email protected]>
|
||
/// Returns `true` if the role belongs to the canonical set of known roles. | ||
#[must_use] | ||
pub const fn is_known(self) -> bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better replace this with using this derive macro https://docs.rs/strum/latest/strum/derive.EnumIs.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is a group matching, and EnumIs
matches individually, I wonder if that would be an improvement. We can hardly beat the performance and simplicity of matches
.
With matches, the macro will expand to a single pattern matching. With EnumIs
, we would have to do something like self.is_role0() || self.is_delegated_representative() || self.is_proposer()
, which would be 3 matching + ors. The compiler might be able to optimize this, but it would be more verbose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No no, I've meant to not have an explicit is_known
method defined by ourselves at all.
rather to use EnumIs
so it will define for us something like is_unknown
which would be more than enough to call !is_unknown
in any other place.
Co-authored-by: Alex Pozhylenkov <[email protected]>
|
||
/// Returns `true` if the role belongs to the canonical set of known roles. | ||
#[must_use] | ||
pub const fn is_known(self) -> bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No no, I've meant to not have an explicit is_known
method defined by ourselves at all.
rather to use EnumIs
so it will define for us something like is_unknown
which would be more than enough to call !is_unknown
in any other place.
|
||
use super::*; | ||
|
||
proptest! { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
proptest! { |
#[cfg(test)] | ||
mod tests { | ||
use minicbor::{Decoder, Encoder}; | ||
use proptest::prelude::*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use proptest::prelude::*; |
Description
This commit introduces RoleIndex as enum for a better static control over the logical variants of the type.
Related Issue(s)
closes #277
Description of Changes
RoleIndex
fromRoleIndex(u16)
toenum
Breaking Changes
RoleIndex
fromRoleIndex(u16)
toenum
Screenshots
N/A
Related Pull Requests
N/A
Please confirm the following checks