aboutsummaryrefslogtreecommitdiffstats
path: root/simulation/governance.go
diff options
context:
space:
mode:
authorWei-Ning Huang <w@dexon.org>2018-09-18 17:57:36 +0800
committerGitHub <noreply@github.com>2018-09-18 17:57:36 +0800
commit8c33027b943e08de21b7bddb82fecc2b2a5664a2 (patch)
treeeaa282b1238eb609d710e9c45bb0689e7a399039 /simulation/governance.go
parent476c759f5e2ea3949cf3aaf4e60ccc7e0439ee73 (diff)
downloaddexon-consensus-8c33027b943e08de21b7bddb82fecc2b2a5664a2.tar
dexon-consensus-8c33027b943e08de21b7bddb82fecc2b2a5664a2.tar.gz
dexon-consensus-8c33027b943e08de21b7bddb82fecc2b2a5664a2.tar.bz2
dexon-consensus-8c33027b943e08de21b7bddb82fecc2b2a5664a2.tar.lz
dexon-consensus-8c33027b943e08de21b7bddb82fecc2b2a5664a2.tar.xz
dexon-consensus-8c33027b943e08de21b7bddb82fecc2b2a5664a2.tar.zst
dexon-consensus-8c33027b943e08de21b7bddb82fecc2b2a5664a2.zip
core: refine governance interface to reduce Get* methods (#114)
Since we have a bunch of static configurations in the governance contract, instead of using a Get* method for each of them, we instead implement a GetConfiguration() method to return a structure of the configurations.
Diffstat (limited to 'simulation/governance.go')
-rw-r--r--simulation/governance.go80
1 files changed, 24 insertions, 56 deletions
diff --git a/simulation/governance.go b/simulation/governance.go
index 79c9119..ebfd32b 100644
--- a/simulation/governance.go
+++ b/simulation/governance.go
@@ -24,14 +24,13 @@ import (
"github.com/dexon-foundation/dexon-consensus-core/core/types"
"github.com/dexon-foundation/dexon-consensus-core/simulation/config"
- "github.com/shopspring/decimal"
)
// simGovernance is a simulated governance contract implementing the
// core.Governance interface.
type simGovernance struct {
lock sync.RWMutex
- validatorSet map[types.ValidatorID]decimal.Decimal
+ notarySet map[types.ValidatorID]struct{}
expectedNumValidators int
k int
phiRatio float32
@@ -46,7 +45,7 @@ type simGovernance struct {
func newSimGovernance(
numValidators int, consensusConfig config.Consensus) *simGovernance {
return &simGovernance{
- validatorSet: make(map[types.ValidatorID]decimal.Decimal),
+ notarySet: make(map[types.ValidatorID]struct{}),
expectedNumValidators: numValidators,
k: consensusConfig.K,
phiRatio: consensusConfig.PhiRatio,
@@ -58,61 +57,30 @@ func newSimGovernance(
}
}
-// GetValidatorSet returns the validator set.
-func (g *simGovernance) GetValidatorSet() (
- ret map[types.ValidatorID]decimal.Decimal) {
+// GetNotarySet returns the current notary set.
+func (g *simGovernance) GetNotarySet() map[types.ValidatorID]struct{} {
g.lock.RLock()
defer g.lock.RUnlock()
- // Return the cloned validatorSet.
- ret = make(map[types.ValidatorID]decimal.Decimal)
- for k, v := range g.validatorSet {
- ret[k] = v
+ // Return the cloned notarySet.
+ ret := make(map[types.ValidatorID]struct{})
+ for k := range g.notarySet {
+ ret[k] = struct{}{}
+ }
+ return ret
+}
+
+// GetConfiguration returns the configuration at a given block height.
+func (g *simGovernance) GetConfiguration(blockHeight uint64) *types.Config {
+ return &types.Config{
+ NumShards: 1,
+ NumChains: g.chainNum,
+ GenesisCRS: g.crs,
+ Lambda: g.lambda,
+ K: g.k,
+ PhiRatio: g.phiRatio,
}
- return
-}
-
-// GetTotalOrderingK returns K.
-func (g *simGovernance) GetTotalOrderingK() int {
- return g.k
-}
-
-// GetTotalOrderingK returns PhiRatio.
-func (g *simGovernance) GetPhiRatio() float32 {
- return g.phiRatio
-}
-
-// GetBlockProposingInterval returns block proposing interval.
-func (g *simGovernance) GetBlockProposingInterval() int {
- return 0
-}
-
-// GetNumShards returns number of shards.
-func (g *simGovernance) GetNumShards() uint32 {
- return 1
-}
-
-// GetNumChains returns number of chains.
-func (g *simGovernance) GetNumChains() uint32 {
- return g.chainNum
-}
-
-// GetConfigurationChangeEvent returns configuration change event since last
-// epoch.
-func (g *simGovernance) GetConfigurationChangeEvent(
- epoch int) []types.ConfigurationChangeEvent {
- return nil
-}
-
-// GetGenesisCRS returns CRS.
-func (g *simGovernance) GetGenesisCRS() string {
- return g.crs
-}
-
-// GetLambda return lambda for BA.
-func (g *simGovernance) GetLambda() time.Duration {
- return g.lambda
}
// addValidator add a new validator into the simulated governance contract.
@@ -120,13 +88,13 @@ func (g *simGovernance) addValidator(vID types.ValidatorID) {
g.lock.Lock()
defer g.lock.Unlock()
- if _, exists := g.validatorSet[vID]; exists {
+ if _, exists := g.notarySet[vID]; exists {
return
}
- if len(g.validatorSet) == g.expectedNumValidators {
+ if len(g.notarySet) == g.expectedNumValidators {
panic(fmt.Errorf("attempt to add validator when ready"))
}
- g.validatorSet[vID] = decimal.NewFromFloat(0)
+ g.notarySet[vID] = struct{}{}
}
// AddDKGComplaint adds a DKGComplaint.