From 5bf0040f14147c8a2d8a1f445a3e925906fd8abd Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Wed, 12 Aug 2015 22:36:43 -0700 Subject: [PATCH] Parts -> PartsHeader; *_parts -> *_parts_header in sign_bytes --- consensus/state.go | 10 +++++----- state/state_test.go | 10 +++++----- types/validator_set.go | 2 +- types/vote.go | 18 +++++++++--------- types/vote_set.go | 26 +++++++++++++------------- types/vote_set_test.go | 26 +++++++++++++------------- 6 files changed, 46 insertions(+), 46 deletions(-) diff --git a/consensus/state.go b/consensus/state.go index 7b2137bc7..14e2b36e6 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -1173,11 +1173,11 @@ func (cs *ConsensusState) signAddVote(type_ byte, hash []byte, header types.Part return nil } vote := &types.Vote{ - Height: cs.Height, - Round: cs.Round, - Type: type_, - BlockHash: hash, - BlockParts: header, + Height: cs.Height, + Round: cs.Round, + Type: type_, + BlockHash: hash, + BlockPartsHeader: header, } err := cs.privValidator.SignVote(cs.state.ChainID, vote) if err == nil { diff --git a/state/state_test.go b/state/state_test.go index 6eda39b82..8eaebe371 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -623,11 +623,11 @@ func TestAddValidator(t *testing.T) { // The validation for the next block should only require 1 signature // (the new validator wasn't active for block0) precommit0 := &types.Vote{ - Height: 1, - Round: 0, - Type: types.VoteTypePrecommit, - BlockHash: block0.Hash(), - BlockParts: block0Parts.Header(), + Height: 1, + Round: 0, + Type: types.VoteTypePrecommit, + BlockHash: block0.Hash(), + BlockPartsHeader: block0Parts.Header(), } privValidators[0].SignVote(s0.ChainID, precommit0) diff --git a/types/validator_set.go b/types/validator_set.go index ecf7287a7..7dc4ee625 100644 --- a/types/validator_set.go +++ b/types/validator_set.go @@ -237,7 +237,7 @@ func (valSet *ValidatorSet) VerifyValidation(chainID string, if !bytes.Equal(precommit.BlockHash, hash) { continue // Not an error, but doesn't count } - if !parts.Equals(precommit.BlockParts) { + if !parts.Equals(precommit.BlockPartsHeader) { continue // Not an error, but doesn't count } // Good precommit! diff --git a/types/vote.go b/types/vote.go index c313c2497..c23ca9cb8 100644 --- a/types/vote.go +++ b/types/vote.go @@ -6,8 +6,8 @@ import ( "io" acm "github.com/tendermint/tendermint/account" - "github.com/tendermint/tendermint/wire" . "github.com/tendermint/tendermint/common" + "github.com/tendermint/tendermint/wire" ) var ( @@ -28,12 +28,12 @@ func (err *ErrVoteConflictingSignature) Error() string { // Represents a prevote, precommit, or commit vote from validators for consensus. type Vote struct { - Height int `json:"height"` - Round int `json:"round"` - Type byte `json:"type"` - BlockHash []byte `json:"block_hash"` // empty if vote is nil. - BlockParts PartSetHeader `json:"block_parts"` // zero if vote is nil. - Signature acm.SignatureEd25519 `json:"signature"` + Height int `json:"height"` + Round int `json:"round"` + Type byte `json:"type"` + BlockHash []byte `json:"block_hash"` // empty if vote is nil. + BlockPartsHeader PartSetHeader `json:"block_parts_header"` // zero if vote is nil. + Signature acm.SignatureEd25519 `json:"signature"` } // Types of votes @@ -44,7 +44,7 @@ const ( func (vote *Vote) WriteSignBytes(chainID string, w io.Writer, n *int64, err *error) { wire.WriteTo([]byte(Fmt(`{"chain_id":"%s"`, chainID)), w, n, err) - wire.WriteTo([]byte(Fmt(`,"vote":{"block_hash":"%X","block_parts":%v`, vote.BlockHash, vote.BlockParts)), w, n, err) + wire.WriteTo([]byte(Fmt(`,"vote":{"block_hash":"%X","block_parts_header":%v`, vote.BlockHash, vote.BlockPartsHeader)), w, n, err) wire.WriteTo([]byte(Fmt(`,"height":%v,"round":%v,"type":%v}}`, vote.Height, vote.Round, vote.Type)), w, n, err) } @@ -67,7 +67,7 @@ func (vote *Vote) String() string { PanicSanity("Unknown vote type") } - return fmt.Sprintf("Vote{%v/%02d/%v(%v) %X#%v %v}", vote.Height, vote.Round, vote.Type, typeString, Fingerprint(vote.BlockHash), vote.BlockParts, vote.Signature) + return fmt.Sprintf("Vote{%v/%02d/%v(%v) %X#%v %v}", vote.Height, vote.Round, vote.Type, typeString, Fingerprint(vote.BlockHash), vote.BlockPartsHeader, vote.Signature) } //-------------------------------------------------------------------------------- diff --git a/types/vote_set.go b/types/vote_set.go index 1b742b6cf..cba34eef1 100644 --- a/types/vote_set.go +++ b/types/vote_set.go @@ -21,15 +21,15 @@ type VoteSet struct { round int type_ byte - mtx sync.Mutex - valSet *ValidatorSet - votes []*Vote // validator index -> vote - votesBitArray *BitArray // validator index -> has vote? - votesByBlock map[string]int64 // string(blockHash)+string(blockParts) -> vote sum. - totalVotes int64 - maj23Hash []byte - maj23Parts PartSetHeader - maj23Exists bool + mtx sync.Mutex + valSet *ValidatorSet + votes []*Vote // validator index -> vote + votesBitArray *BitArray // validator index -> has vote? + votesByBlock map[string]int64 // string(blockHash)+string(blockParts) -> vote sum. + totalVotes int64 + maj23Hash []byte + maj23PartsHeader PartSetHeader + maj23Exists bool } // Constructs a new VoteSet struct used to accumulate votes for given height/round. @@ -149,7 +149,7 @@ func (voteSet *VoteSet) addVote(val *Validator, valIndex int, vote *Vote) (bool, // Add vote. voteSet.votes[valIndex] = vote voteSet.votesBitArray.SetIndex(valIndex, true) - blockKey := string(vote.BlockHash) + string(wire.BinaryBytes(vote.BlockParts)) + blockKey := string(vote.BlockHash) + string(wire.BinaryBytes(vote.BlockPartsHeader)) totalBlockHashVotes := voteSet.votesByBlock[blockKey] + val.VotingPower voteSet.votesByBlock[blockKey] = totalBlockHashVotes voteSet.totalVotes += val.VotingPower @@ -158,7 +158,7 @@ func (voteSet *VoteSet) addVote(val *Validator, valIndex int, vote *Vote) (bool, if totalBlockHashVotes > voteSet.valSet.TotalVotingPower()*2/3 && (totalBlockHashVotes-val.VotingPower) <= voteSet.valSet.TotalVotingPower()*2/3 { voteSet.maj23Hash = vote.BlockHash - voteSet.maj23Parts = vote.BlockParts + voteSet.maj23PartsHeader = vote.BlockPartsHeader voteSet.maj23Exists = true } @@ -223,7 +223,7 @@ func (voteSet *VoteSet) TwoThirdsMajority() (hash []byte, parts PartSetHeader, o voteSet.mtx.Lock() defer voteSet.mtx.Unlock() if voteSet.maj23Exists { - return voteSet.maj23Hash, voteSet.maj23Parts, true + return voteSet.maj23Hash, voteSet.maj23PartsHeader, true } else { return nil, PartSetHeader{}, false } @@ -287,7 +287,7 @@ func (voteSet *VoteSet) MakeValidation() *Validation { if !bytes.Equal(vote.BlockHash, voteSet.maj23Hash) { return false } - if !vote.BlockParts.Equals(voteSet.maj23Parts) { + if !vote.BlockPartsHeader.Equals(voteSet.maj23PartsHeader) { return false } precommits[valIndex] = vote diff --git a/types/vote_set_test.go b/types/vote_set_test.go index 1f7d4025e..d521478e0 100644 --- a/types/vote_set_test.go +++ b/types/vote_set_test.go @@ -53,9 +53,9 @@ func withBlockHash(vote *Vote, blockHash []byte) *Vote { } // Convenience: Return new vote with different blockParts -func withBlockParts(vote *Vote, blockParts PartSetHeader) *Vote { +func withBlockPartsHeader(vote *Vote, blockPartsHeader PartSetHeader) *Vote { vote = vote.Copy() - vote.BlockParts = blockParts + vote.BlockPartsHeader = blockPartsHeader return vote } @@ -138,9 +138,9 @@ func Test2_3MajorityRedux(t *testing.T) { blockHash := CRandBytes(32) blockPartsTotal := 123 - blockParts := PartSetHeader{blockPartsTotal, CRandBytes(32)} + blockPartsHeader := PartSetHeader{blockPartsTotal, CRandBytes(32)} - vote := &Vote{Height: height, Round: round, Type: VoteTypePrevote, BlockHash: blockHash, BlockParts: blockParts} + vote := &Vote{Height: height, Round: round, Type: VoteTypePrevote, BlockHash: blockHash, BlockPartsHeader: blockPartsHeader} // 66 out of 100 voted for nil. for i := 0; i < 66; i++ { @@ -162,8 +162,8 @@ func Test2_3MajorityRedux(t *testing.T) { // 68th validator voted for a different BlockParts PartSetHeader { - blockParts := PartSetHeader{blockPartsTotal, CRandBytes(32)} - signAddVote(privValidators[67], withBlockParts(vote, blockParts), voteSet) + blockPartsHeader := PartSetHeader{blockPartsTotal, CRandBytes(32)} + signAddVote(privValidators[67], withBlockPartsHeader(vote, blockPartsHeader), voteSet) hash, header, ok = voteSet.TwoThirdsMajority() if hash != nil || !header.IsZero() || ok { t.Errorf("There should be no 2/3 majority: last vote added had different PartSetHeader Hash") @@ -172,8 +172,8 @@ func Test2_3MajorityRedux(t *testing.T) { // 69th validator voted for different BlockParts Total { - blockParts := PartSetHeader{blockPartsTotal + 1, blockParts.Hash} - signAddVote(privValidators[68], withBlockParts(vote, blockParts), voteSet) + blockPartsHeader := PartSetHeader{blockPartsTotal + 1, blockPartsHeader.Hash} + signAddVote(privValidators[68], withBlockPartsHeader(vote, blockPartsHeader), voteSet) hash, header, ok = voteSet.TwoThirdsMajority() if hash != nil || !header.IsZero() || ok { t.Errorf("There should be no 2/3 majority: last vote added had different PartSetHeader Total") @@ -189,11 +189,11 @@ func Test2_3MajorityRedux(t *testing.T) { } } - // 71st validator voted for the right BlockHash & BlockParts + // 71st validator voted for the right BlockHash & BlockPartsHeader { signAddVote(privValidators[70], vote, voteSet) hash, header, ok = voteSet.TwoThirdsMajority() - if !bytes.Equal(hash, blockHash) || !header.Equals(blockParts) || !ok { + if !bytes.Equal(hash, blockHash) || !header.Equals(blockPartsHeader) || !ok { t.Errorf("There should be 2/3 majority") } } @@ -238,10 +238,10 @@ func TestBadVotes(t *testing.T) { func TestMakeValidation(t *testing.T) { height, round := 1, 0 voteSet, _, privValidators := randVoteSet(height, round, VoteTypePrecommit, 10, 1) - blockHash, blockParts := CRandBytes(32), PartSetHeader{123, CRandBytes(32)} + blockHash, blockPartsHeader := CRandBytes(32), PartSetHeader{123, CRandBytes(32)} vote := &Vote{Height: height, Round: round, Type: VoteTypePrecommit, - BlockHash: blockHash, BlockParts: blockParts} + BlockHash: blockHash, BlockPartsHeader: blockPartsHeader} // 6 out of 10 voted for some block. for i := 0; i < 6; i++ { @@ -254,7 +254,7 @@ func TestMakeValidation(t *testing.T) { // 7th voted for some other block. { vote := withBlockHash(vote, RandBytes(32)) - vote = withBlockParts(vote, PartSetHeader{123, RandBytes(32)}) + vote = withBlockPartsHeader(vote, PartSetHeader{123, RandBytes(32)}) signAddVote(privValidators[6], vote, voteSet) }