2015-05-01 01:23:51 +03:00
|
|
|
package eth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
2015-05-14 15:24:18 +03:00
|
|
|
"sync/atomic"
|
2015-05-01 01:23:51 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/eth/downloader"
|
|
|
|
"github.com/ethereum/go-ethereum/logger"
|
|
|
|
"github.com/ethereum/go-ethereum/logger/glog"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Sync contains all synchronisation code for the eth protocol
|
|
|
|
|
|
|
|
func (pm *ProtocolManager) update() {
|
2015-05-08 15:22:48 +03:00
|
|
|
forceSync := time.Tick(forceSyncCycle)
|
|
|
|
blockProc := time.Tick(blockProcCycle)
|
2015-05-14 15:24:18 +03:00
|
|
|
blockProcPend := int32(0)
|
2015-05-01 17:30:02 +03:00
|
|
|
|
2015-05-01 01:23:51 +03:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-pm.newPeerCh:
|
|
|
|
// Meet the `minDesiredPeerCount` before we select our best peer
|
|
|
|
if len(pm.peers) < minDesiredPeerCount {
|
|
|
|
break
|
|
|
|
}
|
2015-05-08 15:22:48 +03:00
|
|
|
// Find the best peer and synchronise with it
|
2015-05-01 01:23:51 +03:00
|
|
|
peer := getBestPeer(pm.peers)
|
|
|
|
if peer == nil {
|
2015-05-08 15:22:48 +03:00
|
|
|
glog.V(logger.Debug).Infoln("Sync attempt canceled. No peers available")
|
2015-05-01 01:23:51 +03:00
|
|
|
}
|
2015-05-08 15:22:48 +03:00
|
|
|
go pm.synchronise(peer)
|
2015-05-01 01:23:51 +03:00
|
|
|
|
2015-05-08 15:22:48 +03:00
|
|
|
case <-forceSync:
|
|
|
|
// Force a sync even if not enough peers are present
|
2015-05-01 01:23:51 +03:00
|
|
|
if peer := getBestPeer(pm.peers); peer != nil {
|
2015-05-08 15:22:48 +03:00
|
|
|
go pm.synchronise(peer)
|
2015-05-01 01:23:51 +03:00
|
|
|
}
|
2015-05-08 15:22:48 +03:00
|
|
|
case <-blockProc:
|
|
|
|
// Try to pull some blocks from the downloaded
|
2015-05-14 15:24:18 +03:00
|
|
|
if atomic.CompareAndSwapInt32(&blockProcPend, 0, 1) {
|
|
|
|
go func() {
|
2015-05-14 15:38:49 +03:00
|
|
|
pm.processBlocks()
|
2015-05-14 15:24:18 +03:00
|
|
|
atomic.StoreInt32(&blockProcPend, 0)
|
|
|
|
}()
|
|
|
|
}
|
2015-05-08 15:22:48 +03:00
|
|
|
|
2015-05-01 01:23:51 +03:00
|
|
|
case <-pm.quitSync:
|
2015-05-01 17:30:02 +03:00
|
|
|
return
|
2015-05-01 01:23:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// processBlocks will attempt to reconstruct a chain by checking the first item and check if it's
|
|
|
|
// a known parent. The first block in the chain may be unknown during downloading. When the
|
|
|
|
// downloader isn't downloading blocks will be dropped with an unknown parent until either it
|
|
|
|
// has depleted the list or found a known parent.
|
|
|
|
func (pm *ProtocolManager) processBlocks() error {
|
|
|
|
pm.wg.Add(1)
|
|
|
|
defer pm.wg.Done()
|
|
|
|
|
2015-05-14 15:38:49 +03:00
|
|
|
// Short circuit if no blocks are available for insertion
|
|
|
|
blocks := pm.downloader.TakeBlocks()
|
2015-05-01 01:23:51 +03:00
|
|
|
if len(blocks) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
glog.V(logger.Debug).Infof("Inserting chain with %d blocks (#%v - #%v)\n", len(blocks), blocks[0].Number(), blocks[len(blocks)-1].Number())
|
|
|
|
|
|
|
|
for len(blocks) != 0 && !pm.quit {
|
|
|
|
max := int(math.Min(float64(len(blocks)), float64(blockProcAmount)))
|
|
|
|
_, err := pm.chainman.InsertChain(blocks[:max])
|
|
|
|
if err != nil {
|
2015-05-14 15:24:18 +03:00
|
|
|
glog.V(logger.Warn).Infof("Block insertion failed: %v", err)
|
2015-05-14 15:38:49 +03:00
|
|
|
pm.downloader.Cancel()
|
2015-05-01 01:23:51 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
blocks = blocks[max:]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-08 15:22:48 +03:00
|
|
|
func (pm *ProtocolManager) synchronise(peer *peer) {
|
2015-05-01 01:23:51 +03:00
|
|
|
// Make sure the peer's TD is higher than our own. If not drop.
|
|
|
|
if peer.td.Cmp(pm.chainman.Td()) <= 0 {
|
|
|
|
return
|
|
|
|
}
|
2015-05-03 15:11:47 +03:00
|
|
|
// FIXME if we have the hash in our chain and the TD of the peer is
|
|
|
|
// much higher than ours, something is wrong with us or the peer.
|
|
|
|
// Check if the hash is on our own chain
|
|
|
|
if pm.chainman.HasBlock(peer.recentHash) {
|
|
|
|
return
|
|
|
|
}
|
2015-05-01 01:23:51 +03:00
|
|
|
// Get the hashes from the peer (synchronously)
|
2015-05-08 15:22:48 +03:00
|
|
|
glog.V(logger.Debug).Infof("Attempting synchronisation: %v, 0x%x", peer.id, peer.recentHash)
|
|
|
|
|
|
|
|
err := pm.downloader.Synchronise(peer.id, peer.recentHash)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
glog.V(logger.Debug).Infof("Synchronisation completed")
|
|
|
|
|
|
|
|
case downloader.ErrBusy:
|
|
|
|
glog.V(logger.Debug).Infof("Synchronisation already in progress")
|
|
|
|
|
2015-05-15 13:14:46 +03:00
|
|
|
case downloader.ErrTimeout, downloader.ErrBadPeer, downloader.ErrInvalidChain, downloader.ErrCrossCheckFailed:
|
2015-05-15 01:40:16 +03:00
|
|
|
glog.V(logger.Debug).Infof("Removing peer %v: %v", peer.id, err)
|
2015-05-01 01:23:51 +03:00
|
|
|
pm.removePeer(peer)
|
2015-05-15 01:40:16 +03:00
|
|
|
|
2015-05-11 18:27:34 +03:00
|
|
|
case downloader.ErrPendingQueue:
|
|
|
|
glog.V(logger.Debug).Infoln("Synchronisation aborted:", err)
|
2015-05-15 01:40:16 +03:00
|
|
|
|
2015-05-08 15:22:48 +03:00
|
|
|
default:
|
|
|
|
glog.V(logger.Warn).Infof("Synchronisation failed: %v", err)
|
2015-05-01 01:23:51 +03:00
|
|
|
}
|
|
|
|
}
|