From 66a588a2a2cf7e93722b12c331395be13b72c40b Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Fri, 23 Feb 2024 23:42:54 -0600 Subject: [PATCH] Speedup to UnmarshalBalanceCompat --- 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 }