Skip to content

Commit f051580

Browse files
zsfelfoldikaralabe
authored andcommitted
les: fix fetcher syncing logic (#18072)
1 parent bb29d20 commit f051580

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

les/fetcher.go

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -141,36 +141,39 @@ func (f *lightFetcher) syncLoop() {
141141
s := requesting
142142
requesting = false
143143
var (
144-
rq *distReq
145-
reqID uint64
144+
rq *distReq
145+
reqID uint64
146+
syncing bool
146147
)
147148
if !f.syncing && !(newAnnounce && s) {
148-
rq, reqID = f.nextRequest()
149+
rq, reqID, syncing = f.nextRequest()
149150
}
150-
syncing := f.syncing
151151
f.lock.Unlock()
152152

153153
if rq != nil {
154154
requesting = true
155-
_, ok := <-f.pm.reqDist.queue(rq)
156-
if !ok {
155+
if _, ok := <-f.pm.reqDist.queue(rq); ok {
156+
if syncing {
157+
f.lock.Lock()
158+
f.syncing = true
159+
f.lock.Unlock()
160+
} else {
161+
go func() {
162+
time.Sleep(softRequestTimeout)
163+
f.reqMu.Lock()
164+
req, ok := f.requested[reqID]
165+
if ok {
166+
req.timeout = true
167+
f.requested[reqID] = req
168+
}
169+
f.reqMu.Unlock()
170+
// keep starting new requests while possible
171+
f.requestChn <- false
172+
}()
173+
}
174+
} else {
157175
f.requestChn <- false
158176
}
159-
160-
if !syncing {
161-
go func() {
162-
time.Sleep(softRequestTimeout)
163-
f.reqMu.Lock()
164-
req, ok := f.requested[reqID]
165-
if ok {
166-
req.timeout = true
167-
f.requested[reqID] = req
168-
}
169-
f.reqMu.Unlock()
170-
// keep starting new requests while possible
171-
f.requestChn <- false
172-
}()
173-
}
174177
}
175178
case reqID := <-f.timeoutChn:
176179
f.reqMu.Lock()
@@ -209,6 +212,7 @@ func (f *lightFetcher) syncLoop() {
209212
f.checkSyncedHeaders(p)
210213
f.syncing = false
211214
f.lock.Unlock()
215+
f.requestChn <- false
212216
}
213217
}
214218
}
@@ -405,7 +409,7 @@ func (f *lightFetcher) requestedID(reqID uint64) bool {
405409

406410
// nextRequest selects the peer and announced head to be requested next, amount
407411
// to be downloaded starting from the head backwards is also returned
408-
func (f *lightFetcher) nextRequest() (*distReq, uint64) {
412+
func (f *lightFetcher) nextRequest() (*distReq, uint64, bool) {
409413
var (
410414
bestHash common.Hash
411415
bestAmount uint64
@@ -427,14 +431,12 @@ func (f *lightFetcher) nextRequest() (*distReq, uint64) {
427431
}
428432
}
429433
if bestTd == f.maxConfirmedTd {
430-
return nil, 0
434+
return nil, 0, false
431435
}
432436

433-
f.syncing = bestSyncing
434-
435437
var rq *distReq
436438
reqID := genReqID()
437-
if f.syncing {
439+
if bestSyncing {
438440
rq = &distReq{
439441
getCost: func(dp distPeer) uint64 {
440442
return 0
@@ -500,7 +502,7 @@ func (f *lightFetcher) nextRequest() (*distReq, uint64) {
500502
},
501503
}
502504
}
503-
return rq, reqID
505+
return rq, reqID, bestSyncing
504506
}
505507

506508
// deliverHeaders delivers header download request responses for processing

0 commit comments

Comments
 (0)