diff options
author | Wei-Ning Huang <w@dexon.org> | 2018-09-27 14:27:18 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-27 14:27:18 +0800 |
commit | 506014f22faba150cd7197e7dd817e72a914dc84 (patch) | |
tree | 3f61287a4578c31c447cc8d9283fc3c728dd6cb7 | |
parent | 9c2f700be09c27e8a8d12fc5bc0ccd017d408a32 (diff) | |
download | dexon-consensus-506014f22faba150cd7197e7dd817e72a914dc84.tar.gz dexon-consensus-506014f22faba150cd7197e7dd817e72a914dc84.tar.zst dexon-consensus-506014f22faba150cd7197e7dd817e72a914dc84.zip |
core: update governance interface and config (#145)
1) Remove RoundHeight from config.
2) NotarySet is not from governance contract, we get Node set intead.
Notary set is caculated from the NodeSet using CRS.
3) CRS is not in the governance interface.
-rw-r--r-- | core/consensus.go | 17 | ||||
-rw-r--r-- | core/consensus_test.go | 6 | ||||
-rw-r--r-- | core/interfaces.go | 16 | ||||
-rw-r--r-- | core/test/governance.go | 22 | ||||
-rw-r--r-- | core/types/config.go | 11 | ||||
-rw-r--r-- | integration_test/node.go | 2 | ||||
-rw-r--r-- | integration_test/utils.go | 4 | ||||
-rw-r--r-- | simulation/config/config.go | 26 | ||||
-rw-r--r-- | simulation/governance.go | 14 |
9 files changed, 61 insertions, 57 deletions
diff --git a/core/consensus.go b/core/consensus.go index f2b945b..d7dc3a4 100644 --- a/core/consensus.go +++ b/core/consensus.go @@ -211,10 +211,15 @@ func NewConsensus( network Network, prv crypto.PrivateKey) *Consensus { - // TODO(w): load latest blockHeight from DB, and use config at that height. - var blockHeight uint64 - config := gov.GetConfiguration(blockHeight) - notarySet := gov.GetNotarySet(blockHeight) + // TODO(w): load latest round from DB. + var round uint64 + config := gov.GetConfiguration(round) + + // TODO(w): notarySet is different for each chain, need to write a + // GetNotarySetForChain(nodeSet, shardID, chainID, crs) function to get the + // correct notary set for a given chain. + notarySet := gov.GetNodeSet(round) + crs := gov.GetCRS(round) ID := types.NewNodeID(prv.PublicKey()) @@ -290,7 +295,7 @@ func NewConsensus( con.ID, con.receivers[chainID], nodes, - newGenesisLeaderSelector(config.CRS), + newGenesisLeaderSelector(crs), blockProposer, ) } @@ -386,7 +391,7 @@ func (con *Consensus) runDKGTSIG() { panic(err) } hash := HashConfigurationBlock( - con.gov.GetNotarySet(0), + con.gov.GetNodeSet(0), con.gov.GetConfiguration(0), common.Hash{}, con.cfgModule.prevHash) diff --git a/core/consensus_test.go b/core/consensus_test.go index aa755cd..0bb7e8f 100644 --- a/core/consensus_test.go +++ b/core/consensus_test.go @@ -117,7 +117,7 @@ func (s *ConsensusTestSuite) TestSimpleDeliverBlock() { ) s.Require().Nil(err) - for nID := range gov.GetNotarySet(0) { + for nID := range gov.GetNodeSet(0) { nodes = append(nodes, nID) } @@ -333,7 +333,7 @@ func (s *ConsensusTestSuite) TestPrepareBlock() { nodes []types.NodeID ) s.Require().Nil(err) - for nID := range gov.GetNotarySet(0) { + for nID := range gov.GetNodeSet(0) { nodes = append(nodes, nID) } // Setup core.Consensus and test.App. @@ -380,7 +380,7 @@ func (s *ConsensusTestSuite) TestPrepareGenesisBlock() { nodes []types.NodeID ) s.Require().Nil(err) - for nID := range gov.GetNotarySet(0) { + for nID := range gov.GetNodeSet(0) { nodes = append(nodes, nID) } _, con := s.prepareConsensus(gov, nodes[0]) diff --git a/core/interfaces.go b/core/interfaces.go index eebf8ff..b0a85ae 100644 --- a/core/interfaces.go +++ b/core/interfaces.go @@ -89,13 +89,17 @@ type Network interface { // Note that there are a lot more methods in the governance contract, that this // interface only define those that are required to run the consensus algorithm. type Governance interface { - // GetConfiguration returns the configuration at a given block height. - // Return the genesis configuration if blockHeight == 0. - GetConfiguration(blockHeight uint64) *types.Config + // GetConfiguration returns the configuration at a given round. + // Return the genesis configuration if round == 0. + GetConfiguration(round uint64) *types.Config - // Get the current notary set. - // Return the genesis notary set if blockHeight == 0. - GetNotarySet(blockHeight uint64) map[types.NodeID]struct{} + // GetCRS returns the CRS for a given round. + // Return the genesis CRS if round == 0. + GetCRS(round uint64) []byte + + // GetNodeSet returns the node set at a given round. + // Return the genesis node set if round == 0. + GetNodeSet(round uint64) map[types.NodeID]struct{} // Porpose a ThresholdSignature of round. ProposeThresholdSignature(round uint64, signature crypto.Signature) diff --git a/core/test/governance.go b/core/test/governance.go index 7eca6f6..bc5f58a 100644 --- a/core/test/governance.go +++ b/core/test/governance.go @@ -37,7 +37,7 @@ var ( type Governance struct { lambdaBA time.Duration lambdaDKG time.Duration - notarySet map[types.NodeID]struct{} + nodeSet map[types.NodeID]struct{} privateKeys map[types.NodeID]crypto.PrivateKey tsig map[uint64]crypto.Signature DKGComplaint map[uint64][]*types.DKGComplaint @@ -51,7 +51,7 @@ func NewGovernance(nodeCount int, lambda time.Duration) ( g = &Governance{ lambdaBA: lambda, lambdaDKG: lambda * 10, - notarySet: make(map[types.NodeID]struct{}), + nodeSet: make(map[types.NodeID]struct{}), privateKeys: make(map[types.NodeID]crypto.PrivateKey), tsig: make(map[uint64]crypto.Signature), DKGComplaint: make(map[uint64][]*types.DKGComplaint), @@ -63,30 +63,29 @@ func NewGovernance(nodeCount int, lambda time.Duration) ( return nil, err } nID := types.NewNodeID(prv.PublicKey()) - g.notarySet[nID] = struct{}{} + g.nodeSet[nID] = struct{}{} g.privateKeys[nID] = prv } return } -// GetNotarySet implements Governance interface to return current +// GetNodeSet implements Governance interface to return current // notary set. -func (g *Governance) GetNotarySet(blockHeight uint64) ( +func (g *Governance) GetNodeSet(round uint64) ( ret map[types.NodeID]struct{}) { // Return a cloned map. ret = make(map[types.NodeID]struct{}) - for k := range g.notarySet { + for k := range g.nodeSet { ret[k] = struct{}{} } return } // GetConfiguration returns the configuration at a given block height. -func (g *Governance) GetConfiguration(blockHeight uint64) *types.Config { +func (g *Governance) GetConfiguration(round uint64) *types.Config { return &types.Config{ - CRS: []byte("__ DEXON"), NumShards: 1, - NumChains: uint32(len(g.notarySet)), + NumChains: uint32(len(g.nodeSet)), LambdaBA: g.lambdaBA, LambdaDKG: g.lambdaDKG, K: 0, @@ -94,6 +93,11 @@ func (g *Governance) GetConfiguration(blockHeight uint64) *types.Config { } } +// GetCRS returns the CRS for a given round. +func (g *Governance) GetCRS(round uint64) []byte { + return []byte("__ DEXON") +} + // GetPrivateKey return the private key for that node, this function // is a test utility and not a general Governance interface. func (g *Governance) GetPrivateKey( diff --git a/core/types/config.go b/core/types/config.go index 034853e..9434d62 100644 --- a/core/types/config.go +++ b/core/types/config.go @@ -25,9 +25,6 @@ import ( // Config stands for Current Configuration Parameters. type Config struct { - // CRS. - CRS []byte - // Network related. NumShards uint32 NumChains uint32 @@ -39,9 +36,6 @@ type Config struct { // Total ordering related. K int PhiRatio float32 - - // Confiuration chain related. - RoundHeight uint64 } // Bytes returns []byte representation of Config. @@ -63,9 +57,6 @@ func (c *Config) Bytes() []byte { binaryPhiRatio := make([]byte, 4) binary.LittleEndian.PutUint32(binaryPhiRatio, math.Float32bits(c.PhiRatio)) - binaryRoundHeight := make([]byte, 8) - binary.LittleEndian.PutUint64(binaryRoundHeight, c.RoundHeight) - enc := make([]byte, 0, 40) enc = append(enc, binaryNumShards...) enc = append(enc, binaryNumChains...) @@ -73,7 +64,5 @@ func (c *Config) Bytes() []byte { enc = append(enc, binaryLambdaDKG...) enc = append(enc, binaryK...) enc = append(enc, binaryPhiRatio...) - enc = append(enc, binaryRoundHeight...) - return enc } diff --git a/integration_test/node.go b/integration_test/node.go index 715331e..d66a86a 100644 --- a/integration_test/node.go +++ b/integration_test/node.go @@ -88,7 +88,7 @@ func NewNode( shardID = uint32(0) chainID = uint32(math.MaxUint32) governanceConfig = gov.GetConfiguration(0) - broadcastTargets = gov.GetNotarySet(0) + broadcastTargets = gov.GetNodeSet(0) nodeID = types.NewNodeID(privateKey.PublicKey()) ) hashes := common.Hashes{} diff --git a/integration_test/utils.go b/integration_test/utils.go index 5b414c5..07d41b6 100644 --- a/integration_test/utils.go +++ b/integration_test/utils.go @@ -31,7 +31,7 @@ func PrepareNodes( if err != nil { return } - for nID := range gov.GetNotarySet(0) { + for nID := range gov.GetNodeSet(0) { apps[nID] = test.NewApp() if db, err = blockdb.NewMemBackedBlockDB(); err != nil { @@ -39,7 +39,7 @@ func PrepareNodes( } dbs[nID] = db } - for nID := range gov.GetNotarySet(0) { + for nID := range gov.GetNodeSet(0) { if key, err = gov.GetPrivateKey(nID); err != nil { return } diff --git a/simulation/config/config.go b/simulation/config/config.go index cc31a5b..8bc4ab7 100644 --- a/simulation/config/config.go +++ b/simulation/config/config.go @@ -36,13 +36,12 @@ const ( // Consensus settings. type Consensus struct { - PhiRatio float32 - K int - ChainNum uint32 - GenesisCRS string `toml:"genesis_crs"` - LambdaBA int `toml:"lambda_ba"` - LambdaDKG int `toml:"lambda_dkg"` - RoundHeight uint64 + PhiRatio float32 + K int + ChainNum uint32 + GenesisCRS string `toml:"genesis_crs"` + LambdaBA int `toml:"lambda_ba"` + LambdaDKG int `toml:"lambda_dkg"` } // Legacy config. @@ -94,13 +93,12 @@ func GenerateDefault(path string) error { Title: "DEXON Consensus Simulation Config", Node: Node{ Consensus: Consensus{ - PhiRatio: float32(2) / 3, - K: 1, - ChainNum: 7, - GenesisCRS: "In DEXON we trust.", - LambdaBA: 250, - LambdaDKG: 1000, - RoundHeight: 60, + PhiRatio: float32(2) / 3, + K: 1, + ChainNum: 7, + GenesisCRS: "In DEXON we trust.", + LambdaBA: 250, + LambdaDKG: 1000, }, Legacy: Legacy{ ProposeIntervalMean: 500, diff --git a/simulation/governance.go b/simulation/governance.go index a48e221..eb0f2a2 100644 --- a/simulation/governance.go +++ b/simulation/governance.go @@ -70,8 +70,8 @@ func (g *simGovernance) setNetwork(network *network) { g.network = network } -// GetNotarySet returns the current notary set. -func (g *simGovernance) GetNotarySet( +// GetNodeSet returns the current notary set. +func (g *simGovernance) GetNodeSet( blockHeight uint64) map[types.NodeID]struct{} { g.lock.RLock() defer g.lock.RUnlock() @@ -84,12 +84,11 @@ func (g *simGovernance) GetNotarySet( return ret } -// GetConfiguration returns the configuration at a given block height. -func (g *simGovernance) GetConfiguration(blockHeight uint64) *types.Config { +// GetConfiguration returns the configuration at a given round. +func (g *simGovernance) GetConfiguration(round uint64) *types.Config { return &types.Config{ NumShards: 1, NumChains: g.chainNum, - CRS: g.crs, LambdaBA: g.lambdaBA, LambdaDKG: g.lambdaDKG, K: g.k, @@ -97,6 +96,11 @@ func (g *simGovernance) GetConfiguration(blockHeight uint64) *types.Config { } } +// GetCRS returns the CRS for a given round. +func (g *simGovernance) GetCRS(round uint64) []byte { + return g.crs +} + // addNode add a new node into the simulated governance contract. func (g *simGovernance) addNode(nID types.NodeID) { g.lock.Lock() |