Skip to content

Commit

Permalink
Speedup to UnmarshalBalanceCompat (#544) (#546)
Browse files Browse the repository at this point in the history
(cherry picked from commit a836ab6)

Co-authored-by: Dev Ojha <[email protected]>
  • Loading branch information
mergify[bot] and ValarDragon authored Feb 27, 2024
1 parent 11e2ddc commit 71e9830
Showing 1 changed file with 11 additions and 2 deletions.
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 @@ func (k BaseViewKeeper) getDenomAddressPrefixStore(ctx sdk.Context, denom string
}

// 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 @@ func UnmarshalBalanceCompat(cdc codec.BinaryCodec, bz []byte, denom string) (sdk
return balance, nil
}

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

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

0 comments on commit 71e9830

Please sign in to comment.