Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 14 additions & 16 deletions core/state/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ func makeTestState() (Database, common.Hash, []*testAccount) {
}
if i%5 == 0 {
for j := byte(0); j < 5; j++ {
obj.SetState(db, crypto.Keccak256Hash([]byte{i, i, i, i, i, j, j}), crypto.Keccak256Hash([]byte{i, i, i, i, i, j, j}))
hash := crypto.Keccak256Hash([]byte{i, i, i, i, i, j, j})
obj.SetState(db, hash, hash)
}
}
state.updateStateObject(obj)
Expand Down Expand Up @@ -401,15 +402,14 @@ func TestIncompleteStateSync(t *testing.T) {
// Create a random state to copy
srcDb, srcRoot, srcAccounts := makeTestState()

// isCode reports whether the hash is contract code hash.
isCode := func(hash common.Hash) bool {
for _, acc := range srcAccounts {
if hash == crypto.Keccak256Hash(acc.code) {
return true
}
/// isCodeLookup to save some hashing
var isCode = make(map[common.Hash]struct{})
for _, acc := range srcAccounts {
if len(acc.code) > 0 {
isCode[crypto.Keccak256Hash(acc.code)] = struct{}{}
}
return false
}
isCode[types.EmptyCodeHash] = struct{}{}
checkTrieConsistency(srcDb.TrieDB().DiskDB().(ethdb.Database), srcRoot)

// Create a destination state and sync with the scheduler
Expand Down Expand Up @@ -447,15 +447,13 @@ func TestIncompleteStateSync(t *testing.T) {
batch.Write()
for _, result := range results {
added = append(added, result.Hash)
}
// Check that all known sub-tries added so far are complete or missing entirely.
for _, hash := range added {
if isCode(hash) {
// Check that all known sub-tries added so far are complete or missing entirely.
if _, ok := isCode[result.Hash]; ok {
continue
}
// Can't use checkStateConsistency here because subtrie keys may have odd
// length and crash in LeafKey.
if err := checkTrieConsistency(dstDb, hash); err != nil {
if err := checkTrieConsistency(dstDb, result.Hash); err != nil {
t.Fatalf("state inconsistent: %v", err)
}
}
Expand All @@ -466,9 +464,9 @@ func TestIncompleteStateSync(t *testing.T) {
// Sanity check that removing any node from the database is detected
for _, node := range added[1:] {
var (
key = node.Bytes()
code = isCode(node)
val []byte
key = node.Bytes()
_, code = isCode[node]
val []byte
)
if code {
val = rawdb.ReadCode(dstDb, node)
Expand Down
11 changes: 4 additions & 7 deletions trie/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,14 +378,13 @@ func TestIncompleteSync(t *testing.T) {

nodes, _, codes := sched.Missing(1)
queue := append(append([]common.Hash{}, nodes...), codes...)

for len(queue) > 0 {
// Fetch a batch of trie nodes
results := make([]SyncResult, len(queue))
for i, hash := range queue {
data, err := srcDb.Node(hash)
if err != nil {
t.Fatalf("failed to retrieve Node data for %x: %v", hash, err)
t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
}
results[i] = SyncResult{hash, data}
}
Expand All @@ -402,18 +401,16 @@ func TestIncompleteSync(t *testing.T) {
batch.Write()
for _, result := range results {
added = append(added, result.Hash)
}
// Check that all known sub-tries in the synced trie are complete
for _, root := range added {
if err := checkTrieConsistency(triedb, root); err != nil {
// Check that all known sub-tries in the synced trie are complete
if err := checkTrieConsistency(triedb, result.Hash); err != nil {
t.Fatalf("trie inconsistent: %v", err)
}
}
// Fetch the next batch to retrieve
nodes, _, codes = sched.Missing(1)
queue = append(append(queue[:0], nodes...), codes...)
}
// Sanity check that removing any Node from the database is detected
// Sanity check that removing any node from the database is detected
for _, node := range added[1:] {
key := node.Bytes()
value, _ := diskdb.Get(key)
Expand Down