From cbf0012603deb6d2b8c257c079de98792f7b84cf Mon Sep 17 00:00:00 2001 From: Jimmy Hu Date: Mon, 17 Sep 2018 17:43:56 +0800 Subject: core: DKG interface (#108) --- simulation/governance.go | 44 ++++++++++++++++++++++++++++++++++++++++---- simulation/marshaller.go | 24 ++++++++++++++++++++++++ simulation/network.go | 8 ++++++++ 3 files changed, 72 insertions(+), 4 deletions(-) (limited to 'simulation') 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 -- cgit v1.2.3