syncerLoop is responsible for periodically synchronising with the network, both downloading hashes and blocks as well as handling the announcement handler.
()
| 139 | // syncerLoop is responsible for periodically synchronising with the network, both |
| 140 | // downloading hashes and blocks as well as handling the announcement handler. |
| 141 | func (pm *ProtocolManager) syncerLoop() { |
| 142 | // block until account is unlocked |
| 143 | <-StartSyncerLoop |
| 144 | |
| 145 | // Start and ensure cleanup of sync mechanisms |
| 146 | pm.fetcher.Start() |
| 147 | defer pm.fetcher.Stop() |
| 148 | // defer pm.downloader.Terminate() |
| 149 | defer pm.syncer.Terminate() |
| 150 | |
| 151 | // Wait for different events to fire synchronisation operations |
| 152 | forceSync := time.NewTicker(forceSyncCycle) |
| 153 | defer forceSync.Stop() |
| 154 | |
| 155 | for { |
| 156 | select { |
| 157 | case peer := <-pm.newPeerCh: |
| 158 | // Make sure we have peers to select from, then sync |
| 159 | if pm.peers.Len() < minDesiredPeerCount { |
| 160 | break |
| 161 | } |
| 162 | pm.syncer.AddPeer(peer) |
| 163 | // update from peers |
| 164 | go pm.synchronize(pm.peers.BestPeer()) |
| 165 | |
| 166 | case <-forceSync.C: |
| 167 | // Force a sync even if not enough peers are present |
| 168 | go pm.synchronize(pm.peers.BestPeer()) |
| 169 | |
| 170 | case <-pm.noMorePeers: |
| 171 | return |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | func (pm *ProtocolManager) SyncFromPeer(p *p2p.Peer) { |
| 177 | id := fmt.Sprintf("%x", p.ID().Bytes()[:8]) |