diff options
author | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-05-15 21:38:12 +0800 |
---|---|---|
committer | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-05-15 21:38:12 +0800 |
commit | 7d71a75d7715f97a86e0bd2e5aa9ac149d0ee4b5 (patch) | |
tree | 6be0a8967229d27b6262cea90bb7d54d76670eb9 /eth/downloader/queue.go | |
parent | c1f0d40e34a80f4453a9a54f90e2d4551c3bdb05 (diff) | |
parent | 5c1a7b965ca7901d3b185d75205419b87163a4fa (diff) | |
download | dexon-7d71a75d7715f97a86e0bd2e5aa9ac149d0ee4b5.tar dexon-7d71a75d7715f97a86e0bd2e5aa9ac149d0ee4b5.tar.gz dexon-7d71a75d7715f97a86e0bd2e5aa9ac149d0ee4b5.tar.bz2 dexon-7d71a75d7715f97a86e0bd2e5aa9ac149d0ee4b5.tar.lz dexon-7d71a75d7715f97a86e0bd2e5aa9ac149d0ee4b5.tar.xz dexon-7d71a75d7715f97a86e0bd2e5aa9ac149d0ee4b5.tar.zst dexon-7d71a75d7715f97a86e0bd2e5aa9ac149d0ee4b5.zip |
Merge pull request #988 from karalabe/fix-downloader-vulnerabilities
Fix downloader vulnerabilities
Diffstat (limited to 'eth/downloader/queue.go')
-rw-r--r-- | eth/downloader/queue.go | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/eth/downloader/queue.go b/eth/downloader/queue.go index 6ad915757..13ec9a520 100644 --- a/eth/downloader/queue.go +++ b/eth/downloader/queue.go @@ -122,24 +122,28 @@ func (q *queue) Has(hash common.Hash) bool { return false } -// Insert adds a set of hashes for the download queue for scheduling. -func (q *queue) Insert(hashes []common.Hash) { +// Insert adds a set of hashes for the download queue for scheduling, returning +// the new hashes encountered. +func (q *queue) Insert(hashes []common.Hash) []common.Hash { q.lock.Lock() defer q.lock.Unlock() // Insert all the hashes prioritized in the arrival order - for i, hash := range hashes { - index := q.hashCounter + i - + inserts := make([]common.Hash, 0, len(hashes)) + for _, hash := range hashes { + // Skip anything we already have if old, ok := q.hashPool[hash]; ok { glog.V(logger.Warn).Infof("Hash %x already scheduled at index %v", hash, old) continue } - q.hashPool[hash] = index - q.hashQueue.Push(hash, float32(index)) // Highest gets schedules first + // Update the counters and insert the hash + q.hashCounter = q.hashCounter + 1 + inserts = append(inserts, hash) + + q.hashPool[hash] = q.hashCounter + q.hashQueue.Push(hash, float32(q.hashCounter)) // Highest gets schedules first } - // Update the hash counter for the next batch of inserts - q.hashCounter += len(hashes) + return inserts } // GetHeadBlock retrieves the first block from the cache, or nil if it hasn't @@ -296,18 +300,17 @@ func (q *queue) Deliver(id string, blocks []*types.Block) (err error) { // Iterate over the downloaded blocks and add each of them errs := make([]error, 0) for _, block := range blocks { - // Skip any blocks that fall outside the cache range - index := int(block.NumberU64()) - q.blockOffset - if index >= len(q.blockCache) || index < 0 { - //fmt.Printf("block cache overflown (N=%v O=%v, C=%v)", block.Number(), q.blockOffset, len(q.blockCache)) - continue - } // Skip any blocks that were not requested hash := block.Hash() if _, ok := request.Hashes[hash]; !ok { errs = append(errs, fmt.Errorf("non-requested block %v", hash)) continue } + // If a requested block falls out of the range, the hash chain is invalid + index := int(block.NumberU64()) - q.blockOffset + if index >= len(q.blockCache) || index < 0 { + return ErrInvalidChain + } // Otherwise merge the block and mark the hash block q.blockCache[index] = block |