downloader: all handlers check for isBusy

This commit is contained in:
obscuren 2015-04-18 03:15:26 +02:00
parent a6c0a75f9a
commit 8f873b762b

@ -116,7 +116,7 @@ func (d *Downloader) UnregisterPeer(id string) {
// checks fail an error will be returned. This method is synchronous // checks fail an error will be returned. This method is synchronous
func (d *Downloader) SynchroniseWithPeer(id string) (types.Blocks, error) { func (d *Downloader) SynchroniseWithPeer(id string) (types.Blocks, error) {
// Check if we're busy // Check if we're busy
if d.isFetchingHashes() || d.isDownloadingBlocks() || d.isProcessing() { if d.isBusy() {
return nil, errBusy return nil, errBusy
} }
@ -213,7 +213,7 @@ func (d *Downloader) selectPeer(p *peer) {
// Make sure it's doing neither. Once done we can restart the // Make sure it's doing neither. Once done we can restart the
// downloading process if the TD is higher. For now just get on // downloading process if the TD is higher. For now just get on
// with whatever is going on. This prevents unecessary switching. // with whatever is going on. This prevents unecessary switching.
if !(d.isFetchingHashes() || d.isDownloadingBlocks() || d.isProcessing()) { if !d.isBusy() {
// selected peer must be better than our own // selected peer must be better than our own
// XXX we also check the peer's recent hash to make sure we // XXX we also check the peer's recent hash to make sure we
// don't have it. Some peers report (i think) incorrect TD. // don't have it. Some peers report (i think) incorrect TD.
@ -340,10 +340,6 @@ out:
// from the available peers. // from the available peers.
if d.queue.hashPool.Size() > 0 { if d.queue.hashPool.Size() > 0 {
availablePeers := d.peers.get(idleState) availablePeers := d.peers.get(idleState)
if len(availablePeers) == 0 {
glog.V(logger.Detail).Infoln("No peers available out of", len(d.peers))
}
for _, peer := range availablePeers { for _, peer := range availablePeers {
// Get a possible chunk. If nil is returned no chunk // Get a possible chunk. If nil is returned no chunk
// could be returned due to no hashes available. // could be returned due to no hashes available.
@ -440,7 +436,7 @@ func (d *Downloader) AddBlock(id string, block *types.Block, td *big.Int) {
d.queue.addBlock(id, block, td) d.queue.addBlock(id, block, td)
// if neither go ahead to process // if neither go ahead to process
if !(d.isFetchingHashes() || d.isDownloadingBlocks()) { if !d.isBusy() {
// Check if the parent of the received block is known. // Check if the parent of the received block is known.
// If the block is not know, request it otherwise, request. // If the block is not know, request it otherwise, request.
phash := block.ParentHash() phash := block.ParentHash()
@ -519,3 +515,7 @@ func (d *Downloader) isDownloadingBlocks() bool {
func (d *Downloader) isProcessing() bool { func (d *Downloader) isProcessing() bool {
return atomic.LoadInt32(&d.processingBlocks) == 1 return atomic.LoadInt32(&d.processingBlocks) == 1
} }
func (d *Downloader) isBusy() bool {
return d.isFetchingHashes() || d.isDownloadingBlocks() || d.isProcessing()
}