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

Speedup to UnmarshalBalanceCompat #544

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions x/bank/keeper/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,18 @@
}

// UnmarshalBalanceCompat unmarshal balance amount from storage, it's backward-compatible with the legacy format.
// This function is edited from the original function in x/bank/keeper/view.go, to remove one ValidateDenom call.
// It removes the NewCoin call, and constructs the object directly. It replicates the exact edge case behavior
// of upstream SDK v0.47.x. This is done to maintain compatibility with upstream.
// Should be removed when the next version of the SDK is released.
func UnmarshalBalanceCompat(cdc codec.BinaryCodec, bz []byte, denom string) (sdk.Coin, error) {
if err := sdk.ValidateDenom(denom); err != nil {
return sdk.Coin{}, err
}

amount := math.ZeroInt()
if bz == nil {
return sdk.NewCoin(denom, amount), nil
return sdk.Coin{Denom: denom, Amount: amount}, nil
}

if err := amount.Unmarshal(bz); err != nil {
Expand All @@ -271,5 +275,10 @@
return balance, nil
}

return sdk.NewCoin(denom, amount), nil
// replicate old behavior
if amount.IsNegative() {
panic(fmt.Errorf("negative coin amount: %v", amount))
Dismissed Show dismissed Hide dismissed
}

return sdk.Coin{Denom: denom, Amount: amount}, nil
}
Loading