summaryrefslogtreecommitdiff
path: root/agreement/voteTracker_test.go
blob: 3ed1454d76fd301ae44593da7d4f1cdc769f370b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package agreement

import (
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/algorand/go-algorand/config"
	"github.com/algorand/go-algorand/protocol"
	"github.com/algorand/go-algorand/test/partitiontest"
)

// todo: test validity of threshold events (incl. bundles)
// todo: test vote weights (and not just number of votes)

// make a voteTracker at zero state
func makeVoteTrackerZero() listener {
	return checkedListener{listener: new(voteTracker), listenerContract: new(voteTrackerContract)}
}

// actual tests

func TestVoteTrackerNoOp(t *testing.T) {
	partitiontest.PartitionTest(t)

	helper := voteMakerHelper{}
	helper.Setup()

	voteAcceptEvent := helper.MakeValidVoteAccepted(t, 0, soft)
	testCase := determisticTraceTestCase{
		inputs: []event{
			voteAcceptEvent,
		},
		expectedOutputs: []event{
			thresholdEvent{T: none},
		},
	}

	voteTrackerAutomata := &ioAutomataConcrete{
		listener: makeVoteTrackerZero(),
	}
	res, err := testCase.Validate(voteTrackerAutomata)
	require.NoError(t, err)
	require.NoErrorf(t, res, "Test case 1 did not validate")
}

func TestVoteTrackerSoftQuorum(t *testing.T) {
	partitiontest.PartitionTest(t)

	helper := voteMakerHelper{}
	helper.Setup()

	NumThreshold := soft.threshold(config.Consensus[protocol.ConsensusCurrentVersion])
	require.Falsef(t, soft.reachesQuorum(config.Consensus[protocol.ConsensusCurrentVersion], NumThreshold-1), "Test case malformed; generates too many votes")
	require.Truef(t, soft.reachesQuorum(config.Consensus[protocol.ConsensusCurrentVersion], NumThreshold), "Test case malformed; generates too few votes")
	inputVotes := make([]event, NumThreshold)
	expectedOutputs := make([]event, NumThreshold)
	for i := 0; i < len(inputVotes); i++ {
		inputVotes[i] = helper.MakeValidVoteAccepted(t, i, soft)
		expectedOutputs[i] = thresholdEvent{T: none}
	}
	// given quorum of soft votes, we expect to see soft threshold
	expectedOutputs[len(expectedOutputs)-1] = thresholdEvent{T: softThreshold, Proposal: *helper.proposal}
	testCase := determisticTraceTestCase{
		inputs:          inputVotes,
		expectedOutputs: expectedOutputs,
	}
	voteTrackerAutomata := &ioAutomataConcrete{
		listener: makeVoteTrackerZero(),
	}
	res, err := testCase.Validate(voteTrackerAutomata)
	require.NoError(t, err)
	require.NoErrorf(t, res, "Threshold event not generated")

	// now, do the same thing, but have one less vote, so expect no threshold
	inputVotes = inputVotes[:len(inputVotes)-1]
	expectedOutputs = expectedOutputs[:len(expectedOutputs)-1]
	testCaseNoThreshold := determisticTraceTestCase{
		inputs:          inputVotes,
		expectedOutputs: expectedOutputs,
	}
	voteTrackerAutomata = &ioAutomataConcrete{
		listener: makeVoteTrackerZero(),
	}
	res, err = testCaseNoThreshold.Validate(voteTrackerAutomata)
	require.NoError(t, err)
	require.NoErrorf(t, res, "Threshold event should not have been generated")
}

