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

perf: x/bank create reverse prefix for denom<->address #9611

Merged
merged 32 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
72ada0e
x/bank: create reverse prefix for denom<->address
alexanderbez Jun 29, 2021
9b5f92e
x/bank: update DenomOwners
alexanderbez Jun 29, 2021
cf595e2
x/bank: lint++
alexanderbez Jun 29, 2021
72a1ad1
x/bank: store 0 instead of balance
alexanderbez Jun 30, 2021
01e476d
Update x/bank/types/key.go
alexanderbez Jun 30, 2021
567f228
x/bank: update initBalances
alexanderbez Jun 30, 2021
e3cd0e7
x/bank: update spec
alexanderbez Jun 30, 2021
7312e5b
x/bank: add v0.44 legacy migration
alexanderbez Jun 30, 2021
2f00b31
x/bank: bump ConsensusVersion to 3
alexanderbez Jun 30, 2021
67db855
x/bank: update RegisterServices
alexanderbez Jul 1, 2021
ff041be
x/bank: fix legacy types
alexanderbez Jul 1, 2021
c3ed09f
x/bank: add TestMigrateStore
alexanderbez Jul 1, 2021
c1b4d82
Merge branch 'master' into bez/9590-x-bank-denom-reverse-idx
alexanderbez Jul 1, 2021
313d902
fix TestRunMigrations
amaury1093 Jul 5, 2021
56e11bd
Update x/bank/types/key.go
alexanderbez Jul 5, 2021
54eac65
x/bank: improve naming
alexanderbez Jul 5, 2021
afeffcc
x/bank: add Has check
alexanderbez Jul 5, 2021
2cd4fe8
Merge branch 'master' into bez/9590-x-bank-denom-reverse-idx
alexanderbez Jul 5, 2021
b2bc4f6
Merge branch 'master' into bez/9590-x-bank-denom-reverse-idx
alexanderbez Jul 6, 2021
e2073ac
Merge branch 'master' into bez/9590-x-bank-denom-reverse-idx
alexanderbez Jul 6, 2021
549a022
Merge branch 'master' into bez/9590-x-bank-denom-reverse-idx
alexanderbez Jul 7, 2021
6cccd68
Merge branch 'master' into bez/9590-x-bank-denom-reverse-idx
alexanderbez Jul 11, 2021
4aa6bac
attempt fix TestSignWithMultiSignersAminoJSON
alexanderbez Jul 11, 2021
fef3b67
attempt fix TestSignWithMultiSignersAminoJSON
alexanderbez Jul 12, 2021
83b7019
attempt fix TestNewRedelegateCmd
alexanderbez Jul 12, 2021
5cb8538
Merge branch 'master' into bez/9590-x-bank-denom-reverse-idx
alexanderbez Jul 12, 2021
67b5960
Merge branch 'master' into bez/9590-x-bank-denom-reverse-idx
alexanderbez Jul 18, 2021
f3aeb56
fix build
alexanderbez Jul 18, 2021
9a851f9
Fix delegation query test
amaury1093 Jul 26, 2021
5ada740
Merge branch 'master' into bez/9590-x-bank-denom-reverse-idx
alexanderbez Jul 26, 2021
290e1d7
ci++
alexanderbez Jul 26, 2021
a1fa863
Merge branch 'master' into bez/9590-x-bank-denom-reverse-idx
alexanderbez Jul 26, 2021
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
17 changes: 3 additions & 14 deletions x/bank/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,24 +179,13 @@ func (k BaseKeeper) DenomOwners(
}

ctx := sdk.UnwrapSDKContext(goCtx)

store := ctx.KVStore(k.storeKey)
balancesStore := prefix.NewStore(store, types.BalancesPrefix)
denomPrefixStore := k.getDenomPrefixStore(ctx, req.Denom)

var denomOwners []*types.DenomOwner
pageRes, err := query.FilteredPaginate(
balancesStore,
denomPrefixStore,
req.Pagination,
func(key []byte, value []byte, accumulate bool) (bool, error) {
var balance sdk.Coin
if err := k.cdc.Unmarshal(value, &balance); err != nil {
return false, err
}

if req.Denom != balance.Denom {
return false, nil
}

if accumulate {
address, err := types.AddressFromBalancesStore(key)
if err != nil {
Expand All @@ -207,7 +196,7 @@ func (k BaseKeeper) DenomOwners(
denomOwners,
&types.DenomOwner{
Address: address.String(),
Balance: balance,
Balance: k.GetBalance(ctx, address, req.Denom),
},
)
}
Expand Down
9 changes: 8 additions & 1 deletion x/bank/keeper/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/bank/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
Expand Down Expand Up @@ -254,13 +255,19 @@ func (k BaseSendKeeper) setBalance(ctx sdk.Context, addr sdk.AccAddress, balance
}

accountStore := k.getAccountStore(ctx, addr)
denomPrefixStore := k.getDenomPrefixStore(ctx, balance.Denom)

// Bank invariants require to not store zero balances.
// x/bank invariants prohibit persistence of zero balances
if balance.IsZero() {
accountStore.Delete([]byte(balance.Denom))
denomPrefixStore.Delete(address.MustLengthPrefix(addr))
} else {
bz := k.cdc.MustMarshal(&balance)
accountStore.Set([]byte(balance.Denom), bz)

// Store a reverse index from denomination to account address with a
// sentinel value.
denomPrefixStore.Set(address.MustLengthPrefix(addr), []byte{0})
}

return nil
Expand Down
7 changes: 6 additions & 1 deletion x/bank/keeper/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ func (k BaseViewKeeper) ValidateBalance(ctx sdk.Context, addr sdk.AccAddress) er
// getAccountStore gets the account store of the given address.
func (k BaseViewKeeper) getAccountStore(ctx sdk.Context, addr sdk.AccAddress) prefix.Store {
store := ctx.KVStore(k.storeKey)

return prefix.NewStore(store, types.CreateAccountBalancesPrefix(addr))
}

// getDenomPrefixStore returns a prefix store that acts as a reverse index
// between a denomination and account balance for that denomination.
func (k BaseViewKeeper) getDenomPrefixStore(ctx sdk.Context, denom string) prefix.Store {
return prefix.NewStore(ctx.KVStore(k.storeKey), types.CreateDenomPrefix(denom))
}
10 changes: 10 additions & 0 deletions x/bank/types/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
var (
// BalancesPrefix is the prefix for the account balances store. We use a byte
// (instead of `[]byte("balances")` to save some disk space).
DenomPrefix = []byte{0x03}
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
BalancesPrefix = []byte{0x02}
SupplyKey = []byte{0x00}
DenomMetadataPrefix = []byte{0x1}
Expand All @@ -43,15 +44,24 @@ func AddressFromBalancesStore(key []byte) (sdk.AccAddress, error) {
if len(key) == 0 {
return nil, ErrInvalidKey
}

addrLen := key[0]
bound := int(addrLen)

if len(key)-1 < bound {
return nil, ErrInvalidKey
}

return key[1 : bound+1], nil
}

// CreateAccountBalancesPrefix creates the prefix for an account's balances.
func CreateAccountBalancesPrefix(addr []byte) []byte {
return append(BalancesPrefix, address.MustLengthPrefix(addr)...)
}

// CreateDenomPrefix creates a prefix for a reverse index of denomination to
// account balance for that denomination.
func CreateDenomPrefix(denom string) []byte {
return append(DenomPrefix, []byte(denom)...)
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
}