Synchronise tries to sync up our local block chain with a remote peer. It fetches blocks a peer.
(peer *peer)
| 186 | |
| 187 | // Synchronise tries to sync up our local block chain with a remote peer. It fetches blocks a peer. |
| 188 | func (pm *ProtocolManager) synchronize(peer *peer) { |
| 189 | if peer == nil { |
| 190 | return |
| 191 | } |
| 192 | |
| 193 | // make sure the peer has more blocks |
| 194 | currentBlock := pm.blockchain.CurrentBlock() |
| 195 | height := currentBlock.Number() |
| 196 | pHead, pHt := peer.Head() |
| 197 | if pHt.Cmp(height) <= 0 { |
| 198 | return |
| 199 | } |
| 200 | |
| 201 | // full sync with the downloader |
| 202 | if err := pm.syncer.Synchronise(peer, pHead, pHt, pm.syncMode); err != nil { |
| 203 | return |
| 204 | } |
| 205 | |
| 206 | // because we have done the synchronization |
| 207 | atomic.StoreUint32(&pm.acceptTxs, 1) // Mark initial sync done |
| 208 | |
| 209 | if head := pm.blockchain.CurrentBlock(); head.NumberU64() > 0 { |
| 210 | // We've completed a sync cycle, notify all peers of new state. This path is |
| 211 | // essential in star-topology networks where a gateway node needs to notify |
| 212 | // all its out-of-date peers of the availability of a new block. This failure |
| 213 | // scenario will most often crop up in private and hackathon networks with |
| 214 | // degenerate connectivity, but it should be healthy for the mainnet too to |
| 215 | // more reliably update peers or the local TD state. |
| 216 | go pm.BroadcastBlock(head, false) |
| 217 | go pm.BroadcastBlock(head, true) |
| 218 | } |
| 219 | } |
no test coverage detected