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

core, trie, rpc: speed up tests #28461

Merged
merged 7 commits into from
Nov 21, 2023
Merged
Changes from 1 commit
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
29 changes: 24 additions & 5 deletions trie/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func makeTestTrie(scheme string) (ethdb.Database, *Database, *StateTrie, map[str
func checkTrieContents(t *testing.T, db ethdb.Database, scheme string, root []byte, content map[string][]byte, rawTrie bool) {
// Check root availability and trie contents
ndb := newTestDatabase(db, scheme)
if err := checkTrieConsistency(db, scheme, common.BytesToHash(root), rawTrie); err != nil {
if err := checkTrieConsistency(db, scheme, common.BytesToHash(root), rawTrie, nil); err != nil {
t.Fatalf("inconsistent trie at %x: %v", root, err)
}
type reader interface {
Expand Down Expand Up @@ -101,7 +101,7 @@ func checkTrieContents(t *testing.T, db ethdb.Database, scheme string, root []by
}

// checkTrieConsistency checks that all nodes in a trie are indeed present.
func checkTrieConsistency(db ethdb.Database, scheme string, root common.Hash, rawTrie bool) error {
func checkTrieConsistency(db ethdb.Database, scheme string, root common.Hash, rawTrie bool, resolver NodeResolver) error {
ndb := newTestDatabase(db, scheme)
var it NodeIterator
if rawTrie {
Expand All @@ -117,6 +117,9 @@ func checkTrieConsistency(db ethdb.Database, scheme string, root common.Hash, ra
}
it = trie.MustNodeIterator(nil)
}
if resolver != nil {
it.AddResolver(resolver)
}
for it.Next(true) {
}
return it.Error()
Expand Down Expand Up @@ -512,8 +515,8 @@ func testDuplicateAvoidanceSync(t *testing.T, scheme string) {
// Tests that at any point in time during a sync, only complete sub-tries are in
// the database.
func TestIncompleteSyncHash(t *testing.T) {
testIncompleteSync(t, rawdb.HashScheme)
testIncompleteSync(t, rawdb.PathScheme)
testIncompleteSync(t, rawdb.HashScheme) // 9.585s
testIncompleteSync(t, rawdb.PathScheme) // 19.522
}

func testIncompleteSync(t *testing.T, scheme string) {
Expand Down Expand Up @@ -585,16 +588,32 @@ func testIncompleteSync(t *testing.T, scheme string) {
})
}
}

// Cache trie nodes for faster verification
nodesCache := make(map[string][]byte)
resolver := func(owner common.Hash, path []byte, hash common.Hash) []byte {
if scheme == rawdb.HashScheme {
return nodesCache[string(hash[:])]
}
return nodesCache[string(path)]
}

// Sanity check that removing any node from the database is detected
for i, path := range addedKeys {
owner, inner := ResolvePath([]byte(path))
nodeHash := addedHashes[i]
value := rawdb.ReadTrieNode(diskdb, owner, inner, nodeHash, scheme)
rawdb.DeleteTrieNode(diskdb, owner, inner, nodeHash, scheme)
if err := checkTrieConsistency(diskdb, srcDb.Scheme(), root, false); err == nil {
if err := checkTrieConsistency(diskdb, srcDb.Scheme(), root, false, resolver); err == nil {
MariusVanDerWijden marked this conversation as resolved.
Show resolved Hide resolved
t.Fatalf("trie inconsistency not caught, missing: %x", path)
}
rawdb.WriteTrieNode(diskdb, owner, inner, nodeHash, value, scheme)
// We only add nodes to the cache that we already visited
if scheme == rawdb.HashScheme {
nodesCache[string(nodeHash[:])] = value
} else {
nodesCache[string(inner[:])] = value
}
}
}

Expand Down