Skip to content

Commit c5531d0

Browse files
MariusVanDerWijdenshekhirin
authored andcommitted
trie: reduce unit test time (ethereum#26918)
1 parent f9591fa commit c5531d0

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

trie/proof_test.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
crand "crypto/rand"
2222
"encoding/binary"
23+
"fmt"
2324
mrand "math/rand"
2425
"sort"
2526
"testing"
@@ -30,6 +31,24 @@ import (
3031
"github.com/ethereum/go-ethereum/ethdb/memorydb"
3132
)
3233

34+
// Prng is a pseudo random number generator seeded by strong randomness.
35+
// The randomness is printed on startup in order to make failures reproducible.
36+
var prng = initRnd()
37+
38+
func initRnd() *mrand.Rand {
39+
var seed [8]byte
40+
crand.Read(seed[:])
41+
rnd := mrand.New(mrand.NewSource(int64(binary.LittleEndian.Uint64(seed[:]))))
42+
fmt.Printf("Seed: %x\n", seed)
43+
return rnd
44+
}
45+
46+
func randBytes(n int) []byte {
47+
r := make([]byte, n)
48+
prng.Read(r)
49+
return r
50+
}
51+
3352
// makeProvers creates Merkle trie provers based on different implementations to
3453
// test all variations.
3554
func makeProvers(trie *Trie) []func(key []byte) *memorydb.Database {
@@ -1041,12 +1060,6 @@ func randomTrie(n int) (*Trie, map[string]*kv) {
10411060
return trie, vals
10421061
}
10431062

1044-
func randBytes(n int) []byte {
1045-
r := make([]byte, n)
1046-
crand.Read(r)
1047-
return r
1048-
}
1049-
10501063
func nonRandomTrie(n int) (*Trie, map[string]*kv) {
10511064
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase()))
10521065
vals := make(map[string]*kv)

trie/sync_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ func TestDuplicateAvoidanceSync(t *testing.T) {
434434
// Tests that at any point in time during a sync, only complete sub-tries are in
435435
// the database.
436436
func TestIncompleteSync(t *testing.T) {
437+
t.Parallel()
437438
// Create a random trie to copy
438439
srcDb, srcTrie, _ := makeTestTrie()
439440

trie/trie_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package trie
1818

1919
import (
2020
"bytes"
21-
crand "crypto/rand"
2221
"encoding/binary"
2322
"errors"
2423
"fmt"
@@ -1146,13 +1145,14 @@ func deleteString(trie *Trie, k string) {
11461145

11471146
func TestDecodeNode(t *testing.T) {
11481147
t.Parallel()
1148+
11491149
var (
11501150
hash = make([]byte, 20)
11511151
elems = make([]byte, 20)
11521152
)
11531153
for i := 0; i < 5000000; i++ {
1154-
crand.Read(hash)
1155-
crand.Read(elems)
1154+
prng.Read(hash)
1155+
prng.Read(elems)
11561156
decodeNode(hash, elems)
11571157
}
11581158
}

0 commit comments

Comments
 (0)