diff options
author | Péter Szilágyi <peterke@gmail.com> | 2015-05-15 06:40:16 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2015-05-15 20:01:58 +0800 |
commit | cd2fb0905109828028172c84f9c10f1343647ca6 (patch) | |
tree | ede1984738a0d16f338341f7a1bc94e17381a715 /eth/downloader/queue.go | |
parent | c1f0d40e34a80f4453a9a54f90e2d4551c3bdb05 (diff) | |
download | go-tangerine-cd2fb0905109828028172c84f9c10f1343647ca6.tar.gz go-tangerine-cd2fb0905109828028172c84f9c10f1343647ca6.tar.zst go-tangerine-cd2fb0905109828028172c84f9c10f1343647ca6.zip |
eth, eth/downloader: prevent hash repeater attack
Diffstat (limited to 'eth/downloader/queue.go')
-rw-r--r-- | eth/downloader/queue.go | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/eth/downloader/queue.go b/eth/downloader/queue.go index 6ad915757..fdea1f63f 100644 --- a/eth/downloader/queue.go +++ b/eth/downloader/queue.go @@ -122,24 +122,26 @@ 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 number of new hashes encountered. +func (q *queue) Insert(hashes []common.Hash) int { 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 := 0 + 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, inserts = q.hashCounter+1, inserts+1 + 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 |