mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-24 23:35:19 +00:00
Remove non-determinism from bep3 sims (#427)
* feat: remove non-determism from bep3 sims
This commit is contained in:
parent
5bdffd5c1c
commit
004837d7fc
@ -4,7 +4,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/codec"
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
@ -52,8 +51,8 @@ func GenMaxBlockLock(r *rand.Rand, minBlockLock int64) int64 {
|
|||||||
// GenSupportedAssets gets randomized SupportedAssets
|
// GenSupportedAssets gets randomized SupportedAssets
|
||||||
func GenSupportedAssets(r *rand.Rand) types.AssetParams {
|
func GenSupportedAssets(r *rand.Rand) types.AssetParams {
|
||||||
var assets types.AssetParams
|
var assets types.AssetParams
|
||||||
for i := 0; i < (r.Intn(10) + 1); i++ {
|
numAssets := (r.Intn(10) + 1)
|
||||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
for i := 0; i < numAssets; i++ {
|
||||||
denom := strings.ToLower(simulation.RandStringOfLength(r, (r.Intn(3) + 3)))
|
denom := strings.ToLower(simulation.RandStringOfLength(r, (r.Intn(3) + 3)))
|
||||||
asset := genSupportedAsset(r, denom)
|
asset := genSupportedAsset(r, denom)
|
||||||
assets = append(assets, asset)
|
assets = append(assets, asset)
|
||||||
|
@ -33,13 +33,13 @@ func SimulateMsgCreateAtomicSwap(ak auth.AccountKeeper, k keeper.Keeper) simulat
|
|||||||
senderOtherChain := simulation.RandStringOfLength(r, 43)
|
senderOtherChain := simulation.RandStringOfLength(r, 43)
|
||||||
|
|
||||||
// Generate cryptographically strong pseudo-random number
|
// Generate cryptographically strong pseudo-random number
|
||||||
randomNumber, err := types.GenerateSecureRandomNumber()
|
randomNumber, err := simulation.RandPositiveInt(r, sdk.NewInt(math.MaxInt64))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return noOpMsg, nil, err
|
return noOpMsg, nil, err
|
||||||
}
|
}
|
||||||
// Must use current blocktime instead of 'now' since initial blocktime was randomly generated
|
// Must use current blocktime instead of 'now' since initial blocktime was randomly generated
|
||||||
timestamp := ctx.BlockTime().Unix()
|
timestamp := ctx.BlockTime().Unix()
|
||||||
randomNumberHash := types.CalculateRandomHash(randomNumber.Bytes(), timestamp)
|
randomNumberHash := types.CalculateRandomHash(randomNumber.BigInt().Bytes(), timestamp)
|
||||||
|
|
||||||
// Randomly select an asset from supported assets
|
// Randomly select an asset from supported assets
|
||||||
assets, found := k.GetAssets(ctx)
|
assets, found := k.GetAssets(ctx)
|
||||||
@ -90,7 +90,7 @@ func SimulateMsgCreateAtomicSwap(ak auth.AccountKeeper, k keeper.Keeper) simulat
|
|||||||
if evenOdd%2 == 0 {
|
if evenOdd%2 == 0 {
|
||||||
// Claim future operation
|
// Claim future operation
|
||||||
executionBlock := ctx.BlockHeight() + (msg.HeightSpan / 2)
|
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 {
|
} else {
|
||||||
// Refund future operation
|
// Refund future operation
|
||||||
executionBlock := ctx.BlockHeight() + msg.HeightSpan
|
executionBlock := ctx.BlockHeight() + msg.HeightSpan
|
||||||
|
Loading…
Reference in New Issue
Block a user