network: don't request blocks we already have in the queue

Fixes #2258.
This commit is contained in:
Roman Khimov 2022-01-18 00:04:41 +03:00
parent 03fd91e857
commit 89d754da6f
3 changed files with 27 additions and 2 deletions

View file

@ -17,8 +17,9 @@ type Blockqueuer interface {
type blockQueue struct {
log *zap.Logger
queueLock sync.Mutex
queueLock sync.RWMutex
queue []*block.Block
lastQ uint32
checkBlocks chan struct{}
chain Blockqueuer
relayF func(*block.Block)
@ -115,6 +116,10 @@ func (bq *blockQueue) putBlock(block *block.Block) error {
if bq.queue[pos] == nil || bq.queue[pos].Index < block.Index {
bq.len++
bq.queue[pos] = block
for pos < blockCacheSize && bq.queue[pos] != nil && bq.lastQ+1 == bq.queue[pos].Index {
bq.lastQ = bq.queue[pos].Index
pos++
}
}
l := bq.len
bq.queueLock.Unlock()
@ -129,6 +134,12 @@ func (bq *blockQueue) putBlock(block *block.Block) error {
return nil
}
func (bq *blockQueue) lastQueued() uint32 {
bq.queueLock.RLock()
defer bq.queueLock.RUnlock()
return bq.lastQ
}
func (bq *blockQueue) discard() {
if bq.discarded.CAS(false, true) {
close(bq.checkBlocks)