Skip to content

Commit 2ae7875

Browse files
yun-yeoaaronc
andauthored
fix: Bank module init genesis optimization (#9428)
* optimize the bank module genesis initialization * remove k.setBalances & k.clearBalances and update changelog * fix lint Co-authored-by: Aaron Craelius <aaron@regen.network>
1 parent da87ab0 commit 2ae7875

File tree

3 files changed

+14
-24
lines changed

3 files changed

+14
-24
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
3636

3737
## [Unreleased]
3838

39+
* [\#9428](https://github.com/cosmos/cosmos-sdk/pull/9428) Optimize bank InitGenesis. Removed bank keeper's `k.setBalances` and `k.clearBalances`. Added `k.initBalances`.
3940
* [\#9231](https://github.com/cosmos/cosmos-sdk/pull/9231) Remove redundant staking errors.
4041
* [\#9205](https://github.com/cosmos/cosmos-sdk/pull/9205) Improve readability in `abci` handleQueryP2P
4142
* [\#9235](https://github.com/cosmos/cosmos-sdk/pull/9235) CreateMembershipProof/CreateNonMembershipProof now returns an error

x/bank/keeper/genesis.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (k BaseKeeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) {
2121
panic(err)
2222
}
2323

24-
if err := k.setBalances(ctx, addr, balance.Coins); err != nil {
24+
if err := k.initBalances(ctx, addr, balance.Coins); err != nil {
2525
panic(fmt.Errorf("error on setting balances %w", err))
2626
}
2727

x/bank/keeper/send.go

+12-23
Original file line numberDiff line numberDiff line change
@@ -227,31 +227,20 @@ func (k BaseSendKeeper) addCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.C
227227
return nil
228228
}
229229

230-
// clearBalances removes all balances for a given account by address.
231-
func (k BaseSendKeeper) clearBalances(ctx sdk.Context, addr sdk.AccAddress) {
232-
keys := [][]byte{}
233-
k.IterateAccountBalances(ctx, addr, func(balance sdk.Coin) bool {
234-
keys = append(keys, []byte(balance.Denom))
235-
return false
236-
})
237-
230+
// initBalances sets the balance (multiple coins) for an account by address.
231+
// An error is returned upon failure.
232+
func (k BaseSendKeeper) initBalances(ctx sdk.Context, addr sdk.AccAddress, balances sdk.Coins) error {
238233
accountStore := k.getAccountStore(ctx, addr)
234+
for i := range balances {
235+
balance := balances[i]
236+
if !balance.IsValid() {
237+
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, balance.String())
238+
}
239239

240-
for _, key := range keys {
241-
accountStore.Delete(key)
242-
}
243-
}
244-
245-
// setBalances sets the balance (multiple coins) for an account by address. It will
246-
// clear out all balances prior to setting the new coins as to set existing balances
247-
// to zero if they don't exist in amt. An error is returned upon failure.
248-
func (k BaseSendKeeper) setBalances(ctx sdk.Context, addr sdk.AccAddress, balances sdk.Coins) error {
249-
k.clearBalances(ctx, addr)
250-
251-
for _, balance := range balances {
252-
err := k.setBalance(ctx, addr, balance)
253-
if err != nil {
254-
return err
240+
// Bank invariants require to not store zero balances.
241+
if !balance.IsZero() {
242+
bz := k.cdc.MustMarshal(&balance)
243+
accountStore.Set([]byte(balance.Denom), bz)
255244
}
256245
}
257246

0 commit comments

Comments
 (0)