Skip to content

Commit b44f24e

Browse files
authored
core, trie: speed up some tests with quadratic processing flaw (#21987)
This commit fixes a flaw in two testcases, and brings down the exec-time from ~40s to ~8s for trie/TestIncompleteSync. The checkConsistency was performed over and over again on the complete set of nodes, not just the recently added, turning it into a quadratic runtime.
1 parent 9f6bb49 commit b44f24e

File tree

2 files changed

+16
-21
lines changed

2 files changed

+16
-21
lines changed

core/state/sync_test.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ func makeTestState() (Database, common.Hash, []*testAccount) {
6262
}
6363
if i%5 == 0 {
6464
for j := byte(0); j < 5; j++ {
65-
obj.SetState(db, crypto.Keccak256Hash([]byte{i, i, i, i, i, j, j}), crypto.Keccak256Hash([]byte{i, i, i, i, i, j, j}))
65+
hash := crypto.Keccak256Hash([]byte{i, i, i, i, i, j, j})
66+
obj.SetState(db, hash, hash)
6667
}
6768
}
6869
state.updateStateObject(obj)
@@ -401,15 +402,14 @@ func TestIncompleteStateSync(t *testing.T) {
401402
// Create a random state to copy
402403
srcDb, srcRoot, srcAccounts := makeTestState()
403404

404-
// isCode reports whether the hash is contract code hash.
405-
isCode := func(hash common.Hash) bool {
406-
for _, acc := range srcAccounts {
407-
if hash == crypto.Keccak256Hash(acc.code) {
408-
return true
409-
}
405+
// isCodeLookup to save some hashing
406+
var isCode = make(map[common.Hash]struct{})
407+
for _, acc := range srcAccounts {
408+
if len(acc.code) > 0 {
409+
isCode[crypto.Keccak256Hash(acc.code)] = struct{}{}
410410
}
411-
return false
412411
}
412+
isCode[common.BytesToHash(emptyCodeHash)] = struct{}{}
413413
checkTrieConsistency(srcDb.TrieDB().DiskDB().(ethdb.Database), srcRoot)
414414

415415
// Create a destination state and sync with the scheduler
@@ -447,15 +447,13 @@ func TestIncompleteStateSync(t *testing.T) {
447447
batch.Write()
448448
for _, result := range results {
449449
added = append(added, result.Hash)
450-
}
451-
// Check that all known sub-tries added so far are complete or missing entirely.
452-
for _, hash := range added {
453-
if isCode(hash) {
450+
// Check that all known sub-tries added so far are complete or missing entirely.
451+
if _, ok := isCode[result.Hash]; ok {
454452
continue
455453
}
456454
// Can't use checkStateConsistency here because subtrie keys may have odd
457455
// length and crash in LeafKey.
458-
if err := checkTrieConsistency(dstDb, hash); err != nil {
456+
if err := checkTrieConsistency(dstDb, result.Hash); err != nil {
459457
t.Fatalf("state inconsistent: %v", err)
460458
}
461459
}
@@ -466,9 +464,9 @@ func TestIncompleteStateSync(t *testing.T) {
466464
// Sanity check that removing any node from the database is detected
467465
for _, node := range added[1:] {
468466
var (
469-
key = node.Bytes()
470-
code = isCode(node)
471-
val []byte
467+
key = node.Bytes()
468+
_, code = isCode[node]
469+
val []byte
472470
)
473471
if code {
474472
val = rawdb.ReadCode(dstDb, node)

trie/sync_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,6 @@ func TestIncompleteSync(t *testing.T) {
377377

378378
nodes, _, codes := sched.Missing(1)
379379
queue := append(append([]common.Hash{}, nodes...), codes...)
380-
381380
for len(queue) > 0 {
382381
// Fetch a batch of trie nodes
383382
results := make([]SyncResult, len(queue))
@@ -401,10 +400,8 @@ func TestIncompleteSync(t *testing.T) {
401400
batch.Write()
402401
for _, result := range results {
403402
added = append(added, result.Hash)
404-
}
405-
// Check that all known sub-tries in the synced trie are complete
406-
for _, root := range added {
407-
if err := checkTrieConsistency(triedb, root); err != nil {
403+
// Check that all known sub-tries in the synced trie are complete
404+
if err := checkTrieConsistency(triedb, result.Hash); err != nil {
408405
t.Fatalf("trie inconsistent: %v", err)
409406
}
410407
}

0 commit comments

Comments
 (0)