2019-09-25 16:54:31 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/Workiva/go-datastructures/queue"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
2020-04-08 10:56:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/blockchainer"
|
2019-12-30 07:43:05 +00:00
|
|
|
"go.uber.org/zap"
|
2019-09-25 16:54:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type blockQueue struct {
|
2019-12-30 07:43:05 +00:00
|
|
|
log *zap.Logger
|
2019-09-25 16:54:31 +00:00
|
|
|
queue *queue.PriorityQueue
|
|
|
|
checkBlocks chan struct{}
|
2020-04-08 10:56:04 +00:00
|
|
|
chain blockchainer.Blockchainer
|
2020-02-04 16:32:29 +00:00
|
|
|
relayF func(*block.Block)
|
2019-09-25 16:54:31 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 12:00:53 +00:00
|
|
|
const (
|
|
|
|
// blockCacheSize is the amount of blocks above current height
|
|
|
|
// which are stored in queue.
|
|
|
|
blockCacheSize = 2000
|
|
|
|
)
|
|
|
|
|
2020-04-08 10:56:04 +00:00
|
|
|
func newBlockQueue(capacity int, bc blockchainer.Blockchainer, log *zap.Logger, relayer func(*block.Block)) *blockQueue {
|
2019-12-30 07:43:05 +00:00
|
|
|
if log == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-25 16:54:31 +00:00
|
|
|
return &blockQueue{
|
2019-12-30 07:43:05 +00:00
|
|
|
log: log,
|
2019-09-25 16:54:31 +00:00
|
|
|
queue: queue.NewPriorityQueue(capacity, false),
|
|
|
|
checkBlocks: make(chan struct{}, 1),
|
|
|
|
chain: bc,
|
2020-02-04 16:32:29 +00:00
|
|
|
relayF: relayer,
|
2019-09-25 16:54:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bq *blockQueue) run() {
|
|
|
|
for {
|
|
|
|
_, ok := <-bq.checkBlocks
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
item := bq.queue.Peek()
|
|
|
|
if item == nil {
|
|
|
|
break
|
|
|
|
}
|
2020-01-14 12:32:07 +00:00
|
|
|
minblock := item.(*block.Block)
|
2019-09-25 16:54:31 +00:00
|
|
|
if minblock.Index <= bq.chain.BlockHeight()+1 {
|
|
|
|
_, _ = bq.queue.Get(1)
|
2019-10-29 17:51:17 +00:00
|
|
|
updateBlockQueueLenMetric(bq.length())
|
2019-09-25 16:54:31 +00:00
|
|
|
if minblock.Index == bq.chain.BlockHeight()+1 {
|
|
|
|
err := bq.chain.AddBlock(minblock)
|
|
|
|
if err != nil {
|
2020-02-04 16:32:29 +00:00
|
|
|
// The block might already be added by consensus.
|
2020-08-10 13:51:12 +00:00
|
|
|
if bq.chain.BlockHeight() < minblock.Index {
|
2020-02-04 16:32:29 +00:00
|
|
|
bq.log.Warn("blockQueue: failed adding block into the blockchain",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
zap.Uint32("blockHeight", bq.chain.BlockHeight()),
|
|
|
|
zap.Uint32("nextIndex", minblock.Index))
|
|
|
|
}
|
|
|
|
} else if bq.relayF != nil {
|
|
|
|
bq.relayF(minblock)
|
2019-09-25 16:54:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 12:32:07 +00:00
|
|
|
func (bq *blockQueue) putBlock(block *block.Block) error {
|
2020-09-02 12:00:53 +00:00
|
|
|
h := bq.chain.BlockHeight()
|
|
|
|
if block.Index <= h || h+blockCacheSize < block.Index {
|
2019-09-25 16:54:31 +00:00
|
|
|
// can easily happen when fetching the same blocks from
|
|
|
|
// different peers, thus not considered as error
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err := bq.queue.Put(block)
|
2019-10-29 17:51:17 +00:00
|
|
|
// update metrics
|
|
|
|
updateBlockQueueLenMetric(bq.length())
|
2019-09-25 16:54:31 +00:00
|
|
|
select {
|
|
|
|
case bq.checkBlocks <- struct{}{}:
|
|
|
|
// ok, signalled to goroutine processing queue
|
|
|
|
default:
|
|
|
|
// it's already busy processing blocks
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bq *blockQueue) discard() {
|
|
|
|
close(bq.checkBlocks)
|
|
|
|
bq.queue.Dispose()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bq *blockQueue) length() int {
|
|
|
|
return bq.queue.Len()
|
|
|
|
}
|