Skip to content

Commit bf9e6b1

Browse files
rjl493456442jorgemmsilva
authored andcommitted
triedb/pathdb: improve tests (ethereum#29278)
1 parent c151918 commit bf9e6b1

File tree

8 files changed

+104
-56
lines changed

8 files changed

+104
-56
lines changed

eth/protocols/snap/sync_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ import (
3232
"github.com/ethereum/go-ethereum/core/types"
3333
"github.com/ethereum/go-ethereum/crypto"
3434
"github.com/ethereum/go-ethereum/ethdb"
35+
"github.com/ethereum/go-ethereum/internal/testrand"
3536
"github.com/ethereum/go-ethereum/log"
3637
"github.com/ethereum/go-ethereum/rlp"
3738
"github.com/ethereum/go-ethereum/trie"
38-
"github.com/ethereum/go-ethereum/trie/testutil"
3939
"github.com/ethereum/go-ethereum/trie/trienode"
4040
"github.com/ethereum/go-ethereum/triedb"
4141
"github.com/ethereum/go-ethereum/triedb/pathdb"
@@ -1816,8 +1816,8 @@ func makeUnevenStorageTrie(owner common.Hash, slots int, db *triedb.Database) (c
18161816
break
18171817
}
18181818
for j := 0; j < slots/3; j++ {
1819-
key := append([]byte{byte(n)}, testutil.RandBytes(31)...)
1820-
val, _ := rlp.EncodeToBytes(testutil.RandBytes(32))
1819+
key := append([]byte{byte(n)}, testrand.Bytes(31)...)
1820+
val, _ := rlp.EncodeToBytes(testrand.Bytes(32))
18211821

18221822
elem := &kv{key, val}
18231823
tr.MustUpdate(elem.k, elem.v)

trie/testutil/utils.go renamed to internal/testrand/rand.go

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,17 @@
1414
// You should have received a copy of the GNU Lesser General Public License
1515
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616

17-
package testutil
17+
package testrand
1818

1919
import (
2020
crand "crypto/rand"
2121
"encoding/binary"
2222
mrand "math/rand"
2323

2424
"github.com/ethereum/go-ethereum/common"
25-
"github.com/ethereum/go-ethereum/crypto"
26-
"github.com/ethereum/go-ethereum/trie/trienode"
2725
)
2826

29-
// Prng is a pseudo random number generator seeded by strong randomness.
27+
// prng is a pseudo random number generator seeded by strong randomness.
3028
// The randomness is printed on startup in order to make failures reproducible.
3129
var prng = initRand()
3230

@@ -37,25 +35,19 @@ func initRand() *mrand.Rand {
3735
return rnd
3836
}
3937

40-
// RandBytes generates a random byte slice with specified length.
41-
func RandBytes(n int) []byte {
38+
// Bytes generates a random byte slice with specified length.
39+
func Bytes(n int) []byte {
4240
r := make([]byte, n)
4341
prng.Read(r)
4442
return r
4543
}
4644

47-
// RandomHash generates a random blob of data and returns it as a hash.
48-
func RandomHash() common.Hash {
49-
return common.BytesToHash(RandBytes(common.HashLength))
45+
// Hash generates a random hash.
46+
func Hash() common.Hash {
47+
return common.BytesToHash(Bytes(common.HashLength))
5048
}
5149

52-
// RandomAddress generates a random blob of data and returns it as an address.
53-
func RandomAddress() common.Address {
54-
return common.BytesToAddress(RandBytes(common.AddressLength))
55-
}
56-
57-
// RandomNode generates a random node.
58-
func RandomNode() *trienode.Node {
59-
val := RandBytes(100)
60-
return trienode.New(crypto.Keccak256Hash(val), val)
50+
// Address generates a random address.
51+
func Address() common.Address {
52+
return common.BytesToAddress(Bytes(common.AddressLength))
6153
}

trie/stacktrie_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"github.com/ethereum/go-ethereum/common"
2626
"github.com/ethereum/go-ethereum/core/rawdb"
2727
"github.com/ethereum/go-ethereum/crypto"
28-
"github.com/ethereum/go-ethereum/trie/testutil"
28+
"github.com/ethereum/go-ethereum/internal/testrand"
2929
"github.com/stretchr/testify/assert"
3030
"golang.org/x/exp/slices"
3131
)
@@ -431,12 +431,12 @@ func TestPartialStackTrie(t *testing.T) {
431431
for i := 0; i < n; i++ {
432432
var val []byte
433433
if rand.Intn(3) == 0 {
434-
val = testutil.RandBytes(3)
434+
val = testrand.Bytes(3)
435435
} else {
436-
val = testutil.RandBytes(32)
436+
val = testrand.Bytes(32)
437437
}
438438
entries = append(entries, &kv{
439-
k: testutil.RandBytes(32),
439+
k: testrand.Bytes(32),
440440
v: val,
441441
})
442442
}

triedb/pathdb/database.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ import (
3434
)
3535

3636
const (
37-
// maxDiffLayers is the maximum diff layers allowed in the layer tree.
38-
maxDiffLayers = 128
39-
4037
// defaultCleanSize is the default memory allowance of clean cache.
4138
defaultCleanSize = 16 * 1024 * 1024
4239

@@ -54,6 +51,11 @@ const (
5451
DefaultBufferSize = 64 * 1024 * 1024
5552
)
5653

54+
var (
55+
// maxDiffLayers is the maximum diff layers allowed in the layer tree.
56+
maxDiffLayers = 128
57+
)
58+
5759
// layer is the interface implemented by all state layers which includes some
5860
// public methods and some additional methods for internal usage.
5961
type layer interface {

triedb/pathdb/database_test.go

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import (
2727
"github.com/ethereum/go-ethereum/core/rawdb"
2828
"github.com/ethereum/go-ethereum/core/types"
2929
"github.com/ethereum/go-ethereum/crypto"
30+
"github.com/ethereum/go-ethereum/internal/testrand"
3031
"github.com/ethereum/go-ethereum/rlp"
31-
"github.com/ethereum/go-ethereum/trie/testutil"
3232
"github.com/ethereum/go-ethereum/trie/trienode"
3333
"github.com/ethereum/go-ethereum/trie/triestate"
3434
"github.com/holiman/uint256"
@@ -46,15 +46,18 @@ func updateTrie(addrHash common.Hash, root common.Hash, dirties, cleans map[comm
4646
h.Update(key.Bytes(), val)
4747
}
4848
}
49-
root, nodes, _ := h.Commit(false)
49+
root, nodes, err := h.Commit(false)
50+
if err != nil {
51+
panic(fmt.Errorf("failed to commit hasher, err: %w", err))
52+
}
5053
return root, nodes
5154
}
5255

5356
func generateAccount(storageRoot common.Hash) types.StateAccount {
5457
return types.StateAccount{
5558
Nonce: uint64(rand.Intn(100)),
5659
Balance: uint256.NewInt(rand.Uint64()),
57-
CodeHash: testutil.RandBytes(32),
60+
CodeHash: testrand.Bytes(32),
5861
Root: storageRoot,
5962
}
6063
}
@@ -101,8 +104,8 @@ func newTester(t *testing.T, historyLimit uint64) *tester {
101104
disk, _ = rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false)
102105
db = New(disk, &Config{
103106
StateHistory: historyLimit,
104-
CleanCacheSize: 256 * 1024,
105-
DirtyCacheSize: 256 * 1024,
107+
CleanCacheSize: 16 * 1024,
108+
DirtyCacheSize: 16 * 1024,
106109
})
107110
obj = &tester{
108111
db: db,
@@ -113,7 +116,7 @@ func newTester(t *testing.T, historyLimit uint64) *tester {
113116
snapStorages: make(map[common.Hash]map[common.Hash]map[common.Hash][]byte),
114117
}
115118
)
116-
for i := 0; i < 2*128; i++ {
119+
for i := 0; i < 8; i++ {
117120
var parent = types.EmptyRootHash
118121
if len(obj.roots) != 0 {
119122
parent = obj.roots[len(obj.roots)-1]
@@ -146,8 +149,8 @@ func (t *tester) generateStorage(ctx *genctx, addr common.Address) common.Hash {
146149
origin = make(map[common.Hash][]byte)
147150
)
148151
for i := 0; i < 10; i++ {
149-
v, _ := rlp.EncodeToBytes(common.TrimLeftZeroes(testutil.RandBytes(32)))
150-
hash := testutil.RandomHash()
152+
v, _ := rlp.EncodeToBytes(common.TrimLeftZeroes(testrand.Bytes(32)))
153+
hash := testrand.Hash()
151154

152155
storage[hash] = v
153156
origin[hash] = nil
@@ -175,8 +178,8 @@ func (t *tester) mutateStorage(ctx *genctx, addr common.Address, root common.Has
175178
}
176179
}
177180
for i := 0; i < 3; i++ {
178-
v, _ := rlp.EncodeToBytes(common.TrimLeftZeroes(testutil.RandBytes(32)))
179-
hash := testutil.RandomHash()
181+
v, _ := rlp.EncodeToBytes(common.TrimLeftZeroes(testrand.Bytes(32)))
182+
hash := testrand.Hash()
180183

181184
storage[hash] = v
182185
origin[hash] = nil
@@ -218,7 +221,7 @@ func (t *tester) generate(parent common.Hash) (common.Hash, *trienode.MergedNode
218221
switch rand.Intn(opLen) {
219222
case createAccountOp:
220223
// account creation
221-
addr := testutil.RandomAddress()
224+
addr := testrand.Address()
222225
addrHash := crypto.Keccak256Hash(addr.Bytes())
223226
if _, ok := t.accounts[addrHash]; ok {
224227
continue
@@ -320,14 +323,16 @@ func (t *tester) verifyState(root common.Hash) error {
320323
return errors.New("root node is not available")
321324
}
322325
for addrHash, account := range t.snapAccounts[root] {
323-
blob, err := reader.Node(common.Hash{}, addrHash.Bytes(), crypto.Keccak256Hash(account))
326+
path := crypto.Keccak256(addrHash.Bytes())
327+
blob, err := reader.Node(common.Hash{}, path, crypto.Keccak256Hash(account))
324328
if err != nil || !bytes.Equal(blob, account) {
325329
return fmt.Errorf("account is mismatched: %w", err)
326330
}
327331
}
328332
for addrHash, slots := range t.snapStorages[root] {
329333
for hash, slot := range slots {
330-
blob, err := reader.Node(addrHash, hash.Bytes(), crypto.Keccak256Hash(slot))
334+
path := crypto.Keccak256(hash.Bytes())
335+
blob, err := reader.Node(addrHash, path, crypto.Keccak256Hash(slot))
331336
if err != nil || !bytes.Equal(blob, slot) {
332337
return fmt.Errorf("slot is mismatched: %w", err)
333338
}
@@ -379,6 +384,12 @@ func (t *tester) bottomIndex() int {
379384
}
380385

381386
func TestDatabaseRollback(t *testing.T) {
387+
// Redefine the diff layer depth allowance for faster testing.
388+
maxDiffLayers = 4
389+
defer func() {
390+
maxDiffLayers = 128
391+
}()
392+
382393
// Verify state histories
383394
tester := newTester(t, 0)
384395
defer tester.release()
@@ -409,6 +420,12 @@ func TestDatabaseRollback(t *testing.T) {
409420
}
410421

411422
func TestDatabaseRecoverable(t *testing.T) {
423+
// Redefine the diff layer depth allowance for faster testing.
424+
maxDiffLayers = 4
425+
defer func() {
426+
maxDiffLayers = 128
427+
}()
428+
412429
var (
413430
tester = newTester(t, 0)
414431
index = tester.bottomIndex()
@@ -448,6 +465,12 @@ func TestDatabaseRecoverable(t *testing.T) {
448465
}
449466

450467
func TestDisable(t *testing.T) {
468+
// Redefine the diff layer depth allowance for faster testing.
469+
maxDiffLayers = 4
470+
defer func() {
471+
maxDiffLayers = 128
472+
}()
473+
451474
tester := newTester(t, 0)
452475
defer tester.release()
453476

@@ -484,6 +507,12 @@ func TestDisable(t *testing.T) {
484507
}
485508

486509
func TestCommit(t *testing.T) {
510+
// Redefine the diff layer depth allowance for faster testing.
511+
maxDiffLayers = 4
512+
defer func() {
513+
maxDiffLayers = 128
514+
}()
515+
487516
tester := newTester(t, 0)
488517
defer tester.release()
489518

@@ -508,6 +537,12 @@ func TestCommit(t *testing.T) {
508537
}
509538

510539
func TestJournal(t *testing.T) {
540+
// Redefine the diff layer depth allowance for faster testing.
541+
maxDiffLayers = 4
542+
defer func() {
543+
maxDiffLayers = 128
544+
}()
545+
511546
tester := newTester(t, 0)
512547
defer tester.release()
513548

@@ -532,6 +567,12 @@ func TestJournal(t *testing.T) {
532567
}
533568

534569
func TestCorruptedJournal(t *testing.T) {
570+
// Redefine the diff layer depth allowance for faster testing.
571+
maxDiffLayers = 4
572+
defer func() {
573+
maxDiffLayers = 128
574+
}()
575+
535576
tester := newTester(t, 0)
536577
defer tester.release()
537578

@@ -574,6 +615,12 @@ func TestCorruptedJournal(t *testing.T) {
574615
// truncating the tail histories. This ensures that the ID of the persistent state
575616
// always falls within the range of [oldest-history-id, latest-history-id].
576617
func TestTailTruncateHistory(t *testing.T) {
618+
// Redefine the diff layer depth allowance for faster testing.
619+
maxDiffLayers = 4
620+
defer func() {
621+
maxDiffLayers = 128
622+
}()
623+
577624
tester := newTester(t, 10)
578625
defer tester.release()
579626

triedb/pathdb/difflayer_test.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import (
2222

2323
"github.com/ethereum/go-ethereum/common"
2424
"github.com/ethereum/go-ethereum/core/rawdb"
25-
"github.com/ethereum/go-ethereum/trie/testutil"
25+
"github.com/ethereum/go-ethereum/crypto"
26+
"github.com/ethereum/go-ethereum/internal/testrand"
2627
"github.com/ethereum/go-ethereum/trie/trienode"
2728
)
2829

@@ -66,8 +67,9 @@ func benchmarkSearch(b *testing.B, depth int, total int) {
6667
nodes[common.Hash{}] = make(map[string]*trienode.Node)
6768
for i := 0; i < 3000; i++ {
6869
var (
69-
path = testutil.RandBytes(32)
70-
node = testutil.RandomNode()
70+
path = testrand.Bytes(32)
71+
blob = testrand.Bytes(100)
72+
node = trienode.New(crypto.Keccak256Hash(blob), blob)
7173
)
7274
nodes[common.Hash{}][string(path)] = trienode.New(node.Hash, node.Blob)
7375
if npath == nil && depth == index {
@@ -112,8 +114,9 @@ func BenchmarkPersist(b *testing.B) {
112114
nodes[common.Hash{}] = make(map[string]*trienode.Node)
113115
for i := 0; i < 3000; i++ {
114116
var (
115-
path = testutil.RandBytes(32)
116-
node = testutil.RandomNode()
117+
path = testrand.Bytes(32)
118+
blob = testrand.Bytes(100)
119+
node = trienode.New(crypto.Keccak256Hash(blob), blob)
117120
)
118121
nodes[common.Hash{}][string(path)] = trienode.New(node.Hash, node.Blob)
119122
}
@@ -149,8 +152,9 @@ func BenchmarkJournal(b *testing.B) {
149152
nodes[common.Hash{}] = make(map[string]*trienode.Node)
150153
for i := 0; i < 3000; i++ {
151154
var (
152-
path = testutil.RandBytes(32)
153-
node = testutil.RandomNode()
155+
path = testrand.Bytes(32)
156+
blob = testrand.Bytes(100)
157+
node = trienode.New(crypto.Keccak256Hash(blob), blob)
154158
)
155159
nodes[common.Hash{}][string(path)] = trienode.New(node.Hash, node.Blob)
156160
}

0 commit comments

Comments
 (0)