Skip to content

Commit

Permalink
Use fuzzer filled random source from #18946
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed May 3, 2024
1 parent 9b614e1 commit 2bdf61b
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ SIM_COMMIT ?= true
test-sim-fuzz:
@echo "Running application fuzz for numBlocks=$(SIM_NUM_BLOCKS), blockSize=$(SIM_BLOCK_SIZE). This may take awhile!"
#ld flags are a quick fix to make it work on current osx
@cd ${CURRENT_DIR}/simapp && go test -mod=readonly -json -ldflags="-extldflags=-Wl,-ld_classic" -timeout=30m -fuzztime=30m -run=^$$ -fuzz=FuzzFullAppSimulation -GenesisTime=1714720615 -NumBlocks=2 -BlockSize=20
@cd ${CURRENT_DIR}/simapp && go test -mod=readonly -json -ldflags="-extldflags=-Wl,-ld_classic" -timeout=60m -fuzztime=60m -run=^$$ -fuzz=FuzzFullAppSimulation -GenesisTime=1714720615 -NumBlocks=2 -BlockSize=20

#? test-sim-benchmark: Run benchmark test for simapp
test-sim-benchmark:
Expand Down
4 changes: 3 additions & 1 deletion simapp/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func Run[T SimulationApp](
setupStateFactory func(app T) SimStateFactory,
postRunActions ...func(t *testing.T, app TestInstance[T]),
) {
RunWithSeeds(t, appFactory, setupStateFactory, defaultSeeds, postRunActions...)
RunWithSeeds(t, appFactory, setupStateFactory, defaultSeeds, []int64{}, postRunActions...)
}

