From 87db7f44d06ff4610b7a7a0b3bc845b8d8e342d0 Mon Sep 17 00:00:00 2001
From: Wei-Ning Huang <w@dexon.org>
Date: Thu, 24 Jan 2019 15:05:24 +0800
Subject: consensus: dexcon: snapshot round height when finalizing block (#170)

Instead of having BP to send a tx to register the round height, just
modify the state when finalizing block.
---
 core/vm/governance.go      | 48 ----------------------------------------------
 core/vm/governance_abi.go  | 18 -----------------
 core/vm/governance_test.go | 40 --------------------------------------
 3 files changed, 106 deletions(-)

(limited to 'core/vm')

diff --git a/core/vm/governance.go b/core/vm/governance.go
index a0014005d..b400ba61b 100644
--- a/core/vm/governance.go
+++ b/core/vm/governance.go
@@ -196,15 +196,6 @@ func RunGovernanceContract(evm *EVM, input []byte, contract *Contract) (ret []by
 			return nil, errExecutionReverted
 		}
 		return g.stake(args.PublicKey, args.Name, args.Email, args.Location, args.Url)
-	case "snapshotRound":
-		args := struct {
-			Round  *big.Int
-			Height *big.Int
-		}{}
-		if err := method.Inputs.Unpack(&args, arguments); err != nil {
-			return nil, errExecutionReverted
-		}
-		return g.snapshotRound(args.Round, args.Height)
 	case "transferOwnership":
 		var newOwner common.Address
 		if err := method.Inputs.Unpack(&newOwner, arguments); err != nil {
@@ -711,9 +702,6 @@ func (s *GovernanceStateHelper) appendTo2DByteArray(pos, index *big.Int, data []
 }
 
 // uint256[] public roundHeight;
-func (s *GovernanceStateHelper) LenRoundHeight() *big.Int {
-	return s.getStateBigInt(big.NewInt(roundHeightLoc))
-}
 func (s *GovernanceStateHelper) RoundHeight(round *big.Int) *big.Int {
 	baseLoc := s.getSlotLoc(big.NewInt(roundHeightLoc))
 	loc := new(big.Int).Add(baseLoc, round)
@@ -2169,31 +2157,6 @@ func (g *GovernanceContract) transferOwnership(newOwner common.Address) ([]byte,
 	return nil, nil
 }
 
-func (g *GovernanceContract) snapshotRound(round, height *big.Int) ([]byte, error) {
-	// Validate if this mapping is correct. Only block proposer need to verify this.
-	if g.evm.IsBlockProposer() {
-		realHeight, ok := g.evm.GetRoundHeight(round.Uint64())
-		if !ok {
-			return g.penalize()
-		}
-
-		if height.Cmp(new(big.Int).SetUint64(realHeight)) != 0 {
-			return g.penalize()
-		}
-	}
-
-	// Only allow updating the next round.
-	nextRound := g.state.LenRoundHeight()
-	if round.Cmp(nextRound) != 0 {
-		// No need to penalize, since the only possibility at this point is the
-		// round height is already snapshoted.
-		return nil, errExecutionReverted
-	}
-
-	g.state.PushRoundHeight(height)
-	return nil, nil
-}
-
 func PackProposeCRS(round uint64, signedCRS []byte) ([]byte, error) {
 	method := GovernanceContractName2Method["proposeCRS"]
 	res, err := method.Inputs.Pack(big.NewInt(int64(round)), signedCRS)
@@ -2204,17 +2167,6 @@ func PackProposeCRS(round uint64, signedCRS []byte) ([]byte, error) {
 	return data, nil
 }
 
-func PackNotifyRoundHeight(targetRound, consensusHeight uint64) ([]byte, error) {
-	method := GovernanceContractName2Method["snapshotRound"]
-	res, err := method.Inputs.Pack(
-		big.NewInt(int64(targetRound)), big.NewInt(int64(consensusHeight)))
-	if err != nil {
-		return nil, err
-	}
-	data := append(method.Id(), res...)
-	return data, nil
-}
-
 func PackAddDKGMasterPublicKey(round uint64, mpk *dkgTypes.MasterPublicKey) ([]byte, error) {
 	method := GovernanceContractName2Method["addDKGMasterPublicKey"]
 	encoded, err := rlp.EncodeToBytes(mpk)
diff --git a/core/vm/governance_abi.go b/core/vm/governance_abi.go
index f139ac7a8..4627050dc 100644
--- a/core/vm/governance_abi.go
+++ b/core/vm/governance_abi.go
@@ -892,24 +892,6 @@ const GovernanceABIJSON = `
     "stateMutability": "view",
     "type": "function"
   },
-  {
-    "constant": false,
-    "inputs": [
-      {
-        "name": "Round",
-        "type": "uint256"
-      },
-      {
-        "name": "Height",
-        "type": "uint256"
-      }
-    ],
-    "name": "snapshotRound",
-    "outputs": [],
-    "payable": false,
-    "stateMutability": "nonpayable",
-    "type": "function"
-  },
   {
     "constant": false,
     "inputs": [
diff --git a/core/vm/governance_test.go b/core/vm/governance_test.go
index 879bb406e..292e4ef1d 100644
--- a/core/vm/governance_test.go
+++ b/core/vm/governance_test.go
@@ -625,46 +625,6 @@ func (g *GovernanceContractTestSuite) TestUpdateConfiguration() {
 	g.Require().NoError(err)
 }
 
-func (g *GovernanceContractTestSuite) TestSnapshotRound() {
-	_, addr := g.newPrefundAccount()
-
-	// Wrong height.
-	input, err := abiObject.Pack("snapshotRound", big.NewInt(1), big.NewInt(666))
-	g.Require().NoError(err)
-	_, err = g.call(addr, input, big.NewInt(0))
-	g.Require().NotNil(err)
-
-	// Invalid round.
-	input, err = abiObject.Pack("snapshotRound", big.NewInt(2), big.NewInt(2000))
-	g.Require().NoError(err)
-	_, err = g.call(addr, input, big.NewInt(0))
-	g.Require().NotNil(err)
-
-	// Correct.
-	input, err = abiObject.Pack("snapshotRound", big.NewInt(1), big.NewInt(1000))
-	g.Require().NoError(err)
-	_, err = g.call(addr, input, big.NewInt(0))
-	g.Require().NoError(err)
-
-	// Duplicate round.
-	input, err = abiObject.Pack("snapshotRound", big.NewInt(1), big.NewInt(1000))
-	g.Require().NoError(err)
-	_, err = g.call(addr, input, big.NewInt(0))
-	g.Require().NotNil(err)
-
-	// Invalid round.
-	input, err = abiObject.Pack("snapshotRound", big.NewInt(3), big.NewInt(3000))
-	g.Require().NoError(err)
-	_, err = g.call(addr, input, big.NewInt(0))
-	g.Require().NotNil(err)
-
-	// Correct.
-	input, err = abiObject.Pack("snapshotRound", big.NewInt(2), big.NewInt(2000))
-	g.Require().NoError(err)
-	_, err = g.call(addr, input, big.NewInt(0))
-	g.Require().NoError(err)
-}
-
 func (g *GovernanceContractTestSuite) TestConfigurationReading() {
 	_, addr := g.newPrefundAccount()
 
-- 
cgit v1.2.3