// sanity check for cert quorums
func TestVoteTrackerCertQuorum(t *testing.T) {
	partitiontest.PartitionTest(t)

	helper := voteMakerHelper{}
	helper.Setup()

	NumThreshold := cert.threshold(config.Consensus[protocol.ConsensusCurrentVersion])
	require.Falsef(t, cert.reachesQuorum(config.Consensus[protocol.ConsensusCurrentVersion], NumThreshold-1), "Test case malformed; generates too many votes")
	require.Truef(t, cert.reachesQuorum(config.Consensus[protocol.ConsensusCurrentVersion], NumThreshold), "Test case malformed; generates too few votes")
	inputVotes := make([]event, NumThreshold)
	expectedOutputs := make([]event, NumThreshold)
	for i := 0; i < len(inputVotes); i++ {
		inputVotes[i] = helper.MakeValidVoteAccepted(t, i, cert)
		expectedOutputs[i] = thresholdEvent{T: none}
	}
	expectedOutputs[len(expectedOutputs)-1] = thresholdEvent{T: certThreshold, Proposal: *helper.proposal}
	testCase := determisticTraceTestCase{
		inputs:          inputVotes,
		expectedOutputs: expectedOutputs,
	}
	voteTrackerAutomata := &ioAutomataConcrete{
		listener: makeVoteTrackerZero(),
	}
	res, err := testCase.Validate(voteTrackerAutomata)
	require.NoError(t, err)
	require.NoErrorf(t, res, "Threshold event not generated")

	// now, do the same thing, but have one less vote
	inputVotes = inputVotes[:len(inputVotes)-1]
	expectedOutputs = expectedOutputs[:len(expectedOutputs)-1]
	testCaseNoThreshold := determisticTraceTestCase{
		inputs:          inputVotes,
		expectedOutputs: expectedOutputs,
	}
	voteTrackerAutomata = &ioAutomataConcrete{
		listener: makeVoteTrackerZero(),
	}
	res, err = testCaseNoThreshold.Validate(voteTrackerAutomata)
	require.NoError(t, err)
	require.NoErrorf(t, res, "Threshold event should not have been generated")
}

// sanity check for next quorums
func TestVoteTrackerNextQuorum(t *testing.T) {
	partitiontest.PartitionTest(t)

	helper := voteMakerHelper{}
	helper.Setup()

	NumThreshold := next.threshold(config.Consensus[protocol.ConsensusCurrentVersion])
	require.Falsef(t, next.reachesQuorum(config.Consensus[protocol.ConsensusCurrentVersion], NumThreshold-1), "Test case malformed; generates too many votes")
	require.Truef(t, next.reachesQuorum(config.Consensus[protocol.ConsensusCurrentVersion], NumThreshold), "Test case malformed; generates too few votes")
	inputVotes := make([]event, NumThreshold)
	expectedOutputs := make([]event, NumThreshold)
	for i := 0; i < len(inputVotes); i++ {
		inputVotes[i] = helper.MakeValidVoteAccepted(t, i, next)
		expectedOutputs[i] = thresholdEvent{T: none}
	}
	expectedOutputs[len(expectedOutputs)-1] = thresholdEvent{T: nextThreshold, Proposal: *helper.proposal}
	testCase := determisticTraceTestCase{
		inputs:          inputVotes,
		expectedOutputs: expectedOutputs,
	}
	voteTrackerAutomata := &ioAutomataConcrete{
		listener: makeVoteTrackerZero(),
	}
	res, err := testCase.Validate(voteTrackerAutomata)
	require.NoError(t, err)
	require.NoErrorf(t, res, "Threshold event not generated")

	// now, do the same thing, but have one less vote
	inputVotes = inputVotes[:len(inputVotes)-1]
	expectedOutputs = expectedOutputs[:len(expectedOutputs)-1]
	testCaseNoThreshold := determisticTraceTestCase{
		inputs:          inputVotes,
		expectedOutputs: expectedOutputs,
	}
	voteTrackerAutomata = &ioAutomataConcrete{
		listener: makeVoteTrackerZero(),
	}
	res, err = testCaseNoThreshold.Validate(voteTrackerAutomata)
	require.NoError(t, err)
	require.NoErrorf(t, res, "Threshold event should not have been generated")
}

