-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(cherry picked from commit b8638f8) # Conflicts: # scripts/build/simulations.mk
- Loading branch information
1 parent
0455db9
commit 6bbcbf9
Showing
8 changed files
with
239 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/simapp.test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//go:build sims | ||
|
||
package simapp | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/x/simulation/client/cli" | ||
"testing" | ||
) | ||
|
||
func BenchmarkFullAppSimulation(b *testing.B) { | ||
b.ReportAllocs() | ||
cfg := cli.NewConfigFromFlags() | ||
cfg.ChainID = SimAppChainID | ||
for i := 0; i < b.N; i++ { | ||
RunWithSeed[Tx](b, NewSimApp[Tx], AppConfig, cfg, 1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//go:build sims | ||
|
||
package simapp | ||
|
||
import ( | ||
simsxv2 "github.com/cosmos/cosmos-sdk/simsx/v2" | ||
simcli "github.com/cosmos/cosmos-sdk/x/simulation/client/cli" | ||
"testing" | ||
) | ||
|
||
func FuzzFullAppSimulation(f *testing.F) { | ||
cfg := simcli.NewConfigFromFlags() | ||
cfg.ChainID = SimAppChainID | ||
|
||
f.Fuzz(func(t *testing.T, rawSeed []byte) { | ||
if len(rawSeed) < 8 { | ||
t.Skip() | ||
return | ||
} | ||
randSource := simsxv2.NewByteSource(cfg.FuzzSeed, cfg.Seed) | ||
RunWithRandSource[Tx](t, NewSimApp[Tx], AppConfig, cfg, randSource) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package v2 | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"io" | ||
"math/rand" | ||
) | ||
|
||
const ( | ||
rngMax = 1 << 63 | ||
rngMask = rngMax - 1 | ||
) | ||
|
||
// RandSource defines an interface for random number sources with a method to retrieve the seed. | ||
type RandSource interface { | ||
rand.Source | ||
GetSeed() int64 | ||
} | ||
|
||
var ( | ||
_ RandSource = &SeededRandomSource{} | ||
_ RandSource = &ByteSource{} | ||
) | ||
|
||
// SeededRandomSource wraps a random source with an associated seed value for reproducible random number generation. | ||
// It implements the RandSource interface, allowing access to both the random source and seed. | ||
type SeededRandomSource struct { | ||
rand.Source | ||
seed int64 | ||
} | ||
|
||
// NewSeededRandSource constructor | ||
func NewSeededRandSource(seed int64) *SeededRandomSource { | ||
r := new(SeededRandomSource) | ||
r.Seed(seed) | ||
return r | ||
} | ||
|
||
func (r *SeededRandomSource) Seed(seed int64) { | ||
r.seed = seed | ||
r.Source = rand.NewSource(seed) | ||
} | ||
|
||
func (r SeededRandomSource) GetSeed() int64 { | ||
return r.seed | ||
} | ||
|
||
// ByteSource offers deterministic pseudo-random numbers for math.Rand with fuzzer support. | ||
// The 'seed' data is read in big endian to uint64. When exhausted, | ||
// it falls back to a standard random number generator initialized with a specific 'seed' value. | ||
type ByteSource struct { | ||
seed *bytes.Reader | ||
fallback *rand.Rand | ||
} | ||
|
||
// NewByteSource creates a new ByteSource with a specified byte slice and seed. This gives a fixed sequence of pseudo-random numbers. | ||
// Initially, it utilizes the byte slice. Once that's exhausted, it continues generating numbers using the provided seed. | ||
func NewByteSource(fuzzSeed []byte, seed int64) *ByteSource { | ||
return &ByteSource{ | ||
seed: bytes.NewReader(fuzzSeed), | ||
fallback: rand.New(rand.NewSource(seed)), | ||
} | ||
} | ||
|
||
func (s *ByteSource) Uint64() uint64 { | ||
if s.seed.Len() < 8 { | ||
return s.fallback.Uint64() | ||
} | ||
var b [8]byte | ||
if _, err := s.seed.Read(b[:]); err != nil && err != io.EOF { | ||
panic(err) // Should not happen. | ||
} | ||
return binary.BigEndian.Uint64(b[:]) | ||
} | ||
|
||
func (s *ByteSource) Int63() int64 { | ||
return int64(s.Uint64() & rngMask) | ||
} | ||
|
||
// Seed is not supported and will panic | ||
func (s *ByteSource) Seed(seed int64) { | ||
panic("not supported") | ||
} | ||
|
||
// GetSeed is not supported and will panic | ||
func (s ByteSource) GetSeed() int64 { | ||
panic("not supported") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package v2 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSeededRandSource(t *testing.T) { | ||
const ( | ||
seed1 int64 = 1 | ||
firstValFromSeed1 int64 = 0x4d65822107fcfd52 | ||
secondValFromSeed1 int64 = 0x78629a0f5f3f164f | ||
) | ||
src := NewSeededRandSource(seed1) | ||
for _, v := range []int64{firstValFromSeed1, secondValFromSeed1} { | ||
assert.Equal(t, v, src.Int63()) | ||
} | ||
assert.Equal(t, seed1, src.GetSeed()) | ||
} | ||
|
||
func TestByteSource(t *testing.T) { | ||
const ( | ||
seed1 = 1 | ||
firstValFromSeed1 = 0x4d65822107fcfd52 | ||
secondValFromSeed1 = 0x78629a0f5f3f164f | ||
) | ||
specs := map[string]struct { | ||
fuzzSeed []byte | ||
exp []uint64 | ||
}{ | ||
"fallback fuzz takes over": { | ||
fuzzSeed: []byte{}, | ||
exp: []uint64{firstValFromSeed1, secondValFromSeed1}, | ||
}, | ||
"fuzzSeeds served first": { | ||
fuzzSeed: []byte{ | ||
1, 2, 3, 4, 5, 6, 7, 8, | ||
9, 10, 11, 12, 13, 14, 15, 16, | ||
17, 18, // incomplete uin64, should be ignored | ||
}, | ||
exp: []uint64{0x102030405060708, 0x90a0b0c0d0e0f10, firstValFromSeed1}, | ||
}, | ||
} | ||
for name, spec := range specs { | ||
t.Run(name, func(t *testing.T) { | ||
byteSource := NewByteSource(spec.fuzzSeed, seed1) | ||
for _, v := range spec.exp { | ||
assert.Equal(t, v, byteSource.Uint64()) | ||
} | ||
}) | ||
} | ||
} |