From 0c63646ca8b06bb527737cd6e2a7fe58f169efff Mon Sep 17 00:00:00 2001
From: Jimmy Hu <jimmy.hu@dexon.org>
Date: Wed, 27 Mar 2019 20:47:32 +0800
Subject: core: merge notarySet and DKGSet (#265)

* vendor: sync to latest core

* core: merge notarySet and dkgSet

* dex: optimize network traffic for finalized block
---
 core/governance.go               | 20 ++----------
 core/vm/oracle_contract_abi.go   | 14 --------
 core/vm/oracle_contracts.go      | 69 +++++++++++-----------------------------
 core/vm/oracle_contracts_test.go | 21 ++++--------
 4 files changed, 27 insertions(+), 97 deletions(-)

(limited to 'core')

diff --git a/core/governance.go b/core/governance.go
index 68d5de821..b39fdc3c6 100644
--- a/core/governance.go
+++ b/core/governance.go
@@ -146,7 +146,6 @@ func (g *Governance) Configuration(round uint64) *coreTypes.Config {
 		LambdaBA:         time.Duration(c.LambdaBA) * time.Millisecond,
 		LambdaDKG:        time.Duration(c.LambdaDKG) * time.Millisecond,
 		NotarySetSize:    c.NotarySetSize,
-		DKGSetSize:       c.DKGSetSize,
 		RoundLength:      c.RoundLength,
 		MinBlockInterval: time.Duration(c.MinBlockInterval) * time.Millisecond,
 	}
@@ -199,21 +198,6 @@ func (d *Governance) NotarySetNodeKeyAddresses(round uint64) (map[common.Address
 	return r, nil
 }
 
-func (d *Governance) DKGSet(round uint64) (map[string]struct{}, error) {
-	dkgSet, err := d.nodeSetCache.GetDKGSet(round)
-	if err != nil {
-		return nil, err
-	}
-
-	r := make(map[string]struct{}, len(dkgSet))
-	for id := range dkgSet {
-		if key, exists := d.nodeSetCache.GetPublicKey(id); exists {
-			r[hex.EncodeToString(key.Bytes())] = struct{}{}
-		}
-	}
-	return r, nil
-}
-
 func (g *Governance) DKGComplaints(round uint64) []*dkgTypes.Complaint {
 	s := g.GetStateForDKGAtRound(round)
 	if s == nil {
@@ -245,7 +229,7 @@ func (g *Governance) IsDKGMPKReady(round uint64) bool {
 		return false
 	}
 	config := g.Configuration(round)
-	threshold := 2*uint64(config.DKGSetSize)/3 + 1
+	threshold := 2*uint64(config.NotarySetSize)/3 + 1
 	count := s.DKGMPKReadysCount().Uint64()
 	return count >= threshold
 }
@@ -256,7 +240,7 @@ func (g *Governance) IsDKGFinal(round uint64) bool {
 		return false
 	}
 	config := g.Configuration(round)
-	threshold := 2*uint64(config.DKGSetSize)/3 + 1
+	threshold := 2*uint64(config.NotarySetSize)/3 + 1
 	count := s.DKGFinalizedsCount().Uint64()
 	return count >= threshold
 }
diff --git a/core/vm/oracle_contract_abi.go b/core/vm/oracle_contract_abi.go
index e42e9266f..3345a8f4c 100644
--- a/core/vm/oracle_contract_abi.go
+++ b/core/vm/oracle_contract_abi.go
@@ -55,20 +55,6 @@ const GovernanceABIJSON = `
     "stateMutability": "view",
     "type": "function"
   },
-  {
-    "constant": true,
-    "inputs": [],
-    "name": "dkgSetSize",
-    "outputs": [
-      {
-        "name": "",
-        "type": "uint256"
-      }
-    ],
-    "payable": false,
-    "stateMutability": "view",
-    "type": "function"
-  },
   {
     "constant": true,
     "inputs": [],
diff --git a/core/vm/oracle_contracts.go b/core/vm/oracle_contracts.go
index 155b3ea95..b148bca1f 100644
--- a/core/vm/oracle_contracts.go
+++ b/core/vm/oracle_contracts.go
@@ -88,7 +88,6 @@ const (
 	notarySetSizeLoc
 	notaryParamAlphaLoc
 	notaryParamBetaLoc
-	dkgSetSizeLoc
 	roundLengthLoc
 	minBlockIntervalLoc
 	fineValuesLoc
@@ -704,8 +703,8 @@ func (s *GovernanceState) PutDKGMPKReady(addr common.Address, ready bool) {
 	}
 	s.setStateBigInt(mapLoc, res)
 }
-func (s *GovernanceState) ClearDKGMPKReadys(dkgSet map[coreTypes.NodeID]struct{}) {
-	for id := range dkgSet {
+func (s *GovernanceState) ClearDKGMPKReadys(notarySet map[coreTypes.NodeID]struct{}) {
+	for id := range notarySet {
 		s.PutDKGMPKReady(IdToAddress(id), false)
 	}
 }
@@ -853,22 +852,6 @@ func (s *GovernanceState) NotaryParamBeta() *big.Int {
 	return s.getStateBigInt(big.NewInt(notaryParamBetaLoc))
 }
 
-// uint256 public dkgSetSize;
-func (s *GovernanceState) DKGSetSize() *big.Int {
-	return s.getStateBigInt(big.NewInt(dkgSetSizeLoc))
-}
-func (s *GovernanceState) CalDKGSetSize() {
-	nodeSetSize := float64(len(s.QualifiedNodes()))
-	setSize := math.Ceil((nodeSetSize*0.6-1)/3)*3 + 1
-
-	if nodeSetSize >= 100 {
-		alpha := float64(s.NotaryParamAlpha().Uint64()) / decimalMultiplier
-		beta := float64(s.NotaryParamBeta().Uint64()) / decimalMultiplier
-		setSize = math.Ceil(alpha*math.Log(nodeSetSize) - beta)
-	}
-	s.setStateBigInt(big.NewInt(dkgSetSizeLoc), big.NewInt(int64(setSize)))
-}
-
 // uint256 public roundLength;
 func (s *GovernanceState) RoundLength() *big.Int {
 	return s.getStateBigInt(big.NewInt(roundLengthLoc))
@@ -1012,7 +995,6 @@ func (s *GovernanceState) Configuration() *params.DexconConfig {
 		LambdaBA:          s.getStateBigInt(big.NewInt(lambdaBALoc)).Uint64(),
 		LambdaDKG:         s.getStateBigInt(big.NewInt(lambdaDKGLoc)).Uint64(),
 		NotarySetSize:     uint32(s.getStateBigInt(big.NewInt(notarySetSizeLoc)).Uint64()),
-		DKGSetSize:        uint32(s.getStateBigInt(big.NewInt(dkgSetSizeLoc)).Uint64()),
 		NotaryParamAlpha:  float32(s.getStateBigInt(big.NewInt(notaryParamAlphaLoc)).Uint64()) / decimalMultiplier,
 		NotaryParamBeta:   float32(s.getStateBigInt(big.NewInt(notaryParamBetaLoc)).Uint64()) / decimalMultiplier,
 		RoundLength:       s.getStateBigInt(big.NewInt(roundLengthLoc)).Uint64(),
@@ -1035,14 +1017,12 @@ func (s *GovernanceState) UpdateConfiguration(cfg *params.DexconConfig) {
 	s.setStateBigInt(big.NewInt(notarySetSizeLoc), big.NewInt(int64(cfg.NotarySetSize)))
 	s.setStateBigInt(big.NewInt(notaryParamAlphaLoc), big.NewInt(int64(cfg.NotaryParamAlpha*decimalMultiplier)))
 	s.setStateBigInt(big.NewInt(notaryParamBetaLoc), big.NewInt(int64(cfg.NotaryParamBeta*decimalMultiplier)))
-	s.setStateBigInt(big.NewInt(dkgSetSizeLoc), big.NewInt(int64(cfg.DKGSetSize)))
 	s.setStateBigInt(big.NewInt(roundLengthLoc), big.NewInt(int64(cfg.RoundLength)))
 	s.setStateBigInt(big.NewInt(minBlockIntervalLoc), big.NewInt(int64(cfg.MinBlockInterval)))
 	s.SetFineValues(cfg.FineValues)
 
 	// Calculate set size.
 	s.CalNotarySetSize()
-	s.CalDKGSetSize()
 }
 
 type rawConfigStruct struct {
@@ -1074,7 +1054,6 @@ func (s *GovernanceState) UpdateConfigurationRaw(cfg *rawConfigStruct) {
 	s.SetFineValues(cfg.FineValues)
 
 	s.CalNotarySetSize()
-	s.CalDKGSetSize()
 }
 
 // event ConfigurationChanged();
@@ -1287,15 +1266,15 @@ func (g *GovernanceContract) useGas(gas uint64) ([]byte, error) {
 	return nil, nil
 }
 
-func (g *GovernanceContract) configDKGSetSize(round *big.Int) *big.Int {
+func (g *GovernanceContract) configNotarySetSize(round *big.Int) *big.Int {
 	s, err := getConfigState(g.evm, round)
 	if err != nil {
 		return big.NewInt(0)
 	}
-	return s.DKGSetSize()
+	return s.NotarySetSize()
 }
 
-func (g *GovernanceContract) getDKGSet(round *big.Int) map[coreTypes.NodeID]struct{} {
+func (g *GovernanceContract) getNotarySet(round *big.Int) map[coreTypes.NodeID]struct{} {
 	crsRound := g.state.CRSRound()
 	var crs common.Hash
 	cmp := round.Cmp(crsRound)
@@ -1320,7 +1299,7 @@ func (g *GovernanceContract) getDKGSet(round *big.Int) map[coreTypes.NodeID]stru
 		crs = state.CRS()
 	}
 
-	target := coreTypes.NewDKGSetTarget(coreCommon.Hash(crs))
+	target := coreTypes.NewNotarySetTarget(coreCommon.Hash(crs))
 	ns := coreTypes.NewNodeSet()
 
 	state, err := getConfigState(g.evm, round)
@@ -1334,17 +1313,17 @@ func (g *GovernanceContract) getDKGSet(round *big.Int) map[coreTypes.NodeID]stru
 		}
 		ns.Add(coreTypes.NewNodeID(mpk))
 	}
-	return ns.GetSubSet(int(g.configDKGSetSize(round).Uint64()), target)
+	return ns.GetSubSet(int(g.configNotarySetSize(round).Uint64()), target)
 }
 
-func (g *GovernanceContract) inDKGSet(round *big.Int, nodeID coreTypes.NodeID) bool {
-	dkgSet := g.getDKGSet(round)
+func (g *GovernanceContract) inNotarySet(round *big.Int, nodeID coreTypes.NodeID) bool {
+	dkgSet := g.getNotarySet(round)
 	_, ok := dkgSet[nodeID]
 	return ok
 }
 
 func (g *GovernanceContract) clearDKG() {
-	dkgSet := g.getDKGSet(g.evm.Round)
+	dkgSet := g.getNotarySet(g.evm.Round)
 	g.state.ClearDKGMasterPublicKeyProposed()
 	g.state.ClearDKGMasterPublicKeys()
 	g.state.ClearDKGComplaintProposed()
@@ -1372,7 +1351,7 @@ func (g *GovernanceContract) addDKGComplaint(comp []byte) ([]byte, error) {
 	// Calculate 2f
 	threshold := new(big.Int).Mul(
 		big.NewInt(2),
-		new(big.Int).Div(g.state.DKGSetSize(), big.NewInt(3)))
+		new(big.Int).Div(g.state.NotarySetSize(), big.NewInt(3)))
 
 	// If 2f + 1 of DKG set is finalized, one can not propose complaint anymore.
 	if g.state.DKGFinalizedsCount().Cmp(threshold) > 0 {
@@ -1397,7 +1376,7 @@ func (g *GovernanceContract) addDKGComplaint(comp []byte) ([]byte, error) {
 	}
 
 	// DKGComplaint must belongs to someone in DKG set.
-	if !g.inDKGSet(round, dkgComplaint.ProposerID) {
+	if !g.inNotarySet(round, dkgComplaint.ProposerID) {
 		return nil, errExecutionReverted
 	}
 
@@ -1475,7 +1454,7 @@ func (g *GovernanceContract) addDKGMasterPublicKey(mpk []byte) ([]byte, error) {
 	// Calculate 2f
 	threshold := new(big.Int).Mul(
 		big.NewInt(2),
-		new(big.Int).Div(g.state.DKGSetSize(), big.NewInt(3)))
+		new(big.Int).Div(g.state.NotarySetSize(), big.NewInt(3)))
 
 	// If 2f + 1 of DKG set is mpk ready, one can not propose mpk anymore.
 	if g.state.DKGMPKReadysCount().Cmp(threshold) > 0 {
@@ -1487,7 +1466,7 @@ func (g *GovernanceContract) addDKGMasterPublicKey(mpk []byte) ([]byte, error) {
 	}
 
 	// DKGMasterPublicKey must belongs to someone in DKG set.
-	if !g.inDKGSet(round, dkgMasterPK.ProposerID) {
+	if !g.inNotarySet(round, dkgMasterPK.ProposerID) {
 		return nil, errExecutionReverted
 	}
 
@@ -1518,7 +1497,7 @@ func (g *GovernanceContract) addDKGMPKReady(ready []byte) ([]byte, error) {
 	}
 
 	// DKGFInalize must belongs to someone in DKG set.
-	if !g.inDKGSet(round, dkgReady.ProposerID) {
+	if !g.inNotarySet(round, dkgReady.ProposerID) {
 		return nil, errExecutionReverted
 	}
 
@@ -1552,7 +1531,7 @@ func (g *GovernanceContract) addDKGFinalize(finalize []byte) ([]byte, error) {
 	}
 
 	// DKGFInalize must belongs to someone in DKG set.
-	if !g.inDKGSet(round, dkgFinalize.ProposerID) {
+	if !g.inNotarySet(round, dkgFinalize.ProposerID) {
 		return nil, errExecutionReverted
 	}
 
@@ -1634,7 +1613,6 @@ func (g *GovernanceContract) register(
 		g.state.emitStaked(caller, value)
 
 		g.state.CalNotarySetSize()
-		g.state.CalDKGSetSize()
 	}
 	return g.useGas(GovernanceActionGasCost)
 }
@@ -1664,7 +1642,6 @@ func (g *GovernanceContract) stake() ([]byte, error) {
 	g.state.emitStaked(caller, value)
 
 	g.state.CalNotarySetSize()
-	g.state.CalDKGSetSize()
 
 	return g.useGas(GovernanceActionGasCost)
 }
@@ -1701,7 +1678,6 @@ func (g *GovernanceContract) unstake(amount *big.Int) ([]byte, error) {
 	g.state.emitUnstaked(caller, amount)
 
 	g.state.CalNotarySetSize()
-	g.state.CalDKGSetSize()
 
 	return g.useGas(GovernanceActionGasCost)
 }
@@ -1782,7 +1758,6 @@ func (g *GovernanceContract) payFine(nodeAddr common.Address) ([]byte, error) {
 	g.state.emitFinePaid(nodeAddr, g.contract.Value())
 
 	g.state.CalNotarySetSize()
-	g.state.CalDKGSetSize()
 
 	return g.useGas(GovernanceActionGasCost)
 }
@@ -1803,7 +1778,7 @@ func (g *GovernanceContract) proposeCRS(nextRound *big.Int, signedCRS []byte) ([
 	}
 
 	threshold := coreUtils.GetDKGThreshold(&coreTypes.Config{
-		DKGSetSize: uint32(g.state.DKGSetSize().Uint64())})
+		NotarySetSize: uint32(g.state.NotarySetSize().Uint64())})
 	dkgGPK, err := g.coreDKGUtils.NewGroupPublicKey(&g.state, nextRound, threshold)
 	if err != nil {
 		return nil, errExecutionReverted
@@ -1954,9 +1929,9 @@ func (g *GovernanceContract) resetDKG(newSignedCRS []byte) ([]byte, error) {
 	// Calculate 2f
 	threshold := new(big.Int).Mul(
 		big.NewInt(2),
-		new(big.Int).Div(g.state.DKGSetSize(), big.NewInt(3)))
+		new(big.Int).Div(g.state.NotarySetSize(), big.NewInt(3)))
 	tsigThreshold := coreUtils.GetDKGThreshold(&coreTypes.Config{
-		DKGSetSize: uint32(g.state.DKGSetSize().Uint64())})
+		NotarySetSize: uint32(g.state.NotarySetSize().Uint64())})
 
 	// If 2f + 1 of DKG set is finalized, check if DKG succeeded.
 	if g.state.DKGFinalizedsCount().Cmp(threshold) > 0 {
@@ -2252,12 +2227,6 @@ func (g *GovernanceContract) Run(evm *EVM, input []byte, contract *Contract) (re
 			return nil, errExecutionReverted
 		}
 		return res, nil
-	case "dkgSetSize":
-		res, err := method.Outputs.Pack(g.state.DKGSetSize())
-		if err != nil {
-			return nil, errExecutionReverted
-		}
-		return res, nil
 	case "finedRecords":
 		record := Bytes32{}
 		if err := method.Inputs.Unpack(&record, arguments); err != nil {
diff --git a/core/vm/oracle_contracts_test.go b/core/vm/oracle_contracts_test.go
index 80bc9a73f..ad6ffd9e3 100644
--- a/core/vm/oracle_contracts_test.go
+++ b/core/vm/oracle_contracts_test.go
@@ -189,7 +189,7 @@ func (g *OracleContractsTestSuite) SetupTest() {
 	config.NextHalvingSupply = new(big.Int).Mul(big.NewInt(1e18), big.NewInt(2.5e9))
 	config.LastHalvedAmount = new(big.Int).Mul(big.NewInt(1e18), big.NewInt(1.5e9))
 	config.MiningVelocity = 0.1875
-	config.DKGSetSize = 7
+	config.NotarySetSize = 7
 
 	g.config = config
 
@@ -658,15 +658,6 @@ func (g *OracleContractsTestSuite) TestConfigurationReading() {
 	res, err = g.call(GovernanceContractAddress, addr, input, big.NewInt(0))
 	g.Require().NoError(err)
 
-	// DKGSetSize.
-	input, err = GovernanceABI.ABI.Pack("dkgSetSize")
-	g.Require().NoError(err)
-	res, err = g.call(GovernanceContractAddress, addr, input, big.NewInt(0))
-	g.Require().NoError(err)
-	err = GovernanceABI.ABI.Unpack(&value, "dkgSetSize", res)
-	g.Require().NoError(err)
-	g.Require().True(uint32(value.Uint64()) > 0)
-
 	// RoundLength.
 	input, err = GovernanceABI.ABI.Pack("roundLength")
 	g.Require().NoError(err)
@@ -934,7 +925,7 @@ func (v *testTSigVerifierMock) VerifySignature(coreCommon.Hash, coreCrypto.Signa
 }
 
 func (g *OracleContractsTestSuite) TestResetDKG() {
-	for i := uint32(0); i < g.config.DKGSetSize; i++ {
+	for i := uint32(0); i < g.config.NotarySetSize; i++ {
 		privKey, addr := newPrefundAccount(g.stateDB)
 		pk := crypto.FromECDSAPub(&privKey.PublicKey)
 
@@ -945,7 +936,7 @@ func (g *OracleContractsTestSuite) TestResetDKG() {
 		_, err = g.call(GovernanceContractAddress, addr, input, amount)
 		g.Require().NoError(err)
 	}
-	g.Require().Len(g.s.QualifiedNodes(), int(g.config.DKGSetSize))
+	g.Require().Len(g.s.QualifiedNodes(), int(g.config.NotarySetSize))
 
 	addrs := make(map[int][]common.Address)
 	dkgSets := make(map[int]map[coreTypes.NodeID]struct{})
@@ -972,7 +963,7 @@ func (g *OracleContractsTestSuite) TestResetDKG() {
 		}
 
 		addrs[round] = []common.Address{}
-		target := coreTypes.NewDKGSetTarget(coreCommon.Hash(g.s.CRS()))
+		target := coreTypes.NewNotarySetTarget(coreCommon.Hash(g.s.CRS()))
 		ns := coreTypes.NewNodeSet()
 
 		for _, x := range g.s.QualifiedNodes() {
@@ -982,8 +973,8 @@ func (g *OracleContractsTestSuite) TestResetDKG() {
 			}
 			ns.Add(coreTypes.NewNodeID(mpk))
 		}
-		dkgSet := ns.GetSubSet(int(g.s.DKGSetSize().Uint64()), target)
-		g.Require().Len(dkgSet, int(g.config.DKGSetSize))
+		dkgSet := ns.GetSubSet(int(g.s.NotarySetSize().Uint64()), target)
+		g.Require().Len(dkgSet, int(g.config.NotarySetSize))
 		dkgSets[round] = dkgSet
 
 		for id := range dkgSet {
-- 
cgit v1.2.3