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

refactor(x/auth): removal of Address.String() #19997

Closed
wants to merge 19 commits into from

Conversation

JulianToledano
Copy link
Contributor

@JulianToledano JulianToledano commented Apr 10, 2024

Description

ref:
#13140
#7448

There are two calls to String() that this PR doesn't remove:

func (acc *BaseAccount) SetAddress(addr sdk.AccAddress) error {
if len(acc.Address) != 0 {
return errors.New("cannot override BaseAccount address")
}
acc.Address = addr.String()
return nil
}

func (ma ModuleAccount) Validate() error {
if strings.TrimSpace(ma.Name) == "" {
return errors.New("module account name cannot be blank")
}
if ma.BaseAccount == nil {
return errors.New("uninitialized ModuleAccount: BaseAccount is nil")
}
if ma.Address != sdk.AccAddress(crypto.AddressHash([]byte(ma.Name))).String() {
return fmt.Errorf("address %s cannot be derived from the module name '%s'", ma.Address, ma.Name)
}
return ma.BaseAccount.Validate()
}

To remove those GenesisAccount and AccountI must be refactored.

The refactor of those will be done in an incoming PR to make reviews easier. (also easier to me 🙃)


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 in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • 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 in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • Refactor
    • Enhanced account management and address handling across various testing modules to improve consistency and reliability.
  • Bug Fixes
    • Addressed issues in address conversion and parameter passing to prevent potential errors and improve system stability.

@JulianToledano JulianToledano requested a review from a team as a code owner April 10, 2024 08:32
Copy link
Contributor

coderabbitai bot commented Apr 11, 2024

Walkthrough

Walkthrough

The changes involve a significant refactoring related to the handling of Bech32 address encoding in the Cosmos SDK. This includes initializing and using a new Bech32 codec, transitioning from direct string conversions to using an address codec, and modifying various test setups to integrate these changes.

Changes

Files Changes Summary
x/auth/ante/testutil_test.go
x/auth/keeper/deterministic_test.go
x/auth/keeper/keeper_test.go
x/auth/vesting/types/vesting_account_test.go
Introduced authcodec.NewBech32Codec("cosmos") for address encoding. Refactored address handling using the new codec and updated the initialization of accountKeeper to include the address codec.

Possibly related issues

  • Issue EPIC: remove global bech32 #13140: The changes in this PR align with the objectives to remove global Bech32 usage and replace direct string conversions with codec-based methods. This addresses parts of the issue related to refactoring address handling across various modules.

Recent Review Details

Configuration used: .coderabbit.yml

Commits Files that changed from the base of the PR and between b7d4b6e and 676ef26.
Files selected for processing (4)
  • x/auth/ante/testutil_test.go (2 hunks)
  • x/auth/keeper/deterministic_test.go (7 hunks)
  • x/auth/keeper/keeper_test.go (7 hunks)
  • x/auth/vesting/types/vesting_account_test.go (23 hunks)
Files skipped from review as they are similar to previous changes (4)
  • x/auth/ante/testutil_test.go
  • x/auth/keeper/deterministic_test.go
  • x/auth/keeper/keeper_test.go
  • x/auth/vesting/types/vesting_account_test.go

Warning

Following problems were encountered

  • Git: Failed to clone repository. Please contact CodeRabbit support.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 7

x/auth/ante/sigverify.go Show resolved Hide resolved
Comment on lines +393 to +397
addr, err := svd.ak.AddressCodec().BytesToString(acc.GetAddress())
if err != nil {
return fmt.Errorf("could not convert address to string: %s", err.Error())
}
return fmt.Errorf("the account %s is without a pubkey and did not provide a pubkey in the tx to set it", addr)
Copy link
Contributor

Choose a reason for hiding this comment

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

Improve error handling by separating the error message construction from the error check for clarity and maintainability.

			addr, err := svd.ak.AddressCodec().BytesToString(acc.GetAddress())
			if err != nil {
-				return fmt.Errorf("could not convert address to string: %s", err.Error())
+				errMsg := fmt.Errorf("could not convert address to string: %s", err.Error())
+				return errMsg
			}

This refactor makes the error handling more readable and maintainable by clearly separating the creation of the error message from the return statement.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
addr, err := svd.ak.AddressCodec().BytesToString(acc.GetAddress())
if err != nil {
return fmt.Errorf("could not convert address to string: %s", err.Error())
}
return fmt.Errorf("the account %s is without a pubkey and did not provide a pubkey in the tx to set it", addr)
addr, err := svd.ak.AddressCodec().BytesToString(acc.GetAddress())
if err != nil {
errMsg := fmt.Errorf("could not convert address to string: %s", err.Error())
return errMsg
}
return fmt.Errorf("the account %s is without a pubkey and did not provide a pubkey in the tx to set it", addr)

Comment on lines +409 to +413
addr, err := svd.ak.AddressCodec().BytesToString(acc.GetAddress())
if err != nil {
return fmt.Errorf("could not convert address to string: %s", err.Error())
}
return sdkerrors.ErrInvalidPubKey.Wrapf("the account %s cannot be claimed by public key with address %x", addr, txPubKey.Address())
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure consistent error handling by using sdkerrors.ErrInvalidPubKey.Wrapf for all public key related errors.

		addr, err := svd.ak.AddressCodec().BytesToString(acc.GetAddress())
		if err != nil {
-			return fmt.Errorf("could not convert address to string: %s", err.Error())
+			return sdkerrors.ErrInvalidPubKey.Wrapf("could not convert address to string: %s", err.Error())
		}
-		return sdkerrors.ErrInvalidPubKey.Wrapf("the account %s cannot be claimed by public key with address %x", addr, txPubKey.Address())
+		return sdkerrors.ErrInvalidPubKey.Wrapf("the account %s cannot be claimed by public key with address %s", addr, txPubKey.Address().String())

This change ensures that all public key related errors are handled consistently using the sdkerrors.ErrInvalidPubKey.Wrapf method, improving the readability and maintainability of the code. Additionally, converting the address to a string for the error message enhances clarity.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
addr, err := svd.ak.AddressCodec().BytesToString(acc.GetAddress())
if err != nil {
return fmt.Errorf("could not convert address to string: %s", err.Error())
}
return sdkerrors.ErrInvalidPubKey.Wrapf("the account %s cannot be claimed by public key with address %x", addr, txPubKey.Address())
addr, err := svd.ak.AddressCodec().BytesToString(acc.GetAddress())
if err != nil {
return sdkerrors.ErrInvalidPubKey.Wrapf("could not convert address to string: %s", err.Error())
}
return sdkerrors.ErrInvalidPubKey.Wrapf("the account %s cannot be claimed by public key with address %s", addr, txPubKey.Address().String())

x/auth/CHANGELOG.md Outdated Show resolved Hide resolved
x/genutil/genaccounts.go Outdated Show resolved Hide resolved
x/auth/client/cli/validate_sigs.go Show resolved Hide resolved
x/auth/types/account_retriever.go Show resolved Hide resolved
@JulianToledano JulianToledano changed the title refactor(x/auth)!: removal of Address.String() refactor(x/auth): removal of Address.String() Apr 11, 2024
@tac0turtle
Copy link
Member

lets leave this module as is. It's a bit messy and api breaking. Once mint is completed we will begin the work to use accounts as auth as an option. This will allow for auth to be deprecated. Thank you for this hard work. It always hurts a little when so much work is put into something and it's closed, sorry, 😢

@tac0turtle tac0turtle closed this May 9, 2024
@JulianToledano JulianToledano deleted the julian/auth-account-string-removal branch May 9, 2024 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants