diff options
author | obscuren <geffobscura@gmail.com> | 2015-04-24 20:40:32 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2015-04-24 20:40:32 +0800 |
commit | b86e7526e12a5a49c1739ec02d3c1c5cc667dcb3 (patch) | |
tree | 5bbf718cd0bd4335b7b26e3bb320f1da7736c6da /eth/downloader/downloader.go | |
parent | 9caf880ff93794f80122ee7272c050bf571efa61 (diff) | |
download | dexon-b86e7526e12a5a49c1739ec02d3c1c5cc667dcb3.tar dexon-b86e7526e12a5a49c1739ec02d3c1c5cc667dcb3.tar.gz dexon-b86e7526e12a5a49c1739ec02d3c1c5cc667dcb3.tar.bz2 dexon-b86e7526e12a5a49c1739ec02d3c1c5cc667dcb3.tar.lz dexon-b86e7526e12a5a49c1739ec02d3c1c5cc667dcb3.tar.xz dexon-b86e7526e12a5a49c1739ec02d3c1c5cc667dcb3.tar.zst dexon-b86e7526e12a5a49c1739ec02d3c1c5cc667dcb3.zip |
eth, eth/downloader: moved peer selection to protocol handler
Diffstat (limited to 'eth/downloader/downloader.go')
-rw-r--r-- | eth/downloader/downloader.go | 95 |
1 files changed, 50 insertions, 45 deletions
diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index cfc494b2f..6ac8310b3 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -89,7 +89,7 @@ func New(hasBlock hashCheckFn, insertChain chainInsertFn, currentTd currentTdFn) blockCh: make(chan blockPack, 1), quit: make(chan struct{}), } - go downloader.peerHandler() + //go downloader.peerHandler() go downloader.update() return downloader @@ -110,7 +110,6 @@ func (d *Downloader) RegisterPeer(id string, td *big.Int, hash common.Hash, getH // add peer to our peer set d.peers[id] = peer // broadcast new peer - d.newPeerCh <- peer return nil } @@ -125,72 +124,78 @@ func (d *Downloader) UnregisterPeer(id string) { delete(d.peers, id) } -func (d *Downloader) peerHandler() { - // itimer is used to determine when to start ignoring `minDesiredPeerCount` - itimer := time.NewTimer(peerCountTimeout) +func (d *Downloader) update() { out: for { select { - case <-d.newPeerCh: - // Meet the `minDesiredPeerCount` before we select our best peer - if len(d.peers) < minDesiredPeerCount { + case sync := <-d.syncCh: + var peer *peer = sync.peer + err := d.getFromPeer(peer, sync.hash, sync.ignoreInitial) + if err != nil { + glog.V(logger.Detail).Infoln(err) break } - itimer.Stop() - - d.selectPeer(d.peers.bestPeer()) - case <-itimer.C: - // The timer will make sure that the downloader keeps an active state - // in which it attempts to always check the network for highest td peers - // Either select the peer or restart the timer if no peers could - // be selected. - if peer := d.peers.bestPeer(); peer != nil { - d.selectPeer(d.peers.bestPeer()) - } else { - itimer.Reset(5 * time.Second) - } + + d.process() case <-d.quit: break out } } } -func (d *Downloader) selectPeer(p *peer) { +// SynchroniseWithPeer will select the peer and use it for synchronising. If an empty string is given +// it will use the best peer possible and synchronise if it's TD is higher than our own. If any of the +// checks fail an error will be returned. This method is synchronous +func (d *Downloader) Synchronise(id string, hash common.Hash) (types.Blocks, error) { // Make sure it's doing neither. Once done we can restart the // downloading process if the TD is higher. For now just get on // with whatever is going on. This prevents unecessary switching. if d.isBusy() { - return + return nil, errBusy } - // selected peer must be better than our own - // XXX we also check the peer's recent hash to make sure we - // don't have it. Some peers report (i think) incorrect TD. - if p.td.Cmp(d.currentTd()) <= 0 || d.hasBlock(p.recentHash) { - return + + // Fetch the peer using the id or throw an error if the peer couldn't be found + p := d.peers[id] + if p == nil { + return nil, errUnknownPeer } - glog.V(logger.Detail).Infoln("New peer with highest TD =", p.td) - d.syncCh <- syncPack{p, p.recentHash, false} + // Get the hash from the peer and initiate the downloading progress. + err := d.getFromPeer(p, hash, false) + if err != nil { + return nil, err + } + return d.queue.blocks, nil } -func (d *Downloader) update() { -out: - for { - select { - case sync := <-d.syncCh: - var peer *peer = sync.peer - err := d.getFromPeer(peer, sync.hash, sync.ignoreInitial) - if err != nil { - glog.V(logger.Detail).Infoln(err) - break - } +func (d *Downloader) getFromPeer(p *peer, hash common.Hash, ignoreInitial bool) error { + d.activePeer = p.id + + glog.V(logger.Detail).Infoln("Synchronising with the network using:", p.id) + // Start the fetcher. This will block the update entirely + // interupts need to be send to the appropriate channels + // respectively. + if err := d.startFetchingHashes(p, hash, ignoreInitial); err != nil { + // handle error + glog.V(logger.Debug).Infoln("Error fetching hashes:", err) + // XXX Reset + return err + } - d.process() - case <-d.quit: - break out - } + // Start fetching blocks in paralel. The strategy is simple + // take any available peers, seserve a chunk for each peer available, + // let the peer deliver the chunkn and periodically check if a peer + // has timedout. When done downloading, process blocks. + if err := d.startFetchingBlocks(p); err != nil { + glog.V(logger.Debug).Infoln("Error downloading blocks:", err) + // XXX reset + return err } + + glog.V(logger.Detail).Infoln("Sync completed") + + return nil } // XXX Make synchronous |