trie: reduce unit test time (#26918)

This commit is contained in:
Marius van der Wijden 2023-03-20 09:09:35 +01:00 committed by GitHub
parent ee8e83fa5f
commit 81b0aa0cc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 9 deletions

View File

@ -20,6 +20,7 @@ import (
"bytes" "bytes"
crand "crypto/rand" crand "crypto/rand"
"encoding/binary" "encoding/binary"
"fmt"
mrand "math/rand" mrand "math/rand"
"sort" "sort"
"testing" "testing"
@ -30,6 +31,24 @@ import (
"github.com/ethereum/go-ethereum/ethdb/memorydb" "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 // makeProvers creates Merkle trie provers based on different implementations to
// test all variations. // test all variations.
func makeProvers(trie *Trie) []func(key []byte) *memorydb.Database { func makeProvers(trie *Trie) []func(key []byte) *memorydb.Database {
@ -1041,12 +1060,6 @@ func randomTrie(n int) (*Trie, map[string]*kv) {
return trie, vals 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) { func nonRandomTrie(n int) (*Trie, map[string]*kv) {
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase())) trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase()))
vals := make(map[string]*kv) vals := make(map[string]*kv)

View File

@ -434,6 +434,7 @@ func TestDuplicateAvoidanceSync(t *testing.T) {
// Tests that at any point in time during a sync, only complete sub-tries are in // Tests that at any point in time during a sync, only complete sub-tries are in
// the database. // the database.
func TestIncompleteSync(t *testing.T) { func TestIncompleteSync(t *testing.T) {
t.Parallel()
// Create a random trie to copy // Create a random trie to copy
srcDb, srcTrie, _ := makeTestTrie() srcDb, srcTrie, _ := makeTestTrie()

View File

@ -18,7 +18,6 @@ package trie
import ( import (
"bytes" "bytes"
crand "crypto/rand"
"encoding/binary" "encoding/binary"
"errors" "errors"
"fmt" "fmt"
@ -1146,13 +1145,14 @@ func deleteString(trie *Trie, k string) {
func TestDecodeNode(t *testing.T) { func TestDecodeNode(t *testing.T) {
t.Parallel() t.Parallel()
var ( var (
hash = make([]byte, 20) hash = make([]byte, 20)
elems = make([]byte, 20) elems = make([]byte, 20)
) )
for i := 0; i < 5000000; i++ { for i := 0; i < 5000000; i++ {
crand.Read(hash) prng.Read(hash)
crand.Read(elems) prng.Read(elems)
decodeNode(hash, elems) decodeNode(hash, elems)
} }
} }