diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2018-10-17 19:06:40 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-17 19:06:40 +0800 |
commit | 29f38589a29e434282a783433d9fbb565ce4231b (patch) | |
tree | de7c6ffb73580689fc066e98221c45ef0b0694a4 /core/compaction-chain.go | |
parent | 21ab1ac7be6e88b88f75b10eb83d409bc0322254 (diff) | |
download | dexon-consensus-29f38589a29e434282a783433d9fbb565ce4231b.tar dexon-consensus-29f38589a29e434282a783433d9fbb565ce4231b.tar.gz dexon-consensus-29f38589a29e434282a783433d9fbb565ce4231b.tar.bz2 dexon-consensus-29f38589a29e434282a783433d9fbb565ce4231b.tar.lz dexon-consensus-29f38589a29e434282a783433d9fbb565ce4231b.tar.xz dexon-consensus-29f38589a29e434282a783433d9fbb565ce4231b.tar.zst dexon-consensus-29f38589a29e434282a783433d9fbb565ce4231b.zip |
core: Some sync functions (#220)
Diffstat (limited to 'core/compaction-chain.go')
-rw-r--r-- | core/compaction-chain.go | 61 |
1 files changed, 55 insertions, 6 deletions
diff --git a/core/compaction-chain.go b/core/compaction-chain.go index 3bf87f1..ea26562 100644 --- a/core/compaction-chain.go +++ b/core/compaction-chain.go @@ -22,6 +22,7 @@ import ( "sync" "github.com/dexon-foundation/dexon-consensus-core/common" + "github.com/dexon-foundation/dexon-consensus-core/core/crypto" "github.com/dexon-foundation/dexon-consensus-core/core/types" ) @@ -32,15 +33,18 @@ var ( ) type compactionChain struct { - blocks map[common.Hash]*types.Block - pendingBlocks []*types.Block - blocksLock sync.RWMutex - prevBlockLock sync.RWMutex - prevBlock *types.Block + gov Governance + blocks map[common.Hash]*types.Block + pendingBlocks []*types.Block + pendingFinalizedBlocks []*types.Block + blocksLock sync.RWMutex + prevBlockLock sync.RWMutex + prevBlock *types.Block } -func newCompactionChain() *compactionChain { +func newCompactionChain(gov Governance) *compactionChain { return &compactionChain{ + gov: gov, blocks: make(map[common.Hash]*types.Block), } } @@ -77,6 +81,51 @@ func (cc *compactionChain) processBlock(block *types.Block) error { return nil } +func (cc *compactionChain) processFinalizedBlock(block *types.Block) ( + []*types.Block, error) { + blocks := func() []*types.Block { + cc.blocksLock.Lock() + defer cc.blocksLock.Unlock() + blocks := cc.pendingFinalizedBlocks + cc.pendingFinalizedBlocks = []*types.Block{} + return blocks + }() + threshold := make(map[uint64]int) + gpks := make(map[uint64]*DKGGroupPublicKey) + toPending := []*types.Block{} + confirmed := []*types.Block{} + blocks = append(blocks, block) + for _, b := range blocks { + if !cc.gov.IsDKGFinal(b.Position.Round) { + toPending = append(toPending, b) + continue + } + round := b.Position.Round + if _, exist := gpks[round]; !exist { + threshold[round] = int(cc.gov.Configuration(round).DKGSetSize)/3 + 1 + var err error + gpks[round], err = NewDKGGroupPublicKey( + round, + cc.gov.DKGMasterPublicKeys(round), cc.gov.DKGComplaints(round), + threshold[round]) + if err != nil { + continue + } + } + gpk := gpks[round] + if ok := gpk.VerifySignature(b.Hash, crypto.Signature{ + Type: "bls", + Signature: b.Finalization.Randomness}); !ok { + continue + } + confirmed = append(confirmed, b) + } + cc.blocksLock.Lock() + defer cc.blocksLock.Unlock() + cc.pendingFinalizedBlocks = append(cc.pendingFinalizedBlocks, toPending...) + return confirmed, nil +} + func (cc *compactionChain) extractBlocks() []*types.Block { deliveringBlocks := make([]*types.Block, 0) cc.blocksLock.Lock() |