github.com/murrekatt/go-ethereum@v1.5.8-0.20170123175102-fc52f2c007fb/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  	"time"
    21  
    22  	"github.com/ethereum/go-ethereum/core"
    23  	"github.com/ethereum/go-ethereum/eth/downloader"
    24  	"github.com/ethereum/go-ethereum/light"
    25  	"golang.org/x/net/context"
    26  )
    27  
    28  const (
    29  	//forceSyncCycle      = 10 * time.Second // Time interval to force syncs, even if few peers are available
    30  	minDesiredPeerCount = 5 // Amount of peers desired to start syncing
    31  )
    32  
    33  // syncer is responsible for periodically synchronising with the network, both
    34  // downloading hashes and blocks as well as handling the announcement handler.
    35  func (pm *ProtocolManager) syncer() {
    36  	// Start and ensure cleanup of sync mechanisms
    37  	//pm.fetcher.Start()
    38  	//defer pm.fetcher.Stop()
    39  	defer pm.downloader.Terminate()
    40  
    41  	// Wait for different events to fire synchronisation operations
    42  	//forceSync := time.Tick(forceSyncCycle)
    43  	for {
    44  		select {
    45  		case <-pm.newPeerCh:
    46  			/*			// Make sure we have peers to select from, then sync
    47  						if pm.peers.Len() < minDesiredPeerCount {
    48  							break
    49  						}
    50  						go pm.synchronise(pm.peers.BestPeer())
    51  			*/
    52  		/*case <-forceSync:
    53  		// Force a sync even if not enough peers are present
    54  		go pm.synchronise(pm.peers.BestPeer())
    55  		*/
    56  		case <-pm.noMorePeers:
    57  			return
    58  		}
    59  	}
    60  }
    61  
    62  func (pm *ProtocolManager) needToSync(peerHead blockInfo) bool {
    63  	head := pm.blockchain.CurrentHeader()
    64  	currentTd := core.GetTd(pm.chainDb, head.Hash(), head.Number.Uint64())
    65  	return currentTd != nil && peerHead.Td.Cmp(currentTd) > 0
    66  }
    67  
    68  // synchronise tries to sync up our local block chain with a remote peer.
    69  func (pm *ProtocolManager) synchronise(peer *peer) {
    70  	// Short circuit if no peers are available
    71  	if peer == nil {
    72  		return
    73  	}
    74  
    75  	// Make sure the peer's TD is higher than our own.
    76  	if !pm.needToSync(peer.headBlockInfo()) {
    77  		return
    78  	}
    79  
    80  	ctx, _ := context.WithTimeout(context.Background(), time.Second*5)
    81  	pm.blockchain.(*light.LightChain).SyncCht(ctx)
    82  
    83  	pm.downloader.Synchronise(peer.id, peer.Head(), peer.Td(), downloader.LightSync)
    84  }