Update sync.go

This commit is contained in:
Felix Lange 2023-12-08 10:09:36 +01:00 committed by GitHub
parent 5342dd54e5
commit 4c30df6075
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -260,14 +260,13 @@ func (s *Sync) AddSubTrie(root common.Hash, path []byte, parent common.Hash, par
if root == types.EmptyRootHash { if root == types.EmptyRootHash {
return return
} }
// Short circuit if the trie is already known.
owner, inner := ResolvePath(path) owner, inner := ResolvePath(path)
exist, mismatch := s.hasNode(owner, inner, root) exist, inconsistent := s.hasNode(owner, inner, root)
if exist { if exist {
// The entire subtrie is already present in the database.
return return
} } else if inconsistent {
if mismatch { // There is a pre-existing node with the wrong hash in DB, remove it.
// The node is inconsistent with and needs to be removed.
s.membatch.delNode(owner, inner) s.membatch.delNode(owner, inner)
} }
// Assemble the new sub-trie sync request // Assemble the new sub-trie sync request
@ -593,22 +592,22 @@ func (s *Sync) children(req *nodeRequest, object node) ([]*nodeRequest, error) {
} }
} }
} }
// If the child references another node, resolve or schedule // If the child references another node, resolve or schedule.
// We check all children concurrently.
if node, ok := (child.node).(hashNode); ok { if node, ok := (child.node).(hashNode); ok {
// Check the presence of children concurrently path := child.path
hash := common.BytesToHash(node)
pending.Add(1) pending.Add(1)
go func(path []byte, hash common.Hash) { go func() {
defer pending.Done() defer pending.Done()
// Short circuit if the child node is already known.
owner, inner := ResolvePath(path) owner, inner := ResolvePath(path)
exist, mismatch := s.hasNode(owner, inner, hash) exist, inconsistent := s.hasNode(owner, inner, hash)
if exist { if exist {
return return
} } else if inconsistent {
if mismatch { // There is a pre-existing node with the wrong hash in DB, remove it.
batchMu.Lock() batchMu.Lock()
s.membatch.delNode(owner, inner) // remove the inconsistent node s.membatch.delNode(owner, inner)
batchMu.Unlock() batchMu.Unlock()
} }
// Locally unknown node, schedule for retrieval // Locally unknown node, schedule for retrieval
@ -618,7 +617,7 @@ func (s *Sync) children(req *nodeRequest, object node) ([]*nodeRequest, error) {
parent: req, parent: req,
callback: req.callback, callback: req.callback,
} }
}(child.path, common.BytesToHash(node)) }()
} }
} }
pending.Wait() pending.Wait()