From 874c4c599a80b9c6f9c085c216be5fd6492cd2c2 Mon Sep 17 00:00:00 2001 From: Mission Liao Date: Mon, 17 Sep 2018 17:16:26 +0800 Subject: cleanup (#109) - With context, we don't need stopChan - Remove core.BlockChain. - Remove unused variable. --- core/blockchain.go | 26 ------------------------- core/consensus.go | 51 +++++++++++++++++++++++-------------------------- simulation/validator.go | 33 +++++++++++++++----------------- 3 files changed, 39 insertions(+), 71 deletions(-) delete mode 100644 core/blockchain.go diff --git a/core/blockchain.go b/core/blockchain.go deleted file mode 100644 index 1222516..0000000 --- a/core/blockchain.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2018 The dexon-consensus-core Authors -// This file is part of the dexon-consensus-core library. -// -// The dexon-consensus-core library is free software: you can redistribute it -// and/or modify it under the terms of the GNU Lesser General Public License as -// published by the Free Software Foundation, either version 3 of the License, -// or (at your option) any later version. -// -// The dexon-consensus-core library is distributed in the hope that it will be -// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser -// General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the dexon-consensus-core library. If not, see -// . - -package core - -import "github.com/dexon-foundation/dexon-consensus-core/core/types" - -// BlockChain is the basic datastructure used for storing blocks in each -// validator. -type BlockChain struct { - validatorID types.ValidatorID -} diff --git a/core/consensus.go b/core/consensus.go index 85b6e38..4b92994 100644 --- a/core/consensus.go +++ b/core/consensus.go @@ -128,7 +128,8 @@ type Consensus struct { prvKey crypto.PrivateKey sigToPub SigToPubFn lock sync.RWMutex - stopChan chan struct{} + ctx context.Context + ctxCancel context.CancelFunc } // NewConsensus construct an Consensus instance. @@ -148,6 +149,8 @@ func NewConsensus( for vID := range validatorSet { rb.addValidator(vID) } + // Setup context. + ctx, ctxCancel := context.WithCancel(context.Background()) // Setup sequencer by information returned from Governace. var validators types.ValidatorIDs @@ -160,19 +163,20 @@ func NewConsensus( gov.GetChainNumber()) con := &Consensus{ - ID: types.NewValidatorID(prv.PublicKey()), - rbModule: rb, - toModule: to, - ctModule: newConsensusTimestamp(), - ccModule: newCompactionChain(db, sigToPub), - app: newNonBlockingApplication(app), - gov: gov, - db: db, - network: network, - tick: tick, - prvKey: prv, - sigToPub: sigToPub, - stopChan: make(chan struct{}), + ID: types.NewValidatorID(prv.PublicKey()), + rbModule: rb, + toModule: to, + ctModule: newConsensusTimestamp(), + ccModule: newCompactionChain(db, sigToPub), + app: newNonBlockingApplication(app), + gov: gov, + db: db, + network: network, + tick: tick, + prvKey: prv, + sigToPub: sigToPub, + ctx: ctx, + ctxCancel: ctxCancel, } con.baModules = make([]*agreement, con.gov.GetChainNumber()) @@ -204,17 +208,12 @@ func NewConsensus( // Run starts running DEXON Consensus. func (con *Consensus) Run() { - ctx, cancel := context.WithCancel(context.Background()) ticks := make([]chan struct{}, 0, con.gov.GetChainNumber()) for i := uint32(0); i < con.gov.GetChainNumber(); i++ { tick := make(chan struct{}) ticks = append(ticks, tick) - go con.runBA(ctx, i, tick) + go con.runBA(i, tick) } - go func() { - <-con.stopChan - cancel() - }() go con.processMsg(con.network.ReceiveChan(), con.PreProcessBlock) // Reset ticker. <-con.tick.C @@ -227,8 +226,7 @@ func (con *Consensus) Run() { } } -func (con *Consensus) runBA( - ctx context.Context, chainID uint32, tick <-chan struct{}) { +func (con *Consensus) runBA(chainID uint32, tick <-chan struct{}) { // TODO(jimmy-dexon): move this function inside agreement. validatorSet := con.gov.GetValidatorSet() validators := make(types.ValidatorIDs, 0, len(validatorSet)) @@ -243,7 +241,7 @@ func (con *Consensus) runBA( BALoop: for { select { - case <-ctx.Done(): + case <-con.ctx.Done(): break BALoop default: } @@ -305,7 +303,7 @@ ProposingBlockLoop: for { select { case <-con.tick.C: - case <-con.stopChan: + case <-con.ctx.Done(): break ProposingBlockLoop } block := &types.Block{ @@ -326,8 +324,7 @@ ProposingBlockLoop: // Stop the Consensus core. func (con *Consensus) Stop() { - con.stopChan <- struct{}{} - con.stopChan <- struct{}{} + con.ctxCancel() } func (con *Consensus) processMsg( @@ -337,7 +334,7 @@ func (con *Consensus) processMsg( var msg interface{} select { case msg = <-msgChan: - case <-con.stopChan: + case <-con.ctx.Done(): return } diff --git a/simulation/validator.go b/simulation/validator.go index 46d42d3..483912b 100644 --- a/simulation/validator.go +++ b/simulation/validator.go @@ -36,16 +36,14 @@ type validator struct { gov *simGovernance db blockdb.BlockDatabase - config config.Validator - netModule *network - isFinished chan struct{} + config config.Validator + netModule *network - ID types.ValidatorID - chainID uint64 - prvKey crypto.PrivateKey - sigToPub core.SigToPubFn - consensus *core.Consensus - compactionChain *core.BlockChain + ID types.ValidatorID + chainID uint64 + prvKey crypto.PrivateKey + sigToPub core.SigToPubFn + consensus *core.Consensus } // newValidator returns a new empty validator. @@ -63,15 +61,14 @@ func newValidator( } gov := newSimGovernance(config.Validator.Num, config.Validator.Consensus) return &validator{ - ID: id, - prvKey: prvKey, - sigToPub: sigToPub, - config: config.Validator, - app: newSimApp(id, netModule), - gov: gov, - db: db, - netModule: netModule, - isFinished: make(chan struct{}), + ID: id, + prvKey: prvKey, + sigToPub: sigToPub, + config: config.Validator, + app: newSimApp(id, netModule), + gov: gov, + db: db, + netModule: netModule, } } -- cgit v1.2.3