|
|
@ -50,10 +50,11 @@ type queue struct { |
|
|
|
// newQueue creates a new download queue for scheduling block retrieval.
|
|
|
|
// newQueue creates a new download queue for scheduling block retrieval.
|
|
|
|
func newQueue() *queue { |
|
|
|
func newQueue() *queue { |
|
|
|
return &queue{ |
|
|
|
return &queue{ |
|
|
|
hashPool: make(map[common.Hash]int), |
|
|
|
hashPool: make(map[common.Hash]int), |
|
|
|
hashQueue: prque.New(), |
|
|
|
hashQueue: prque.New(), |
|
|
|
pendPool: make(map[string]*fetchRequest), |
|
|
|
pendPool: make(map[string]*fetchRequest), |
|
|
|
blockPool: make(map[common.Hash]int), |
|
|
|
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.blockPool = make(map[common.Hash]int) |
|
|
|
q.blockOffset = 0 |
|
|
|
q.blockOffset = 0 |
|
|
|
q.blockCache = nil |
|
|
|
q.blockCache = make([]*Block, blockCacheLimit) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Size retrieves the number of hashes in the queue, returning separately for
|
|
|
|
// 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
|
|
|
|
// Reserve reserves a set of hashes for the given peer, skipping any previously
|
|
|
|
// failed download.
|
|
|
|
// failed download.
|
|
|
|
func (q *queue) Reserve(p *peer) *fetchRequest { |
|
|
|
func (q *queue) Reserve(p *peer, count int) *fetchRequest { |
|
|
|
q.lock.Lock() |
|
|
|
q.lock.Lock() |
|
|
|
defer q.lock.Unlock() |
|
|
|
defer q.lock.Unlock() |
|
|
|
|
|
|
|
|
|
|
@ -345,20 +346,12 @@ func (q *queue) Deliver(id string, blocks []*types.Block) (err error) { |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Alloc ensures that the block cache is the correct size, given a starting
|
|
|
|
// Prepare configures the block cache offset to allow accepting inbound blocks.
|
|
|
|
// offset, and a memory cap.
|
|
|
|
func (q *queue) Prepare(offset int) { |
|
|
|
func (q *queue) Alloc(offset int) { |
|
|
|
|
|
|
|
q.lock.Lock() |
|
|
|
q.lock.Lock() |
|
|
|
defer q.lock.Unlock() |
|
|
|
defer q.lock.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
if q.blockOffset < offset { |
|
|
|
if q.blockOffset < offset { |
|
|
|
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))...) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|