cosmos-sdk/x/bank/simulation/genesis.go

87 lines
2.9 KiB
Go
Raw Normal View History

package simulation
// DONTCOVER
import (
"encoding/json"
"fmt"
"math/rand"
2020-01-30 13:31:16 -08:00
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)
// RandomGenesisDefaultSendParam computes randomized allow all send transfers param for the bank module
func RandomGenesisDefaultSendParam(r *rand.Rand) bool {
// 90% chance of transfers being enable or P(a) = 0.9 for success
return r.Int63n(101) <= 90
}
// RandomGenesisSendParams randomized Parameters for the bank module
func RandomGenesisSendParams(r *rand.Rand) types.SendEnabledParams {
params := types.DefaultParams()
// 90% chance of transfers being DefaultSendEnabled=true or P(a) = 0.9 for success
// 50% of the time add an additional denom specific record (P(b) = 0.475 = 0.5 * 0.95)
if r.Int63n(101) <= 50 {
// set send enabled 95% of the time
bondEnabled := r.Int63n(101) <= 95
params = params.SetSendEnabledParam(
sdk.DefaultBondDenom,
bondEnabled)
}
// overall probability of enabled for bond denom is 94.75% (P(a)+P(b) - P(a)*P(b))
return params.SendEnabled
}
// RandomGenesisBalances returns a slice of account balances. Each account has
2020-01-30 13:31:16 -08:00
// a balance of simState.InitialStake for sdk.DefaultBondDenom.
func RandomGenesisBalances(simState *module.SimulationState) []types.Balance {
genesisBalances := []types.Balance{}
for _, acc := range simState.Accounts {
genesisBalances = append(genesisBalances, types.Balance{
Change `address` from bytes to bech32 strings (#7242) * init * Fix bank proto messages * missing conversions * remove casttype for addresses * Fix tests * Fix consaddress * more test fixes * Fix tests * fixed tests * migrate missing proto declarations * format * Fix format * Fix alignment * Fix more tests * Fix ibc merge issue * Fix fmt * Fix more tests * Fix missing address declarations * Fix staking tests * Fix more tests * Fix config * fixed tests * Fix more tests * Update staking grpc tests * Fix merge issue * fixed failing tests in x/distr * fixed sim tests * fixed failing tests * Fix bugs * Add logs * fixed slashing issue * Fix staking grpc tests * Fix all bank tests :) * Fix tests in distribution * Fix more tests in distr * Fix slashing tests * Fix statking tests * Fix evidence tests * Fix gov tests * Fix bug in create vesting account * Fix test * remove fmt * fixed gov tests * fixed x/ibc tests * fixed x/ibc-transfer tests * fixed staking tests * fixed staking tests * fixed test * fixed distribution issue * fix pagination test * fmt * lint * fix build * fix format * revert tally tests * revert tally tests * lint * Fix sim test * revert * revert * fixed tally issue * fix tests * revert * fmt * refactor * remove `GetAddress()` * remove fmt * revert fmt.Striger usage * Fix tests * Fix rest test * disable interfacer lint check * make proto-format * add nolint rule * remove stray println Co-authored-by: aleem1314 <aleem.md789@gmail.com> Co-authored-by: atheesh <atheesh@vitwit.com>
2020-09-25 03:25:37 -07:00
Address: acc.Address.String(),
2020-01-30 13:31:16 -08:00
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(simState.InitialStake))),
})
}
return genesisBalances
}
// RandomizedGenState generates a random GenesisState for bank
func RandomizedGenState(simState *module.SimulationState) {
var sendEnabledParams types.SendEnabledParams
simState.AppParams.GetOrGenerate(
simState.Cdc, string(types.KeySendEnabled), &sendEnabledParams, simState.Rand,
func(r *rand.Rand) { sendEnabledParams = RandomGenesisSendParams(r) },
)
var defaultSendEnabledParam bool
simState.AppParams.GetOrGenerate(
simState.Cdc, string(types.KeyDefaultSendEnabled), &defaultSendEnabledParam, simState.Rand,
func(r *rand.Rand) { defaultSendEnabledParam = RandomGenesisDefaultSendParam(r) },
)
numAccs := int64(len(simState.Accounts))
totalSupply := sdk.NewInt(simState.InitialStake * (numAccs + simState.NumBonded))
supply := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, totalSupply))
bankGenesis := types.GenesisState{
Params: types.Params{
SendEnabled: sendEnabledParams,
DefaultSendEnabled: defaultSendEnabledParam,
},
Balances: RandomGenesisBalances(simState),
Supply: supply,
}
paramsBytes, err := json.MarshalIndent(&bankGenesis.Params, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("Selected randomly generated bank parameters:\n%s\n", paramsBytes)
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(&bankGenesis)
}