Skip to content

Commit bceb10b

Browse files
bors[bot]Tarak Ben Yousseftarakby
authored
Merge #4112
4112: [Randmomness part 5] update math/rand usage in ledger r=tarakby a=tarakby ( result of splitting #4052 into several PRs) - prepare for Go1.20 update by removing deprecated `math/rand` functions `Seed` and `Read`. Co-authored-by: Tarak Ben Youssef <[email protected]> Co-authored-by: Tarak Ben Youssef <[email protected]>
2 parents 10204df + 7d086e0 commit bceb10b

File tree

12 files changed

+79
-56
lines changed

12 files changed

+79
-56
lines changed

ledger/common/bitutils/utils_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package bitutils
22

33
import (
4+
crand "crypto/rand"
45
"math/big"
56
"math/bits"
67
"math/rand"
@@ -9,6 +10,7 @@ import (
910
"testing"
1011

1112
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
1214
)
1315

1416
func TestBitVectorAllocation(t *testing.T) {
@@ -38,7 +40,6 @@ func Test_PaddedByteSliceLength(t *testing.T) {
3840
func TestBitTools(t *testing.T) {
3941
seed := time.Now().UnixNano()
4042
t.Logf("rand seed is %d", seed)
41-
rand.Seed(seed)
4243
r := rand.NewSource(seed)
4344

4445
const maxBits = 131 * 8 // upper bound of indices to test
@@ -71,7 +72,8 @@ func TestBitTools(t *testing.T) {
7172
t.Run("testing WriteBit", func(t *testing.T) {
7273
b.SetInt64(0)
7374
bytes := MakeBitVector(maxBits)
74-
rand.Read(bytes) // fill bytes with random values to verify that writing to each individual bit works
75+
_, err := crand.Read(bytes) // fill bytes with random values to verify that writing to each individual bit works
76+
require.NoError(t, err)
7577

7678
// build a random big bit by bit
7779
for idx := 0; idx < maxBits; idx++ {
@@ -91,7 +93,8 @@ func TestBitTools(t *testing.T) {
9193
t.Run("testing ClearBit and SetBit", func(t *testing.T) {
9294
b.SetInt64(0)
9395
bytes := MakeBitVector(maxBits)
94-
rand.Read(bytes) // fill bytes with random values to verify that writing to each individual bit works
96+
_, err := crand.Read(bytes) // fill bytes with random values to verify that writing to each individual bit works
97+
require.NoError(t, err)
9598

9699
// build a random big bit by bit
97100
for idx := 0; idx < maxBits; idx++ {

ledger/common/hash/hash_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
package hash_test
22

33
import (
4-
"math/rand"
4+
"crypto/rand"
55
"testing"
6-
"time"
76

87
"golang.org/x/crypto/sha3"
98

109
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
1111

1212
cryhash "github.com/onflow/flow-go/crypto/hash"
1313
"github.com/onflow/flow-go/ledger"
1414
"github.com/onflow/flow-go/ledger/common/hash"
1515
)
1616

1717
func TestHash(t *testing.T) {
18-
r := time.Now().UnixNano()
19-
rand.Seed(r)
20-
t.Logf("math rand seed is %d", r)
21-
2218
t.Run("lengthSanity", func(t *testing.T) {
2319
assert.Equal(t, 32, hash.HashLen)
2420
})
@@ -28,8 +24,10 @@ func TestHash(t *testing.T) {
2824

2925
for i := 0; i < 5000; i++ {
3026
value := make([]byte, i)
31-
rand.Read(path[:])
32-
rand.Read(value)
27+
_, err := rand.Read(path[:])
28+
require.NoError(t, err)
29+
_, err = rand.Read(value)
30+
require.NoError(t, err)
3331
h := hash.HashLeaf(path, value)
3432

3533
hasher := sha3.New256()
@@ -44,8 +42,10 @@ func TestHash(t *testing.T) {
4442
var h1, h2 hash.Hash
4543

4644
for i := 0; i < 5000; i++ {
47-
rand.Read(h1[:])
48-
rand.Read(h2[:])
45+
_, err := rand.Read(h1[:])
46+
require.NoError(t, err)
47+
_, err = rand.Read(h2[:])
48+
require.NoError(t, err)
4949
h := hash.HashInterNode(h1, h2)
5050

5151
hasher := sha3.New256()
@@ -94,8 +94,8 @@ func Test_ComputeCompactValue(t *testing.T) {
9494
func BenchmarkHash(b *testing.B) {
9595

9696
var h1, h2 hash.Hash
97-
rand.Read(h1[:])
98-
rand.Read(h2[:])
97+
_, _ = rand.Read(h1[:])
98+
_, _ = rand.Read(h2[:])
9999

100100
// customized sha3 for ledger
101101
b.Run("LedgerSha3", func(b *testing.B) {

ledger/common/testutils/testutils.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package testutils
22

33
import (
4+
crand "crypto/rand"
45
"encoding/binary"
56
"encoding/hex"
67
"fmt"
@@ -151,7 +152,10 @@ func RandomPaths(n int) []l.Path {
151152
i := 0
152153
for i < n {
153154
var path l.Path
154-
rand.Read(path[:])
155+
_, err := crand.Read(path[:])
156+
if err != nil {
157+
panic("randomness failed")
158+
}
155159
// deduplicate
156160
if _, found := alreadySelectPaths[path]; !found {
157161
paths = append(paths, path)
@@ -166,11 +170,17 @@ func RandomPaths(n int) []l.Path {
166170
func RandomPayload(minByteSize int, maxByteSize int) *l.Payload {
167171
keyByteSize := minByteSize + rand.Intn(maxByteSize-minByteSize)
168172
keydata := make([]byte, keyByteSize)
169-
rand.Read(keydata)
173+
_, err := crand.Read(keydata)
174+
if err != nil {
175+
panic("randomness failed")
176+
}
170177
key := l.Key{KeyParts: []l.KeyPart{{Type: 0, Value: keydata}}}
171178
valueByteSize := minByteSize + rand.Intn(maxByteSize-minByteSize)
172179
valuedata := make([]byte, valueByteSize)
173-
rand.Read(valuedata)
180+
_, err = crand.Read(valuedata)
181+
if err != nil {
182+
panic("random generation failed")
183+
}
174184
value := l.Value(valuedata)
175185
return l.NewPayload(key, value)
176186
}
@@ -196,7 +206,10 @@ func RandomValues(n int, minByteSize, maxByteSize int) []l.Value {
196206
byteSize = minByteSize + rand.Intn(maxByteSize-minByteSize)
197207
}
198208
value := make([]byte, byteSize)
199-
rand.Read(value)
209+
_, err := rand.Read(value)
210+
if err != nil {
211+
panic("random generation failed")
212+
}
200213
values = append(values, value)
201214
}
202215
return values
@@ -218,7 +231,10 @@ func RandomUniqueKeys(n, m, minByteSize, maxByteSize int) []l.Key {
218231
byteSize = minByteSize + rand.Intn(maxByteSize-minByteSize)
219232
}
220233
keyPartData := make([]byte, byteSize)
221-
rand.Read(keyPartData)
234+
_, err := crand.Read(keyPartData)
235+
if err != nil {
236+
panic("random generation failed")
237+
}
222238
keyParts = append(keyParts, l.NewKeyPart(uint16(j), keyPartData))
223239
}
224240
key := l.NewKey(keyParts)

ledger/complete/ledger_benchmark_test.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package complete_test
22

33
import (
44
"math"
5-
"math/rand"
65
"testing"
76
"time"
87

@@ -40,8 +39,6 @@ func benchmarkStorage(steps int, b *testing.B) {
4039
checkpointsToKeep = 1
4140
)
4241

43-
rand.Seed(time.Now().UnixNano())
44-
4542
dir := b.TempDir()
4643

4744
diskWal, err := wal.NewDiskWAL(zerolog.Nop(), nil, metrics.NewNoopCollector(), dir, steps+1, pathfinder.PathByteSize, wal.SegmentSize)
@@ -155,8 +152,6 @@ func BenchmarkTrieUpdate(b *testing.B) {
155152
checkpointsToKeep = 1
156153
)
157154

158-
rand.Seed(1)
159-
160155
dir := b.TempDir()
161156

162157
diskWal, err := wal.NewDiskWAL(zerolog.Nop(), nil, metrics.NewNoopCollector(), dir, capacity, pathfinder.PathByteSize, wal.SegmentSize)
@@ -209,8 +204,6 @@ func BenchmarkTrieRead(b *testing.B) {
209204
checkpointsToKeep = 1
210205
)
211206

212-
rand.Seed(1)
213-
214207
dir := b.TempDir()
215208

216209
diskWal, err := wal.NewDiskWAL(zerolog.Nop(), nil, metrics.NewNoopCollector(), dir, capacity, pathfinder.PathByteSize, wal.SegmentSize)
@@ -272,8 +265,6 @@ func BenchmarkLedgerGetOneValue(b *testing.B) {
272265
checkpointsToKeep = 1
273266
)
274267

275-
rand.Seed(1)
276-
277268
dir := b.TempDir()
278269

279270
diskWal, err := wal.NewDiskWAL(zerolog.Nop(), nil, metrics.NewNoopCollector(), dir, capacity, pathfinder.PathByteSize, wal.SegmentSize)
@@ -352,8 +343,6 @@ func BenchmarkTrieProve(b *testing.B) {
352343
checkpointsToKeep = 1
353344
)
354345

355-
rand.Seed(1)
356-
357346
dir := b.TempDir()
358347

359348
diskWal, err := wal.NewDiskWAL(zerolog.Nop(), nil, metrics.NewNoopCollector(), dir, capacity, pathfinder.PathByteSize, wal.SegmentSize)

ledger/complete/ledger_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"math"
88
"math/rand"
99
"testing"
10-
"time"
1110

1211
"github.com/rs/zerolog"
1312
"github.com/stretchr/testify/assert"
@@ -591,7 +590,6 @@ func TestLedgerFunctionality(t *testing.T) {
591590
checkpointsToKeep = 1
592591
)
593592

594-
rand.Seed(time.Now().UnixNano())
595593
// You can manually increase this for more coverage
596594
experimentRep := 2
597595
metricsCollector := &metrics.NoopCollector{}

ledger/complete/mtrie/flattener/encoding_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package flattener_test
22

33
import (
44
"bytes"
5+
crand "crypto/rand"
56
"errors"
67
"fmt"
78
"math/rand"
@@ -160,7 +161,8 @@ func TestRandomLeafNodeEncodingDecoding(t *testing.T) {
160161
height := rand.Intn(257)
161162

162163
var hashValue hash.Hash
163-
rand.Read(hashValue[:])
164+
_, err := crand.Read(hashValue[:])
165+
require.NoError(t, err)
164166

165167
n := node.NewNode(height, nil, nil, paths[i], payloads[i], hashValue)
166168

ledger/complete/mtrie/forest_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,6 @@ func TestRandomUpdateReadProofValueSizes(t *testing.T) {
783783
rep := 10
784784
maxNumPathsPerStep := 10
785785
seed := time.Now().UnixNano()
786-
rand.Seed(seed)
787786
t.Log(seed)
788787

789788
forest, err := NewForest(5, &metrics.NoopCollector{}, nil)

ledger/complete/mtrie/trie/trie_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ import (
55
"encoding/binary"
66
"encoding/hex"
77
"math"
8-
"math/rand"
98
"sort"
109
"testing"
11-
"time"
1210

1311
"github.com/stretchr/testify/require"
1412
"gotest.tools/assert"
@@ -354,9 +352,7 @@ func deduplicateWrites(paths []ledger.Path, payloads []ledger.Payload) ([]ledger
354352
}
355353

356354
func TestSplitByPath(t *testing.T) {
357-
seed := time.Now().UnixNano()
358-
t.Logf("rand seed is %d", seed)
359-
rand.Seed(seed)
355+
rand := unittest.GetPRG(t)
360356

361357
const pathsNumber = 100
362358
const redundantPaths = 10
@@ -367,7 +363,8 @@ func TestSplitByPath(t *testing.T) {
367363
paths := make([]ledger.Path, 0, pathsNumber)
368364
for i := 0; i < pathsNumber-redundantPaths; i++ {
369365
var p ledger.Path
370-
rand.Read(p[:])
366+
_, err := rand.Read(p[:])
367+
require.NoError(t, err)
371368
paths = append(paths, p)
372369
}
373370
for i := 0; i < redundantPaths; i++ {
@@ -490,6 +487,7 @@ func Test_DifferentiateEmptyVsLeaf(t *testing.T) {
490487
}
491488

492489
func Test_Pruning(t *testing.T) {
490+
rand := unittest.GetPRG(t)
493491
emptyTrie := trie.NewEmptyMTrie()
494492

495493
path1 := testutils.PathByUint16(1 << 12) // 000100...
@@ -655,7 +653,8 @@ func Test_Pruning(t *testing.T) {
655653

656654
for i := 0; i < numberOfUpdates; {
657655
var path ledger.Path
658-
rand.Read(path[:])
656+
_, err := rand.Read(path[:])
657+
require.NoError(t, err)
659658
// deduplicate
660659
if _, found := allPaths[path]; !found {
661660
payload := testutils.RandomPayload(1, 100)

ledger/complete/mtrie/trieCache_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package mtrie
66
// test across boundry
77

88
import (
9-
"math/rand"
9+
"crypto/rand"
1010
"testing"
1111

1212
"github.com/stretchr/testify/require"
@@ -174,10 +174,16 @@ func TestConcurrentAccess(t *testing.T) {
174174

175175
func randomMTrie() (*trie.MTrie, error) {
176176
var randomPath ledger.Path
177-
rand.Read(randomPath[:])
177+
_, err := rand.Read(randomPath[:])
178+
if err != nil {
179+
return nil, err
180+
}
178181

179182
var randomHashValue hash.Hash
180-
rand.Read(randomHashValue[:])
183+
_, err = rand.Read(randomHashValue[:])
184+
if err != nil {
185+
return nil, err
186+
}
181187

182188
root := node.NewNode(256, nil, nil, randomPath, nil, randomHashValue)
183189

ledger/complete/wal/checkpoint_v6_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package wal
33
import (
44
"bufio"
55
"bytes"
6+
"crypto/rand"
67
"errors"
78
"fmt"
89
"io"
9-
"math/rand"
1010
"os"
1111
"path"
1212
"path/filepath"
@@ -87,7 +87,10 @@ func createSimpleTrie(t *testing.T) []*trie.MTrie {
8787

8888
func randPathPayload() (ledger.Path, ledger.Payload) {
8989
var path ledger.Path
90-
rand.Read(path[:])
90+
_, err := rand.Read(path[:])
91+
if err != nil {
92+
panic("randomness failed")
93+
}
9194
payload := testutils.RandomPayload(1, 100)
9295
return path, *payload
9396
}
@@ -220,10 +223,16 @@ func TestEncodeSubTrie(t *testing.T) {
220223

221224
func randomNode() *node.Node {
222225
var randomPath ledger.Path
223-
rand.Read(randomPath[:])
226+
_, err := rand.Read(randomPath[:])
227+
if err != nil {
228+
panic("randomness failed")
229+
}
224230

225231
var randomHashValue hash.Hash
226-
rand.Read(randomHashValue[:])
232+
_, err = rand.Read(randomHashValue[:])
233+
if err != nil {
234+
panic("randomness failed")
235+
}
227236

228237
return node.NewNode(256, nil, nil, randomPath, nil, randomHashValue)
229238
}

0 commit comments

Comments
 (0)