Restructure
This commit is contained in:
parent
9ad3d62e49
commit
a49f9d6314
|
@ -9,7 +9,13 @@ import (
|
||||||
"github.com/tendermint/tendermint/libs/log"
|
"github.com/tendermint/tendermint/libs/log"
|
||||||
|
|
||||||
gaia "github.com/cosmos/cosmos-sdk/cmd/gaia/app"
|
gaia "github.com/cosmos/cosmos-sdk/cmd/gaia/app"
|
||||||
// stakesim "github.com/cosmos/cosmos-sdk/x/stake/simulation"
|
"github.com/cosmos/cosmos-sdk/x/mock"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
NumKeys = 10
|
||||||
|
NumBlocks = 1000
|
||||||
|
BlockSize = 1000
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFullGaiaSimulation(t *testing.T) {
|
func TestFullGaiaSimulation(t *testing.T) {
|
||||||
|
@ -18,4 +24,16 @@ func TestFullGaiaSimulation(t *testing.T) {
|
||||||
db := dbm.NewMemDB()
|
db := dbm.NewMemDB()
|
||||||
app := gaia.NewGaiaApp(logger, db, nil)
|
app := gaia.NewGaiaApp(logger, db, nil)
|
||||||
require.Equal(t, "GaiaApp", app.Name())
|
require.Equal(t, "GaiaApp", app.Name())
|
||||||
|
|
||||||
|
// Run randomized simulation
|
||||||
|
mock.RandomizedTesting(
|
||||||
|
t, app.BaseApp,
|
||||||
|
[]mock.TestAndRunTx{},
|
||||||
|
[]mock.RandSetup{},
|
||||||
|
[]mock.Invariant{},
|
||||||
|
NumKeys,
|
||||||
|
NumBlocks,
|
||||||
|
BlockSize,
|
||||||
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,36 +2,36 @@ package mock
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
abci "github.com/tendermint/tendermint/abci/types"
|
abci "github.com/tendermint/tendermint/abci/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RandomizedTesting tests application by sending random messages.
|
// RandomizedTesting tests application by sending random messages.
|
||||||
func (app *App) RandomizedTesting(
|
func RandomizedTesting(
|
||||||
t *testing.T, ops []TestAndRunTx, setups []RandSetup,
|
t *testing.T, app *baseapp.BaseApp, ops []TestAndRunTx, setups []RandSetup,
|
||||||
invariants []Invariant, numKeys int, numBlocks int, blockSize int,
|
invariants []Invariant, numKeys int, numBlocks int, blockSize int,
|
||||||
) {
|
) {
|
||||||
time := time.Now().UnixNano()
|
time := time.Now().UnixNano()
|
||||||
app.RandomizedTestingFromSeed(t, time, ops, setups, invariants, numKeys, numBlocks, blockSize)
|
RandomizedTestingFromSeed(t, app, time, ops, setups, invariants, numKeys, numBlocks, blockSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RandomizedTestingFromSeed tests an application by running the provided
|
// RandomizedTestingFromSeed tests an application by running the provided
|
||||||
// operations, testing the provided invariants, but using the provided seed.
|
// operations, testing the provided invariants, but using the provided seed.
|
||||||
func (app *App) RandomizedTestingFromSeed(
|
func RandomizedTestingFromSeed(
|
||||||
t *testing.T, seed int64, ops []TestAndRunTx, setups []RandSetup,
|
t *testing.T, app *baseapp.BaseApp, seed int64, ops []TestAndRunTx, setups []RandSetup,
|
||||||
invariants []Invariant, numKeys int, numBlocks int, blockSize int,
|
invariants []Invariant, numKeys int, numBlocks int, blockSize int,
|
||||||
) {
|
) {
|
||||||
log := fmt.Sprintf("Starting SingleModuleTest with randomness created with seed %d", int(seed))
|
log := fmt.Sprintf("Starting SingleModuleTest with randomness created with seed %d", int(seed))
|
||||||
keys, addrs := GeneratePrivKeyAddressPairs(numKeys)
|
keys, _ := GeneratePrivKeyAddressPairs(numKeys)
|
||||||
r := rand.New(rand.NewSource(seed))
|
r := rand.New(rand.NewSource(seed))
|
||||||
|
|
||||||
RandomSetGenesis(r, app, addrs, []string{"foocoin"})
|
// XXX TODO
|
||||||
|
// RandomSetGenesis(r, app, addrs, []string{"foocoin"})
|
||||||
app.InitChain(abci.RequestInitChain{})
|
app.InitChain(abci.RequestInitChain{})
|
||||||
for i := 0; i < len(setups); i++ {
|
for i := 0; i < len(setups); i++ {
|
||||||
setups[i](r, keys)
|
setups[i](r, keys)
|
||||||
|
@ -45,7 +45,7 @@ func (app *App) RandomizedTestingFromSeed(
|
||||||
|
|
||||||
// Make sure invariants hold at beginning of block and when nothing was
|
// Make sure invariants hold at beginning of block and when nothing was
|
||||||
// done.
|
// done.
|
||||||
app.assertAllInvariants(t, invariants, log)
|
AssertAllInvariants(t, app, invariants, log)
|
||||||
|
|
||||||
ctx := app.NewContext(false, header)
|
ctx := app.NewContext(false, header)
|
||||||
|
|
||||||
|
@ -56,7 +56,7 @@ func (app *App) RandomizedTestingFromSeed(
|
||||||
log += "\n" + logUpdate
|
log += "\n" + logUpdate
|
||||||
|
|
||||||
require.Nil(t, err, log)
|
require.Nil(t, err, log)
|
||||||
app.assertAllInvariants(t, invariants, log)
|
AssertAllInvariants(t, app, invariants, log)
|
||||||
}
|
}
|
||||||
|
|
||||||
app.EndBlock(abci.RequestEndBlock{})
|
app.EndBlock(abci.RequestEndBlock{})
|
||||||
|
@ -64,100 +64,8 @@ func (app *App) RandomizedTestingFromSeed(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SimpleRandomizedTestingFromSeed
|
func AssertAllInvariants(t *testing.T, app *baseapp.BaseApp, tests []Invariant, log string) {
|
||||||
func (app *App) SimpleRandomizedTestingFromSeed(
|
|
||||||
t *testing.T, seed int64, ops []TestAndRunMsg, setups []RandSetup,
|
|
||||||
invariants []Invariant, numKeys int, numBlocks int, blockSize int,
|
|
||||||
) {
|
|
||||||
log := fmt.Sprintf("Starting SimpleSingleModuleTest with randomness created with seed %d", int(seed))
|
|
||||||
keys, addrs := GeneratePrivKeyAddressPairs(numKeys)
|
|
||||||
r := rand.New(rand.NewSource(seed))
|
|
||||||
|
|
||||||
RandomSetGenesis(r, app, addrs, []string{"foocoin"})
|
|
||||||
app.InitChain(abci.RequestInitChain{})
|
|
||||||
for i := 0; i < len(setups); i++ {
|
|
||||||
setups[i](r, keys)
|
|
||||||
}
|
|
||||||
app.Commit()
|
|
||||||
|
|
||||||
header := abci.Header{Height: 0}
|
|
||||||
|
|
||||||
for i := 0; i < numBlocks; i++ {
|
|
||||||
app.BeginBlock(abci.RequestBeginBlock{})
|
|
||||||
|
|
||||||
app.assertAllInvariants(t, invariants, log)
|
|
||||||
|
|
||||||
ctx := app.NewContext(false, header)
|
|
||||||
|
|
||||||
// TODO: Add modes to simulate "no load", "medium load", and
|
|
||||||
// "high load" blocks.
|
|
||||||
for j := 0; j < blockSize; j++ {
|
|
||||||
logUpdate, err := ops[r.Intn(len(ops))](t, r, ctx, keys, log)
|
|
||||||
log += "\n" + logUpdate
|
|
||||||
|
|
||||||
require.Nil(t, err, log)
|
|
||||||
app.assertAllInvariants(t, invariants, log)
|
|
||||||
}
|
|
||||||
|
|
||||||
app.EndBlock(abci.RequestEndBlock{})
|
|
||||||
header.Height++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) assertAllInvariants(t *testing.T, tests []Invariant, log string) {
|
|
||||||
for i := 0; i < len(tests); i++ {
|
for i := 0; i < len(tests); i++ {
|
||||||
tests[i](t, app, log)
|
tests[i](t, app, log)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// BigInterval is a representation of the interval [lo, hi), where
|
|
||||||
// lo and hi are both of type sdk.Int
|
|
||||||
type BigInterval struct {
|
|
||||||
lo sdk.Int
|
|
||||||
hi sdk.Int
|
|
||||||
}
|
|
||||||
|
|
||||||
// RandFromBigInterval chooses an interval uniformly from the provided list of
|
|
||||||
// BigIntervals, and then chooses an element from an interval uniformly at random.
|
|
||||||
func RandFromBigInterval(r *rand.Rand, intervals []BigInterval) sdk.Int {
|
|
||||||
if len(intervals) == 0 {
|
|
||||||
return sdk.ZeroInt()
|
|
||||||
}
|
|
||||||
|
|
||||||
interval := intervals[r.Intn(len(intervals))]
|
|
||||||
|
|
||||||
lo := interval.lo
|
|
||||||
hi := interval.hi
|
|
||||||
|
|
||||||
diff := hi.Sub(lo)
|
|
||||||
result := sdk.NewIntFromBigInt(new(big.Int).Rand(r, diff.BigInt()))
|
|
||||||
result = result.Add(lo)
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
// shamelessly copied from https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-golang#31832326
|
|
||||||
|
|
||||||
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
||||||
const (
|
|
||||||
letterIdxBits = 6 // 6 bits to represent a letter index
|
|
||||||
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
|
|
||||||
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
|
|
||||||
)
|
|
||||||
|
|
||||||
func RandStringOfLength(r *rand.Rand, n int) string {
|
|
||||||
b := make([]byte, n)
|
|
||||||
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
|
|
||||||
for i, cache, remain := n-1, r.Int63(), letterIdxMax; i >= 0; {
|
|
||||||
if remain == 0 {
|
|
||||||
cache, remain = r.Int63(), letterIdxMax
|
|
||||||
}
|
|
||||||
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
|
|
||||||
b[i] = letterBytes[idx]
|
|
||||||
i--
|
|
||||||
}
|
|
||||||
cache >>= letterIdxBits
|
|
||||||
remain--
|
|
||||||
}
|
|
||||||
return string(b)
|
|
||||||
}
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
"github.com/tendermint/tendermint/crypto"
|
"github.com/tendermint/tendermint/crypto"
|
||||||
)
|
)
|
||||||
|
@ -13,33 +14,24 @@ type (
|
||||||
// transition was as expected. It returns a descriptive message "action"
|
// transition was as expected. It returns a descriptive message "action"
|
||||||
// about what this fuzzed tx actually did, for ease of debugging.
|
// about what this fuzzed tx actually did, for ease of debugging.
|
||||||
TestAndRunTx func(
|
TestAndRunTx func(
|
||||||
t *testing.T, r *rand.Rand, app *App, ctx sdk.Context,
|
t *testing.T, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
|
||||||
privKeys []crypto.PrivKey, log string,
|
privKeys []crypto.PrivKey, log string,
|
||||||
) (action string, err sdk.Error)
|
) (action string, err sdk.Error)
|
||||||
|
|
||||||
// TestAndRunMsg produces a fuzzed message, calls the appropriate handler
|
|
||||||
// (bypassing all ante handler checks), and ensures that the state
|
|
||||||
// transition was as expected. It returns a descriptive message "action"
|
|
||||||
// about what this fuzzed msg actually did for ease of debugging.
|
|
||||||
TestAndRunMsg func(
|
|
||||||
t *testing.T, r *rand.Rand, ctx sdk.Context,
|
|
||||||
privKey []crypto.PrivKey, log string,
|
|
||||||
) (action string, err sdk.Error)
|
|
||||||
|
|
||||||
// RandSetup performs the random setup the mock module needs.
|
// RandSetup performs the random setup the mock module needs.
|
||||||
RandSetup func(r *rand.Rand, privKeys []crypto.PrivKey)
|
RandSetup func(r *rand.Rand, privKeys []crypto.PrivKey)
|
||||||
|
|
||||||
// An Invariant is a function which tests a particular invariant.
|
// An Invariant is a function which tests a particular invariant.
|
||||||
// If the invariant has been broken, the function should halt the
|
// If the invariant has been broken, the function should halt the
|
||||||
// test and output the log.
|
// test and output the log.
|
||||||
Invariant func(t *testing.T, app *App, log string)
|
Invariant func(t *testing.T, app *baseapp.BaseApp, log string)
|
||||||
)
|
)
|
||||||
|
|
||||||
// PeriodicInvariant returns an Invariant function closure that asserts
|
// PeriodicInvariant returns an Invariant function closure that asserts
|
||||||
// a given invariant if the mock application's last block modulo the given
|
// a given invariant if the mock application's last block modulo the given
|
||||||
// period is congruent to the given offset.
|
// period is congruent to the given offset.
|
||||||
func PeriodicInvariant(invariant Invariant, period int, offset int) Invariant {
|
func PeriodicInvariant(invariant Invariant, period int, offset int) Invariant {
|
||||||
return func(t *testing.T, app *App, log string) {
|
return func(t *testing.T, app *baseapp.BaseApp, log string) {
|
||||||
if int(app.LastBlockHeight())%period == offset {
|
if int(app.LastBlockHeight())%period == offset {
|
||||||
invariant(t, app, log)
|
invariant(t, app, log)
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,60 @@
|
||||||
|
package mock
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/big"
|
||||||
|
"math/rand"
|
||||||
|
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
// BigInterval is a representation of the interval [lo, hi), where
|
||||||
|
// lo and hi are both of type sdk.Int
|
||||||
|
type BigInterval struct {
|
||||||
|
lo sdk.Int
|
||||||
|
hi sdk.Int
|
||||||
|
}
|
||||||
|
|
||||||
|
// RandFromBigInterval chooses an interval uniformly from the provided list of
|
||||||
|
// BigIntervals, and then chooses an element from an interval uniformly at random.
|
||||||
|
func RandFromBigInterval(r *rand.Rand, intervals []BigInterval) sdk.Int {
|
||||||
|
if len(intervals) == 0 {
|
||||||
|
return sdk.ZeroInt()
|
||||||
|
}
|
||||||
|
|
||||||
|
interval := intervals[r.Intn(len(intervals))]
|
||||||
|
|
||||||
|
lo := interval.lo
|
||||||
|
hi := interval.hi
|
||||||
|
|
||||||
|
diff := hi.Sub(lo)
|
||||||
|
result := sdk.NewIntFromBigInt(new(big.Int).Rand(r, diff.BigInt()))
|
||||||
|
result = result.Add(lo)
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// shamelessly copied from https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-golang#31832326
|
||||||
|
|
||||||
|
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
const (
|
||||||
|
letterIdxBits = 6 // 6 bits to represent a letter index
|
||||||
|
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
|
||||||
|
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
|
||||||
|
)
|
||||||
|
|
||||||
|
func RandStringOfLength(r *rand.Rand, n int) string {
|
||||||
|
b := make([]byte, n)
|
||||||
|
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
|
||||||
|
for i, cache, remain := n-1, r.Int63(), letterIdxMax; i >= 0; {
|
||||||
|
if remain == 0 {
|
||||||
|
cache, remain = r.Int63(), letterIdxMax
|
||||||
|
}
|
||||||
|
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
|
||||||
|
b[i] = letterBytes[idx]
|
||||||
|
i--
|
||||||
|
}
|
||||||
|
cache >>= letterIdxBits
|
||||||
|
remain--
|
||||||
|
}
|
||||||
|
return string(b)
|
||||||
|
}
|
Loading…
Reference in New Issue