From 6a7c4d1c868a8fdb47ddf42b2f607e9a5c3ce1bb Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Wed, 7 Nov 2018 10:37:45 -0500 Subject: [PATCH] rand utile ... --- x/mock/simulation/rand_util.go | 93 +++++++++++++++++++++ x/mock/simulation/random_simulate_blocks.go | 8 +- x/mock/simulation/types.go | 13 --- x/mock/simulation/util.go | 81 ------------------ 4 files changed, 94 insertions(+), 101 deletions(-) create mode 100644 x/mock/simulation/rand_util.go diff --git a/x/mock/simulation/rand_util.go b/x/mock/simulation/rand_util.go new file mode 100644 index 000000000..076995d36 --- /dev/null +++ b/x/mock/simulation/rand_util.go @@ -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<= 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) +} diff --git a/x/mock/simulation/random_simulate_blocks.go b/x/mock/simulation/random_simulate_blocks.go index 17a26f762..9cfe27011 100644 --- a/x/mock/simulation/random_simulate_blocks.go +++ b/x/mock/simulation/random_simulate_blocks.go @@ -50,12 +50,6 @@ func initChain(r *rand.Rand, params Params, 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 // operations, testing the provided invariants, but using the provided seed. 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)) params := RandomParams(r) // := DefaultParams() 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()) timeDiff := maxTimePerBlock - minTimePerBlock diff --git a/x/mock/simulation/types.go b/x/mock/simulation/types.go index 198e2a4ff..65118547f 100644 --- a/x/mock/simulation/types.go +++ b/x/mock/simulation/types.go @@ -70,16 +70,3 @@ type WeightedOperation struct { Weight int 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 - } -} diff --git a/x/mock/simulation/util.go b/x/mock/simulation/util.go index bb974b65f..10ed6b3b0 100644 --- a/x/mock/simulation/util.go +++ b/x/mock/simulation/util.go @@ -2,50 +2,15 @@ package simulation import ( "fmt" - "math/big" - "math/rand" "os" "sort" "strings" "testing" "time" - "github.com/tendermint/tendermint/crypto/ed25519" - "github.com/tendermint/tendermint/crypto/secp256k1" - "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<= 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 func DisplayEvents(events map[string]uint) { 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 func addLogMessage(testingmode bool, blockLogBuilders []*strings.Builder, height int) func(string) { 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 func logPrinter(testingmode bool, logs []*strings.Builder) func() { if testingmode {