forked from cerc-io/plugeth
all: remove deprecated uses of math.rand (#26710)
This PR is a (superior) alternative to https://github.com/ethereum/go-ethereum/pull/26708, it handles deprecation, primarily two specific cases. `rand.Seed` is typically used in two ways - `rand.Seed(time.Now().UnixNano())` -- we seed it, just to be sure to get some random, and not always get the same thing on every run. This is not needed, with global seeding, so those are just removed. - `rand.Seed(1)` this is typically done to ensure we have a stable test. If we rely on this, we need to fix up the tests to use a deterministic prng-source. A few occurrences like this has been replaced with a proper custom source. `rand.Read` has been replaced by `crypto/rand`.`Read` in this PR.
This commit is contained in:
@@ -18,6 +18,7 @@ package bloombits
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
crand "crypto/rand"
|
||||
"math/rand"
|
||||
"testing"
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -18,6 +18,7 @@ package snapshot
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
crand "crypto/rand"
|
||||
"math/rand"
|
||||
"testing"
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -18,6 +18,7 @@ package snapshot
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
crand "crypto/rand"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
@@ -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
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package snapshot
|
||||
|
||||
import (
|
||||
crand "crypto/rand"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"math/big"
|
||||
@@ -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
|
||||
|
||||
@@ -18,6 +18,7 @@ package txpool
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
crand "crypto/rand"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user