2018-07-17 14:06:30 -07:00
|
|
|
package app
|
2018-07-11 15:14:37 -07:00
|
|
|
|
|
|
|
import (
|
2018-07-17 15:04:10 -07:00
|
|
|
"encoding/json"
|
|
|
|
"math/rand"
|
2018-07-17 14:06:30 -07:00
|
|
|
"os"
|
2018-07-17 22:44:40 -07:00
|
|
|
"strconv"
|
2018-07-11 15:14:37 -07:00
|
|
|
"testing"
|
2018-07-17 22:44:40 -07:00
|
|
|
"time"
|
2018-07-11 15:14:37 -07:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-07-12 13:01:43 -07:00
|
|
|
|
|
|
|
dbm "github.com/tendermint/tendermint/libs/db"
|
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
|
|
|
2018-07-17 15:04:10 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-07-18 00:05:48 -07:00
|
|
|
banksim "github.com/cosmos/cosmos-sdk/x/bank/simulation"
|
2018-07-16 18:25:15 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/mock/simulation"
|
2018-07-17 11:33:53 -07:00
|
|
|
stake "github.com/cosmos/cosmos-sdk/x/stake"
|
2018-07-17 15:04:10 -07:00
|
|
|
stakesim "github.com/cosmos/cosmos-sdk/x/stake/simulation"
|
2018-07-16 18:15:50 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
NumKeys = 10
|
2018-07-17 16:01:36 -07:00
|
|
|
NumBlocks = 100
|
2018-07-17 22:37:38 -07:00
|
|
|
BlockSize = 100
|
2018-07-17 14:06:30 -07:00
|
|
|
|
2018-07-17 22:37:38 -07:00
|
|
|
simulationEnvEnable = "GAIA_SIMULATION_ENABLED"
|
|
|
|
simulationEnvSeed = "GAIA_SIMULATION_SEED"
|
|
|
|
simulationEnvKeys = "GAIA_SIMULATION_KEYS"
|
|
|
|
simulationEnvBlocks = "GAIA_SIMULATION_BLOCKS"
|
|
|
|
simulationEnvBlockSize = "GAIA_SIMULATION_BLOCK_SIZE"
|
2018-07-11 15:14:37 -07:00
|
|
|
)
|
|
|
|
|
2018-07-17 15:04:10 -07:00
|
|
|
func appStateFn(r *rand.Rand, accs []sdk.AccAddress) json.RawMessage {
|
|
|
|
var genesisAccounts []GenesisAccount
|
2018-07-17 11:33:53 -07:00
|
|
|
|
2018-07-17 15:04:10 -07:00
|
|
|
// Randomly generate some genesis accounts
|
|
|
|
for _, addr := range accs {
|
|
|
|
coins := sdk.Coins{sdk.Coin{"steak", sdk.NewInt(100)}}
|
|
|
|
genesisAccounts = append(genesisAccounts, GenesisAccount{
|
|
|
|
Address: addr,
|
|
|
|
Coins: coins,
|
|
|
|
})
|
|
|
|
}
|
2018-07-16 18:15:50 -07:00
|
|
|
|
2018-07-17 11:33:53 -07:00
|
|
|
// Default genesis state
|
2018-07-17 16:01:36 -07:00
|
|
|
stakeGenesis := stake.DefaultGenesisState()
|
|
|
|
stakeGenesis.Pool.LooseTokens = sdk.NewRat(1000)
|
2018-07-17 14:06:30 -07:00
|
|
|
genesis := GenesisState{
|
2018-07-17 15:04:10 -07:00
|
|
|
Accounts: genesisAccounts,
|
2018-07-17 16:01:36 -07:00
|
|
|
StakeData: stakeGenesis,
|
2018-07-17 11:33:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal genesis
|
2018-07-17 14:06:30 -07:00
|
|
|
appState, err := MakeCodec().MarshalJSON(genesis)
|
2018-07-17 11:33:53 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-07-17 15:04:10 -07:00
|
|
|
return appState
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFullGaiaSimulation(t *testing.T) {
|
2018-07-17 22:37:38 -07:00
|
|
|
if os.Getenv(simulationEnvEnable) == "" {
|
2018-07-17 15:04:10 -07:00
|
|
|
t.Skip("Skipping Gaia simulation")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup Gaia application
|
|
|
|
logger := log.NewNopLogger()
|
|
|
|
db := dbm.NewMemDB()
|
|
|
|
app := NewGaiaApp(logger, db, nil)
|
|
|
|
require.Equal(t, "GaiaApp", app.Name())
|
|
|
|
|
2018-07-17 22:37:38 -07:00
|
|
|
var seed int64
|
2018-07-17 22:44:40 -07:00
|
|
|
var err error
|
2018-07-17 22:37:38 -07:00
|
|
|
envSeed := os.Getenv(simulationEnvSeed)
|
|
|
|
if envSeed != "" {
|
|
|
|
seed, err = strconv.ParseInt(envSeed, 10, 64)
|
|
|
|
require.Nil(t, err)
|
|
|
|
} else {
|
|
|
|
seed = time.Now().UnixNano()
|
|
|
|
}
|
|
|
|
|
2018-07-16 18:15:50 -07:00
|
|
|
// Run randomized simulation
|
2018-07-17 22:37:38 -07:00
|
|
|
simulation.SimulateFromSeed(
|
|
|
|
t, app.BaseApp, appStateFn, seed,
|
2018-07-17 15:04:10 -07:00
|
|
|
[]simulation.TestAndRunTx{
|
2018-07-18 00:05:48 -07:00
|
|
|
banksim.TestAndRunSingleInputMsgSend(app.accountMapper),
|
2018-07-17 15:04:10 -07:00
|
|
|
stakesim.SimulateMsgCreateValidator(app.accountMapper, app.stakeKeeper),
|
2018-07-17 22:44:40 -07:00
|
|
|
stakesim.SimulateMsgEditValidator(app.stakeKeeper),
|
2018-07-17 22:37:38 -07:00
|
|
|
stakesim.SimulateMsgDelegate(app.accountMapper, app.stakeKeeper),
|
|
|
|
stakesim.SimulateMsgBeginUnbonding(app.accountMapper, app.stakeKeeper),
|
|
|
|
stakesim.SimulateMsgCompleteUnbonding(app.stakeKeeper),
|
|
|
|
stakesim.SimulateMsgBeginRedelegate(app.accountMapper, app.stakeKeeper),
|
|
|
|
stakesim.SimulateMsgCompleteRedelegate(app.stakeKeeper),
|
2018-07-17 15:04:10 -07:00
|
|
|
},
|
2018-07-16 18:25:15 -07:00
|
|
|
[]simulation.RandSetup{},
|
2018-07-17 15:04:10 -07:00
|
|
|
[]simulation.Invariant{
|
2018-07-18 00:05:48 -07:00
|
|
|
banksim.NonnegativeBalanceInvariant(app.accountMapper),
|
2018-07-17 15:04:10 -07:00
|
|
|
stakesim.AllInvariants(app.coinKeeper, app.stakeKeeper, app.accountMapper),
|
|
|
|
},
|
2018-07-16 18:15:50 -07:00
|
|
|
NumKeys,
|
|
|
|
NumBlocks,
|
|
|
|
BlockSize,
|
|
|
|
)
|
|
|
|
|
2018-07-11 15:14:37 -07:00
|
|
|
}
|