Skip to content

Commit

Permalink
allocate multiple genesis coins on bank simulation (#9)
Browse files Browse the repository at this point in the history
* allocate multiple genesis coins on bank simulation

* fix test

* fix genesis

* fix test final

* increase default tx size

* Update x/bank/simulation/genesis.go

Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com>

* fix test

Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com>
  • Loading branch information
mconcat and ValarDragon committed Nov 19, 2021
1 parent d04ab1c commit f82267d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion simapp/helpers/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

// SimAppChainID hardcoded chainID for simulation
const (
DefaultGenTxGas = 1000000
DefaultGenTxGas = 2000000
SimAppChainID = "simulation-app"
)

Expand Down
19 changes: 16 additions & 3 deletions x/bank/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ func RandomGenesisSendParams(r *rand.Rand) types.SendEnabledParams {
return params.SendEnabled
}

func GenesisCoins(bondDenom string, initialStake int64) sdk.Coins {
coins := make(sdk.Coins, 4)
for i := range coins {
coins[i] = sdk.NewCoin(fmt.Sprintf("%s%02d", bondDenom, i), sdk.NewInt(initialStake))
}
coins = coins.Add(sdk.NewInt64Coin(bondDenom, initialStake))
return coins
}

// RandomGenesisBalances returns a slice of account balances. Each account has
// a balance of simState.InitialStake for sdk.DefaultBondDenom.
func RandomGenesisBalances(simState *module.SimulationState) []types.Balance {
Expand All @@ -43,7 +52,7 @@ func RandomGenesisBalances(simState *module.SimulationState) []types.Balance {
for _, acc := range simState.Accounts {
genesisBalances = append(genesisBalances, types.Balance{
Address: acc.Address.String(),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(simState.InitialStake))),
Coins: GenesisCoins(sdk.DefaultBondDenom, simState.InitialStake),
})
}

Expand All @@ -65,8 +74,12 @@ func RandomizedGenState(simState *module.SimulationState) {
)

numAccs := int64(len(simState.Accounts))
totalSupply := sdk.NewInt(simState.InitialStake * (numAccs + simState.NumBonded))
supply := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, totalSupply))

supply := GenesisCoins(sdk.DefaultBondDenom, simState.InitialStake)
for i, coin := range supply {
supply[i] = sdk.NewCoin(coin.Denom, coin.Amount.MulRaw(numAccs))
}
supply = supply.Add(sdk.NewInt64Coin(sdk.DefaultBondDenom, simState.InitialStake*simState.NumBonded))

bankGenesis := types.GenesisState{
Params: types.Params{
Expand Down
4 changes: 2 additions & 2 deletions x/bank/simulation/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func TestRandomizedGenState(t *testing.T) {
require.Len(t, bankGenesis.Params.GetSendEnabled(), 1)
require.Len(t, bankGenesis.Balances, 3)
require.Equal(t, "cosmos1ghekyjucln7y67ntx7cf27m9dpuxxemn4c8g4r", bankGenesis.Balances[2].GetAddress().String())
require.Equal(t, "1000stake", bankGenesis.Balances[2].GetCoins().String())
require.Equal(t, "6000stake", bankGenesis.Supply.String())
require.Equal(t, simulation.GenesisCoins("stake", 1000).String(), bankGenesis.Balances[2].GetCoins().String())
require.Equal(t, "6000stake,3000stake00,3000stake01,3000stake02,3000stake03", bankGenesis.Supply.String())
}

// TestRandomizedGenState tests abnormal scenarios of applying RandomizedGenState.
Expand Down

0 comments on commit f82267d

Please sign in to comment.