|
|
@ -77,11 +77,11 @@ type ProtocolManager struct { |
|
|
|
newPeerCh chan *peer |
|
|
|
newPeerCh chan *peer |
|
|
|
txsyncCh chan *txsync |
|
|
|
txsyncCh chan *txsync |
|
|
|
quitSync chan struct{} |
|
|
|
quitSync chan struct{} |
|
|
|
|
|
|
|
noMorePeers chan struct{} |
|
|
|
|
|
|
|
|
|
|
|
// wait group is used for graceful shutdowns during downloading
|
|
|
|
// wait group is used for graceful shutdowns during downloading
|
|
|
|
// and processing
|
|
|
|
// and processing
|
|
|
|
wg sync.WaitGroup |
|
|
|
wg sync.WaitGroup |
|
|
|
quit bool |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// NewProtocolManager returns a new ethereum sub protocol manager. The Ethereum sub protocol manages peers capable
|
|
|
|
// NewProtocolManager returns a new ethereum sub protocol manager. The Ethereum sub protocol manages peers capable
|
|
|
@ -101,7 +101,8 @@ func NewProtocolManager(config *core.ChainConfig, fastSync bool, networkId int, |
|
|
|
blockchain: blockchain, |
|
|
|
blockchain: blockchain, |
|
|
|
chaindb: chaindb, |
|
|
|
chaindb: chaindb, |
|
|
|
peers: newPeerSet(), |
|
|
|
peers: newPeerSet(), |
|
|
|
newPeerCh: make(chan *peer, 1), |
|
|
|
newPeerCh: make(chan *peer), |
|
|
|
|
|
|
|
noMorePeers: make(chan struct{}), |
|
|
|
txsyncCh: make(chan *txsync), |
|
|
|
txsyncCh: make(chan *txsync), |
|
|
|
quitSync: make(chan struct{}), |
|
|
|
quitSync: make(chan struct{}), |
|
|
|
} |
|
|
|
} |
|
|
@ -120,8 +121,14 @@ func NewProtocolManager(config *core.ChainConfig, fastSync bool, networkId int, |
|
|
|
Length: ProtocolLengths[i], |
|
|
|
Length: ProtocolLengths[i], |
|
|
|
Run: func(p *p2p.Peer, rw p2p.MsgReadWriter) error { |
|
|
|
Run: func(p *p2p.Peer, rw p2p.MsgReadWriter) error { |
|
|
|
peer := manager.newPeer(int(version), p, rw) |
|
|
|
peer := manager.newPeer(int(version), p, rw) |
|
|
|
manager.newPeerCh <- peer |
|
|
|
select { |
|
|
|
|
|
|
|
case manager.newPeerCh <- peer: |
|
|
|
|
|
|
|
manager.wg.Add(1) |
|
|
|
|
|
|
|
defer manager.wg.Done() |
|
|
|
return manager.handle(peer) |
|
|
|
return manager.handle(peer) |
|
|
|
|
|
|
|
case <-manager.quitSync: |
|
|
|
|
|
|
|
return p2p.DiscQuitting |
|
|
|
|
|
|
|
} |
|
|
|
}, |
|
|
|
}, |
|
|
|
NodeInfo: func() interface{} { |
|
|
|
NodeInfo: func() interface{} { |
|
|
|
return manager.NodeInfo() |
|
|
|
return manager.NodeInfo() |
|
|
@ -187,16 +194,25 @@ func (pm *ProtocolManager) Start() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (pm *ProtocolManager) Stop() { |
|
|
|
func (pm *ProtocolManager) Stop() { |
|
|
|
// Showing a log message. During download / process this could actually
|
|
|
|
|
|
|
|
// take between 5 to 10 seconds and therefor feedback is required.
|
|
|
|
|
|
|
|
glog.V(logger.Info).Infoln("Stopping ethereum protocol handler...") |
|
|
|
glog.V(logger.Info).Infoln("Stopping ethereum protocol handler...") |
|
|
|
|
|
|
|
|
|
|
|
pm.quit = true |
|
|
|
|
|
|
|
pm.txSub.Unsubscribe() // quits txBroadcastLoop
|
|
|
|
pm.txSub.Unsubscribe() // quits txBroadcastLoop
|
|
|
|
pm.minedBlockSub.Unsubscribe() // quits blockBroadcastLoop
|
|
|
|
pm.minedBlockSub.Unsubscribe() // quits blockBroadcastLoop
|
|
|
|
close(pm.quitSync) // quits syncer, fetcher, txsyncLoop
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Wait for any process action
|
|
|
|
// Quit the sync loop.
|
|
|
|
|
|
|
|
// After this send has completed, no new peers will be accepted.
|
|
|
|
|
|
|
|
pm.noMorePeers <- struct{}{} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Quit fetcher, txsyncLoop.
|
|
|
|
|
|
|
|
close(pm.quitSync) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Disconnect existing sessions.
|
|
|
|
|
|
|
|
// This also closes the gate for any new registrations on the peer set.
|
|
|
|
|
|
|
|
// sessions which are already established but not added to pm.peers yet
|
|
|
|
|
|
|
|
// will exit when they try to register.
|
|
|
|
|
|
|
|
pm.peers.Close() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Wait for all peer handler goroutines and the loops to come down.
|
|
|
|
pm.wg.Wait() |
|
|
|
pm.wg.Wait() |
|
|
|
|
|
|
|
|
|
|
|
glog.V(logger.Info).Infoln("Ethereum protocol handler stopped") |
|
|
|
glog.V(logger.Info).Infoln("Ethereum protocol handler stopped") |
|
|
|