les: simplify requesting/syncing logic

This commit is contained in:
Zsolt Felfoldi 2019-05-17 11:36:08 +02:00
parent 70ff967f7a
commit e40fe2117b

View file

@ -55,7 +55,7 @@ type lightFetcher struct {
requested map[uint64]fetchRequest requested map[uint64]fetchRequest
deliverChn chan fetchResponse deliverChn chan fetchResponse
timeoutChn chan uint64 timeoutChn chan uint64
requesting bool requestTriggered bool
requestTrigger chan struct{} requestTrigger chan struct{}
lastTrustedHeader *types.Header lastTrustedHeader *types.Header
} }
@ -152,14 +152,13 @@ func (f *lightFetcher) syncLoop() {
if !f.syncing { if !f.syncing {
rq, reqID, syncing = f.nextRequest() rq, reqID, syncing = f.nextRequest()
} }
f.requesting = rq != nil f.requestTriggered = rq != nil
f.lock.Unlock() f.lock.Unlock()
if rq != nil { if rq != nil {
if _, ok := <-f.pm.reqDist.queue(rq); ok { if _, ok := <-f.pm.reqDist.queue(rq); ok {
if syncing { if syncing {
f.lock.Lock() f.lock.Lock()
f.requesting = false
f.syncing = true f.syncing = true
f.lock.Unlock() f.lock.Unlock()
} else { } else {
@ -216,12 +215,8 @@ func (f *lightFetcher) syncLoop() {
p.Log().Debug("Done synchronising with peer") p.Log().Debug("Done synchronising with peer")
f.checkSyncedHeaders(p) f.checkSyncedHeaders(p)
f.syncing = false f.syncing = false
r := f.requesting
f.requesting = true
f.lock.Unlock() f.lock.Unlock()
if !r { f.requestTrigger <- struct{}{} // f.requestTriggered is always true here
f.requestTrigger <- struct{}{}
}
} }
} }
} }
@ -355,8 +350,8 @@ func (f *lightFetcher) announce(p *peer, head *announceData) {
fp.lastAnnounced = n fp.lastAnnounced = n
p.lock.Unlock() p.lock.Unlock()
f.checkUpdateStats(p, nil) f.checkUpdateStats(p, nil)
if !f.requesting { if !f.requestTriggered {
f.requesting = true f.requestTriggered = true
f.requestTrigger <- struct{}{} f.requestTrigger <- struct{}{}
} }
} }