diff options
Diffstat (limited to 'core/test')
-rw-r--r-- | core/test/governance.go | 11 | ||||
-rw-r--r-- | core/test/state.go | 57 | ||||
-rw-r--r-- | core/test/state_test.go | 21 |
3 files changed, 46 insertions, 43 deletions
diff --git a/core/test/governance.go b/core/test/governance.go index 94dcb6c..a3b9168 100644 --- a/core/test/governance.go +++ b/core/test/governance.go @@ -25,6 +25,7 @@ import ( "github.com/dexon-foundation/dexon-consensus-core/common" "github.com/dexon-foundation/dexon-consensus-core/core/crypto" "github.com/dexon-foundation/dexon-consensus-core/core/types" + typesDKG "github.com/dexon-foundation/dexon-consensus-core/core/types/dkg" ) var ( @@ -112,7 +113,7 @@ func (g *Governance) ProposeCRS(round uint64, signedCRS []byte) { // AddDKGComplaint add a DKGComplaint. func (g *Governance) AddDKGComplaint( - round uint64, complaint *types.DKGComplaint) { + round uint64, complaint *typesDKG.Complaint) { if round != complaint.Round { return } @@ -123,13 +124,13 @@ func (g *Governance) AddDKGComplaint( } // DKGComplaints returns the DKGComplaints of round. -func (g *Governance) DKGComplaints(round uint64) []*types.DKGComplaint { +func (g *Governance) DKGComplaints(round uint64) []*typesDKG.Complaint { return g.state.DKGComplaints(round) } // AddDKGMasterPublicKey adds a DKGMasterPublicKey. func (g *Governance) AddDKGMasterPublicKey( - round uint64, masterPublicKey *types.DKGMasterPublicKey) { + round uint64, masterPublicKey *typesDKG.MasterPublicKey) { if round != masterPublicKey.Round { return } @@ -138,12 +139,12 @@ func (g *Governance) AddDKGMasterPublicKey( // DKGMasterPublicKeys returns the DKGMasterPublicKeys of round. func (g *Governance) DKGMasterPublicKeys( - round uint64) []*types.DKGMasterPublicKey { + round uint64) []*typesDKG.MasterPublicKey { return g.state.DKGMasterPublicKeys(round) } // AddDKGFinalize adds a DKG finalize message. -func (g *Governance) AddDKGFinalize(round uint64, final *types.DKGFinalize) { +func (g *Governance) AddDKGFinalize(round uint64, final *typesDKG.Finalize) { if round != final.Round { return } diff --git a/core/test/state.go b/core/test/state.go index 59b3dc5..f8f9ebe 100644 --- a/core/test/state.go +++ b/core/test/state.go @@ -27,6 +27,7 @@ import ( "github.com/dexon-foundation/dexon-consensus-core/core/crypto" "github.com/dexon-foundation/dexon-consensus-core/core/crypto/ecdsa" "github.com/dexon-foundation/dexon-consensus-core/core/types" + typesDKG "github.com/dexon-foundation/dexon-consensus-core/core/types/dkg" "github.com/dexon-foundation/dexon/rlp" ) @@ -102,9 +103,9 @@ type State struct { // Nodes nodes map[types.NodeID]crypto.PublicKey // DKG & CRS - dkgComplaints map[uint64]map[types.NodeID][]*types.DKGComplaint - dkgMasterPublicKeys map[uint64]map[types.NodeID]*types.DKGMasterPublicKey - dkgFinals map[uint64]map[types.NodeID]*types.DKGFinalize + dkgComplaints map[uint64]map[types.NodeID][]*typesDKG.Complaint + dkgMasterPublicKeys map[uint64]map[types.NodeID]*typesDKG.MasterPublicKey + dkgFinals map[uint64]map[types.NodeID]*typesDKG.Finalize crs []common.Hash // Other stuffs local bool @@ -114,9 +115,9 @@ type State struct { // applied. pendingChangedConfigs map[StateChangeType]interface{} pendingNodes [][]byte - pendingDKGComplaints []*types.DKGComplaint - pendingDKGFinals []*types.DKGFinalize - pendingDKGMasterPublicKeys []*types.DKGMasterPublicKey + pendingDKGComplaints []*typesDKG.Complaint + pendingDKGFinals []*typesDKG.Finalize + pendingDKGMasterPublicKeys []*typesDKG.MasterPublicKey pendingCRS []*crsAdditionRequest pendingChangesLock sync.Mutex } @@ -147,11 +148,11 @@ func NewState( dkgSetSize: uint32(len(nodes)), pendingChangedConfigs: make(map[StateChangeType]interface{}), dkgFinals: make( - map[uint64]map[types.NodeID]*types.DKGFinalize), + map[uint64]map[types.NodeID]*typesDKG.Finalize), dkgComplaints: make( - map[uint64]map[types.NodeID][]*types.DKGComplaint), + map[uint64]map[types.NodeID][]*typesDKG.Complaint), dkgMasterPublicKeys: make( - map[uint64]map[types.NodeID]*types.DKGMasterPublicKey), + map[uint64]map[types.NodeID]*typesDKG.MasterPublicKey), } } @@ -185,13 +186,13 @@ func (s *State) unpackPayload( v = &crsAdditionRequest{} err = rlp.DecodeBytes(raw.Payload, v) case StateAddDKGComplaint: - v = &types.DKGComplaint{} + v = &typesDKG.Complaint{} err = rlp.DecodeBytes(raw.Payload, v) case StateAddDKGMasterPublicKey: - v = &types.DKGMasterPublicKey{} + v = &typesDKG.MasterPublicKey{} err = rlp.DecodeBytes(raw.Payload, v) case StateAddDKGFinal: - v = &types.DKGFinalize{} + v = &typesDKG.Finalize{} err = rlp.DecodeBytes(raw.Payload, v) case StateChangeNumChains: var tmp uint32 @@ -335,7 +336,7 @@ func (s *State) isValidRequest(req *StateChangeRequest) (err error) { // responsible for acquiring appropriate lock. switch req.Type { case StateAddDKGComplaint: - comp := req.Payload.(*types.DKGComplaint) + comp := req.Payload.(*typesDKG.Complaint) // If we've received DKG final from that proposer, we would ignore // its complaint. if _, exists := s.dkgFinals[comp.Round][comp.ProposerID]; exists { @@ -389,24 +390,24 @@ func (s *State) applyRequest(req *StateChangeRequest) error { } s.crs = append(s.crs, crsRequest.CRS) case StateAddDKGComplaint: - comp := req.Payload.(*types.DKGComplaint) + comp := req.Payload.(*typesDKG.Complaint) if _, exists := s.dkgComplaints[comp.Round]; !exists { s.dkgComplaints[comp.Round] = make( - map[types.NodeID][]*types.DKGComplaint) + map[types.NodeID][]*typesDKG.Complaint) } s.dkgComplaints[comp.Round][comp.ProposerID] = append( s.dkgComplaints[comp.Round][comp.ProposerID], comp) case StateAddDKGMasterPublicKey: - mKey := req.Payload.(*types.DKGMasterPublicKey) + mKey := req.Payload.(*typesDKG.MasterPublicKey) if _, exists := s.dkgMasterPublicKeys[mKey.Round]; !exists { s.dkgMasterPublicKeys[mKey.Round] = make( - map[types.NodeID]*types.DKGMasterPublicKey) + map[types.NodeID]*typesDKG.MasterPublicKey) } s.dkgMasterPublicKeys[mKey.Round][mKey.ProposerID] = mKey case StateAddDKGFinal: - final := req.Payload.(*types.DKGFinalize) + final := req.Payload.(*typesDKG.Finalize) if _, exists := s.dkgFinals[final.Round]; !exists { - s.dkgFinals[final.Round] = make(map[types.NodeID]*types.DKGFinalize) + s.dkgFinals[final.Round] = make(map[types.NodeID]*typesDKG.Finalize) } s.dkgFinals[final.Round][final.ProposerID] = final case StateChangeNumChains: @@ -491,13 +492,13 @@ func (s *State) RequestChange( s.pendingCRS = append(s.pendingCRS, payload.(*crsAdditionRequest)) case StateAddDKGComplaint: s.pendingDKGComplaints = append( - s.pendingDKGComplaints, payload.(*types.DKGComplaint)) + s.pendingDKGComplaints, payload.(*typesDKG.Complaint)) case StateAddDKGMasterPublicKey: s.pendingDKGMasterPublicKeys = append( - s.pendingDKGMasterPublicKeys, payload.(*types.DKGMasterPublicKey)) + s.pendingDKGMasterPublicKeys, payload.(*typesDKG.MasterPublicKey)) case StateAddDKGFinal: s.pendingDKGFinals = append( - s.pendingDKGFinals, payload.(*types.DKGFinalize)) + s.pendingDKGFinals, payload.(*typesDKG.Finalize)) default: s.pendingChangedConfigs[t] = payload } @@ -516,21 +517,21 @@ func (s *State) CRS(round uint64) common.Hash { // DKGComplaints access current received dkg complaints for that round. // This information won't be snapshot, thus can't be cached in test.Governance. -func (s *State) DKGComplaints(round uint64) []*types.DKGComplaint { +func (s *State) DKGComplaints(round uint64) []*typesDKG.Complaint { s.lock.RLock() defer s.lock.RUnlock() comps, exists := s.dkgComplaints[round] if !exists { return nil } - tmpComps := make([]*types.DKGComplaint, 0, len(comps)) + tmpComps := make([]*typesDKG.Complaint, 0, len(comps)) for _, compProp := range comps { for _, comp := range compProp { bytes, err := rlp.EncodeToBytes(comp) if err != nil { panic(err) } - compCopy := &types.DKGComplaint{} + compCopy := &typesDKG.Complaint{} if err = rlp.DecodeBytes(bytes, compCopy); err != nil { panic(err) } @@ -543,21 +544,21 @@ func (s *State) DKGComplaints(round uint64) []*types.DKGComplaint { // DKGMasterPublicKeys access current received dkg master public keys for that // round. This information won't be snapshot, thus can't be cached in // test.Governance. -func (s *State) DKGMasterPublicKeys(round uint64) []*types.DKGMasterPublicKey { +func (s *State) DKGMasterPublicKeys(round uint64) []*typesDKG.MasterPublicKey { s.lock.RLock() defer s.lock.RUnlock() masterPublicKeys, exists := s.dkgMasterPublicKeys[round] if !exists { return nil } - mpks := make([]*types.DKGMasterPublicKey, 0, len(masterPublicKeys)) + mpks := make([]*typesDKG.MasterPublicKey, 0, len(masterPublicKeys)) for _, mpk := range masterPublicKeys { // Return a deep copied master public keys. b, err := rlp.EncodeToBytes(mpk) if err != nil { panic(err) } - mpkCopy := types.NewDKGMasterPublicKey() + mpkCopy := typesDKG.NewMasterPublicKey() if err = rlp.DecodeBytes(b, mpkCopy); err != nil { panic(err) } diff --git a/core/test/state_test.go b/core/test/state_test.go index b5ed383..ebb5a25 100644 --- a/core/test/state_test.go +++ b/core/test/state_test.go @@ -27,6 +27,7 @@ import ( "github.com/dexon-foundation/dexon-consensus-core/core/crypto/dkg" "github.com/dexon-foundation/dexon-consensus-core/core/crypto/ecdsa" "github.com/dexon-foundation/dexon-consensus-core/core/types" + typesDKG "github.com/dexon-foundation/dexon-consensus-core/core/types/dkg" "github.com/stretchr/testify/suite" ) @@ -35,7 +36,7 @@ type StateTestSuite struct { } func (s *StateTestSuite) newDKGMasterPublicKey( - round uint64) *types.DKGMasterPublicKey { + round uint64) *typesDKG.MasterPublicKey { prvKey, err := ecdsa.NewPrivateKey() s.Require().NoError(err) pubKey := prvKey.PublicKey() @@ -43,7 +44,7 @@ func (s *StateTestSuite) newDKGMasterPublicKey( _, pubShare := dkg.NewPrivateKeyShares(3) dID, err := dkg.BytesID(nodeID.Hash[:]) s.Require().NoError(err) - return &types.DKGMasterPublicKey{ + return &typesDKG.MasterPublicKey{ ProposerID: nodeID, Round: round, DKGID: dID, @@ -51,16 +52,16 @@ func (s *StateTestSuite) newDKGMasterPublicKey( } } -func (s *StateTestSuite) newDKGComplaint(round uint64) *types.DKGComplaint { +func (s *StateTestSuite) newDKGComplaint(round uint64) *typesDKG.Complaint { prvKey, err := ecdsa.NewPrivateKey() s.Require().NoError(err) pubKey := prvKey.PublicKey() nodeID := types.NewNodeID(pubKey) // TODO(mission): sign it, and it doesn't make sense to complaint self. - return &types.DKGComplaint{ + return &typesDKG.Complaint{ ProposerID: nodeID, Round: round, - PrivateShare: types.DKGPrivateShare{ + PrivateShare: typesDKG.PrivateShare{ ProposerID: nodeID, ReceiverID: nodeID, Round: round, @@ -69,13 +70,13 @@ func (s *StateTestSuite) newDKGComplaint(round uint64) *types.DKGComplaint { } } -func (s *StateTestSuite) newDKGFinal(round uint64) *types.DKGFinalize { +func (s *StateTestSuite) newDKGFinal(round uint64) *typesDKG.Finalize { prvKey, err := ecdsa.NewPrivateKey() s.Require().NoError(err) pubKey := prvKey.PublicKey() nodeID := types.NewNodeID(pubKey) // TODO(mission): sign it. - return &types.DKGFinalize{ + return &typesDKG.Finalize{ ProposerID: nodeID, Round: round, } @@ -126,9 +127,9 @@ func (s *StateTestSuite) findNode( func (s *StateTestSuite) makeDKGChanges( st *State, - masterPubKey *types.DKGMasterPublicKey, - complaint *types.DKGComplaint, - final *types.DKGFinalize) { + masterPubKey *typesDKG.MasterPublicKey, + complaint *typesDKG.Complaint, + final *typesDKG.Finalize) { st.RequestChange(StateAddDKGMasterPublicKey, masterPubKey) st.RequestChange(StateAddDKGComplaint, complaint) st.RequestChange(StateAddDKGFinal, final) |