cosmos-sdk/x/stake/simulation/sim_test.go

73 lines
2.5 KiB
Go
Raw Normal View History

2018-07-11 10:43:25 -07:00
package simulation
2018-07-10 11:46:28 -07:00
import (
2018-07-17 11:50:30 -07:00
"encoding/json"
2018-07-17 16:01:36 -07:00
"math/rand"
2018-07-10 11:46:28 -07:00
"testing"
abci "github.com/tendermint/tendermint/abci/types"
2018-07-10 11:46:28 -07:00
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
2018-07-10 11:46:28 -07:00
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/distribution"
2018-07-10 11:46:28 -07:00
"github.com/cosmos/cosmos-sdk/x/mock"
2018-07-17 11:50:30 -07:00
"github.com/cosmos/cosmos-sdk/x/mock/simulation"
"github.com/cosmos/cosmos-sdk/x/params"
2018-07-11 10:43:25 -07:00
"github.com/cosmos/cosmos-sdk/x/stake"
2018-07-10 19:36:12 -07:00
)
2018-07-10 17:36:50 -07:00
// TestStakeWithRandomMessages
2018-07-10 11:46:28 -07:00
func TestStakeWithRandomMessages(t *testing.T) {
mapp := mock.NewApp()
bank.RegisterCodec(mapp.Cdc)
mapper := mapp.AccountKeeper
bankKeeper := bank.NewBaseKeeper(mapper)
feeKey := sdk.NewKVStoreKey("fee")
2018-07-10 11:46:28 -07:00
stakeKey := sdk.NewKVStoreKey("stake")
stakeTKey := sdk.NewTransientStoreKey("transient_stake")
paramsKey := sdk.NewKVStoreKey("params")
paramsTKey := sdk.NewTransientStoreKey("transient_params")
distrKey := sdk.NewKVStoreKey("distr")
feeCollectionKeeper := auth.NewFeeCollectionKeeper(mapp.Cdc, feeKey)
paramstore := params.NewKeeper(mapp.Cdc, paramsKey, paramsTKey)
stakeKeeper := stake.NewKeeper(mapp.Cdc, stakeKey, stakeTKey, bankKeeper, paramstore.Subspace(stake.DefaultParamspace), stake.DefaultCodespace)
distrKeeper := distribution.NewKeeper(mapp.Cdc, distrKey, paramstore.Subspace(distribution.DefaultParamspace), bankKeeper, stakeKeeper, feeCollectionKeeper, distribution.DefaultCodespace)
2018-07-11 10:43:25 -07:00
mapp.Router().AddRoute("stake", stake.NewHandler(stakeKeeper))
2018-07-10 19:36:12 -07:00
mapp.SetEndBlocker(func(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
validatorUpdates, tags := stake.EndBlocker(ctx, stakeKeeper)
2018-07-10 19:36:12 -07:00
return abci.ResponseEndBlock{
ValidatorUpdates: validatorUpdates,
Tags: tags,
2018-07-10 19:36:12 -07:00
}
})
2018-07-10 11:46:28 -07:00
err := mapp.CompleteSetup(stakeKey, stakeTKey, paramsKey, paramsTKey)
2018-07-10 11:46:28 -07:00
if err != nil {
panic(err)
}
appStateFn := func(r *rand.Rand, accs []simulation.Account) json.RawMessage {
simulation.RandomSetGenesis(r, mapp, accs, []string{"stake"})
2018-07-17 16:01:36 -07:00
return json.RawMessage("{}")
}
2018-07-17 11:50:30 -07:00
simulation.Simulate(
2018-07-17 16:01:36 -07:00
t, mapp.BaseApp, appStateFn,
[]simulation.WeightedOperation{
{10, SimulateMsgCreateValidator(mapper, stakeKeeper)},
{5, SimulateMsgEditValidator(stakeKeeper)},
{15, SimulateMsgDelegate(mapper, stakeKeeper)},
{10, SimulateMsgBeginUnbonding(mapper, stakeKeeper)},
{10, SimulateMsgBeginRedelegate(mapper, stakeKeeper)},
2018-07-17 11:50:30 -07:00
}, []simulation.RandSetup{
2018-07-17 22:50:04 -07:00
Setup(mapp, stakeKeeper),
2018-07-17 11:50:30 -07:00
}, []simulation.Invariant{
AllInvariants(bankKeeper, stakeKeeper, feeCollectionKeeper, distrKeeper, mapp.AccountKeeper),
}, 10, 100,
false,
2018-07-10 11:46:28 -07:00
)
}