From 71e98307ccf5dcae0b49ec2a017318fc60f6619f Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Mon, 26 Feb 2024 18:45:49 -0600 Subject: [PATCH] Speedup to UnmarshalBalanceCompat (#544) (#546) (cherry picked from commit a836ab6f581555f34aa6aa0042d356a9b14a91bd) Co-authored-by: Dev Ojha --- x/bank/keeper/view.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/x/bank/keeper/view.go b/x/bank/keeper/view.go index bb0242612efc..9ff522cb02f0 100644 --- a/x/bank/keeper/view.go +++ b/x/bank/keeper/view.go @@ -251,6 +251,10 @@ 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 @@ -258,7 +262,7 @@ func UnmarshalBalanceCompat(cdc codec.BinaryCodec, bz []byte, denom string) (sdk 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 { @@ -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 }