// sanity check propose votes don't trigger anything
func TestVoteTrackerProposeNoOp(t *testing.T) {
	partitiontest.PartitionTest(t)

	helper := voteMakerHelper{}
	helper.Setup()

	const NumUpperBound = 2000
	inputVotes := make([]event, NumUpperBound)
	for i := 0; i < len(inputVotes); i++ {
		inputVotes[i] = helper.MakeValidVoteAccepted(t, i, propose)
	}

	// here, each input is a separate test-case
	for i := 0; i < NumUpperBound; i++ {
		testCase := determisticTraceTestCase{
			inputs:          inputVotes[i : i+1],
			expectedOutputs: nil, // we expect the input to panic
		}
		voteTrackerAutomata := &ioAutomataConcrete{
			listener: makeVoteTrackerZero(),
		}

		res, err := testCase.Validate(voteTrackerAutomata)
		require.NoError(t, err, "A vote with step propose did not result in a precondition violation")
		require.NoError(t, res, "A vote with step propose did not result in a precondition violation")
	}
}

func TestVoteTrackerEquivocatorWeightCountedOnce(t *testing.T) {
	partitiontest.PartitionTest(t)

	helper := voteMakerHelper{}
	helper.Setup()

	NumThreshold := soft.threshold(config.Consensus[protocol.ConsensusCurrentVersion])
	inputVotes := make([]event, NumThreshold)
	expectedOutputs := make([]event, NumThreshold)
	for i := 0; i < int(NumThreshold-1); i++ {
		inputVotes[i] = helper.MakeValidVoteAccepted(t, i, soft)
		expectedOutputs[i] = thresholdEvent{T: none}
	}
	// generate an equivocation
	inputVotes[NumThreshold-1] = helper.MakeValidVoteAccepted(t, 0, soft)
	expectedOutputs[NumThreshold-1] = thresholdEvent{T: none}

	testCase := determisticTraceTestCase{
		inputs:          inputVotes,
		expectedOutputs: expectedOutputs,
	}
	voteTrackerAutomata := &ioAutomataConcrete{
		listener: makeVoteTrackerZero(),
	}
	res, err := testCase.Validate(voteTrackerAutomata)
	require.NoError(t, err)
	require.NoErrorf(t, res, "Threshold event generated due to equivocation double counting")

}

func TestVoteTrackerEquivDoesntReemitThreshold(t *testing.T) {
	partitiontest.PartitionTest(t)

	helper := voteMakerHelper{}
	helper.Setup()

	NumThreshold := soft.threshold(config.Consensus[protocol.ConsensusCurrentVersion])
	inputVotes := make([]event, NumThreshold+2)
	expectedOutputs := make([]event, NumThreshold+2)
	for i := 0; i < int(NumThreshold); i++ {
		inputVotes[i] = helper.MakeValidVoteAccepted(t, i, soft)
		expectedOutputs[i] = thresholdEvent{T: none}
	}
	expectedOutputs[NumThreshold-1] = thresholdEvent{T: softThreshold, Proposal: *helper.proposal}

	// generate an equivocation
	v := randomBlockHash()
	equivVal := proposalValue{BlockDigest: v}
	require.NotEqualf(t, *helper.proposal, equivVal, "Test does not generate equivocating values...")
	inputVotes[NumThreshold] = helper.MakeValidVoteAcceptedVal(t, 0, soft, equivVal)
	expectedOutputs[NumThreshold] = thresholdEvent{T: none}

	// generate one more valid vote
	inputVotes[NumThreshold+1] = helper.MakeValidVoteAccepted(t, int(NumThreshold+1), soft)
	expectedOutputs[NumThreshold+1] = thresholdEvent{T: none}

	testCase := determisticTraceTestCase{
		inputs:          inputVotes,
		expectedOutputs: expectedOutputs,
	}
	voteTrackerAutomata := &ioAutomataConcrete{
		listener: makeVoteTrackerZero(),
	}
	res, err := testCase.Validate(voteTrackerAutomata)
	require.NoError(t, err)
	require.NoErrorf(t, res, "Extra threshold events generated")
}

