aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMission Liao <mission.liao@dexon.org>2018-09-17 17:16:26 +0800
committerGitHub <noreply@github.com>2018-09-17 17:16:26 +0800
commit874c4c599a80b9c6f9c085c216be5fd6492cd2c2 (patch)
tree390ff0d0ad11712612b2ee96a1703528c5789773
parent2110dcdae6ef551cedc006649a1ae1f429bcc013 (diff)
downloaddexon-consensus-874c4c599a80b9c6f9c085c216be5fd6492cd2c2.tar
dexon-consensus-874c4c599a80b9c6f9c085c216be5fd6492cd2c2.tar.gz
dexon-consensus-874c4c599a80b9c6f9c085c216be5fd6492cd2c2.tar.bz2
dexon-consensus-874c4c599a80b9c6f9c085c216be5fd6492cd2c2.tar.lz
dexon-consensus-874c4c599a80b9c6f9c085c216be5fd6492cd2c2.tar.xz
dexon-consensus-874c4c599a80b9c6f9c085c216be5fd6492cd2c2.tar.zst
dexon-consensus-874c4c599a80b9c6f9c085c216be5fd6492cd2c2.zip
cleanup (#109)
- With context, we don't need stopChan - Remove core.BlockChain. - Remove unused variable.
-rw-r--r--core/blockchain.go26
-rw-r--r--core/consensus.go51
-rw-r--r--simulation/validator.go33
3 files changed, 39 insertions, 71 deletions
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
-// <http://www.gnu.org/licenses/>.
-
-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,
}
}