@@ -154,7 +154,7 @@ type Downloader struct {
154
154
blockCh chan blockPack // [eth/61] Channel receiving inbound blocks
155
155
headerCh chan headerPack // [eth/62] Channel receiving inbound block headers
156
156
bodyCh chan bodyPack // [eth/62] Channel receiving inbound block bodies
157
- processCh chan bool // Channel to signal the block fetcher of new or finished work
157
+ wakeCh chan bool // Channel to signal the block/body fetcher of new tasks
158
158
159
159
cancelCh chan struct {} // Channel to cancel mid-flight syncs
160
160
cancelLock sync.RWMutex // Lock to protect the cancel channel in delivers
@@ -188,7 +188,7 @@ func New(mux *event.TypeMux, hasBlock hashCheckFn, getBlock blockRetrievalFn, he
188
188
blockCh : make (chan blockPack , 1 ),
189
189
headerCh : make (chan headerPack , 1 ),
190
190
bodyCh : make (chan bodyPack , 1 ),
191
- processCh : make (chan bool , 1 ),
191
+ wakeCh : make (chan bool , 1 ),
192
192
}
193
193
}
194
194
@@ -282,6 +282,10 @@ func (d *Downloader) synchronise(id string, hash common.Hash, td *big.Int) error
282
282
d .queue .Reset ()
283
283
d .peers .Reset ()
284
284
285
+ select {
286
+ case <- d .wakeCh :
287
+ default :
288
+ }
285
289
// Create cancel channel for aborting mid-flight
286
290
d .cancelLock .Lock ()
287
291
d .cancelCh = make (chan struct {})
@@ -633,7 +637,7 @@ func (d *Downloader) fetchHashes61(p *peer, td *big.Int, from uint64) error {
633
637
glog .V (logger .Debug ).Infof ("%v: no available hashes" , p )
634
638
635
639
select {
636
- case d .processCh <- false :
640
+ case d .wakeCh <- false :
637
641
case <- d .cancelCh :
638
642
}
639
643
// If no hashes were retrieved at all, the peer violated it's TD promise that it had a
@@ -664,12 +668,18 @@ func (d *Downloader) fetchHashes61(p *peer, td *big.Int, from uint64) error {
664
668
return errBadPeer
665
669
}
666
670
// Notify the block fetcher of new hashes, but stop if queue is full
667
- cont := d .queue .Pending () < maxQueuedHashes
668
- select {
669
- case d .processCh <- cont :
670
- default :
671
- }
672
- if ! cont {
671
+ if d .queue .Pending () < maxQueuedHashes {
672
+ // We still have hashes to fetch, send continuation wake signal (potential)
673
+ select {
674
+ case d .wakeCh <- true :
675
+ default :
676
+ }
677
+ } else {
678
+ // Hash limit reached, send a termination wake signal (enforced)
679
+ select {
680
+ case d .wakeCh <- false :
681
+ case <- d .cancelCh :
682
+ }
673
683
return nil
674
684
}
675
685
// Queue not yet full, fetch the next batch
@@ -766,7 +776,7 @@ func (d *Downloader) fetchBlocks61(from uint64) error {
766
776
default :
767
777
}
768
778
769
- case cont := <- d .processCh :
779
+ case cont := <- d .wakeCh :
770
780
// The hash fetcher sent a continuation flag, check if it's done
771
781
if ! cont {
772
782
finished = true
@@ -1053,7 +1063,7 @@ func (d *Downloader) fetchHeaders(p *peer, td *big.Int, from uint64) error {
1053
1063
glog .V (logger .Debug ).Infof ("%v: no available headers" , p )
1054
1064
1055
1065
select {
1056
- case d .processCh <- false :
1066
+ case d .wakeCh <- false :
1057
1067
case <- d .cancelCh :
1058
1068
}
1059
1069
// If no headers were retrieved at all, the peer violated it's TD promise that it had a
@@ -1084,12 +1094,18 @@ func (d *Downloader) fetchHeaders(p *peer, td *big.Int, from uint64) error {
1084
1094
return errBadPeer
1085
1095
}
1086
1096
// Notify the block fetcher of new headers, but stop if queue is full
1087
- cont := d .queue .Pending () < maxQueuedHeaders
1088
- select {
1089
- case d .processCh <- cont :
1090
- default :
1091
- }
1092
- if ! cont {
1097
+ if d .queue .Pending () < maxQueuedHeaders {
1098
+ // We still have headers to fetch, send continuation wake signal (potential)
1099
+ select {
1100
+ case d .wakeCh <- true :
1101
+ default :
1102
+ }
1103
+ } else {
1104
+ // Header limit reached, send a termination wake signal (enforced)
1105
+ select {
1106
+ case d .wakeCh <- false :
1107
+ case <- d .cancelCh :
1108
+ }
1093
1109
return nil
1094
1110
}
1095
1111
// Queue not yet full, fetch the next batch
@@ -1104,8 +1120,8 @@ func (d *Downloader) fetchHeaders(p *peer, td *big.Int, from uint64) error {
1104
1120
1105
1121
// Finish the sync gracefully instead of dumping the gathered data though
1106
1122
select {
1107
- case d .processCh <- false :
1108
- default :
1123
+ case d .wakeCh <- false :
1124
+ case <- d . cancelCh :
1109
1125
}
1110
1126
return nil
1111
1127
}
@@ -1199,7 +1215,7 @@ func (d *Downloader) fetchBodies(from uint64) error {
1199
1215
default :
1200
1216
}
1201
1217
1202
- case cont := <- d .processCh :
1218
+ case cont := <- d .wakeCh :
1203
1219
// The header fetcher sent a continuation flag, check if it's done
1204
1220
if ! cont {
1205
1221
finished = true
0 commit comments