func TestVoteTrackerEquivocationsCount(t *testing.T) {
	partitiontest.PartitionTest(t)

	helper := voteMakerHelper{}
	helper.Setup()

	// generate an equivocation value pair
	v1 := randomBlockHash()
	equivVal1 := proposalValue{BlockDigest: v1}
	v2 := randomBlockHash()
	equivVal2 := proposalValue{BlockDigest: v2}
	require.NotEqualf(t, equivVal1, equivVal2, "Test does not generate equivocating values...")

	// lets use cert this time...
	NumThreshold := cert.threshold(config.Consensus[protocol.ConsensusCurrentVersion])
	TotalThreshold := NumThreshold + NumThreshold - NumThreshold/2
	inputVotes := make([]event, NumThreshold+NumThreshold/2)
	expectedOutputs := make([]event, NumThreshold+NumThreshold/2)
	// generate threshold/2 non equivocating votes
	for i := 0; i < int(NumThreshold/2); i++ {
		inputVotes[i] = helper.MakeValidVoteAcceptedVal(t, i, cert, equivVal1)
		expectedOutputs[i] = thresholdEvent{T: none}
	}
	// generate threshold/2 votes for v2. This shouldn't trigger a threshold event
	for i := int(NumThreshold / 2); i < int(NumThreshold); i++ {
		inputVotes[i] = helper.MakeValidVoteAcceptedVal(t, i, cert, equivVal2)
		expectedOutputs[i] = thresholdEvent{T: none}
	}
	// now, for the last threshold/2 votes, have them equivocate for v1. This should generate a threshold event.
	// we may need to update our test case once we implement early next-vote bottom detection.
	for i := int(NumThreshold / 2); i < int(NumThreshold); i++ {
		inputVotes[int(NumThreshold)+i-int(NumThreshold/2)] = helper.MakeValidVoteAcceptedVal(t, i, cert, equivVal1)
		expectedOutputs[int(NumThreshold)+i-int(NumThreshold/2)] = thresholdEvent{T: none}
	}
	expectedOutputs[TotalThreshold-1] = thresholdEvent{T: certThreshold, Proposal: equivVal1}

	testCase := determisticTraceTestCase{
		inputs:          inputVotes,
		expectedOutputs: expectedOutputs,
	}
	voteTrackerAutomata := &ioAutomataConcrete{
		listener: makeVoteTrackerZero(),
	}
	res, err := testCase.Validate(voteTrackerAutomata)
	require.NoError(t, err)
	require.NoErrorf(t, res, "Equivocation adversely affected threshold generation")
}

// same test as before, except equivocations voting v2, v3 should also count towards quorum for v1
func TestVoteTrackerSuperEquivocationsCount(t *testing.T) {
	partitiontest.PartitionTest(t)

	helper := voteMakerHelper{}
	helper.Setup()

	// generate an equivocation value triplet
	v1 := randomBlockHash()
	equivVal1 := proposalValue{BlockDigest: v1}
	v2 := randomBlockHash()
	equivVal2 := proposalValue{BlockDigest: v2}
	v3 := randomBlockHash()
	equivVal3 := proposalValue{BlockDigest: v3}
	require.NotEqualf(t, equivVal1, equivVal2, "Test does not generate equivocating values...")
	require.NotEqualf(t, equivVal2, equivVal3, "Test does not generate equivocating values...")
	require.NotEqualf(t, equivVal1, equivVal3, "Test does not generate equivocating values...")

	// lets use cert this time...
	NumThreshold := cert.threshold(config.Consensus[protocol.ConsensusCurrentVersion])
	TotalThreshold := NumThreshold + NumThreshold - NumThreshold/2
	inputVotes := make([]event, NumThreshold+NumThreshold/2)
	expectedOutputs := make([]event, NumThreshold+NumThreshold/2)
	// generate threshold/2 non equivocating votes
	for i := 0; i < int(NumThreshold/2); i++ {
		inputVotes[i] = helper.MakeValidVoteAcceptedVal(t, i, cert, equivVal1)
		expectedOutputs[i] = thresholdEvent{T: none}
	}
	// generate threshold/2 votes for v2. This shouldn't trigger a threshold event
	for i := int(NumThreshold / 2); i < int(NumThreshold); i++ {
		inputVotes[i] = helper.MakeValidVoteAcceptedVal(t, i, cert, equivVal2)
		expectedOutputs[i] = thresholdEvent{T: none}
	}
	// now, for the last threshold/2 votes, have them equivocate for v1. This should generate a threshold event.
	// we may need to update our test case once we implement early next-vote bottom detection.
	for i := int(NumThreshold / 2); i < int(NumThreshold); i++ {
		inputVotes[int(NumThreshold)+i-int(NumThreshold/2)] = helper.MakeValidVoteAcceptedVal(t, i, cert, equivVal3)
		expectedOutputs[int(NumThreshold)+i-int(NumThreshold/2)] = thresholdEvent{T: none}
	}
	expectedOutputs[TotalThreshold-1] = thresholdEvent{T: certThreshold, Proposal: equivVal1}

	testCase := determisticTraceTestCase{
		inputs:          inputVotes,
		expectedOutputs: expectedOutputs,
	}
	voteTrackerAutomata := &ioAutomataConcrete{
		listener: makeVoteTrackerZero(),
	}
	res, err := testCase.Validate(voteTrackerAutomata)
	require.NoError(t, err)
	require.NoErrorf(t, res, "Equivocation adversely affected threshold generation")
}

