mirror of
				https://github.com/0glabs/0g-chain.git
				synced 2025-11-04 09:57:27 +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"
 | 
			
		||||
	"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)
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user