Skip to content

Commit

Permalink
state pkg use math/rand/v2 in tests (#12619)
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov authored Nov 6, 2024
1 parent 2812675 commit a5becca
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
if: runner.os == 'Linux'
uses: golangci/golangci-lint-action@v6
with:
version: v1.59.1
version: v1.61.0
args: --help

- name: Lint
Expand Down
2 changes: 2 additions & 0 deletions erigon-lib/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ linters:
- testifylint #TODO: enable me
- perfsprint #TODO: enable me
- protogetter
- typecheck
enable:
- unconvert
- predeclared
Expand Down Expand Up @@ -111,6 +112,7 @@ issues:
- unused
- gocritic
- perfsprint
- typecheck
- path: hack\.go
linters:
- gosec
Expand Down
21 changes: 10 additions & 11 deletions erigon-lib/state/aggregator_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"bytes"
"context"
"fmt"
"math/rand"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -109,7 +108,7 @@ func BenchmarkAggregator_Processing(b *testing.B) {
}

func queueKeys(ctx context.Context, seed, ofSize uint64) <-chan []byte {
rnd := rand.New(rand.NewSource(int64(seed)))
rnd := newRnd(seed)
keys := make(chan []byte, 1)
go func() {
for {
Expand All @@ -127,10 +126,10 @@ func queueKeys(ctx context.Context, seed, ofSize uint64) <-chan []byte {
}

func Benchmark_BtreeIndex_Allocation(b *testing.B) {
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
rnd := newRnd(uint64(time.Now().UnixNano()))
for i := 0; i < b.N; i++ {
now := time.Now()
count := rnd.Intn(1000000000)
count := rnd.IntN(1000000000)
bt := newBtAlloc(uint64(count), uint64(1<<12), true, nil, nil)
bt.traverseDfs()
fmt.Printf("alloc %v\n", time.Since(now))
Expand All @@ -139,7 +138,7 @@ func Benchmark_BtreeIndex_Allocation(b *testing.B) {

func Benchmark_BtreeIndex_Search(b *testing.B) {
logger := log.New()
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
rnd := newRnd(uint64(time.Now().UnixNano()))
tmp := b.TempDir()
defer os.RemoveAll(tmp)
dataPath := "../../data/storage.256-288.kv"
Expand All @@ -159,7 +158,7 @@ func Benchmark_BtreeIndex_Search(b *testing.B) {
getter := seg.NewReader(kv.MakeGetter(), comp)

for i := 0; i < b.N; i++ {
p := rnd.Intn(len(keys))
p := rnd.IntN(len(keys))
cur, err := bt.Seek(getter, keys[p])
require.NoErrorf(b, err, "i=%d", i)
require.EqualValues(b, keys[p], cur.Key())
Expand Down Expand Up @@ -193,12 +192,12 @@ func Benchmark_BTree_Seek(b *testing.B) {
M := uint64(1024)
compress := seg.CompressNone
kv, bt, keys, _ := benchInitBtreeIndex(b, M, compress)
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
rnd := newRnd(uint64(time.Now().UnixNano()))
getter := seg.NewReader(kv.MakeGetter(), compress)

b.Run("seek_only", func(b *testing.B) {
for i := 0; i < b.N; i++ {
p := rnd.Intn(len(keys))
p := rnd.IntN(len(keys))

cur, err := bt.Seek(getter, keys[p])
require.NoError(b, err)
Expand All @@ -209,7 +208,7 @@ func Benchmark_BTree_Seek(b *testing.B) {

b.Run("seek_then_next", func(b *testing.B) {
for i := 0; i < b.N; i++ {
p := rnd.Intn(len(keys))
p := rnd.IntN(len(keys))

cur, err := bt.Seek(getter, keys[p])
require.NoError(b, err)
Expand Down Expand Up @@ -249,7 +248,7 @@ func Benchmark_Recsplit_Find_ExternalFile(b *testing.B) {
b.Skip("requires existing KV index file at ../../data/storage.kv")
}

rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
rnd := newRnd(uint64(time.Now().UnixNano()))
tmp := b.TempDir()

defer os.RemoveAll(tmp)
Expand All @@ -269,7 +268,7 @@ func Benchmark_Recsplit_Find_ExternalFile(b *testing.B) {
require.NoError(b, err)

for i := 0; i < b.N; i++ {
p := rnd.Intn(len(keys))
p := rnd.IntN(len(keys))

offset, _ := idxr.Lookup(keys[p])
getter.Reset(offset)
Expand Down
5 changes: 3 additions & 2 deletions erigon-lib/state/aggregator_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ package state
import (
"context"
"encoding/binary"
"testing"
"time"

"github.com/c2h5oh/datasize"
"github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/common/datadir"
Expand All @@ -30,8 +33,6 @@ import (
"github.com/erigontech/erigon-lib/log/v3"
"github.com/erigontech/erigon-lib/types"
"github.com/holiman/uint256"
"testing"
"time"

"github.com/stretchr/testify/require"
)
Expand Down
31 changes: 16 additions & 15 deletions erigon-lib/state/aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"encoding/binary"
"encoding/hex"
"fmt"
"github.com/erigontech/erigon-lib/commitment"
"math"
"math/rand"
"os"
Expand All @@ -33,6 +32,8 @@ import (
"testing"
"time"

"github.com/erigontech/erigon-lib/commitment"

"github.com/erigontech/erigon-lib/common/background"

"github.com/c2h5oh/datasize"
Expand Down Expand Up @@ -109,7 +110,7 @@ func aggregatorV3_RestartOnDatadir(t *testing.T, rc runCfg) {
defer domains.Close()

var latestCommitTxNum uint64
rnd := rand.New(rand.NewSource(time.Now().Unix()))
rnd := newRnd(0)

someKey := []byte("somekey")
txs := (aggStep / 2) * 19
Expand Down Expand Up @@ -249,7 +250,7 @@ func TestAggregatorV3_PruneSmallBatches(t *testing.T) {
maxTx := aggStep * 5
t.Logf("step=%d tx_count=%d\n", aggStep, maxTx)

rnd := rand.New(rand.NewSource(0))
rnd := newRnd(0)

generateSharedDomainsUpdates(t, domains, maxTx, rnd, 20, 10, aggStep/2)

Expand Down Expand Up @@ -429,7 +430,7 @@ func fillRawdbTxNumsIndexForSharedDomains(t *testing.T, rwTx kv.RwTx, maxTx, com
}
}

func generateSharedDomainsUpdates(t *testing.T, domains *SharedDomains, maxTxNum uint64, rnd *rand.Rand, keyMaxLen, keysCount, commitEvery uint64) map[string]struct{} {
func generateSharedDomainsUpdates(t *testing.T, domains *SharedDomains, maxTxNum uint64, rnd *rndGen, keyMaxLen, keysCount, commitEvery uint64) map[string]struct{} {
t.Helper()
usedKeys := make(map[string]struct{}, keysCount*maxTxNum)
for txNum := uint64(1); txNum <= maxTxNum; txNum++ {
Expand All @@ -445,14 +446,14 @@ func generateSharedDomainsUpdates(t *testing.T, domains *SharedDomains, maxTxNum
return usedKeys
}

func generateSharedDomainsUpdatesForTx(t *testing.T, domains *SharedDomains, txNum uint64, rnd *rand.Rand, prevKeys map[string]struct{}, keyMaxLen, keysCount uint64) map[string]struct{} {
func generateSharedDomainsUpdatesForTx(t *testing.T, domains *SharedDomains, txNum uint64, rnd *rndGen, prevKeys map[string]struct{}, keyMaxLen, keysCount uint64) map[string]struct{} {
t.Helper()
domains.SetTxNum(txNum)

getKey := func() ([]byte, bool) {
r := rnd.Intn(100)
r := rnd.IntN(100)
if r < 50 && len(prevKeys) > 0 {
ri := rnd.Intn(len(prevKeys))
ri := rnd.IntN(len(prevKeys))
for k := range prevKeys {
if ri == 0 {
return []byte(k), true
Expand All @@ -471,7 +472,7 @@ func generateSharedDomainsUpdatesForTx(t *testing.T, domains *SharedDomains, txN
for j := uint64(0); j < keysCount; j++ {
key, existed := getKey()

r := rnd.Intn(101)
r := rnd.IntN(101)
switch {
case r <= 33:
buf := types.EncodeAccountBytesV3(txNum, uint256.NewInt(txNum*100_000), nil, 0)
Expand All @@ -484,7 +485,7 @@ func generateSharedDomainsUpdatesForTx(t *testing.T, domains *SharedDomains, txN
require.NoError(t, err)

case r > 33 && r <= 66:
codeUpd := make([]byte, rnd.Intn(24576))
codeUpd := make([]byte, rnd.IntN(24576))
_, err := rnd.Read(codeUpd)
require.NoError(t, err)
for limit := 1000; len(key) > length.Addr && limit > 0; limit-- {
Expand Down Expand Up @@ -569,7 +570,7 @@ func TestAggregatorV3_RestartOnFiles(t *testing.T) {
txs := aggStep * 5
t.Logf("step=%d tx_count=%d\n", aggStep, txs)

rnd := rand.New(rand.NewSource(0))
rnd := newRnd(0)
keys := make([][]byte, txs)

for txNum := uint64(1); txNum <= txs; txNum++ {
Expand Down Expand Up @@ -708,7 +709,7 @@ func TestAggregatorV3_ReplaceCommittedKeys(t *testing.T) {
txs := (aggStep) * StepsInColdFile
t.Logf("step=%d tx_count=%d", aggStep, txs)

rnd := rand.New(rand.NewSource(0))
rnd := newRnd(0)
keys := make([][]byte, txs/2)

var prev1, prev2 []byte
Expand Down Expand Up @@ -822,7 +823,7 @@ func pivotKeysFromKV(dataPath string) ([][]byte, error) {
func generateKV(tb testing.TB, tmp string, keySize, valueSize, keyCount int, logger log.Logger, compressFlags seg.FileCompression) string {
tb.Helper()

rnd := rand.New(rand.NewSource(0))
rnd := newRnd(0)
values := make([]byte, valueSize)

dataPath := path.Join(tmp, fmt.Sprintf("%dk.kv", keyCount/1000))
Expand All @@ -842,7 +843,7 @@ func generateKV(tb testing.TB, tmp string, keySize, valueSize, keyCount int, log
binary.BigEndian.PutUint64(key[keySize-8:], uint64(i))
require.NoError(tb, err)

n, err = rnd.Read(values[:rnd.Intn(valueSize)+1])
n, err = rnd.Read(values[:rnd.IntN(valueSize)+1])
require.NoError(tb, err)

err = collector.Collect(key, values[:n])
Expand Down Expand Up @@ -904,7 +905,7 @@ func testDbAndAggregatorv3(t *testing.T, aggStep uint64) (kv.RwDB, *Aggregator)
func generateInputData(tb testing.TB, keySize, valueSize, keyCount int) ([][]byte, [][]byte) {
tb.Helper()

rnd := rand.New(rand.NewSource(0))
rnd := newRnd(0)
values := make([][]byte, keyCount)
keys := make([][]byte, keyCount)

Expand All @@ -915,7 +916,7 @@ func generateInputData(tb testing.TB, keySize, valueSize, keyCount int) ([][]byt
require.NoError(tb, err)
keys[i] = common.Copy(bk[:n])

n, err = rnd.Read(bv[:rnd.Intn(valueSize)+1])
n, err = rnd.Read(bv[:rnd.IntN(valueSize)+1])
require.NoError(tb, err)

values[i] = common.Copy(bv[:n])
Expand Down
5 changes: 2 additions & 3 deletions erigon-lib/state/bps_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ import (
"time"
"unsafe"

"github.com/erigontech/erigon-lib/common/dbg"

"github.com/c2h5oh/datasize"
"github.com/erigontech/erigon-lib/seg"

"github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/common/dbg"
"github.com/erigontech/erigon-lib/log/v3"
"github.com/erigontech/erigon-lib/recsplit/eliasfano32"
"github.com/erigontech/erigon-lib/seg"
)

// nolint
Expand Down
6 changes: 2 additions & 4 deletions erigon-lib/state/domain_shared_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package state
import (
"context"
"encoding/binary"
"math/rand"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -46,8 +45,7 @@ func Benchmark_SharedDomains_GetLatest(t *testing.B) {
defer domains.Close()
maxTx := stepSize * 258

seed := int64(4500)
rnd := rand.New(rand.NewSource(seed))
rnd := newRnd(4500)

keys := make([][]byte, 8)
for i := 0; i < len(keys); i++ {
Expand Down Expand Up @@ -104,7 +102,7 @@ func Benchmark_SharedDomains_GetLatest(t *testing.B) {

for ik := 0; ik < t.N; ik++ {
for i := 0; i < len(keys); i++ {
ts := uint64(rnd.Intn(int(maxTx)))
ts := uint64(rnd.IntN(int(maxTx)))
v, ok, err := ac2.HistorySeek(kv.AccountsHistory, keys[i], ts, rwTx)

require.True(t, ok)
Expand Down
7 changes: 3 additions & 4 deletions erigon-lib/state/domain_shared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"encoding/binary"
"fmt"
"math/rand"
"testing"
"time"

Expand Down Expand Up @@ -53,7 +52,7 @@ func TestSharedDomain_CommitmentKeyReplacement(t *testing.T) {
require.NoError(t, err)
defer domains.Close()

rnd := rand.New(rand.NewSource(2342))
rnd := newRnd(2342)
maxTx := stepSize * 8

// 1. generate data
Expand Down Expand Up @@ -134,7 +133,7 @@ func TestSharedDomain_Unwind(t *testing.T) {
maxTx := stepSize
hashes := make([][]byte, maxTx)
count := 10
rnd := rand.New(rand.NewSource(0))
rnd := newRnd(0)
ac.Close()
err = rwTx.Commit()
require.NoError(t, err)
Expand Down Expand Up @@ -180,7 +179,7 @@ Loop:
err = domains.Flush(ctx, rwTx)
require.NoError(t, err)

unwindTo := uint64(commitStep * rnd.Intn(int(maxTx)/commitStep))
unwindTo := uint64(commitStep * rnd.IntN(int(maxTx)/commitStep))
domains.currentChangesAccumulator = nil

acu := agg.BeginFilesRo()
Expand Down
Loading

0 comments on commit a5becca

Please sign in to comment.