diff options
author | obscuren <geffobscura@gmail.com> | 2015-05-15 06:43:00 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2015-05-15 06:43:10 +0800 |
commit | b71091e337fef7e3cfad56c61c97a42094e87531 (patch) | |
tree | e3945e86e63714aeb77569cca28329a73de608d7 /eth/downloader/downloader.go | |
parent | 0f76a1c6df44bc3242a64e76bef66dfe312d259b (diff) | |
download | go-tangerine-b71091e337fef7e3cfad56c61c97a42094e87531.tar.gz go-tangerine-b71091e337fef7e3cfad56c61c97a42094e87531.tar.zst go-tangerine-b71091e337fef7e3cfad56c61c97a42094e87531.zip |
eth, eth/downloader, miner: use download events to check miner start
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{}) } }() |