From cbcd0f08284a709e5298df3333437fb47b3d1a2e Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Tue, 17 Jul 2018 03:25:15 +0200 Subject: [PATCH] Restructure contd. --- cmd/gaia/simulation/sim_test.go | 10 +++--- .../random_simulate_blocks.go | 5 +-- x/mock/{ => simulation}/types.go | 2 +- x/mock/{ => simulation}/util.go | 31 +------------------ x/mock/test_utils.go | 28 +++++++++++++++++ 5 files changed, 38 insertions(+), 38 deletions(-) rename x/mock/{ => simulation}/random_simulate_blocks.go (94%) rename x/mock/{ => simulation}/types.go (98%) rename x/mock/{ => simulation}/util.go (56%) diff --git a/cmd/gaia/simulation/sim_test.go b/cmd/gaia/simulation/sim_test.go index 3cbbd68c8..b1382deb0 100644 --- a/cmd/gaia/simulation/sim_test.go +++ b/cmd/gaia/simulation/sim_test.go @@ -9,7 +9,7 @@ import ( "github.com/tendermint/tendermint/libs/log" gaia "github.com/cosmos/cosmos-sdk/cmd/gaia/app" - "github.com/cosmos/cosmos-sdk/x/mock" + "github.com/cosmos/cosmos-sdk/x/mock/simulation" ) const ( @@ -26,11 +26,11 @@ func TestFullGaiaSimulation(t *testing.T) { require.Equal(t, "GaiaApp", app.Name()) // Run randomized simulation - mock.RandomizedTesting( + simulation.RandomizedTesting( t, app.BaseApp, - []mock.TestAndRunTx{}, - []mock.RandSetup{}, - []mock.Invariant{}, + []simulation.TestAndRunTx{}, + []simulation.RandSetup{}, + []simulation.Invariant{}, NumKeys, NumBlocks, BlockSize, diff --git a/x/mock/random_simulate_blocks.go b/x/mock/simulation/random_simulate_blocks.go similarity index 94% rename from x/mock/random_simulate_blocks.go rename to x/mock/simulation/random_simulate_blocks.go index 5021d2785..d66373258 100644 --- a/x/mock/random_simulate_blocks.go +++ b/x/mock/simulation/random_simulate_blocks.go @@ -1,4 +1,4 @@ -package mock +package simulation import ( "fmt" @@ -7,6 +7,7 @@ import ( "time" "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/x/mock" "github.com/stretchr/testify/require" abci "github.com/tendermint/tendermint/abci/types" ) @@ -27,7 +28,7 @@ func RandomizedTestingFromSeed( invariants []Invariant, numKeys int, numBlocks int, blockSize int, ) { log := fmt.Sprintf("Starting SingleModuleTest with randomness created with seed %d", int(seed)) - keys, _ := GeneratePrivKeyAddressPairs(numKeys) + keys, _ := mock.GeneratePrivKeyAddressPairs(numKeys) r := rand.New(rand.NewSource(seed)) // XXX TODO diff --git a/x/mock/types.go b/x/mock/simulation/types.go similarity index 98% rename from x/mock/types.go rename to x/mock/simulation/types.go index 3a68e2422..41736ce2c 100644 --- a/x/mock/types.go +++ b/x/mock/simulation/types.go @@ -1,4 +1,4 @@ -package mock +package simulation import ( "math/rand" diff --git a/x/mock/util.go b/x/mock/simulation/util.go similarity index 56% rename from x/mock/util.go rename to x/mock/simulation/util.go index 7b86cc69e..97307dc3e 100644 --- a/x/mock/util.go +++ b/x/mock/simulation/util.go @@ -1,38 +1,9 @@ -package mock +package simulation 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" diff --git a/x/mock/test_utils.go b/x/mock/test_utils.go index f9e1e8f5e..fa7d9875f 100644 --- a/x/mock/test_utils.go +++ b/x/mock/test_utils.go @@ -1,6 +1,8 @@ package mock import ( + "math/big" + "math/rand" "testing" "github.com/cosmos/cosmos-sdk/baseapp" @@ -10,6 +12,32 @@ import ( "github.com/tendermint/tendermint/crypto" ) +// 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 +} + // CheckBalance checks the balance of an account. func CheckBalance(t *testing.T, app *App, addr sdk.AccAddress, exp sdk.Coins) { ctxCheck := app.BaseApp.NewContext(true, abci.Header{})