github.com/gochain-io/gochain@v2.2.26+incompatible/les/sync.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package les 18 19 import ( 20 "context" 21 "time" 22 23 "github.com/gochain-io/gochain/core/rawdb" 24 "github.com/gochain-io/gochain/eth/downloader" 25 "github.com/gochain-io/gochain/light" 26 "github.com/gochain-io/gochain/log" 27 ) 28 29 const ( 30 //forceSyncCycle = 10 * time.Second // Time interval to force syncs, even if few peers are available 31 minDesiredPeerCount = 5 // Amount of peers desired to start syncing 32 ) 33 34 // syncer is responsible for periodically synchronising with the network, both 35 // downloading hashes and blocks as well as handling the announcement handler. 36 func (pm *ProtocolManager) syncer() { 37 // Start and ensure cleanup of sync mechanisms 38 //pm.fetcher.Start() 39 //defer pm.fetcher.Stop() 40 defer pm.downloader.Terminate() 41 42 // Wait for different events to fire synchronisation operations 43 //forceSync := time.Tick(forceSyncCycle) 44 for { 45 select { 46 case <-pm.newPeerCh: 47 /* // Make sure we have peers to select from, then sync 48 if pm.peers.Len() < minDesiredPeerCount { 49 break 50 } 51 go pm.synchronise(pm.peers.BestPeer()) 52 */ 53 /*case <-forceSync: 54 // Force a sync even if not enough peers are present 55 go pm.synchronise(pm.peers.BestPeer()) 56 */ 57 case <-pm.noMorePeers: 58 return 59 } 60 } 61 } 62 63 func (pm *ProtocolManager) needToSync(peerHead blockInfo) bool { 64 head := pm.blockchain.CurrentHeader() 65 currentTd := rawdb.ReadTd(pm.chainDb.GlobalTable(), head.Hash(), head.Number.Uint64()) 66 return currentTd != nil && peerHead.Td.Cmp(currentTd) > 0 67 } 68 69 // synchronise tries to sync up our local block chain with a remote peer. 70 func (pm *ProtocolManager) synchronise(peer *peer) { 71 // Short circuit if no peers are available 72 if peer == nil { 73 return 74 } 75 76 // Make sure the peer's TD is higher than our own. 77 if !pm.needToSync(peer.headBlockInfo()) { 78 return 79 } 80 81 ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) 82 defer cancel() 83 pm.blockchain.(*light.LightChain).SyncCht(ctx) 84 if err := pm.downloader.Synchronise(ctx, peer.id, peer.Head(), peer.Td(), downloader.LightSync); err != nil { 85 log.Error("Cannot synchronise downloader", "id", peer.id, "head", peer.Head(), "err", err) 86 } 87 }