Skip to content

Commit c637830

Browse files
jgimenofdymyljasahith-narahariAlessio Tregliarobert-zaremba
authored
[Bank] Remove the unsafe balance changing API (cosmos#8473)
* temp commit * setbalance now is internal * remove set balances in genesis * feedback test commit * update tests * fix: genesis panic message * fix not bonded pool * fix(staking): genesis test * fix(simapp): rollback state fix change * fix(staking): genesis large val set test * [Bank Refactor] Frojdi jonathan/remove setsupply (cosmos#8491) * init supply in a different way * remove external usage of set supply * change(staking): replace SetSupply with MintCoins in tests * change(evidence): replace SetSupply with MintCoins in tests * change(crisis): remove SetSupply in tests * change(bank): remove set supply from genesis tests * change(bank): remove set supply from keeper tests * change(bank): remove remaining set supply usage from keeper tests * change(bank): remove set supply usage from grpc query and querier tests * change(bank): remove SetSupply from keeper interface Co-authored-by: Frojdi Dymylja <frojdi.dymylja@gmail.com> * remove setbalances from genesis in gov * remove keyring * add init genesis state * change(staking): make genesis checks coherent and add tests * remove setbalances on distribution * fix(staking): genesis tests * [Bank Refactor]: Remove SetBalances usage from the code and tests (cosmos#8509) * change(distribution): remove SetBalances usage from keeper tests * add(simapp): FundAccount utility function * chore(staking): use FundAccount in keeper tests * change(staking): remove usage of SetBalance in allocation tests * change(staking): remove usage of SetBalance in delegation tests * change(staking): remove usage of SetBalance in proposal handler tests * change(staking): remove usage of SetBalances in grpc query tests * change(staking): remove usage of SetBalances in operations tests * change(distribution): remove usage of SetBalances in genesis * change(authz): remove usage of SetBalances keeper and operations test * fix(authz): TestKeeperFees failing test * change(slashing): remove SetBalances from expected BankKeeper * change(slashing): remove usage of SetBalances in tests * change(distribution): remove SetBalances from expected BankKeeper * change(genutil): remove usage of SetBalances from tests * change(gov): remove SetBalances from expected BankKeeper * change(gov): remove usage of SetBalances from tests * change(staking): remove usage of SetBalances from slash tests * change(staking): remove SetBalances from expected BankKeeper * change(staking): remove usage of SetBalances from delegation tests * change(staking): remove usage of SetBalances from operations tests * change(staking): remove usage of SetBalances from validator tests * change(bank): remove usage of SetBalances from app tests * change(bank): remove usage of SetBalances from bench tests * change(bank): remove usage of SetBalances from querier tests * change(bank): remove usage of SetBalances from grpc query tests * change(bank): remove usage of SetBalances from operations tests * change(bank): partially remove usage of SetBalances from keeper tests * change(bank): finalize removal of usage of SetBalances from keeper tests * change(auth): remove usage of SetBalances from verify tests * change(auth): partially remove usage of SetBalances from tests * [Bank refactor]: finalize removal of setbalances from auth (cosmos#8527) * add tests with is check tx * temp commit * fix test * fix other test and remove setbalances * change(auth): remove usage of SetBalances is vesting tests Co-authored-by: Jonathan Gimeno <jgimeno@gmail.com> * change(types): remove usage of SetBalances in queries * fix(types): pagination tests * [Bank refactor] fix pagination tests (cosmos#8550) * fix tests * lint * change(bank): remove SetBalances from keeper public API Co-authored-by: Jonathan Gimeno <jgimeno@gmail.com> Co-authored-by: SaReN <sahithnarahari@gmail.com> * change(bank): remove SubtractCoins from keeper public API * change(ibc/transfer): remove AddCoins from relay tests * change(bank): remove AddCoins from public keeper API * fix imports * remove set balances * fix fee test * remove set balances * fix(staking): remove dependency on minter authorization for staking pools * chore: update CHANGELOG.md * update: x/distribution/keeper/keeper_test.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * Update simapp/test_helpers.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * Update x/staking/genesis_test.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * fix(simapp): FundAccount amount variable name * fix some PR issues Co-authored-by: Frojdi Dymylja <frojdi.dymylja@gmail.com> Co-authored-by: Frojdi Dymylja <33157909+fdymylja@users.noreply.github.com> Co-authored-by: SaReN <sahithnarahari@gmail.com> Co-authored-by: Alessio Treglia <alessio@tendermint.com> Co-authored-by: Robert Zaremba <robert@zaremba.ch>
1 parent 3929db6 commit c637830

File tree

2 files changed

+81
-21
lines changed

2 files changed

+81
-21
lines changed

state.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ import (
1414
"github.com/cosmos/cosmos-sdk/codec"
1515
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
1616
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
17+
sdk "github.com/cosmos/cosmos-sdk/types"
1718
"github.com/cosmos/cosmos-sdk/types/module"
1819
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
1920
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
21+
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
22+
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
2023
)
2124

2225
// AppStateFn returns the initial application state using a genesis or the simulation parameters.
@@ -68,6 +71,57 @@ func AppStateFn(cdc codec.JSONMarshaler, simManager *module.SimulationManager) s
6871
appState, simAccs = AppStateRandomizedFn(simManager, r, cdc, accs, genesisTimestamp, appParams)
6972
}
7073

74+
rawState := make(map[string]json.RawMessage)
75+
err := json.Unmarshal(appState, &rawState)
76+
if err != nil {
77+
panic(err)
78+
}
79+
80+
stakingStateBz, ok := rawState[stakingtypes.ModuleName]
81+
if !ok {
82+
panic("staking genesis state is missing")
83+
}
84+
85+
stakingState := new(stakingtypes.GenesisState)
86+
err = cdc.UnmarshalJSON(stakingStateBz, stakingState)
87+
if err != nil {
88+
panic(err)
89+
}
90+
// compute not bonded balance
91+
notBondedTokens := sdk.ZeroInt()
92+
for _, val := range stakingState.Validators {
93+
if val.Status != stakingtypes.Unbonded {
94+
continue
95+
}
96+
notBondedTokens = notBondedTokens.Add(val.GetTokens())
97+
}
98+
notBondedCoins := sdk.NewCoin(stakingState.Params.BondDenom, notBondedTokens)
99+
// edit bank state to make it have the not bonded pool tokens
100+
bankStateBz, ok := rawState[banktypes.ModuleName]
101+
// TODO(fdymylja/jonathan): should we panic in this case
102+
if !ok {
103+
panic("bank genesis state is missing")
104+
}
105+
bankState := new(banktypes.GenesisState)
106+
err = cdc.UnmarshalJSON(bankStateBz, bankState)
107+
if err != nil {
108+
panic(err)
109+
}
110+
111+
bankState.Balances = append(bankState.Balances, banktypes.Balance{
112+
Address: authtypes.NewModuleAddress(stakingtypes.NotBondedPoolName).String(),
113+
Coins: sdk.NewCoins(notBondedCoins),
114+
})
115+
116+
// change appState back
117+
rawState[stakingtypes.ModuleName] = cdc.MustMarshalJSON(stakingState)
118+
rawState[banktypes.ModuleName] = cdc.MustMarshalJSON(bankState)
119+
120+
// replace appstate
121+
appState, err = json.Marshal(rawState)
122+
if err != nil {
123+
panic(err)
124+
}
71125
return appState, simAccs, chainID, genesisTimestamp
72126
}
73127
}