// check that SM panics on seeing two quorums
func TestVoteTrackerPanicsOnTwoSoftQuorums(t *testing.T) {
	partitiontest.PartitionTest(t)

	helper := voteMakerHelper{}
	helper.Setup()

	// generate an equivocation value pair
	v1 := randomBlockHash()
	equivVal1 := proposalValue{BlockDigest: v1}
	v2 := randomBlockHash()
	equivVal2 := proposalValue{BlockDigest: v2}
	require.NotEqualf(t, equivVal1, equivVal2, "Test does not generate equivocating values...")

	NumThreshold := soft.threshold(config.Consensus[protocol.ConsensusCurrentVersion])
	inputVotes := make([]event, 2*NumThreshold)
	expectedOutputs := make([]event, 2*NumThreshold)
	// generate quorum for v1
	for i := 0; i < int(NumThreshold); i++ {
		inputVotes[i] = helper.MakeValidVoteAcceptedVal(t, i, soft, equivVal1)
		expectedOutputs[i] = thresholdEvent{T: none}
	}
	expectedOutputs[NumThreshold-1] = thresholdEvent{T: softThreshold, Proposal: equivVal1}
	// generate quorum for v2
	for i := int(NumThreshold); i < int(2*NumThreshold); i++ {
		inputVotes[i] = helper.MakeValidVoteAcceptedVal(t, i, soft, equivVal2)
		expectedOutputs[i] = thresholdEvent{T: none}
	}
	// the last output should be a panic. Express this by shortening expected outputs
	expectedOutputs = expectedOutputs[:2*NumThreshold-1]

	testCase := determisticTraceTestCase{
		inputs:          inputVotes,
		expectedOutputs: expectedOutputs,
	}
	voteTrackerAutomata := &ioAutomataConcrete{
		listener: makeVoteTrackerZero(),
	}
	res, err := testCase.Validate(voteTrackerAutomata)
	require.NoError(t, err)
	require.NoErrorf(t, res, "VoteTracker did not panic on seeing two quorums for v1 != v2")
}

