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

fix: keep the balance query endpoint compatible with legacy blocks (backport #13301) #13318

Merged
merged 3 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
yihuang marked this conversation as resolved.
Show resolved Hide resolved
* (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))
yihuang marked this conversation as resolved.
Show resolved Hide resolved

### Bug Fixes

Expand Down
7 changes: 4 additions & 3 deletions x/bank/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
45 changes: 33 additions & 12 deletions x/bank/keeper/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
yihuang marked this conversation as resolved.
Show resolved Hide resolved
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
Expand All @@ -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
}
}
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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
}