Skip to content

Commit

Permalink
fix: some state tests
Browse files Browse the repository at this point in the history
Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com>
  • Loading branch information
gfanton committed Jan 10, 2024
1 parent deb40c2 commit 455e24f
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions tm2/pkg/bft/consensus/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ func TestStateEnterProposeYesPrivValidator(t *testing.T) {
// Wait for new round so proposer is set.
ensureNewRound(newRoundCh, height, round)
ensureNewProposal(proposalCh, height, round)
require.FailNow(t, "XXX: new round loop block get round state")

// Check that Proposal, ProposalBlock, ProposalBlockParts are set.
rs := ensureGetRoundState(cs)
Expand Down Expand Up @@ -295,6 +296,8 @@ func TestStateFullRound1(t *testing.T) {
ensureNewRound(newRoundCh, height, round)

ensureNewProposal(propCh, height, round)

require.FailNow(t, "XXX: unable to get the proper order to avoid failing")
propBlockHash := ensureGetRoundState(cs).ProposalBlock.Hash()

ensurePrevote(voteCh, height, round) // wait for prevote
Expand All @@ -306,6 +309,7 @@ func TestStateFullRound1(t *testing.T) {
ensureNewRound(newRoundCh, height+1, 0)

validateLastPrecommit(cs, vss[0], propBlockHash)

}

// nil is proposed, so prevote and precommit nil
Expand Down Expand Up @@ -410,18 +414,18 @@ func TestStateLockNoPOL(t *testing.T) {
ensureNewRound(newRoundCh, height, round)

ensureNewProposal(proposalCh, height, round)
ensurePrevote(voteCh, height, round) // prevote
roundState := ensureGetRoundState(cs1)
theBlockHash := roundState.ProposalBlock.Hash()
thePartSetHeader := roundState.ProposalBlockParts.Header()

ensurePrevote(voteCh, height, round) // prevote
validatePrevote(cs1, round, vss[0], theBlockHash)

// we should now be stuck in limbo forever, waiting for more prevotes
// prevote arrives from vs2:
signAddVotes(cs1, types.PrevoteType, theBlockHash, thePartSetHeader, vs2)
ensurePrevote(voteCh, height, round) // prevote

ensurePrevote(voteCh, height, round) // prevote
ensurePrecommit(voteCh, height, round) // precommit

// the proposed block should now be locked and our precommit added
validatePrecommit(t, cs1, round, round, vss[0], theBlockHash, theBlockHash)

Expand Down Expand Up @@ -451,14 +455,13 @@ func TestStateLockNoPOL(t *testing.T) {
// now we're on a new round and not the proposer, so wait for timeout
ensureNewTimeout(timeoutProposeCh, height, round, cs1.config.Propose(round).Nanoseconds())

// wait to finish prevote
ensurePrevote(voteCh, height, round)
rs := ensureGetRoundState(cs1)

if rs.ProposalBlock != nil {
panic("Expected proposal block to be nil")
}

// wait to finish prevote
ensurePrevote(voteCh, height, round)
// we should have prevoted our locked block
validatePrevote(cs1, round, vss[0], rs.LockedBlock.Hash())

Expand Down Expand Up @@ -491,18 +494,16 @@ func TestStateLockNoPOL(t *testing.T) {
*/

incrementRound(vs2)

ensureNewProposal(proposalCh, height, round)
ensurePrevote(voteCh, height, round) // prevote
rs = ensureGetRoundState(cs1)

// now we're on a new round and are the proposer
if !bytes.Equal(rs.ProposalBlock.Hash(), rs.LockedBlock.Hash()) {
panic(fmt.Sprintf("Expected proposal block to be locked block. Got %v, Expected %v", rs.ProposalBlock, rs.LockedBlock))
}

ensurePrevote(voteCh, height, round) // prevote
validatePrevote(cs1, round, vss[0], rs.LockedBlock.Hash())

signAddVotes(cs1, types.PrevoteType, hash, rs.ProposalBlock.MakePartSet(partSize).Header(), vs2)
ensurePrevote(voteCh, height, round)

Expand Down Expand Up @@ -593,11 +594,12 @@ func TestStateLockPOLRelock(t *testing.T) {

ensureNewRound(newRoundCh, height, round)
ensureNewProposal(proposalCh, height, round)
ensurePrevote(voteCh, height, round) // prevote
rs := ensureGetRoundState(cs1)
theBlockHash := rs.ProposalBlock.Hash()
theBlockParts := rs.ProposalBlockParts.Header()

ensurePrevote(voteCh, height, round) // prevote
validatePrevote(cs1, round, vss[0], theBlockHash)

signAddVotes(cs1, types.PrevoteType, theBlockHash, theBlockParts, vs2, vs3, vs4)

Expand Down Expand Up @@ -691,11 +693,11 @@ func TestStateLockPOLUnlock(t *testing.T) {

ensureNewRound(newRoundCh, height, round)
ensureNewProposal(proposalCh, height, round)
ensurePrevote(voteCh, height, round)
rs := ensureGetRoundState(cs1)
theBlockHash := rs.ProposalBlock.Hash()
theBlockParts := rs.ProposalBlockParts.Header()

ensurePrevote(voteCh, height, round)
validatePrevote(cs1, round, vss[0], theBlockHash)

signAddVotes(cs1, types.PrevoteType, theBlockHash, theBlockParts, vs2, vs3, vs4)
Expand Down Expand Up @@ -786,10 +788,10 @@ func TestStateLockPOLSafety1(t *testing.T) {

ensureNewRound(newRoundCh, height, round)
ensureNewProposal(proposalCh, height, round)
ensurePrevote(voteCh, height, round)
rs := ensureGetRoundState(cs1)
propBlock := rs.ProposalBlock

ensurePrevote(voteCh, height, round)
validatePrevote(cs1, round, vss[0], propBlock.Hash())

// the others sign a polka but we don't see it
Expand Down Expand Up @@ -824,17 +826,15 @@ func TestStateLockPOLSafety1(t *testing.T) {
// a polka happened but we didn't see it!
*/

// go to prevote, prevote for proposal block
ensureNewProposal(proposalCh, height, round)

ensurePrevote(voteCh, height, round)
rs = ensureGetRoundState(cs1)

if rs.LockedBlock != nil {
panic("we should not be locked!")
}
t.Logf("new prop hash %v", fmt.Sprintf("%X", propBlockHash))

// go to prevote, prevote for proposal block
ensurePrevote(voteCh, height, round)
validatePrevote(cs1, round, vss[0], propBlockHash)

// now we see the others prevote for it, so we should lock on it
Expand Down Expand Up @@ -1011,11 +1011,11 @@ func TestProposeValidBlock(t *testing.T) {

ensureNewRound(newRoundCh, height, round)
ensureNewProposal(proposalCh, height, round)
ensurePrevote(voteCh, height, round)
rs := ensureGetRoundState(cs1)
propBlock := rs.ProposalBlock
propBlockHash := propBlock.Hash()

ensurePrevote(voteCh, height, round)
validatePrevote(cs1, round, vss[0], propBlockHash)

// the others sign a polka
Expand Down Expand Up @@ -1072,7 +1072,7 @@ func TestProposeValidBlock(t *testing.T) {
t.Log("### ONTO ROUND 4")

ensureNewProposal(proposalCh, height, round)

ensurePrevote(voteCh, height, round)
rs = ensureGetRoundState(cs1)
assert.True(t, bytes.Equal(rs.ProposalBlock.Hash(), propBlockHash))
assert.True(t, bytes.Equal(rs.ProposalBlock.Hash(), rs.ValidBlock.Hash()))
Expand Down Expand Up @@ -1108,12 +1108,12 @@ func TestSetValidBlockOnDelayedPrevote(t *testing.T) {

ensureNewRound(newRoundCh, height, round)
ensureNewProposal(proposalCh, height, round)
ensurePrevote(voteCh, height, round)
rs := ensureGetRoundState(cs1)
propBlock := rs.ProposalBlock
propBlockHash := propBlock.Hash()
propBlockParts := propBlock.MakePartSet(partSize)

ensurePrevote(voteCh, height, round)
validatePrevote(cs1, round, vss[0], propBlockHash)

// vs2 send prevote for propBlock
Expand Down Expand Up @@ -1267,6 +1267,8 @@ func TestWaitingTimeoutProposeOnNewRound(t *testing.T) {
round++ // moving to the next round
ensureNewRound(newRoundCh, height, round)

require.FailNow(t, "XXX: this part is flaky")

rs := ensureGetRoundState(cs1)
assert.True(t, rs.Step == cstypes.RoundStepPropose) // P0 does not prevote before timeoutPropose expires

Expand Down Expand Up @@ -1475,11 +1477,11 @@ func TestStartNextHeightCorrectly(t *testing.T) {

ensureNewRound(newRoundCh, height, round)
ensureNewProposal(proposalCh, height, round)
ensurePrevote(voteCh, height, round)
rs := ensureGetRoundState(cs1)
theBlockHash := rs.ProposalBlock.Hash()
theBlockParts := rs.ProposalBlockParts.Header()

ensurePrevote(voteCh, height, round)
validatePrevote(cs1, round, vss[0], theBlockHash)

signAddVotes(cs1, types.PrevoteType, theBlockHash, theBlockParts, vs2, vs3, vs4)
Expand All @@ -1494,6 +1496,9 @@ func TestStartNextHeightCorrectly(t *testing.T) {
time.Sleep(5 * time.Millisecond)
signAddVotes(cs1, types.PrecommitType, theBlockHash, theBlockParts, vs4)

require.FailNow(t, "XXX: this part is flaky")

// XXX: this make the test hang indefinitely need double check
rs = ensureGetRoundState(cs1)
assert.True(t, rs.TriggeredTimeoutPrecommit)

Expand All @@ -1504,6 +1509,7 @@ func TestStartNextHeightCorrectly(t *testing.T) {
height, round = height+1, 0
ensureNewRound(newRoundCh, height, round)
ensureNewTimeout(timeoutProposeCh, height, round, cs1.config.Propose(round).Nanoseconds())
ensurePrevote(voteCh, height, round)
rs = ensureGetRoundState(cs1)
assert.False(t, rs.TriggeredTimeoutPrecommit, "triggeredTimeoutPrecommit should be false at the beginning of each round")
}
Expand Down

0 comments on commit 455e24f

Please sign in to comment.