Skip to content

Commit a2a56c3

Browse files
mergify[bot]yun-yeoamaury1093
authored
fix: Bank module init genesis optimization (backport #9428) (#9440)
* 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> (cherry picked from commit 2ae7875) # Conflicts: # CHANGELOG.md # x/bank/keeper/genesis.go # x/bank/keeper/send.go * fix conflicts * Update CHANGELOG.md Co-authored-by: yys <sw.yunsuk@gmail.com> Co-authored-by: Amaury M <1293565+amaurym@users.noreply.github.com>
1 parent d773c88 commit a2a56c3

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
3636

3737
## Unreleased
3838

39+
### Improvements
40+
41+
* [\#9428](https://github.com/cosmos/cosmos-sdk/pull/9428) Optimize bank InitGenesis. Added `k.initBalances`.
42+
3943
### Bug Fixes
4044

4145
* [\#9401](https://github.com/cosmos/cosmos-sdk/pull/9401) Fixes incorrect export of IBC identifier sequences. Previously, the next identifier sequence for clients/connections/channels was not set during genesis export. This resulted in the next identifiers being generated on the new chain to reuse old identifiers (the sequences began again from 0).

x/bank/keeper/genesis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (k BaseKeeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) {
2020
panic(err)
2121
}
2222

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

x/bank/keeper/send.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,28 @@ func (k BaseSendKeeper) SetBalances(ctx sdk.Context, addr sdk.AccAddress, balanc
258258
return nil
259259
}
260260

261+
// initBalances sets the balance (multiple coins) for an account by address.
262+
// An error is returned upon failure.
263+
func (k BaseSendKeeper) initBalances(ctx sdk.Context, addr sdk.AccAddress, balances sdk.Coins) error {
264+
store := ctx.KVStore(k.storeKey)
265+
balancesStore := prefix.NewStore(store, types.BalancesPrefix)
266+
accountStore := prefix.NewStore(balancesStore, addr.Bytes())
267+
for i := range balances {
268+
balance := balances[i]
269+
if !balance.IsValid() {
270+
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, balance.String())
271+
}
272+
273+
// Bank invariants require to not store zero balances.
274+
if !balance.IsZero() {
275+
bz := k.cdc.MustMarshalBinaryBare(&balance)
276+
accountStore.Set([]byte(balance.Denom), bz)
277+
}
278+
}
279+
280+
return nil
281+
}
282+
261283
// SetBalance sets the coin balance for an account by address.
262284
func (k BaseSendKeeper) SetBalance(ctx sdk.Context, addr sdk.AccAddress, balance sdk.Coin) error {
263285
if !balance.IsValid() {

0 commit comments

Comments
 (0)