aboutsummaryrefslogtreecommitdiffstats
path: root/eth/sync.go
diff options
context:
space:
mode:
authorJeffrey Wilcke <jeffrey@ethereum.org>2015-05-14 23:16:30 +0800
committerJeffrey Wilcke <jeffrey@ethereum.org>2015-05-14 23:16:30 +0800
commit060a07cf69a2083611565e92cfa78d35c71cdb9f (patch)
tree4d499550d07ebf21d74f08dc7667213faf7621de /eth/sync.go
parent90b94e64fcdc6a2099ec5c48e41acf7f95a6b804 (diff)
parentfe87feccb157b2426075523a592cabcb4c6d1cf0 (diff)
downloaddexon-060a07cf69a2083611565e92cfa78d35c71cdb9f.tar
dexon-060a07cf69a2083611565e92cfa78d35c71cdb9f.tar.gz
dexon-060a07cf69a2083611565e92cfa78d35c71cdb9f.tar.bz2
dexon-060a07cf69a2083611565e92cfa78d35c71cdb9f.tar.lz
dexon-060a07cf69a2083611565e92cfa78d35c71cdb9f.tar.xz
dexon-060a07cf69a2083611565e92cfa78d35c71cdb9f.tar.zst
dexon-060a07cf69a2083611565e92cfa78d35c71cdb9f.zip
Merge pull request #974 from karalabe/downloader-fix-unknown-parent-attack
eth, eth/downloader: handle a potential unknown parent attack
Diffstat (limited to 'eth/sync.go')
-rw-r--r--eth/sync.go14
1 files changed, 10 insertions, 4 deletions
diff --git a/eth/sync.go b/eth/sync.go
index 00b571782..c89f34596 100644
--- a/eth/sync.go
+++ b/eth/sync.go
@@ -2,6 +2,7 @@ package eth
import (
"math"
+ "sync/atomic"
"time"
"github.com/ethereum/go-ethereum/eth/downloader"
@@ -14,6 +15,7 @@ import (
func (pm *ProtocolManager) update() {
forceSync := time.Tick(forceSyncCycle)
blockProc := time.Tick(blockProcCycle)
+ blockProcPend := int32(0)
for {
select {
@@ -36,7 +38,12 @@ func (pm *ProtocolManager) update() {
}
case <-blockProc:
// Try to pull some blocks from the downloaded
- go pm.processBlocks()
+ if atomic.CompareAndSwapInt32(&blockProcPend, 0, 1) {
+ go func() {
+ pm.processBlocks()
+ atomic.StoreInt32(&blockProcPend, 0)
+ }()
+ }
case <-pm.quitSync:
return
@@ -52,7 +59,7 @@ func (pm *ProtocolManager) processBlocks() error {
pm.wg.Add(1)
defer pm.wg.Done()
- // Take a batch of blocks (will return nil if a previous batch has not reached the chain yet)
+ // Short circuit if no blocks are available for insertion
blocks := pm.downloader.TakeBlocks()
if len(blocks) == 0 {
return nil
@@ -63,9 +70,8 @@ func (pm *ProtocolManager) processBlocks() error {
max := int(math.Min(float64(len(blocks)), float64(blockProcAmount)))
_, err := pm.chainman.InsertChain(blocks[:max])
if err != nil {
- // cancel download process
+ glog.V(logger.Warn).Infof("Block insertion failed: %v", err)
pm.downloader.Cancel()
-
return err
}
blocks = blocks[max:]