Skip to content

Commit

Permalink
set target height (#2807)
Browse files Browse the repository at this point in the history
Co-authored-by: dustinxie <dahuaxie@gmail.com>
  • Loading branch information
CoderZhi and dustinxie committed Sep 26, 2021
1 parent dba4993 commit e89be88
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
11 changes: 6 additions & 5 deletions blocksync/blocksync.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,14 @@ func (bs *blockSyncer) ProcessBlock(ctx context.Context, peer string, blk *block
}

tip := bs.tipHeightHandler()
if !bs.buf.AddBlock(tip, newPeerBlock(peer, blk)) {
return nil
}
added, targetHeight := bs.buf.AddBlock(tip, newPeerBlock(peer, blk))
bs.mu.Lock()
defer bs.mu.Unlock()
if blk.Height() > bs.targetHeight {
bs.targetHeight = blk.Height()
if targetHeight > bs.targetHeight {
bs.targetHeight = targetHeight
}
if !added {
return nil
}
syncedHeight := tip
for {
Expand Down
19 changes: 11 additions & 8 deletions blocksync/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,21 @@ func (b *blockBuffer) Cleanup(height uint64) {
}

// AddBlock tries to put given block into buffer and flush buffer into blockchain.
func (b *blockBuffer) AddBlock(tipHeight uint64, blk *peerBlock) bool {
func (b *blockBuffer) AddBlock(tipHeight uint64, blk *peerBlock) (bool, uint64) {
b.mu.Lock()
defer b.mu.Unlock()
blkHeight := blk.block.Height()
if blkHeight > tipHeight && blkHeight <= tipHeight+b.bufferSize {
if _, ok := b.blockQueues[blkHeight]; !ok {
b.blockQueues[blkHeight] = newUniQueue()
}
b.blockQueues[blkHeight].enque(blk)
return true
if blkHeight <= tipHeight {
return false, 0
}
if blkHeight > tipHeight+b.bufferSize {
return false, tipHeight + b.bufferSize
}
if _, ok := b.blockQueues[blkHeight]; !ok {
b.blockQueues[blkHeight] = newUniQueue()
}
return false
b.blockQueues[blkHeight].enque(blk)
return true, blkHeight
}

// GetBlocksIntervalsToSync returns groups of syncBlocksInterval are missing upto targetHeight.
Expand Down

0 comments on commit e89be88

Please sign in to comment.