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

chore(x/gov, x/authz): use cosmossdk.io/core/codec instead of github.com/cosmos/cosmos-sdk/codec #23292

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

hoank101
Copy link
Contributor

@hoank101 hoank101 commented Jan 10, 2025

Description

Partially addresses: #23253


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, you can find examples of the prefixes below:
  • 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.

Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.

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

  • Dependency Updates

    • Migrated codec imports from Cosmos SDK to cosmossdk.io/core/codec
  • Error Handling Improvements

    • Enhanced JSON marshaling and unmarshaling processes with explicit error checking
    • Replaced MustMarshalJSON with more robust error management techniques
  • Module Changes

    • Updated codec usage in authz and gov modules
    • Preserved existing functional behavior during codec transition

The changes primarily focus on improving codec handling and error management across multiple modules with minimal impact on existing functionality.

@hoank101 hoank101 requested review from JulianToledano, ziscky and a team as code owners January 10, 2025 03:14
Copy link
Contributor

coderabbitai bot commented Jan 10, 2025

📝 Walkthrough

Walkthrough

This pull request focuses on updating codec imports across multiple modules from github.com/cosmos/cosmos-sdk/codec to cosmossdk.io/core/codec. The changes primarily involve modifying import statements and adjusting error handling in methods like DefaultGenesis. The modifications affect the x/authz and x/gov modules, introducing more explicit error handling for JSON marshaling and unmarshaling operations while maintaining the core functionality of the existing code.

Changes

File Change Summary
x/authz/migrations/v2/store.go Updated codec import from Cosmos SDK to core codec
x/authz/module/module.go - Updated codec import
- Modified DefaultGenesis method with explicit error handling
x/authz/simulation/decoder.go - Updated codec import
- Replaced MustUnmarshal with Unmarshal and added error checking
x/gov/migrations/v5/store.go Updated codec import from Cosmos SDK to core codec
x/gov/module.go - Updated codec import
- Modified DefaultGenesis method with explicit error handling

Possibly related PRs

Suggested labels

backport/v0.52.x

Suggested reviewers

  • tac0turtle
  • lucaslopezf
  • julienrbrt
  • kocubinski
  • sontrinh16

Finishing Touches

  • 📝 Generate Docstrings (Beta)

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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: 1

🔭 Outside diff range comments (1)
x/staking/types/validator.go (1)

Line range hint 18-22: Fix import ordering according to project guidelines.

The imports need to be reordered following the pattern: standard libs, default libs, cosmossdk.io prefix, github.com/cosmos/cosmos-sdk prefix.

	"cosmossdk.io/errors"
	"cosmossdk.io/math"

-	"cosmossdk.io/core/codec"
-	codectypes "github.com/cosmos/cosmos-sdk/codec/types"
-	cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
-	sdk "github.com/cosmos/cosmos-sdk/types"
-	sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
+	"cosmossdk.io/core/codec"
+
+	codectypes "github.com/cosmos/cosmos-sdk/codec/types"
+	cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
+	sdk "github.com/cosmos/cosmos-sdk/types"
+	sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
🧰 Tools
🪛 golangci-lint (1.62.2)

18-18: File is not gci-ed with --skip-generated -s standard -s default -s prefix(cosmossdk.io) -s prefix(github.com/cosmos/cosmos-sdk) --custom-order

(gci)

📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 517839b and 9908ef5.

