2019-08-28 07:58:25 -07:00
|
|
|
package simulation
|
|
|
|
|
|
|
|
// DONTCOVER
|
|
|
|
|
|
|
|
import (
|
2020-08-26 02:39:38 -07:00
|
|
|
"encoding/json"
|
2020-07-08 10:43:08 -07:00
|
|
|
"fmt"
|
2019-08-28 07:58:25 -07:00
|
|
|
"math/rand"
|
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-08-28 07:58:25 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/module"
|
2020-02-20 06:24:26 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank/types"
|
2019-08-28 07:58:25 -07:00
|
|
|
)
|
|
|
|
|
2020-07-08 10:43:08 -07:00
|
|
|
// 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
|
|
|
|
}
|
2019-08-28 07:58:25 -07:00
|
|
|
|
2020-07-08 10:43:08 -07:00
|
|
|
// 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
|
2019-08-28 07:58:25 -07:00
|
|
|
}
|
|
|
|
|
2020-08-24 03:55:42 -07:00
|
|
|
// 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{
|
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
|
|
|
|
}
|
|
|
|
|
2019-08-28 07:58:25 -07:00
|
|
|
// RandomizedGenState generates a random GenesisState for bank
|
|
|
|
func RandomizedGenState(simState *module.SimulationState) {
|
2020-07-08 10:43:08 -07:00
|
|
|
var sendEnabledParams types.SendEnabledParams
|
2019-08-28 07:58:25 -07:00
|
|
|
simState.AppParams.GetOrGenerate(
|
2020-07-08 10:43:08 -07:00
|
|
|
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) },
|
2019-08-28 07:58:25 -07:00
|
|
|
)
|
|
|
|
|
2020-04-20 08:22:12 -07:00
|
|
|
numAccs := int64(len(simState.Accounts))
|
|
|
|
totalSupply := sdk.NewInt(simState.InitialStake * (numAccs + simState.NumBonded))
|
|
|
|
supply := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, totalSupply))
|
2019-08-28 07:58:25 -07:00
|
|
|
|
2020-07-08 10:43:08 -07:00
|
|
|
bankGenesis := types.GenesisState{
|
|
|
|
Params: types.Params{
|
|
|
|
SendEnabled: sendEnabledParams,
|
|
|
|
DefaultSendEnabled: defaultSendEnabledParam,
|
|
|
|
},
|
|
|
|
Balances: RandomGenesisBalances(simState),
|
|
|
|
Supply: supply,
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
paramsBytes, err := json.MarshalIndent(&bankGenesis.Params, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fmt.Printf("Selected randomly generated bank parameters:\n%s\n", paramsBytes)
|
2020-08-13 06:20:02 -07:00
|
|
|
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(&bankGenesis)
|
2019-08-28 07:58:25 -07:00
|
|
|
}
|