perf: Replace runsim with Go stdlib testing (#24045)
Co-authored-by: Alex | Interchain Labs <alex@interchainlabs.io>
This commit is contained in:
co-authored by
Alex | Interchain Labs
parent
b7c35f2568
commit
bc57ce3ba7
+21
-11
@@ -31,22 +31,32 @@ func RandomAcc(r *rand.Rand, accs []Account) (Account, int) {
|
||||
return accs[idx], idx
|
||||
}
|
||||
|
||||
// RandomAccounts generates n random accounts
|
||||
// RandomAccounts deterministically generates n random accounts without duplicates.
|
||||
func RandomAccounts(r *rand.Rand, n int) []Account {
|
||||
accs := make([]Account, n)
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
idx := make(map[string]struct{}, n)
|
||||
var i int
|
||||
for i < n {
|
||||
// don't need that much entropy for simulation
|
||||
privkeySeed := make([]byte, 15)
|
||||
r.Read(privkeySeed)
|
||||
|
||||
accs[i].PrivKey = secp256k1.GenPrivKeyFromSecret(privkeySeed)
|
||||
accs[i].PubKey = accs[i].PrivKey.PubKey()
|
||||
accs[i].Address = sdk.AccAddress(accs[i].PubKey.Address())
|
||||
|
||||
accs[i].ConsKey = ed25519.GenPrivKeyFromSecret(privkeySeed)
|
||||
if _, err := r.Read(privkeySeed); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
privKey := secp256k1.GenPrivKeyFromSecret(privkeySeed)
|
||||
pubKey := privKey.PubKey()
|
||||
addr := sdk.AccAddress(pubKey.Address())
|
||||
if _, exists := idx[string(addr.Bytes())]; exists {
|
||||
continue
|
||||
}
|
||||
idx[string(addr.Bytes())] = struct{}{}
|
||||
accs[i] = Account{
|
||||
Address: addr,
|
||||
PrivKey: privKey,
|
||||
PubKey: pubKey,
|
||||
ConsKey: ed25519.GenPrivKeyFromSecret(privkeySeed),
|
||||
}
|
||||
i++
|
||||
}
|
||||
|
||||
return accs
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package simulation
|
||||
|
||||
import "testing"
|
||||
|
||||
// Config contains the necessary configuration flags for the simulator
|
||||
type Config struct {
|
||||
GenesisFile string // custom simulation genesis file; cannot be used with params file
|
||||
@@ -12,6 +14,7 @@ type Config struct {
|
||||
|
||||
Seed int64 // simulation random seed
|
||||
InitialBlockHeight int // initial block to start the simulation
|
||||
GenesisTime int64 // genesis time to start the simulation
|
||||
NumBlocks int // number of new blocks to simulate from the initial block height
|
||||
BlockSize int // operations per block
|
||||
ChainID string // chain-id used on the simulation
|
||||
@@ -19,9 +22,28 @@ type Config struct {
|
||||
Lean bool // lean simulation log output
|
||||
Commit bool // have the simulation commit
|
||||
|
||||
OnOperation bool // run slow invariants every operation
|
||||
AllInvariants bool // print all failed invariants if a broken invariant is found
|
||||
|
||||
DBBackend string // custom db backend type
|
||||
BlockMaxGas int64 // custom max gas for block
|
||||
FuzzSeed []byte
|
||||
TB testing.TB
|
||||
FauxMerkle bool
|
||||
|
||||
// Deprecated: unused and will be removed
|
||||
OnOperation bool // run slow invariants every operation
|
||||
// Deprecated: unused and will be removed
|
||||
AllInvariants bool // print all failed invariants if a broken invariant is found
|
||||
}
|
||||
|
||||
func (c Config) shallowCopy() Config {
|
||||
return c
|
||||
}
|
||||
|
||||
// With sets the values of t, seed, and fuzzSeed in a copy of the Config and returns the copy.
|
||||
func (c Config) With(tb testing.TB, seed int64, fuzzSeed []byte) Config {
|
||||
tb.Helper()
|
||||
r := c.shallowCopy()
|
||||
r.TB = tb
|
||||
r.Seed = seed
|
||||
r.FuzzSeed = fuzzSeed
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -147,31 +147,8 @@ func RandSubsetCoins(r *rand.Rand, coins sdk.Coins) sdk.Coins {
|
||||
}
|
||||
|
||||
// DeriveRand derives a new Rand deterministically from another random source.
|
||||
// Unlike rand.New(rand.NewSource(seed)), the result is "more random"
|
||||
// depending on the source and state of r.
|
||||
//
|
||||
// NOTE: not crypto safe.
|
||||
func DeriveRand(r *rand.Rand) *rand.Rand {
|
||||
const num = 8 // TODO what's a good number? Too large is too slow.
|
||||
ms := multiSource(make([]rand.Source, num))
|
||||
|
||||
for i := 0; i < num; i++ {
|
||||
ms[i] = rand.NewSource(r.Int63())
|
||||
}
|
||||
|
||||
return rand.New(ms)
|
||||
}
|
||||
|
||||
type multiSource []rand.Source
|
||||
|
||||
func (ms multiSource) Int63() (r int64) {
|
||||
for _, source := range ms {
|
||||
r ^= source.Int63()
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func (ms multiSource) Seed(_ int64) {
|
||||
panic("multiSource Seed should not be called")
|
||||
return rand.New(rand.NewSource(r.Int63()))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user