// check that SM panics on seeing soft quorum for bot (currently enforced by contract)
func TestVoteTrackerPanicsOnSoftBotQuorum(t *testing.T) {
	partitiontest.PartitionTest(t)

	helper := voteMakerHelper{}
	helper.Setup()

	// generate an equivocation value pair

	NumThreshold := soft.threshold(config.Consensus[protocol.ConsensusCurrentVersion])
	inputVotes := make([]event, NumThreshold)
	expectedOutputs := make([]event, NumThreshold)
	// generate quorum for bot
	for i := 0; i < int(NumThreshold); i++ {
		inputVotes[i] = helper.MakeValidVoteAcceptedVal(t, i, soft, bottom)
		expectedOutputs[i] = thresholdEvent{T: none}
	}
	expectedOutputs[NumThreshold-1] = thresholdEvent{T: softThreshold, Proposal: bottom}
	// the last output should be a panic. Express this by shortening expected outputs
	expectedOutputs = expectedOutputs[:NumThreshold-1]

	testCase := determisticTraceTestCase{
		inputs:          inputVotes,
		expectedOutputs: expectedOutputs,
	}
	voteTrackerAutomata := &ioAutomataConcrete{
		listener: makeVoteTrackerZero(),
	}
	res, err := testCase.Validate(voteTrackerAutomata)
	require.NoError(t, err)
	require.NoErrorf(t, res, "VoteTracker did not panic on seeing soft vote bot quorum")
}

// check that SM panics on seeing two next quorums, in particular bot, val in same step.
func TestVoteTrackerPanicsOnTwoNextQuorums(t *testing.T) {
	partitiontest.PartitionTest(t)

	helper := voteMakerHelper{}
	helper.Setup()

	// generate an equivocation value pair
	v2 := randomBlockHash()
	val2 := proposalValue{BlockDigest: v2}
	require.NotEqualf(t, bottom, val2, "Test does not generate equivocating values...")

	NumThreshold := next.threshold(config.Consensus[protocol.ConsensusCurrentVersion])
	inputVotes := make([]event, 2*NumThreshold)
	expectedOutputs := make([]event, 2*NumThreshold)
	// generate quorum for bot
	for i := 0; i < int(NumThreshold); i++ {
		inputVotes[i] = helper.MakeValidVoteAcceptedVal(t, i, next, bottom)
		expectedOutputs[i] = thresholdEvent{T: none}
	}
	expectedOutputs[NumThreshold-1] = thresholdEvent{T: nextThreshold, Proposal: bottom}
	// generate quorum for v2
	for i := int(NumThreshold); i < int(2*NumThreshold); i++ {
		inputVotes[i] = helper.MakeValidVoteAcceptedVal(t, i, next, val2)
		expectedOutputs[i] = thresholdEvent{T: none}
	}
	// the last output should be a panic. Express this by shortening expected outputs
	expectedOutputs = expectedOutputs[:2*NumThreshold-1]

	testCase := determisticTraceTestCase{
		inputs:          inputVotes,
		expectedOutputs: expectedOutputs,
	}
	voteTrackerAutomata := &ioAutomataConcrete{
		listener: makeVoteTrackerZero(),
	}
	res, err := testCase.Validate(voteTrackerAutomata)
	require.NoError(t, err)
	require.NoErrorf(t, res, "VoteTracker did not panic on seeing two quorums for v1 != v2")
}

func TestVoteTrackerRejectsTooManyEquivocators(t *testing.T) {
	partitiontest.PartitionTest(t)

	helper := voteMakerHelper{}
	helper.Setup()
	Num := soft.threshold(config.Consensus[protocol.ConsensusCurrentVersion])
	inputVotes := make([]event, Num*2)
	expectedOutputs := make([]event, Num*2)
	for i := 0; i < int(2*Num); i++ {
		Val := proposalValue{BlockDigest: randomBlockHash()}
		inputVotes[i] = helper.MakeValidVoteAcceptedVal(t, int(i/2), soft, Val)
		expectedOutputs[i] = thresholdEvent{T: none}
	}
	// We should now have threshold many equivocators... should have panicked when the last equivocation was seen.
	expectedOutputs[2*Num-2] = thresholdEvent{T: softThreshold, Proposal: inputVotes[2*Num-2].(voteAcceptedEvent).Vote.R.Proposal}
	expectedOutputs = expectedOutputs[:2*Num-1]
	testCase := determisticTraceTestCase{
		inputs:          inputVotes,
		expectedOutputs: expectedOutputs,
	}
	voteTrackerAutomata := &ioAutomataConcrete{
		listener: makeVoteTrackerZero(),
	}
	res, err := testCase.Validate(voteTrackerAutomata)
	require.NoError(t, err)
	require.NoErrorf(t, res, "VoteTracker did not reject too many equivocations")
}

