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

x/authz module suggestions for minor code improvements #11569

Closed
dalmirel opened this issue Apr 7, 2022 · 0 comments · Fixed by #11840
Closed

x/authz module suggestions for minor code improvements #11569

dalmirel opened this issue Apr 7, 2022 · 0 comments · Fixed by #11840
Assignees
Labels
C:x/authz Type: Code Hygiene General cleanup and restructuring of code to provide clarity, flexibility, and modularity.

Comments

@dalmirel
Copy link

dalmirel commented Apr 7, 2022

As a part of model based testing audit of the authz module, code inspection has been done in order to find issues or possible improvements in code.
The following are recommendations for slightly neater and maybe better organized code (as far as recommended module structure). from a perspective of someone who is just getting introduced to authz implementation and Cosmos SDK itself.

Version:
master (commit: 8800d2e)
auditing is performed for tag: v0.46.0-alpha4 (commit: 354faa8)

Code improvement suggestions:

  1. validateAndBech32fy – possibly validateAllowAndDenyValidators as the name of the function or something similar would be more appropriate taking into account the logic of the func?

    func validateAndBech32fy(allowed []sdk.ValAddress, denied []sdk.ValAddress) ([]string, []string, error) {

  2. Move the line outside of /bellow of the switch statement, in order not to duplicate code:

    amount = msg.Amount

  3. This line here finished here probably by merge mistake, this comment should be on top of this

    func normalizeAuthzType(authzType AuthorizationType) (string, error) {

  4. Define variable for cached value av := g.Authorization.GetCachedValue() in

    a, ok := g.Authorization.GetCachedValue().(Authorization)

    And return this value in
    return nil, sdkerrors.ErrInvalidType.Wrapf("expected %T, got %T", (Authorization)(nil), g.Authorization.GetCachedValue())
    the same way as in ValidateBasic func

  5. Adding some comments for query Grants function in order to help understanding since there are two return branches and different logics in one method - depending on MsgTypeUrl - as stated in

    // Optional, msg_type_url, when set, will query only grants matching given msg type.
    string msg_type_url = 3;
    Or perhaps define authorizations var authorizations []*authz.Grant and pagination var pageRes *query.PageResponse variables, assign them values in both cases (grants for certain MsgType and in case of querying all MsgTypes grants) and have only one return from Grants func.

  6. Even though this is happening in tx - and no store changes will be committed in the end if err is returned, I think it is cleaner to move store.deletion: after the successful deletion of grant queue value and before emitting event.

    store.Delete(skey)
    if grant.Expiration != nil {
    err := k.removeFromGrantQueue(ctx, skey, granter, grantee, *grant.Expiration)
    if err != nil {
    return err
    }
    }
    return ctx.EventManager().EmitTypedEvent(&authz.EventRevoke{

  7. if you wish to follow the guide for building modules and recommended module structure, since there is genesis_test.go with unit tests for these functions, I suggest you create keeper/genesis.go file with InitGenesis and ExportGenesis functions (move them from keeper.go file .

  8. Note all Grant Queue key parts in comments that should explain key format here and/or here.

  9. NewCmdGrantAuthorization() improvements:

  • add info that spending limit is entered from CLI as max token amount for staking authorizations, because currently, we have:
    cmd.Flags().String(FlagSpendLimit, "", "SpendLimit for Send Authorization, an array of Coins allowed spend")
  • since spendLimit could be an array of coins for send authorizations, return an error for stake authorizations in case that more than one spending limit has been entered. Current implementation is:
    delegateLimit = &spendLimit[0]

    so, only first limit would be considered.
  • currently stake authorizations are created for denom that could differ from bond_denom. There is no check, but on the other hand I could not find a way to access this information. I got a link from @AmauryM to an existing issue: Expose chain information via new RPC route #11582 and an explanation that currently, we are creating "invalid" stake authorizations and leave them be until, that concrete authorized stake transactions hit the node and get rejected because of invalid denom.
@amaury1093 amaury1093 added Type: Code Hygiene General cleanup and restructuring of code to provide clarity, flexibility, and modularity. C:x/authz labels Apr 12, 2022
@likhita-809 likhita-809 self-assigned this Apr 28, 2022
@blushi blushi moved this from Ready to In Progress in Cosmos SDK Maintenance Apr 28, 2022
@amaury1093 amaury1093 moved this from In Progress to In Review in Cosmos SDK Maintenance May 2, 2022
@mergify mergify bot closed this as completed in #11840 May 9, 2022
mergify bot pushed a commit that referenced this issue May 9, 2022
## Description

Closes: #11569 



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
Repository owner moved this from In Review to Done in Cosmos SDK Maintenance May 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:x/authz Type: Code Hygiene General cleanup and restructuring of code to provide clarity, flexibility, and modularity.
Projects
No open projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants