all: update math/rand to math/rand/v2

To ease the potential error and confusion among math/rand and
crypto/rand. math/rand/v2 has been introduced from 1.22.
This can avoid critical errors wehn calling rand.Read() in the
security-sensitive places. Other improvement can refer the golang doc
This commit is contained in:
Yang Hau 2024-09-08 11:47:55 +02:00
parent d71831255d
commit 93c2bc0f2a
81 changed files with 382 additions and 294 deletions

View file

@ -19,7 +19,7 @@ package keystore
import ( import (
"errors" "errors"
"fmt" "fmt"
"math/rand" "math/rand/v2"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"

View file

@ -17,7 +17,7 @@
package keystore package keystore
import ( import (
"math/rand" "math/rand/v2"
"os" "os"
"runtime" "runtime"
"slices" "slices"

View file

@ -19,7 +19,7 @@ package light
import ( import (
"crypto/rand" "crypto/rand"
"crypto/sha256" "crypto/sha256"
mrand "math/rand" mrand "math/rand/v2"
"github.com/ethereum/go-ethereum/beacon/merkle" "github.com/ethereum/go-ethereum/beacon/merkle"
"github.com/ethereum/go-ethereum/beacon/params" "github.com/ethereum/go-ethereum/beacon/params"
@ -74,7 +74,7 @@ func GenerateTestCheckpoint(period uint64, committee *types.SerializedSyncCommit
func makeBitmask(signerCount int) (bitmask [params.SyncCommitteeBitmaskSize]byte) { func makeBitmask(signerCount int) (bitmask [params.SyncCommitteeBitmaskSize]byte) {
for i := 0; i < params.SyncCommitteeSize; i++ { for i := 0; i < params.SyncCommitteeSize; i++ {
if mrand.Intn(params.SyncCommitteeSize-i) < signerCount { if mrand.IntN(params.SyncCommitteeSize-i) < signerCount {
bitmask[i/8] += byte(1) << (i & 7) bitmask[i/8] += byte(1) << (i & 7)
signerCount-- signerCount--
} }

View file

@ -21,7 +21,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
"math/rand" "math/rand/v2"
"reflect" "reflect"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -758,7 +758,7 @@ func (s *Suite) snapGetAccountRange(t *utesting.T, tc *accRangeTest) error {
} }
// write request // write request
req := &snap.GetAccountRangePacket{ req := &snap.GetAccountRangePacket{
ID: uint64(rand.Int63()), ID: uint64(rand.Int64()),
Root: tc.root, Root: tc.root,
Origin: tc.startingHash, Origin: tc.startingHash,
Limit: tc.limitHash, Limit: tc.limitHash,
@ -828,7 +828,7 @@ func (s *Suite) snapGetStorageRanges(t *utesting.T, tc *stRangesTest) error {
// write request // write request
req := &snap.GetStorageRangesPacket{ req := &snap.GetStorageRangesPacket{
ID: uint64(rand.Int63()), ID: uint64(rand.Int64()),
Root: tc.root, Root: tc.root,
Accounts: tc.accounts, Accounts: tc.accounts,
Origin: tc.origin, Origin: tc.origin,
@ -882,7 +882,7 @@ func (s *Suite) snapGetByteCodes(t *utesting.T, tc *byteCodesTest) error {
} }
// write request // write request
req := &snap.GetByteCodesPacket{ req := &snap.GetByteCodesPacket{
ID: uint64(rand.Int63()), ID: uint64(rand.Int64()),
Hashes: tc.hashes, Hashes: tc.hashes,
Bytes: tc.nBytes, Bytes: tc.nBytes,
} }
@ -942,7 +942,7 @@ func (s *Suite) snapGetTrieNodes(t *utesting.T, tc *trieNodesTest) error {
// write0 request // write0 request
req := &snap.GetTrieNodesPacket{ req := &snap.GetTrieNodesPacket{
ID: uint64(rand.Int63()), ID: uint64(rand.Int64()),
Root: tc.root, Root: tc.root,
Paths: tc.paths, Paths: tc.paths,
Bytes: tc.nBytes, Bytes: tc.nBytes,

View file

@ -24,7 +24,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"math/rand" "math/rand/v2"
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
@ -201,7 +201,7 @@ func TestFileOut(t *testing.T) {
var ( var (
have, want []byte have, want []byte
err error err error
path = fmt.Sprintf("%s/test_file_out-%d", t.TempDir(), rand.Int63()) path = fmt.Sprintf("%s/test_file_out-%d", t.TempDir(), rand.Int64())
) )
if want, err = runSelf(fmt.Sprintf("--log.file=%s", path), "logtest"); err != nil { if want, err = runSelf(fmt.Sprintf("--log.file=%s", path), "logtest"); err != nil {
t.Fatal(err) t.Fatal(err)
@ -221,7 +221,7 @@ func TestRotatingFileOut(t *testing.T) {
var ( var (
have, want []byte have, want []byte
err error err error
path = fmt.Sprintf("%s/test_file_out-%d", t.TempDir(), rand.Int63()) path = fmt.Sprintf("%s/test_file_out-%d", t.TempDir(), rand.Int64())
) )
if want, err = runSelf(fmt.Sprintf("--log.file=%s", path), "--log.rotate", "logtest"); err != nil { if want, err = runSelf(fmt.Sprintf("--log.file=%s", path), "--log.rotate", "logtest"); err != nil {
t.Fatal(err) t.Fatal(err)

View file

@ -19,7 +19,7 @@ package bitutil
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"math/rand" "math/rand/v2"
"testing" "testing"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
@ -167,14 +167,14 @@ func BenchmarkEncoding4KBSaturated(b *testing.B) { benchmarkEncoding(b, 4096, 0.
func benchmarkEncoding(b *testing.B, bytes int, fill float64) { func benchmarkEncoding(b *testing.B, bytes int, fill float64) {
// Generate a random slice of bytes to compress // Generate a random slice of bytes to compress
random := rand.NewSource(0) // reproducible and comparable random := rand.New(rand.NewPCG(0, 0)) // reproducible and comparable
data := make([]byte, bytes) data := make([]byte, bytes)
bits := int(float64(bytes) * 8 * fill) bits := int(float64(bytes) * 8 * fill)
for i := 0; i < bits; i++ { for i := 0; i < bits; i++ {
idx := random.Int63() % int64(len(data)) idx := random.Int64() % int64(len(data))
bit := uint(random.Int63() % 8) bit := uint(random.Int64() % 8)
data[idx] |= 1 << bit data[idx] |= 1 << bit
} }
// Reset the benchmark and measure encoding/decoding // Reset the benchmark and measure encoding/decoding

View file

@ -20,7 +20,7 @@ import (
crand "crypto/rand" crand "crypto/rand"
"fmt" "fmt"
"io" "io"
"math/rand" "math/rand/v2"
"testing" "testing"
) )
@ -192,7 +192,7 @@ func BenchmarkLRU(b *testing.B) {
values = make([][]byte, capacity) values = make([][]byte, capacity)
) )
for i := range indexes { for i := range indexes {
indexes[i] = rand.Intn(capacity) indexes[i] = rand.IntN(capacity)
} }
for i := range keys { for i := range keys {
b := make([]byte, 32) b := make([]byte, 32)

View file

@ -17,7 +17,7 @@
package prque package prque
import ( import (
"math/rand" "math/rand/v2"
"sync" "sync"
"testing" "testing"
"time" "time"
@ -65,7 +65,7 @@ func TestLazyQueue(t *testing.T) {
) )
for i := range items[:] { for i := range items[:] {
items[i].p = rand.Int63n(testPriorityStep * 10) items[i].p = rand.Int64N(testPriorityStep * 10)
if items[i].p > maxPri { if items[i].p > maxPri {
maxPri = items[i].p maxPri = items[i].p
} }
@ -95,9 +95,9 @@ func TestLazyQueue(t *testing.T) {
}() }()
for c := 0; c < testSteps; c++ { for c := 0; c < testSteps; c++ {
i := rand.Intn(testItems) i := rand.IntN(testItems)
lock.Lock() lock.Lock()
items[i].p += rand.Int63n(testPriorityStep*2-1) + 1 items[i].p += rand.Int64N(testPriorityStep*2-1) + 1
if items[i].p > maxPri { if items[i].p > maxPri {
maxPri = items[i].p maxPri = items[i].p
} }
@ -105,7 +105,7 @@ func TestLazyQueue(t *testing.T) {
if items[i].p > items[i].maxp { if items[i].p > items[i].maxp {
q.Update(items[i].index) q.Update(items[i].index)
} }
if rand.Intn(100) == 0 { if rand.IntN(100) == 0 {
p := q.PopItem().(*lazyItem) p := q.PopItem().(*lazyItem)
if p.p != maxPri { if p.p != maxPri {
lock.Unlock() lock.Unlock()

View file

@ -9,7 +9,7 @@
package prque package prque
import ( import (
"math/rand" "math/rand/v2"
"testing" "testing"
) )
@ -103,7 +103,7 @@ func BenchmarkPush(b *testing.B) {
prio := make([]int64, b.N) prio := make([]int64, b.N)
for i := 0; i < len(data); i++ { for i := 0; i < len(data); i++ {
data[i] = rand.Int() data[i] = rand.Int()
prio[i] = rand.Int63() prio[i] = rand.Int64()
} }
// Execute the benchmark // Execute the benchmark
b.ResetTimer() b.ResetTimer()
@ -119,7 +119,7 @@ func BenchmarkPop(b *testing.B) {
prio := make([]int64, b.N) prio := make([]int64, b.N)
for i := 0; i < len(data); i++ { for i := 0; i < len(data); i++ {
data[i] = rand.Int() data[i] = rand.Int()
prio[i] = rand.Int63() prio[i] = rand.Int64()
} }
queue := New[int64, int](nil) queue := New[int64, int](nil)
for i := 0; i < len(data); i++ { for i := 0; i < len(data); i++ {

View file

@ -9,7 +9,7 @@
package prque package prque
import ( import (
"math/rand" "math/rand/v2"
"sort" "sort"
"testing" "testing"
) )
@ -19,7 +19,7 @@ func TestSstack(t *testing.T) {
size := 16 * blockSize size := 16 * blockSize
data := make([]*item[int64, int], size) data := make([]*item[int64, int], size)
for i := 0; i < size; i++ { for i := 0; i < size; i++ {
data[i] = &item[int64, int]{rand.Int(), rand.Int63()} data[i] = &item[int64, int]{rand.Int(), rand.Int64()}
} }
stack := newSstack[int64, int](nil) stack := newSstack[int64, int](nil)
for rep := 0; rep < 2; rep++ { for rep := 0; rep < 2; rep++ {
@ -74,7 +74,7 @@ func TestSstackReset(t *testing.T) {
size := 16 * blockSize size := 16 * blockSize
data := make([]*item[int64, int], size) data := make([]*item[int64, int], size)
for i := 0; i < size; i++ { for i := 0; i < size; i++ {
data[i] = &item[int64, int]{rand.Int(), rand.Int63()} data[i] = &item[int64, int]{rand.Int(), rand.Int64()}
} }
stack := newSstack[int64, int](nil) stack := newSstack[int64, int](nil)
for rep := 0; rep < 2; rep++ { for rep := 0; rep < 2; rep++ {

View file

@ -24,7 +24,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
"math/rand" "math/rand/v2"
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
@ -154,7 +154,7 @@ func (h *Hash) SetBytes(b []byte) {
// Generate implements testing/quick.Generator. // Generate implements testing/quick.Generator.
func (h Hash) Generate(rand *rand.Rand, size int) reflect.Value { func (h Hash) Generate(rand *rand.Rand, size int) reflect.Value {
m := rand.Intn(len(h)) m := rand.IntN(len(h))
for i := len(h) - 1; i > m; i-- { for i := len(h) - 1; i > m; i-- {
h[i] = byte(rand.Uint32()) h[i] = byte(rand.Uint32())
} }

View file

@ -23,7 +23,7 @@ import (
"fmt" "fmt"
"io" "io"
"math/big" "math/big"
"math/rand" "math/rand/v2"
"sync" "sync"
"time" "time"
@ -534,7 +534,7 @@ func (c *Clique) Prepare(chain consensus.ChainHeaderReader, header *types.Header
} }
// If there's pending proposals, cast a vote on them // If there's pending proposals, cast a vote on them
if len(addresses) > 0 { if len(addresses) > 0 {
header.Coinbase = addresses[rand.Intn(len(addresses))] header.Coinbase = addresses[rand.IntN(len(addresses))]
if c.proposals[header.Coinbase] { if c.proposals[header.Coinbase] {
copy(header.Nonce[:], nonceAuthVote) copy(header.Nonce[:], nonceAuthVote)
} else { } else {
@ -651,7 +651,7 @@ func (c *Clique) Seal(chain consensus.ChainHeaderReader, block *types.Block, res
if header.Difficulty.Cmp(diffNoTurn) == 0 { if header.Difficulty.Cmp(diffNoTurn) == 0 {
// It's not our turn explicitly to sign, delay it a bit // It's not our turn explicitly to sign, delay it a bit
wiggle := time.Duration(len(snap.Signers)/2+1) * wiggleTime wiggle := time.Duration(len(snap.Signers)/2+1) * wiggleTime
delay += time.Duration(rand.Int63n(int64(wiggle))) delay += time.Duration(rand.Int64N(int64(wiggle)))
log.Trace("Out-of-turn signing requested", "wiggle", common.PrettyDuration(wiggle)) log.Trace("Out-of-turn signing requested", "wiggle", common.PrettyDuration(wiggle))
} }

View file

@ -21,7 +21,7 @@ import (
"encoding/binary" "encoding/binary"
"encoding/json" "encoding/json"
"math/big" "math/big"
"math/rand" "math/rand/v2"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"

View file

@ -20,7 +20,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
"math/rand" "math/rand/v2"
"os" "os"
"path" "path"
"sync" "sync"

View file

@ -19,7 +19,7 @@ package bloombits
import ( import (
"bytes" "bytes"
crand "crypto/rand" crand "crypto/rand"
"math/rand" "math/rand/v2"
"testing" "testing"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"

View file

@ -18,7 +18,7 @@ package bloombits
import ( import (
"context" "context"
"math/rand" "math/rand/v2"
"sync/atomic" "sync/atomic"
"testing" "testing"
"time" "time"
@ -115,7 +115,7 @@ func makeRandomIndexes(lengths []int, max int) [][]bloomIndexes {
res[i] = make([]bloomIndexes, topics) res[i] = make([]bloomIndexes, topics)
for j := 0; j < topics; j++ { for j := 0; j < topics; j++ {
for k := 0; k < len(res[i][j]); k++ { for k := 0; k < len(res[i][j]); k++ {
res[i][j][k] = uint(rand.Intn(max-1) + 2) res[i][j][k] = uint(rand.IntN(max-1) + 2)
} }
} }
} }

View file

@ -21,7 +21,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
"math/rand" "math/rand/v2"
"testing" "testing"
"time" "time"
@ -56,8 +56,8 @@ func testChainIndexer(t *testing.T, count int) {
backends := make([]*testChainIndexBackend, count) backends := make([]*testChainIndexBackend, count)
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
var ( var (
sectionSize = uint64(rand.Intn(100) + 1) sectionSize = uint64(rand.IntN(100) + 1)
confirmsReq = uint64(rand.Intn(10)) confirmsReq = uint64(rand.IntN(10))
) )
backends[i] = &testChainIndexBackend{t: t, processCh: make(chan uint64)} backends[i] = &testChainIndexBackend{t: t, processCh: make(chan uint64)}
backends[i].indexer = NewChainIndexer(db, rawdb.NewTable(db, string([]byte{byte(i)})), backends[i], sectionSize, confirmsReq, 0, fmt.Sprintf("indexer-%d", i)) backends[i].indexer = NewChainIndexer(db, rawdb.NewTable(db, string([]byte{byte(i)})), backends[i], sectionSize, confirmsReq, 0, fmt.Sprintf("indexer-%d", i))
@ -92,7 +92,7 @@ func testChainIndexer(t *testing.T, count int) {
} }
// inject inserts a new random canonical header into the database directly // inject inserts a new random canonical header into the database directly
inject := func(number uint64) { inject := func(number uint64) {
header := &types.Header{Number: big.NewInt(int64(number)), Extra: big.NewInt(rand.Int63()).Bytes()} header := &types.Header{Number: big.NewInt(int64(number)), Extra: big.NewInt(rand.Int64()).Bytes()}
if number > 0 { if number > 0 {
header.ParentHash = rawdb.ReadCanonicalHash(db, number-1) header.ParentHash = rawdb.ReadCanonicalHash(db, number-1)
} }

View file

@ -21,7 +21,7 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"math/big" "math/big"
"math/rand" "math/rand/v2"
"os" "os"
"reflect" "reflect"
"testing" "testing"

View file

@ -20,7 +20,7 @@ import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"math/rand" "math/rand/v2"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -1205,7 +1205,7 @@ func (randTest) Generate(r *rand.Rand, size int) reflect.Value {
return nil return nil
} }
var ret []uint64 var ret []uint64
index := rand.Intn(length) index := rand.IntN(length)
for i := index; len(ret) < n && i < length; i++ { for i := index; len(ret) < n && i < length; i++ {
ret = append(ret, items[i]) ret = append(ret, items[i])
} }
@ -1229,11 +1229,11 @@ func (randTest) Generate(r *rand.Rand, size int) reflect.Value {
var steps randTest var steps randTest
for i := 0; i < size; i++ { for i := 0; i < size; i++ {
step := randTestStep{op: r.Intn(opMax)} step := randTestStep{op: r.IntN(opMax)}
switch step.op { switch step.op {
case opReload, opCheckAll: case opReload, opCheckAll:
case opAppend: case opAppend:
num := r.Intn(3) num := r.IntN(3)
step.items = addItems(num) step.items = addItems(num)
if len(step.items) == 0 { if len(step.items) == 0 {
step.blobs = nil step.blobs = nil
@ -1241,12 +1241,12 @@ func (randTest) Generate(r *rand.Rand, size int) reflect.Value {
step.blobs = getVals(step.items[0], num) step.blobs = getVals(step.items[0], num)
} }
case opRetrieve: case opRetrieve:
step.items = getItems(r.Intn(3)) step.items = getItems(r.IntN(3))
case opTruncateHead: case opTruncateHead:
if len(items) == 0 { if len(items) == 0 {
step.target = deleted step.target = deleted
} else { } else {
index := r.Intn(len(items)) index := r.IntN(len(items))
items = items[:index] items = items[:index]
step.target = deleted + uint64(index) step.target = deleted + uint64(index)
} }
@ -1257,7 +1257,7 @@ func (randTest) Generate(r *rand.Rand, size int) reflect.Value {
if len(items) == 0 { if len(items) == 0 {
step.target = deleted step.target = deleted
} else { } else {
index := r.Intn(len(items)) index := r.IntN(len(items))
items = items[index:] items = items[index:]
deleted += uint64(index) deleted += uint64(index)
step.target = deleted step.target = deleted

View file

@ -21,7 +21,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
"math/rand" "math/rand/v2"
"sync" "sync"
"testing" "testing"
@ -166,7 +166,7 @@ func TestFreezerConcurrentModifyRetrieve(t *testing.T) {
defer wg.Done() defer wg.Done()
for frozen := range written { for frozen := range written {
for rc := 0; rc < 80; rc++ { for rc := 0; rc < 80; rc++ {
num := uint64(rand.Intn(int(frozen))) num := uint64(rand.IntN(int(frozen)))
value, err := f.Ancient("test", num) value, err := f.Ancient("test", num)
if err != nil { if err != nil {
panic(fmt.Errorf("error reading %d (frozen %d): %v", num, frozen, err)) panic(fmt.Errorf("error reading %d (frozen %d): %v", num, frozen, err))

View file

@ -20,7 +20,7 @@ import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"math" "math"
"math/rand" "math/rand/v2"
"slices" "slices"
"sync" "sync"
"sync/atomic" "sync/atomic"
@ -80,15 +80,15 @@ var (
func init() { func init() {
// Init the bloom offsets in the range [0:24] (requires 8 bytes) // Init the bloom offsets in the range [0:24] (requires 8 bytes)
bloomDestructHasherOffset = rand.Intn(25) bloomDestructHasherOffset = rand.IntN(25)
bloomAccountHasherOffset = rand.Intn(25) bloomAccountHasherOffset = rand.IntN(25)
bloomStorageHasherOffset = rand.Intn(25) bloomStorageHasherOffset = rand.IntN(25)
// The destruct and account blooms must be different, as the storage slots // The destruct and account blooms must be different, as the storage slots
// will check for destruction too for every bloom miss. It should not collide // will check for destruction too for every bloom miss. It should not collide
// with modified accounts. // with modified accounts.
for bloomAccountHasherOffset == bloomDestructHasherOffset { for bloomAccountHasherOffset == bloomDestructHasherOffset {
bloomAccountHasherOffset = rand.Intn(25) bloomAccountHasherOffset = rand.IntN(25)
} }
} }

View file

@ -19,7 +19,7 @@ package snapshot
import ( import (
"bytes" "bytes"
crand "crypto/rand" crand "crypto/rand"
"math/rand" "math/rand/v2"
"testing" "testing"
"github.com/VictoriaMetrics/fastcache" "github.com/VictoriaMetrics/fastcache"
@ -68,10 +68,10 @@ func TestMergeBasics(t *testing.T) {
data := randomAccount() data := randomAccount()
accounts[h] = data accounts[h] = data
if rand.Intn(4) == 0 { if rand.IntN(4) == 0 {
destructs[h] = struct{}{} destructs[h] = struct{}{}
} }
if rand.Intn(2) == 0 { if rand.IntN(2) == 0 {
accStorage := make(map[common.Hash][]byte) accStorage := make(map[common.Hash][]byte)
value := make([]byte, 32) value := make([]byte, 32)
crand.Read(value) crand.Read(value)

View file

@ -21,7 +21,7 @@ import (
crand "crypto/rand" crand "crypto/rand"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"math/rand" "math/rand/v2"
"testing" "testing"
"github.com/VictoriaMetrics/fastcache" "github.com/VictoriaMetrics/fastcache"
@ -42,10 +42,10 @@ func TestAccountIteratorBasics(t *testing.T) {
data := randomAccount() data := randomAccount()
accounts[h] = data accounts[h] = data
if rand.Intn(4) == 0 { if rand.IntN(4) == 0 {
destructs[h] = struct{}{} destructs[h] = struct{}{}
} }
if rand.Intn(2) == 0 { if rand.IntN(2) == 0 {
accStorage := make(map[common.Hash][]byte) accStorage := make(map[common.Hash][]byte)
value := make([]byte, 32) value := make([]byte, 32)
crand.Read(value) crand.Read(value)
@ -81,7 +81,7 @@ func TestStorageIteratorBasics(t *testing.T) {
var nilstorage int var nilstorage int
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
crand.Read(value) crand.Read(value)
if rand.Intn(2) == 0 { if rand.IntN(2) == 0 {
accStorage[randomHash()] = common.CopyBytes(value) accStorage[randomHash()] = common.CopyBytes(value)
} else { } else {
accStorage[randomHash()] = nil // delete slot accStorage[randomHash()] = nil // delete slot
@ -1028,7 +1028,7 @@ func benchmarkAccountIteration(b *testing.B, iterator func(snap snapshot) Accoun
layers[i] = make(map[common.Hash][]byte) layers[i] = make(map[common.Hash][]byte)
} }
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
depth := rand.Intn(len(layers)) depth := rand.IntN(len(layers))
layers[depth][randomHash()] = randomAccount() layers[depth][randomHash()] = randomAccount()
} }
stack := snapshot(emptyLayer()) stack := snapshot(emptyLayer())

View file

@ -20,7 +20,7 @@ import (
crand "crypto/rand" crand "crypto/rand"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"math/rand" "math/rand/v2"
"testing" "testing"
"time" "time"

View file

@ -22,7 +22,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"math" "math"
"math/rand" "math/rand/v2"
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
@ -109,7 +109,7 @@ func newStateTestAction(addr common.Address, r *rand.Rand, index int) testAction
} }
var nonRandom = index != -1 var nonRandom = index != -1
if index == -1 { if index == -1 {
index = r.Intn(len(actions)) index = r.IntN(len(actions))
} }
action := actions[index] action := actions[index]
var names []string var names []string
@ -118,9 +118,9 @@ func newStateTestAction(addr common.Address, r *rand.Rand, index int) testAction
} }
for i := range action.args { for i := range action.args {
if nonRandom { if nonRandom {
action.args[i] = rand.Int63n(10000) + 1 // set balance to non-zero action.args[i] = rand.Int64N(10000) + 1 // set balance to non-zero
} else { } else {
action.args[i] = rand.Int63n(10000) action.args[i] = rand.Int64N(10000)
} }
names = append(names, fmt.Sprint(action.args[i])) names = append(names, fmt.Sprint(action.args[i]))
} }
@ -135,7 +135,7 @@ func (*stateTest) Generate(r *rand.Rand, size int) reflect.Value {
for i := range addrs { for i := range addrs {
addrs[i][0] = byte(i) addrs[i][0] = byte(i)
} }
actions := make([][]testAction, rand.Intn(5)+1) actions := make([][]testAction, rand.IntN(5)+1)
for i := 0; i < len(actions); i++ { for i := 0; i < len(actions); i++ {
actions[i] = make([]testAction, size) actions[i] = make([]testAction, size)
@ -146,7 +146,7 @@ func (*stateTest) Generate(r *rand.Rand, size int) reflect.Value {
actions[i][j] = newStateTestAction(common.HexToAddress("0xdeadbeef"), r, 0) actions[i][j] = newStateTestAction(common.HexToAddress("0xdeadbeef"), r, 0)
continue continue
} }
actions[i][j] = newStateTestAction(addrs[r.Intn(len(addrs))], r, -1) actions[i][j] = newStateTestAction(addrs[r.IntN(len(addrs))], r, -1)
} }
} }
chunk := int(math.Sqrt(float64(size))) chunk := int(math.Sqrt(float64(size)))
@ -197,13 +197,13 @@ func (test *stateTest) run() bool {
} }
disk = rawdb.NewMemoryDatabase() disk = rawdb.NewMemoryDatabase()
tdb = triedb.NewDatabase(disk, &triedb.Config{PathDB: pathdb.Defaults}) tdb = triedb.NewDatabase(disk, &triedb.Config{PathDB: pathdb.Defaults})
byzantium = rand.Intn(2) == 0 byzantium = rand.IntN(2) == 0
) )
defer disk.Close() defer disk.Close()
defer tdb.Close() defer tdb.Close()
var snaps *snapshot.Tree var snaps *snapshot.Tree
if rand.Intn(3) == 0 { if rand.IntN(3) == 0 {
snaps, _ = snapshot.New(snapshot.Config{ snaps, _ = snapshot.New(snapshot.Config{
CacheSize: 1, CacheSize: 1,
Recovery: false, Recovery: false,

View file

@ -22,7 +22,7 @@ import (
"fmt" "fmt"
"maps" "maps"
"math" "math"
"math/rand" "math/rand/v2"
"reflect" "reflect"
"slices" "slices"
"strings" "strings"
@ -471,13 +471,13 @@ func newTestAction(addr common.Address, r *rand.Rand) testAction {
args: make([]int64, 2), args: make([]int64, 2),
}, },
} }
action := actions[r.Intn(len(actions))] action := actions[r.IntN(len(actions))]
var nameargs []string var nameargs []string
if !action.noAddr { if !action.noAddr {
nameargs = append(nameargs, addr.Hex()) nameargs = append(nameargs, addr.Hex())
} }
for i := range action.args { for i := range action.args {
action.args[i] = rand.Int63n(100) action.args[i] = rand.Int64N(100)
nameargs = append(nameargs, fmt.Sprint(action.args[i])) nameargs = append(nameargs, fmt.Sprint(action.args[i]))
} }
action.name += strings.Join(nameargs, ", ") action.name += strings.Join(nameargs, ", ")
@ -494,7 +494,7 @@ func (*snapshotTest) Generate(r *rand.Rand, size int) reflect.Value {
} }
actions := make([]testAction, size) actions := make([]testAction, size)
for i := range actions { for i := range actions {
addr := addrs[r.Intn(len(addrs))] addr := addrs[r.IntN(len(addrs))]
actions[i] = newTestAction(addr, r) actions[i] = newTestAction(addr, r)
} }
// Generate snapshot indexes. // Generate snapshot indexes.
@ -506,7 +506,7 @@ func (*snapshotTest) Generate(r *rand.Rand, size int) reflect.Value {
snaplen := len(actions) / nsnapshots snaplen := len(actions) / nsnapshots
for i := range snapshots { for i := range snapshots {
// Try to place the snapshots some number of actions apart from each other. // Try to place the snapshots some number of actions apart from each other.
snapshots[i] = (i * snaplen) + r.Intn(snaplen) snapshots[i] = (i * snaplen) + r.IntN(snaplen)
} }
return reflect.ValueOf(&snapshotTest{addrs, actions, snapshots, nil}) return reflect.ValueOf(&snapshotTest{addrs, actions, snapshots, nil})
} }

View file

@ -18,7 +18,7 @@ package blobpool
import ( import (
"container/heap" "container/heap"
mrand "math/rand" mrand "math/rand/v2"
"testing" "testing"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -26,7 +26,7 @@ import (
"github.com/holiman/uint256" "github.com/holiman/uint256"
) )
var rand = mrand.New(mrand.NewSource(1)) var rand = mrand.New(mrand.NewPCG(1, 1))
// verifyHeapInternals verifies that all accounts present in the index are also // verifyHeapInternals verifies that all accounts present in the index are also
// present in the heap and internals are consistent across various indices. // present in the heap and internals are consistent across various indices.
@ -193,7 +193,15 @@ func benchmarkPriceHeapReinit(b *testing.B, datacap uint64) {
index := make(map[common.Address][]*blobTxMeta) index := make(map[common.Address][]*blobTxMeta)
for i := 0; i < int(blobs); i++ { for i := 0; i < int(blobs); i++ {
var addr common.Address var addr common.Address
rand.Read(addr[:])
addr.SetBytes(func() []byte {
ret := make([]byte, common.AddressLength)
randInts := rand.Perm(256)
for i, v := range randInts {
ret[i] = byte(v)
}
return ret
}())
var ( var (
execTip = uint256.NewInt(rand.Uint64()) execTip = uint256.NewInt(rand.Uint64())
@ -253,7 +261,14 @@ func benchmarkPriceHeapOverflow(b *testing.B, datacap uint64) {
index := make(map[common.Address][]*blobTxMeta) index := make(map[common.Address][]*blobTxMeta)
for i := 0; i < int(blobs); i++ { for i := 0; i < int(blobs); i++ {
var addr common.Address var addr common.Address
rand.Read(addr[:]) addr.SetBytes(func() []byte {
ret := make([]byte, common.AddressLength)
randInts := rand.Perm(256)
for i, v := range randInts {
ret[i] = byte(v)
}
return ret
}())
var ( var (
execTip = uint256.NewInt(rand.Uint64()) execTip = uint256.NewInt(rand.Uint64())
@ -284,7 +299,14 @@ func benchmarkPriceHeapOverflow(b *testing.B, datacap uint64) {
metas = make([]*blobTxMeta, b.N) metas = make([]*blobTxMeta, b.N)
) )
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
rand.Read(addrs[i][:]) addrs[i].SetBytes(func() []byte {
ret := make([]byte, common.AddressLength)
randInts := rand.Perm(256)
for i, v := range randInts {
ret[i] = byte(v)
}
return ret
}())
var ( var (
execTip = uint256.NewInt(rand.Uint64()) execTip = uint256.NewInt(rand.Uint64())

View file

@ -22,7 +22,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
"math/rand" "math/rand/v2"
"os" "os"
"sync" "sync"
"sync/atomic" "sync/atomic"
@ -933,7 +933,7 @@ func testQueueGlobalLimiting(t *testing.T, nolocals bool) {
txs := make(types.Transactions, 0, 3*config.GlobalQueue) txs := make(types.Transactions, 0, 3*config.GlobalQueue)
for len(txs) < cap(txs) { for len(txs) < cap(txs) {
key := keys[rand.Intn(len(keys)-1)] // skip adding transactions with the local account key := keys[rand.IntN(len(keys)-1)] // skip adding transactions with the local account
addr := crypto.PubkeyToAddress(key.PublicKey) addr := crypto.PubkeyToAddress(key.PublicKey)
txs = append(txs, transaction(nonces[addr]+1, 100000, key)) txs = append(txs, transaction(nonces[addr]+1, 100000, key))
@ -1261,7 +1261,7 @@ func TestAllowedTxSize(t *testing.T) {
t.Fatalf("failed to add transaction of size %d, close to maximal: %v", int(tx.Size()), err) t.Fatalf("failed to add transaction of size %d, close to maximal: %v", int(tx.Size()), err)
} }
// Try adding a transaction with random allowed size // Try adding a transaction with random allowed size
if err := pool.addRemoteSync(pricedDataTransaction(1, pool.currentHead.Load().GasLimit, big.NewInt(1), key, uint64(rand.Intn(int(dataSize))))); err != nil { if err := pool.addRemoteSync(pricedDataTransaction(1, pool.currentHead.Load().GasLimit, big.NewInt(1), key, uint64(rand.IntN(int(dataSize))))); err != nil {
t.Fatalf("failed to add transaction of random allowed size: %v", err) t.Fatalf("failed to add transaction of random allowed size: %v", err)
} }
// Try adding a transaction of minimal not allowed size // Try adding a transaction of minimal not allowed size
@ -1269,7 +1269,7 @@ func TestAllowedTxSize(t *testing.T) {
t.Fatalf("expected rejection on slightly oversize transaction") t.Fatalf("expected rejection on slightly oversize transaction")
} }
// Try adding a transaction of random not allowed size // Try adding a transaction of random not allowed size
if err := pool.addRemoteSync(pricedDataTransaction(2, pool.currentHead.Load().GasLimit, big.NewInt(1), key, dataSize+1+uint64(rand.Intn(10*txMaxSize)))); err == nil { if err := pool.addRemoteSync(pricedDataTransaction(2, pool.currentHead.Load().GasLimit, big.NewInt(1), key, dataSize+1+uint64(rand.IntN(10*txMaxSize)))); err == nil {
t.Fatalf("expected rejection on oversize transaction") t.Fatalf("expected rejection on oversize transaction")
} }
// Run some sanity checks on the pool internals // Run some sanity checks on the pool internals

View file

@ -18,7 +18,7 @@ package legacypool
import ( import (
"math/big" "math/big"
"math/rand" "math/rand/v2"
"testing" "testing"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"

View file

@ -21,7 +21,7 @@ import (
"fmt" "fmt"
"io" "io"
"math/big" "math/big"
mrand "math/rand" mrand "math/rand/v2"
"testing" "testing"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -174,9 +174,9 @@ type dummyDerivableList struct {
func newDummy(seed int) *dummyDerivableList { func newDummy(seed int) *dummyDerivableList {
d := &dummyDerivableList{} d := &dummyDerivableList{}
src := mrand.NewSource(int64(seed)) src := mrand.New(mrand.NewPCG(uint64(seed), uint64(seed)))
// don't use lists longer than 4K items // don't use lists longer than 4K items
d.len = int(src.Int63() & 0x0FFF) d.len = int(src.Int64() & 0x0FFF)
d.seed = seed d.seed = seed
return d return d
} }
@ -186,10 +186,14 @@ func (d *dummyDerivableList) Len() int {
} }
func (d *dummyDerivableList) EncodeIndex(i int, w *bytes.Buffer) { func (d *dummyDerivableList) EncodeIndex(i int, w *bytes.Buffer) {
src := mrand.NewSource(int64(d.seed + i)) src := mrand.New(mrand.NewPCG(uint64(d.seed+i), uint64(d.seed+i)))
// max item size 256, at least 1 byte per item // max item size 256, at least 1 byte per item
size := 1 + src.Int63()&0x00FF size := 1 + src.Int64()&0x00FF
io.CopyN(w, mrand.New(src), size) b := make([]byte, size)
for i := range size {
b[i] = byte(src.Int())
}
io.CopyN(w, bytes.NewBuffer(b), size)
} }
func printList(l types.DerivableList) { func printList(l types.DerivableList) {

View file

@ -22,7 +22,7 @@ import (
crand "crypto/rand" crand "crypto/rand"
"fmt" "fmt"
"math/big" "math/big"
"math/rand" "math/rand/v2"
"reflect" "reflect"
"sync" "sync"
"testing" "testing"

View file

@ -20,7 +20,7 @@ import (
"fmt" "fmt"
"log/slog" "log/slog"
"math/big" "math/big"
"math/rand" "math/rand/v2"
"os" "os"
"sync" "sync"
"testing" "testing"
@ -317,14 +317,14 @@ func XTestDelivery(t *testing.T) {
i := 4 i := 4
for { for {
peer := dummyPeer(fmt.Sprintf("peer-%d", i)) peer := dummyPeer(fmt.Sprintf("peer-%d", i))
f, _, _ := q.ReserveBodies(peer, rand.Intn(30)) f, _, _ := q.ReserveBodies(peer, rand.IntN(30))
if f != nil { if f != nil {
var ( var (
emptyList []*types.Header emptyList []*types.Header
txset [][]*types.Transaction txset [][]*types.Transaction
uncleset [][]*types.Header uncleset [][]*types.Header
) )
numToSkip := rand.Intn(len(f.Headers)) numToSkip := rand.IntN(len(f.Headers))
for _, hdr := range f.Headers[0 : len(f.Headers)-numToSkip] { for _, hdr := range f.Headers[0 : len(f.Headers)-numToSkip] {
txset = append(txset, world.getTransactions(hdr.Number.Uint64())) txset = append(txset, world.getTransactions(hdr.Number.Uint64()))
uncleset = append(uncleset, emptyList) uncleset = append(uncleset, emptyList)
@ -356,7 +356,7 @@ func XTestDelivery(t *testing.T) {
// reserve receiptfetch // reserve receiptfetch
peer := dummyPeer("peer-3") peer := dummyPeer("peer-3")
for { for {
f, _, _ := q.ReserveReceipts(peer, rand.Intn(50)) f, _, _ := q.ReserveReceipts(peer, rand.IntN(50))
if f != nil { if f != nil {
var rcs [][]*types.Receipt var rcs [][]*types.Receipt
for _, hdr := range f.Headers { for _, hdr := range f.Headers {
@ -384,7 +384,7 @@ func XTestDelivery(t *testing.T) {
time.Sleep(300 * time.Millisecond) time.Sleep(300 * time.Millisecond)
//world.tick() //world.tick()
//fmt.Printf("trying to progress\n") //fmt.Printf("trying to progress\n")
world.progress(rand.Intn(100)) world.progress(rand.IntN(100))
} }
for i := 0; i < 50; i++ { for i := 0; i < 50; i++ {
time.Sleep(2990 * time.Millisecond) time.Sleep(2990 * time.Millisecond)

View file

@ -20,7 +20,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"math/rand" "math/rand/v2"
"sort" "sort"
"time" "time"
@ -712,7 +712,7 @@ func (s *skeleton) assignTasks(success chan *headerResponse, fail chan *headerRe
// Matched a pending task to an idle peer, allocate a unique request id // Matched a pending task to an idle peer, allocate a unique request id
var reqid uint64 var reqid uint64
for { for {
reqid = uint64(rand.Int63()) reqid = uint64(rand.Int64())
if reqid == 0 { if reqid == 0 {
continue continue
} }

View file

@ -20,7 +20,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"math" "math"
mrand "math/rand" mrand "math/rand/v2"
"sort" "sort"
"time" "time"
@ -988,7 +988,7 @@ func (f *TxFetcher) forEachPeer(peers map[string]struct{}, do func(peer string))
list = append(list, peer) list = append(list, peer)
} }
sort.Strings(list) sort.Strings(list)
rotateStrings(list, f.rand.Intn(len(list))) rotateStrings(list, f.rand.IntN(len(list)))
for _, peer := range list { for _, peer := range list {
do(peer) do(peer)
} }

View file

@ -19,7 +19,7 @@ package fetcher
import ( import (
"errors" "errors"
"math/big" "math/big"
"math/rand" "math/rand/v2"
"slices" "slices"
"testing" "testing"
"time" "time"
@ -1292,7 +1292,7 @@ func TestTransactionFetcherUnderpricedDoSProtection(t *testing.T) {
// Create a slew of transactions to max out the underpriced set // Create a slew of transactions to max out the underpriced set
var txs []*types.Transaction var txs []*types.Transaction
for i := 0; i < maxTxUnderpricedSetSize+1; i++ { for i := 0; i < maxTxUnderpricedSetSize+1; i++ {
txs = append(txs, types.NewTransaction(rand.Uint64(), common.Address{byte(rand.Intn(256))}, new(big.Int), 0, new(big.Int), nil)) txs = append(txs, types.NewTransaction(rand.Uint64(), common.Address{byte(rand.IntN(256))}, new(big.Int), 0, new(big.Int), nil))
} }
var ( var (
hashes []common.Hash hashes []common.Hash
@ -1842,7 +1842,7 @@ func testTransactionFetcher(t *testing.T, tt txFetcherTest) {
fetcher := tt.init() fetcher := tt.init()
fetcher.clock = clock fetcher.clock = clock
fetcher.step = wait fetcher.step = wait
fetcher.rand = rand.New(rand.NewSource(0x3a29)) fetcher.rand = rand.New(rand.New(rand.NewPCG(uint64(0x3a29), uint64(0x3a29))))
fetcher.Start() fetcher.Start()
defer fetcher.Stop() defer fetcher.Stop()

View file

@ -20,7 +20,7 @@ import (
"context" "context"
"errors" "errors"
"math/big" "math/big"
"math/rand" "math/rand/v2"
"reflect" "reflect"
"runtime" "runtime"
"testing" "testing"

View file

@ -19,7 +19,7 @@ package eth
import ( import (
"math" "math"
"math/big" "math/big"
"math/rand" "math/rand/v2"
"testing" "testing"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -393,7 +393,7 @@ func testGetBlockBodies(t *testing.T, protocol uint) {
) )
for j := 0; j < tt.random; j++ { for j := 0; j < tt.random; j++ {
for { for {
num := rand.Int63n(int64(backend.chain.CurrentBlock().Number.Uint64())) num := rand.Int64N(int64(backend.chain.CurrentBlock().Number.Uint64()))
if !seen[num] { if !seen[num] {
seen[num] = true seen[num] = true

View file

@ -18,7 +18,7 @@ package eth
import ( import (
"math/big" "math/big"
"math/rand" "math/rand/v2"
"sync" "sync"
mapset "github.com/deckarep/golang-set/v2" mapset "github.com/deckarep/golang-set/v2"

View file

@ -18,7 +18,7 @@ package snap
import ( import (
"bytes" "bytes"
"math/rand" "math/rand/v2"
"slices" "slices"
"testing" "testing"
@ -163,12 +163,12 @@ func buildPartial(owner common.Hash, db ethdb.KeyValueReader, batch ethdb.Batch,
func TestPartialGentree(t *testing.T) { func TestPartialGentree(t *testing.T) {
for round := 0; round < 100; round++ { for round := 0; round < 100; round++ {
var ( var (
n = rand.Intn(1024) + 10 n = rand.IntN(1024) + 10
entries []*kv entries []*kv
) )
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
var val []byte var val []byte
if rand.Intn(3) == 0 { if rand.IntN(3) == 0 {
val = testrand.Bytes(3) val = testrand.Bytes(3)
} else { } else {
val = testrand.Bytes(32) val = testrand.Bytes(32)
@ -223,8 +223,8 @@ func TestPartialGentree(t *testing.T) {
last int last int
) )
for { for {
first = rand.Intn(len(entries)) first = rand.IntN(len(entries))
last = rand.Intn(len(entries)) last = rand.IntN(len(entries))
if first <= last { if first <= last {
break break
} }
@ -255,12 +255,12 @@ func TestPartialGentree(t *testing.T) {
func TestGentreeDanglingClearing(t *testing.T) { func TestGentreeDanglingClearing(t *testing.T) {
for round := 0; round < 100; round++ { for round := 0; round < 100; round++ {
var ( var (
n = rand.Intn(1024) + 10 n = rand.IntN(1024) + 10
entries []*kv entries []*kv
) )
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
var val []byte var val []byte
if rand.Intn(3) == 0 { if rand.IntN(3) == 0 {
val = testrand.Bytes(3) val = testrand.Bytes(3)
} else { } else {
val = testrand.Bytes(32) val = testrand.Bytes(32)
@ -332,8 +332,8 @@ func TestGentreeDanglingClearing(t *testing.T) {
last int last int
) )
for { for {
first = rand.Intn(len(entries)) first = rand.IntN(len(entries))
last = rand.Intn(len(entries)) last = rand.IntN(len(entries))
if first <= last { if first <= last {
break break
} }
@ -365,7 +365,7 @@ func TestFlushPartialTree(t *testing.T) {
var entries []*kv var entries []*kv
for i := 0; i < 1024; i++ { for i := 0; i < 1024; i++ {
var val []byte var val []byte
if rand.Intn(3) == 0 { if rand.IntN(3) == 0 {
val = testrand.Bytes(3) val = testrand.Bytes(3)
} else { } else {
val = testrand.Bytes(32) val = testrand.Bytes(32)
@ -410,7 +410,7 @@ func TestFlushPartialTree(t *testing.T) {
tr := newPathTrie(common.Hash{}, c.first != 0, db, batch) tr := newPathTrie(common.Hash{}, c.first != 0, db, batch)
for i := c.first; i <= c.last; i++ { for i := c.first; i <= c.last; i++ {
tr.update(entries[i].k, entries[i].v) tr.update(entries[i].k, entries[i].v)
if rand.Intn(2) == 0 { if rand.IntN(2) == 0 {
tr.commit(false) tr.commit(false)
batch.Replay(combined) batch.Replay(combined)
@ -449,7 +449,7 @@ func TestBoundSplit(t *testing.T) {
var entries []*kv var entries []*kv
for i := 0; i < 1024; i++ { for i := 0; i < 1024; i++ {
var val []byte var val []byte
if rand.Intn(3) == 0 { if rand.IntN(3) == 0 {
val = testrand.Bytes(3) val = testrand.Bytes(3)
} else { } else {
val = testrand.Bytes(32) val = testrand.Bytes(32)
@ -473,7 +473,7 @@ func TestBoundSplit(t *testing.T) {
if next == len(entries) { if next == len(entries) {
break break
} }
last = rand.Intn(len(entries)-next) + next last = rand.IntN(len(entries)-next) + next
r := buildPartial(common.Hash{}, db, db.NewBatch(), entries, next, last) r := buildPartial(common.Hash{}, db, db.NewBatch(), entries, next, last)
set := r.modifies() set := r.modifies()
@ -523,7 +523,7 @@ func TestTinyPartialTree(t *testing.T) {
var entries []*kv var entries []*kv
for i := 0; i < 1024; i++ { for i := 0; i < 1024; i++ {
var val []byte var val []byte
if rand.Intn(3) == 0 { if rand.IntN(3) == 0 {
val = testrand.Bytes(3) val = testrand.Bytes(3)
} else { } else {
val = testrand.Bytes(32) val = testrand.Bytes(32)

View file

@ -23,7 +23,7 @@ import (
"fmt" "fmt"
gomath "math" gomath "math"
"math/big" "math/big"
"math/rand" "math/rand/v2"
"sort" "sort"
"sync" "sync"
"sync/atomic" "sync/atomic"
@ -1046,7 +1046,7 @@ func (s *Syncer) assignAccountTasks(success chan *accountResponse, fail chan *ac
// Matched a pending task to an idle peer, allocate a unique request id // Matched a pending task to an idle peer, allocate a unique request id
var reqid uint64 var reqid uint64
for { for {
reqid = uint64(rand.Int63()) reqid = uint64(rand.Int64())
if reqid == 0 { if reqid == 0 {
continue continue
} }
@ -1147,7 +1147,7 @@ func (s *Syncer) assignBytecodeTasks(success chan *bytecodeResponse, fail chan *
// Matched a pending task to an idle peer, allocate a unique request id // Matched a pending task to an idle peer, allocate a unique request id
var reqid uint64 var reqid uint64
for { for {
reqid = uint64(rand.Int63()) reqid = uint64(rand.Int64())
if reqid == 0 { if reqid == 0 {
continue continue
} }
@ -1251,7 +1251,7 @@ func (s *Syncer) assignStorageTasks(success chan *storageResponse, fail chan *st
// Matched a pending task to an idle peer, allocate a unique request id // Matched a pending task to an idle peer, allocate a unique request id
var reqid uint64 var reqid uint64
for { for {
reqid = uint64(rand.Int63()) reqid = uint64(rand.Int64())
if reqid == 0 { if reqid == 0 {
continue continue
} }
@ -1419,7 +1419,7 @@ func (s *Syncer) assignTrienodeHealTasks(success chan *trienodeHealResponse, fai
// Matched a pending task to an idle peer, allocate a unique request id // Matched a pending task to an idle peer, allocate a unique request id
var reqid uint64 var reqid uint64
for { for {
reqid = uint64(rand.Int63()) reqid = uint64(rand.Int64())
if reqid == 0 { if reqid == 0 {
continue continue
} }
@ -1547,7 +1547,7 @@ func (s *Syncer) assignBytecodeHealTasks(success chan *bytecodeHealResponse, fai
// Matched a pending task to an idle peer, allocate a unique request id // Matched a pending task to an idle peer, allocate a unique request id
var reqid uint64 var reqid uint64
for { for {
reqid = uint64(rand.Int63()) reqid = uint64(rand.Int64())
if reqid == 0 { if reqid == 0 {
continue continue
} }

View file

@ -22,7 +22,7 @@ import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"math/big" "math/big"
mrand "math/rand" mrand "math/rand/v2"
"slices" "slices"
"sync" "sync"
"testing" "testing"
@ -1797,7 +1797,7 @@ func makeUnevenStorageTrie(owner common.Hash, slots int, db *triedb.Database) (c
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
var n int var n int
for { for {
n = mrand.Intn(15) // the last range is set empty deliberately n = mrand.IntN(15) // the last range is set empty deliberately
if _, ok := chosen[byte(n)]; ok { if _, ok := chosen[byte(n)]; ok {
continue continue
} }

View file

@ -20,7 +20,7 @@ import (
"context" "context"
"crypto/ecdsa" "crypto/ecdsa"
"math/big" "math/big"
"math/rand" "math/rand/v2"
"testing" "testing"
"time" "time"
@ -161,7 +161,7 @@ func TestFork(t *testing.T) {
parent, _ := client.HeaderByNumber(ctx, nil) parent, _ := client.HeaderByNumber(ctx, nil)
// 2. // 2.
n := int(rand.Int31n(21)) n := int(rand.Int32N(21))
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
sim.Commit() sim.Commit()
} }

View file

@ -17,7 +17,7 @@
package event package event
import ( import (
"math/rand" "math/rand/v2"
"sync" "sync"
"testing" "testing"
"time" "time"
@ -113,7 +113,7 @@ func TestMuxConcurrent(t *testing.T) {
} }
} }
sub := func(i int) { sub := func(i int) {
time.Sleep(time.Duration(rand.Intn(99)) * time.Millisecond) time.Sleep(time.Duration(rand.IntN(99)) * time.Millisecond)
sub := mux.Subscribe(testEvent(0)) sub := mux.Subscribe(testEvent(0))
<-sub.Chan() <-sub.Chan()
sub.Unsubscribe() sub.Unsubscribe()

View file

@ -23,7 +23,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"math/rand" "math/rand/v2"
"os" "os"
"time" "time"
@ -90,9 +90,7 @@ func randomSource() *rand.Rand {
if _, err := crand.Read(bytes); err == nil { if _, err := crand.Read(bytes); err == nil {
seed = int64(binary.LittleEndian.Uint64(bytes)) seed = int64(binary.LittleEndian.Uint64(bytes))
} }
return rand.New(rand.New(rand.NewPCG(uint64(seed), uint64(seed))))
src := rand.NewSource(seed)
return rand.New(src)
} }
// This function runs the main event loop from a goroutine that is started // This function runs the main event loop from a goroutine that is started

View file

@ -19,7 +19,7 @@ package testrand
import ( import (
crand "crypto/rand" crand "crypto/rand"
"encoding/binary" "encoding/binary"
mrand "math/rand" mrand "math/rand/v2"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
) )
@ -31,14 +31,16 @@ var prng = initRand()
func initRand() *mrand.Rand { func initRand() *mrand.Rand {
var seed [8]byte var seed [8]byte
crand.Read(seed[:]) crand.Read(seed[:])
rnd := mrand.New(mrand.NewSource(int64(binary.LittleEndian.Uint64(seed[:])))) rnd := mrand.New(mrand.NewPCG(binary.LittleEndian.Uint64(seed[:]), binary.LittleEndian.Uint64(seed[:])))
return rnd return rnd
} }
// Bytes generates a random byte slice with specified length. // Bytes generates a random byte slice with specified length.
func Bytes(n int) []byte { func Bytes(n int) []byte {
r := make([]byte, n) r := make([]byte, n)
prng.Read(r) for i := range r {
r[i] = byte(mrand.Int())
}
return r return r
} }

View file

@ -1,7 +1,7 @@
package log package log
import ( import (
"math/rand" "math/rand/v2"
"testing" "testing"
) )
@ -11,7 +11,7 @@ func BenchmarkPrettyInt64Logfmt(b *testing.B) {
buf := make([]byte, 100) buf := make([]byte, 100)
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
sink = appendInt64(buf, rand.Int63()) sink = appendInt64(buf, rand.Int64())
} }
} }

View file

@ -2,7 +2,7 @@ package metrics
import ( import (
"math" "math"
"math/rand" "math/rand/v2"
"slices" "slices"
"sync" "sync"
"time" "time"
@ -359,9 +359,9 @@ func (s *UniformSample) Update(v int64) {
} else { } else {
var r int64 var r int64
if s.rand != nil { if s.rand != nil {
r = s.rand.Int63n(s.count) r = s.rand.Int64N(s.count)
} else { } else {
r = rand.Int63n(s.count) r = rand.Int64N(s.count)
} }
if r < int64(len(s.values)) { if r < int64(len(s.values)) {
s.values[int(r)] = v s.values[int(r)] = v

View file

@ -2,7 +2,7 @@ package metrics
import ( import (
"math" "math"
"math/rand" "math/rand/v2"
"runtime" "runtime"
"testing" "testing"
"time" "time"
@ -158,7 +158,7 @@ func TestExpDecaySampleRescale(t *testing.T) {
func TestExpDecaySampleSnapshot(t *testing.T) { func TestExpDecaySampleSnapshot(t *testing.T) {
now := time.Now() now := time.Now()
s := NewExpDecaySample(100, 0.99).(*ExpDecaySample).SetRand(rand.New(rand.NewSource(1))) s := NewExpDecaySample(100, 0.99).(*ExpDecaySample).SetRand(rand.New(rand.NewPCG(1, 1)))
for i := 1; i <= 10000; i++ { for i := 1; i <= 10000; i++ {
s.(*ExpDecaySample).update(now.Add(time.Duration(i)), int64(i)) s.(*ExpDecaySample).update(now.Add(time.Duration(i)), int64(i))
} }
@ -169,7 +169,7 @@ func TestExpDecaySampleSnapshot(t *testing.T) {
func TestExpDecaySampleStatistics(t *testing.T) { func TestExpDecaySampleStatistics(t *testing.T) {
now := time.Now() now := time.Now()
s := NewExpDecaySample(100, 0.99).(*ExpDecaySample).SetRand(rand.New(rand.NewSource(1))) s := NewExpDecaySample(100, 0.99).(*ExpDecaySample).SetRand(rand.New(rand.NewPCG(1, 1)))
for i := 1; i <= 10000; i++ { for i := 1; i <= 10000; i++ {
s.(*ExpDecaySample).update(now.Add(time.Duration(i)), int64(i)) s.(*ExpDecaySample).update(now.Add(time.Duration(i)), int64(i))
} }
@ -219,7 +219,7 @@ func TestUniformSampleIncludesTail(t *testing.T) {
} }
func TestUniformSampleSnapshot(t *testing.T) { func TestUniformSampleSnapshot(t *testing.T) {
s := NewUniformSample(100).(*UniformSample).SetRand(rand.New(rand.NewSource(1))) s := NewUniformSample(100).(*UniformSample).SetRand(rand.New(rand.NewPCG(1, 1)))
for i := 1; i <= 10000; i++ { for i := 1; i <= 10000; i++ {
s.Update(int64(i)) s.Update(int64(i))
} }
@ -229,7 +229,7 @@ func TestUniformSampleSnapshot(t *testing.T) {
} }
func TestUniformSampleStatistics(t *testing.T) { func TestUniformSampleStatistics(t *testing.T) {
s := NewUniformSample(100).(*UniformSample).SetRand(rand.New(rand.NewSource(1))) s := NewUniformSample(100).(*UniformSample).SetRand(rand.New(rand.NewPCG(1, 1)))
for i := 1; i <= 10000; i++ { for i := 1; i <= 10000; i++ {
s.Update(int64(i)) s.Update(int64(i))
} }
@ -327,7 +327,7 @@ func TestUniformSampleConcurrentUpdateCount(t *testing.T) {
for { for {
select { select {
case <-t.C: case <-t.C:
s.Update(rand.Int63()) s.Update(rand.Int64())
case <-quit: case <-quit:
t.Stop() t.Stop()
return return
@ -345,7 +345,7 @@ func BenchmarkCalculatePercentiles(b *testing.B) {
pss := []float64{0.5, 0.75, 0.95, 0.99, 0.999, 0.9999} pss := []float64{0.5, 0.75, 0.95, 0.99, 0.999, 0.9999}
var vals []int64 var vals []int64
for i := 0; i < 1000; i++ { for i := 0; i < 1000; i++ {
vals = append(vals, int64(rand.Int31())) vals = append(vals, int64(rand.Int32()))
} }
v := make([]int64, len(vals)) v := make([]int64, len(vals))
b.ResetTimer() b.ResetTimer()

View file

@ -19,7 +19,7 @@ package miner
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"math/big" "math/big"
"math/rand" "math/rand/v2"
"testing" "testing"
"time" "time"
@ -61,7 +61,7 @@ func testTransactionPriceNonceSort(t *testing.T, baseFee *big.Int) {
count := 25 count := 25
for i := 0; i < 25; i++ { for i := 0; i < 25; i++ {
var tx *types.Transaction var tx *types.Transaction
gasFeeCap := rand.Intn(50) gasFeeCap := rand.IntN(50)
if baseFee == nil { if baseFee == nil {
tx = types.NewTx(&types.LegacyTx{ tx = types.NewTx(&types.LegacyTx{
Nonce: uint64(start + i), Nonce: uint64(start + i),
@ -78,7 +78,7 @@ func testTransactionPriceNonceSort(t *testing.T, baseFee *big.Int) {
Value: big.NewInt(100), Value: big.NewInt(100),
Gas: 100, Gas: 100,
GasFeeCap: big.NewInt(int64(gasFeeCap)), GasFeeCap: big.NewInt(int64(gasFeeCap)),
GasTipCap: big.NewInt(int64(rand.Intn(gasFeeCap + 1))), GasTipCap: big.NewInt(int64(rand.IntN(gasFeeCap + 1))),
Data: nil, Data: nil,
}) })
if count == 25 && int64(gasFeeCap) < baseFee.Int64() { if count == 25 && int64(gasFeeCap) < baseFee.Int64() {

View file

@ -22,7 +22,7 @@ import (
"encoding/binary" "encoding/binary"
"errors" "errors"
"fmt" "fmt"
mrand "math/rand" mrand "math/rand/v2"
"net" "net"
"sync" "sync"
"sync/atomic" "sync/atomic"
@ -150,8 +150,8 @@ func (cfg dialConfig) withDefaults() dialConfig {
if cfg.rand == nil { if cfg.rand == nil {
seedb := make([]byte, 8) seedb := make([]byte, 8)
crand.Read(seedb) crand.Read(seedb)
seed := int64(binary.BigEndian.Uint64(seedb)) seed := binary.BigEndian.Uint64(seedb)
cfg.rand = mrand.New(mrand.NewSource(seed)) cfg.rand = mrand.New(mrand.NewPCG(seed, seed))
} }
return cfg return cfg
} }
@ -397,7 +397,7 @@ func (d *dialScheduler) checkDial(n *enode.Node) error {
// startStaticDials starts n static dial tasks. // startStaticDials starts n static dial tasks.
func (d *dialScheduler) startStaticDials(n int) (started int) { func (d *dialScheduler) startStaticDials(n int) (started int) {
for started = 0; started < n && len(d.staticPool) > 0; started++ { for started = 0; started < n && len(d.staticPool) > 0; started++ {
idx := d.rand.Intn(len(d.staticPool)) idx := d.rand.IntN(len(d.staticPool))
task := d.staticPool[idx] task := d.staticPool[idx]
d.startDial(task) d.startDial(task)
d.removeFromStaticPool(idx) d.removeFromStaticPool(idx)

View file

@ -20,7 +20,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"math/rand" "math/rand/v2"
"net" "net"
"reflect" "reflect"
"sync" "sync"
@ -423,7 +423,7 @@ func runDialTest(t *testing.T, config dialConfig, rounds []dialTestRound) {
config.dialer = dialer config.dialer = dialer
config.resolver = resolver config.resolver = resolver
config.log = testlog.Logger(t, log.LvlTrace) config.log = testlog.Logger(t, log.LvlTrace)
config.rand = rand.New(rand.NewSource(0x1111)) config.rand = rand.New(rand.NewPCG(0x1111, 0x1111))
// Set up the dialer. The setup function below runs on the dialTask // Set up the dialer. The setup function below runs on the dialTask
// goroutine and adds the peer. // goroutine and adds the peer.

View file

@ -20,7 +20,7 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
crand "crypto/rand" crand "crypto/rand"
"encoding/binary" "encoding/binary"
"math/rand" "math/rand/v2"
"net" "net"
"net/netip" "net/netip"
"sync" "sync"
@ -99,8 +99,8 @@ type ReadPacket struct {
} }
type randomSource interface { type randomSource interface {
Intn(int) int IntN(int) int
Int63n(int64) int64 Int64N(int64) int64
Shuffle(int, func(int, int)) Shuffle(int, func(int, int))
} }
@ -114,23 +114,23 @@ func (r *reseedingRandom) seed() {
var b [8]byte var b [8]byte
crand.Read(b[:]) crand.Read(b[:])
seed := binary.BigEndian.Uint64(b[:]) seed := binary.BigEndian.Uint64(b[:])
new := rand.New(rand.NewSource(int64(seed))) new := rand.New(rand.NewPCG(seed, seed))
r.mu.Lock() r.mu.Lock()
r.cur = new r.cur = new
r.mu.Unlock() r.mu.Unlock()
} }
func (r *reseedingRandom) Intn(n int) int { func (r *reseedingRandom) IntN(n int) int {
r.mu.Lock() r.mu.Lock()
defer r.mu.Unlock() defer r.mu.Unlock()
return r.cur.Intn(n) return r.cur.IntN(n)
} }
func (r *reseedingRandom) Int63n(n int64) int64 { func (r *reseedingRandom) Int64N(n int64) int64 {
r.mu.Lock() r.mu.Lock()
defer r.mu.Unlock() defer r.mu.Unlock()
return r.cur.Int63n(n) return r.cur.Int64N(n)
} }
func (r *reseedingRandom) Shuffle(n int, swap func(i, j int)) { func (r *reseedingRandom) Shuffle(n int, swap func(i, j int)) {

View file

@ -460,7 +460,7 @@ func (tab *Table) loadSeedNodes() {
func (tab *Table) nextRefreshTime() time.Duration { func (tab *Table) nextRefreshTime() time.Duration {
half := tab.cfg.RefreshInterval / 2 half := tab.cfg.RefreshInterval / 2
return half + time.Duration(tab.rand.Int63n(int64(half))) return half + time.Duration(tab.rand.Int64N(int64(half)))
} }
// bucket returns the bucket for the given node ID hash. // bucket returns the bucket for the given node ID hash.
@ -605,7 +605,7 @@ func (tab *Table) deleteInBucket(b *bucket, id enode.ID) *tableNode {
tab.log.Debug("Removed dead node", "b", b.index, "id", n.ID(), "ip", n.IPAddr()) tab.log.Debug("Removed dead node", "b", b.index, "id", n.ID(), "ip", n.IPAddr())
return nil return nil
} }
rindex := tab.rand.Intn(len(b.replacements)) rindex := tab.rand.IntN(len(b.replacements))
rep := b.replacements[rindex] rep := b.replacements[rindex]
b.replacements = slices.Delete(b.replacements, rindex, rindex+1) b.replacements = slices.Delete(b.replacements, rindex, rindex+1)
b.entries = append(b.entries, rep) b.entries = append(b.entries, rep)

View file

@ -208,7 +208,7 @@ func (list *revalidationList) get(now mclock.AbsTime, rand randomSource, exclude
return nil return nil
} }
for i := 0; i < len(list.nodes)*3; i++ { for i := 0; i < len(list.nodes)*3; i++ {
n := list.nodes[rand.Intn(len(list.nodes))] n := list.nodes[rand.IntN(len(list.nodes))]
_, excluded := exclude[n.ID()] _, excluded := exclude[n.ID()]
if !excluded { if !excluded {
return n return n
@ -218,7 +218,7 @@ func (list *revalidationList) get(now mclock.AbsTime, rand randomSource, exclude
} }
func (list *revalidationList) schedule(now mclock.AbsTime, rand randomSource) { func (list *revalidationList) schedule(now mclock.AbsTime, rand randomSource) {
list.nextTime = now.Add(time.Duration(rand.Int63n(int64(list.interval)))) list.nextTime = now.Add(time.Duration(rand.Int64N(int64(list.interval))))
} }
func (list *revalidationList) push(n *tableNode, now mclock.AbsTime, rand randomSource) { func (list *revalidationList) push(n *tableNode, now mclock.AbsTime, rand randomSource) {

View file

@ -19,7 +19,7 @@ package discover
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"fmt" "fmt"
"math/rand" "math/rand/v2"
"net" "net"
"reflect" "reflect"
"slices" "slices"
@ -264,7 +264,7 @@ func (*closeTest) Generate(rand *rand.Rand, size int) reflect.Value {
t := &closeTest{ t := &closeTest{
Self: gen(enode.ID{}, rand).(enode.ID), Self: gen(enode.ID{}, rand).(enode.ID),
Target: gen(enode.ID{}, rand).(enode.ID), Target: gen(enode.ID{}, rand).(enode.ID),
N: rand.Intn(bucketSize), N: rand.IntN(bucketSize),
} }
for _, id := range gen([]enode.ID{}, rand).([]enode.ID) { for _, id := range gen([]enode.ID{}, rand).([]enode.ID) {
r := new(enr.Record) r := new(enr.Record)
@ -486,7 +486,7 @@ func gen(typ interface{}, rand *rand.Rand) interface{} {
func quickcfg() *quick.Config { func quickcfg() *quick.Config {
return &quick.Config{ return &quick.Config{
MaxCount: 5000, MaxCount: 5000,
Rand: rand.New(rand.NewSource(time.Now().Unix())), Rand: rand.New(rand.NewPCG(uint64(time.Now().Unix()), uint64(time.Now().Unix()))),
} }
} }

View file

@ -22,7 +22,7 @@ import (
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt" "fmt"
"math/rand" "math/rand/v2"
"net" "net"
"slices" "slices"
"sync" "sync"
@ -96,7 +96,7 @@ func idAtDistance(a enode.ID, n int) (b enode.ID) {
} }
b[pos] = a[pos]&^bit | ^a[pos]&bit // TODO: randomize end bits b[pos] = a[pos]&^bit | ^a[pos]&bit // TODO: randomize end bits
for i := pos + 1; i < len(a); i++ { for i := pos + 1; i < len(a); i++ {
b[i] = byte(rand.Intn(255)) b[i] = byte(rand.IntN(255))
} }
return b return b
} }

View file

@ -24,7 +24,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"math/rand" "math/rand/v2"
"net" "net"
"net/netip" "net/netip"
"reflect" "reflect"
@ -167,7 +167,7 @@ func TestUDPv4_responseTimeouts(t *testing.T) {
defer test.close() defer test.close()
randomDuration := func(max time.Duration) time.Duration { randomDuration := func(max time.Duration) time.Duration {
return time.Duration(rand.Int63n(int64(max))) return time.Duration(rand.Int64N(int64(max)))
} }
var ( var (
@ -182,7 +182,7 @@ func TestUDPv4_responseTimeouts(t *testing.T) {
// For all other requests, a reply is scheduled to arrive // For all other requests, a reply is scheduled to arrive
// within the timeout window. // within the timeout window.
p := &replyMatcher{ p := &replyMatcher{
ptype: byte(rand.Intn(255)), ptype: byte(rand.IntN(255)),
callback: func(v4wire.Packet) (bool, bool) { return true, true }, callback: func(v4wire.Packet) (bool, bool) { return true, true },
} }
binary.BigEndian.PutUint64(p.from[:], uint64(i)) binary.BigEndian.PutUint64(p.from[:], uint64(i))

View file

@ -21,7 +21,7 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"math/rand" "math/rand/v2"
"net" "net"
"net/netip" "net/netip"
"reflect" "reflect"
@ -56,7 +56,7 @@ func TestUDPv5_lookupE2E(t *testing.T) {
defer node.Close() defer node.Close()
} }
last := nodes[N-1] last := nodes[N-1]
target := nodes[rand.Intn(N-2)].Self() target := nodes[rand.IntN(N-2)].Self()
// It is expected that all nodes can be found. // It is expected that all nodes can be found.
expectedResult := make([]*enode.Node, len(nodes)) expectedResult := make([]*enode.Node, len(nodes))

View file

@ -21,7 +21,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"math/rand" "math/rand/v2"
"net" "net"
"strings" "strings"
"sync" "sync"
@ -312,7 +312,7 @@ func (it *randomIterator) pickTree() *clientTree {
switch { switch {
case canSync: case canSync:
// Pick a random tree. // Pick a random tree.
return trees[rand.Intn(len(trees))] return trees[rand.IntN(len(trees))]
case len(trees) > 0: case len(trees) > 0:
// No sync action can be performed on any tree right now. The only meaningful // No sync action can be performed on any tree right now. The only meaningful
// thing to do is waiting for any root record to get updated. // thing to do is waiting for any root record to get updated.

View file

@ -18,7 +18,7 @@ package dnsdisc
import ( import (
"context" "context"
"math/rand" "math/rand/v2"
"time" "time"
"github.com/ethereum/go-ethereum/common/mclock" "github.com/ethereum/go-ethereum/common/mclock"
@ -131,7 +131,7 @@ func (ct *clientTree) syncNextLink(ctx context.Context) error {
} }
func (ct *clientTree) syncNextRandomENR(ctx context.Context) (*enode.Node, error) { func (ct *clientTree) syncNextRandomENR(ctx context.Context) (*enode.Node, error) {
index := rand.Intn(len(ct.enrs.missing)) index := rand.IntN(len(ct.enrs.missing))
hash := ct.enrs.missing[index] hash := ct.enrs.missing[index]
e, err := ct.enrs.resolveNext(ctx, hash) e, err := ct.enrs.resolveNext(ctx, hash)
if err != nil { if err != nil {

View file

@ -17,7 +17,7 @@
package dnsdisc package dnsdisc
import ( import (
"math/rand" "math/rand/v2"
"strconv" "strconv"
"testing" "testing"
) )
@ -67,7 +67,7 @@ func TestLinkCacheRandom(t *testing.T) {
var lc linkCache var lc linkCache
var remove []string var remove []string
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
a, b := tags[rand.Intn(len(tags))], tags[rand.Intn(len(tags))] a, b := tags[rand.IntN(len(tags))], tags[rand.IntN(len(tags))]
lc.addLink(a, b) lc.addLink(a, b)
remove = append(remove, a) remove = append(remove, a)
} }

View file

@ -17,7 +17,7 @@
package enode package enode
import ( import (
"math/rand" "math/rand/v2"
"net" "net"
"net/netip" "net/netip"
"testing" "testing"
@ -90,7 +90,7 @@ func TestLocalNodeSeqPersist(t *testing.T) {
// This test checks behavior of the endpoint predictor. // This test checks behavior of the endpoint predictor.
func TestLocalNodeEndpoint(t *testing.T) { func TestLocalNodeEndpoint(t *testing.T) {
var ( var (
rng = rand.New(rand.NewSource(4)) rng = rand.New(rand.NewPCG(4, 4))
fallback = &net.UDPAddr{IP: net.IP{127, 0, 0, 1}, Port: 80} fallback = &net.UDPAddr{IP: net.IP{127, 0, 0, 1}, Port: 80}
predicted = &net.UDPAddr{IP: net.IP{127, 0, 1, 2}, Port: 81} predicted = &net.UDPAddr{IP: net.IP{127, 0, 1, 2}, Port: 81}
staticIP = net.IP{127, 0, 1, 2} staticIP = net.IP{127, 0, 1, 2}

View file

@ -20,7 +20,7 @@ import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"math/rand" "math/rand/v2"
"testing" "testing"
"time" "time"
@ -29,11 +29,13 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
var rnd = rand.New(rand.NewSource(time.Now().UnixNano())) var rnd = rand.New(rand.NewPCG(uint64(time.Now().Unix()), uint64(time.Now().Unix())))
func randomString(strlen int) string { func randomString(strlen int) string {
b := make([]byte, strlen) b := make([]byte, strlen)
rnd.Read(b) for i := range b {
b[i] = byte(rand.Int())
}
return string(b) return string(b)
} }

View file

@ -20,7 +20,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"math" "math"
"math/rand" "math/rand/v2"
"net" "net"
"strings" "strings"
"sync" "sync"
@ -120,9 +120,10 @@ func (n *upnp) addAnyPortMapping(protocol string, extport, intport int, ip net.I
func (n *upnp) randomPort() int { func (n *upnp) randomPort() int {
if n.rand == nil { if n.rand == nil {
n.rand = rand.New(rand.NewSource(time.Now().UnixNano())) seed := uint64(time.Now().UnixNano())
n.rand = rand.New(rand.NewPCG(seed, seed))
} }
return n.rand.Intn(math.MaxUint16-10000) + 10000 return n.rand.IntN(math.MaxUint16-10000) + 10000
} }
func (n *upnp) internalAddress() (net.IP, error) { func (n *upnp) internalAddress() (net.IP, error) {

View file

@ -18,7 +18,7 @@ package netutil
import ( import (
"fmt" "fmt"
"math/rand" "math/rand/v2"
"net" "net"
"net/netip" "net/netip"
) )
@ -54,12 +54,14 @@ func IPToAddr(ip net.IP) netip.Addr {
// RandomAddr creates a random IP address. // RandomAddr creates a random IP address.
func RandomAddr(rng *rand.Rand, ipv4 bool) netip.Addr { func RandomAddr(rng *rand.Rand, ipv4 bool) netip.Addr {
var bytes []byte var bytes []byte
if ipv4 || rng.Intn(2) == 0 { if ipv4 || rng.IntN(2) == 0 {
bytes = make([]byte, 4) bytes = make([]byte, 4)
} else { } else {
bytes = make([]byte, 16) bytes = make([]byte, 16)
} }
rng.Read(bytes) for i := range bytes {
bytes[i] = byte(rand.Int())
}
addr, ok := netip.AddrFromSlice(bytes) addr, ok := netip.AddrFromSlice(bytes)
if !ok { if !ok {
panic(fmt.Errorf("BUG! invalid IP %v", bytes)) panic(fmt.Errorf("BUG! invalid IP %v", bytes))

View file

@ -18,7 +18,7 @@ package netutil
import ( import (
"fmt" "fmt"
"math/rand" "math/rand/v2"
"net" "net"
"net/netip" "net/netip"
"reflect" "reflect"
@ -264,7 +264,7 @@ func TestDistinctNetSet(t *testing.T) {
func TestDistinctNetSetAddRemove(t *testing.T) { func TestDistinctNetSetAddRemove(t *testing.T) {
cfg := &quick.Config{ cfg := &quick.Config{
Values: func(s []reflect.Value, rng *rand.Rand) { Values: func(s []reflect.Value, rng *rand.Rand) {
slice := make([]netip.Addr, rng.Intn(20)+1) slice := make([]netip.Addr, rng.IntN(20)+1)
for i := range slice { for i := range slice {
slice[i] = RandomAddr(rng, false) slice[i] = RandomAddr(rng, false)
} }

View file

@ -20,7 +20,7 @@ import (
"encoding/binary" "encoding/binary"
"errors" "errors"
"fmt" "fmt"
"math/rand" "math/rand/v2"
"net" "net"
"reflect" "reflect"
"strconv" "strconv"
@ -201,7 +201,7 @@ func TestPeerDisconnect(t *testing.T) {
// This test is supposed to verify that Peer can reliably handle // This test is supposed to verify that Peer can reliably handle
// multiple causes of disconnection occurring at the same time. // multiple causes of disconnection occurring at the same time.
func TestPeerDisconnectRace(t *testing.T) { func TestPeerDisconnectRace(t *testing.T) {
maybe := func() bool { return rand.Intn(2) == 1 } maybe := func() bool { return rand.IntN(2) == 1 }
for i := 0; i < 1000; i++ { for i := 0; i < 1000; i++ {
protoclose := make(chan error) protoclose := make(chan error)

View file

@ -29,7 +29,7 @@ import (
"fmt" "fmt"
"hash" "hash"
"io" "io"
mrand "math/rand" mrand "math/rand/v2"
"net" "net"
"time" "time"
@ -635,7 +635,7 @@ func (h *handshakeState) sealEIP8(msg interface{}) ([]byte, error) {
} }
// Pad with random amount of data. the amount needs to be at least 100 bytes to make // Pad with random amount of data. the amount needs to be at least 100 bytes to make
// the message distinguishable from pre-EIP-8 handshakes. // the message distinguishable from pre-EIP-8 handshakes.
h.wbuf.appendZero(mrand.Intn(100) + 100) h.wbuf.appendZero(mrand.IntN(100) + 100)
prefix := make([]byte, 2) prefix := make([]byte, 2)
binary.BigEndian.PutUint16(prefix, uint16(len(h.wbuf.data)+eciesOverhead)) binary.BigEndian.PutUint16(prefix, uint16(len(h.wbuf.data)+eciesOverhead))

View file

@ -22,7 +22,7 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io" "io"
"math/rand" "math/rand/v2"
"net" "net"
"reflect" "reflect"
"strings" "strings"
@ -392,9 +392,11 @@ func BenchmarkThroughput(b *testing.B) {
conn1, conn2 = NewConn(pipe1, nil), NewConn(pipe2, &keyA.PublicKey) conn1, conn2 = NewConn(pipe1, nil), NewConn(pipe2, &keyA.PublicKey)
handshakeDone = make(chan error, 1) handshakeDone = make(chan error, 1)
msgdata = make([]byte, 1024) msgdata = make([]byte, 1024)
rand = rand.New(rand.NewSource(1337)) rand = rand.New(rand.NewPCG(1337, 1337))
) )
rand.Read(msgdata) for i := range msgdata {
msgdata[i] = byte(rand.Int())
}
// Server side. // Server side.
go func() { go func() {

View file

@ -21,7 +21,7 @@ import (
"crypto/sha256" "crypto/sha256"
"errors" "errors"
"io" "io"
"math/rand" "math/rand/v2"
"net" "net"
"reflect" "reflect"
"strconv" "strconv"
@ -508,7 +508,7 @@ func newkey() *ecdsa.PrivateKey {
func randomID() (id enode.ID) { func randomID() (id enode.ID) {
for i := range id { for i := range id {
id[i] = byte(rand.Intn(255)) id[i] = byte(rand.IntN(255))
} }
return id return id
} }

View file

@ -21,7 +21,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"math/rand" "math/rand/v2"
"net" "net"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -357,7 +357,7 @@ func testClientCancel(transport string, t *testing.T) {
var ( var (
ctx context.Context ctx context.Context
cancel func() cancel func()
timeout = time.Duration(rand.Int63n(int64(maxContextCancelTimeout))) timeout = time.Duration(rand.Int64N(int64(maxContextCancelTimeout)))
) )
if index < ncallers/2 { if index < ncallers/2 {
// For half of the callers, create a context without deadline // For half of the callers, create a context without deadline
@ -899,7 +899,7 @@ func httpTestClient(srv *Server, transport string, fl *flakeyListener) (*Client,
func ipcTestClient(srv *Server, fl *flakeyListener) (*Client, net.Listener) { func ipcTestClient(srv *Server, fl *flakeyListener) (*Client, net.Listener) {
// Listen on a random endpoint. // Listen on a random endpoint.
endpoint := fmt.Sprintf("go-ethereum-test-ipc-%d-%d", os.Getpid(), rand.Int63()) endpoint := fmt.Sprintf("go-ethereum-test-ipc-%d-%d", os.Getpid(), rand.Int64())
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
endpoint = `\\.\pipe\` + endpoint endpoint = `\\.\pipe\` + endpoint
} else { } else {
@ -931,12 +931,12 @@ type flakeyListener struct {
} }
func (l *flakeyListener) Accept() (net.Conn, error) { func (l *flakeyListener) Accept() (net.Conn, error) {
delay := time.Duration(rand.Int63n(int64(l.maxAcceptDelay))) delay := time.Duration(rand.Int64N(int64(l.maxAcceptDelay)))
time.Sleep(delay) time.Sleep(delay)
c, err := l.Listener.Accept() c, err := l.Listener.Accept()
if err == nil { if err == nil {
timeout := time.Duration(rand.Int63n(int64(l.maxKillTimeout))) timeout := time.Duration(rand.Int64N(int64(l.maxKillTimeout)))
time.AfterFunc(timeout, func() { time.AfterFunc(timeout, func() {
log.Debug(fmt.Sprintf("killing conn %v after %v", c.LocalAddr(), timeout)) log.Debug(fmt.Sprintf("killing conn %v after %v", c.LocalAddr(), timeout))
c.Close() c.Close()

View file

@ -24,7 +24,7 @@ import (
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"errors" "errors"
"math/rand" "math/rand/v2"
"reflect" "reflect"
"strings" "strings"
"sync" "sync"
@ -60,22 +60,24 @@ func NewID() ID {
// randomIDGenerator returns a function generates a random IDs. // randomIDGenerator returns a function generates a random IDs.
func randomIDGenerator() func() ID { func randomIDGenerator() func() ID {
var buf = make([]byte, 8) var buf = make([]byte, 8)
var seed int64 var seed uint64
if _, err := crand.Read(buf); err == nil { if _, err := crand.Read(buf); err == nil {
seed = int64(binary.BigEndian.Uint64(buf)) seed = binary.BigEndian.Uint64(buf)
} else { } else {
seed = int64(time.Now().Nanosecond()) seed = uint64(time.Now().Nanosecond())
} }
var ( var (
mu sync.Mutex mu sync.Mutex
rng = rand.New(rand.NewSource(seed)) rng = rand.New(rand.NewPCG(seed, seed))
) )
return func() ID { return func() ID {
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()
id := make([]byte, 16) id := make([]byte, 16)
rng.Read(id) for i := range id {
id[i] = byte(rng.Int())
}
return encodeID(id) return encodeID(id)
} }
} }

View file

@ -17,7 +17,7 @@
package tests package tests
import ( import (
"math/rand" "math/rand/v2"
"testing" "testing"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"

View file

@ -20,7 +20,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"math/big" "math/big"
"math/rand" "math/rand/v2"
"time" "time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -36,7 +36,7 @@ var (
func init() { func init() {
// Random is nice, but we need it deterministic // Random is nice, but we need it deterministic
rand := rand.New(rand.NewSource(0x3a29)) rand := rand.New(rand.NewPCG(0x3a29, 0x3a29))
peers = make([]string, 10) peers = make([]string, 10)
for i := 0; i < len(peers); i++ { for i := 0; i < len(peers); i++ {
@ -44,7 +44,7 @@ func init() {
} }
txs = make([]*types.Transaction, 65536) // We need to bump enough to hit all the limits txs = make([]*types.Transaction, 65536) // We need to bump enough to hit all the limits
for i := 0; i < len(txs); i++ { for i := 0; i < len(txs); i++ {
txs[i] = types.NewTransaction(rand.Uint64(), common.Address{byte(rand.Intn(256))}, new(big.Int), 0, new(big.Int), nil) txs[i] = types.NewTransaction(rand.Uint64(), common.Address{byte(rand.IntN(256))}, new(big.Int), 0, new(big.Int), nil)
} }
} }
@ -75,7 +75,7 @@ func fuzz(input []byte) int {
} }
// Create a fetcher and hook into it's simulated fields // Create a fetcher and hook into it's simulated fields
clock := new(mclock.Simulated) clock := new(mclock.Simulated)
rand := rand.New(rand.NewSource(0x3a29)) // Same used in package tests!!! rand := rand.New(rand.NewPCG(0x3a29, 0x3a29)) // Same used in package tests!!!
f := fetcher.NewTxFetcherForTests( f := fetcher.NewTxFetcherForTests(
func(common.Hash) bool { return false }, func(common.Hash) bool { return false },

View file

@ -21,7 +21,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"math/big" "math/big"
"math/rand" "math/rand/v2"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -111,7 +111,7 @@ func execStateTest(t *testing.T, st *testMatcher, test *StateTest) {
// one. // one.
executionMask := 0xf executionMask := 0xf
if testing.Short() { if testing.Short() {
executionMask = (1 << (rand.Int63() & 4)) executionMask = (1 << (rand.Int64() & 4))
} }
t.Run(key+"/hash/trie", func(t *testing.T) { t.Run(key+"/hash/trie", func(t *testing.T) {
if executionMask&0x1 == 0 { if executionMask&0x1 == 0 {

View file

@ -20,7 +20,7 @@ import (
"bytes" "bytes"
crand "crypto/rand" crand "crypto/rand"
"encoding/hex" "encoding/hex"
"math/rand" "math/rand/v2"
"testing" "testing"
) )
@ -95,7 +95,7 @@ func TestHexToCompactInPlace(t *testing.T) {
func TestHexToCompactInPlaceRandom(t *testing.T) { func TestHexToCompactInPlaceRandom(t *testing.T) {
for i := 0; i < 10000; i++ { for i := 0; i < 10000; i++ {
l := rand.Intn(128) l := rand.IntN(128)
key := make([]byte, l) key := make([]byte, l)
crand.Read(key) crand.Read(key)
hexBytes := keybytesToHex(key) hexBytes := keybytesToHex(key)

View file

@ -19,7 +19,7 @@ package trie
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"math/rand" "math/rand/v2"
"testing" "testing"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -413,14 +413,14 @@ func testIteratorContinueAfterError(t *testing.T, memonly bool, scheme string) {
) )
for { for {
if memonly { if memonly {
rpath = paths[rand.Intn(len(paths))] rpath = paths[rand.IntN(len(paths))]
n := nodes.Nodes[string(rpath)] n := nodes.Nodes[string(rpath)]
if n == nil { if n == nil {
continue continue
} }
rhash = n.Hash rhash = n.Hash
} else { } else {
index := rand.Intn(len(paths)) index := rand.IntN(len(paths))
rpath = paths[index] rpath = paths[index]
rhash = hashes[index] rhash = hashes[index]
} }

View file

@ -21,7 +21,7 @@ import (
crand "crypto/rand" crand "crypto/rand"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
mrand "math/rand" mrand "math/rand/v2"
"slices" "slices"
"testing" "testing"
@ -38,14 +38,16 @@ var prng = initRnd()
func initRnd() *mrand.Rand { func initRnd() *mrand.Rand {
var seed [8]byte var seed [8]byte
crand.Read(seed[:]) crand.Read(seed[:])
rnd := mrand.New(mrand.NewSource(int64(binary.LittleEndian.Uint64(seed[:])))) rnd := mrand.New(mrand.NewPCG(binary.LittleEndian.Uint64(seed[:]), binary.LittleEndian.Uint64(seed[:])))
fmt.Printf("Seed: %x\n", seed) fmt.Printf("Seed: %x\n", seed)
return rnd return rnd
} }
func randBytes(n int) []byte { func randBytes(n int) []byte {
r := make([]byte, n) r := make([]byte, n)
prng.Read(r) for i := range r {
r[i] = byte(prng.Int())
}
return r return r
} }
@ -124,7 +126,7 @@ func TestBadProof(t *testing.T) {
t.Fatalf("prover %d: nil proof", i) t.Fatalf("prover %d: nil proof", i)
} }
it := proof.NewIterator(nil, nil) it := proof.NewIterator(nil, nil)
for i, d := 0, mrand.Intn(proof.Len()); i <= d; i++ { for i, d := 0, mrand.IntN(proof.Len()); i <= d; i++ {
it.Next() it.Next()
} }
key := it.Key() key := it.Key()
@ -175,8 +177,8 @@ func TestRangeProof(t *testing.T) {
} }
slices.SortFunc(entries, (*kv).cmp) slices.SortFunc(entries, (*kv).cmp)
for i := 0; i < 500; i++ { for i := 0; i < 500; i++ {
start := mrand.Intn(len(entries)) start := mrand.IntN(len(entries))
end := mrand.Intn(len(entries)-start) + start + 1 end := mrand.IntN(len(entries)-start) + start + 1
proof := memorydb.New() proof := memorydb.New()
if err := trie.Prove(entries[start].k, proof); err != nil { if err := trie.Prove(entries[start].k, proof); err != nil {
@ -208,8 +210,8 @@ func TestRangeProofWithNonExistentProof(t *testing.T) {
} }
slices.SortFunc(entries, (*kv).cmp) slices.SortFunc(entries, (*kv).cmp)
for i := 0; i < 500; i++ { for i := 0; i < 500; i++ {
start := mrand.Intn(len(entries)) start := mrand.IntN(len(entries))
end := mrand.Intn(len(entries)-start) + start + 1 end := mrand.IntN(len(entries)-start) + start + 1
proof := memorydb.New() proof := memorydb.New()
// Short circuit if the decreased key is same with the previous key // Short circuit if the decreased key is same with the previous key
@ -457,8 +459,8 @@ func TestBadRangeProof(t *testing.T) {
slices.SortFunc(entries, (*kv).cmp) slices.SortFunc(entries, (*kv).cmp)
for i := 0; i < 500; i++ { for i := 0; i < 500; i++ {
start := mrand.Intn(len(entries)) start := mrand.IntN(len(entries))
end := mrand.Intn(len(entries)-start) + start + 1 end := mrand.IntN(len(entries)-start) + start + 1
proof := memorydb.New() proof := memorydb.New()
if err := trie.Prove(entries[start].k, proof); err != nil { if err := trie.Prove(entries[start].k, proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err) t.Fatalf("Failed to prove the first node %v", err)
@ -473,20 +475,20 @@ func TestBadRangeProof(t *testing.T) {
vals = append(vals, entries[i].v) vals = append(vals, entries[i].v)
} }
var first = keys[0] var first = keys[0]
testcase := mrand.Intn(6) testcase := mrand.IntN(6)
var index int var index int
switch testcase { switch testcase {
case 0: case 0:
// Modified key // Modified key
index = mrand.Intn(end - start) index = mrand.IntN(end - start)
keys[index] = randBytes(32) // In theory it can't be same keys[index] = randBytes(32) // In theory it can't be same
case 1: case 1:
// Modified val // Modified val
index = mrand.Intn(end - start) index = mrand.IntN(end - start)
vals[index] = randBytes(20) // In theory it can't be same vals[index] = randBytes(20) // In theory it can't be same
case 2: case 2:
// Gapped entry slice // Gapped entry slice
index = mrand.Intn(end - start) index = mrand.IntN(end - start)
if (index == 0 && start < 100) || (index == end-start-1) { if (index == 0 && start < 100) || (index == end-start-1) {
continue continue
} }
@ -494,8 +496,8 @@ func TestBadRangeProof(t *testing.T) {
vals = append(vals[:index], vals[index+1:]...) vals = append(vals[:index], vals[index+1:]...)
case 3: case 3:
// Out of order // Out of order
index1 := mrand.Intn(end - start) index1 := mrand.IntN(end - start)
index2 := mrand.Intn(end - start) index2 := mrand.IntN(end - start)
if index1 == index2 { if index1 == index2 {
continue continue
} }
@ -503,11 +505,11 @@ func TestBadRangeProof(t *testing.T) {
vals[index1], vals[index2] = vals[index2], vals[index1] vals[index1], vals[index2] = vals[index2], vals[index1]
case 4: case 4:
// Set random key to nil, do nothing // Set random key to nil, do nothing
index = mrand.Intn(end - start) index = mrand.IntN(end - start)
keys[index] = nil keys[index] = nil
case 5: case 5:
// Set random value to nil, deletion // Set random value to nil, deletion
index = mrand.Intn(end - start) index = mrand.IntN(end - start)
vals[index] = nil vals[index] = nil
} }
_, err := VerifyRangeProof(trie.Hash(), first, keys, vals, proof) _, err := VerifyRangeProof(trie.Hash(), first, keys, vals, proof)
@ -801,8 +803,8 @@ func TestAllElementsEmptyValueRangeProof(t *testing.T) {
// mutateByte changes one byte in b. // mutateByte changes one byte in b.
func mutateByte(b []byte) { func mutateByte(b []byte) {
for r := mrand.Intn(len(b)); ; { for r := mrand.IntN(len(b)); ; {
new := byte(mrand.Intn(255)) new := byte(mrand.IntN(255))
if new != b[r] { if new != b[r] {
b[r] = new b[r] = new
break break

View file

@ -20,7 +20,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"maps" "maps"
"math/rand" "math/rand/v2"
"testing" "testing"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -589,7 +589,7 @@ func testIncompleteSync(t *testing.T, scheme string) {
} }
// Sanity check that removing any node from the database is detected // Sanity check that removing any node from the database is detected
for i, path := range addedKeys { for i, path := range addedKeys {
if rand.Int31n(100) > 5 { if rand.Int32N(100) > 5 {
// Only check 5 percent of added keys as a sanity check // Only check 5 percent of added keys as a sanity check
continue continue
} }

View file

@ -23,7 +23,8 @@ import (
"fmt" "fmt"
"hash" "hash"
"io" "io"
"math/rand" "math"
"math/rand/v2"
"reflect" "reflect"
"sort" "sort"
"testing" "testing"
@ -404,27 +405,29 @@ func (randTest) Generate(r *rand.Rand, size int) reflect.Value {
return reflect.ValueOf(generateSteps(finishedFn, r)) return reflect.ValueOf(generateSteps(finishedFn, r))
} }
func generateSteps(finished func() bool, r io.Reader) randTest { func generateSteps(finished func() bool, r *rand.Rand) randTest {
var allKeys [][]byte var allKeys [][]byte
var one = []byte{0} one := 0
genKey := func() []byte { genKey := func() []byte {
r.Read(one) one = r.IntN(math.MaxInt8)
if len(allKeys) < 2 || one[0]%100 > 90 { if len(allKeys) < 2 || one%100 > 90 {
// new key // new key
size := one[0] % 50 size := one % 50
key := make([]byte, size) key := make([]byte, size)
r.Read(key) for i := 0; i < size; i++ {
key[i] = byte(r.IntN(math.MaxInt8))
}
allKeys = append(allKeys, key) allKeys = append(allKeys, key)
return key return key
} }
// use existing key // use existing key
idx := int(one[0]) % len(allKeys) idx := int(one) % len(allKeys)
return allKeys[idx] return allKeys[idx]
} }
var steps randTest var steps randTest
for !finished() { for !finished() {
r.Read(one) one = r.IntN(math.MaxInt8)
step := randTestStep{op: int(one[0]) % opMax} step := randTestStep{op: int(one) % opMax}
switch step.op { switch step.op {
case opUpdate: case opUpdate:
step.key = genKey() step.key = genKey()
@ -487,7 +490,7 @@ func runRandTestBool(rt randTest) bool {
func runRandTest(rt randTest) error { func runRandTest(rt randTest) error {
var scheme = rawdb.HashScheme var scheme = rawdb.HashScheme
if rand.Intn(2) == 0 { if rand.IntN(2) == 0 {
scheme = rawdb.PathScheme scheme = rawdb.PathScheme
} }
var ( var (
@ -776,18 +779,20 @@ func TestCommitAfterHash(t *testing.T) {
func makeAccounts(size int) (addresses [][20]byte, accounts [][]byte) { func makeAccounts(size int) (addresses [][20]byte, accounts [][]byte) {
// Make the random benchmark deterministic // Make the random benchmark deterministic
random := rand.New(rand.NewSource(0)) random := rand.New(rand.NewPCG(0, 0))
// Create a realistic account trie to hash // Create a realistic account trie to hash
addresses = make([][20]byte, size) addresses = make([][20]byte, size)
for i := 0; i < len(addresses); i++ { for i := 0; i < len(addresses); i++ {
data := make([]byte, 20) data := make([]byte, 20)
random.Read(data) for i := range data {
data[i] = byte(random.Int())
}
copy(addresses[i][:], data) copy(addresses[i][:], data)
} }
accounts = make([][]byte, len(addresses)) accounts = make([][]byte, len(addresses))
for i := 0; i < len(accounts); i++ { for i := 0; i < len(accounts); i++ {
var ( var (
nonce = uint64(random.Int63()) nonce = uint64(random.Int64())
root = types.EmptyRootHash root = types.EmptyRootHash
code = crypto.Keccak256(nil) code = crypto.Keccak256(nil)
) )
@ -797,7 +802,9 @@ func makeAccounts(size int) (addresses [][20]byte, accounts [][]byte) {
// Therefore, we instead just read via byte buffer // Therefore, we instead just read via byte buffer
numBytes := random.Uint32() % 33 // [0, 32] bytes numBytes := random.Uint32() % 33 // [0, 32] bytes
balanceBytes := make([]byte, numBytes) balanceBytes := make([]byte, numBytes)
random.Read(balanceBytes) for i := range balanceBytes {
balanceBytes[i] = byte(prng.Int())
}
balance := new(uint256.Int).SetBytes(balanceBytes) balance := new(uint256.Int).SetBytes(balanceBytes)
data, _ := rlp.EncodeToBytes(&types.StateAccount{Nonce: nonce, Balance: balance, Root: root, CodeHash: code}) data, _ := rlp.EncodeToBytes(&types.StateAccount{Nonce: nonce, Balance: balance, Root: root, CodeHash: code})
accounts[i] = data accounts[i] = data
@ -914,7 +921,7 @@ func TestCommitSequenceRandomBlobs(t *testing.T) {
{200, common.FromHex("dde92ca9812e068e6982d04b40846dc65a61a9fd4996fc0f55f2fde172a8e13c")}, {200, common.FromHex("dde92ca9812e068e6982d04b40846dc65a61a9fd4996fc0f55f2fde172a8e13c")},
{2000, common.FromHex("ab553a7f9aff82e3929c382908e30ef7dd17a332933e92ba3fe873fc661ef382")}, {2000, common.FromHex("ab553a7f9aff82e3929c382908e30ef7dd17a332933e92ba3fe873fc661ef382")},
} { } {
prng := rand.New(rand.NewSource(int64(i))) prng := rand.New(rand.NewPCG(uint64(i), uint64(i)))
// This spongeDb is used to check the sequence of disk-db-writes // This spongeDb is used to check the sequence of disk-db-writes
s := &spongeDb{sponge: crypto.NewKeccakState()} s := &spongeDb{sponge: crypto.NewKeccakState()}
db := newTestDatabase(rawdb.NewDatabase(s), rawdb.HashScheme) db := newTestDatabase(rawdb.NewDatabase(s), rawdb.HashScheme)
@ -924,13 +931,17 @@ func TestCommitSequenceRandomBlobs(t *testing.T) {
key := make([]byte, 32) key := make([]byte, 32)
var val []byte var val []byte
// 50% short elements, 50% large elements // 50% short elements, 50% large elements
if prng.Intn(2) == 0 { if prng.IntN(2) == 0 {
val = make([]byte, 1+prng.Intn(32)) val = make([]byte, 1+prng.IntN(32))
} else { } else {
val = make([]byte, 1+prng.Intn(4096)) val = make([]byte, 1+prng.IntN(4096))
}
for i := range key {
key[i] = byte(prng.Int())
}
for i := range val {
val[i] = byte(prng.Int())
} }
prng.Read(key)
prng.Read(val)
trie.MustUpdate(key, val) trie.MustUpdate(key, val)
} }
// Flush trie -> database // Flush trie -> database
@ -946,7 +957,7 @@ func TestCommitSequenceRandomBlobs(t *testing.T) {
func TestCommitSequenceStackTrie(t *testing.T) { func TestCommitSequenceStackTrie(t *testing.T) {
for count := 1; count < 200; count++ { for count := 1; count < 200; count++ {
prng := rand.New(rand.NewSource(int64(count))) prng := rand.New(rand.NewPCG(uint64(count), uint64(count)))
// This spongeDb is used to check the sequence of disk-db-writes // This spongeDb is used to check the sequence of disk-db-writes
s := &spongeDb{ s := &spongeDb{
sponge: sha3.NewLegacyKeccak256(), sponge: sha3.NewLegacyKeccak256(),
@ -973,12 +984,14 @@ func TestCommitSequenceStackTrie(t *testing.T) {
binary.BigEndian.PutUint64(key, uint64(i)) binary.BigEndian.PutUint64(key, uint64(i))
var val []byte var val []byte
// 50% short elements, 50% large elements // 50% short elements, 50% large elements
if prng.Intn(2) == 0 { if prng.IntN(2) == 0 {
val = make([]byte, 1+prng.Intn(32)) val = make([]byte, 1+prng.IntN(32))
} else { } else {
val = make([]byte, 1+prng.Intn(1024)) val = make([]byte, 1+prng.IntN(1024))
}
for i := range val {
val[i] = byte(prng.Int())
} }
prng.Read(val)
trie.Update(key, val) trie.Update(key, val)
stTrie.Update(key, val) stTrie.Update(key, val)
} }
@ -1187,8 +1200,12 @@ func TestDecodeNode(t *testing.T) {
elems = make([]byte, 20) elems = make([]byte, 20)
) )
for i := 0; i < 5000000; i++ { for i := 0; i < 5000000; i++ {
prng.Read(hash) for i := range hash {
prng.Read(elems) hash[i] = byte(prng.Int())
}
for i := range elems {
elems[i] = byte(prng.Int())
}
decodeNode(hash, elems) decodeNode(hash, elems)
} }
} }
@ -1201,8 +1218,42 @@ func FuzzTrie(f *testing.F) {
steps-- steps--
return steps < 0 || input.Len() == 0 return steps < 0 || input.Len() == 0
} }
if err := runRandTest(generateSteps(finishedFn, input)); err != nil { if err := runRandTest(generateFuzzSteps(finishedFn, input)); err != nil {
t.Fatal(err) t.Fatal(err)
} }
}) })
} }
func generateFuzzSteps(finished func() bool, r io.Reader) randTest {
var allKeys [][]byte
var one = []byte{0}
genKey := func() []byte {
r.Read(one)
if len(allKeys) < 2 || one[0]%100 > 90 {
// new key
size := one[0] % 50
key := make([]byte, size)
r.Read(key)
allKeys = append(allKeys, key)
return key
}
// use existing key
idx := int(one[0]) % len(allKeys)
return allKeys[idx]
}
var steps randTest
for !finished() {
r.Read(one)
step := randTestStep{op: int(one[0]) % opMax}
switch step.op {
case opUpdate:
step.key = genKey()
step.value = make([]byte, 8)
binary.BigEndian.PutUint64(step.value, uint64(len(steps)))
case opGet, opDelete, opProve:
step.key = genKey()
}
steps = append(steps, step)
}
return steps
}

View file

@ -20,7 +20,7 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"math/rand" "math/rand/v2"
"testing" "testing"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -58,7 +58,7 @@ func updateTrie(db *Database, stateRoot common.Hash, addrHash common.Hash, root
func generateAccount(storageRoot common.Hash) types.StateAccount { func generateAccount(storageRoot common.Hash) types.StateAccount {
return types.StateAccount{ return types.StateAccount{
Nonce: uint64(rand.Intn(100)), Nonce: uint64(rand.IntN(100)),
Balance: uint256.NewInt(rand.Uint64()), Balance: uint256.NewInt(rand.Uint64()),
CodeHash: testrand.Bytes(32), CodeHash: testrand.Bytes(32),
Root: storageRoot, Root: storageRoot,
@ -223,7 +223,7 @@ func (t *tester) generate(parent common.Hash) (common.Hash, *trienode.MergedNode
dirties = make(map[common.Hash]struct{}) dirties = make(map[common.Hash]struct{})
) )
for i := 0; i < 20; i++ { for i := 0; i < 20; i++ {
switch rand.Intn(opLen) { switch rand.IntN(opLen) {
case createAccountOp: case createAccountOp:
// account creation // account creation
addr := testrand.Address() addr := testrand.Address()