diff options
author | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-05-15 19:12:46 +0800 |
---|---|---|
committer | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-05-15 19:12:46 +0800 |
commit | c1f0d40e34a80f4453a9a54f90e2d4551c3bdb05 (patch) | |
tree | 7ab7c369420a05ff51337a4aed7c5ec88490b90f /eth/downloader/downloader.go | |
parent | 158efbaa459c534bfb53624e1df7f65e7e8ca24a (diff) | |
parent | cfb2b51b749a5897eb47bee15e67a0f9d850bfbd (diff) | |
download | go-tangerine-c1f0d40e34a80f4453a9a54f90e2d4551c3bdb05.tar go-tangerine-c1f0d40e34a80f4453a9a54f90e2d4551c3bdb05.tar.gz go-tangerine-c1f0d40e34a80f4453a9a54f90e2d4551c3bdb05.tar.bz2 go-tangerine-c1f0d40e34a80f4453a9a54f90e2d4551c3bdb05.tar.lz go-tangerine-c1f0d40e34a80f4453a9a54f90e2d4551c3bdb05.tar.xz go-tangerine-c1f0d40e34a80f4453a9a54f90e2d4551c3bdb05.tar.zst go-tangerine-c1f0d40e34a80f4453a9a54f90e2d4551c3bdb05.zip |
Merge pull request #987 from obscuren/develop
Miner updates and downloader events
Diffstat (limited to 'eth/downloader/downloader.go')
-rw-r--r-- | eth/downloader/downloader.go | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index cc75c3014..616971f73 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -9,6 +9,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger/glog" ) @@ -55,6 +56,8 @@ type hashPack struct { } type Downloader struct { + mux *event.TypeMux + mu sync.RWMutex queue *queue peers *peerSet @@ -76,8 +79,9 @@ type Downloader struct { cancelLock sync.RWMutex // Lock to protect the cancel channel in delivers } -func New(hasBlock hashCheckFn, getBlock getBlockFn) *Downloader { +func New(mux *event.TypeMux, hasBlock hashCheckFn, getBlock getBlockFn) *Downloader { downloader := &Downloader{ + mux: mux, queue: newQueue(), peers: newPeerSet(), hasBlock: hasBlock, @@ -93,6 +97,11 @@ func (d *Downloader) Stats() (current int, max int) { return d.queue.Size() } +// Synchronising returns the state of the downloader +func (d *Downloader) Synchronising() bool { + return atomic.LoadInt32(&d.synchronising) > 0 +} + // RegisterPeer injects a new download peer into the set of block source to be // used for fetching hashes and blocks from. func (d *Downloader) RegisterPeer(id string, head common.Hash, getHashes hashFetcherFn, getBlocks blockFetcherFn) error { @@ -129,6 +138,9 @@ func (d *Downloader) Synchronise(id string, hash common.Hash) error { if atomic.CompareAndSwapInt32(&d.notified, 0, 1) { glog.V(logger.Info).Infoln("Block synchronisation started") } + + d.mux.Post(StartEvent{}) + // Create cancel channel for aborting mid-flight d.cancelLock.Lock() d.cancelCh = make(chan struct{}) @@ -166,6 +178,9 @@ func (d *Downloader) syncWithPeer(p *peer, hash common.Hash) (err error) { // reset on error if err != nil { d.queue.Reset() + d.mux.Post(FailedEvent{err}) + } else { + d.mux.Post(DoneEvent{}) } }() |