From 7591bb56c30acb2fc84e30b9e30a1cdb45418e0f Mon Sep 17 00:00:00 2001 From: yihuang Date: Fri, 16 Sep 2022 15:57:42 +0800 Subject: [PATCH 1/3] fix: keep the balance query endpoint compatible with legacy blocks (#13301) * keep the balance query endpoint compatible with legacy blocks Closes: #13296 A temporary solution before the proxy tool developed, since the balance endpoint is too important. * Update CHANGELOG.md * Apply suggestions from code review Co-authored-by: Julien Robert Co-authored-by: Marko (cherry picked from commit 6c4f94b67146d9b664a1104593105f6c5c595080) # Conflicts: # CHANGELOG.md # x/bank/keeper/view.go --- CHANGELOG.md | 5 +++++ x/bank/keeper/grpc_query.go | 7 +++--- x/bank/keeper/view.go | 45 +++++++++++++++++++++++++++---------- 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 443bdfa9f0c1..8b7320aeea27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,9 +49,14 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements * [#13233](https://github.com/cosmos/cosmos-sdk/pull/13233) Add `--append` to `add-genesis-account` sub-command to append new tokens after an account is already created. +<<<<<<< HEAD * (x/group) [#13214](https://github.com/cosmos/cosmos-sdk/pull/13214) Add `withdraw-proposal` command to group module's CLI transaction commands. * (x/auth) [#13048](https://github.com/cosmos/cosmos-sdk/pull/13048) Add handling of AccountNumberStoreKeyPrefix to the simulation decoder. * (simapp) [#13108](https://github.com/cosmos/cosmos-sdk/pull/13108) Call `SetIAVLCacheSize` with the configured value in simapp. +======= +* [#13236](https://github.com/cosmos/cosmos-sdk/pull/13236) Integrate Filter Logging +* [#13301](https://github.com/cosmos/cosmos-sdk/pull/13301) Keep the balance query endpoint compatible with legacy blocks +>>>>>>> 6c4f94b67 (fix: keep the balance query endpoint compatible with legacy blocks (#13301)) ### Bug Fixes diff --git a/x/bank/keeper/grpc_query.go b/x/bank/keeper/grpc_query.go index 26efb3f1a7fe..b96bfa032e5e 100644 --- a/x/bank/keeper/grpc_query.go +++ b/x/bank/keeper/grpc_query.go @@ -61,11 +61,12 @@ func (k BaseKeeper) AllBalances(ctx context.Context, req *types.QueryAllBalances accountStore := k.getAccountStore(sdkCtx, addr) pageRes, err := query.Paginate(accountStore, req.Pagination, func(key, value []byte) error { - var amount math.Int - if err := amount.Unmarshal(value); err != nil { + denom := string(key) + balance, err := UnmarshalBalanceCompat(k.cdc, value, denom) + if err != nil { return err } - balances = append(balances, sdk.NewCoin(string(key), amount)) + balances = append(balances, balance) return nil }) if err != nil { diff --git a/x/bank/keeper/view.go b/x/bank/keeper/view.go index 5fc3446c2da5..125a4206133e 100644 --- a/x/bank/keeper/view.go +++ b/x/bank/keeper/view.go @@ -99,17 +99,17 @@ func (k BaseViewKeeper) GetAccountsBalances(ctx sdk.Context) []types.Balance { // by address. func (k BaseViewKeeper) GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin { accountStore := k.getAccountStore(ctx, addr) +<<<<<<< HEAD amount := sdk.ZeroInt() +======= +>>>>>>> 6c4f94b67 (fix: keep the balance query endpoint compatible with legacy blocks (#13301)) bz := accountStore.Get([]byte(denom)) - if bz == nil { - return sdk.NewCoin(denom, amount) - } - - if err := amount.Unmarshal(bz); err != nil { + balance, err := UnmarshalBalanceCompat(k.cdc, bz, denom) + if err != nil { panic(err) } - return sdk.NewCoin(denom, amount) + return balance } // IterateAccountBalances iterates over the balances of a single account and @@ -122,12 +122,13 @@ func (k BaseViewKeeper) IterateAccountBalances(ctx sdk.Context, addr sdk.AccAddr defer iterator.Close() for ; iterator.Valid(); iterator.Next() { - var amount math.Int - if err := amount.Unmarshal(iterator.Value()); err != nil { + denom := string(iterator.Key()) + balance, err := UnmarshalBalanceCompat(k.cdc, iterator.Value(), denom) + if err != nil { panic(err) } - if cb(sdk.NewCoin(string(iterator.Key()), amount)) { + if cb(balance) { break } } @@ -152,12 +153,12 @@ func (k BaseViewKeeper) IterateAllBalances(ctx sdk.Context, cb func(sdk.AccAddre panic(err) } - var amount math.Int - if err := amount.Unmarshal(iterator.Value()); err != nil { + balance, err := UnmarshalBalanceCompat(k.cdc, iterator.Value(), denom) + if err != nil { panic(err) } - if cb(address, sdk.NewCoin(denom, amount)) { + if cb(address, balance) { break } } @@ -242,3 +243,23 @@ func (k BaseViewKeeper) getAccountStore(ctx sdk.Context, addr sdk.AccAddress) pr func (k BaseViewKeeper) getDenomAddressPrefixStore(ctx sdk.Context, denom string) prefix.Store { return prefix.NewStore(ctx.KVStore(k.storeKey), types.CreateDenomAddressPrefix(denom)) } + +// UnmarshalBalanceCompat unmarshal balance amount from storage, it's backward-compatible with the legacy format. +func UnmarshalBalanceCompat(cdc codec.BinaryCodec, bz []byte, denom string) (sdk.Coin, error) { + amount := math.ZeroInt() + if bz == nil { + return sdk.NewCoin(denom, amount), nil + } + + if err := amount.Unmarshal(bz); err != nil { + // try to unmarshal with the legacy format. + var balance sdk.Coin + if cdc.Unmarshal(bz, &balance) != nil { + // return with the original error + return sdk.Coin{}, err + } + return balance, nil + } + + return sdk.NewCoin(denom, amount), nil +} From 74600b509d541ad0865f9f2958bd576cf3797696 Mon Sep 17 00:00:00 2001 From: yihuang Date: Fri, 16 Sep 2022 16:02:23 +0800 Subject: [PATCH 2/3] Apply suggestions from code review --- CHANGELOG.md | 6 +----- x/bank/keeper/view.go | 4 ---- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b7320aeea27..1032f3b13184 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,14 +49,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements * [#13233](https://github.com/cosmos/cosmos-sdk/pull/13233) Add `--append` to `add-genesis-account` sub-command to append new tokens after an account is already created. -<<<<<<< HEAD * (x/group) [#13214](https://github.com/cosmos/cosmos-sdk/pull/13214) Add `withdraw-proposal` command to group module's CLI transaction commands. * (x/auth) [#13048](https://github.com/cosmos/cosmos-sdk/pull/13048) Add handling of AccountNumberStoreKeyPrefix to the simulation decoder. * (simapp) [#13108](https://github.com/cosmos/cosmos-sdk/pull/13108) Call `SetIAVLCacheSize` with the configured value in simapp. -======= -* [#13236](https://github.com/cosmos/cosmos-sdk/pull/13236) Integrate Filter Logging -* [#13301](https://github.com/cosmos/cosmos-sdk/pull/13301) Keep the balance query endpoint compatible with legacy blocks ->>>>>>> 6c4f94b67 (fix: keep the balance query endpoint compatible with legacy blocks (#13301)) +* [#13318](https://github.com/cosmos/cosmos-sdk/pull/13318) Keep the balance query endpoint compatible with legacy blocks ### Bug Fixes diff --git a/x/bank/keeper/view.go b/x/bank/keeper/view.go index 125a4206133e..e189edca1de3 100644 --- a/x/bank/keeper/view.go +++ b/x/bank/keeper/view.go @@ -99,10 +99,6 @@ func (k BaseViewKeeper) GetAccountsBalances(ctx sdk.Context) []types.Balance { // by address. func (k BaseViewKeeper) GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin { accountStore := k.getAccountStore(ctx, addr) -<<<<<<< HEAD - amount := sdk.ZeroInt() -======= ->>>>>>> 6c4f94b67 (fix: keep the balance query endpoint compatible with legacy blocks (#13301)) bz := accountStore.Get([]byte(denom)) balance, err := UnmarshalBalanceCompat(k.cdc, bz, denom) if err != nil { From 6d1334e90b13fe5a1670349498f1075de6764c1a Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Fri, 16 Sep 2022 12:13:19 +0200 Subject: [PATCH 3/3] fix lint --- x/bank/keeper/grpc_query.go | 1 - 1 file changed, 1 deletion(-) diff --git a/x/bank/keeper/grpc_query.go b/x/bank/keeper/grpc_query.go index b96bfa032e5e..a96ade989c9f 100644 --- a/x/bank/keeper/grpc_query.go +++ b/x/bank/keeper/grpc_query.go @@ -3,7 +3,6 @@ package keeper import ( "context" - "cosmossdk.io/math" "google.golang.org/grpc/codes" "google.golang.org/grpc/status"