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

trie: reduce unit test time #26918

Merged
merged 3 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
25 changes: 19 additions & 6 deletions trie/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
crand "crypto/rand"
"encoding/binary"
"fmt"
mrand "math/rand"
"sort"
"testing"
Expand All @@ -30,6 +31,24 @@ import (
"github.com/ethereum/go-ethereum/ethdb/memorydb"
)

// Prng is a pseudo random number generator seeded by strong randomness.
// The randomness is printed on startup in order to make failures reproducible.
var prng = initRnd()

func initRnd() *mrand.Rand {
var seed [8]byte
crand.Read(seed[:])
rnd := mrand.New(mrand.NewSource(int64(binary.LittleEndian.Uint64(seed[:]))))
fmt.Printf("Seed: %x\n", seed)
return rnd
}

func randBytes(n int) []byte {
r := make([]byte, n)
prng.Read(r)
return r
}

// makeProvers creates Merkle trie provers based on different implementations to
// test all variations.
func makeProvers(trie *Trie) []func(key []byte) *memorydb.Database {
Expand Down Expand Up @@ -1041,12 +1060,6 @@ func randomTrie(n int) (*Trie, map[string]*kv) {
return trie, vals
}

func randBytes(n int) []byte {
r := make([]byte, n)
crand.Read(r)
return r
}

func nonRandomTrie(n int) (*Trie, map[string]*kv) {
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase()))
vals := make(map[string]*kv)
Expand Down
6 changes: 3 additions & 3 deletions trie/trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package trie

import (
"bytes"
crand "crypto/rand"
"encoding/binary"
"errors"
"fmt"
Expand Down Expand Up @@ -1146,13 +1145,14 @@ func deleteString(trie *Trie, k string) {

func TestDecodeNode(t *testing.T) {
t.Parallel()

var (
hash = make([]byte, 20)
elems = make([]byte, 20)
)
for i := 0; i < 5000000; i++ {
crand.Read(hash)
crand.Read(elems)
prng.Read(hash)
prng.Read(elems)
decodeNode(hash, elems)
}
}