Remove non-determinism from bep3 sims (#427)

* feat: remove non-determism from bep3 sims
This commit is contained in:
Kevin Davis 2020-04-13 13:06:59 -04:00 committed by GitHub
parent 5bdffd5c1c
commit 004837d7fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 6 deletions

View File

@ -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)

View File

@ -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