@@ -65,12 +65,15 @@ type Downloader struct {
6565
6666 // Status
6767 synchronising int32
68+ notified int32
6869
6970 // Channels
7071 newPeerCh chan * peer
7172 hashCh chan hashPack
7273 blockCh chan blockPack
73- cancelCh chan struct {}
74+
75+ cancelCh chan struct {} // Channel to cancel mid-flight syncs
76+ cancelLock sync.RWMutex // Lock to protect the cancel channel in delivers
7477}
7578
7679func New (hasBlock hashCheckFn , getBlock getBlockFn ) * Downloader {
@@ -83,7 +86,6 @@ func New(hasBlock hashCheckFn, getBlock getBlockFn) *Downloader {
8386 hashCh : make (chan hashPack , 1 ),
8487 blockCh : make (chan blockPack , 1 ),
8588 }
86-
8789 return downloader
8890}
8991
@@ -123,8 +125,14 @@ func (d *Downloader) Synchronise(id string, hash common.Hash) error {
123125 }
124126 defer atomic .StoreInt32 (& d .synchronising , 0 )
125127
126- // Create cancel channel for aborting midflight
128+ // Post a user notification of the sync (only once per session)
129+ if atomic .CompareAndSwapInt32 (& d .notified , 0 , 1 ) {
130+ glog .V (logger .Info ).Infoln ("Block synchronisation started" )
131+ }
132+ // Create cancel channel for aborting mid-flight
133+ d .cancelLock .Lock ()
127134 d .cancelCh = make (chan struct {})
135+ d .cancelLock .Unlock ()
128136
129137 // Abort if the queue still contains some leftover data
130138 if _ , cached := d .queue .Size (); cached > 0 && d .queue .GetHeadBlock () != nil {
@@ -183,32 +191,15 @@ func (d *Downloader) syncWithPeer(p *peer, hash common.Hash) (err error) {
183191// Cancel cancels all of the operations and resets the queue. It returns true
184192// if the cancel operation was completed.
185193func (d * Downloader ) Cancel () bool {
186- hs , bs := d .queue .Size ()
187194 // If we're not syncing just return.
195+ hs , bs := d .queue .Size ()
188196 if atomic .LoadInt32 (& d .synchronising ) == 0 && hs == 0 && bs == 0 {
189197 return false
190198 }
191-
199+ // Close the current cancel channel
200+ d .cancelLock .RLock ()
192201 close (d .cancelCh )
193-
194- // clean up
195- hashDone:
196- for {
197- select {
198- case <- d .hashCh :
199- default :
200- break hashDone
201- }
202- }
203-
204- blockDone:
205- for {
206- select {
207- case <- d .blockCh :
208- default :
209- break blockDone
210- }
211- }
202+ d .cancelLock .RUnlock ()
212203
213204 // reset the queue
214205 d .queue .Reset ()
@@ -421,9 +412,18 @@ func (d *Downloader) DeliverBlocks(id string, blocks []*types.Block) error {
421412 if atomic .LoadInt32 (& d .synchronising ) == 0 {
422413 return errNoSyncActive
423414 }
424- d .blockCh <- blockPack {id , blocks }
415+ // Deliver or abort if the sync is canceled while queuing
416+ d .cancelLock .RLock ()
417+ cancel := d .cancelCh
418+ d .cancelLock .RUnlock ()
425419
426- return nil
420+ select {
421+ case d .blockCh <- blockPack {id , blocks }:
422+ return nil
423+
424+ case <- cancel :
425+ return errNoSyncActive
426+ }
427427}
428428
429429// DeliverHashes injects a new batch of hashes received from a remote node into
@@ -434,11 +434,16 @@ func (d *Downloader) DeliverHashes(id string, hashes []common.Hash) error {
434434 if atomic .LoadInt32 (& d .synchronising ) == 0 {
435435 return errNoSyncActive
436436 }
437- if glog .V (logger .Debug ) && len (hashes ) != 0 {
438- from , to := hashes [0 ], hashes [len (hashes )- 1 ]
439- glog .V (logger .Debug ).Infof ("adding %d (T=%d) hashes [ %x / %x ] from: %s\n " , len (hashes ), d .queue .Pending (), from [:4 ], to [:4 ], id )
440- }
441- d .hashCh <- hashPack {id , hashes }
437+ // Deliver or abort if the sync is canceled while queuing
438+ d .cancelLock .RLock ()
439+ cancel := d .cancelCh
440+ d .cancelLock .RUnlock ()
442441
443- return nil
442+ select {
443+ case d .hashCh <- hashPack {id , hashes }:
444+ return nil
445+
446+ case <- cancel :
447+ return errNoSyncActive
448+ }
444449}
0 commit comments