/* tests for filtering component of vote tracker */

func TestVoteTrackerFiltersDuplicateVoteOnce(t *testing.T) {
	partitiontest.PartitionTest(t)

	helper := voteMakerHelper{}
	helper.Setup()
	v1 := randomBlockHash()
	Val1 := proposalValue{BlockDigest: v1}
	const Num = 10
	inputVotes := make([]event, Num+1)
	expectedOutputs := make([]event, Num+1)
	for i := 0; i < int(Num+1); i++ {
		switch {
		case i < Num:
			inputVotes[i] = helper.MakeValidVoteAcceptedVal(t, i, next, Val1)
			expectedOutputs[i] = thresholdEvent{T: none}
		case i == Num:
			inputVotes[i] = voteFilterRequestEvent{RawVote: inputVotes[Num-1].(voteAcceptedEvent).Vote.R}
			expectedOutputs[i] = filteredStepEvent{T: voteFilteredStep}
		}
	}
	testCase := determisticTraceTestCase{
		inputs:          inputVotes,
		expectedOutputs: expectedOutputs,
	}
	voteTrackerAutomata := &ioAutomataConcrete{
		listener: makeVoteTrackerZero(),
	}
	res, err := testCase.Validate(voteTrackerAutomata)
	require.NoError(t, err)
	require.NoErrorf(t, res, "VoteTracker did not filter duplicate")
}

func TestVoteTrackerForwardsFirstEquivocation(t *testing.T) {
	partitiontest.PartitionTest(t)

	helper := voteMakerHelper{}
	helper.Setup()
	const V1Bound = 10
	const V2Bound = 20
	const V1V2Bound = 30

	// generate an equivocation value pair
	v1 := randomBlockHash()
	equivVal1 := proposalValue{BlockDigest: v1}
	v2 := randomBlockHash()
	equivVal2 := proposalValue{BlockDigest: v2}
	v3 := randomBlockHash()
	equivVal3 := proposalValue{BlockDigest: v3}
	require.NotEqualf(t, equivVal1, equivVal2, "Test does not generate equivocating values...")
	require.NotEqualf(t, equivVal2, equivVal3, "Test does not generate equivocating values...")
	require.NotEqualf(t, equivVal1, equivVal3, "Test does not generate equivocating values...")

	inputVotes := make([]event, V1V2Bound+1)
	expectedOutputs := make([]event, V1V2Bound+1)
	for i := 0; i < int(V1V2Bound+1); i++ {
		switch {
		case i < V1Bound:
			// these will eventually equivocate
			inputVotes[i] = helper.MakeValidVoteAcceptedVal(t, i, next, equivVal1)
			expectedOutputs[i] = thresholdEvent{T: none}
		case i == V1Bound:
			// simple duplicate
			inputVotes[i] = voteFilterRequestEvent{RawVote: inputVotes[i-1].(voteAcceptedEvent).Vote.R}
			expectedOutputs[i] = filteredStepEvent{T: voteFilteredStep}
		case i < V2Bound:
			// these dont equivocate
			inputVotes[i] = helper.MakeValidVoteAcceptedVal(t, i, next, equivVal2)
			expectedOutputs[i] = thresholdEvent{T: none}
		case i == V2Bound:
			// simple duplicate
			rv := inputVotes[i-1].(voteAcceptedEvent).Vote.R
			inputVotes[i] = voteFilterRequestEvent{RawVote: rv}
			require.EqualValuesf(t, equivVal2, rv.Proposal, "test case is malformed, filtering incorrect vote")
			expectedOutputs[i] = filteredStepEvent{T: voteFilteredStep}
		case i == V2Bound+1:
			// make sure first equivocation is not filtered
			voteTwo := helper.MakeValidVoteAcceptedVal(t, V2Bound-1, next, equivVal1)
			inputVotes[i] = voteFilterRequestEvent{RawVote: voteTwo.Vote.R}
			expectedOutputs[i] = emptyEvent{}
		case i < V1V2Bound:
			// now, add some equivocations
			inputVotes[i] = helper.MakeValidVoteAcceptedVal(t, i-V2Bound, next, equivVal2)
			expectedOutputs[i] = thresholdEvent{T: none}
		case i == V1V2Bound:
			voteThree := helper.MakeValidVoteAcceptedVal(t, 2, next, equivVal3)
			inputVotes[i] = voteFilterRequestEvent{RawVote: voteThree.Vote.R}
			expectedOutputs[i] = filteredStepEvent{T: voteFilteredStep}
		}
	}
	testCase := determisticTraceTestCase{
		inputs:          inputVotes,
		expectedOutputs: expectedOutputs,
	}
	voteTrackerAutomata := &ioAutomataConcrete{
		listener: makeVoteTrackerZero(),
	}
	res, err := testCase.Validate(voteTrackerAutomata)
	require.NoError(t, err)
	require.NoErrorf(t, res, "VoteTracker filtered first equivocation")
}