func RunWithSeeds[T SimulationApp](
Expand All @@ -81,6 +81,7 @@ func RunWithSeeds[T SimulationApp](
) T,
setupStateFactory func(app T) SimStateFactory,
seeds []int64,
xseeds []int64,
postRunActions ...func(t *testing.T, app TestInstance[T]),
) {
cfg := cli.NewConfigFromFlags()
Expand All @@ -92,6 +93,7 @@ func RunWithSeeds[T SimulationApp](
// setup environment
tCfg := cfg.Clone()
tCfg.Seed = seed
tCfg.XSeeds = xseeds
testInstance := NewSimulationAppInstance(t, tCfg, appFactory)

app := testInstance.App
Expand Down
14 changes: 13 additions & 1 deletion simapp/sim_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,19 @@ func BenchmarkFullAppSimulation(b *testing.B) {
app := NewSimApp(logger, db, nil, true, appOptions, interBlockCacheOpt(), baseapp.SetChainID(SimAppChainID))

// run randomized simulation
simParams, simErr := simulation.SimulateFromSeed(b, log.NewNopLogger(), os.Stdout, app.BaseApp, simtestutil.AppStateFn(app.AppCodec(), app.AuthKeeper.AddressCodec(), app.StakingKeeper.ValidatorAddressCodec(), app.SimulationManager(), app.DefaultGenesis()), simtypes.RandomAccounts, simtestutil.SimulationOperations(app, app.AppCodec(), config, app.txConfig), BlockedAddresses(), config, app.AppCodec(), app.txConfig.SigningContext().AddressCodec())
simParams, simErr := simulation.SimulateFromSeed(
b,
log.NewNopLogger(),
os.Stdout,
app.BaseApp,
simtestutil.AppStateFn(app.AppCodec(), app.AuthKeeper.AddressCodec(), app.StakingKeeper.ValidatorAddressCodec(), app.SimulationManager(), app.DefaultGenesis()),
simtypes.RandomAccounts,
simtestutil.SimulationOperations(app, app.AppCodec(), config, app.txConfig),
BlockedAddresses(),
config,
app.AppCodec(),
app.txConfig.SigningContext().AddressCodec(),
)

// export state and simParams before the simulation error is checked
if err = simtestutil.CheckExportSimulation(app, config, simParams); err != nil {
Expand Down
20 changes: 16 additions & 4 deletions simapp/sim_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package simapp

import (
"encoding/binary"
"encoding/json"
"flag"
"io"
Expand Down Expand Up @@ -196,7 +197,7 @@ func TestAppStateDeterminism(t *testing.T) {
}
}
// run simulations
RunWithSeeds(t, interBlockCachingAppFactory, setupStateFactory, seeds, captureAndCheckHash)
RunWithSeeds(t, interBlockCachingAppFactory, setupStateFactory, seeds, []int64{}, captureAndCheckHash)
}

type ComparableStoreApp interface {
Expand Down Expand Up @@ -239,8 +240,19 @@ func AssertEqualStores(t *testing.T, app ComparableStoreApp, newApp ComparableSt
}

func FuzzFullAppSimulation(f *testing.F) {
f.Add(int64(1))
f.Fuzz(func(t *testing.T, seed int64) {
RunWithSeeds(t, NewSimApp, setupStateFactory, []int64{seed})
// f.Add(int64(1))
f.Fuzz(func(t *testing.T, raw_seeds []byte) {
if len(raw_seeds) < 8 {
t.Skip()
}
var seeds, xseeds []int64
for len(raw_seeds) > 7 {
seeds = append(seeds, int64(binary.BigEndian.Uint64(raw_seeds)))
raw_seeds = raw_seeds[8:]
}
if len(seeds) > 1 {
xseeds = seeds[1:]
}
RunWithSeeds(t, NewSimApp, setupStateFactory, []int64{seeds[0]}, xseeds)
})
}
1 change: 1 addition & 0 deletions types/simulation/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Config struct {

DBBackend string // custom db backend type
BlockMaxGas int64 // custom max gas for block
XSeeds []int64
}

func (c Config) Clone() Config {
Expand Down
38 changes: 37 additions & 1 deletion x/simulation/simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ func SimulateFromSeed(
// in case we have to end early, don't os.Exit so that we can run cleanup code.
testingMode, _, b := getTestingMode(tb)

r := rand.New(rand.NewSource(config.Seed))
// r := rand.New(rand.NewSource(config.Seed))
var rng arraySource
rng.arr = config.XSeeds
rng.src = rand.New(rand.NewSource(config.Seed))

Check failure

Code scanning / gosec

Use of weak random number generator (math/rand instead of crypto/rand) Error

Use of weak random number generator (math/rand instead of crypto/rand)
r := rand.New(&rng)

Check failure

Code scanning / gosec

Use of weak random number generator (math/rand instead of crypto/rand) Error

Use of weak random number generator (math/rand instead of crypto/rand)
params := RandomParams(r)

startTime := time.Now()

Check warning

Code scanning / CodeQL

Calling the system time Warning

Calling the system time may be a possible source of non-determinism
Expand Down Expand Up @@ -414,3 +418,35 @@ func runQueuedTimeOperations(tb testing.TB, queueOps []simulation.FutureOperatio

return numOpsRan, allFutureOps
}

const (
rngMax = 1 << 63
rngMask = rngMax - 1
)

type arraySource struct {
pos int
arr []int64
src *rand.Rand
}

// Int63 returns a non-negative pseudo-random 63-bit integer as an int64.
func (rng *arraySource) Int63() int64 {
return int64(rng.Uint64() & rngMask)
}

// Uint64 returns a non-negative pseudo-random 64-bit integer as an uint64.
func (rng *arraySource) Uint64() uint64 {
if rng.pos >= len(rng.arr) {
return rng.src.Uint64()
}
val := rng.arr[rng.pos]
rng.pos = rng.pos + 1

Check failure on line 444 in x/simulation/simulate.go

View workflow job for this annotation

GitHub Actions / golangci-lint

assignOp: replace `rng.pos = rng.pos + 1` with `rng.pos++` (gocritic)
if val < 0 {
return uint64(-val)
}

return uint64(val)
}

func (rng *arraySource) Seed(seed int64) {}

0 comments on commit 2bdf61b

Please sign in to comment.