2019-08-15 08:35:25 -07:00
|
|
|
package simapp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-10-17 06:47:35 -07:00
|
|
|
"io"
|
2019-08-15 08:35:25 -07:00
|
|
|
"math/rand"
|
2021-10-13 00:38:22 -07:00
|
|
|
"os"
|
2019-08-15 08:35:25 -07:00
|
|
|
"time"
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
tmjson "github.com/tendermint/tendermint/libs/json"
|
2019-08-15 08:35:25 -07:00
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
|
|
|
|
2019-08-28 07:58:25 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2020-08-28 09:02:38 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
|
2020-10-23 05:07:52 -07:00
|
|
|
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
|
2021-02-17 10:20:33 -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-03-23 04:55:44 -07:00
|
|
|
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
2020-06-17 11:42:27 -07:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2021-02-17 10:20:33 -08:00
|
|
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
|
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
2019-08-15 08:35:25 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// AppStateFn returns the initial application state using a genesis or the simulation parameters.
|
|
|
|
// It panics if the user provides files for both of them.
|
|
|
|
// If a file is not given for the genesis or the sim params, it creates a randomized one.
|
2021-04-29 03:46:22 -07:00
|
|
|
func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simtypes.AppStateFn {
|
2020-03-23 04:55:44 -07:00
|
|
|
return func(r *rand.Rand, accs []simtypes.Account, config simtypes.Config,
|
|
|
|
) (appState json.RawMessage, simAccs []simtypes.Account, chainID string, genesisTimestamp time.Time) {
|
2019-08-28 07:58:25 -07:00
|
|
|
|
2019-09-09 11:13:43 -07:00
|
|
|
if FlagGenesisTimeValue == 0 {
|
2020-03-23 04:55:44 -07:00
|
|
|
genesisTimestamp = simtypes.RandTimestamp(r)
|
2019-08-28 07:58:25 -07:00
|
|
|
} else {
|
2019-09-09 11:13:43 -07:00
|
|
|
genesisTimestamp = time.Unix(FlagGenesisTimeValue, 0)
|
2019-08-28 07:58:25 -07:00
|
|
|
}
|
2019-08-15 08:35:25 -07:00
|
|
|
|
2019-12-05 01:29:54 -08:00
|
|
|
chainID = config.ChainID
|
2019-08-28 07:58:25 -07:00
|
|
|
switch {
|
|
|
|
case config.ParamsFile != "" && config.GenesisFile != "":
|
|
|
|
panic("cannot provide both a genesis file and a params file")
|
2019-08-15 08:35:25 -07:00
|
|
|
|
2019-08-28 07:58:25 -07:00
|
|
|
case config.GenesisFile != "":
|
2019-10-23 02:14:45 -07:00
|
|
|
// override the default chain-id from simapp to set it later to the config
|
2019-10-07 10:17:06 -07:00
|
|
|
genesisDoc, accounts := AppStateFromGenesisFileFn(r, cdc, config.GenesisFile)
|
|
|
|
|
|
|
|
if FlagGenesisTimeValue == 0 {
|
|
|
|
// use genesis timestamp if no custom timestamp is provided (i.e no random timestamp)
|
|
|
|
genesisTimestamp = genesisDoc.GenesisTime
|
|
|
|
}
|
|
|
|
|
|
|
|
appState = genesisDoc.AppState
|
|
|
|
chainID = genesisDoc.ChainID
|
|
|
|
simAccs = accounts
|
2019-08-15 08:35:25 -07:00
|
|
|
|
2019-08-28 07:58:25 -07:00
|
|
|
case config.ParamsFile != "":
|
2020-03-23 04:55:44 -07:00
|
|
|
appParams := make(simtypes.AppParams)
|
2021-10-13 00:38:22 -07:00
|
|
|
bz, err := os.ReadFile(config.ParamsFile)
|
2019-08-28 07:58:25 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-08-15 08:35:25 -07:00
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
err = json.Unmarshal(bz, &appParams)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-10-23 02:14:45 -07:00
|
|
|
appState, simAccs = AppStateRandomizedFn(simManager, r, cdc, accs, genesisTimestamp, appParams)
|
2019-08-15 08:35:25 -07:00
|
|
|
|
2019-08-28 07:58:25 -07:00
|
|
|
default:
|
2020-03-23 04:55:44 -07:00
|
|
|
appParams := make(simtypes.AppParams)
|
2019-10-23 02:14:45 -07:00
|
|
|
appState, simAccs = AppStateRandomizedFn(simManager, r, cdc, accs, genesisTimestamp, appParams)
|
2019-08-15 08:35:25 -07:00
|
|
|
}
|
|
|
|
|
2021-02-17 10:20:33 -08:00
|
|
|
rawState := make(map[string]json.RawMessage)
|
|
|
|
err := json.Unmarshal(appState, &rawState)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stakingStateBz, ok := rawState[stakingtypes.ModuleName]
|
|
|
|
if !ok {
|
|
|
|
panic("staking genesis state is missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
stakingState := new(stakingtypes.GenesisState)
|
|
|
|
err = cdc.UnmarshalJSON(stakingStateBz, stakingState)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
// compute not bonded balance
|
|
|
|
notBondedTokens := sdk.ZeroInt()
|
|
|
|
for _, val := range stakingState.Validators {
|
|
|
|
if val.Status != stakingtypes.Unbonded {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
notBondedTokens = notBondedTokens.Add(val.GetTokens())
|
|
|
|
}
|
|
|
|
notBondedCoins := sdk.NewCoin(stakingState.Params.BondDenom, notBondedTokens)
|
|
|
|
// edit bank state to make it have the not bonded pool tokens
|
|
|
|
bankStateBz, ok := rawState[banktypes.ModuleName]
|
|
|
|
// TODO(fdymylja/jonathan): should we panic in this case
|
|
|
|
if !ok {
|
|
|
|
panic("bank genesis state is missing")
|
|
|
|
}
|
|
|
|
bankState := new(banktypes.GenesisState)
|
|
|
|
err = cdc.UnmarshalJSON(bankStateBz, bankState)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-05-17 08:42:44 -07:00
|
|
|
stakingAddr := authtypes.NewModuleAddress(stakingtypes.NotBondedPoolName).String()
|
|
|
|
var found bool
|
|
|
|
for _, balance := range bankState.Balances {
|
|
|
|
if balance.Address == stakingAddr {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
bankState.Balances = append(bankState.Balances, banktypes.Balance{
|
|
|
|
Address: stakingAddr,
|
|
|
|
Coins: sdk.NewCoins(notBondedCoins),
|
|
|
|
})
|
|
|
|
}
|
2021-02-17 10:20:33 -08:00
|
|
|
|
|
|
|
// change appState back
|
|
|
|
rawState[stakingtypes.ModuleName] = cdc.MustMarshalJSON(stakingState)
|
|
|
|
rawState[banktypes.ModuleName] = cdc.MustMarshalJSON(bankState)
|
|
|
|
|
|
|
|
// replace appstate
|
|
|
|
appState, err = json.Marshal(rawState)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-08-28 07:58:25 -07:00
|
|
|
return appState, simAccs, chainID, genesisTimestamp
|
2019-08-15 08:35:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppStateRandomizedFn creates calls each module's GenesisState generator function
|
2019-08-28 07:58:25 -07:00
|
|
|
// and creates the simulation params
|
2019-08-15 08:35:25 -07:00
|
|
|
func AppStateRandomizedFn(
|
2021-04-29 03:46:22 -07:00
|
|
|
simManager *module.SimulationManager, r *rand.Rand, cdc codec.JSONCodec,
|
2020-03-23 04:55:44 -07:00
|
|
|
accs []simtypes.Account, genesisTimestamp time.Time, appParams simtypes.AppParams,
|
|
|
|
) (json.RawMessage, []simtypes.Account) {
|
2019-08-28 07:58:25 -07:00
|
|
|
numAccs := int64(len(accs))
|
2021-01-06 07:20:12 -08:00
|
|
|
genesisState := NewDefaultGenesisState(cdc)
|
2019-08-15 08:35:25 -07:00
|
|
|
|
2019-08-28 07:58:25 -07:00
|
|
|
// generate a random amount of initial stake coins and a random initial
|
|
|
|
// number of bonded accounts
|
|
|
|
var initialStake, numInitiallyBonded int64
|
|
|
|
appParams.GetOrGenerate(
|
2020-10-23 05:07:52 -07:00
|
|
|
cdc, simappparams.StakePerAccount, &initialStake, r,
|
2019-12-12 07:20:30 -08:00
|
|
|
func(r *rand.Rand) { initialStake = r.Int63n(1e12) },
|
2019-08-28 07:58:25 -07:00
|
|
|
)
|
|
|
|
appParams.GetOrGenerate(
|
2020-10-23 05:07:52 -07:00
|
|
|
cdc, simappparams.InitiallyBondedValidators, &numInitiallyBonded, r,
|
2019-10-23 02:14:45 -07:00
|
|
|
func(r *rand.Rand) { numInitiallyBonded = int64(r.Intn(300)) },
|
2019-08-15 08:35:25 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
if numInitiallyBonded > numAccs {
|
|
|
|
numInitiallyBonded = numAccs
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf(
|
|
|
|
`Selected randomly generated parameters for simulated genesis:
|
|
|
|
{
|
2019-08-28 07:58:25 -07:00
|
|
|
stake_per_account: "%d",
|
|
|
|
initially_bonded_validators: "%d"
|
2019-08-15 08:35:25 -07:00
|
|
|
}
|
2019-08-28 07:58:25 -07:00
|
|
|
`, initialStake, numInitiallyBonded,
|
2019-08-15 08:35:25 -07:00
|
|
|
)
|
|
|
|
|
2019-08-28 07:58:25 -07:00
|
|
|
simState := &module.SimulationState{
|
|
|
|
AppParams: appParams,
|
|
|
|
Cdc: cdc,
|
|
|
|
Rand: r,
|
|
|
|
GenState: genesisState,
|
|
|
|
Accounts: accs,
|
|
|
|
InitialStake: initialStake,
|
|
|
|
NumBonded: numInitiallyBonded,
|
|
|
|
GenTimestamp: genesisTimestamp,
|
|
|
|
}
|
|
|
|
|
|
|
|
simManager.GenerateGenesisStates(simState)
|
|
|
|
|
2020-08-13 06:20:02 -07:00
|
|
|
appState, err := json.Marshal(genesisState)
|
2019-08-15 08:35:25 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-10-23 02:14:45 -07:00
|
|
|
return appState, accs
|
2019-08-15 08:35:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// AppStateFromGenesisFileFn util function to generate the genesis AppState
|
2019-10-07 10:17:06 -07:00
|
|
|
// from a genesis.json file.
|
2021-04-29 03:46:22 -07:00
|
|
|
func AppStateFromGenesisFileFn(r io.Reader, cdc codec.JSONCodec, genesisFile string) (tmtypes.GenesisDoc, []simtypes.Account) {
|
2021-10-13 00:38:22 -07:00
|
|
|
bytes, err := os.ReadFile(genesisFile)
|
2019-08-15 08:35:25 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-08-28 07:58:25 -07:00
|
|
|
var genesis tmtypes.GenesisDoc
|
2020-08-26 02:39:38 -07:00
|
|
|
// NOTE: Tendermint uses a custom JSON decoder for GenesisDoc
|
|
|
|
err = tmjson.Unmarshal(bytes, &genesis)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-08-15 08:35:25 -07:00
|
|
|
|
|
|
|
var appState GenesisState
|
2020-08-26 02:39:38 -07:00
|
|
|
err = json.Unmarshal(genesis.AppState, &appState)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-08-15 08:35:25 -07:00
|
|
|
|
2020-06-17 11:42:27 -07:00
|
|
|
var authGenesis authtypes.GenesisState
|
|
|
|
if appState[authtypes.ModuleName] != nil {
|
|
|
|
cdc.MustUnmarshalJSON(appState[authtypes.ModuleName], &authGenesis)
|
2019-09-12 12:22:25 -07:00
|
|
|
}
|
2019-08-15 08:35:25 -07:00
|
|
|
|
2020-03-23 04:55:44 -07:00
|
|
|
newAccs := make([]simtypes.Account, len(authGenesis.Accounts))
|
2019-10-07 10:17:06 -07:00
|
|
|
for i, acc := range authGenesis.Accounts {
|
2019-08-15 08:35:25 -07:00
|
|
|
// Pick a random private key, since we don't know the actual key
|
|
|
|
// This should be fine as it's only used for mock Tendermint validators
|
|
|
|
// and these keys are never actually used to sign by mock Tendermint.
|
|
|
|
privkeySeed := make([]byte, 15)
|
2019-08-28 07:58:25 -07:00
|
|
|
if _, err := r.Read(privkeySeed); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-08-15 08:35:25 -07:00
|
|
|
|
2020-08-14 10:58:53 -07:00
|
|
|
privKey := secp256k1.GenPrivKeyFromSecret(privkeySeed)
|
2019-08-28 07:58:25 -07:00
|
|
|
|
2020-07-29 09:00:15 -07:00
|
|
|
a, ok := acc.GetCachedValue().(authtypes.AccountI)
|
|
|
|
if !ok {
|
|
|
|
panic("expected account")
|
|
|
|
}
|
|
|
|
|
2019-08-28 07:58:25 -07:00
|
|
|
// create simulator accounts
|
2020-07-29 09:00:15 -07:00
|
|
|
simAcc := simtypes.Account{PrivKey: privKey, PubKey: privKey.PubKey(), Address: a.GetAddress()}
|
2019-10-07 10:17:06 -07:00
|
|
|
newAccs[i] = simAcc
|
2019-08-15 08:35:25 -07:00
|
|
|
}
|
|
|
|
|
2019-10-07 10:17:06 -07:00
|
|
|
return genesis, newAccs
|
2019-08-15 08:35:25 -07:00
|
|
|
}
|