diff options
author | Péter Szilágyi <peterke@gmail.com> | 2015-05-14 20:24:18 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2015-05-14 20:24:18 +0800 |
commit | a4246c2da658d9b5b02a4caba511688748a88b19 (patch) | |
tree | 0733053cac6948e0a1cb40eb2f44520569d767b9 /eth/downloader/downloader.go | |
parent | 7cb0e242450fd15c6ee8f5d70af41504a047e0aa (diff) | |
download | dexon-a4246c2da658d9b5b02a4caba511688748a88b19.tar dexon-a4246c2da658d9b5b02a4caba511688748a88b19.tar.gz dexon-a4246c2da658d9b5b02a4caba511688748a88b19.tar.bz2 dexon-a4246c2da658d9b5b02a4caba511688748a88b19.tar.lz dexon-a4246c2da658d9b5b02a4caba511688748a88b19.tar.xz dexon-a4246c2da658d9b5b02a4caba511688748a88b19.tar.zst dexon-a4246c2da658d9b5b02a4caba511688748a88b19.zip |
eth, eth/downloader: handle a potential unknown parent attack
Diffstat (limited to 'eth/downloader/downloader.go')
-rw-r--r-- | eth/downloader/downloader.go | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index c6eecfe2f..f33aa334a 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -37,6 +37,7 @@ var ( errCancelHashFetch = errors.New("hash fetching cancelled (requested)") errCancelBlockFetch = errors.New("block downloading cancelled (requested)") errNoSyncActive = errors.New("no sync active") + ErrUnknownParent = errors.New("block has unknown parent") ) type hashCheckFn func(common.Hash) bool @@ -142,16 +143,19 @@ func (d *Downloader) Synchronise(id string, hash common.Hash) error { return d.syncWithPeer(p, hash) } -// TakeBlocks takes blocks from the queue and yields them to the blockTaker handler -// it's possible it yields no blocks -func (d *Downloader) TakeBlocks() types.Blocks { - // Check that there are blocks available and its parents are known +// TakeBlocks takes blocks from the queue and yields them to the caller. +func (d *Downloader) TakeBlocks() (types.Blocks, error) { + // If the head block is missing, no blocks are ready head := d.queue.GetHeadBlock() - if head == nil || !d.hasBlock(head.ParentHash()) { - return nil + if head == nil { + return nil, nil } - // Retrieve a full batch of blocks - return d.queue.TakeBlocks(head) + // If the parent hash of the head is unknown, notify the caller + if !d.hasBlock(head.ParentHash()) { + return nil, ErrUnknownParent + } + // Otherwise retrieve a full batch of blocks + return d.queue.TakeBlocks(head), nil } func (d *Downloader) Has(hash common.Hash) bool { |