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/distribution): use cosmossdk.io/core/codec instead of github.com/cosmos/cosmos-sdk/codec #23302

Closed
wants to merge 1 commit into from

Conversation

caseylove
Copy link
Contributor

@caseylove caseylove commented Jan 10, 2025

Description

Partially addresses: #23253

Summary by CodeRabbit

  • Dependency Updates

    • Updated codec library from Cosmos SDK to cosmossdk.io/core
  • Error Handling Improvements

    • Enhanced marshaling and unmarshaling error handling across distribution module
    • Added explicit error checks and panic mechanisms for codec operations
  • Migration Changes

    • Updated import statements and method implementations in migration files
    • Improved robustness of JSON and binary encoding processes

@caseylove caseylove requested review from alpe, JulianToledano and a team as code owners January 10, 2025 04:13
Copy link
Contributor

coderabbitai bot commented Jan 10, 2025

📝 Walkthrough

Walkthrough

The pull request focuses on updating the codec import and error handling in the distribution module. The changes primarily involve replacing the codec import from github.com/cosmos/cosmos-sdk/codec with cosmossdk.io/core/codec across multiple files. Additionally, the code transitions from using MustMarshal/MustUnmarshal methods to explicit error handling approaches, introducing more robust error checking mechanisms. These modifications affect the marshaling and unmarshaling processes in various functions within the distribution module.

Changes

File Change Summary
x/distribution/migrations/v4/migrate.go - Updated codec import to cosmossdk.io/core/codec
- Modified SetPreviousProposerConsAddr to use Marshal with error handling
x/distribution/module.go - Updated codec import to cosmossdk.io/core/codec
- Added error handling in DefaultGenesis method
x/distribution/simulation/decoder.go - Updated codec import to cosmossdk.io/core/codec
- Replaced MustUnmarshal with Unmarshal and added explicit error handling for multiple decoding scenarios

Possibly related PRs

Suggested reviewers

  • tac0turtle
  • alpe
  • lucaslopezf
  • kocubinski

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.

@github-actions github-actions bot added C:x/distribution distribution module related C:Simulations labels Jan 10, 2025
@julienrbrt julienrbrt added the backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release label Jan 10, 2025
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: 2

📜 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 dd775e1.

