sync.go 2.5 KB

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