This commit is contained in:
Jared Wasinger 2024-02-22 20:31:31 -08:00
parent c7cd97fe1c
commit dc16ec33ee

View file

@ -248,38 +248,35 @@ func (sf *subfetcher) loop() {
sf.trie = trie sf.trie = trie
} }
// Trie opened successfully, keep prefetching items // Trie opened successfully, keep prefetching items
for { for keepRunning := range sf.wake {
select { // Subfetcher was woken up, retrieve any tasks to avoid spinning the lock
case keepRunning := <-sf.wake: sf.lock.Lock()
// Subfetcher was woken up, retrieve any tasks to avoid spinning the lock tasks := sf.tasks
sf.lock.Lock() sf.tasks = nil
tasks := sf.tasks sf.lock.Unlock()
sf.tasks = nil
sf.lock.Unlock()
// Prefetch all tasks // Prefetch all tasks
for _, task := range tasks { for _, task := range tasks {
if _, ok := sf.seen[string(task)]; ok { if _, ok := sf.seen[string(task)]; ok {
sf.dups++ sf.dups++
continue continue
}
if len(task) == common.AddressLength {
sf.trie.GetAccount(common.BytesToAddress(task))
} else {
sf.trie.GetStorage(sf.addr, task)
}
sf.seen[string(task)] = struct{}{}
} }
// if any trie retrieval request is made, ensure it is completed if len(task) == common.AddressLength {
// after pending tasks have been processed. sf.trie.GetAccount(common.BytesToAddress(task))
select { } else {
case ch := <-sf.copy: sf.trie.GetStorage(sf.addr, task)
ch <- sf.db.CopyTrie(sf.trie)
default:
}
if !keepRunning {
return
} }
sf.seen[string(task)] = struct{}{}
}
// if any trie retrieval request is made, ensure it is completed
// after pending tasks have been processed.
select {
case ch := <-sf.copy:
ch <- sf.db.CopyTrie(sf.trie)
default:
}
if !keepRunning {
return
} }
} }
} }