func TestVoteTrackerFiltersFutureEquivocations(t *testing.T) {
	partitiontest.PartitionTest(t)

	helper := voteMakerHelper{}
	helper.Setup()
	const Num = 100
	inputVotes := make([]event, Num)
	expectedOutputs := make([]event, Num)
	for i := 0; i < int(Num); i++ {
		switch {
		case i == 0:
			Val := proposalValue{BlockDigest: randomBlockHash()}
			inputVotes[i] = helper.MakeValidVoteAcceptedVal(t, 0, soft, Val)
			expectedOutputs[i] = thresholdEvent{T: none}
		case i == 1:
			// first equivocation should not be filtered
			Val := proposalValue{BlockDigest: randomBlockHash()}
			VA := helper.MakeValidVoteAcceptedVal(t, 0, soft, Val)
			inputVotes[i] = voteFilterRequestEvent{RawVote: VA.Vote.R}
			expectedOutputs[i] = emptyEvent{}
		case i == 2:
			// add an equivocation
			Val := proposalValue{BlockDigest: randomBlockHash()}
			inputVotes[i] = helper.MakeValidVoteAcceptedVal(t, 0, soft, Val)
			expectedOutputs[i] = thresholdEvent{T: none}
		case i < Num:
			// future equivocations should be filtered
			Val := proposalValue{BlockDigest: randomBlockHash()}
			VA := helper.MakeValidVoteAcceptedVal(t, 0, soft, Val)
			inputVotes[i] = voteFilterRequestEvent{RawVote: VA.Vote.R}
			expectedOutputs[i] = filteredStepEvent{T: voteFilteredStep}
		}
	}
	testCase := determisticTraceTestCase{
		inputs:          inputVotes,
		expectedOutputs: expectedOutputs,
	}
	voteTrackerAutomata := &ioAutomataConcrete{
		listener: makeVoteTrackerZero(),
	}
	res, err := testCase.Validate(voteTrackerAutomata)
	require.NoError(t, err)
	require.NoErrorf(t, res, "VoteTracker did not filter equivocations")
}

/* Check that machine panics on unknown event */

func TestVoteTrackerRejectsUnknownEvent(t *testing.T) {
	partitiontest.PartitionTest(t)

	testCase := determisticTraceTestCase{
		inputs: []event{
			emptyEvent{},
		},
		expectedOutputs: []event{},
	}
	voteTrackerAutomata := &ioAutomataConcrete{
		listener: &voteTracker{}, // we also want the base machine to panic, so don't wrap in contract
	}
	res, err := testCase.Validate(voteTrackerAutomata)
	require.NoError(t, err)
	require.NoErrorf(t, res, "VoteTracker did not reject unknown event")
}