From 4b40c1b8990d2a371a77018feea32d038163f2ec Mon Sep 17 00:00:00 2001 From: Mission Liao Date: Sun, 17 Mar 2019 09:56:23 +0800 Subject: dkg: add reset field (#492) * Add Reset fields to DKG types * Fix crypto part after adding Reset field * Prohibit DKG messages with different resetCount * Add TODO * Add reset parameter to dkgProtocol constructor * Add TODO * Fix inconsist hash to prepare CRS * Add reset parameter when runnning DKG * Fix test for utils.RoundEvent * Add dummy test to prohibit DKG messages with unexpected reset count * Fix test.App --- core/test/state.go | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) (limited to 'core/test/state.go') diff --git a/core/test/state.go b/core/test/state.go index fbf4505..89d2e90 100644 --- a/core/test/state.go +++ b/core/test/state.go @@ -74,6 +74,12 @@ var ( // ErrStatePendingChangesNotEqual means pending change requests of two // states are not equal. ErrStatePendingChangesNotEqual = errors.New("pending changes not equal") + // ErrChangeWontApply means the state change won't be applied for some + // reason. + ErrChangeWontApply = errors.New("change won't apply") + // ErrUnmatchedResetCount means an DKG message attempt to apply is not + // the latest reset count in State module. + ErrUnmatchedResetCount = errors.New("unmatched reset count of DKG message") // ErrNotInRemoteMode means callers attempts to call functions for remote // mode when the State instance is still in local mode. ErrNotInRemoteMode = errors.New( @@ -628,20 +634,33 @@ func (s *State) PackOwnRequests() (b []byte, err error) { } // isValidRequest checks if this request is valid to proceed or not. -func (s *State) isValidRequest(req *StateChangeRequest) (err error) { +func (s *State) isValidRequest(req *StateChangeRequest) error { // NOTE: there would be no lock in this helper, callers should be // responsible for acquiring appropriate lock. switch req.Type { + case StateAddDKGMPKReady: + ready := req.Payload.(*typesDKG.MPKReady) + if ready.Reset != s.dkgResetCount[ready.Round] { + return ErrUnmatchedResetCount + } + case StateAddDKGFinal: + final := req.Payload.(*typesDKG.Finalize) + if final.Reset != s.dkgResetCount[final.Round] { + return ErrUnmatchedResetCount + } case StateAddDKGMasterPublicKey: mpk := req.Payload.(*typesDKG.MasterPublicKey) + if mpk.Reset != s.dkgResetCount[mpk.Round] { + return ErrUnmatchedResetCount + } // If we've received identical MPK, ignore it. mpkForRound, exists := s.dkgMasterPublicKeys[mpk.Round] if exists { if oldMpk, exists := mpkForRound[mpk.ProposerID]; exists { if !oldMpk.Equal(mpk) { - err = ErrDuplicatedChange + return ErrDuplicatedChange } - return + return ErrChangeWontApply } } // If we've received MPK from that proposer, we would ignore @@ -651,6 +670,9 @@ func (s *State) isValidRequest(req *StateChangeRequest) (err error) { } case StateAddDKGComplaint: comp := req.Payload.(*typesDKG.Complaint) + if comp.Reset != s.dkgResetCount[comp.Round] { + return ErrUnmatchedResetCount + } // If we've received DKG final from that proposer, we would ignore // its complaint. if _, exists := s.dkgFinals[comp.Round][comp.ProposerID]; exists { @@ -687,6 +709,8 @@ func (s *State) isValidRequest(req *StateChangeRequest) (err error) { if s.crs[len(s.crs)-1].Equal(newCRS) { return ErrDuplicatedChange } + // TODO(mission): find a smart way to make sure the caller call request + // this change with correct resetCount. } return nil } -- cgit v1.2.3