Skip to content

Commit

Permalink
fix: Bank module init genesis optimization (#9428)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
yun-yeo authored and mergify-bot committed Jun 1, 2021
1 parent 7075c49 commit 5da8a99
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,18 @@ Ref: https://keepachangelog.com/en/1.0.0/

# Changelog

<<<<<<< HEAD
## Unreleased
=======
## [Unreleased]

* [\#9428](https://github.com/cosmos/cosmos-sdk/pull/9428) Optimize bank InitGenesis. Removed bank keeper's `k.setBalances` and `k.clearBalances`. Added `k.initBalances`.
* [\#9231](https://github.com/cosmos/cosmos-sdk/pull/9231) Remove redundant staking errors.
* [\#9205](https://github.com/cosmos/cosmos-sdk/pull/9205) Improve readability in `abci` handleQueryP2P
* [\#9235](https://github.com/cosmos/cosmos-sdk/pull/9235) CreateMembershipProof/CreateNonMembershipProof now returns an error
if input key is empty, or input data contains empty key.
* [\#9314](https://github.com/cosmos/cosmos-sdk/pull/9314) Update Rosetta SDK to upstream's latest release.
>>>>>>> 2ae787548 (fix: Bank module init genesis optimization (#9428))
### Features

Expand Down
4 changes: 4 additions & 0 deletions x/bank/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ func (k BaseKeeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) {
panic(err)
}

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

Expand Down
17 changes: 17 additions & 0 deletions x/bank/keeper/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ func (k BaseSendKeeper) AddCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.C
return nil
}

<<<<<<< HEAD
// ClearBalances removes all balances for a given account by address.
func (k BaseSendKeeper) ClearBalances(ctx sdk.Context, addr sdk.AccAddress) {
keys := [][]byte{}
Expand Down Expand Up @@ -252,6 +253,22 @@ func (k BaseSendKeeper) SetBalances(ctx sdk.Context, addr sdk.AccAddress, balanc
err := k.SetBalance(ctx, addr, balance)
if err != nil {
return err
=======
// initBalances sets the balance (multiple coins) for an account by address.
// An error is returned upon failure.
func (k BaseSendKeeper) initBalances(ctx sdk.Context, addr sdk.AccAddress, balances sdk.Coins) error {
accountStore := k.getAccountStore(ctx, addr)
for i := range balances {
balance := balances[i]
if !balance.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, balance.String())
}

// Bank invariants require to not store zero balances.
if !balance.IsZero() {
bz := k.cdc.MustMarshal(&balance)
accountStore.Set([]byte(balance.Denom), bz)
>>>>>>> 2ae787548 (fix: Bank module init genesis optimization (#9428))
}
}

Expand Down

0 comments on commit 5da8a99

Please sign in to comment.