From 0ce3805a4bab86534e933d4c35968c1ef419217f Mon Sep 17 00:00:00 2001 From: William Banfield Date: Thu, 20 Jan 2022 14:59:25 -0500 Subject: [PATCH] consensus: fix round advances in state test --- internal/consensus/common_test.go | 25 +++++++++++++++++++++++++ internal/consensus/state_test.go | 9 +++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/internal/consensus/common_test.go b/internal/consensus/common_test.go index 3a6541f9f..8bfaf6dca 100644 --- a/internal/consensus/common_test.go +++ b/internal/consensus/common_test.go @@ -686,7 +686,32 @@ func ensurePrevote(t *testing.T, voteCh <-chan tmpubsub.Message, height int64, r t.Helper() ensureVote(t, voteCh, height, round, tmproto.PrevoteType) } +func ensurePrevoteMatch(t *testing.T, voteCh <-chan tmpubsub.Message, height int64, round int32, hash []byte) { + t.Helper() + ensureVoteMatch(t, voteCh, height, round, hash, tmproto.PrevoteType) +} +func ensureVoteMatch(t *testing.T, voteCh <-chan tmpubsub.Message, height int64, round int32, hash []byte, voteType tmproto.SignedMsgType) { + t.Helper() + select { + case <-time.After(ensureTimeout): + t.Fatal("Timeout expired while waiting for NewVote event") + case msg := <-voteCh: + voteEvent, ok := msg.Data().(types.EventDataVote) + require.True(t, ok, "expected a EventDataVote, got %T. Wrong subscription channel?", + msg.Data()) + vote := voteEvent.Vote + require.Equal(t, height, vote.Height) + require.Equal(t, round, vote.Round) + + require.Equal(t, voteType, vote.Type) + if hash == nil { + require.Nil(t, vote.BlockID.Hash, "Expected prevote to be for nil, got %X", vote.BlockID.Hash) + } else { + require.True(t, bytes.Equal(vote.BlockID.Hash, hash), "Expected prevote to be for %X, got %X", hash, vote.BlockID.Hash) + } + } +} func ensureVote(t *testing.T, voteCh <-chan tmpubsub.Message, height int64, round int32, voteType tmproto.SignedMsgType) { t.Helper() select { diff --git a/internal/consensus/state_test.go b/internal/consensus/state_test.go index 43277eecc..0bb81062f 100644 --- a/internal/consensus/state_test.go +++ b/internal/consensus/state_test.go @@ -353,7 +353,7 @@ func TestStateOversizedBlock(t *testing.T) { // propose, prevote, and precommit a block func TestStateFullRound1(t *testing.T) { config := configSetup(t) - logger := log.TestingLogger() + logger := log.NewTestingLogger(t) ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -372,7 +372,9 @@ func TestStateFullRound1(t *testing.T) { t.Error(err) } - voteCh := subscribe(ctx, t, cs.eventBus, types.EventQueryVote) + pv, err := cs.privValidator.GetPubKey(ctx) + require.NoError(t, err) + voteCh := subscribeToVoter(ctx, t, cs, pv.Address()) propCh := subscribe(ctx, t, cs.eventBus, types.EventQueryCompleteProposal) newRoundCh := subscribe(ctx, t, cs.eventBus, types.EventQueryNewRound) @@ -384,8 +386,7 @@ func TestStateFullRound1(t *testing.T) { ensureNewProposal(t, propCh, height, round) propBlockHash := cs.GetRoundState().ProposalBlock.Hash() - ensurePrevote(t, voteCh, height, round) // wait for prevote - validatePrevote(ctx, t, cs, round, vss[0], propBlockHash) + ensurePrevoteMatch(t, voteCh, height, round, propBlockHash) // wait for prevote ensurePrecommit(t, voteCh, height, round) // wait for precommit