2019-08-28 07:58:25 -07:00
|
|
|
package simulation
|
|
|
|
|
|
|
|
// DONTCOVER
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2019-09-12 12:22:25 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-08-28 07:58:25 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/module"
|
2019-09-12 12:22:25 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/exported"
|
2019-08-28 07:58:25 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
2019-10-10 08:53:30 -07:00
|
|
|
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
|
2019-08-28 07:58:25 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/simulation"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Simulation parameter constants
|
|
|
|
const (
|
|
|
|
MaxMemoChars = "max_memo_characters"
|
|
|
|
TxSigLimit = "tx_sig_limit"
|
|
|
|
TxSizeCostPerByte = "tx_size_cost_per_byte"
|
|
|
|
SigVerifyCostED25519 = "sig_verify_cost_ed25519"
|
|
|
|
SigVerifyCostSECP256K1 = "sig_verify_cost_secp256k1"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GenMaxMemoChars randomized MaxMemoChars
|
|
|
|
func GenMaxMemoChars(r *rand.Rand) uint64 {
|
|
|
|
return uint64(simulation.RandIntBetween(r, 100, 200))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenTxSigLimit randomized TxSigLimit
|
2019-10-23 02:14:45 -07:00
|
|
|
// make sure that sigLimit is always high
|
|
|
|
// so that arbitrarily simulated messages from other
|
|
|
|
// modules can still create valid transactions
|
2019-08-28 07:58:25 -07:00
|
|
|
func GenTxSigLimit(r *rand.Rand) uint64 {
|
2019-10-23 02:14:45 -07:00
|
|
|
return uint64(r.Intn(7) + 5)
|
2019-08-28 07:58:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// GenTxSizeCostPerByte randomized TxSizeCostPerByte
|
|
|
|
func GenTxSizeCostPerByte(r *rand.Rand) uint64 {
|
|
|
|
return uint64(simulation.RandIntBetween(r, 5, 15))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenSigVerifyCostED25519 randomized SigVerifyCostED25519
|
|
|
|
func GenSigVerifyCostED25519(r *rand.Rand) uint64 {
|
|
|
|
return uint64(simulation.RandIntBetween(r, 500, 1000))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenSigVerifyCostSECP256K1 randomized SigVerifyCostSECP256K1
|
|
|
|
func GenSigVerifyCostSECP256K1(r *rand.Rand) uint64 {
|
|
|
|
return uint64(simulation.RandIntBetween(r, 500, 1000))
|
|
|
|
}
|
|
|
|
|
|
|
|
// RandomizedGenState generates a random GenesisState for auth
|
|
|
|
func RandomizedGenState(simState *module.SimulationState) {
|
|
|
|
var maxMemoChars uint64
|
|
|
|
simState.AppParams.GetOrGenerate(
|
|
|
|
simState.Cdc, MaxMemoChars, &maxMemoChars, simState.Rand,
|
|
|
|
func(r *rand.Rand) { maxMemoChars = GenMaxMemoChars(r) },
|
|
|
|
)
|
|
|
|
|
|
|
|
var txSigLimit uint64
|
|
|
|
simState.AppParams.GetOrGenerate(
|
|
|
|
simState.Cdc, TxSigLimit, &txSigLimit, simState.Rand,
|
|
|
|
func(r *rand.Rand) { txSigLimit = GenTxSigLimit(r) },
|
|
|
|
)
|
|
|
|
|
|
|
|
var txSizeCostPerByte uint64
|
|
|
|
simState.AppParams.GetOrGenerate(
|
|
|
|
simState.Cdc, TxSizeCostPerByte, &txSizeCostPerByte, simState.Rand,
|
|
|
|
func(r *rand.Rand) { txSizeCostPerByte = GenTxSizeCostPerByte(r) },
|
|
|
|
)
|
|
|
|
|
|
|
|
var sigVerifyCostED25519 uint64
|
|
|
|
simState.AppParams.GetOrGenerate(
|
|
|
|
simState.Cdc, SigVerifyCostED25519, &sigVerifyCostED25519, simState.Rand,
|
|
|
|
func(r *rand.Rand) { sigVerifyCostED25519 = GenSigVerifyCostED25519(r) },
|
|
|
|
)
|
|
|
|
|
|
|
|
var sigVerifyCostSECP256K1 uint64
|
|
|
|
simState.AppParams.GetOrGenerate(
|
|
|
|
simState.Cdc, SigVerifyCostSECP256K1, &sigVerifyCostSECP256K1, simState.Rand,
|
2019-12-10 08:48:57 -08:00
|
|
|
func(r *rand.Rand) { sigVerifyCostSECP256K1 = GenSigVerifyCostSECP256K1(r) },
|
2019-08-28 07:58:25 -07:00
|
|
|
)
|
|
|
|
|
2019-09-12 12:22:25 -07:00
|
|
|
params := types.NewParams(maxMemoChars, txSigLimit, txSizeCostPerByte,
|
|
|
|
sigVerifyCostED25519, sigVerifyCostSECP256K1)
|
|
|
|
genesisAccs := RandomGenesisAccounts(simState)
|
|
|
|
|
|
|
|
authGenesis := types.NewGenesisState(params, genesisAccs)
|
2019-08-28 07:58:25 -07:00
|
|
|
|
|
|
|
fmt.Printf("Selected randomly generated auth parameters:\n%s\n", codec.MustMarshalJSONIndent(simState.Cdc, authGenesis.Params))
|
|
|
|
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(authGenesis)
|
|
|
|
}
|
2019-09-12 12:22:25 -07:00
|
|
|
|
|
|
|
// RandomGenesisAccounts returns randomly generated genesis accounts
|
2019-09-13 15:26:29 -07:00
|
|
|
func RandomGenesisAccounts(simState *module.SimulationState) (genesisAccs exported.GenesisAccounts) {
|
2019-09-12 12:22:25 -07:00
|
|
|
for i, acc := range simState.Accounts {
|
|
|
|
bacc := types.NewBaseAccountWithAddress(acc.Address)
|
2020-02-18 04:50:13 -08:00
|
|
|
var gacc exported.GenesisAccount = bacc
|
2019-09-12 12:22:25 -07:00
|
|
|
|
|
|
|
// Only consider making a vesting account once the initial bonded validator
|
|
|
|
// set is exhausted due to needing to track DelegatedVesting.
|
|
|
|
if int64(i) > simState.NumBonded && simState.Rand.Intn(100) < 50 {
|
2020-01-30 13:31:16 -08:00
|
|
|
initialVesting := sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, simState.Rand.Int63n(simState.InitialStake)))
|
2019-09-12 12:22:25 -07:00
|
|
|
var endTime int64
|
|
|
|
|
|
|
|
startTime := simState.GenTimestamp.Unix()
|
|
|
|
|
|
|
|
// Allow for some vesting accounts to vest very quickly while others very slowly.
|
|
|
|
if simState.Rand.Intn(100) < 50 {
|
|
|
|
endTime = int64(simulation.RandIntBetween(simState.Rand, int(startTime)+1, int(startTime+(60*60*24*30))))
|
|
|
|
} else {
|
|
|
|
endTime = int64(simulation.RandIntBetween(simState.Rand, int(startTime)+1, int(startTime+(60*60*12))))
|
|
|
|
}
|
|
|
|
|
2020-02-18 04:50:13 -08:00
|
|
|
bva := vestingtypes.NewBaseVestingAccount(bacc, initialVesting, endTime)
|
2020-01-30 13:31:16 -08:00
|
|
|
|
2019-09-12 12:22:25 -07:00
|
|
|
if simState.Rand.Intn(100) < 50 {
|
2020-01-30 13:31:16 -08:00
|
|
|
gacc = vestingtypes.NewContinuousVestingAccountRaw(bva, startTime)
|
2019-09-12 12:22:25 -07:00
|
|
|
} else {
|
2020-01-30 13:31:16 -08:00
|
|
|
gacc = vestingtypes.NewDelayedVestingAccountRaw(bva)
|
2019-09-12 12:22:25 -07:00
|
|
|
}
|
|
|
|
}
|
2020-01-30 13:31:16 -08:00
|
|
|
|
2019-09-12 12:22:25 -07:00
|
|
|
genesisAccs = append(genesisAccs, gacc)
|
|
|
|
}
|
|
|
|
|
2019-09-19 06:21:38 -07:00
|
|
|
return genesisAccs
|
2019-09-12 12:22:25 -07:00
|
|
|
}
|