test_helpers.go

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/cosmos/cosmos-sdk/types/errors"
2828
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
2929
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
30+
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
3031
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
3132
)
3233

@@ -119,7 +120,6 @@ func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs
119120
delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), sdk.OneDec()))
120121

121122
}
122-
123123
// set validators and delegations
124124
stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations)
125125
genesisState[stakingtypes.ModuleName] = app.AppCodec().MustMarshalJSON(stakingGenesis)
@@ -130,6 +130,12 @@ func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs
130130
totalSupply = totalSupply.Add(b.Coins.Add(sdk.NewCoin(sdk.DefaultBondDenom, bondAmt))...)
131131
}
132132

133+
// add bonded amount to bonded pool module account
134+
balances = append(balances, banktypes.Balance{
135+
Address: authtypes.NewModuleAddress(stakingtypes.BondedPoolName).String(),
136+
Coins: sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, bondAmt)},
137+
})
138+
133139
// update total supply
134140
bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{})
135141
genesisState[banktypes.ModuleName] = app.AppCodec().MustMarshalJSON(bankGenesis)
@@ -231,21 +237,11 @@ func createIncrementalAccounts(accNum int) []sdk.AccAddress {
231237
func AddTestAddrsFromPubKeys(app *SimApp, ctx sdk.Context, pubKeys []cryptotypes.PubKey, accAmt sdk.Int) {
232238
initCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), accAmt))
233239