📒 Files selected for processing (3)
  • x/distribution/migrations/v4/migrate.go (2 hunks)
  • x/distribution/module.go (2 hunks)
  • x/distribution/simulation/decoder.go (2 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
x/distribution/module.go (1)

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

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

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

x/distribution/migrations/v4/migrate.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 (1)
  • GitHub Check: Summary
🔇 Additional comments (1)
x/distribution/module.go (1)

118-122: LGTM: Error handling in DefaultGenesis is appropriate

The change from MustMarshalJSON to explicit error handling with panic is appropriate here because:

  1. This is a genesis function where panicking on error is acceptable
  2. It's consistent with other genesis-related functions in the file
  3. The error handling provides more context in case of failure

Comment on lines +49 to +52
bz, err := cdc.Marshal(&gogotypes.BytesValue{Value: consAddr})
if err != nil {
panic(err)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Improve error handling pattern

The current implementation catches the error from Marshal only to immediately panic. This is inconsistent with the function's error return type and creates misleading API behavior. Consider either:

  1. Return the error directly:
 bz, err := cdc.Marshal(&gogotypes.BytesValue{Value: consAddr})
 if err != nil {
-    panic(err)
+    return err
 }
  1. Or if panicking is truly intended, use MustMarshal:
-bz, err := cdc.Marshal(&gogotypes.BytesValue{Value: consAddr})
-if err != nil {
-    panic(err)
-}
+bz := cdc.MustMarshal(&gogotypes.BytesValue{Value: consAddr})

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +21 to +89
if err := cdc.Unmarshal(kvA.Value, &feePoolA); err != nil {
panic(err)
}
if err := cdc.Unmarshal(kvB.Value, &feePoolB); err != nil {
panic(err)
}
return fmt.Sprintf("%v\n%v", feePoolA, feePoolB)

case bytes.Equal(kvA.Key[:1], types.ValidatorOutstandingRewardsPrefix):
var rewardsA, rewardsB types.ValidatorOutstandingRewards
cdc.MustUnmarshal(kvA.Value, &rewardsA)
cdc.MustUnmarshal(kvB.Value, &rewardsB)
if err := cdc.Unmarshal(kvA.Value, &rewardsA); err != nil {
panic(err)
}
if err := cdc.Unmarshal(kvB.Value, &rewardsB); err != nil {
panic(err)
}
return fmt.Sprintf("%v\n%v", rewardsA, rewardsB)

case bytes.Equal(kvA.Key[:1], types.DelegatorWithdrawAddrPrefix):
return fmt.Sprintf("%v\n%v", sdk.AccAddress(kvA.Value), sdk.AccAddress(kvB.Value))

case bytes.Equal(kvA.Key[:1], types.DelegatorStartingInfoPrefix):
var infoA, infoB types.DelegatorStartingInfo
cdc.MustUnmarshal(kvA.Value, &infoA)
cdc.MustUnmarshal(kvB.Value, &infoB)
if err := cdc.Unmarshal(kvA.Value, &infoA); err != nil {
panic(err)
}
if err := cdc.Unmarshal(kvB.Value, &infoB); err != nil {
panic(err)
}
return fmt.Sprintf("%v\n%v", infoA, infoB)

case bytes.Equal(kvA.Key[:1], types.ValidatorHistoricalRewardsPrefix):
var rewardsA, rewardsB types.ValidatorHistoricalRewards
cdc.MustUnmarshal(kvA.Value, &rewardsA)
cdc.MustUnmarshal(kvB.Value, &rewardsB)
if err := cdc.Unmarshal(kvA.Value, &rewardsA); err != nil {
panic(err)
}
if err := cdc.Unmarshal(kvB.Value, &rewardsB); err != nil {
panic(err)
}
return fmt.Sprintf("%v\n%v", rewardsA, rewardsB)

case bytes.Equal(kvA.Key[:1], types.ValidatorCurrentRewardsPrefix):
var rewardsA, rewardsB types.ValidatorCurrentRewards
cdc.MustUnmarshal(kvA.Value, &rewardsA)
cdc.MustUnmarshal(kvB.Value, &rewardsB)
if err := cdc.Unmarshal(kvA.Value, &rewardsA); err != nil {
panic(err)
}
if err := cdc.Unmarshal(kvB.Value, &rewardsB); err != nil {
panic(err)
}
return fmt.Sprintf("%v\n%v", rewardsA, rewardsB)

case bytes.Equal(kvA.Key[:1], types.ValidatorAccumulatedCommissionPrefix):
var commissionA, commissionB types.ValidatorAccumulatedCommission
cdc.MustUnmarshal(kvA.Value, &commissionA)
cdc.MustUnmarshal(kvB.Value, &commissionB)
if err := cdc.Unmarshal(kvA.Value, &commissionA); err != nil {
panic(err)
}
if err := cdc.Unmarshal(kvB.Value, &commissionB); err != nil {
panic(err)
}
return fmt.Sprintf("%v\n%v", commissionA, commissionB)

case bytes.Equal(kvA.Key[:1], types.ValidatorSlashEventPrefix):
var eventA, eventB types.ValidatorSlashEvent
cdc.MustUnmarshal(kvA.Value, &eventA)
cdc.MustUnmarshal(kvB.Value, &eventB)
if err := cdc.Unmarshal(kvA.Value, &eventA); err != nil {
panic(err)
}
if err := cdc.Unmarshal(kvB.Value, &eventB); err != nil {
panic(err)
}
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

Reduce code duplication in error handling

The current implementation repeats the same error handling pattern 14 times. Consider creating a helper function to reduce duplication:

+func mustUnmarshalWithContext[T any](cdc codec.Codec, bz []byte, out T) {
+    if err := cdc.Unmarshal(bz, out); err != nil {
+        panic(err)
+    }
+}

 func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string {
     return func(kvA, kvB kv.Pair) string {
         switch {
         case bytes.Equal(kvA.Key[:1], types.FeePoolKey):
             var feePoolA, feePoolB types.FeePool
-            if err := cdc.Unmarshal(kvA.Value, &feePoolA); err != nil {
-                panic(err)
-            }
-            if err := cdc.Unmarshal(kvB.Value, &feePoolB); err != nil {
-                panic(err)
-            }
+            mustUnmarshalWithContext(cdc, kvA.Value, &feePoolA)
+            mustUnmarshalWithContext(cdc, kvB.Value, &feePoolB)

This would:

  1. Reduce code duplication
  2. Make the code more maintainable
  3. Keep the same functionality
  4. Make it easier to modify error handling in the future if needed

Committable suggestion skipped: line range outside the PR's diff.

@julienrbrt
Copy link
Member

To merge into #23313

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/distribution distribution module related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants