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

fix: Bank module init genesis optimization #9428

Merged
merged 5 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
2 changes: 1 addition & 1 deletion x/bank/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (k BaseKeeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) {
panic(err)
}

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

Expand Down
19 changes: 19 additions & 0 deletions x/bank/keeper/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,25 @@ func (k BaseSendKeeper) clearBalances(ctx sdk.Context, addr sdk.AccAddress) {
}
}

// 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 _, balance := range balances {
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)
}
}

return nil
}

tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
// setBalances sets the balance (multiple coins) for an account by address. It will
// clear out all balances prior to setting the new coins as to set existing balances
// to zero if they don't exist in amt. An error is returned upon failure.
Expand Down