aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorWei-Ning Huang <w@dexon.org>2018-08-10 15:12:56 +0800
committerJimmy Hu <jimmy.hu@dexon.org>2018-08-10 15:12:56 +0800
commit09a0ab086cdafcb27b74e6346efdc8e96ca8145d (patch)
treec14bda316a48c94e5300b5799f152a82f0594558 /core
parent03917cdfa1f762849541e1bed31394340ed1f957 (diff)
downloaddexon-consensus-09a0ab086cdafcb27b74e6346efdc8e96ca8145d.tar
dexon-consensus-09a0ab086cdafcb27b74e6346efdc8e96ca8145d.tar.gz
dexon-consensus-09a0ab086cdafcb27b74e6346efdc8e96ca8145d.tar.bz2
dexon-consensus-09a0ab086cdafcb27b74e6346efdc8e96ca8145d.tar.lz
dexon-consensus-09a0ab086cdafcb27b74e6346efdc8e96ca8145d.tar.xz
dexon-consensus-09a0ab086cdafcb27b74e6346efdc8e96ca8145d.tar.zst
dexon-consensus-09a0ab086cdafcb27b74e6346efdc8e96ca8145d.zip
core: update governance interface and move K into config (#40)
Diffstat (limited to 'core')
-rw-r--r--core/consensus.go7
-rw-r--r--core/consensus_test.go6
-rw-r--r--core/governance.go10
-rw-r--r--core/test/governance.go (renamed from core/test/gov.go)38
-rw-r--r--core/types/configuration.go46
-rw-r--r--core/types/membership-event.go16
6 files changed, 83 insertions, 40 deletions
diff --git a/core/consensus.go b/core/consensus.go
index 33f2f8b..d668c9e 100644
--- a/core/consensus.go
+++ b/core/consensus.go
@@ -42,7 +42,6 @@ func NewConsensus(
app Application,
gov Governance,
db blockdb.BlockDatabase) *Consensus {
-
validatorSet := gov.GetValidatorSet()
// Setup acking by information returned from Governace.
@@ -52,11 +51,9 @@ func NewConsensus(
}
// Setup sequencer by information returned from Governace.
- // TODO(mission): the value of 'K' should be in governace.
- // TODO(mission): the ratio of 'phi' should be in governance.
to := newTotalOrdering(
- 0,
- uint64(2*(len(validatorSet)-1)/3+1),
+ uint64(gov.GetTotalOrderingK()),
+ uint64(float32(len(validatorSet)-1)*gov.GetPhiRatio()+1),
uint64(len(validatorSet)))
return &Consensus{
diff --git a/core/consensus_test.go b/core/consensus_test.go
index 1544818..beeb1a7 100644
--- a/core/consensus_test.go
+++ b/core/consensus_test.go
@@ -53,7 +53,7 @@ func (s *ConsensusTestSuite) prepareGenesisBlock(
return block
}
-func (s *ConsensusTestSuite) prepareConsensus(gov *test.Gov) (
+func (s *ConsensusTestSuite) prepareConsensus(gov *test.Governance) (
*test.App, *Consensus) {
app := test.NewApp()
@@ -77,7 +77,7 @@ func (s *ConsensusTestSuite) TestSimpleDeliverBlock() {
// This test case only works for Total Ordering with K=0.
var (
minInterval = 50 * time.Millisecond
- gov = test.NewGov(4, 1000)
+ gov = test.NewGovernance(4, 1000)
req = s.Require()
validators []types.ValidatorID
)
@@ -262,7 +262,7 @@ func (s *ConsensusTestSuite) TestPrepareBlock() {
// - Make sure Consensus.PrepareBlock would only attempt to
// ack the prepared block.
var (
- gov = test.NewGov(4, 1000)
+ gov = test.NewGovernance(4, 1000)
req = s.Require()
validators []types.ValidatorID
)
diff --git a/core/governance.go b/core/governance.go
index a7069a5..d94fad0 100644
--- a/core/governance.go
+++ b/core/governance.go
@@ -30,9 +30,15 @@ type Governance interface {
// Get the current validator set and it's corresponding stake.
GetValidatorSet() map[types.ValidatorID]decimal.Decimal
+ // Get K
+ GetTotalOrderingK() int
+
+ // Get PhiRatio
+ GetPhiRatio() float32
+
// Get block proposing interval (in milliseconds).
GetBlockProposingInterval() int
- // Get membership events after a certain epoch.
- GetMembershipEvents(epoch int) []types.MembershipEvent
+ // Get configuration change events after a certain epoch.
+ GetConfigurationChangeEvent(epoch int) []types.ConfigurationChangeEvent
}
diff --git a/core/test/gov.go b/core/test/governance.go
index 7a5bdfc..a7ae1b0 100644
--- a/core/test/gov.go
+++ b/core/test/governance.go
@@ -23,22 +23,21 @@ import (
"github.com/shopspring/decimal"
)
-// Gov is an implementation of Goverance for testing purpose.
-type Gov struct {
+// Governance is an implementation of Goverance for testing purpose.
+type Governance struct {
BlockProposingInterval int
Validators map[types.ValidatorID]decimal.Decimal
}
-// NewGov constructs a Gov instance.
-func NewGov(
- validatorCount, proposingInterval int) (gov *Gov) {
+// NewGovernance constructs a Governance instance.
+func NewGovernance(validatorCount, proposingInterval int) (g *Governance) {
- gov = &Gov{
+ g = &Governance{
BlockProposingInterval: proposingInterval,
Validators: make(map[types.ValidatorID]decimal.Decimal),
}
for i := 0; i < validatorCount; i++ {
- gov.Validators[types.ValidatorID{Hash: common.NewRandomHash()}] =
+ g.Validators[types.ValidatorID{Hash: common.NewRandomHash()}] =
decimal.NewFromFloat(0)
}
return
@@ -46,18 +45,29 @@ func NewGov(
// GetValidatorSet implements Governance interface to return current
// validator set.
-func (gov *Gov) GetValidatorSet() map[types.ValidatorID]decimal.Decimal {
- return gov.Validators
+func (g *Governance) GetValidatorSet() map[types.ValidatorID]decimal.Decimal {
+ return g.Validators
}
// GetBlockProposingInterval implements Governance interface to return maximum
// allowed block proposing interval in millisecond.
-func (gov *Gov) GetBlockProposingInterval() int {
- return gov.BlockProposingInterval
+func (g *Governance) GetBlockProposingInterval() int {
+ return g.BlockProposingInterval
}
-// GetMembershipEvents implements Governance interface to return membership
-// changed events.
-func (gov *Gov) GetMembershipEvents(epoch int) []types.MembershipEvent {
+// GetTotalOrderingK returns K.
+func (g *Governance) GetTotalOrderingK() int {
+ return 0
+}
+
+// GetPhiRatio returns phi ratio.
+func (g *Governance) GetPhiRatio() float32 {
+ return 0.667
+}
+
+// GetConfigurationChangeEvent Get configuration change events after a certain
+// epoch.
+func (g *Governance) GetConfigurationChangeEvent(
+ epoch int) []types.ConfigurationChangeEvent {
return nil
}
diff --git a/core/types/configuration.go b/core/types/configuration.go
new file mode 100644
index 0000000..4158331
--- /dev/null
+++ b/core/types/configuration.go
@@ -0,0 +1,46 @@
+// 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 types
+
+// ConfigurationChangeEventType specifies the action of a membership event.
+type ConfigurationChangeEventType int
+
+// Event enums.
+const (
+ KChanged ConfigurationChangeEventType = iota
+ MembershipAdd
+ MembershipRemove
+)
+
+// IntegerEventPayload is a general payload for integer type value.
+type IntegerEventPayload struct {
+ Value int64
+}
+
+// MembershipEventPayload is the payload type for membership event.
+type MembershipEventPayload struct {
+ ID ValidatorID
+ Evidence []byte
+}
+
+// ConfigurationChangeEvent specifies the event of membership changes.
+type ConfigurationChangeEvent struct {
+ Epoch int
+ Event ConfigurationChangeEventType
+ Payload interface{}
+}
diff --git a/core/types/membership-event.go b/core/types/membership-event.go
deleted file mode 100644
index 8d05039..0000000
--- a/core/types/membership-event.go
+++ /dev/null
@@ -1,16 +0,0 @@
-package types
-
-// MembershipActionType specifies the action of a membership event.
-type MembershipActionType int
-
-// Event enums.
-const (
- MembershipActionAdd MembershipActionType = iota
- MembershipActionDelete
-)
-
-// MembershipEvent specifies the event of membership changes.
-type MembershipEvent struct {
- Epoch int
- Action MembershipActionType
-}