parent
74b2a90087
commit
6a7c4d1c86
|
@ -0,0 +1,93 @@
|
||||||
|
package simulation
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/big"
|
||||||
|
"math/rand"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/tendermint/tendermint/crypto/ed25519"
|
||||||
|
"github.com/tendermint/tendermint/crypto/secp256k1"
|
||||||
|
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
"github.com/cosmos/cosmos-sdk/x/mock"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
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
|
||||||
|
)
|
||||||
|
|
||||||
|
// shamelessly copied from
|
||||||
|
// https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-golang#31832326
|
||||||
|
// Generate a random string of a particular length
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RandomAcc pick a random account from an array
|
||||||
|
func RandomAcc(r *rand.Rand, accs []Account) Account {
|
||||||
|
return accs[r.Intn(
|
||||||
|
len(accs),
|
||||||
|
)]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate a random amount
|
||||||
|
func RandomAmount(r *rand.Rand, max sdk.Int) sdk.Int {
|
||||||
|
return sdk.NewInt(int64(r.Intn(int(max.Int64()))))
|
||||||
|
}
|
||||||
|
|
||||||
|
// RandomDecAmount generates a random decimal amount
|
||||||
|
func RandomDecAmount(r *rand.Rand, max sdk.Dec) sdk.Dec {
|
||||||
|
randInt := big.NewInt(0).Rand(r, max.Int)
|
||||||
|
return sdk.NewDecFromBigIntWithPrec(randInt, sdk.Precision)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RandomAccounts generates n random accounts
|
||||||
|
func RandomAccounts(r *rand.Rand, n int) []Account {
|
||||||
|
accs := make([]Account, n)
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
// don't need that much entropy for simulation
|
||||||
|
privkeySeed := make([]byte, 15)
|
||||||
|
r.Read(privkeySeed)
|
||||||
|
useSecp := r.Int63()%2 == 0
|
||||||
|
if useSecp {
|
||||||
|
accs[i].PrivKey = secp256k1.GenPrivKeySecp256k1(privkeySeed)
|
||||||
|
} else {
|
||||||
|
accs[i].PrivKey = ed25519.GenPrivKeyFromSecret(privkeySeed)
|
||||||
|
}
|
||||||
|
accs[i].PubKey = accs[i].PrivKey.PubKey()
|
||||||
|
accs[i].Address = sdk.AccAddress(accs[i].PubKey.Address())
|
||||||
|
}
|
||||||
|
return accs
|
||||||
|
}
|
||||||
|
|
||||||
|
// RandomSetGenesis wraps mock.RandomSetGenesis, but using simulation accounts
|
||||||
|
func RandomSetGenesis(r *rand.Rand, app *mock.App, accs []Account, denoms []string) {
|
||||||
|
addrs := make([]sdk.AccAddress, len(accs))
|
||||||
|
for i := 0; i < len(accs); i++ {
|
||||||
|
addrs[i] = accs[i].Address
|
||||||
|
}
|
||||||
|
mock.RandomSetGenesis(r, app, addrs, denoms)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RandTimestamp generates a random timestamp
|
||||||
|
func RandTimestamp(r *rand.Rand) time.Time {
|
||||||
|
// json.Marshal breaks for timestamps greater with year greater than 9999
|
||||||
|
unixTime := r.Int63n(253373529600)
|
||||||
|
return time.Unix(unixTime, 0)
|
||||||
|
}
|
|
@ -50,12 +50,6 @@ func initChain(r *rand.Rand, params Params,
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func randTimestamp(r *rand.Rand) time.Time {
|
|
||||||
// json.Marshal breaks for timestamps greater with year greater than 9999
|
|
||||||
unixTime := r.Int63n(253373529600)
|
|
||||||
return time.Unix(unixTime, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SimulateFromSeed tests an application by running the provided
|
// SimulateFromSeed 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 SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp,
|
func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp,
|
||||||
|
@ -70,7 +64,7 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp,
|
||||||
r := rand.New(rand.NewSource(seed))
|
r := rand.New(rand.NewSource(seed))
|
||||||
params := RandomParams(r) // := DefaultParams()
|
params := RandomParams(r) // := DefaultParams()
|
||||||
fmt.Printf("Randomized simulation params: %+v\n", params)
|
fmt.Printf("Randomized simulation params: %+v\n", params)
|
||||||
timestamp := randTimestamp(r)
|
timestamp := RandTimestamp(r)
|
||||||
fmt.Printf("Starting the simulation from time %v, unixtime %v\n", timestamp.UTC().Format(time.UnixDate), timestamp.Unix())
|
fmt.Printf("Starting the simulation from time %v, unixtime %v\n", timestamp.UTC().Format(time.UnixDate), timestamp.Unix())
|
||||||
timeDiff := maxTimePerBlock - minTimePerBlock
|
timeDiff := maxTimePerBlock - minTimePerBlock
|
||||||
|
|
||||||
|
|
|
@ -70,16 +70,3 @@ type WeightedOperation struct {
|
||||||
Weight int
|
Weight int
|
||||||
Op Operation
|
Op Operation
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO remove? not being called anywhere
|
|
||||||
// PeriodicInvariant returns an Invariant function closure that asserts a given
|
|
||||||
// invariant if the mock application's last block modulo the given period is
|
|
||||||
// congruent to the given offset.
|
|
||||||
func PeriodicInvariant(invariant Invariant, period int, offset int) Invariant {
|
|
||||||
return func(app *baseapp.BaseApp) error {
|
|
||||||
if int(app.LastBlockHeight())%period == offset {
|
|
||||||
return invariant(app)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -2,50 +2,15 @@ package simulation
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
|
||||||
"math/rand"
|
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/tendermint/tendermint/crypto/ed25519"
|
|
||||||
"github.com/tendermint/tendermint/crypto/secp256k1"
|
|
||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/baseapp"
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
||||||
"github.com/cosmos/cosmos-sdk/x/mock"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// shamelessly copied from https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-golang#31832326
|
|
||||||
// TODO we should probably move this to tendermint/libs/common/random.go
|
|
||||||
|
|
||||||
const (
|
|
||||||
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
||||||
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
|
|
||||||
)
|
|
||||||
|
|
||||||
// Generate a random string of a particular length
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pretty-print events as a table
|
// Pretty-print events as a table
|
||||||
func DisplayEvents(events map[string]uint) {
|
func DisplayEvents(events map[string]uint) {
|
||||||
var keys []string
|
var keys []string
|
||||||
|
@ -59,43 +24,6 @@ func DisplayEvents(events map[string]uint) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// RandomAcc pick a random account from an array
|
|
||||||
func RandomAcc(r *rand.Rand, accs []Account) Account {
|
|
||||||
return accs[r.Intn(
|
|
||||||
len(accs),
|
|
||||||
)]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate a random amount
|
|
||||||
func RandomAmount(r *rand.Rand, max sdk.Int) sdk.Int {
|
|
||||||
return sdk.NewInt(int64(r.Intn(int(max.Int64()))))
|
|
||||||
}
|
|
||||||
|
|
||||||
// RandomDecAmount generates a random decimal amount
|
|
||||||
func RandomDecAmount(r *rand.Rand, max sdk.Dec) sdk.Dec {
|
|
||||||
randInt := big.NewInt(0).Rand(r, max.Int)
|
|
||||||
return sdk.NewDecFromBigIntWithPrec(randInt, sdk.Precision)
|
|
||||||
}
|
|
||||||
|
|
||||||
// RandomAccounts generates n random accounts
|
|
||||||
func RandomAccounts(r *rand.Rand, n int) []Account {
|
|
||||||
accs := make([]Account, n)
|
|
||||||
for i := 0; i < n; i++ {
|
|
||||||
// don't need that much entropy for simulation
|
|
||||||
privkeySeed := make([]byte, 15)
|
|
||||||
r.Read(privkeySeed)
|
|
||||||
useSecp := r.Int63()%2 == 0
|
|
||||||
if useSecp {
|
|
||||||
accs[i].PrivKey = secp256k1.GenPrivKeySecp256k1(privkeySeed)
|
|
||||||
} else {
|
|
||||||
accs[i].PrivKey = ed25519.GenPrivKeyFromSecret(privkeySeed)
|
|
||||||
}
|
|
||||||
accs[i].PubKey = accs[i].PrivKey.PubKey()
|
|
||||||
accs[i].Address = sdk.AccAddress(accs[i].PubKey.Address())
|
|
||||||
}
|
|
||||||
return accs
|
|
||||||
}
|
|
||||||
|
|
||||||
// Builds a function to add logs for this particular block
|
// Builds a function to add logs for this particular block
|
||||||
func addLogMessage(testingmode bool, blockLogBuilders []*strings.Builder, height int) func(string) {
|
func addLogMessage(testingmode bool, blockLogBuilders []*strings.Builder, height int) func(string) {
|
||||||
if testingmode {
|
if testingmode {
|
||||||
|
@ -124,15 +52,6 @@ func assertAllInvariants(t *testing.T, app *baseapp.BaseApp,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// RandomSetGenesis wraps mock.RandomSetGenesis, but using simulation accounts
|
|
||||||
func RandomSetGenesis(r *rand.Rand, app *mock.App, accs []Account, denoms []string) {
|
|
||||||
addrs := make([]sdk.AccAddress, len(accs))
|
|
||||||
for i := 0; i < len(accs); i++ {
|
|
||||||
addrs[i] = accs[i].Address
|
|
||||||
}
|
|
||||||
mock.RandomSetGenesis(r, app, addrs, denoms)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Creates a function to print out the logs
|
// Creates a function to print out the logs
|
||||||
func logPrinter(testingmode bool, logs []*strings.Builder) func() {
|
func logPrinter(testingmode bool, logs []*strings.Builder) func() {
|
||||||
if testingmode {
|
if testingmode {
|
||||||
|
|
Loading…
Reference in New Issue