Skip to content

Commit 67b9ee1

Browse files
yun-yeomergify-bot
authored andcommitted
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
1 parent 35e9e7a commit 67b9ee1

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,18 @@ Ref: https://keepachangelog.com/en/1.0.0/
3434

3535
# Changelog
3636

37+
<<<<<<< HEAD
3738
## Unreleased
39+
=======
40+
## [Unreleased]
41+
42+
* [\#9428](https://github.com/cosmos/cosmos-sdk/pull/9428) Optimize bank InitGenesis. Removed bank keeper's `k.setBalances` and `k.clearBalances`. Added `k.initBalances`.
43+
* [\#9231](https://github.com/cosmos/cosmos-sdk/pull/9231) Remove redundant staking errors.
44+
* [\#9205](https://github.com/cosmos/cosmos-sdk/pull/9205) Improve readability in `abci` handleQueryP2P
45+
* [\#9235](https://github.com/cosmos/cosmos-sdk/pull/9235) CreateMembershipProof/CreateNonMembershipProof now returns an error
46+
if input key is empty, or input data contains empty key.
47+
* [\#9314](https://github.com/cosmos/cosmos-sdk/pull/9314) Update Rosetta SDK to upstream's latest release.
48+
>>>>>>> 2ae787548 (fix: Bank module init genesis optimization (#9428))
3849
3950
### Bug Fixes
4051

x/bank/keeper/genesis.go

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

23+
<<<<<<< HEAD
2324
if err := k.SetBalances(ctx, addr, balance.Coins); err != nil {
25+
=======
26+
if err := k.initBalances(ctx, addr, balance.Coins); err != nil {
27+
>>>>>>> 2ae787548 (fix: Bank module init genesis optimization (#9428))
2428
panic(fmt.Errorf("error on setting balances %w", err))
2529
}
2630

x/bank/keeper/send.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ func (k BaseSendKeeper) AddCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.C
225225
return nil
226226
}
227227

228+
<<<<<<< HEAD
228229
// ClearBalances removes all balances for a given account by address.
229230
func (k BaseSendKeeper) ClearBalances(ctx sdk.Context, addr sdk.AccAddress) {
230231
keys := [][]byte{}
@@ -252,6 +253,22 @@ func (k BaseSendKeeper) SetBalances(ctx sdk.Context, addr sdk.AccAddress, balanc
252253
err := k.SetBalance(ctx, addr, balance)
253254
if err != nil {
254255
return err
256+
=======
257+
// initBalances sets the balance (multiple coins) for an account by address.
258+
// An error is returned upon failure.
259+
func (k BaseSendKeeper) initBalances(ctx sdk.Context, addr sdk.AccAddress, balances sdk.Coins) error {
260+
accountStore := k.getAccountStore(ctx, addr)
261+
for i := range balances {
262+
balance := balances[i]
263+
if !balance.IsValid() {
264+
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, balance.String())
265+
}
266+
267+
// Bank invariants require to not store zero balances.
268+
if !balance.IsZero() {
269+
bz := k.cdc.MustMarshal(&balance)
270+
accountStore.Set([]byte(balance.Denom), bz)
271+
>>>>>>> 2ae787548 (fix: Bank module init genesis optimization (#9428))
255272
}
256273
}
257274

0 commit comments

Comments
 (0)