2015-04-30 22:23:51 +00:00
|
|
|
package eth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"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() {
|
|
|
|
// itimer is used to determine when to start ignoring `minDesiredPeerCount`
|
|
|
|
itimer := time.NewTimer(peerCountTimeout)
|
|
|
|
// btimer is used for picking of blocks from the downloader
|
2015-05-01 14:30:02 +00:00
|
|
|
btimer := time.Tick(blockProcTimer)
|
|
|
|
|
2015-04-30 22:23:51 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-pm.newPeerCh:
|
|
|
|
// Meet the `minDesiredPeerCount` before we select our best peer
|
|
|
|
if len(pm.peers) < minDesiredPeerCount {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the best peer
|
|
|
|
peer := getBestPeer(pm.peers)
|
|
|
|
if peer == nil {
|
|
|
|
glog.V(logger.Debug).Infoln("Sync attempt cancelled. No peers available")
|
|
|
|
}
|
|
|
|
|
|
|
|
itimer.Stop()
|
2015-05-07 18:07:20 +00:00
|
|
|
go pm.synchronize(peer)
|
2015-04-30 22:23:51 +00:00
|
|
|
case <-itimer.C:
|
|
|
|
// The timer will make sure that the downloader keeps an active state
|
|
|
|
// in which it attempts to always check the network for highest td peers
|
|
|
|
// Either select the peer or restart the timer if no peers could
|
|
|
|
// be selected.
|
|
|
|
if peer := getBestPeer(pm.peers); peer != nil {
|
2015-05-07 18:07:20 +00:00
|
|
|
go pm.synchronize(peer)
|
2015-04-30 22:23:51 +00:00
|
|
|
} else {
|
|
|
|
itimer.Reset(5 * time.Second)
|
|
|
|
}
|
2015-05-01 14:30:02 +00:00
|
|
|
case <-btimer:
|
2015-04-30 22:23:51 +00:00
|
|
|
go pm.processBlocks()
|
|
|
|
case <-pm.quitSync:
|
2015-05-01 14:30:02 +00:00
|
|
|
return
|
2015-04-30 22:23:51 +00: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()
|
|
|
|
|
|
|
|
blocks := pm.downloader.TakeBlocks()
|
|
|
|
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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
blocks = blocks[max:]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-07 18:07:20 +00:00
|
|
|
func (pm *ProtocolManager) synchronize(peer *peer) {
|
2015-04-30 22:23:51 +00: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 12:11:47 +00: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-04-30 22:23:51 +00:00
|
|
|
// Get the hashes from the peer (synchronously)
|
2015-05-07 18:07:20 +00:00
|
|
|
err := pm.downloader.Synchronize(peer.id, peer.recentHash)
|
2015-04-30 22:23:51 +00:00
|
|
|
if err != nil && err == downloader.ErrBadPeer {
|
|
|
|
glog.V(logger.Debug).Infoln("removed peer from peer set due to bad action")
|
|
|
|
pm.removePeer(peer)
|
|
|
|
} else if err != nil {
|
|
|
|
// handle error
|
|
|
|
glog.V(logger.Detail).Infoln("error downloading:", err)
|
|
|
|
}
|
|
|
|
}
|