-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Conversation
…com/cosmos/cosmos-sdk/codec
📝 WalkthroughWalkthroughThe pull request focuses on updating the codec import and error handling in the distribution module. The changes primarily involve replacing the codec import from Changes
Possibly related PRs
Suggested reviewers
Finishing Touches
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
Documentation and Community
|
There was a problem hiding this 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
📒 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 appropriateThe change from
MustMarshalJSON
to explicit error handling with panic is appropriate here because:
- This is a genesis function where panicking on error is acceptable
- It's consistent with other genesis-related functions in the file
- The error handling provides more context in case of failure
bz, err := cdc.Marshal(&gogotypes.BytesValue{Value: consAddr}) | ||
if err != nil { | ||
panic(err) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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:
- Return the error directly:
bz, err := cdc.Marshal(&gogotypes.BytesValue{Value: consAddr})
if err != nil {
- panic(err)
+ return err
}
- 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.
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) | ||
} |
There was a problem hiding this comment.
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:
- Reduce code duplication
- Make the code more maintainable
- Keep the same functionality
- Make it easier to modify error handling in the future if needed
Committable suggestion skipped: line range outside the PR's diff.
To merge into #23313 |
Description
Partially addresses: #23253
Summary by CodeRabbit
Dependency Updates
Error Handling Improvements
Migration Changes