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

feat: refactor whitelisted_addresses #392

Merged
merged 4 commits into from
Apr 25, 2024

Conversation

trinitys7
Copy link
Contributor

Closes #390

@trinitys7 trinitys7 requested a review from a team as a code owner April 25, 2024 06:34
testutil/app/app.go Outdated Show resolved Hide resolved
testutil/app/app.go Outdated Show resolved Hide resolved
Copy link
Contributor

@danwt danwt left a comment

Choose a reason for hiding this comment

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

  • There might be a few unused funcs, are you sure the wiring is complete?
  • Could we just have 1 proposal type called UpdatePerms, which has a grant/revoke bool or enum

string genesis_operator_address = 3;
Copy link
Contributor

Choose a reason for hiding this comment

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

is sequencer_operator_address a better name?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This one is the original name in sequencer module, I didn't change anything about it. So if it's required, I think we will need another chores issue for that.

Comment on lines +35 to 37
if !m.sequencerKeeper.HasPermission(ctx, accAddr, types.ModuleName) {
return nil, sdkerrors.ErrUnauthorized
}
Copy link
Contributor

Choose a reason for hiding this comment

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

unauthorized or noPermission?

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 hub-genesis's orginally use Unauthoried from cosmos-sdk, while the denommetadata define an error called NoPermission

x/hub-genesis/keeper/msg_server_test.go Outdated Show resolved Hide resolved
x/sequencers/client/cli/tx.go Outdated Show resolved Hide resolved
x/sequencers/client/cli/tx.go Outdated Show resolved Hide resolved
x/sequencers/types/keys.go Outdated Show resolved Hide resolved
Comment on lines +23 to +25
if len(p.Permissions) == 0 {
return errors.New("permissions field cannot be empty")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

why?

Copy link
Contributor

Choose a reason for hiding this comment

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

extra branch for no reason I think

Copy link
Contributor Author

@trinitys7 trinitys7 Apr 25, 2024

Choose a reason for hiding this comment

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

Why do you think it is not needed? In this case, we can get rid of the empty proposal that happen in our chain

Copy link
Contributor

Choose a reason for hiding this comment

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

it's just an extra branch for no reason
let the empty proposal happen

x/sequencers/types/permission.go Outdated Show resolved Hide resolved
x/sequencers/types/proposal_permission.go Outdated Show resolved Hide resolved
x/sequencers/types/proposal_permission.go Outdated Show resolved Hide resolved
proto/hub-genesis/genesis.proto Outdated Show resolved Hide resolved
@@ -33,7 +33,12 @@ func (k msgServer) CreateDenomMetadata(
return nil, err
}

if !k.IsAddressPermissioned(ctx, msg.SenderAddress) {

Choose a reason for hiding this comment

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

should refactor IsAddressPermissioned and reuse it

Copy link
Contributor

Choose a reason for hiding this comment

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

also make it private

x/denommetadata/module.go Show resolved Hide resolved
x/sequencers/types/permission.go Show resolved Hide resolved
x/sequencers/types/genesis.go Show resolved Hide resolved
@trinitys7
Copy link
Contributor Author

@danwt @anhductn2001 @lacsomot guys, thanks for all the comments you leave. I think I solves most of them. Please help me check again one more time before we can merge them into rdk

@@ -56,7 +56,7 @@ func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncod
if err := cdc.UnmarshalJSON(bz, &genState); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

why bother unmashalling genState if it's not used?

// params are all parameters for the module
Params params = 1 [ (gogoproto.nullable) = false ];
}
message GenesisState {}
Copy link
Contributor

Choose a reason for hiding this comment

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

if this is empty, why do we need it at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should keep it as it's the cosmos-sdk standard, and in case we need to store some states in the future

(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
];
// is_locked is a boolean that indicates if the genesis event has occured
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// is_locked is a boolean that indicates if the genesis event has occured
// is_locked is a boolean that indicates if the genesis event has occurred

@@ -33,7 +33,12 @@ func (k msgServer) CreateDenomMetadata(
return nil, err
}

if !k.IsAddressPermissioned(ctx, msg.SenderAddress) {
Copy link
Contributor

Choose a reason for hiding this comment

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

also make it private

@@ -27,7 +27,12 @@ func (m msgServer) TriggerGenesisEvent(goCtx context.Context, msg *types.MsgHubG
ctx := sdk.UnwrapSDKContext(goCtx)

// Get the sender and validate they are in the Allowlist
if !m.IsAddressInGenesisTriggererAllowList(ctx, msg.Address) {
accAddr, err := sdk.AccAddressFromBech32(msg.Address)
Copy link
Contributor

Choose a reason for hiding this comment

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

these checks could indeed be moved to a separate method. no blocker though

return errors.Wrapf(err, "address format error")
}

res, err := queryClient.Permissions(context.Background(), &types.QueryPermissionsRequest{
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
res, err := queryClient.Permissions(context.Background(), &types.QueryPermissionsRequest{
res, err := queryClient.Permissions(cmd.Context(), &types.QueryPermissionsRequest{

// method.
message QueryPermissionsResponse {
// permissions defines the permissions for the given address.
string permissions = 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

why not repeated string permissions = 1; ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need to store it in KVStore, and the repeated string, which will be convert to []string can not be Marshal to bytes in codec. This's the reason I make a wrapper struct for that

params := q.GetParams(ctx)
return &types.QueryParamsResponse{Params: params}, nil
}

// IBCDenomByDenomTrace returns IBC denom base on denom trace
func (q Querier) IBCDenomByDenomTrace(
Copy link
Contributor

Choose a reason for hiding this comment

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

I know it's not related, but this should be covered by a test. Maybe leave a TODO, this PR is already big enough.

@@ -0,0 +1,57 @@
package sequencers

Copy link
Contributor

Choose a reason for hiding this comment

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

be nice to write a test for these

Comment on lines +12 to +57
func NewUpdatePermissionProposalHandler(k *keeper.Keeper) govtypes.Handler {
return func(ctx sdk.Context, content govtypes.Content) error {
switch c := content.(type) {
case *types.GrantPermissionsProposal:
return HandleGrantPermissionsProposal(ctx, k, c)
case *types.RevokePermissionsProposal:
return HandleRevokePermissionsProposal(ctx, k, c)
default:
return errorsmod.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized permissions proposal content type: %T", c)
}
}
}

// HandleGrantPermissionsProposal is a handler for executing a grant permissions proposal
func HandleGrantPermissionsProposal(ctx sdk.Context, k *keeper.Keeper, p *types.GrantPermissionsProposal) error {
if err := p.ValidateBasic(); err != nil {
return err
}

for _, addrPerms := range p.AddressPermissions {
accAddr, err := sdk.AccAddressFromBech32(addrPerms.Address)
if err != nil {
return err
}

k.GrantPermissions(ctx, accAddr, addrPerms.PermissionList)
}
return nil
}

// HandleRevokePermissionsProposal is a handler for executing a revoke permissions proposal
func HandleRevokePermissionsProposal(ctx sdk.Context, k *keeper.Keeper, p *types.RevokePermissionsProposal) error {
if err := p.ValidateBasic(); err != nil {
return err
}

for _, addrPerms := range p.AddressPermissions {
accAddr, err := sdk.AccAddressFromBech32(addrPerms.Address)
if err != nil {
return err
}

k.RevokePermissions(ctx, accAddr, addrPerms.PermissionList)
}
return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

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

There is some repetition here - could make it DRYer like so:

Suggested change
func NewUpdatePermissionProposalHandler(k *keeper.Keeper) govtypes.Handler {
return func(ctx sdk.Context, content govtypes.Content) error {
switch c := content.(type) {
case *types.GrantPermissionsProposal:
return HandleGrantPermissionsProposal(ctx, k, c)
case *types.RevokePermissionsProposal:
return HandleRevokePermissionsProposal(ctx, k, c)
default:
return errorsmod.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized permissions proposal content type: %T", c)
}
}
}
// HandleGrantPermissionsProposal is a handler for executing a grant permissions proposal
func HandleGrantPermissionsProposal(ctx sdk.Context, k *keeper.Keeper, p *types.GrantPermissionsProposal) error {
if err := p.ValidateBasic(); err != nil {
return err
}
for _, addrPerms := range p.AddressPermissions {
accAddr, err := sdk.AccAddressFromBech32(addrPerms.Address)
if err != nil {
return err
}
k.GrantPermissions(ctx, accAddr, addrPerms.PermissionList)
}
return nil
}
// HandleRevokePermissionsProposal is a handler for executing a revoke permissions proposal
func HandleRevokePermissionsProposal(ctx sdk.Context, k *keeper.Keeper, p *types.RevokePermissionsProposal) error {
if err := p.ValidateBasic(); err != nil {
return err
}
for _, addrPerms := range p.AddressPermissions {
accAddr, err := sdk.AccAddressFromBech32(addrPerms.Address)
if err != nil {
return err
}
k.RevokePermissions(ctx, accAddr, addrPerms.PermissionList)
}
return nil
}
func NewUpdatePermissionProposalHandler(k *keeper.Keeper) govtypes.Handler {
return func(ctx sdk.Context, content govtypes.Content) error {
if err := content.ValidateBasic(); err != nil {
return err
}
switch c := content.(type) {
case *types.GrantPermissionsProposal:
return HandlePermissionsProposal(ctx, c.AddressPermissions, k.GrantPermissions)
case *types.RevokePermissionsProposal:
return HandlePermissionsProposal(ctx, c.AddressPermissions, k.RevokePermissions)
default:
return errorsmod.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized permissions proposal content type: %T", c)
}
}
}
// action can be grant or revoke
type actionFn func(ctx sdk.Context, accAddr sdk.AccAddress, permList types.PermissionList)
// HandlePermissionsProposal is a handler for executing a permissions proposal
func HandlePermissionsProposal(ctx sdk.Context, perms []types.AddressPermissions, action actionFn) error {
for _, perm := range perms {
accAddr, err := sdk.AccAddressFromBech32(perm.Address)
if err != nil {
return err
}
action(ctx, accAddr, perm.PermissionList)
}
return nil
}

Copy link
Contributor

@zale144 zale144 left a comment

Choose a reason for hiding this comment

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

Good work. Just a couple of minor things that could maybe be either resolved now, or left as TODOs

@omritoptix omritoptix merged commit 8d63aac into main Apr 25, 2024
6 checks passed
@omritoptix omritoptix deleted the trinity/refactor-acc-permission branch April 25, 2024 21:17
Comment on lines +45 to +47
newPerms := slices.DeleteFunc(permissionList.Permissions, func(perm string) bool {
return slices.Contains(revokePermList.Permissions, perm)
})
Copy link
Contributor

Choose a reason for hiding this comment

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

n^2 dos vector?

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 don't think this could make the chain receive dos attack, currently we just have vesting, hubgenesis, and denommetadata module that require permissions. This won't affect anything

Comment on lines +37 to +39
if !p.Equal(NewPermissionsList(perms)) {
return fmt.Errorf("PermissionList is not sorted yet")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we require sorted? should have a comment explaining

Copy link
Contributor Author

Choose a reason for hiding this comment

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

can remove sorting, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

refactor whitelisted_addresses
6 participants