From 004837d7fc297a06be57ade054ab35e1b736092c Mon Sep 17 00:00:00 2001 From: Kevin Davis Date: Mon, 13 Apr 2020 13:06:59 -0400 Subject: [PATCH] Remove non-determinism from bep3 sims (#427) * feat: remove non-determism from bep3 sims --- x/bep3/simulation/genesis.go | 5 ++--- x/bep3/simulation/operations/msg.go | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/x/bep3/simulation/genesis.go b/x/bep3/simulation/genesis.go index b5402736..e4af0003 100644 --- a/x/bep3/simulation/genesis.go +++ b/x/bep3/simulation/genesis.go @@ -4,7 +4,6 @@ import ( "fmt" "math/rand" "strings" - "time" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -52,8 +51,8 @@ func GenMaxBlockLock(r *rand.Rand, minBlockLock int64) int64 { // GenSupportedAssets gets randomized SupportedAssets func GenSupportedAssets(r *rand.Rand) types.AssetParams { var assets types.AssetParams - for i := 0; i < (r.Intn(10) + 1); i++ { - r := rand.New(rand.NewSource(time.Now().UnixNano())) + numAssets := (r.Intn(10) + 1) + for i := 0; i < numAssets; i++ { denom := strings.ToLower(simulation.RandStringOfLength(r, (r.Intn(3) + 3))) asset := genSupportedAsset(r, denom) assets = append(assets, asset) diff --git a/x/bep3/simulation/operations/msg.go b/x/bep3/simulation/operations/msg.go index 2f0fa6a0..c04eb218 100644 --- a/x/bep3/simulation/operations/msg.go +++ b/x/bep3/simulation/operations/msg.go @@ -33,13 +33,13 @@ func SimulateMsgCreateAtomicSwap(ak auth.AccountKeeper, k keeper.Keeper) simulat senderOtherChain := simulation.RandStringOfLength(r, 43) // Generate cryptographically strong pseudo-random number - randomNumber, err := types.GenerateSecureRandomNumber() + randomNumber, err := simulation.RandPositiveInt(r, sdk.NewInt(math.MaxInt64)) if err != nil { return noOpMsg, nil, err } // Must use current blocktime instead of 'now' since initial blocktime was randomly generated timestamp := ctx.BlockTime().Unix() - randomNumberHash := types.CalculateRandomHash(randomNumber.Bytes(), timestamp) + randomNumberHash := types.CalculateRandomHash(randomNumber.BigInt().Bytes(), timestamp) // Randomly select an asset from supported assets assets, found := k.GetAssets(ctx) @@ -90,7 +90,7 @@ func SimulateMsgCreateAtomicSwap(ak auth.AccountKeeper, k keeper.Keeper) simulat if evenOdd%2 == 0 { // Claim future operation executionBlock := ctx.BlockHeight() + (msg.HeightSpan / 2) - futureOp = loadClaimFutureOp(acc.Address, swapID, randomNumber.Bytes(), executionBlock, handler) + futureOp = loadClaimFutureOp(acc.Address, swapID, randomNumber.BigInt().Bytes(), executionBlock, handler) } else { // Refund future operation executionBlock := ctx.BlockHeight() + msg.HeightSpan