network: move index-position relation into helper

Just to make things more clear, no functional changes.
This commit is contained in:
Roman Khimov 2022-01-18 00:01:26 +03:00
parent a23943c161
commit d52a06a82d

View file

@ -32,6 +32,10 @@ const (
blockCacheSize = 2000 blockCacheSize = 2000
) )
func indexToPosition(i uint32) int {
return int(i) % blockCacheSize
}
func newBlockQueue(capacity int, bc Blockqueuer, log *zap.Logger, relayer func(*block.Block)) *blockQueue { func newBlockQueue(capacity int, bc Blockqueuer, log *zap.Logger, relayer func(*block.Block)) *blockQueue {
if log == nil { if log == nil {
return nil return nil
@ -56,12 +60,12 @@ func (bq *blockQueue) run() {
} }
for { for {
h := bq.chain.BlockHeight() h := bq.chain.BlockHeight()
pos := int(h+1) % blockCacheSize pos := indexToPosition(h + 1)
bq.queueLock.Lock() bq.queueLock.Lock()
b := bq.queue[pos] b := bq.queue[pos]
// The chain moved forward using blocks from other sources (consensus). // The chain moved forward using blocks from other sources (consensus).
for i := lastHeight; i < h; i++ { for i := lastHeight; i < h; i++ {
old := int(i+1) % blockCacheSize old := indexToPosition(i + 1)
if bq.queue[old] != nil && bq.queue[old].Index == i { if bq.queue[old] != nil && bq.queue[old].Index == i {
bq.len-- bq.len--
bq.queue[old] = nil bq.queue[old] = nil
@ -106,7 +110,7 @@ func (bq *blockQueue) putBlock(block *block.Block) error {
bq.queueLock.Unlock() bq.queueLock.Unlock()
return nil return nil
} }
pos := block.Index % blockCacheSize pos := indexToPosition(block.Index)
// If we already have it, keep the old block, throw away new one. // If we already have it, keep the old block, throw away new one.
if bq.queue[pos] == nil || bq.queue[pos].Index < block.Index { if bq.queue[pos] == nil || bq.queue[pos].Index < block.Index {
bq.len++ bq.len++