diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2018-11-13 15:26:09 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-13 15:26:09 +0800 |
commit | 86838fe70789292de0851f82426e5241c0f0cc96 (patch) | |
tree | 7189ff5417eadc5c61e96136e5204f5689d96a9d | |
parent | 4752fffa3546c2968b310811dbd30281b197032c (diff) | |
download | dexon-consensus-86838fe70789292de0851f82426e5241c0f0cc96.tar dexon-consensus-86838fe70789292de0851f82426e5241c0f0cc96.tar.gz dexon-consensus-86838fe70789292de0851f82426e5241c0f0cc96.tar.bz2 dexon-consensus-86838fe70789292de0851f82426e5241c0f0cc96.tar.lz dexon-consensus-86838fe70789292de0851f82426e5241c0f0cc96.tar.xz dexon-consensus-86838fe70789292de0851f82426e5241c0f0cc96.tar.zst dexon-consensus-86838fe70789292de0851f82426e5241c0f0cc96.zip |
core: Fix data race. (#320)
Finalization.Randomness will be set at output
-rw-r--r-- | core/compaction-chain.go | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/core/compaction-chain.go b/core/compaction-chain.go index 94dd24c..4a5ba36 100644 --- a/core/compaction-chain.go +++ b/core/compaction-chain.go @@ -42,6 +42,7 @@ type compactionChain struct { chainUnsynced uint32 tsigVerifier *TSigVerifierCache blocks map[common.Hash]*types.Block + blockRandomness map[common.Hash][]byte pendingBlocks []*types.Block pendingFinalizedBlocks *finalizedBlockHeap lock sync.RWMutex @@ -55,6 +56,7 @@ func newCompactionChain(gov Governance) *compactionChain { gov: gov, tsigVerifier: NewTSigVerifierCache(gov, 7), blocks: make(map[common.Hash]*types.Block), + blockRandomness: make(map[common.Hash][]byte), pendingFinalizedBlocks: pendingFinalizedBlocks, } } @@ -131,7 +133,7 @@ func (cc *compactionChain) extractBlocks() []*types.Block { defer cc.lock.Unlock() // cc.pendingBlocks[0] will not be popped and will equal to cc.prevBlock. for len(cc.pendingBlocks) > 1 && - (len(cc.pendingBlocks[1].Finalization.Randomness) != 0 || + (len(cc.blockRandomness[cc.pendingBlocks[1].Hash]) != 0 || cc.pendingBlocks[1].Position.Round == 0) { delete(cc.blocks, cc.pendingBlocks[0].Hash) cc.pendingBlocks = cc.pendingBlocks[1:] @@ -139,6 +141,10 @@ func (cc *compactionChain) extractBlocks() []*types.Block { block := cc.pendingBlocks[0] block.Finalization.ParentHash = prevBlock.Hash block.Finalization.Height = prevBlock.Finalization.Height + 1 + if block.Position.Round != 0 { + block.Finalization.Randomness = cc.blockRandomness[block.Hash] + delete(cc.blockRandomness, block.Hash) + } deliveringBlocks = append(deliveringBlocks, block) prevBlock = block } @@ -255,16 +261,12 @@ func (cc *compactionChain) processBlockRandomnessResult( rand *types.BlockRandomnessResult) error { cc.lock.Lock() defer cc.lock.Unlock() - // prevBlock is already delivered so it doesn't need to process randomness. - if rand.BlockHash == cc.prevBlock.Hash { - return nil - } // TODO(jimmy-dexon): the result should not be discarded here. Blocks may // be registered later. if !cc.blockRegisteredNoLock(rand.BlockHash) { return ErrBlockNotRegistered } - cc.blocks[rand.BlockHash].Finalization.Randomness = rand.Randomness + cc.blockRandomness[rand.BlockHash] = rand.Randomness return nil } |