aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMission Liao <mission.liao@dexon.org>2018-12-04 15:24:10 +0800
committerGitHub <noreply@github.com>2018-12-04 15:24:10 +0800
commit3a6b94650d38ea979e00f15350321737418aba39 (patch)
tree6a7d4ab849565c02e76ef80210b33b7d952394b4
parent0be7c830e4f96a7ae1a957c4bb12772447617076 (diff)
downloaddexon-consensus-3a6b94650d38ea979e00f15350321737418aba39.tar
dexon-consensus-3a6b94650d38ea979e00f15350321737418aba39.tar.gz
dexon-consensus-3a6b94650d38ea979e00f15350321737418aba39.tar.bz2
dexon-consensus-3a6b94650d38ea979e00f15350321737418aba39.tar.lz
dexon-consensus-3a6b94650d38ea979e00f15350321737418aba39.tar.xz
dexon-consensus-3a6b94650d38ea979e00f15350321737418aba39.tar.zst
dexon-consensus-3a6b94650d38ea979e00f15350321737418aba39.zip
core: log the last pending block in compaction chain (#355)
-rw-r--r--core/compaction-chain.go38
-rw-r--r--core/compaction-chain_test.go2
-rw-r--r--core/consensus.go5
3 files changed, 25 insertions, 20 deletions
diff --git a/core/compaction-chain.go b/core/compaction-chain.go
index 89ba978..dcd99f4 100644
--- a/core/compaction-chain.go
+++ b/core/compaction-chain.go
@@ -82,7 +82,6 @@ func (cc *compactionChain) init(initBlock *types.Block) {
if initBlock.Finalization.Height == 0 {
cc.chainUnsynced = cc.gov.Configuration(uint64(0)).NumChains
}
- cc.pendingBlocks = append(cc.pendingBlocks, initBlock)
}
func (cc *compactionChain) registerBlock(block *types.Block) {
@@ -111,7 +110,7 @@ func (cc *compactionChain) blockRegisteredNoLock(
}
func (cc *compactionChain) processBlock(block *types.Block) error {
- prevBlock := cc.lastBlock()
+ prevBlock := cc.lastDeliveredBlock()
if prevBlock == nil {
return ErrNotInitiazlied
}
@@ -125,7 +124,7 @@ func (cc *compactionChain) processBlock(block *types.Block) error {
}
func (cc *compactionChain) extractBlocks() []*types.Block {
- prevBlock := cc.lastBlock()
+ prevBlock := cc.lastDeliveredBlock()
// Check if we're synced.
if !func() bool {
@@ -138,9 +137,6 @@ func (cc *compactionChain) extractBlocks() []*types.Block {
if prevBlock.Finalization.Height == 0 {
return cc.chainUnsynced == 0
}
- if prevBlock.Hash != cc.pendingBlocks[0].Hash {
- return false
- }
return true
}() {
return []*types.Block{}
@@ -148,14 +144,12 @@ func (cc *compactionChain) extractBlocks() []*types.Block {
deliveringBlocks := make([]*types.Block, 0)
cc.lock.Lock()
defer cc.lock.Unlock()
- // cc.pendingBlocks[0] will not be popped and will equal to cc.prevBlock.
- for len(cc.pendingBlocks) > 1 &&
- (len(cc.blockRandomness[cc.pendingBlocks[1].Hash]) != 0 ||
- cc.pendingBlocks[1].Position.Round == 0) {
+ var block *types.Block
+ for len(cc.pendingBlocks) > 0 &&
+ (len(cc.blockRandomness[cc.pendingBlocks[0].Hash]) != 0 ||
+ cc.pendingBlocks[0].Position.Round == 0) {
delete(cc.blocks, cc.pendingBlocks[0].Hash)
- cc.pendingBlocks = cc.pendingBlocks[1:]
-
- block := cc.pendingBlocks[0]
+ block, cc.pendingBlocks = cc.pendingBlocks[0], cc.pendingBlocks[1:]
block.Finalization.ParentHash = prevBlock.Hash
block.Finalization.Height = prevBlock.Finalization.Height + 1
if block.Position.Round != 0 {
@@ -165,9 +159,7 @@ func (cc *compactionChain) extractBlocks() []*types.Block {
deliveringBlocks = append(deliveringBlocks, block)
prevBlock = block
}
-
cc.prevBlock = prevBlock
-
return deliveringBlocks
}
@@ -190,7 +182,7 @@ func (cc *compactionChain) verifyRandomness(
}
func (cc *compactionChain) processFinalizedBlock(block *types.Block) error {
- if block.Finalization.Height <= cc.lastBlock().Finalization.Height {
+ if block.Finalization.Height <= cc.lastDeliveredBlock().Finalization.Height {
return nil
}
// Block of round 0 should not have randomness.
@@ -246,8 +238,20 @@ func (cc *compactionChain) purgePending() {
}
}
-func (cc *compactionChain) lastBlock() *types.Block {
+// lastDeliveredBlock returns the last delivered block, or the one used to
+// initialize this module.
+func (cc *compactionChain) lastDeliveredBlock() *types.Block {
cc.lock.RLock()
defer cc.lock.RUnlock()
return cc.prevBlock
}
+
+// lastPendingBlock returns the last pending block.
+func (cc *compactionChain) lastPendingBlock() *types.Block {
+ cc.lock.RLock()
+ defer cc.lock.RUnlock()
+ if len(cc.pendingBlocks) > 0 {
+ return cc.pendingBlocks[0]
+ }
+ return nil
+}
diff --git a/core/compaction-chain_test.go b/core/compaction-chain_test.go
index 6c9022a..2e2d518 100644
--- a/core/compaction-chain_test.go
+++ b/core/compaction-chain_test.go
@@ -89,7 +89,7 @@ func (s *CompactionChainTestSuite) TestProcessBlock() {
for _, block := range blocks {
s.Require().NoError(cc.processBlock(block))
}
- s.Len(cc.pendingBlocks, len(blocks)+1)
+ s.Len(cc.pendingBlocks, len(blocks))
}
func (s *CompactionChainTestSuite) TestExtractBlocks() {
diff --git a/core/consensus.go b/core/consensus.go
index a1aa072..bfe893c 100644
--- a/core/consensus.go
+++ b/core/consensus.go
@@ -1018,8 +1018,9 @@ func (con *Consensus) processBlock(block *types.Block) (err error) {
go con.event.NotifyTime(b.Finalization.Timestamp)
}
deliveredBlocks = con.ccModule.extractBlocks()
- con.logger.Debug("Last block in compaction chain",
- "block", con.ccModule.lastBlock())
+ con.logger.Debug("Last blocks in compaction chain",
+ "delivered", con.ccModule.lastDeliveredBlock(),
+ "pending", con.ccModule.lastPendingBlock())
for _, b := range deliveredBlocks {
if err = con.db.Update(*b); err != nil {
panic(err)