mirror of
				https://github.com/0glabs/0g-chain.git
				synced 2025-11-04 11:17:28 +00:00 
			
		
		
		
	* first pass at genesis and msgs * add proposal generation * add permission generation * add decoder * add invariants * add committee change proposal generator * improve committee change proposal generation * fix error formatting * update sims to v0.38 * Update x/committee/keeper/invariants.go Co-Authored-By: Denali Marsh <denali@kava.io> * Update x/committee/keeper/invariants.go Co-Authored-By: Denali Marsh <denali@kava.io> * tidy up comments * tidy up random helpers * add committee to ImportExport test * add member check to vote invariant * fix comment wording Co-authored-by: Kevin Davis <karzak@users.noreply.github.com> Co-authored-by: Denali Marsh <denali@kava.io> Co-authored-by: Kevin Davis <karzak@users.noreply.github.com>
		
			
				
	
	
		
			72 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			72 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package simulation
 | 
						|
 | 
						|
import (
 | 
						|
	"fmt"
 | 
						|
	"math/big"
 | 
						|
	"math/rand"
 | 
						|
	"time"
 | 
						|
 | 
						|
	sdk "github.com/cosmos/cosmos-sdk/types"
 | 
						|
	"github.com/cosmos/cosmos-sdk/x/simulation"
 | 
						|
)
 | 
						|
 | 
						|
func RandomAddresses(r *rand.Rand, accs []simulation.Account) []sdk.AccAddress {
 | 
						|
	r.Shuffle(len(accs), func(i, j int) {
 | 
						|
		accs[i], accs[j] = accs[j], accs[i]
 | 
						|
	})
 | 
						|
 | 
						|
	var addresses []sdk.AccAddress
 | 
						|
	numAddresses := r.Intn(len(accs) + 1)
 | 
						|
	for i := 0; i < numAddresses; i++ {
 | 
						|
		addresses = append(addresses, accs[i].Address)
 | 
						|
	}
 | 
						|
	return addresses
 | 
						|
}
 | 
						|
 | 
						|
func RandomPositiveDuration(r *rand.Rand, inclusiveMin, exclusiveMax time.Duration) (time.Duration, error) {
 | 
						|
	min := int64(inclusiveMin)
 | 
						|
	max := int64(exclusiveMax)
 | 
						|
	if min < 0 || max < 0 {
 | 
						|
		return 0, fmt.Errorf("min and max must be positive")
 | 
						|
	}
 | 
						|
	if min >= max {
 | 
						|
		return 0, fmt.Errorf("max must be < min")
 | 
						|
	}
 | 
						|
	randPositiveInt64 := r.Int63n(max-min) + min
 | 
						|
	return time.Duration(randPositiveInt64), nil
 | 
						|
}
 | 
						|
 | 
						|
func RandomTime(r *rand.Rand, inclusiveMin, exclusiveMax time.Time) (time.Time, error) {
 | 
						|
	if exclusiveMax.Before(inclusiveMin) {
 | 
						|
		return time.Time{}, fmt.Errorf("max must be > min")
 | 
						|
	}
 | 
						|
	period := exclusiveMax.Sub(inclusiveMin)
 | 
						|
	subPeriod, err := RandomPositiveDuration(r, 0, period)
 | 
						|
	if err != nil {
 | 
						|
		return time.Time{}, err
 | 
						|
	}
 | 
						|
	return inclusiveMin.Add(subPeriod), nil
 | 
						|
}
 | 
						|
 | 
						|
// RandInt randomly generates an sdk.Int in the range [inclusiveMin, inclusiveMax]. It works for negative and positive integers.
 | 
						|
func RandIntInclusive(r *rand.Rand, inclusiveMin, inclusiveMax sdk.Int) (sdk.Int, error) {
 | 
						|
	if inclusiveMin.GT(inclusiveMax) {
 | 
						|
		return sdk.Int{}, fmt.Errorf("min larger than max")
 | 
						|
	}
 | 
						|
	return RandInt(r, inclusiveMin, inclusiveMax.Add(sdk.OneInt()))
 | 
						|
}
 | 
						|
 | 
						|
// RandInt randomly generates an sdk.Int in the range [inclusiveMin, exclusiveMax). It works for negative and positive integers.
 | 
						|
func RandInt(r *rand.Rand, inclusiveMin, exclusiveMax sdk.Int) (sdk.Int, error) {
 | 
						|
	// validate input
 | 
						|
	if inclusiveMin.GTE(exclusiveMax) {
 | 
						|
		return sdk.Int{}, fmt.Errorf("min larger or equal to max")
 | 
						|
	}
 | 
						|
	// shift the range to start at 0
 | 
						|
	shiftedRange := exclusiveMax.Sub(inclusiveMin) // should always be positive given the check above
 | 
						|
	// randomly pick from the shifted range
 | 
						|
	shiftedRandInt := sdk.NewIntFromBigInt(new(big.Int).Rand(r, shiftedRange.BigInt()))
 | 
						|
	// shift back to the original range
 | 
						|
	return shiftedRandInt.Add(inclusiveMin), nil
 | 
						|
}
 |