diff options
author | haoping-ku <haoping.ku@cobinhood.com> | 2018-11-06 10:43:30 +0800 |
---|---|---|
committer | Mission Liao <mission.liao@dexon.org> | 2018-11-06 10:43:30 +0800 |
commit | 3967549d1a291d5983227294d3f301e7684c3807 (patch) | |
tree | 5ddbf0b8fd77881fdc963beebdebe70681df1ef1 /core/blockpool.go | |
parent | f3e03e18d815d92493d3a85ecb3a0048247ecf71 (diff) | |
download | tangerine-consensus-3967549d1a291d5983227294d3f301e7684c3807.tar tangerine-consensus-3967549d1a291d5983227294d3f301e7684c3807.tar.gz tangerine-consensus-3967549d1a291d5983227294d3f301e7684c3807.tar.bz2 tangerine-consensus-3967549d1a291d5983227294d3f301e7684c3807.tar.lz tangerine-consensus-3967549d1a291d5983227294d3f301e7684c3807.tar.xz tangerine-consensus-3967549d1a291d5983227294d3f301e7684c3807.tar.zst tangerine-consensus-3967549d1a291d5983227294d3f301e7684c3807.zip |
core: lattice-data: fix details and add test (#299)
Diffstat (limited to 'core/blockpool.go')
-rw-r--r-- | core/blockpool.go | 26 |
1 files changed, 10 insertions, 16 deletions
diff --git a/core/blockpool.go b/core/blockpool.go index 2619667..7861a73 100644 --- a/core/blockpool.go +++ b/core/blockpool.go @@ -23,11 +23,10 @@ import ( "github.com/dexon-foundation/dexon-consensus/core/types" ) -// blockPool is a slice of heap of blocks, indexed by chainID, -// and the heap is sorted based on heights of blocks. +// blockPool is a heaped slice of blocks, indexed by chainID, and each in it is +// sorted by block's height. type blockPool []types.ByPosition -// newBlockPool constructs a blockPool. func newBlockPool(chainNum uint32) (pool blockPool) { pool = make(blockPool, chainNum) for _, p := range pool { @@ -36,12 +35,12 @@ func newBlockPool(chainNum uint32) (pool blockPool) { return } -// resize the pool if new chain is added. func (p *blockPool) resize(num uint32) { if uint32(len(*p)) >= num { + // Do nothing If the origin size is larger. return } - newPool := make([]types.ByPosition, num) + newPool := make(blockPool, num) copy(newPool, *p) for i := uint32(len(*p)); i < num; i++ { newChain := types.ByPosition{} @@ -51,25 +50,20 @@ func (p *blockPool) resize(num uint32) { *p = newPool } -// addBlock adds a block into pending set and make sure these -// blocks are sorted by height. +// addBlock adds a block into pool and sorts them by height. func (p blockPool) addBlock(b *types.Block) { heap.Push(&p[b.Position.ChainID], b) } -// purgeBlocks purge blocks of that chain with less-or-equal height. -// NOTE: we won't check the validity of 'chainID', the caller should -// be sure what he is expecting. +// purgeBlocks purges blocks of a specified chain with less-or-equal heights. +// NOTE: "chainID" is not checked here, this should be ensured by the called. func (p blockPool) purgeBlocks(chainID uint32, height uint64) { - for { - if len(p[chainID]) == 0 || p[chainID][0].Position.Height > height { - break - } + for len(p[chainID]) > 0 && p[chainID][0].Position.Height <= height { heap.Pop(&p[chainID]) } } -// tip get the blocks with lowest height of the chain if any. +// tip returns block with the smallest height, nil if no existing block. func (p blockPool) tip(chainID uint32) *types.Block { if len(p[chainID]) == 0 { return nil @@ -77,7 +71,7 @@ func (p blockPool) tip(chainID uint32) *types.Block { return p[chainID][0] } -// removeTip removes block with lowest height of the specified chain. +// removeTip removes block with lowest height of a specified chain. func (p blockPool) removeTip(chainID uint32) { if len(p[chainID]) > 0 { heap.Pop(&p[chainID]) |