diff options
author | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-06-09 18:45:41 +0800 |
---|---|---|
committer | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-06-09 18:45:41 +0800 |
commit | 087949227c3b10c2ae45cb1a54a6de1f4f5d6600 (patch) | |
tree | e9833af8a79a73d50c4142a4d9b4a5a54d990376 /eth/downloader/queue.go | |
parent | 11f65cf885317d6c355b4c4a8d7420bcb82839d1 (diff) | |
parent | 4ed3509a02c7f5a09036e6e9cb615c6def6d25f3 (diff) | |
download | dexon-087949227c3b10c2ae45cb1a54a6de1f4f5d6600.tar dexon-087949227c3b10c2ae45cb1a54a6de1f4f5d6600.tar.gz dexon-087949227c3b10c2ae45cb1a54a6de1f4f5d6600.tar.bz2 dexon-087949227c3b10c2ae45cb1a54a6de1f4f5d6600.tar.lz dexon-087949227c3b10c2ae45cb1a54a6de1f4f5d6600.tar.xz dexon-087949227c3b10c2ae45cb1a54a6de1f4f5d6600.tar.zst dexon-087949227c3b10c2ae45cb1a54a6de1f4f5d6600.zip |
Merge pull request #1153 from karalabe/downloader-banned-starvation-attack
eth/downloader: gather and ban hashes from invalid chains
Diffstat (limited to 'eth/downloader/queue.go')
-rw-r--r-- | eth/downloader/queue.go | 30 |
1 files changed, 11 insertions, 19 deletions
diff --git a/eth/downloader/queue.go b/eth/downloader/queue.go index 671ffe51b..7abbd42fd 100644 --- a/eth/downloader/queue.go +++ b/eth/downloader/queue.go @@ -16,7 +16,7 @@ import ( "gopkg.in/karalabe/cookiejar.v2/collections/prque" ) -const ( +var ( blockCacheLimit = 8 * MaxBlockFetch // Maximum number of blocks to cache before throttling the download ) @@ -50,10 +50,11 @@ type queue struct { // newQueue creates a new download queue for scheduling block retrieval. func newQueue() *queue { return &queue{ - hashPool: make(map[common.Hash]int), - hashQueue: prque.New(), - pendPool: make(map[string]*fetchRequest), - blockPool: make(map[common.Hash]int), + hashPool: make(map[common.Hash]int), + hashQueue: prque.New(), + pendPool: make(map[string]*fetchRequest), + blockPool: make(map[common.Hash]int), + blockCache: make([]*Block, blockCacheLimit), } } @@ -70,7 +71,7 @@ func (q *queue) Reset() { q.blockPool = make(map[common.Hash]int) q.blockOffset = 0 - q.blockCache = nil + q.blockCache = make([]*Block, blockCacheLimit) } // Size retrieves the number of hashes in the queue, returning separately for @@ -208,7 +209,7 @@ func (q *queue) TakeBlocks() []*Block { // Reserve reserves a set of hashes for the given peer, skipping any previously // failed download. -func (q *queue) Reserve(p *peer) *fetchRequest { +func (q *queue) Reserve(p *peer, count int) *fetchRequest { q.lock.Lock() defer q.lock.Unlock() @@ -229,8 +230,7 @@ func (q *queue) Reserve(p *peer) *fetchRequest { send := make(map[common.Hash]int) skip := make(map[common.Hash]int) - capacity := p.Capacity() - for proc := 0; proc < space && len(send) < capacity && !q.hashQueue.Empty(); proc++ { + for proc := 0; proc < space && len(send) < count && !q.hashQueue.Empty(); proc++ { hash, priority := q.hashQueue.Pop() if p.ignored.Has(hash) { skip[hash.(common.Hash)] = int(priority) @@ -345,20 +345,12 @@ func (q *queue) Deliver(id string, blocks []*types.Block) (err error) { return nil } -// Alloc ensures that the block cache is the correct size, given a starting -// offset, and a memory cap. -func (q *queue) Alloc(offset int) { +// Prepare configures the block cache offset to allow accepting inbound blocks. +func (q *queue) Prepare(offset int) { q.lock.Lock() defer q.lock.Unlock() if q.blockOffset < offset { q.blockOffset = offset } - size := len(q.hashPool) - if size > blockCacheLimit { - size = blockCacheLimit - } - if len(q.blockCache) < size { - q.blockCache = append(q.blockCache, make([]*Block, size-len(q.blockCache))...) - } } |