diff options
author | Mission Liao <mission.liao@dexon.org> | 2018-11-09 12:23:58 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-09 12:23:58 +0800 |
commit | a7e3c046693d9df860422e71a14c8659b36afe6a (patch) | |
tree | 3c4148b202a3d2acb3e326198c10c1b4b35738f6 | |
parent | 71a1dd9c072fb64da026ebe56acd2d187ec272c8 (diff) | |
download | tangerine-consensus-a7e3c046693d9df860422e71a14c8659b36afe6a.tar.gz tangerine-consensus-a7e3c046693d9df860422e71a14c8659b36afe6a.tar.zst tangerine-consensus-a7e3c046693d9df860422e71a14c8659b36afe6a.zip |
core: Extract VoteHeader (#313)
-rw-r--r-- | core/agreement-state.go | 18 | ||||
-rw-r--r-- | core/agreement-state_test.go | 6 | ||||
-rw-r--r-- | core/agreement_test.go | 6 | ||||
-rw-r--r-- | core/authenticator_test.go | 15 | ||||
-rw-r--r-- | core/consensus_test.go | 14 | ||||
-rw-r--r-- | core/crypto_test.go | 8 | ||||
-rw-r--r-- | core/types/vote.go | 39 |
7 files changed, 45 insertions, 61 deletions
diff --git a/core/agreement-state.go b/core/agreement-state.go index fe329f8..9023799 100644 --- a/core/agreement-state.go +++ b/core/agreement-state.go @@ -72,11 +72,7 @@ func (s *initialState) nextState() (agreementState, error) { hash := s.a.recv.ProposeBlock() s.a.lock.Lock() defer s.a.lock.Unlock() - s.a.recv.ProposeVote(&types.Vote{ - Type: types.VoteInit, - BlockHash: hash, - Period: s.a.period, - }) + s.a.recv.ProposeVote(types.NewVote(types.VoteInit, hash, s.a.period)) return newPreCommitState(s.a), nil } @@ -98,11 +94,7 @@ func (s *preCommitState) nextState() (agreementState, error) { if hash == nullBlockHash { hash = s.a.leader.leaderBlockHash() } - s.a.recv.ProposeVote(&types.Vote{ - Type: types.VotePreCom, - BlockHash: hash, - Period: s.a.period, - }) + s.a.recv.ProposeVote(types.NewVote(types.VotePreCom, hash, s.a.period)) return newCommitState(s.a), nil } @@ -127,11 +119,7 @@ func (s *commitState) nextState() (agreementState, error) { } else { hash = skipBlockHash } - s.a.recv.ProposeVote(&types.Vote{ - Type: types.VoteCom, - BlockHash: hash, - Period: s.a.period, - }) + s.a.recv.ProposeVote(types.NewVote(types.VoteCom, hash, s.a.period)) return newForwardState(s.a), nil } diff --git a/core/agreement-state_test.go b/core/agreement-state_test.go index af1f1dd..6537d1b 100644 --- a/core/agreement-state_test.go +++ b/core/agreement-state_test.go @@ -74,11 +74,7 @@ func (s *AgreementStateTestSuite) prepareVote( nID types.NodeID, voteType types.VoteType, blockHash common.Hash, period uint64) ( vote *types.Vote) { - vote = &types.Vote{ - Type: voteType, - BlockHash: blockHash, - Period: period, - } + vote = types.NewVote(voteType, blockHash, period) s.Require().NoError(s.auths[nID].SignVote(vote)) return } diff --git a/core/agreement_test.go b/core/agreement_test.go index be66c96..52c9e4d 100644 --- a/core/agreement_test.go +++ b/core/agreement_test.go @@ -131,11 +131,7 @@ func (s *AgreementTestSuite) prepareVote( nID types.NodeID, voteType types.VoteType, blockHash common.Hash, period uint64) ( vote *types.Vote) { - vote = &types.Vote{ - Type: voteType, - BlockHash: blockHash, - Period: period, - } + vote = types.NewVote(voteType, blockHash, period) s.Require().NoError(s.auths[nID].SignVote(vote)) return } diff --git a/core/authenticator_test.go b/core/authenticator_test.go index 02758a6..68e4243 100644 --- a/core/authenticator_test.go +++ b/core/authenticator_test.go @@ -53,15 +53,12 @@ func (s *AuthenticatorTestSuite) TestBlock() { func (s *AuthenticatorTestSuite) TestVote() { k := s.setupAuthenticator() - v := &types.Vote{ - ProposerID: types.NodeID{Hash: common.NewRandomHash()}, - Type: types.VoteCom, - BlockHash: common.NewRandomHash(), - Period: 123, - Position: types.Position{ - ChainID: 4, - Height: 6, - }} + v := types.NewVote(types.VoteCom, common.NewRandomHash(), 123) + v.Position = types.Position{ + ChainID: 4, + Height: 6, + } + v.ProposerID = types.NodeID{Hash: common.NewRandomHash()} s.NoError(k.SignVote(v)) ok, err := k.VerifyVote(v) s.True(ok) diff --git a/core/consensus_test.go b/core/consensus_test.go index f518630..176cceb 100644 --- a/core/consensus_test.go +++ b/core/consensus_test.go @@ -569,11 +569,8 @@ func (s *ConsensusTestSuite) TestSyncBA() { Position: pos, } for _, auth := range auths { - vote := &types.Vote{ - Type: types.VoteCom, - BlockHash: hash, - Position: pos, - } + vote := types.NewVote(types.VoteCom, hash, 0) + vote.Position = pos s.Require().NoError(auth.SignVote(vote)) baResult.Votes = append(baResult.Votes, *vote) } @@ -604,11 +601,8 @@ func (s *ConsensusTestSuite) TestSyncBA() { s.Require().NoError(auths[0].SignVote(&baResult.Votes[0])) for _, auth := range auths { - vote := &types.Vote{ - Type: types.VoteCom, - BlockHash: hash, - Position: pos, - } + vote := types.NewVote(types.VoteCom, hash, 0) + vote.Position = pos s.Require().NoError(auth.SignVote(vote)) baResult.Votes = append(baResult.Votes, *vote) } diff --git a/core/crypto_test.go b/core/crypto_test.go index 5fc8ea7..5aefec5 100644 --- a/core/crypto_test.go +++ b/core/crypto_test.go @@ -131,12 +131,8 @@ func (s *CryptoTestSuite) TestVoteSignature() { s.Require().NoError(err) pub := prv.PublicKey() nID := types.NewNodeID(pub) - vote := &types.Vote{ - ProposerID: nID, - Type: types.VoteInit, - BlockHash: common.NewRandomHash(), - Period: 1, - } + vote := types.NewVote(types.VoteInit, common.NewRandomHash(), 1) + vote.ProposerID = nID vote.Signature, err = prv.Sign(hashVote(vote)) s.Require().NoError(err) ok, err := verifyVoteSignature(vote) diff --git a/core/types/vote.go b/core/types/vote.go index 5b99f22..12c3af8 100644 --- a/core/types/vote.go +++ b/core/types/vote.go @@ -36,13 +36,18 @@ const ( MaxVoteType ) +// VoteHeader is the header for vote, which can be used as map keys. +type VoteHeader struct { + ProposerID NodeID `json:"proposer_id"` + Type VoteType `json:"type"` + BlockHash common.Hash `json:"block_hash"` + Period uint64 `json:"period"` + Position Position `json:"position"` +} + // Vote is the vote structure defined in Crypto Shuffle Algorithm. type Vote struct { - ProposerID NodeID `json:"proposer_id"` - Type VoteType `json:"type"` - BlockHash common.Hash `json:"block_hash"` - Period uint64 `json:"period"` - Position Position `json:"position"` + VoteHeader `json:"header"` Signature crypto.Signature `json:"signature"` } @@ -51,14 +56,26 @@ func (v *Vote) String() string { &v.Position, v.Period, v.Type, v.BlockHash.String()[:6]) } +// NewVote constructs a Vote instance with header fields. +func NewVote(t VoteType, hash common.Hash, period uint64) *Vote { + return &Vote{ + VoteHeader: VoteHeader{ + Type: t, + BlockHash: hash, + Period: period, + }} +} + // Clone returns a deep copy of a vote. func (v *Vote) Clone() *Vote { return &Vote{ - ProposerID: v.ProposerID, - Type: v.Type, - BlockHash: v.BlockHash, - Period: v.Period, - Position: v.Position, - Signature: v.Signature.Clone(), + VoteHeader: VoteHeader{ + ProposerID: v.ProposerID, + Type: v.Type, + BlockHash: v.BlockHash, + Period: v.Period, + Position: v.Position, + }, + Signature: v.Signature.Clone(), } } |