Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all: remove deprecated uses of math.rand #26710

Merged
merged 2 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions accounts/keystore/account_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ func TestWatchNewFile(t *testing.T) {
func TestWatchNoDir(t *testing.T) {
t.Parallel()
// Create ks but not the directory that it watches.
rand.Seed(time.Now().UnixNano())
dir := filepath.Join(os.TempDir(), fmt.Sprintf("eth-keystore-watchnodir-test-%d-%d", os.Getpid(), rand.Int()))
ks := NewKeyStore(dir, LightScryptN, LightScryptP)
list := ks.Accounts()
Expand Down Expand Up @@ -322,7 +321,6 @@ func TestUpdatedKeyfileContents(t *testing.T) {
t.Parallel()

// Create a temporary keystore to test with
rand.Seed(time.Now().UnixNano())
dir := filepath.Join(os.TempDir(), fmt.Sprintf("eth-keystore-updatedkeyfilecontents-test-%d-%d", os.Getpid(), rand.Int()))
ks := NewKeyStore(dir, LightScryptN, LightScryptP)

Expand Down
5 changes: 3 additions & 2 deletions common/lru/basiclru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package lru

import (
crand "crypto/rand"
"fmt"
"io"
"math/rand"
Expand Down Expand Up @@ -181,9 +182,9 @@ func BenchmarkLRU(b *testing.B) {
}
for i := range keys {
b := make([]byte, 32)
rand.Read(b)
crand.Read(b)
keys[i] = string(b)
rand.Read(b)
crand.Read(b)
values[i] = b
}

Expand Down
1 change: 0 additions & 1 deletion common/prque/lazyqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func testSetIndex(a interface{}, i int) {
}

func TestLazyQueue(t *testing.T) {
rand.Seed(time.Now().UnixNano())
clock := &mclock.Simulated{}
q := NewLazyQueue(testSetIndex, testPriority, testMaxPriority, clock, testQueueRefresh)

Expand Down
6 changes: 3 additions & 3 deletions consensus/ethash/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package ethash

import (
crand "crypto/rand"
"encoding/binary"
"encoding/json"
"math/big"
Expand Down Expand Up @@ -90,16 +91,15 @@ func TestCalcDifficulty(t *testing.T) {

func randSlice(min, max uint32) []byte {
var b = make([]byte, 4)
rand.Read(b)
crand.Read(b)
a := binary.LittleEndian.Uint32(b)
size := min + a%(max-min)
out := make([]byte, size)
rand.Read(out)
crand.Read(out)
return out
}

func TestDifficultyCalculators(t *testing.T) {
rand.Seed(2)
for i := 0; i < 5000; i++ {
// 1 to 300 seconds diff
var timeDelta = uint64(1 + rand.Uint32()%3000)
Expand Down
3 changes: 2 additions & 1 deletion core/bloombits/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package bloombits

import (
"bytes"
crand "crypto/rand"
"math/rand"
"testing"

Expand Down Expand Up @@ -78,7 +79,7 @@ func BenchmarkGenerator(b *testing.B) {
}
})
for i := 0; i < types.BloomBitLength; i++ {
rand.Read(input[i][:])
crand.Read(input[i][:])
}
b.Run("random", func(b *testing.B) {
b.ReportAllocs()
Expand Down
5 changes: 0 additions & 5 deletions core/rawdb/freezer_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,12 @@ import (
"sync/atomic"
"testing"
"testing/quick"
"time"

"github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/metrics"
"github.com/stretchr/testify/require"
)

func init() {
rand.Seed(time.Now().Unix())
}

// TestFreezerBasics test initializing a freezertable from scratch, writing to the table,
// and reading it back.
func TestFreezerBasics(t *testing.T) {
Expand Down
9 changes: 5 additions & 4 deletions core/state/snapshot/difflayer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package snapshot

import (
"bytes"
crand "crypto/rand"
"math/rand"
"testing"

Expand Down Expand Up @@ -73,7 +74,7 @@ func TestMergeBasics(t *testing.T) {
if rand.Intn(2) == 0 {
accStorage := make(map[common.Hash][]byte)
value := make([]byte, 32)
rand.Read(value)
crand.Read(value)
accStorage[randomHash()] = value
storage[h] = accStorage
}
Expand Down Expand Up @@ -294,7 +295,7 @@ func BenchmarkSearchSlot(b *testing.B) {
accStorage := make(map[common.Hash][]byte)
for i := 0; i < 5; i++ {
value := make([]byte, 32)
rand.Read(value)
crand.Read(value)
accStorage[randomHash()] = value
storage[accountKey] = accStorage
}
Expand Down Expand Up @@ -330,7 +331,7 @@ func BenchmarkFlatten(b *testing.B) {
accStorage := make(map[common.Hash][]byte)
for i := 0; i < 20; i++ {
value := make([]byte, 32)
rand.Read(value)
crand.Read(value)
accStorage[randomHash()] = value
}
storage[accountKey] = accStorage
Expand Down Expand Up @@ -379,7 +380,7 @@ func BenchmarkJournal(b *testing.B) {
accStorage := make(map[common.Hash][]byte)
for i := 0; i < 200; i++ {
value := make([]byte, 32)
rand.Read(value)
crand.Read(value)
accStorage[randomHash()] = value
}
storage[accountKey] = accStorage
Expand Down
5 changes: 3 additions & 2 deletions core/state/snapshot/iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package snapshot

import (
"bytes"
crand "crypto/rand"
"encoding/binary"
"fmt"
"math/rand"
Expand Down Expand Up @@ -47,7 +48,7 @@ func TestAccountIteratorBasics(t *testing.T) {
if rand.Intn(2) == 0 {
accStorage := make(map[common.Hash][]byte)
value := make([]byte, 32)
rand.Read(value)
crand.Read(value)
accStorage[randomHash()] = value
storage[h] = accStorage
}
Expand Down Expand Up @@ -79,7 +80,7 @@ func TestStorageIteratorBasics(t *testing.T) {

var nilstorage int
for i := 0; i < 100; i++ {
rand.Read(value)
crand.Read(value)
if rand.Intn(2) == 0 {
accStorage[randomHash()] = common.CopyBytes(value)
} else {
Expand Down
3 changes: 2 additions & 1 deletion core/state/snapshot/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package snapshot

import (
crand "crypto/rand"
"encoding/binary"
"fmt"
"math/big"
Expand All @@ -33,7 +34,7 @@ import (
// randomHash generates a random blob of data and returns it as a hash.
func randomHash() common.Hash {
var hash common.Hash
if n, err := rand.Read(hash[:]); n != common.HashLength || err != nil {
if n, err := crand.Read(hash[:]); n != common.HashLength || err != nil {
panic(err)
}
return hash
Expand Down
3 changes: 2 additions & 1 deletion core/txpool/txpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package txpool

import (
"crypto/ecdsa"
crand "crypto/rand"
"errors"
"fmt"
"math/big"
Expand Down Expand Up @@ -92,7 +93,7 @@ func pricedTransaction(nonce uint64, gaslimit uint64, gasprice *big.Int, key *ec

func pricedDataTransaction(nonce uint64, gaslimit uint64, gasprice *big.Int, key *ecdsa.PrivateKey, bytes uint64) *types.Transaction {
data := make([]byte, bytes)
rand.Read(data)
crand.Read(data)

tx, _ := types.SignTx(types.NewTransaction(nonce, common.Address{}, big.NewInt(0), gaslimit, gasprice, data), types.HomesteadSigner{}, key)
return tx
Expand Down
11 changes: 1 addition & 10 deletions crypto/signify/signify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@
package signify

import (
"math/rand"
"crypto/rand"
"os"
"testing"
"time"

"github.com/jedisct1/go-minisign"
)
Expand All @@ -41,8 +40,6 @@ func TestSignify(t *testing.T) {
defer os.Remove(tmpFile.Name())
defer tmpFile.Close()

rand.Seed(time.Now().UnixNano())

data := make([]byte, 1024)
rand.Read(data)
tmpFile.Write(data)
Expand Down Expand Up @@ -85,8 +82,6 @@ func TestSignifyTrustedCommentTooManyLines(t *testing.T) {
defer os.Remove(tmpFile.Name())
defer tmpFile.Close()

rand.Seed(time.Now().UnixNano())

data := make([]byte, 1024)
rand.Read(data)
tmpFile.Write(data)
Expand All @@ -110,8 +105,6 @@ func TestSignifyTrustedCommentTooManyLinesLF(t *testing.T) {
defer os.Remove(tmpFile.Name())
defer tmpFile.Close()

rand.Seed(time.Now().UnixNano())

data := make([]byte, 1024)
rand.Read(data)
tmpFile.Write(data)
Expand All @@ -135,8 +128,6 @@ func TestSignifyTrustedCommentEmpty(t *testing.T) {
defer os.Remove(tmpFile.Name())
defer tmpFile.Close()

rand.Seed(time.Now().UnixNano())

data := make([]byte, 1024)
rand.Read(data)
tmpFile.Write(data)
Expand Down
2 changes: 1 addition & 1 deletion ethdb/dbtest/testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package dbtest

import (
"bytes"
"math/rand"
"crypto/rand"
"reflect"
"sort"
"testing"
Expand Down
1 change: 0 additions & 1 deletion event/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func TestSubscribeDuplicateType(t *testing.T) {
}

func TestMuxConcurrent(t *testing.T) {
rand.Seed(time.Now().Unix())
mux := new(TypeMux)
defer mux.Stop()

Expand Down
3 changes: 2 additions & 1 deletion les/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package les

import (
"context"
crand "crypto/rand"
"errors"
"flag"
"math/rand"
Expand Down Expand Up @@ -326,7 +327,7 @@ func getHead(ctx context.Context, t *testing.T, client *rpc.Client) (uint64, com
func testRequest(ctx context.Context, t *testing.T, client *rpc.Client) bool {
var res string
var addr common.Address
rand.Read(addr[:])
crand.Read(addr[:])
c, cancel := context.WithTimeout(ctx, time.Second*12)
defer cancel()
err := client.CallContext(c, &res, "eth_getBalance", addr, "latest")
Expand Down
9 changes: 5 additions & 4 deletions les/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package les

import (
crand "crypto/rand"
"encoding/binary"
"fmt"
"math/big"
Expand Down Expand Up @@ -114,7 +115,7 @@ func (b *benchmarkProofsOrCode) init(h *serverHandler, count int) error {

func (b *benchmarkProofsOrCode) request(peer *serverPeer, index int) error {
key := make([]byte, 32)
rand.Read(key)
crand.Read(key)
if b.code {
return peer.requestCode(0, []CodeReq{{BHash: b.headHash, AccKey: key}})
}
Expand Down Expand Up @@ -176,7 +177,7 @@ func (b *benchmarkTxSend) init(h *serverHandler, count int) error {

for i := range b.txs {
data := make([]byte, txSizeCostLimit)
rand.Read(data)
crand.Read(data)
tx, err := types.SignTx(types.NewTransaction(0, addr, new(big.Int), 0, new(big.Int), data), signer, key)
if err != nil {
panic(err)
Expand All @@ -200,7 +201,7 @@ func (b *benchmarkTxStatus) init(h *serverHandler, count int) error {

func (b *benchmarkTxStatus) request(peer *serverPeer, index int) error {
var hash common.Hash
rand.Read(hash[:])
crand.Read(hash[:])
return peer.requestTxStatus(0, []common.Hash{hash})
}

Expand Down Expand Up @@ -278,7 +279,7 @@ func (h *serverHandler) measure(setup *benchmarkSetup, count int) error {
clientMeteredPipe := &meteredPipe{rw: clientPipe}
serverMeteredPipe := &meteredPipe{rw: serverPipe}
var id enode.ID
rand.Read(id[:])
crand.Read(id[:])

peer1 := newServerPeer(lpv2, NetworkId, false, p2p.NewPeer(id, "client", nil), clientMeteredPipe)
peer2 := newClientPeer(lpv2, NetworkId, p2p.NewPeer(id, "server", nil), serverMeteredPipe)
Expand Down
2 changes: 1 addition & 1 deletion les/utils/limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package utils

import (
"math/rand"
"crypto/rand"
"testing"

"github.com/ethereum/go-ethereum/p2p/enode"
Expand Down
2 changes: 1 addition & 1 deletion les/vflux/client/fillset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package client

import (
"math/rand"
"crypto/rand"
"testing"
"time"

Expand Down
1 change: 0 additions & 1 deletion les/vflux/server/clientpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ func alwaysTrueFn() bool {
}

func testClientPool(t *testing.T, activeLimit, clientCount, paidCount int, randomDisconnect bool) {
rand.Seed(time.Now().UnixNano())
var (
clock mclock.Simulated
db = rawdb.NewMemoryDatabase()
Expand Down
Loading