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

testutil: replace math/rand deprecation #2850

Merged
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: 1 addition & 1 deletion app/k1util/k1util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
package k1util_test

import (
"crypto/rand"
"encoding/hex"
"math/rand"
"os"
"path"
"testing"
Expand Down
5 changes: 4 additions & 1 deletion app/obolapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"encoding/json"
"io"
"math/rand"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -45,7 +46,9 @@ func TestLockPublish(t *testing.T) {
},
}

lock, _, _ := cluster.NewForT(t, 3, 3, 4, 0, opts...)
seed := 0
random := rand.New(rand.NewSource(int64(seed)))
lock, _, _ := cluster.NewForT(t, 3, 3, 4, seed, random, opts...)

cl, err := obolapi.New(srv.URL)
require.NoError(t, err)
Expand Down
12 changes: 8 additions & 4 deletions cluster/cluster_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,14 @@ func TestSupportEIP712Sigs(t *testing.T) {
}

func RandomDepositData() DepositData {
return RandomDepositDataSeed(testutil.NewSeedRand())
}

func RandomDepositDataSeed(r *rand.Rand) DepositData {
return DepositData{
PubKey: testutil.RandomBytes48(),
WithdrawalCredentials: testutil.RandomBytes32(),
Amount: rand.Int(),
Signature: testutil.RandomBytes96(),
PubKey: testutil.RandomBytes48Seed(r),
WithdrawalCredentials: testutil.RandomBytes32Seed(r),
Amount: r.Int(),
Signature: testutil.RandomBytes96Seed(r),
}
}
60 changes: 31 additions & 29 deletions cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestEncode(t *testing.T) {
for _, version := range cluster.SupportedVersionsForT(t) {
t.Run(version, func(t *testing.T) {
vStr := strings.ReplaceAll(version, ".", "_")
rand.Seed(1)
r := rand.New(rand.NewSource(1))

const (
numVals = 2
Expand All @@ -52,13 +52,13 @@ func TestEncode(t *testing.T) {
}
// Definition version prior to v1.5 don't support multiple validator addresses.
if isAnyVersion(version, v1_0, v1_1, v1_2, v1_3, v1_4) {
opts = append(opts, cluster.WithLegacyVAddrs(testutil.RandomETHAddress(), testutil.RandomETHAddress()))
opts = append(opts, cluster.WithLegacyVAddrs(testutil.RandomETHAddressSeed(r), testutil.RandomETHAddressSeed(r)))
}

var feeRecipientAddrs, withdrawalAddrs []string
for i := 0; i < numVals; i++ {
feeRecipientAddrs = append(feeRecipientAddrs, testutil.RandomETHAddress())
withdrawalAddrs = append(withdrawalAddrs, testutil.RandomETHAddress())
feeRecipientAddrs = append(feeRecipientAddrs, testutil.RandomETHAddressSeed(r))
withdrawalAddrs = append(withdrawalAddrs, testutil.RandomETHAddressSeed(r))
}

definition, err := cluster.NewDefinition(
Expand All @@ -69,21 +69,21 @@ func TestEncode(t *testing.T) {
withdrawalAddrs,
eth2util.Sepolia.GenesisForkVersionHex,
cluster.Creator{
Address: testutil.RandomETHAddress(),
ConfigSignature: testutil.RandomSecp256k1Signature(),
Address: testutil.RandomETHAddressSeed(r),
ConfigSignature: testutil.RandomSecp256k1SignatureSeed(r),
},
[]cluster.Operator{
{
Address: testutil.RandomETHAddress(),
ENR: fmt.Sprintf("enr://%x", testutil.RandomBytes32()),
ConfigSignature: testutil.RandomSecp256k1Signature(),
ENRSignature: testutil.RandomSecp256k1Signature(),
Address: testutil.RandomETHAddressSeed(r),
ENR: fmt.Sprintf("enr://%x", testutil.RandomBytes32Seed(r)),
ConfigSignature: testutil.RandomSecp256k1SignatureSeed(r),
ENRSignature: testutil.RandomSecp256k1SignatureSeed(r),
},
{
Address: testutil.RandomETHAddress(),
ENR: fmt.Sprintf("enr://%x", testutil.RandomBytes32()),
ConfigSignature: testutil.RandomSecp256k1Signature(),
ENRSignature: testutil.RandomSecp256k1Signature(),
Address: testutil.RandomETHAddressSeed(r),
ENR: fmt.Sprintf("enr://%x", testutil.RandomBytes32Seed(r)),
ConfigSignature: testutil.RandomSecp256k1SignatureSeed(r),
ENRSignature: testutil.RandomSecp256k1SignatureSeed(r),
},
},
[]int{16, 16},
Expand Down Expand Up @@ -134,29 +134,29 @@ func TestEncode(t *testing.T) {

lock := cluster.Lock{
Definition: definition,
SignatureAggregate: testutil.RandomBytes32(),
SignatureAggregate: testutil.RandomBytes32Seed(r),
Validators: []cluster.DistValidator{
{
PubKey: testutil.RandomBytes48(),
PubKey: testutil.RandomBytes48Seed(r),
PubShares: [][]byte{
testutil.RandomBytes48(),
testutil.RandomBytes48(),
testutil.RandomBytes48Seed(r),
testutil.RandomBytes48Seed(r),
},
PartialDepositData: []cluster.DepositData{cluster.RandomDepositData()},
BuilderRegistration: cluster.RandomRegistration(t, eth2util.Sepolia.Name),
PartialDepositData: []cluster.DepositData{cluster.RandomDepositDataSeed(r)},
BuilderRegistration: cluster.RandomRegistrationSeed(t, eth2util.Sepolia.Name, r),
}, {
PubKey: testutil.RandomBytes48(),
PubKey: testutil.RandomBytes48Seed(r),
PubShares: [][]byte{
testutil.RandomBytes48(),
testutil.RandomBytes48(),
testutil.RandomBytes48Seed(r),
testutil.RandomBytes48Seed(r),
},
PartialDepositData: []cluster.DepositData{cluster.RandomDepositData()},
BuilderRegistration: cluster.RandomRegistration(t, eth2util.Sepolia.Name),
PartialDepositData: []cluster.DepositData{cluster.RandomDepositDataSeed(r)},
BuilderRegistration: cluster.RandomRegistrationSeed(t, eth2util.Sepolia.Name, r),
},
},
NodeSignatures: [][]byte{
testutil.RandomBytes32(),
testutil.RandomBytes32(),
testutil.RandomBytes32Seed(r),
testutil.RandomBytes32Seed(r),
},
}

Expand Down Expand Up @@ -187,7 +187,7 @@ func TestEncode(t *testing.T) {
// Lock version v1.8.0 supports multiple PartialDepositData.
if isAnyVersion(version, v1_8) {
for i := range lock.Validators {
dd := cluster.RandomDepositData()
dd := cluster.RandomDepositDataSeed(r)
dd.PubKey = lock.Validators[i].PubKey
lock.Validators[i].PartialDepositData = append(lock.Validators[i].PartialDepositData, dd)
}
Expand Down Expand Up @@ -265,7 +265,9 @@ func TestExamples(t *testing.T) {
}

func TestDefinitionPeers(t *testing.T) {
lock, _, _ := cluster.NewForT(t, 2, 3, 4, 5)
seed := 5
random := rand.New(rand.NewSource(int64(seed)))
lock, _, _ := cluster.NewForT(t, 2, 3, 4, seed, random)
peers, err := lock.Peers()
require.NoError(t, err)

Expand Down
5 changes: 4 additions & 1 deletion cluster/helpers_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package cluster
import (
"context"
"fmt"
"math/rand"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -72,7 +73,9 @@ func TestVerifySig(t *testing.T) {
}

func TestFetchDefinition(t *testing.T) {
lock, _, _ := NewForT(t, 1, 2, 3, 0)
seed := 0
random := rand.New(rand.NewSource(int64(seed)))
lock, _, _ := NewForT(t, 1, 2, 3, seed, random)
validDef := lock.Definition
invalidDef := Definition{}

Expand Down
5 changes: 4 additions & 1 deletion cluster/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package cluster_test

import (
"math/rand"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -11,7 +12,9 @@ import (
)

func TestVerifyLock(t *testing.T) {
lock, _, _ := cluster.NewForT(t, 3, 3, 4, 0)
seed := 0
random := rand.New(rand.NewSource(int64(seed)))
lock, _, _ := cluster.NewForT(t, 3, 3, 4, seed, random)
require.NoError(t, lock.Definition.VerifySignatures())
require.NoError(t, lock.VerifySignatures())
}
5 changes: 4 additions & 1 deletion cluster/manifest/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package manifest_test

import (
"math/rand"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -13,7 +14,9 @@ import (
)

func TestDuplicateENRs(t *testing.T) {
lock, _, _ := cluster.NewForT(t, 1, 3, 4, 0)
seed := 0
random := rand.New(rand.NewSource(int64(seed)))
lock, _, _ := cluster.NewForT(t, 1, 3, 4, seed, random)

_, err := manifest.ClusterPeers(&manifestpb.Cluster{Operators: []*manifestpb.Operator{
{Enr: lock.Operators[0].ENR},
Expand Down
4 changes: 3 additions & 1 deletion cluster/manifest/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ func testLoadLegacy(t *testing.T, version string) {
opts = append(opts, cluster.WithLegacyVAddrs(testutil.RandomETHAddress(), testutil.RandomETHAddress()))
}

lock, _, _ := cluster.NewForT(t, rand.Intn(10), k, n, 0, opts...)
seed := 0
random := rand.New(rand.NewSource(int64(seed)))
lock, _, _ := cluster.NewForT(t, rand.Intn(10), k, n, seed, random, opts...)

b, err := json.MarshalIndent(lock, "", " ")
require.NoError(t, err)
Expand Down
9 changes: 5 additions & 4 deletions cluster/manifest/mutationaddvalidator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package manifest_test
import (
"encoding/hex"
"encoding/json"
"math/rand"
"os"
"testing"

Expand Down Expand Up @@ -68,8 +69,9 @@ func TestAddValidators(t *testing.T) {
setIncrementingTime(t)

nodes := 4
lock, secrets, _ := cluster.NewForT(t, 3, 3, nodes, 1)

seed := 1
random := rand.New(rand.NewSource(int64(seed)))
lock, secrets, _ := cluster.NewForT(t, 3, 3, nodes, seed, random)
// Convert validators into manifest.Validator
var vals []*manifestpb.Validator
for i, validator := range lock.Validators {
Expand All @@ -78,8 +80,7 @@ func TestAddValidators(t *testing.T) {

vals = append(vals, val)
}

genVals, err := manifest.NewGenValidators(testutil.RandomBytes32(), vals)
genVals, err := manifest.NewGenValidators(testutil.RandomBytes32Seed(random), vals)
require.NoError(t, err)
genHash, err := manifest.Hash(genVals)
testutil.RequireNoError(t, err)
Expand Down
7 changes: 5 additions & 2 deletions cluster/manifest/mutationnodeapproval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package manifest_test

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

Expand Down Expand Up @@ -36,9 +37,11 @@ func setIncrementingTime(t *testing.T) {
func TestNodeApprovals(t *testing.T) {
setIncrementingTime(t)

lock, secrets, _ := cluster.NewForT(t, 1, 3, 4, 1)
seed := 1
random := rand.New(rand.NewSource(int64(seed)))
lock, secrets, _ := cluster.NewForT(t, 1, 3, 4, seed, random)

parent := testutil.RandomBytes32()
parent := testutil.RandomBytes32Seed(random)

var approvals []*manifestpb.SignedMutation
for _, secret := range secrets {
Expand Down
29 changes: 16 additions & 13 deletions cluster/test_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
// It also returns the peer p2p keys and BLS secret shares. If the seed is zero a random cluster on available loopback
// ports is generated, else a deterministic cluster is generated.
// Note this is not defined in testutil since it is tightly coupled with the cluster package.
func NewForT(t *testing.T, dv, k, n, seed int, opts ...func(*Definition)) (Lock, []*k1.PrivateKey, [][]tbls.PrivateKey) {
func NewForT(t *testing.T, dv, k, n, seed int, random *rand.Rand, opts ...func(*Definition)) (Lock, []*k1.PrivateKey, [][]tbls.PrivateKey) {
t.Helper()

var (
Expand All @@ -35,22 +35,20 @@ func NewForT(t *testing.T, dv, k, n, seed int, opts ...func(*Definition)) (Lock,
dvShares [][]tbls.PrivateKey
)

random := io.Reader(rand.New(rand.NewSource(int64(seed)))) //nolint:gosec // Explicit use of weak random generator for determinism.
randomReader := io.Reader(rand.New(rand.NewSource(int64(seed)))) //nolint:gosec // Explicit use of weak random generator for determinism.
if seed == 0 {
random = crand.Reader
} else {
rand.Seed(int64(seed))
randomReader = crand.Reader
}

var feeRecipientAddrs, withdrawalAddrs []string
for i := 0; i < dv; i++ {
rootSecret, err := tbls.GenerateInsecureKey(t, random)
rootSecret, err := tbls.GenerateInsecureKey(t, randomReader)
require.NoError(t, err)

rootPublic, err := tbls.SecretToPublicKey(rootSecret)
require.NoError(t, err)

shares, err := tbls.ThresholdSplitInsecure(t, rootSecret, uint(n), uint(k), random)
shares, err := tbls.ThresholdSplitInsecure(t, rootSecret, uint(n), uint(k), randomReader)
require.NoError(t, err)

var pubshares [][]byte
Expand All @@ -66,7 +64,7 @@ func NewForT(t *testing.T, dv, k, n, seed int, opts ...func(*Definition)) (Lock,
privshares = append(privshares, sharePrivkey)
}

feeRecipientAddr := testutil.RandomETHAddress()
feeRecipientAddr := testutil.RandomETHAddressSeed(random)
reg := getSignedRegistration(t, rootSecret, feeRecipientAddr, eth2util.Goerli.Name)

vals = append(vals, DistValidator{
Expand All @@ -76,7 +74,7 @@ func NewForT(t *testing.T, dv, k, n, seed int, opts ...func(*Definition)) (Lock,
})
dvShares = append(dvShares, privshares)
feeRecipientAddrs = append(feeRecipientAddrs, feeRecipientAddr)
withdrawalAddrs = append(withdrawalAddrs, testutil.RandomETHAddress())
withdrawalAddrs = append(withdrawalAddrs, testutil.RandomETHAddressSeed(random))
}

for i := 0; i < n; i++ {
Expand Down Expand Up @@ -104,7 +102,7 @@ func NewForT(t *testing.T, dv, k, n, seed int, opts ...func(*Definition)) (Lock,

def, err := NewDefinition("test cluster", dv, k,
feeRecipientAddrs, withdrawalAddrs,
eth2util.Goerli.GenesisForkVersionHex, creator, ops, nil, random, opts...)
eth2util.Goerli.GenesisForkVersionHex, creator, ops, nil, randomReader, opts...)
require.NoError(t, err)

// Definition version prior to v1.3.0 don't support EIP712 signatures.
Expand Down Expand Up @@ -189,17 +187,22 @@ func getSignedRegistration(t *testing.T, secret tbls.PrivateKey, feeRecipientAdd
// RandomRegistration returns a random builder registration.
func RandomRegistration(t *testing.T, network string) BuilderRegistration {
t.Helper()
return RandomRegistrationSeed(t, network, testutil.NewSeedRand())
}

func RandomRegistrationSeed(t *testing.T, network string, r *rand.Rand) BuilderRegistration {
t.Helper()

timestamp, err := eth2util.NetworkToGenesisTime(network)
require.NoError(t, err)

return BuilderRegistration{
Message: Registration{
FeeRecipient: testutil.RandomBytes32()[:20],
FeeRecipient: testutil.RandomBytes32Seed(r)[:20],
GasLimit: 30000000,
Timestamp: timestamp,
PubKey: testutil.RandomBytes48(),
PubKey: testutil.RandomBytes48Seed(r),
},
Signature: testutil.RandomBytes96(),
Signature: testutil.RandomBytes96Seed(r),
}
}
5 changes: 4 additions & 1 deletion cluster/test_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package cluster_test

import (
"math/rand"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -11,7 +12,9 @@ import (
)

func TestNewCluster(t *testing.T) {
lock, _, _ := cluster.NewForT(t, 3, 3, 3, 0)
seed := 0
random := rand.New(rand.NewSource(int64(seed)))
lock, _, _ := cluster.NewForT(t, 3, 3, 3, seed, random)
require.NoError(t, lock.VerifyHashes())
require.NoError(t, lock.VerifySignatures())
}
Loading
Loading