From d72b0255b760b3b0ded0c1ce59985754b8000a78 Mon Sep 17 00:00:00 2001
From: Wei-Ning Huang <w@dexon.org>
Date: Wed, 20 Mar 2019 11:55:01 +0800
Subject: consensus: dexcon: disqualify dead node (#280)

Since a qualified node might fail stopped, we need to remove them from
qualified nodes to maintain network integrity. We do this by inspect the
previous round to see if there are dead nodes. A dead node is a notary
set node that does not propose any block in the previous round. We
disqualify them by fining them so their staked value is 1 wei below
minStake. This make them unqualified for being notary set in the follow
on rounds.
---
 dex/app_test.go                  | 12 ++---
 dex/downloader/testchain_test.go |  4 ++
 dex/governance.go                | 99 ++--------------------------------------
 dex/recovery.go                  |  2 +-
 4 files changed, 14 insertions(+), 103 deletions(-)

(limited to 'dex')

diff --git a/dex/app_test.go b/dex/app_test.go
index 7b158dd9e..e648abdbd 100644
--- a/dex/app_test.go
+++ b/dex/app_test.go
@@ -2299,12 +2299,10 @@ func newDexon(masterKey *ecdsa.PrivateKey, accountNum int) (*Dexon, []*ecdsa.Pri
 	db := ethdb.NewMemDatabase()
 
 	genesis := core.DefaultTestnetGenesisBlock()
-	genesis.Alloc = core.GenesisAlloc{
-		crypto.PubkeyToAddress(masterKey.PublicKey): {
-			Balance:   big.NewInt(100000000000000000),
-			Staked:    big.NewInt(50000000000000000),
-			PublicKey: crypto.FromECDSAPub(&masterKey.PublicKey),
-		},
+	genesis.Alloc[crypto.PubkeyToAddress(masterKey.PublicKey)] = core.GenesisAccount{
+		Balance:   big.NewInt(100000000000000000),
+		Staked:    big.NewInt(50000000000000000),
+		PublicKey: crypto.FromECDSAPub(&masterKey.PublicKey),
 	}
 
 	var accounts []*ecdsa.PrivateKey
@@ -2322,7 +2320,7 @@ func newDexon(masterKey *ecdsa.PrivateKey, accountNum int) (*Dexon, []*ecdsa.Pri
 	}
 
 	genesis.Config.Dexcon.BlockGasLimit = 2000000
-	genesis.Config.Dexcon.RoundLength = 60
+	genesis.Config.Dexcon.RoundLength = 600
 	genesis.Config.Dexcon.Owner = crypto.PubkeyToAddress(masterKey.PublicKey)
 
 	chainConfig, _, err := core.SetupGenesisBlock(db, genesis)
diff --git a/dex/downloader/testchain_test.go b/dex/downloader/testchain_test.go
index 722159bc0..15528c509 100644
--- a/dex/downloader/testchain_test.go
+++ b/dex/downloader/testchain_test.go
@@ -342,3 +342,7 @@ func (g *govStateFetcher) GetStateForConfigAtRound(round uint64) *vm.GovernanceS
 	}
 	return nil
 }
+
+func (g *govStateFetcher) NotarySetNodeKeyAddresses(round uint64) (map[common.Address]struct{}, error) {
+	return make(map[common.Address]struct{}), nil
+}
diff --git a/dex/governance.go b/dex/governance.go
index c2f2918b1..0d6ca0eba 100644
--- a/dex/governance.go
+++ b/dex/governance.go
@@ -20,13 +20,8 @@ package dex
 import (
 	"context"
 	"crypto/ecdsa"
-	"encoding/hex"
 	"math/big"
 
-	coreCommon "github.com/dexon-foundation/dexon-consensus/common"
-	dexCore "github.com/dexon-foundation/dexon-consensus/core"
-	coreCrypto "github.com/dexon-foundation/dexon-consensus/core/crypto"
-	coreEcdsa "github.com/dexon-foundation/dexon-consensus/core/crypto/ecdsa"
 	coreTypes "github.com/dexon-foundation/dexon-consensus/core/types"
 	dkgTypes "github.com/dexon-foundation/dexon-consensus/core/types/dkg"
 
@@ -42,11 +37,10 @@ import (
 type DexconGovernance struct {
 	*core.Governance
 
-	b            *DexAPIBackend
-	chainConfig  *params.ChainConfig
-	privateKey   *ecdsa.PrivateKey
-	address      common.Address
-	nodeSetCache *dexCore.NodeSetCache
+	b           *DexAPIBackend
+	chainConfig *params.ChainConfig
+	privateKey  *ecdsa.PrivateKey
+	address     common.Address
 }
 
 // NewDexconGovernance returns a governance implementation of the DEXON
@@ -61,7 +55,6 @@ func NewDexconGovernance(backend *DexAPIBackend, chainConfig *params.ChainConfig
 		privateKey:  privKey,
 		address:     crypto.PubkeyToAddress(privKey.PublicKey),
 	}
-	g.nodeSetCache = dexCore.NewNodeSetCache(g)
 	return g
 }
 
@@ -110,36 +103,10 @@ func (d *DexconGovernance) sendGovTx(ctx context.Context, data []byte) error {
 	return d.b.SendTx(ctx, tx)
 }
 
-// CRS returns the CRS for a given round.
-func (d *DexconGovernance) CRS(round uint64) coreCommon.Hash {
-	if round <= dexCore.DKGDelayRound {
-		s := d.GetStateAtRound(0)
-		crs := s.CRS()
-		for i := uint64(0); i < round; i++ {
-			crs = crypto.Keccak256Hash(crs[:])
-		}
-		return coreCommon.Hash(crs)
-	}
-	if round > d.CRSRound() {
-		return coreCommon.Hash{}
-	}
-	var s *vm.GovernanceState
-	if round == d.CRSRound() {
-		s = d.GetHeadState()
-	} else {
-		s = d.GetStateAtRound(round)
-	}
-	return coreCommon.Hash(s.CRS())
-}
-
 func (d *DexconGovernance) Round() uint64 {
 	return d.b.CurrentBlock().Round()
 }
 
-func (d *DexconGovernance) CRSRound() uint64 {
-	return d.GetHeadState().CRSRound().Uint64()
-}
-
 // ProposeCRS send proposals of a new CRS
 func (d *DexconGovernance) ProposeCRS(round uint64, signedCRS []byte) {
 	data, err := vm.PackProposeCRS(round, signedCRS)
@@ -154,21 +121,6 @@ func (d *DexconGovernance) ProposeCRS(round uint64, signedCRS []byte) {
 	}
 }
 
-// NodeSet returns the current node set.
-func (d *DexconGovernance) NodeSet(round uint64) []coreCrypto.PublicKey {
-	s := d.GetStateForConfigAtRound(round)
-	var pks []coreCrypto.PublicKey
-
-	for _, n := range s.QualifiedNodes() {
-		pk, err := coreEcdsa.NewPublicKeyFromByteSlice(n.PublicKey)
-		if err != nil {
-			panic(err)
-		}
-		pks = append(pks, pk)
-	}
-	return pks
-}
-
 // AddDKGComplaint adds a DKGComplaint.
 func (d *DexconGovernance) AddDKGComplaint(round uint64, complaint *dkgTypes.Complaint) {
 	data, err := vm.PackAddDKGComplaint(round, complaint)
@@ -253,49 +205,6 @@ func (d *DexconGovernance) ReportForkBlock(block1, block2 *coreTypes.Block) {
 	}
 }
 
-func (d *DexconGovernance) NotarySet(round uint64) (map[string]struct{}, error) {
-	notarySet, err := d.nodeSetCache.GetNotarySet(round)
-	if err != nil {
-		return nil, err
-	}
-
-	r := make(map[string]struct{}, len(notarySet))
-	for id := range notarySet {
-		if key, exists := d.nodeSetCache.GetPublicKey(id); exists {
-			r[hex.EncodeToString(key.Bytes())] = struct{}{}
-		}
-	}
-	return r, nil
-}
-
-func (d *DexconGovernance) NotarySetAddresses(round uint64) (map[common.Address]struct{}, error) {
-	notarySet, err := d.nodeSetCache.GetNotarySet(round)
-	if err != nil {
-		return nil, err
-	}
-
-	r := make(map[common.Address]struct{}, len(notarySet))
-	for id := range notarySet {
-		r[vm.IdToAddress(id)] = struct{}{}
-	}
-	return r, nil
-}
-
-func (d *DexconGovernance) 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 (d *DexconGovernance) ResetDKG(newSignedCRS []byte) {
 	data, err := vm.PackResetDKG(newSignedCRS)
 	if err != nil {
diff --git a/dex/recovery.go b/dex/recovery.go
index cfc8ae203..0e5c60e1a 100644
--- a/dex/recovery.go
+++ b/dex/recovery.go
@@ -442,7 +442,7 @@ func (r *Recovery) Votes(height uint64) (uint64, error) {
 		return 0, err
 	}
 
-	notarySet, err := r.gov.NotarySetAddresses(r.gov.Round())
+	notarySet, err := r.gov.NotarySetNodeKeyAddresses(r.gov.Round())
 	if err != nil {
 		return 0, err
 	}
-- 
cgit v1.2.3