diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2019-03-10 17:59:17 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-10 17:59:17 +0800 |
commit | 4345050093710739c9e417956bc3a8339e7d99a6 (patch) | |
tree | 24fe4e21b95605f8854f6be073a762ece294d479 | |
parent | ac3187e706bc11f1b36e32015a6e51a96cc8cfe9 (diff) | |
download | dexon-consensus-4345050093710739c9e417956bc3a8339e7d99a6.tar dexon-consensus-4345050093710739c9e417956bc3a8339e7d99a6.tar.gz dexon-consensus-4345050093710739c9e417956bc3a8339e7d99a6.tar.bz2 dexon-consensus-4345050093710739c9e417956bc3a8339e7d99a6.tar.lz dexon-consensus-4345050093710739c9e417956bc3a8339e7d99a6.tar.xz dexon-consensus-4345050093710739c9e417956bc3a8339e7d99a6.tar.zst dexon-consensus-4345050093710739c9e417956bc3a8339e7d99a6.zip |
core: reduce blockrandomness message (#477)
* core: reduce blockchain randomness msg
* add test
-rw-r--r-- | core/blockchain.go | 30 | ||||
-rw-r--r-- | core/blockchain_test.go | 25 | ||||
-rw-r--r-- | core/consensus.go | 3 |
3 files changed, 44 insertions, 14 deletions
diff --git a/core/blockchain.go b/core/blockchain.go index aacb65d..b6c8b1d 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -311,21 +311,23 @@ func (bc *blockChain) addBlock(b *types.Block) error { return nil } +func (bc *blockChain) shouldAddRandomness(r *types.BlockRandomnessResult) bool { + bc.lock.RLock() + defer bc.lock.RUnlock() + if bc.lastDelivered != nil && + bc.lastDelivered.Position.Newer(r.Position) { + return false + } + _, exists := bc.pendingRandomnesses[r.Position] + if exists { + return false + } + b := bc.findPendingBlock(r.Position) + return b == nil || len(b.Finalization.Randomness) == 0 +} + func (bc *blockChain) addRandomness(r *types.BlockRandomnessResult) error { - if func() bool { - bc.lock.RLock() - defer bc.lock.RUnlock() - if bc.lastDelivered != nil && - bc.lastDelivered.Position.Newer(r.Position) { - return true - } - _, exists := bc.pendingRandomnesses[r.Position] - if exists { - return true - } - b := bc.findPendingBlock(r.Position) - return b != nil && len(b.Finalization.Randomness) > 0 - }() { + if !bc.shouldAddRandomness(r) { return nil } ok, err := bc.verifyRandomness(r.BlockHash, r.Position.Round, r.Randomness) diff --git a/core/blockchain_test.go b/core/blockchain_test.go index 991dc94..46e7630 100644 --- a/core/blockchain_test.go +++ b/core/blockchain_test.go @@ -495,6 +495,31 @@ func (s *BlockChainTestSuite) TestAddEmptyBlockDirectly() { s.Require().NotNil(rec.block) } +func (s *BlockChainTestSuite) TestShouldAddRandomness() { + initBlock := s.newRoundOneInitBlock() + bc := s.newBlockChain(initBlock, 10) + blocks := s.newBlocks(2, initBlock) + b0, b1 := blocks[0], blocks[1] + r0 := s.newRandomnessFromBlock(b0) + r1 := s.newRandomnessFromBlock(b1) + + // If a block is extracted, the randomness should not be added. + s.Require().NoError(bc.addBlock(b0)) + s.True(bc.shouldAddRandomness(r0)) + s.Require().NoError(bc.addRandomness(r0)) + s.False(bc.shouldAddRandomness(r0)) + s.Require().Len(bc.extractBlocks(), 1) + s.Require().Equal(b0.Hash, bc.lastDelivered.Hash) + + // If a block has already have randomness, it should not be added. + s.True(bc.shouldAddRandomness(r1)) + s.Require().NoError(bc.addRandomness(r1)) + s.Require().Len(bc.pendingRandomnesses, 1) + s.False(bc.shouldAddRandomness(r1)) + s.Require().NoError(bc.addBlock(b1)) + s.False(bc.shouldAddRandomness(r1)) +} + func TestBlockChain(t *testing.T) { suite.Run(t, new(BlockChainTestSuite)) } diff --git a/core/consensus.go b/core/consensus.go index 050bfe7..5945406 100644 --- a/core/consensus.go +++ b/core/consensus.go @@ -1070,6 +1070,9 @@ func (con *Consensus) ProcessBlockRandomnessResult( if rand.Position.Round == 0 { return nil } + if !con.bcModule.shouldAddRandomness(rand) { + return nil + } if err := con.bcModule.addRandomness(rand); err != nil { return err } |