234-
setTotalSupply(app, ctx, accAmt, len(pubKeys))
235-
236-
// fill all the addresses with some coins, set the loose pool tokens simultaneously
237-
for _, pubKey := range pubKeys {
238-
saveAccount(app, ctx, sdk.AccAddress(pubKey.Address()), initCoins)
240+
for _, pk := range pubKeys {
241+
initAccountWithCoins(app, ctx, sdk.AccAddress(pk.Address()), initCoins)
239242
}
240243
}
241244

242-
// setTotalSupply provides the total supply based on accAmt * totalAccounts.
243-
func setTotalSupply(app *SimApp, ctx sdk.Context, accAmt sdk.Int, totalAccounts int) {
244-
totalSupply := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), accAmt.MulRaw(int64(totalAccounts))))
245-
prevSupply := app.BankKeeper.GetSupply(ctx)
246-
app.BankKeeper.SetSupply(ctx, banktypes.NewSupply(prevSupply.GetTotal().Add(totalSupply...)))
247-
}
248-
249245
// AddTestAddrs constructs and returns accNum amount of accounts with an
250246
// initial balance of accAmt in random order
251247
func AddTestAddrs(app *SimApp, ctx sdk.Context, accNum int, accAmt sdk.Int) []sdk.AccAddress {
@@ -262,21 +258,21 @@ func addTestAddrs(app *SimApp, ctx sdk.Context, accNum int, accAmt sdk.Int, stra
262258
testAddrs := strategy(accNum)
263259

264260
initCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), accAmt))
265-
setTotalSupply(app, ctx, accAmt, accNum)
266261

267-
// fill all the addresses with some coins, set the loose pool tokens simultaneously
268262
for _, addr := range testAddrs {
269-
saveAccount(app, ctx, addr, initCoins)
263+
initAccountWithCoins(app, ctx, addr, initCoins)
270264
}
271265

272266
return testAddrs
273267
}
274268

275-
// saveAccount saves the provided account into the simapp with balance based on initCoins.
276-
func saveAccount(app *SimApp, ctx sdk.Context, addr sdk.AccAddress, initCoins sdk.Coins) {
277-
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr)
278-
app.AccountKeeper.SetAccount(ctx, acc)
279-
err := app.BankKeeper.AddCoins(ctx, addr, initCoins)
269+
func initAccountWithCoins(app *SimApp, ctx sdk.Context, addr sdk.AccAddress, coins sdk.Coins) {
270+
err := app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, coins)
271+
if err != nil {
272+
panic(err)
273+
}
274+
275+
err = app.BankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr, coins)
280276
if err != nil {
281277
panic(err)
282278
}
@@ -440,3 +436,13 @@ type EmptyAppOptions struct{}
440436
func (ao EmptyAppOptions) Get(o string) interface{} {
441437
return nil
442438
}
439+
440+
// FundAccount is a utility function that funds an account by minting and sending the coins to the address
441+
// TODO(fdymylja): instead of using the mint module account, which has the permission of minting, create a "faucet" account
442+
func FundAccount(app *SimApp, ctx sdk.Context, addr sdk.AccAddress, amounts sdk.Coins) error {
443+
err := app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, amounts)
444+
if err != nil {
445+
return err
446+
}
447+
return app.BankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr, amounts)
448+
}

0 commit comments

Comments
 (0)