mirror of
https://github.com/fluencelabs/tendermint
synced 2025-04-25 14:52:17 +00:00
Merge pull request #1328 from tendermint/bucky/add-vote-readability
addVote readability
This commit is contained in:
commit
20b198681b
@ -152,6 +152,7 @@ func TestMempoolRmBadTx(t *testing.T) {
|
|||||||
txs := cs.mempool.Reap(1)
|
txs := cs.mempool.Reap(1)
|
||||||
if len(txs) == 0 {
|
if len(txs) == 0 {
|
||||||
emptyMempoolCh <- struct{}{}
|
emptyMempoolCh <- struct{}{}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
time.Sleep(10 * time.Millisecond)
|
time.Sleep(10 * time.Millisecond)
|
||||||
}
|
}
|
||||||
|
@ -1359,111 +1359,115 @@ func (cs *ConsensusState) addVote(vote *types.Vote, peerID p2p.ID) (added bool,
|
|||||||
return added, ErrVoteHeightMismatch
|
return added, ErrVoteHeightMismatch
|
||||||
}
|
}
|
||||||
added, err = cs.LastCommit.AddVote(vote)
|
added, err = cs.LastCommit.AddVote(vote)
|
||||||
if added {
|
if !added {
|
||||||
cs.Logger.Info(cmn.Fmt("Added to lastPrecommits: %v", cs.LastCommit.StringShort()))
|
return added, err
|
||||||
cs.eventBus.PublishEventVote(types.EventDataVote{vote})
|
}
|
||||||
|
|
||||||
// if we can skip timeoutCommit and have all the votes now,
|
cs.Logger.Info(cmn.Fmt("Added to lastPrecommits: %v", cs.LastCommit.StringShort()))
|
||||||
if cs.config.SkipTimeoutCommit && cs.LastCommit.HasAll() {
|
cs.eventBus.PublishEventVote(types.EventDataVote{vote})
|
||||||
// go straight to new round (skip timeout commit)
|
|
||||||
// cs.scheduleTimeout(time.Duration(0), cs.Height, 0, cstypes.RoundStepNewHeight)
|
// if we can skip timeoutCommit and have all the votes now,
|
||||||
cs.enterNewRound(cs.Height, 0)
|
if cs.config.SkipTimeoutCommit && cs.LastCommit.HasAll() {
|
||||||
}
|
// go straight to new round (skip timeout commit)
|
||||||
|
// cs.scheduleTimeout(time.Duration(0), cs.Height, 0, cstypes.RoundStepNewHeight)
|
||||||
|
cs.enterNewRound(cs.Height, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// A prevote/precommit for this height?
|
// Height mismatch is ignored.
|
||||||
if vote.Height == cs.Height {
|
// Not necessarily a bad peer, but not favourable behaviour.
|
||||||
height := cs.Height
|
if vote.Height != cs.Height {
|
||||||
added, err = cs.Votes.AddVote(vote, peerID)
|
err = ErrVoteHeightMismatch
|
||||||
if added {
|
cs.Logger.Info("Vote ignored and not added", "voteHeight", vote.Height, "csHeight", cs.Height, "err", err)
|
||||||
cs.eventBus.PublishEventVote(types.EventDataVote{vote})
|
return
|
||||||
|
}
|
||||||
|
|
||||||
switch vote.Type {
|
height := cs.Height
|
||||||
case types.VoteTypePrevote:
|
added, err = cs.Votes.AddVote(vote, peerID)
|
||||||
prevotes := cs.Votes.Prevotes(vote.Round)
|
if !added {
|
||||||
cs.Logger.Info("Added to prevote", "vote", vote, "prevotes", prevotes.StringShort())
|
|
||||||
blockID, ok := prevotes.TwoThirdsMajority()
|
|
||||||
// First, unlock if prevotes is a valid POL.
|
|
||||||
// >> lockRound < POLRound <= unlockOrChangeLockRound (see spec)
|
|
||||||
// NOTE: If (lockRound < POLRound) but !(POLRound <= unlockOrChangeLockRound),
|
|
||||||
// we'll still enterNewRound(H,vote.R) and enterPrecommit(H,vote.R) to process it
|
|
||||||
// there.
|
|
||||||
if (cs.LockedBlock != nil) && (cs.LockedRound < vote.Round) && (vote.Round <= cs.Round) {
|
|
||||||
if ok && !cs.LockedBlock.HashesTo(blockID.Hash) {
|
|
||||||
cs.Logger.Info("Unlocking because of POL.", "lockedRound", cs.LockedRound, "POLRound", vote.Round)
|
|
||||||
cs.LockedRound = 0
|
|
||||||
cs.LockedBlock = nil
|
|
||||||
cs.LockedBlockParts = nil
|
|
||||||
cs.eventBus.PublishEventUnlock(cs.RoundStateEvent())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Update ValidBlock
|
|
||||||
if ok && !blockID.IsZero() && !cs.ValidBlock.HashesTo(blockID.Hash) && vote.Round > cs.ValidRound {
|
|
||||||
// update valid value
|
|
||||||
if cs.ProposalBlock.HashesTo(blockID.Hash) {
|
|
||||||
cs.ValidRound = vote.Round
|
|
||||||
cs.ValidBlock = cs.ProposalBlock
|
|
||||||
cs.ValidBlockParts = cs.ProposalBlockParts
|
|
||||||
}
|
|
||||||
//TODO: We might want to update ValidBlock also in case we don't have that block yet,
|
|
||||||
// and obtain the required block using gossiping
|
|
||||||
}
|
|
||||||
|
|
||||||
if cs.Round <= vote.Round && prevotes.HasTwoThirdsAny() {
|
|
||||||
// Round-skip over to PrevoteWait or goto Precommit.
|
|
||||||
cs.enterNewRound(height, vote.Round) // if the vote is ahead of us
|
|
||||||
if prevotes.HasTwoThirdsMajority() {
|
|
||||||
cs.enterPrecommit(height, vote.Round)
|
|
||||||
} else {
|
|
||||||
cs.enterPrevote(height, vote.Round) // if the vote is ahead of us
|
|
||||||
cs.enterPrevoteWait(height, vote.Round)
|
|
||||||
}
|
|
||||||
} else if cs.Proposal != nil && 0 <= cs.Proposal.POLRound && cs.Proposal.POLRound == vote.Round {
|
|
||||||
// If the proposal is now complete, enter prevote of cs.Round.
|
|
||||||
if cs.isProposalComplete() {
|
|
||||||
cs.enterPrevote(height, cs.Round)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case types.VoteTypePrecommit:
|
|
||||||
precommits := cs.Votes.Precommits(vote.Round)
|
|
||||||
cs.Logger.Info("Added to precommit", "vote", vote, "precommits", precommits.StringShort())
|
|
||||||
blockID, ok := precommits.TwoThirdsMajority()
|
|
||||||
if ok {
|
|
||||||
if len(blockID.Hash) == 0 {
|
|
||||||
cs.enterNewRound(height, vote.Round+1)
|
|
||||||
} else {
|
|
||||||
cs.enterNewRound(height, vote.Round)
|
|
||||||
cs.enterPrecommit(height, vote.Round)
|
|
||||||
cs.enterCommit(height, vote.Round)
|
|
||||||
|
|
||||||
if cs.config.SkipTimeoutCommit && precommits.HasAll() {
|
|
||||||
// if we have all the votes now,
|
|
||||||
// go straight to new round (skip timeout commit)
|
|
||||||
// cs.scheduleTimeout(time.Duration(0), cs.Height, 0, cstypes.RoundStepNewHeight)
|
|
||||||
cs.enterNewRound(cs.Height, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
} else if cs.Round <= vote.Round && precommits.HasTwoThirdsAny() {
|
|
||||||
cs.enterNewRound(height, vote.Round)
|
|
||||||
cs.enterPrecommit(height, vote.Round)
|
|
||||||
cs.enterPrecommitWait(height, vote.Round)
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
cmn.PanicSanity(cmn.Fmt("Unexpected vote type %X", vote.Type)) // Should not happen.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Either duplicate, or error upon cs.Votes.AddByIndex()
|
// Either duplicate, or error upon cs.Votes.AddByIndex()
|
||||||
return
|
return
|
||||||
} else {
|
|
||||||
err = ErrVoteHeightMismatch
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Height mismatch, bad peer?
|
cs.eventBus.PublishEventVote(types.EventDataVote{vote})
|
||||||
cs.Logger.Info("Vote ignored and not added", "voteHeight", vote.Height, "csHeight", cs.Height, "err", err)
|
|
||||||
|
switch vote.Type {
|
||||||
|
case types.VoteTypePrevote:
|
||||||
|
prevotes := cs.Votes.Prevotes(vote.Round)
|
||||||
|
cs.Logger.Info("Added to prevote", "vote", vote, "prevotes", prevotes.StringShort())
|
||||||
|
blockID, ok := prevotes.TwoThirdsMajority()
|
||||||
|
// First, unlock if prevotes is a valid POL.
|
||||||
|
// >> lockRound < POLRound <= unlockOrChangeLockRound (see spec)
|
||||||
|
// NOTE: If (lockRound < POLRound) but !(POLRound <= unlockOrChangeLockRound),
|
||||||
|
// we'll still enterNewRound(H,vote.R) and enterPrecommit(H,vote.R) to process it
|
||||||
|
// there.
|
||||||
|
if (cs.LockedBlock != nil) && (cs.LockedRound < vote.Round) && (vote.Round <= cs.Round) {
|
||||||
|
if ok && !cs.LockedBlock.HashesTo(blockID.Hash) {
|
||||||
|
cs.Logger.Info("Unlocking because of POL.", "lockedRound", cs.LockedRound, "POLRound", vote.Round)
|
||||||
|
cs.LockedRound = 0
|
||||||
|
cs.LockedBlock = nil
|
||||||
|
cs.LockedBlockParts = nil
|
||||||
|
cs.eventBus.PublishEventUnlock(cs.RoundStateEvent())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Update ValidBlock
|
||||||
|
if ok && !blockID.IsZero() && !cs.ValidBlock.HashesTo(blockID.Hash) && vote.Round > cs.ValidRound {
|
||||||
|
// update valid value
|
||||||
|
if cs.ProposalBlock.HashesTo(blockID.Hash) {
|
||||||
|
cs.ValidRound = vote.Round
|
||||||
|
cs.ValidBlock = cs.ProposalBlock
|
||||||
|
cs.ValidBlockParts = cs.ProposalBlockParts
|
||||||
|
}
|
||||||
|
//TODO: We might want to update ValidBlock also in case we don't have that block yet,
|
||||||
|
// and obtain the required block using gossiping
|
||||||
|
}
|
||||||
|
|
||||||
|
if cs.Round <= vote.Round && prevotes.HasTwoThirdsAny() {
|
||||||
|
// Round-skip over to PrevoteWait or goto Precommit.
|
||||||
|
cs.enterNewRound(height, vote.Round) // if the vote is ahead of us
|
||||||
|
if prevotes.HasTwoThirdsMajority() {
|
||||||
|
cs.enterPrecommit(height, vote.Round)
|
||||||
|
} else {
|
||||||
|
cs.enterPrevote(height, vote.Round) // if the vote is ahead of us
|
||||||
|
cs.enterPrevoteWait(height, vote.Round)
|
||||||
|
}
|
||||||
|
} else if cs.Proposal != nil && 0 <= cs.Proposal.POLRound && cs.Proposal.POLRound == vote.Round {
|
||||||
|
// If the proposal is now complete, enter prevote of cs.Round.
|
||||||
|
if cs.isProposalComplete() {
|
||||||
|
cs.enterPrevote(height, cs.Round)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case types.VoteTypePrecommit:
|
||||||
|
precommits := cs.Votes.Precommits(vote.Round)
|
||||||
|
cs.Logger.Info("Added to precommit", "vote", vote, "precommits", precommits.StringShort())
|
||||||
|
blockID, ok := precommits.TwoThirdsMajority()
|
||||||
|
if ok {
|
||||||
|
if len(blockID.Hash) == 0 {
|
||||||
|
cs.enterNewRound(height, vote.Round+1)
|
||||||
|
} else {
|
||||||
|
cs.enterNewRound(height, vote.Round)
|
||||||
|
cs.enterPrecommit(height, vote.Round)
|
||||||
|
cs.enterCommit(height, vote.Round)
|
||||||
|
|
||||||
|
if cs.config.SkipTimeoutCommit && precommits.HasAll() {
|
||||||
|
// if we have all the votes now,
|
||||||
|
// go straight to new round (skip timeout commit)
|
||||||
|
// cs.scheduleTimeout(time.Duration(0), cs.Height, 0, cstypes.RoundStepNewHeight)
|
||||||
|
cs.enterNewRound(cs.Height, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
} else if cs.Round <= vote.Round && precommits.HasTwoThirdsAny() {
|
||||||
|
cs.enterNewRound(height, vote.Round)
|
||||||
|
cs.enterPrecommit(height, vote.Round)
|
||||||
|
cs.enterPrecommitWait(height, vote.Round)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
panic(cmn.Fmt("Unexpected vote type %X", vote.Type)) // go-wire should prevent this.
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user