diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2018-09-17 17:43:56 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-17 17:43:56 +0800 |
commit | cbf0012603deb6d2b8c257c079de98792f7b84cf (patch) | |
tree | 10294191481d4b7516ef0bc089bca1ebf1aa705c | |
parent | 874c4c599a80b9c6f9c085c216be5fd6492cd2c2 (diff) | |
download | tangerine-consensus-cbf0012603deb6d2b8c257c079de98792f7b84cf.tar.gz tangerine-consensus-cbf0012603deb6d2b8c257c079de98792f7b84cf.tar.zst tangerine-consensus-cbf0012603deb6d2b8c257c079de98792f7b84cf.zip |
core: DKG interface (#108)
-rw-r--r-- | core/consensus_test.go | 5 | ||||
-rw-r--r-- | core/interfaces.go | 15 | ||||
-rw-r--r-- | core/test/governance.go | 35 | ||||
-rw-r--r-- | core/types/dkg.go | 47 | ||||
-rw-r--r-- | integration_test/network.go | 5 | ||||
-rw-r--r-- | simulation/governance.go | 44 | ||||
-rw-r--r-- | simulation/marshaller.go | 24 | ||||
-rw-r--r-- | simulation/network.go | 8 |
8 files changed, 179 insertions, 4 deletions
diff --git a/core/consensus_test.go b/core/consensus_test.go index a66a459..313600a 100644 --- a/core/consensus_test.go +++ b/core/consensus_test.go @@ -45,6 +45,11 @@ func (n *network) BroadcastBlock(block *types.Block) { func (n *network) BroadcastNotaryAck(notaryAck *types.NotaryAck) { } +// SendDKGPrivateShare sends PrivateShare to a DKG participant. +func (n *network) SendDKGPrivateShare( + recv types.ValidatorID, prvShare *types.DKGPrivateShare) { +} + // ReceiveChan returns a channel to receive messages from DEXON network. func (n *network) ReceiveChan() <-chan interface{} { return make(chan interface{}) diff --git a/core/interfaces.go b/core/interfaces.go index d4ace51..aa4756e 100644 --- a/core/interfaces.go +++ b/core/interfaces.go @@ -64,6 +64,9 @@ type Network interface { // BroadcastNotaryAck broadcasts notaryAck to all nodes in DEXON network. BroadcastNotaryAck(notaryAck *types.NotaryAck) + // SendDKGPrivateShare sends PrivateShare to a DKG participant. + SendDKGPrivateShare(recv types.ValidatorID, prvShare *types.DKGPrivateShare) + // ReceiveChan returns a channel to receive messages from DEXON network. ReceiveChan() <-chan interface{} } @@ -92,4 +95,16 @@ type Governance interface { // Get configuration change events after an epoch. GetConfigurationChangeEvent(epoch int) []types.ConfigurationChangeEvent + + // AddDKGComplaint adds a DKGComplaint. + AddDKGComplaint(complaint *types.DKGComplaint) + + // GetDKGComplaints gets all the DKGComplaints of round. + DKGComplaints(round uint64) []*types.DKGComplaint + + // AddDKGMasterPublicKey adds a DKGMasterPublicKey. + AddDKGMasterPublicKey(masterPublicKey *types.DKGMasterPublicKey) + + // DKGMasterPublicKeys gets all the DKGMasterPublicKey of round. + DKGMasterPublicKeys(round uint64) []*types.DKGMasterPublicKey } diff --git a/core/test/governance.go b/core/test/governance.go index 46f8606..0e5c249 100644 --- a/core/test/governance.go +++ b/core/test/governance.go @@ -37,6 +37,8 @@ type Governance struct { BlockProposingInterval int Validators map[types.ValidatorID]decimal.Decimal PrivateKeys map[types.ValidatorID]crypto.PrivateKey + DKGComplaint map[uint64][]*types.DKGComplaint + DKGMasterPublicKey map[uint64][]*types.DKGMasterPublicKey } // NewGovernance constructs a Governance instance. @@ -46,6 +48,8 @@ func NewGovernance(validatorCount, proposingInterval int) ( BlockProposingInterval: proposingInterval, Validators: make(map[types.ValidatorID]decimal.Decimal), PrivateKeys: make(map[types.ValidatorID]crypto.PrivateKey), + DKGComplaint: make(map[uint64][]*types.DKGComplaint), + DKGMasterPublicKey: make(map[uint64][]*types.DKGMasterPublicKey), } for i := 0; i < validatorCount; i++ { prv, err := eth.NewPrivateKey() @@ -110,3 +114,34 @@ func (g *Governance) GetPrivateKey( } return } + +// AddDKGComplaint add a DKGComplaint. +func (g *Governance) AddDKGComplaint(complaint *types.DKGComplaint) { + g.DKGComplaint[complaint.Round] = append(g.DKGComplaint[complaint.Round], complaint) +} + +// DKGComplaints returns the DKGComplaints of round. +func (g *Governance) DKGComplaints(round uint64) []*types.DKGComplaint { + complaints, exist := g.DKGComplaint[round] + if !exist { + return []*types.DKGComplaint{} + } + return complaints +} + +// AddDKGMasterPublicKey adds a DKGMasterPublicKey. +func (g *Governance) AddDKGMasterPublicKey( + masterPublicKey *types.DKGMasterPublicKey) { + g.DKGMasterPublicKey[masterPublicKey.Round] = append( + g.DKGMasterPublicKey[masterPublicKey.Round], masterPublicKey) +} + +// DKGMasterPublicKeys returns the DKGMasterPublicKeys of round. +func (g *Governance) DKGMasterPublicKeys( + round uint64) []*types.DKGMasterPublicKey { + masterPublicKeys, exist := g.DKGMasterPublicKey[round] + if !exist { + return []*types.DKGMasterPublicKey{} + } + return masterPublicKeys +} diff --git a/core/types/dkg.go b/core/types/dkg.go new file mode 100644 index 0000000..7fee404 --- /dev/null +++ b/core/types/dkg.go @@ -0,0 +1,47 @@ +// Copyright 2018 The dexon-consensus-core Authors +// This file is part of the dexon-consensus-core library. +// +// The dexon-consensus-core library is free software: you can redistribute it +// and/or modify it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation, either version 3 of the License, +// or (at your option) any later version. +// +// The dexon-consensus-core library is distributed in the hope that it will be +// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser +// General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the dexon-consensus-core library. If not, see +// <http://www.gnu.org/licenses/>. + +package types + +import ( + "github.com/dexon-foundation/dexon-consensus-core/crypto" + "github.com/dexon-foundation/dexon-consensus-core/crypto/dkg" +) + +// DKGPrivateShare describe a secret share in DKG protocol. +type DKGPrivateShare struct { + ProposerID ValidatorID `json:"proposer_id"` + Round uint64 `json:"round"` + PrivateKeyShare dkg.PrivateKey `json:"private_key_share"` + Signature crypto.Signature `json:"signature"` +} + +// DKGMasterPublicKey decrtibe a master public key in DKG protocol. +type DKGMasterPublicKey struct { + ProposerID ValidatorID `json:"proposer_id"` + Round uint64 `json:"round"` + PublicKeyShares dkg.PublicKeyShares `json:"private_key_share"` + Signature crypto.Signature `json:"signature"` +} + +// DKGComplaint describe a complaint in DKG protocol. +type DKGComplaint struct { + ProposerID ValidatorID `json:"proposer_id"` + Round uint64 `json:"round"` + PrivateShare DKGPrivateShare `json:"private_share"` + Signature crypto.Signature `json:"signature"` +} diff --git a/integration_test/network.go b/integration_test/network.go index 446d01a..735d48a 100644 --- a/integration_test/network.go +++ b/integration_test/network.go @@ -36,6 +36,11 @@ func (n *Network) BroadcastBlock(block *types.Block) { func (n *Network) BroadcastNotaryAck(notaryAck *types.NotaryAck) { } +// SendDKGPrivateShare sends PrivateShare to a DKG participant. +func (n *Network) SendDKGPrivateShare( + recv types.ValidatorID, prvShare *types.DKGPrivateShare) { +} + // ReceiveChan returns a channel to receive messages from DEXON network. func (n *Network) ReceiveChan() <-chan interface{} { return make(chan interface{}) diff --git a/simulation/governance.go b/simulation/governance.go index 61794a5..5220ae5 100644 --- a/simulation/governance.go +++ b/simulation/governance.go @@ -36,6 +36,8 @@ type simGovernance struct { phiRatio float32 chainNum uint32 crs string + dkgComplaint map[uint64][]*types.DKGComplaint + dkgMasterPublicKey map[uint64][]*types.DKGMasterPublicKey } // newSimGovernance returns a new simGovernance instance. @@ -44,10 +46,12 @@ func newSimGovernance( return &simGovernance{ validatorSet: make(map[types.ValidatorID]decimal.Decimal), expectedNumValidators: numValidators, - k: consensusConfig.K, - phiRatio: consensusConfig.PhiRatio, - chainNum: consensusConfig.ChainNum, - crs: consensusConfig.GenesisCRS, + k: consensusConfig.K, + phiRatio: consensusConfig.PhiRatio, + chainNum: consensusConfig.ChainNum, + crs: consensusConfig.GenesisCRS, + dkgComplaint: make(map[uint64][]*types.DKGComplaint), + dkgMasterPublicKey: make(map[uint64][]*types.DKGMasterPublicKey), } } @@ -111,3 +115,35 @@ func (g *simGovernance) addValidator(vID types.ValidatorID) { } g.validatorSet[vID] = decimal.NewFromFloat(0) } + +// AddDKGComplaint adds a DKGComplaint. +func (g *simGovernance) AddDKGComplaint(complaint *types.DKGComplaint) { + g.dkgComplaint[complaint.Round] = append( + g.dkgComplaint[complaint.Round], complaint) +} + +// DKGComplaints returns the DKGComplaints of round. +func (g *simGovernance) DKGComplaints(round uint64) []*types.DKGComplaint { + complaints, exist := g.dkgComplaint[round] + if !exist { + return []*types.DKGComplaint{} + } + return complaints +} + +// AddDKGMasterPublicKey adds a DKGMasterPublicKey. +func (g *simGovernance) AddDKGMasterPublicKey( + masterPublicKey *types.DKGMasterPublicKey) { + g.dkgMasterPublicKey[masterPublicKey.Round] = append( + g.dkgMasterPublicKey[masterPublicKey.Round], masterPublicKey) +} + +// DKGMasterPublicKeys returns the DKGMasterPublicKeys of round. +func (g *simGovernance) DKGMasterPublicKeys( + round uint64) []*types.DKGMasterPublicKey { + masterPublicKeys, exist := g.dkgMasterPublicKey[round] + if !exist { + return []*types.DKGMasterPublicKey{} + } + return masterPublicKeys +} diff --git a/simulation/marshaller.go b/simulation/marshaller.go index 88e2f6a..ad16419 100644 --- a/simulation/marshaller.go +++ b/simulation/marshaller.go @@ -69,6 +69,24 @@ func (m *jsonMarshaller) Unmarshal( break } msg = vote + case "dkg-private-share": + privateShare := &types.DKGPrivateShare{} + if err = json.Unmarshal(payload, privateShare); err != nil { + break + } + msg = privateShare + case "dkg-master-public-key": + masterPublicKey := &types.DKGMasterPublicKey{} + if err = json.Unmarshal(payload, masterPublicKey); err != nil { + break + } + msg = masterPublicKey + case "dkg-complaint": + complaint := &types.DKGComplaint{} + if err = json.Unmarshal(payload, complaint); err != nil { + break + } + msg = complaint default: err = fmt.Errorf("unrecognized message type: %v", msgType) } @@ -95,6 +113,12 @@ func (m *jsonMarshaller) Marshal(msg interface{}) ( msgType = "notary-ack" case *types.Vote: msgType = "vote" + case *types.DKGPrivateShare: + msgType = "dkg-private-share" + case *types.DKGMasterPublicKey: + msgType = "dkg-master-public-key" + case *types.DKGComplaint: + msgType = "dkg-complaint" default: err = fmt.Errorf("unknwon message type: %v", msg) } diff --git a/simulation/network.go b/simulation/network.go index 90f3aec..89b4f59 100644 --- a/simulation/network.go +++ b/simulation/network.go @@ -138,6 +138,14 @@ func (n *network) BroadcastNotaryAck(notaryAck *types.NotaryAck) { } } +// SendDKGPrivateShare implements core.Network interface. +func (n *network) SendDKGPrivateShare( + recv types.ValidatorID, prvShare *types.DKGPrivateShare) { + if err := n.trans.Send(recv, prvShare); err != nil { + panic(err) + } +} + // ReceiveChan implements core.Network interface. func (n *network) ReceiveChan() <-chan interface{} { return n.toConsensus |