📒 Files selected for processing (3)
  • x/staking/migrations/v5/store.go (1 hunks)
  • x/staking/migrations/v6/store.go (1 hunks)
  • x/staking/types/validator.go (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • x/staking/migrations/v5/store.go
🧰 Additional context used
📓 Path-based instructions (2)
x/staking/migrations/v6/store.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

x/staking/types/validator.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

🪛 golangci-lint (1.62.2)
x/staking/migrations/v6/store.go

8-8: File is not gci-ed with --skip-generated -s standard -s default -s prefix(cosmossdk.io) -s prefix(github.com/cosmos/cosmos-sdk) --custom-order

(gci)

x/staking/types/validator.go

18-18: File is not gci-ed with --skip-generated -s standard -s default -s prefix(cosmossdk.io) -s prefix(github.com/cosmos/cosmos-sdk) --custom-order

(gci)

🪛 GitHub Actions: Lint
x/staking/migrations/v6/store.go

[error] 6-8: File is not properly formatted with gci. Import ordering needs to follow: --skip-generated -s standard -s default -s prefix(cosmossdk.io) -s prefix(github.com/cosmos/cosmos-sdk) --custom-order

x/staking/types/validator.go

[error] 14-18: File is not properly formatted with gci. Import ordering needs to follow: --skip-generated -s standard -s default -s prefix(cosmossdk.io) -s prefix(github.com/cosmos/cosmos-sdk) --custom-order

⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: tests (00)
  • GitHub Check: test-simapp-v2
  • GitHub Check: test-system-v2
  • GitHub Check: Analyze
  • GitHub Check: Summary
🔇 Additional comments (2)
x/staking/migrations/v6/store.go (1)

Line range hint 12-19: LGTM! Migration logic remains unchanged.

The codec interface change is compatible, and the store migration logic is unaffected by the import path update.

Let's verify the codec interface compatibility:

✅ Verification successful

Migration code is safe and unaffected by codec changes

The migration function only performs store deletions and doesn't use the codec parameter, making it immune to any codec interface changes.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Verify that both old and new codec packages expose the same BinaryCodec interface
rg -A 5 "type BinaryCodec interface" 

Length of output: 373


Script:

#!/bin/bash
# Find storetypes package and its codec usage
rg -A 5 "package storetypes"

# Find all BinaryCodec interfaces and their usages
ast-grep --pattern 'type BinaryCodec interface { $$$ }'

Length of output: 295

🧰 Tools
🪛 golangci-lint (1.62.2)

6-6: File is not gci-ed with --skip-generated -s standard -s default -s prefix(cosmossdk.io) -s prefix(github.com/cosmos/cosmos-sdk) --custom-order

(gci)


8-8: File is not gci-ed with --skip-generated -s standard -s default -s prefix(cosmossdk.io) -s prefix(github.com/cosmos/cosmos-sdk) --custom-order

(gci)

🪛 GitHub Actions: Lint

[error] 6-8: File is not properly formatted with gci. Import ordering needs to follow: --skip-generated -s standard -s default -s prefix(cosmossdk.io) -s prefix(github.com/cosmos/cosmos-sdk) --custom-order

x/staking/types/validator.go (1)

Line range hint 147-147: LGTM! Removal of panic-prone functions improves safety.

The removal of MustMarshalValidator and MustUnmarshalValidator functions is a good change as it:

  1. Eliminates potential panics in production
  2. Forces proper error handling
  3. Simplifies the codebase

Let's verify that no code depends on the removed functions:

✅ Verification successful

Confirmed: MustMarshalValidator and MustUnmarshalValidator functions were safely removed

The verification shows that no code in the codebase depends on the removed panic-prone functions. All validator marshaling operations now properly handle errors.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any remaining usage of the removed functions
rg "MustMarshalValidator|MustUnmarshalValidator"

Length of output: 50


Script:

#!/bin/bash
# Search with broader patterns to catch variations
rg -i "marshal.*validator|unmarshal.*validator" -A 2

# Use ast-grep to find any function calls related to validator marshaling
ast-grep --pattern 'Must$_Marshal$_($$$)'

# Search for any validator-related functions with Must prefix
rg "Must.*Validator"

# Look for any Marshal/Unmarshal functions in validator files
fd validator --exec grep -i "marshal" {} \;

Length of output: 70973

🧰 Tools
🪛 golangci-lint (1.62.2)

18-18: File is not gci-ed with --skip-generated -s standard -s default -s prefix(cosmossdk.io) -s prefix(github.com/cosmos/cosmos-sdk) --custom-order

(gci)

@@ -5,7 +5,7 @@ import (

storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/codec"
"cosmossdk.io/core/codec"
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Fix import ordering according to project guidelines.

The imports need to be reordered following the pattern: standard libs, default libs, cosmossdk.io prefix, github.com/cosmos/cosmos-sdk prefix.

import (
	"context"

	storetypes "cosmossdk.io/store/types"
-	"cosmossdk.io/core/codec"
+
+	"cosmossdk.io/core/codec"
)
📝 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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"cosmossdk.io/core/codec"
import (
"context"
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/core/codec"
)
🧰 Tools
🪛 golangci-lint (1.62.2)

8-8: File is not gci-ed with --skip-generated -s standard -s default -s prefix(cosmossdk.io) -s prefix(github.com/cosmos/cosmos-sdk) --custom-order

(gci)

🪛 GitHub Actions: Lint

[error] 6-8: File is not properly formatted with gci. Import ordering needs to follow: --skip-generated -s standard -s default -s prefix(cosmossdk.io) -s prefix(github.com/cosmos/cosmos-sdk) --custom-order

@caseylove
Copy link
Contributor

@hoank101 hey, the changes to module x/staking have been made in #23289

@hoank101 hoank101 requested a review from julienrbrt as a code owner January 10, 2025 03:39
@hoank101 hoank101 changed the title chore(x/staking): use cosmossdk.io/core/codec instead of github.com/cosmos/cosmos-sdk/codec chore(x/staking, x/gov): use cosmossdk.io/core/codec instead of github.com/cosmos/cosmos-sdk/codec Jan 10, 2025
@hoank101 hoank101 changed the title chore(x/staking, x/gov): use cosmossdk.io/core/codec instead of github.com/cosmos/cosmos-sdk/codec chore(x/gov): use cosmossdk.io/core/codec instead of github.com/cosmos/cosmos-sdk/codec Jan 10, 2025
@hoank101 hoank101 requested a review from raynaudoe as a code owner January 10, 2025 03:45
@hoank101 hoank101 changed the title chore(x/gov): use cosmossdk.io/core/codec instead of github.com/cosmos/cosmos-sdk/codec chore(x/gov, x/authz): use cosmossdk.io/core/codec instead of github.com/cosmos/cosmos-sdk/codec Jan 10, 2025
@hoank101
Copy link
Contributor Author

cc @julienrbrt

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: 0

🧹 Nitpick comments (2)
x/authz/simulation/decoder.go (1)

21-26: Consider returning errors instead of panicking

While the error handling is now explicit, panicking in a decoder function could destabilize simulations. Consider modifying the function signature to return an error instead.

-func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string {
+func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) (string, error) {
-    return func(kvA, kvB kv.Pair) string {
+    return func(kvA, kvB kv.Pair) (string, error) {
         switch {
         case bytes.Equal(kvA.Key[:1], keeper.GrantKey):
             var grantA, grantB authz.Grant
             if err := cdc.Unmarshal(kvA.Value, &grantA); err != nil {
-                panic(err)
+                return "", err
             }
             if err := cdc.Unmarshal(kvB.Value, &grantB); err != nil {
-                panic(err)
+                return "", err
             }
-            return fmt.Sprintf("%v\n%v", grantA, grantB)
+            return fmt.Sprintf("%v\n%v", grantA, grantB), nil

Also applies to: 30-35

x/gov/module.go (1)

160-164: Consider returning error instead of panic

While the current implementation improves error visibility compared to MustMarshalJSON, consider returning an error instead of panicking for better error handling. However, since this is called during chain initialization, the current approach is acceptable.

-func (am AppModule) DefaultGenesis() json.RawMessage {
+func (am AppModule) DefaultGenesis() (json.RawMessage, error) {
 	data, err := am.cdc.MarshalJSON(v1.DefaultGenesisState())
 	if err != nil {
-		panic(err)
+		return nil, fmt.Errorf("failed to marshal default genesis state: %w", err)
 	}
-	return data
+	return data, nil
}
📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9908ef5 and 44b7150.

📒 Files selected for processing (5)
  • x/authz/migrations/v2/store.go (1 hunks)
  • x/authz/module/module.go (2 hunks)
  • x/authz/simulation/decoder.go (2 hunks)
  • x/gov/migrations/v5/store.go (1 hunks)
  • x/gov/module.go (2 hunks)
🧰 Additional context used
📓 Path-based instructions (5)
x/authz/migrations/v2/store.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

x/gov/module.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

x/gov/migrations/v5/store.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

x/authz/simulation/decoder.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

x/authz/module/module.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: test-simapp-v2
  • GitHub Check: test-system-v2
  • GitHub Check: Analyze
  • GitHub Check: Summary
🔇 Additional comments (6)
x/authz/simulation/decoder.go (1)

7-7: Import change looks good

The codec import has been correctly updated to use the new package path.

x/gov/migrations/v5/store.go (1)

7-7: Import change looks good

The codec import has been correctly updated to use the new package path, and the existing error handling is comprehensive.

x/authz/migrations/v2/store.go (1)

7-7: Import change looks good

The codec import has been correctly updated to use the new package path, and the existing error handling is comprehensive.

x/authz/module/module.go (2)

13-13: Import change looks good

The codec import has been correctly updated to use the new package path.


113-117: Error handling in DefaultGenesis looks good

The explicit error handling with panic is acceptable here as DefaultGenesis is a startup function, and failing fast is appropriate when the default genesis state cannot be marshaled.

x/gov/module.go (1)

13-13: LGTM: Import change aligns with migration objectives

The transition from github.com/cosmos/cosmos-sdk/codec to cosmossdk.io/core/codec is correctly implemented.

@julienrbrt julienrbrt requested a review from tac0turtle January 11, 2025 20:52
@julienrbrt
Copy link
Member

ref: #23253 (comment)

@julienrbrt julienrbrt added the backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release label Jan 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release C:Simulations C:x/authz C:x/gov
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants