-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathparticipant_test.go
1208 lines (1148 loc) · 39.1 KB
/
participant_test.go
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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package gpbft_test
import (
"bytes"
"errors"
"fmt"
"math/rand"
"sync"
"testing"
"time"
"github.com/filecoin-project/go-f3/emulator"
"github.com/filecoin-project/go-f3/gpbft"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"golang.org/x/exp/slices"
)
var ptCid = gpbft.MakeCid([]byte("pt"))
var wrongPtCid = gpbft.MakeCid([]byte("wrong"))
var somePowerEntry = gpbft.PowerEntry{
ID: 1513,
Power: gpbft.NewStoragePower(1514),
PubKey: gpbft.PubKey("ghoti"),
}
type participantTestSubject struct {
*gpbft.Participant
t *testing.T
rng *rand.Rand
host *gpbft.MockHost
id gpbft.ActorID
pubKey gpbft.PubKey
instance uint64
networkName gpbft.NetworkName
canonicalChain *gpbft.ECChain
supplementalData *gpbft.SupplementalData
powerTable *gpbft.PowerTable
beacon []byte
time time.Time
delta time.Duration
trace []string
}
func newParticipantTestSubject(t *testing.T, seed int64, instance uint64) *participantTestSubject {
// Generate some canonical chain.
canonicalChain, err := gpbft.NewChain(&gpbft.TipSet{Epoch: 0, Key: []byte("genesis"), PowerTable: ptCid})
require.NoError(t, err)
const (
delta = 2 * time.Second
deltaBackOffExponent = 1.3
)
rng := rand.New(rand.NewSource(seed))
subject := &participantTestSubject{
t: t,
rng: rng,
id: gpbft.ActorID(rng.Uint64()),
pubKey: generateRandomBytes(rng),
delta: delta,
instance: instance,
networkName: "fish",
canonicalChain: canonicalChain,
supplementalData: &gpbft.SupplementalData{
Commitments: [32]byte{},
PowerTable: ptCid,
},
powerTable: gpbft.NewPowerTable(),
beacon: generateRandomBytes(rng),
time: time.Now(),
}
// Assure power table contains the power entry for the test subject
require.NoError(t, subject.powerTable.Add(gpbft.PowerEntry{
ID: subject.id,
Power: gpbft.NewStoragePower(int64(rng.Intn(1413))),
PubKey: subject.pubKey,
}))
subject.host = gpbft.NewMockHost(t)
// Expect ad-hoc calls to getting network name as such calls bear no significance
// to correctness.
subject.host.On("NetworkName").Return(subject.networkName).Maybe()
subject.Participant, err = gpbft.NewParticipant(subject.host,
gpbft.WithTracer(subject),
gpbft.WithDelta(delta),
gpbft.WithDeltaBackOffExponent(deltaBackOffExponent))
require.NoError(t, err)
subject.requireNotStarted()
return subject
}
func (pt *participantTestSubject) Log(format string, args ...any) {
pt.trace = append(pt.trace, fmt.Sprintf(format, args...))
}
func (pt *participantTestSubject) expectBeginInstance() {
// Prepare the test host.
pt.host.On("GetProposal", pt.instance).Return(pt.supplementalData, pt.canonicalChain, nil)
pt.host.On("GetCommittee", pt.instance).Return(&gpbft.Committee{PowerTable: pt.powerTable, Beacon: pt.beacon}, nil).Once()
pt.host.On("Time").Return(pt.time)
// We need to use `Maybe` here because `MarshalPayloadForSigning` may be called
// an additional time for verification.
// Without the `Maybe` the tests immediately fails here:
// https://github.com/filecoin-project/go-f3/blob/d27d281109d31485fc4ac103e2af58afb86c158f/gpbft/gpbft.go#L395
pt.host.On("MarshalPayloadForSigning", pt.networkName, mock.AnythingOfType("*gpbft.Payload")).
Return([]byte(gpbft.DomainSeparationTag + ":" + pt.networkName)).Maybe()
// Expect calls to get the host state prior to beginning of an instance.
pt.host.EXPECT().GetProposal(pt.instance)
pt.host.EXPECT().GetCommittee(pt.instance)
pt.host.EXPECT().Time()
// Expect alarm is set to 2X of configured delta.
pt.host.EXPECT().SetAlarm(pt.time.Add(2 * pt.delta))
// Expect a broadcast occurs with quality phase message, and the expected chain, signature.
payload := gpbft.Payload{
Instance: pt.instance,
Phase: gpbft.QUALITY_PHASE,
Value: pt.canonicalChain,
SupplementalData: *pt.supplementalData,
}
pt.host.EXPECT().RequestBroadcast(mock.MatchedBy(func(o *gpbft.MessageBuilder) bool {
return o.NetworkName == pt.networkName &&
payload.Eq(&o.Payload) &&
o.Justification == nil &&
o.BeaconForTicket == nil
})).Return(nil)
}
func (pt *participantTestSubject) requireNotStarted() {
pt.t.Helper()
instant := pt.Progress()
require.Zero(pt.t, instant.ID)
require.Zero(pt.t, instant.Round)
require.Equal(pt.t, gpbft.INITIAL_PHASE, instant.Phase)
require.Equal(pt.t, "nil", pt.Describe())
}
func (pt *participantTestSubject) requireInstanceRoundPhase(wantInstance, wantRound uint64, wantPhase gpbft.Phase) {
pt.t.Helper()
require.Equal(pt.t, fmt.Sprintf("{%d}, round %d, phase %s", wantInstance, wantRound, wantPhase), pt.Describe())
}
func (pt *participantTestSubject) requireStart() {
pt.expectBeginInstance()
require.NoError(pt.t, pt.Start())
pt.assertHostExpectations()
pt.requireInstanceRoundPhase(pt.instance, 0, gpbft.QUALITY_PHASE)
}
// Participants start new instances asynchronously by setting an alarm
// that is triggered immediately, thus the underlying use of
// ReceiveAlarm() in tests that require starting instances.
// See [participant.go:Start()] for reference
func (pt *participantTestSubject) Start() error {
pt.host.EXPECT().SetAlarm(pt.time)
require.NoError(pt.t, pt.Participant.StartInstanceAt(pt.instance, pt.time))
return pt.ReceiveAlarm()
}
func (pt *participantTestSubject) assertHostExpectations() bool {
return pt.host.AssertExpectations(pt.t)
}
func (pt *participantTestSubject) mockValidSignature(target gpbft.PubKey, sig []byte) *mock.Call {
return pt.host.
On("Verify", target, pt.matchMessageSigningPayload(), sig).
Return(nil)
}
func (pt *participantTestSubject) mockInvalidSignature(target gpbft.PubKey, sig []byte) {
pt.host.On("Verify", target, pt.matchMessageSigningPayload(), sig).
Return(errors.New("mock verification failure"))
}
func (pt *participantTestSubject) mockInvalidTicket(target gpbft.PubKey, ticket gpbft.Ticket) *mock.Call {
return pt.host.On(
"Verify",
target,
mock.MatchedBy(func(msg []byte) bool {
return bytes.HasPrefix(msg, []byte(gpbft.DomainSeparationTagVRF+":"+pt.networkName))
}), []byte(ticket)).
Return(errors.New("mock verification failure"))
}
func (pt *participantTestSubject) mockValidTicket(target gpbft.PubKey, ticket gpbft.Ticket) *mock.Call {
return pt.host.On(
"Verify",
target,
pt.matchTicketSigningPayload(), []byte(ticket)).
Return(nil)
}
func (pt *participantTestSubject) mockCommitteeForInstance(instance uint64, powerTable *gpbft.PowerTable, beacon []byte) {
pt.host.On("GetCommittee", instance).Return(&gpbft.Committee{PowerTable: powerTable, Beacon: beacon}, nil).Once()
}
func (pt *participantTestSubject) mockCommitteeUnavailableForInstance(instance uint64) {
pt.host.On("GetCommittee", instance).Return(nil, errors.New("committee not available"))
}
func (pt *participantTestSubject) matchMessageSigningPayload() any {
return mock.MatchedBy(func(msg []byte) bool {
return bytes.HasPrefix(msg, []byte(gpbft.DomainSeparationTag+":"+pt.networkName))
})
}
func (pt *participantTestSubject) matchTicketSigningPayload() any {
return mock.MatchedBy(func(msg []byte) bool {
return bytes.HasPrefix(msg, []byte(gpbft.DomainSeparationTagVRF+":"+pt.networkName))
})
}
func generateRandomBytes(rng *rand.Rand) []byte {
var wantSignature [32]byte
rng.Read(wantSignature[:])
return wantSignature[:]
}
func TestParticipant(t *testing.T) {
t.Parallel()
const seed = 984651320
signature := []byte("barreleye")
t.Run("panic is recovered", func(t *testing.T) {
t.Run("on Start", func(t *testing.T) {
subject := newParticipantTestSubject(t, seed, 0)
subject.host.On("GetProposal", subject.instance).Panic("saw me no chain")
require.NotPanics(t, func() {
require.ErrorContains(t, subject.Start(), "saw me no chain")
})
})
t.Run("on ReceiveAlarm", func(t *testing.T) {
subject := newParticipantTestSubject(t, seed, 0)
subject.host.On("GetProposal", subject.instance).Panic("saw me no chain")
require.NotPanics(t, func() {
require.ErrorContains(t, subject.ReceiveAlarm(), "saw me no chain")
})
})
t.Run("on ValidateMessage", func(t *testing.T) {
subject := newParticipantTestSubject(t, seed, 0)
subject.requireStart()
require.NotPanics(t, func() {
gotValidated, gotErr := subject.ValidateMessage(nil)
require.Nil(t, gotValidated)
require.Error(t, gotErr)
})
})
t.Run("on ReceiveMessage", func(t *testing.T) {
subject := newParticipantTestSubject(t, seed, 0)
subject.requireStart()
require.NotPanics(t, func() {
gotErr := subject.ReceiveMessage(nil)
require.Error(t, gotErr)
})
})
})
t.Run("when not started", func(t *testing.T) {
t.Run("message is validated", func(t *testing.T) {
initialInstance := uint64(0)
subject := newParticipantTestSubject(t, seed, initialInstance)
subject.mockCommitteeForInstance(initialInstance, subject.powerTable, subject.beacon)
gotValidated, gotValidateErr := subject.ValidateMessage(&gpbft.GMessage{
Sender: subject.id,
Vote: gpbft.Payload{
SupplementalData: *subject.supplementalData,
},
})
require.Nil(t, gotValidated)
require.ErrorContains(t, gotValidateErr, "invalid vote phase: 0")
})
t.Run("message is accepted (queued)", func(t *testing.T) {
subject := newParticipantTestSubject(t, seed, 0)
gotReceiveErr := subject.ReceiveMessage(Validated(new(gpbft.GMessage)))
require.NoError(t, gotReceiveErr)
})
t.Run("instance is begun", func(t *testing.T) {
t.Run("on ReceiveAlarm", func(t *testing.T) {
subject := newParticipantTestSubject(t, seed, 0)
subject.expectBeginInstance()
require.NoError(t, subject.ReceiveAlarm())
subject.assertHostExpectations()
subject.requireInstanceRoundPhase(0, 0, gpbft.QUALITY_PHASE)
})
t.Run("on Start", func(t *testing.T) {
subject := newParticipantTestSubject(t, seed, 47)
subject.expectBeginInstance()
require.NoError(t, subject.Start())
subject.assertHostExpectations()
subject.requireInstanceRoundPhase(47, 0, gpbft.QUALITY_PHASE)
})
t.Run("on SkipTToInstance", func(t *testing.T) {
// initialize participant in instance 47
subject := newParticipantTestSubject(t, seed, 47)
subject.host.On("Time").Return(subject.time)
subject.host.EXPECT().SetAlarm(subject.time)
// expect an update of the participate to 57
fInstance := uint64(57)
subject.instance = fInstance
subject.expectBeginInstance()
// Receiving the certificate should skip directly to the finality instance.
require.NoError(t, subject.StartInstanceAt(fInstance, subject.time))
// set subject to the finality instance to see if participant
// has begun the right instance.
require.NoError(t, subject.ReceiveAlarm())
subject.assertHostExpectations()
subject.requireInstanceRoundPhase(57, 0, gpbft.QUALITY_PHASE)
})
})
t.Run("instance is not begun", func(t *testing.T) {
t.Run("on zero canonical chain", func(t *testing.T) {
subject := newParticipantTestSubject(t, seed, 0)
var zeroChain gpbft.ECChain
emptySupplementalData := new(gpbft.SupplementalData)
subject.host.On("GetProposal", subject.instance).Return(emptySupplementalData, &zeroChain, nil)
require.ErrorContains(t, subject.Start(), "cannot be zero-valued")
subject.assertHostExpectations()
subject.requireNotStarted()
})
t.Run("on invalid canonical chain", func(t *testing.T) {
subject := newParticipantTestSubject(t, seed, 0)
invalidChain := &gpbft.ECChain{TipSets: []*gpbft.TipSet{{PowerTable: subject.supplementalData.PowerTable}}}
emptySupplementalData := new(gpbft.SupplementalData)
subject.host.On("GetProposal", subject.instance).Return(emptySupplementalData, invalidChain, nil)
require.ErrorContains(t, subject.Start(), "invalid canonical chain")
subject.assertHostExpectations()
subject.requireNotStarted()
})
t.Run("on failure to fetch chain", func(t *testing.T) {
subject := newParticipantTestSubject(t, seed, 0)
invalidChain := &gpbft.ECChain{TipSets: []*gpbft.TipSet{{PowerTable: subject.supplementalData.PowerTable}}}
emptySupplementalData := new(gpbft.SupplementalData)
subject.host.On("GetProposal", subject.instance).Return(emptySupplementalData, invalidChain, errors.New("fish"))
require.ErrorContains(t, subject.Start(), "fish")
subject.assertHostExpectations()
subject.requireNotStarted()
})
t.Run("on failure to fetch committee", func(t *testing.T) {
subject := newParticipantTestSubject(t, seed, 0)
chain := &gpbft.ECChain{TipSets: []*gpbft.TipSet{{
Epoch: 0,
Key: []byte("key"),
PowerTable: ptCid,
Commitments: [32]byte{},
}}}
supplementalData := &gpbft.SupplementalData{
PowerTable: chain.TipSets[0].PowerTable,
}
subject.host.On("GetProposal", subject.instance).Return(supplementalData, chain, nil)
subject.host.On("GetCommittee", subject.instance).Return(nil, errors.New("fish"))
require.ErrorContains(t, subject.Start(), "fish")
subject.assertHostExpectations()
subject.requireNotStarted()
})
})
})
t.Run("when started", func(t *testing.T) {
t.Run("on ReceiveMessage", func(t *testing.T) {
const initialInstance = 47
tests := []struct {
name string
message func(subject *participantTestSubject) *gpbft.GMessage
wantErr string
wantTrace string
}{
{
name: "prior instance message is dropped",
message: func(subject *participantTestSubject) *gpbft.GMessage {
return &gpbft.GMessage{
Vote: gpbft.Payload{
Instance: initialInstance - 1,
SupplementalData: *subject.supplementalData,
},
}
},
wantTrace: "dropping message from old instance",
},
{
name: "current instance message with unexpected base is rejected",
message: func(subject *participantTestSubject) *gpbft.GMessage {
require.NoError(subject.t, subject.powerTable.Add(somePowerEntry))
return &gpbft.GMessage{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstance,
Phase: gpbft.QUALITY_PHASE,
SupplementalData: *subject.supplementalData,
Value: &gpbft.ECChain{TipSets: []*gpbft.TipSet{{Epoch: 0, Key: []byte("wrong"), PowerTable: subject.supplementalData.PowerTable}}},
},
Signature: signature,
}
},
wantErr: "unexpected base",
},
{
name: "current instance message with unexpected supplement is rejected",
message: func(subject *participantTestSubject) *gpbft.GMessage {
require.NoError(subject.t, subject.powerTable.Add(somePowerEntry))
return &gpbft.GMessage{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstance,
Phase: gpbft.QUALITY_PHASE,
SupplementalData: gpbft.SupplementalData{
Commitments: [32]byte{},
PowerTable: wrongPtCid,
},
Value: subject.canonicalChain,
},
Signature: signature,
}
},
wantErr: "unexpected supplement",
},
{
name: "future instance message with unexpected base is queued",
message: func(subject *participantTestSubject) *gpbft.GMessage {
require.NoError(subject.t, subject.powerTable.Add(somePowerEntry))
return &gpbft.GMessage{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstance + 1,
Phase: gpbft.QUALITY_PHASE,
SupplementalData: *subject.supplementalData,
Value: &gpbft.ECChain{TipSets: []*gpbft.TipSet{{Epoch: 0, Key: []byte("wrong"), PowerTable: subject.supplementalData.PowerTable}}},
},
Signature: signature,
}
},
},
{
name: "valid current instance message is accepted",
message: func(subject *participantTestSubject) *gpbft.GMessage {
require.NoError(subject.t, subject.powerTable.Add(somePowerEntry))
return &gpbft.GMessage{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstance,
Phase: gpbft.QUALITY_PHASE,
SupplementalData: *subject.supplementalData,
Value: subject.canonicalChain,
},
Signature: signature,
}
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
subject := newParticipantTestSubject(t, seed, initialInstance)
subject.requireStart()
gotErr := subject.ReceiveMessage(Validated(test.message(subject)))
if test.wantErr == "" {
require.NoError(t, gotErr)
} else {
require.ErrorContains(t, gotErr, test.wantErr)
}
if test.wantTrace != "" {
var found bool
for _, msg := range subject.trace {
require.Contains(t, msg, test.wantTrace)
found = true
}
require.True(t, found, "trace %s not found", test.wantTrace)
}
})
}
})
})
}
func TestParticipant_ValidateMessage(t *testing.T) {
const (
seed = 894651320
initialInstanceNumber = 47
)
var (
zeroPowerEntry = gpbft.PowerEntry{
ID: 1613,
Power: gpbft.NewStoragePower(0),
PubKey: gpbft.PubKey("fishmonger"),
}
signature = []byte("barreleye")
)
tests := []struct {
name string
msg func(*participantTestSubject) *gpbft.GMessage
msgs func(*participantTestSubject) []*gpbft.GMessage
wantErr string
}{
{
name: "valid message is accepted",
msg: func(subject *participantTestSubject) *gpbft.GMessage {
subject.mockValidSignature(somePowerEntry.PubKey, signature)
return &gpbft.GMessage{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
Phase: gpbft.QUALITY_PHASE,
Value: subject.canonicalChain,
SupplementalData: *subject.supplementalData,
},
Signature: signature,
}
},
},
{
name: "far future instanceID is rejected",
msg: func(subject *participantTestSubject) *gpbft.GMessage {
subject.mockCommitteeUnavailableForInstance(initialInstanceNumber + 5)
return &gpbft.GMessage{
Vote: gpbft.Payload{
Instance: initialInstanceNumber + 5,
SupplementalData: *subject.supplementalData,
},
}
},
wantErr: gpbft.ErrValidationNoCommittee.Error(),
},
{
name: "zero message is error",
msg: func(subject *participantTestSubject) *gpbft.GMessage {
return &gpbft.GMessage{
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
SupplementalData: *subject.supplementalData,
},
}
},
wantErr: "sender 0 with zero power or not in power table",
},
{
name: "unknown power is error",
msg: func(subject *participantTestSubject) *gpbft.GMessage {
return &gpbft.GMessage{
Sender: 42,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
SupplementalData: *subject.supplementalData,
},
}
},
wantErr: "sender 42 with zero power or not in power table",
},
{
name: "zero power is error",
msg: func(subject *participantTestSubject) *gpbft.GMessage {
return &gpbft.GMessage{
Sender: zeroPowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
SupplementalData: *subject.supplementalData,
},
}
},
wantErr: "sender 1613 with zero power or not in power table",
},
{
name: "invalid value chain is error",
msg: func(subject *participantTestSubject) *gpbft.GMessage {
return &gpbft.GMessage{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
Phase: gpbft.QUALITY_PHASE,
Value: &gpbft.ECChain{TipSets: []*gpbft.TipSet{
subject.canonicalChain.Base(),
{PowerTable: subject.supplementalData.PowerTable}}},
SupplementalData: *subject.supplementalData,
},
}
},
wantErr: "invalid message vote value chain",
},
{
name: "zero vote is error",
msg: func(subject *participantTestSubject) *gpbft.GMessage {
return &gpbft.GMessage{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
SupplementalData: *subject.supplementalData,
},
}
},
wantErr: "invalid vote phase: 0",
},
{
name: "unknown vote phase is error",
msg: func(subject *participantTestSubject) *gpbft.GMessage {
return &gpbft.GMessage{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
Phase: 42,
SupplementalData: *subject.supplementalData,
},
}
},
wantErr: "invalid vote phase: 42",
},
{
name: "QUALITY with non-zero vote round is error",
msg: func(subject *participantTestSubject) *gpbft.GMessage {
return &gpbft.GMessage{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
Phase: gpbft.QUALITY_PHASE,
Round: 7,
SupplementalData: *subject.supplementalData,
},
}
},
wantErr: "unexpected round 7 for quality phase",
},
{
name: "QUALITY with zero vote value is error",
msg: func(subject *participantTestSubject) *gpbft.GMessage {
return &gpbft.GMessage{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
Phase: gpbft.QUALITY_PHASE,
SupplementalData: *subject.supplementalData,
},
}
},
wantErr: "unexpected zero value for quality phase",
},
{
name: "CONVERGE with zero vote round is error",
msg: func(subject *participantTestSubject) *gpbft.GMessage {
return &gpbft.GMessage{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
Phase: gpbft.CONVERGE_PHASE,
SupplementalData: *subject.supplementalData,
},
}
},
wantErr: "unexpected round 0 for converge phase",
},
{
name: "CONVERGE with zero vote value is error",
msg: func(subject *participantTestSubject) *gpbft.GMessage {
return &gpbft.GMessage{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
Phase: gpbft.CONVERGE_PHASE,
Round: 42,
SupplementalData: *subject.supplementalData,
},
}
},
wantErr: "unexpected zero value for converge phase",
},
{
name: "CONVERGE with invalid vote value is error",
msg: func(subject *participantTestSubject) *gpbft.GMessage {
return &gpbft.GMessage{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
Phase: gpbft.CONVERGE_PHASE,
Round: 42,
Value: &gpbft.ECChain{TipSets: []*gpbft.TipSet{subject.canonicalChain.Base(), {PowerTable: subject.supplementalData.PowerTable}}},
SupplementalData: *subject.supplementalData,
},
}
},
wantErr: "invalid message vote value chain",
},
{
name: "CONVERGE with unverified ticket is error",
msg: func(subject *participantTestSubject) *gpbft.GMessage {
ticket := gpbft.Ticket("fish-cake")
subject.mockInvalidTicket(somePowerEntry.PubKey, ticket)
return &gpbft.GMessage{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
Phase: gpbft.CONVERGE_PHASE,
Round: 42,
Value: subject.canonicalChain,
SupplementalData: *subject.supplementalData,
},
Ticket: ticket,
}
},
wantErr: "failed to verify ticket from 1513",
},
{
name: "DECIDE with non-zero vote round is error",
msg: func(subject *participantTestSubject) *gpbft.GMessage {
return &gpbft.GMessage{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
Phase: gpbft.DECIDE_PHASE,
Round: 42,
SupplementalData: *subject.supplementalData,
},
}
},
wantErr: "unexpected non-zero round 42 for decide phase",
},
{
name: "DECIDE with zero vote value is error",
msg: func(subject *participantTestSubject) *gpbft.GMessage {
return &gpbft.GMessage{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
Phase: gpbft.DECIDE_PHASE,
SupplementalData: *subject.supplementalData,
},
}
},
wantErr: "unexpected zero value for decide phase",
},
{
name: "invalid vote signature is error",
msg: func(subject *participantTestSubject) *gpbft.GMessage {
subject.mockInvalidSignature(somePowerEntry.PubKey, signature)
return &gpbft.GMessage{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
Phase: gpbft.DECIDE_PHASE,
Value: subject.canonicalChain,
SupplementalData: *subject.supplementalData,
},
Signature: signature,
}
},
wantErr: "invalid signature",
},
{
name: "non nil Justification when not needed is error",
msgs: func(subject *participantTestSubject) []*gpbft.GMessage {
subject.mockValidSignature(somePowerEntry.PubKey, signature)
nonNilJustification := &gpbft.Justification{
Vote: gpbft.Payload{
SupplementalData: *subject.supplementalData,
},
}
return []*gpbft.GMessage{
{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
Phase: gpbft.PREPARE_PHASE,
SupplementalData: *subject.supplementalData,
},
Signature: signature,
Justification: nonNilJustification,
},
{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
Phase: gpbft.QUALITY_PHASE,
Value: subject.canonicalChain,
SupplementalData: *subject.supplementalData,
},
Signature: signature,
Justification: nonNilJustification,
},
{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
Phase: gpbft.COMMIT_PHASE,
SupplementalData: *subject.supplementalData,
},
Signature: signature,
Justification: nonNilJustification,
},
}
},
wantErr: "has unexpected justification",
},
{
name: "nil Justification when needed is error",
msgs: func(subject *participantTestSubject) []*gpbft.GMessage {
subject.mockValidSignature(somePowerEntry.PubKey, signature)
return []*gpbft.GMessage{
{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
Phase: gpbft.DECIDE_PHASE,
Value: subject.canonicalChain,
SupplementalData: *subject.supplementalData,
},
Signature: signature,
},
{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
Phase: gpbft.COMMIT_PHASE,
Value: subject.canonicalChain,
SupplementalData: *subject.supplementalData,
},
Signature: signature,
},
}
},
wantErr: "has no justification",
},
{
name: "CONVERGE with nil Justification when needed is error",
msgs: func(subject *participantTestSubject) []*gpbft.GMessage {
ticket := gpbft.Ticket("fishcake")
subject.mockValidTicket(somePowerEntry.PubKey, ticket)
subject.mockValidSignature(somePowerEntry.PubKey, signature)
return []*gpbft.GMessage{
{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
Phase: gpbft.CONVERGE_PHASE,
Round: 4,
Value: subject.canonicalChain,
SupplementalData: *subject.supplementalData,
},
Ticket: ticket,
Signature: signature,
},
}
},
wantErr: "has no justification",
},
{
name: "justification and vote instance mismatch is error",
msgs: func(subject *participantTestSubject) []*gpbft.GMessage {
subject.mockValidSignature(somePowerEntry.PubKey, signature)
return []*gpbft.GMessage{
{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
Phase: gpbft.COMMIT_PHASE,
Value: subject.canonicalChain,
SupplementalData: *subject.supplementalData,
},
Signature: signature,
Justification: &gpbft.Justification{
Vote: gpbft.Payload{
Instance: initialInstanceNumber + 3,
SupplementalData: *subject.supplementalData,
},
},
},
}
},
wantErr: "has evidence from instanceID: 50",
},
{
name: "justification at unexpected phase is error",
msgs: func(subject *participantTestSubject) []*gpbft.GMessage {
subject.mockValidTicket(somePowerEntry.PubKey, signature).Maybe()
subject.mockValidSignature(somePowerEntry.PubKey, signature)
return []*gpbft.GMessage{
{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
Round: 22,
Phase: gpbft.CONVERGE_PHASE,
Value: subject.canonicalChain,
SupplementalData: *subject.supplementalData,
},
Signature: signature,
Justification: &gpbft.Justification{
Vote: gpbft.Payload{
Phase: gpbft.CONVERGE_PHASE,
Instance: initialInstanceNumber,
SupplementalData: *subject.supplementalData,
},
},
Ticket: signature,
},
{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
Phase: gpbft.COMMIT_PHASE,
Value: subject.canonicalChain,
SupplementalData: *subject.supplementalData,
},
Signature: signature,
Justification: &gpbft.Justification{
Vote: gpbft.Payload{
Phase: gpbft.DECIDE_PHASE,
Instance: initialInstanceNumber,
SupplementalData: *subject.supplementalData,
},
},
Ticket: signature,
},
{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
Phase: gpbft.DECIDE_PHASE,
Value: subject.canonicalChain,
SupplementalData: *subject.supplementalData,
},
Signature: signature,
Justification: &gpbft.Justification{
Vote: gpbft.Payload{
Phase: gpbft.QUALITY_PHASE,
Instance: initialInstanceNumber,
SupplementalData: *subject.supplementalData,
},
},
Ticket: signature,
},
}
},
wantErr: "has justification with unexpected phase",
},
{
name: "justification from wrong round is error",
msgs: func(subject *participantTestSubject) []*gpbft.GMessage {
subject.mockValidTicket(somePowerEntry.PubKey, signature).Maybe()
subject.mockValidSignature(somePowerEntry.PubKey, signature)
return []*gpbft.GMessage{
{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
Round: 22,
Phase: gpbft.CONVERGE_PHASE,
Value: subject.canonicalChain,
SupplementalData: *subject.supplementalData,
},
Signature: signature,
Justification: &gpbft.Justification{
Vote: gpbft.Payload{
Phase: gpbft.COMMIT_PHASE,
Round: 22,
Instance: initialInstanceNumber,
SupplementalData: *subject.supplementalData,
},
},
Ticket: signature,
},
{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
Round: 22,
Phase: gpbft.CONVERGE_PHASE,
Value: subject.canonicalChain,
SupplementalData: *subject.supplementalData,
},
Signature: signature,
Justification: &gpbft.Justification{
Vote: gpbft.Payload{
Phase: gpbft.PREPARE_PHASE,
Round: 22,
Instance: initialInstanceNumber,
SupplementalData: *subject.supplementalData,
},
},
Ticket: signature,
},
}
},
wantErr: "has justification from wrong round",
},
{
name: "justification with invalid value is error",
msg: func(subject *participantTestSubject) *gpbft.GMessage {
subject.mockValidSignature(somePowerEntry.PubKey, signature)
return &gpbft.GMessage{
Sender: somePowerEntry.ID,
Vote: gpbft.Payload{
Instance: initialInstanceNumber,
Phase: gpbft.COMMIT_PHASE,
Value: subject.canonicalChain,
SupplementalData: *subject.supplementalData,
},
Signature: signature,
Justification: &gpbft.Justification{
Vote: gpbft.Payload{
Instance: initialInstanceNumber,