2018-07-16 18:25:15 -07:00
|
|
|
package simulation
|
2018-07-06 13:19:11 -07:00
|
|
|
|
|
|
|
import (
|
2018-07-17 11:33:53 -07:00
|
|
|
"encoding/json"
|
2018-07-06 13:19:11 -07:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-07-16 18:15:50 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
2018-07-17 15:04:10 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-07-16 18:25:15 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/mock"
|
2018-07-06 13:19:11 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
)
|
|
|
|
|
2018-07-17 11:33:53 -07:00
|
|
|
// Simulate tests application by sending random messages.
|
|
|
|
func Simulate(
|
2018-07-17 15:04:10 -07:00
|
|
|
t *testing.T, app *baseapp.BaseApp, appStateFn func(r *rand.Rand, accs []sdk.AccAddress) json.RawMessage, ops []TestAndRunTx, setups []RandSetup,
|
2018-07-06 13:19:11 -07:00
|
|
|
invariants []Invariant, numKeys int, numBlocks int, blockSize int,
|
|
|
|
) {
|
|
|
|
time := time.Now().UnixNano()
|
2018-07-17 15:04:10 -07:00
|
|
|
SimulateFromSeed(t, app, appStateFn, time, ops, setups, invariants, numKeys, numBlocks, blockSize)
|
2018-07-06 13:19:11 -07:00
|
|
|
}
|
|
|
|
|
2018-07-17 11:33:53 -07:00
|
|
|
// SimulateFromSeed tests an application by running the provided
|
2018-07-06 13:19:11 -07:00
|
|
|
// operations, testing the provided invariants, but using the provided seed.
|
2018-07-17 11:33:53 -07:00
|
|
|
func SimulateFromSeed(
|
2018-07-17 15:04:10 -07:00
|
|
|
t *testing.T, app *baseapp.BaseApp, appStateFn func(r *rand.Rand, accs []sdk.AccAddress) json.RawMessage, seed int64, ops []TestAndRunTx, setups []RandSetup,
|
2018-07-06 13:19:11 -07:00
|
|
|
invariants []Invariant, numKeys int, numBlocks int, blockSize int,
|
|
|
|
) {
|
2018-07-17 15:04:10 -07:00
|
|
|
log := fmt.Sprintf("Starting SimulateFromSeed with randomness created with seed %d", int(seed))
|
|
|
|
keys, addrs := mock.GeneratePrivKeyAddressPairs(numKeys)
|
2018-07-06 13:19:11 -07:00
|
|
|
r := rand.New(rand.NewSource(seed))
|
|
|
|
|
2018-07-17 16:27:51 -07:00
|
|
|
// Setup event stats
|
|
|
|
events := make(map[string]uint)
|
|
|
|
event := func(what string) {
|
2018-07-17 22:50:04 -07:00
|
|
|
events[what]++
|
2018-07-17 16:27:51 -07:00
|
|
|
}
|
|
|
|
|
2018-07-17 15:04:10 -07:00
|
|
|
app.InitChain(abci.RequestInitChain{AppStateBytes: appStateFn(r, addrs)})
|
2018-07-06 13:19:11 -07:00
|
|
|
for i := 0; i < len(setups); i++ {
|
|
|
|
setups[i](r, keys)
|
|
|
|
}
|
2018-07-10 11:46:28 -07:00
|
|
|
app.Commit()
|
2018-07-06 13:19:11 -07:00
|
|
|
|
|
|
|
header := abci.Header{Height: 0}
|
|
|
|
|
|
|
|
for i := 0; i < numBlocks; i++ {
|
|
|
|
app.BeginBlock(abci.RequestBeginBlock{})
|
|
|
|
|
|
|
|
// Make sure invariants hold at beginning of block and when nothing was
|
|
|
|
// done.
|
2018-07-16 18:15:50 -07:00
|
|
|
AssertAllInvariants(t, app, invariants, log)
|
2018-07-06 13:19:11 -07:00
|
|
|
|
|
|
|
ctx := app.NewContext(false, header)
|
|
|
|
|
|
|
|
// TODO: Add modes to simulate "no load", "medium load", and
|
|
|
|
// "high load" blocks.
|
|
|
|
for j := 0; j < blockSize; j++ {
|
2018-07-17 16:27:51 -07:00
|
|
|
logUpdate, err := ops[r.Intn(len(ops))](t, r, app, ctx, keys, log, event)
|
2018-07-06 13:19:11 -07:00
|
|
|
log += "\n" + logUpdate
|
|
|
|
|
|
|
|
require.Nil(t, err, log)
|
2018-07-16 18:15:50 -07:00
|
|
|
AssertAllInvariants(t, app, invariants, log)
|
2018-07-06 13:19:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
app.EndBlock(abci.RequestEndBlock{})
|
|
|
|
header.Height++
|
|
|
|
}
|
2018-07-17 16:27:51 -07:00
|
|
|
|
|
|
|
DisplayEvents(events)
|
2018-07-06 13:19:11 -07:00
|
|
|
}
|
|
|
|
|
2018-07-17 15:04:10 -07:00
|
|
|
// AssertAllInvariants asserts a list of provided invariants against application state
|
2018-07-16 18:15:50 -07:00
|
|
|
func AssertAllInvariants(t *testing.T, app *baseapp.BaseApp, tests []Invariant, log string) {
|
2018-07-06 13:19:11 -07:00
|
|
|
for i := 0; i < len(tests); i++ {
|
|
|
|
tests[i](t, app, log)
|
|
|
|
}
|
|
|
|
}
|