-
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,9 @@ import ( | |
"bytes" | ||
"fmt" | ||
|
||
"cosmossdk.io/core/codec" | ||
"cosmossdk.io/x/distribution/types" | ||
|
||
"github.com/cosmos/cosmos-sdk/codec" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/kv" | ||
) | ||
|
@@ -18,47 +18,75 @@ func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string { | |
switch { | ||
case bytes.Equal(kvA.Key[:1], types.FeePoolKey): | ||
var feePoolA, feePoolB types.FeePool | ||
cdc.MustUnmarshal(kvA.Value, &feePoolA) | ||
cdc.MustUnmarshal(kvB.Value, &feePoolB) | ||
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) | ||
} | ||
Comment on lines
+21
to
+89
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
|
||
return fmt.Sprintf("%v\n%v", eventA, eventB) | ||
|
||
default: | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:MustMarshal
: