-
Notifications
You must be signed in to change notification settings - Fork 4
/
dbft_test.go
1258 lines (1053 loc) · 38.5 KB
/
dbft_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 dbft_test
import (
"crypto/rand"
"encoding/binary"
"fmt"
"testing"
"time"
"github.com/nspcc-dev/dbft"
"github.com/nspcc-dev/dbft/internal/consensus"
"github.com/nspcc-dev/dbft/internal/crypto"
"github.com/nspcc-dev/dbft/timer"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)
type Payload = dbft.ConsensusPayload[crypto.Uint256]
type testState struct {
myIndex int
count int
privs []dbft.PrivateKey
pubs []dbft.PublicKey
ch []Payload
currHeight uint32
currHash crypto.Uint256
pool *testPool
preBlocks []dbft.PreBlock[crypto.Uint256]
blocks []dbft.Block[crypto.Uint256]
verify func(b dbft.Block[crypto.Uint256]) bool
}
type (
testTx uint64
testPool struct {
storage map[crypto.Uint256]testTx
}
)
const debugTests = false
func TestDBFT_OnStartPrimarySendPrepareRequest(t *testing.T) {
s := newTestState(2, 7)
t.Run("backup sends nothing on start", func(t *testing.T) {
s.currHeight = 0
service, err := dbft.New[crypto.Uint256](s.getOptions()...)
require.NoError(t, err)
service.Start(0)
require.Nil(t, s.tryRecv())
})
t.Run("primary send PrepareRequest on start", func(t *testing.T) {
s.currHeight = 1
service, _ := dbft.New[crypto.Uint256](s.getOptions()...)
service.Start(0)
p := s.tryRecv()
require.NotNil(t, p)
require.Equal(t, dbft.PrepareRequestType, p.Type())
require.EqualValues(t, 2, p.Height())
require.EqualValues(t, 0, p.ViewNumber())
require.NotNil(t, p.Payload())
require.EqualValues(t, 2, p.ValidatorIndex())
t.Run("primary send ChangeView on timeout", func(t *testing.T) {
service.OnTimeout(s.currHeight+1, 0)
// if there are many faulty must send RecoveryRequest
cv := s.tryRecv()
require.NotNil(t, cv)
require.Equal(t, dbft.RecoveryRequestType, cv.Type())
require.Nil(t, s.tryRecv())
// if all nodes are up must send ChangeView
for i := range service.LastSeenMessage {
service.LastSeenMessage[i] = &dbft.HeightView{s.currHeight + 1, 0}
}
service.OnTimeout(s.currHeight+1, 0)
cv = s.tryRecv()
require.NotNil(t, cv)
require.Equal(t, dbft.ChangeViewType, cv.Type())
require.EqualValues(t, 1, cv.GetChangeView().NewViewNumber())
require.Nil(t, s.tryRecv())
})
})
}
func TestDBFT_SingleNode(t *testing.T) {
for _, amev := range []bool{false, true} {
t.Run(fmt.Sprintf("AMEV %t", amev), func(t *testing.T) {
s := newTestState(0, 1)
s.currHeight = 2
opts := s.getOptions()
if amev {
opts = s.getAMEVOptions()
}
service, _ := dbft.New[crypto.Uint256](opts...)
service.Start(0)
p := s.tryRecv()
require.NotNil(t, p)
require.Equal(t, dbft.PrepareRequestType, p.Type())
require.EqualValues(t, 3, p.Height())
require.EqualValues(t, 0, p.ViewNumber())
require.NotNil(t, p.Payload())
require.EqualValues(t, 0, p.ValidatorIndex())
if amev {
cm := s.tryRecv()
require.NotNil(t, cm)
require.Equal(t, dbft.PreCommitType, cm.Type())
require.EqualValues(t, s.currHeight+1, cm.Height())
require.EqualValues(t, 0, cm.ViewNumber())
require.NotNil(t, cm.Payload())
require.EqualValues(t, 0, cm.ValidatorIndex())
}
cm := s.tryRecv()
require.NotNil(t, cm)
require.Equal(t, dbft.CommitType, cm.Type())
require.EqualValues(t, s.currHeight+1, cm.Height())
require.EqualValues(t, 0, cm.ViewNumber())
require.NotNil(t, cm.Payload())
require.EqualValues(t, 0, cm.ValidatorIndex())
b := s.nextBlock()
require.NotNil(t, b)
require.Equal(t, s.currHeight+1, b.Index())
})
}
}
func TestDBFT_OnReceiveRequestSendResponse(t *testing.T) {
s := newTestState(2, 7)
s.verify = func(b dbft.Block[crypto.Uint256]) bool {
for _, tx := range b.Transactions() {
if tx.(testTx)%10 == 0 {
return false
}
}
return true
}
t.Run("receive request from primary", func(t *testing.T) {
s.currHeight = 4
service, _ := dbft.New[crypto.Uint256](s.getOptions()...)
txs := []testTx{1}
s.pool.Add(txs[0])
p := s.getPrepareRequest(5, txs[0].Hash())
service.Start(0)
service.OnReceive(p)
resp := s.tryRecv()
require.NotNil(t, resp)
require.Equal(t, dbft.PrepareResponseType, resp.Type())
require.EqualValues(t, s.currHeight+1, resp.Height())
require.EqualValues(t, 0, resp.ViewNumber())
require.EqualValues(t, s.myIndex, resp.ValidatorIndex())
require.NotNil(t, resp.Payload())
require.Equal(t, p.Hash(), resp.GetPrepareResponse().PreparationHash())
// do nothing on second receive
service.OnReceive(p)
require.Nil(t, s.tryRecv())
t.Run("receive response from primary", func(t *testing.T) {
resp := s.getPrepareResponse(5, p.Hash(), 0)
service.OnReceive(resp)
require.Nil(t, s.tryRecv())
})
})
t.Run("change view on invalid tx", func(t *testing.T) {
s.currHeight = 4
service, _ := dbft.New[crypto.Uint256](s.getOptions()...)
txs := []testTx{10}
service.Start(0)
for i := range service.LastSeenMessage {
service.LastSeenMessage[i] = &dbft.HeightView{s.currHeight + 1, 0}
}
p := s.getPrepareRequest(5, txs[0].Hash())
service.OnReceive(p)
require.Nil(t, s.tryRecv())
service.OnTransaction(testTx(10))
cv := s.tryRecv()
require.NotNil(t, cv)
require.Equal(t, dbft.ChangeViewType, cv.Type())
require.EqualValues(t, s.currHeight+1, cv.Height())
require.EqualValues(t, 0, cv.ViewNumber())
require.EqualValues(t, s.myIndex, cv.ValidatorIndex())
require.NotNil(t, cv.Payload())
require.EqualValues(t, 1, cv.GetChangeView().NewViewNumber())
})
t.Run("receive invalid prepare request", func(t *testing.T) {
s.currHeight = 4
service, _ := dbft.New[crypto.Uint256](s.getOptions()...)
txs := []testTx{1, 2}
s.pool.Add(txs[0])
service.Start(0)
t.Run("wrong primary index", func(t *testing.T) {
p := s.getPrepareRequest(4, txs[0].Hash())
service.OnReceive(p)
require.Nil(t, s.tryRecv())
})
t.Run("old height", func(t *testing.T) {
p := s.getPrepareRequestWithHeight(5, 3, txs[0].Hash())
service.OnReceive(p)
require.Nil(t, s.tryRecv())
})
t.Run("does not have all transactions", func(t *testing.T) {
p := s.getPrepareRequest(5, txs[0].Hash(), txs[1].Hash())
service.OnReceive(p)
require.Nil(t, s.tryRecv())
// do nothing with already present transaction
service.OnTransaction(txs[0])
require.Nil(t, s.tryRecv())
service.OnTransaction(txs[1])
resp := s.tryRecv()
require.NotNil(t, resp)
require.Equal(t, dbft.PrepareResponseType, resp.Type())
require.EqualValues(t, s.currHeight+1, resp.Height())
require.EqualValues(t, 0, resp.ViewNumber())
require.EqualValues(t, s.myIndex, resp.ValidatorIndex())
require.NotNil(t, resp.Payload())
require.Equal(t, p.Hash(), resp.GetPrepareResponse().PreparationHash())
// do not send response twice
service.OnTransaction(txs[1])
require.Nil(t, s.tryRecv())
})
})
}
func TestDBFT_CommitOnTransaction(t *testing.T) {
s := newTestState(0, 4)
s.currHeight = 1
srv, _ := dbft.New[crypto.Uint256](s.getOptions()...)
srv.Start(0)
require.Nil(t, s.tryRecv())
tx := testTx(42)
req := s.getPrepareRequest(2, tx.Hash())
srv.OnReceive(req)
srv.OnReceive(s.getPrepareResponse(1, req.Hash(), 0))
srv.OnReceive(s.getPrepareResponse(3, req.Hash(), 0))
require.Nil(t, srv.Header()) // missing transaction.
// Test state for forming header.
s1 := &testState{
count: s.count,
pool: newTestPool(),
currHeight: 1,
pubs: s.pubs,
privs: s.privs,
}
s1.pool.Add(tx)
srv1, _ := dbft.New[crypto.Uint256](s1.getOptions()...)
srv1.Start(0)
srv1.OnReceive(req)
srv1.OnReceive(s1.getPrepareResponse(1, req.Hash(), 0))
srv1.OnReceive(s1.getPrepareResponse(3, req.Hash(), 0))
require.NotNil(t, srv1.Header())
for _, i := range []uint16{1, 2, 3} {
require.NoError(t, srv1.Header().Sign(s1.privs[i]))
c := s1.getCommit(i, srv1.Header().Signature(), 0)
srv.OnReceive(c)
}
require.Nil(t, s.nextBlock())
srv.OnTransaction(tx)
require.NotNil(t, s.nextBlock())
}
func TestDBFT_OnReceiveCommit(t *testing.T) {
s := newTestState(2, 4)
t.Run("send commit after enough responses", func(t *testing.T) {
s.currHeight = 1
service, _ := dbft.New[crypto.Uint256](s.getOptions()...)
service.Start(0)
req := s.tryRecv()
require.NotNil(t, req)
resp := s.getPrepareResponse(1, req.Hash(), 0)
service.OnReceive(resp)
require.Nil(t, s.tryRecv())
resp = s.getPrepareResponse(0, req.Hash(), 0)
service.OnReceive(resp)
cm := s.tryRecv()
require.NotNil(t, cm)
require.Equal(t, dbft.CommitType, cm.Type())
require.EqualValues(t, s.currHeight+1, cm.Height())
require.EqualValues(t, 0, cm.ViewNumber())
require.EqualValues(t, s.myIndex, cm.ValidatorIndex())
require.NotNil(t, cm.Payload())
pub := s.pubs[s.myIndex]
require.NoError(t, service.Header().Verify(pub, cm.GetCommit().Signature()))
t.Run("send recovery message on timeout", func(t *testing.T) {
service.OnTimeout(1, 0)
require.Nil(t, s.tryRecv())
service.OnTimeout(s.currHeight+1, 0)
r := s.tryRecv()
require.NotNil(t, r)
require.Equal(t, dbft.RecoveryMessageType, r.Type())
})
t.Run("process block after enough commits", func(t *testing.T) {
s0 := s.copyWithIndex(0)
require.NoError(t, service.Header().Sign(s0.privs[0]))
c0 := s0.getCommit(0, service.Header().Signature(), 0)
service.OnReceive(c0)
require.Nil(t, s.tryRecv())
require.Nil(t, s.nextBlock())
s1 := s.copyWithIndex(1)
require.NoError(t, service.Header().Sign(s1.privs[1]))
c1 := s1.getCommit(1, service.Header().Signature(), 0)
service.OnReceive(c1)
require.Nil(t, s.tryRecv())
b := s.nextBlock()
require.NotNil(t, b)
require.Equal(t, s.currHeight+1, b.Index())
})
})
}
func TestDBFT_OnReceiveRecoveryRequest(t *testing.T) {
s := newTestState(2, 4)
t.Run("send recovery message", func(t *testing.T) {
s.currHeight = 1
service, _ := dbft.New[crypto.Uint256](s.getOptions()...)
service.Start(0)
req := s.tryRecv()
require.NotNil(t, req)
resp := s.getPrepareResponse(1, req.Hash(), 0)
service.OnReceive(resp)
require.Nil(t, s.tryRecv())
resp = s.getPrepareResponse(0, req.Hash(), 0)
service.OnReceive(resp)
cm := s.tryRecv()
require.NotNil(t, cm)
rr := s.getRecoveryRequest(3)
service.OnReceive(rr)
rm := s.tryRecv()
require.NotNil(t, rm)
require.Equal(t, dbft.RecoveryMessageType, rm.Type())
other := s.copyWithIndex(3)
srv2, _ := dbft.New[crypto.Uint256](other.getOptions()...)
srv2.Start(0)
srv2.OnReceive(rm)
r2 := other.tryRecv()
require.NotNil(t, r2)
require.Equal(t, dbft.PrepareResponseType, r2.Type())
cm2 := other.tryRecv()
require.NotNil(t, cm2)
require.Equal(t, dbft.CommitType, cm2.Type())
pub := other.pubs[other.myIndex]
require.NoError(t, service.Header().Verify(pub, cm2.GetCommit().Signature()))
// send commit once during recovery
require.Nil(t, s.tryRecv())
})
}
func TestDBFT_OnReceiveRecoveryRequestResponds(t *testing.T) {
type recoveryset struct {
nodes int
sender int
receiver int
replies bool
}
var params []recoveryset
for _, nodes := range []int{4, 5, 7, 10} { // 5 is a bad BFT number, but we want to test the logic anyway.
for sender := range nodes {
for recv := range nodes {
params = append(params, recoveryset{nodes, sender, recv, false})
for i := 1; i <= ((nodes-1)/3)+1; i++ {
ind := (sender + i) % nodes
if ind == recv {
params[len(params)-1].replies = true
break
}
}
}
}
}
for _, param := range params {
t.Run(fmt.Sprintf("%d nodes, %d sender, %d receiver", param.nodes, param.sender, param.receiver), func(t *testing.T) {
s := newTestState(param.receiver, param.nodes)
s.currHeight = 1
service, _ := dbft.New[crypto.Uint256](s.getOptions()...)
service.Start(uint64(param.receiver))
_ = s.tryRecv() // Flush the queue if primary.
rr := s.getRecoveryRequest(uint16(param.sender))
service.OnReceive(rr)
rm := s.tryRecv()
if param.replies {
require.NotNil(t, rm)
require.Equal(t, dbft.RecoveryMessageType, rm.Type())
} else {
require.Nil(t, rm)
}
})
}
}
func TestDBFT_OnReceiveChangeView(t *testing.T) {
s := newTestState(2, 4)
t.Run("change view correctly", func(t *testing.T) {
s.currHeight = 6
service, _ := dbft.New[crypto.Uint256](s.getOptions()...)
service.Start(0)
resp := s.getChangeView(1, 1)
service.OnReceive(resp)
require.Nil(t, s.tryRecv())
resp = s.getChangeView(0, 1)
service.OnReceive(resp)
require.Nil(t, s.tryRecv())
service.OnTimeout(s.currHeight+1, 0)
cv := s.tryRecv()
require.NotNil(t, cv)
require.Equal(t, dbft.ChangeViewType, cv.Type())
t.Run("primary sends prepare request after timeout", func(t *testing.T) {
service.OnTimeout(s.currHeight+1, 1)
pr := s.tryRecv()
require.NotNil(t, pr)
require.Equal(t, dbft.PrepareRequestType, pr.Type())
})
})
}
func TestDBFT_Invalid(t *testing.T) {
t.Run("without keys", func(t *testing.T) {
_, err := dbft.New[crypto.Uint256]()
require.Error(t, err)
})
priv, pub := crypto.Generate(rand.Reader)
require.NotNil(t, priv)
require.NotNil(t, pub)
opts := []func(*dbft.Config[crypto.Uint256]){dbft.WithGetKeyPair[crypto.Uint256](func(_ []dbft.PublicKey) (int, dbft.PrivateKey, dbft.PublicKey) {
return -1, nil, nil
})}
t.Run("without Timer", func(t *testing.T) {
_, err := dbft.New(opts...)
require.Error(t, err)
})
opts = append(opts, dbft.WithTimer[crypto.Uint256](timer.New()))
t.Run("without CurrentHeight", func(t *testing.T) {
_, err := dbft.New(opts...)
require.Error(t, err)
})
opts = append(opts, dbft.WithCurrentHeight[crypto.Uint256](func() uint32 { return 0 }))
t.Run("without CurrentBlockHash", func(t *testing.T) {
_, err := dbft.New(opts...)
require.Error(t, err)
})
opts = append(opts, dbft.WithCurrentBlockHash[crypto.Uint256](func() crypto.Uint256 { return crypto.Uint256{} }))
t.Run("without GetValidators", func(t *testing.T) {
_, err := dbft.New(opts...)
require.Error(t, err)
})
opts = append(opts, dbft.WithGetValidators[crypto.Uint256](func(...dbft.Transaction[crypto.Uint256]) []dbft.PublicKey {
return []dbft.PublicKey{pub}
}))
t.Run("without NewBlockFromContext", func(t *testing.T) {
_, err := dbft.New(opts...)
require.Error(t, err)
})
opts = append(opts, dbft.WithNewBlockFromContext[crypto.Uint256](func(_ *dbft.Context[crypto.Uint256]) dbft.Block[crypto.Uint256] {
return nil
}))
t.Run("without NewConsensusPayload", func(t *testing.T) {
_, err := dbft.New(opts...)
require.Error(t, err)
})
opts = append(opts, dbft.WithNewConsensusPayload[crypto.Uint256](func(_ *dbft.Context[crypto.Uint256], _ dbft.MessageType, _ any) dbft.ConsensusPayload[crypto.Uint256] {
return nil
}))
t.Run("without NewPrepareRequest", func(t *testing.T) {
_, err := dbft.New(opts...)
require.Error(t, err)
})
opts = append(opts, dbft.WithNewPrepareRequest[crypto.Uint256](func(uint64, uint64, []crypto.Uint256) dbft.PrepareRequest[crypto.Uint256] {
return nil
}))
t.Run("without NewPrepareResponse", func(t *testing.T) {
_, err := dbft.New(opts...)
require.Error(t, err)
})
opts = append(opts, dbft.WithNewPrepareResponse[crypto.Uint256](func(crypto.Uint256) dbft.PrepareResponse[crypto.Uint256] {
return nil
}))
t.Run("without NewChangeView", func(t *testing.T) {
_, err := dbft.New(opts...)
require.Error(t, err)
})
opts = append(opts, dbft.WithNewChangeView[crypto.Uint256](func(byte, dbft.ChangeViewReason, uint64) dbft.ChangeView {
return nil
}))
t.Run("without NewCommit", func(t *testing.T) {
_, err := dbft.New(opts...)
require.Error(t, err)
})
opts = append(opts, dbft.WithNewCommit[crypto.Uint256](func([]byte) dbft.Commit {
return nil
}))
t.Run("without NewRecoveryRequest", func(t *testing.T) {
_, err := dbft.New(opts...)
require.Error(t, err)
})
opts = append(opts, dbft.WithNewRecoveryRequest[crypto.Uint256](func(uint64) dbft.RecoveryRequest {
return nil
}))
t.Run("without NewRecoveryMessage", func(t *testing.T) {
_, err := dbft.New(opts...)
require.Error(t, err)
})
opts = append(opts, dbft.WithNewRecoveryMessage[crypto.Uint256](func() dbft.RecoveryMessage[crypto.Uint256] {
return nil
}))
t.Run("with all defaults", func(t *testing.T) {
d, err := dbft.New(opts...)
require.NoError(t, err)
require.NotNil(t, d)
require.NotNil(t, d.Config.RequestTx)
require.NotNil(t, d.Config.GetTx)
require.NotNil(t, d.Config.GetVerified)
require.NotNil(t, d.Config.VerifyBlock)
require.NotNil(t, d.Config.Broadcast)
require.NotNil(t, d.Config.ProcessBlock)
require.NotNil(t, d.Config.GetBlock)
require.NotNil(t, d.Config.WatchOnly)
})
}
// TestDBFT_FourGoodNodesDeadlock checks that the following liveness lock is not really
// a liveness lock and there's a way to accept block in this situation.
// 0 :> [type |-> "cv", view |-> 1] <--- this is the primary at view 1
// 1 :> [type |-> "cv", view |-> 1] <--- this is the primary at view 0
// 2 :> [type |-> "commitSent", view |-> 0]
// 3 :> [type |-> "commitSent", view |-> 1]
//
// Test structure note: the test is organized to reproduce the liveness lock scenario
// described in https://github.com/neo-project/neo-modules/issues/792#issue-1609058923
// at the section named "1. Liveness lock with four non-faulty nodes". However, some
// steps are rearranged so that it's possible to reach the target network state described
// above. It is done because dbft implementation contains additional constraints comparing
// to the TLA+ model.
func TestDBFT_FourGoodNodesDeadlock(t *testing.T) {
r0 := newTestState(0, 4)
r0.currHeight = 4
s0, _ := dbft.New[crypto.Uint256](r0.getOptions()...)
s0.Start(0)
r1 := r0.copyWithIndex(1)
s1, _ := dbft.New[crypto.Uint256](r1.getOptions()...)
s1.Start(0)
r2 := r0.copyWithIndex(2)
s2, _ := dbft.New[crypto.Uint256](r2.getOptions()...)
s2.Start(0)
r3 := r0.copyWithIndex(3)
s3, _ := dbft.New[crypto.Uint256](r3.getOptions()...)
s3.Start(0)
// Step 1. The primary (at view 0) replica 1 sends the PrepareRequest message.
reqV0 := r1.tryRecv()
require.NotNil(t, reqV0)
require.Equal(t, dbft.PrepareRequestType, reqV0.Type())
// Step 2 will be performed later, see the comment to Step 2.
// Step 3. The backup (at view 0) replica 0 receives the PrepareRequest of
// view 0 and broadcasts its PrepareResponse.
s0.OnReceive(reqV0)
resp0V0 := r0.tryRecv()
require.NotNil(t, resp0V0)
require.Equal(t, dbft.PrepareResponseType, resp0V0.Type())
// Step 4 will be performed later, see the comment to Step 4.
// Step 5. The backup (at view 0) replica 2 receives the PrepareRequest of
// view 0 and broadcasts its PrepareResponse.
s2.OnReceive(reqV0)
resp2V0 := r2.tryRecv()
require.NotNil(t, resp2V0)
require.Equal(t, dbft.PrepareResponseType, resp2V0.Type())
// Step 6. The backup (at view 0) replica 2 collects M prepare messages (from
// itself and replicas 0, 1) and broadcasts the Commit message for view 0.
s2.OnReceive(resp0V0)
cm2V0 := r2.tryRecv()
require.NotNil(t, cm2V0)
require.Equal(t, dbft.CommitType, cm2V0.Type())
// Step 7. The backup (at view 0) replica 3 decides to change its view
// (possible on timeout) and sends the ChangeView message.
s3.OnReceive(resp0V0)
s3.OnReceive(resp2V0)
s3.OnTimeout(r3.currHeight+1, 0)
cv3V0 := r3.tryRecv()
require.NotNil(t, cv3V0)
require.Equal(t, dbft.ChangeViewType, cv3V0.Type())
// Step 2. The primary (at view 0) replica 1 decides to change its view
// (possible on timeout after receiving at least M non-commit messages from the
// current view) and sends the ChangeView message.
s1.OnReceive(resp0V0)
s1.OnReceive(cv3V0)
s1.OnTimeout(r1.currHeight+1, 0)
cv1V0 := r1.tryRecv()
require.NotNil(t, cv1V0)
require.Equal(t, dbft.ChangeViewType, cv1V0.Type())
// Step 4. The backup (at view 0) replica 0 decides to change its view
// (possible on timeout after receiving at least M non-commit messages from the
// current view) and sends the ChangeView message.
s0.OnReceive(cv3V0)
s0.OnTimeout(r0.currHeight+1, 0)
cv0V0 := r0.tryRecv()
require.NotNil(t, cv0V0)
require.Equal(t, dbft.ChangeViewType, cv0V0.Type())
// Step 8. The primary (at view 0) replica 1 collects M ChangeView messages
// (from itself and replicas 1, 3) and changes its view to 1.
s1.OnReceive(cv0V0)
require.Equal(t, uint8(1), s1.ViewNumber)
// Step 9. The backup (at view 0) replica 0 collects M ChangeView messages
// (from itself and replicas 0, 3) and changes its view to 1.
s0.OnReceive(cv1V0)
require.Equal(t, uint8(1), s0.ViewNumber)
// Step 10. The primary (at view 1) replica 0 sends the PrepareRequest message.
s0.OnTimeout(r0.currHeight+1, 1)
reqV1 := r0.tryRecv()
require.NotNil(t, reqV1)
require.Equal(t, dbft.PrepareRequestType, reqV1.Type())
// Step 11. The backup (at view 1) replica 1 receives the PrepareRequest of
// view 1 and sends the PrepareResponse.
s1.OnReceive(reqV1)
resp1V1 := r1.tryRecv()
require.NotNil(t, resp1V1)
require.Equal(t, dbft.PrepareResponseType, resp1V1.Type())
// Steps 12, 13 will be performed later, see the comments to Step 12, 13.
// Step 14. The backup (at view 0) replica 3 collects M ChangeView messages
// (from itself and replicas 0, 1) and changes its view to 1.
s3.OnReceive(cv0V0)
s3.OnReceive(cv1V0)
require.Equal(t, uint8(1), s3.ViewNumber)
// Intermediate step A. It is added to make step 14 possible. The backup (at
// view 1) replica 3 doesn't receive anything for a long time and sends
// RecoveryRequest.
s3.OnTimeout(r3.currHeight+1, 1)
rcvr3V1 := r3.tryRecv()
require.NotNil(t, rcvr3V1)
require.Equal(t, dbft.RecoveryRequestType, rcvr3V1.Type())
// Intermediate step B. The backup (at view 1) replica 1 should receive any
// message from replica 3 to be able to change view. However, it couldn't be
// PrepareResponse because replica 1 will immediately commit then. Thus, the
// only thing that remains is to receive RecoveryRequest from replica 3.
// Replica 1 then should answer with Recovery message.
s1.OnReceive(rcvr3V1)
rcvrResp1V1 := r1.tryRecv()
require.NotNil(t, rcvrResp1V1)
require.Equal(t, dbft.RecoveryMessageType, rcvrResp1V1.Type())
// Intermediate step C. The primary (at view 1) replica 0 should receive
// RecoveryRequest from replica 3. The purpose of this step is the same as
// in Intermediate step B.
s0.OnReceive(rcvr3V1)
rcvrResp0V1 := r0.tryRecv()
require.NotNil(t, rcvrResp0V1)
require.Equal(t, dbft.RecoveryMessageType, rcvrResp0V1.Type())
// Step 12. According to the neo-project/neo#792, at this step the backup (at view 1)
// replica 1 decides to change its view (possible on timeout) and sends the
// ChangeView message. However, the recovery message will be broadcast instead
// of CV, because there's additional condition: too much (>F) "lost" or committed
// nodes are present, see https://github.com/roman-khimov/dbft/blob/b769eb3e0f070d6eabb9443a5931eb4a2e46c538/send.go#L68.
// Replica 1 aware of replica 0 that has sent the PrepareRequest for view 1.
// It can also be aware of replica 2 that has committed at view 0, but it won't
// change the situation. The final way to allow CV is to receive something
// except from PrepareResponse from replica 3 to remove replica 3 from the list
// of "lost" nodes. That's why we'he added Intermediate steps A and B.
//
// After that replica 1 is allowed to send the CV message.
s1.OnTimeout(r1.currHeight+1, 1)
cv1V1 := r1.tryRecv()
require.NotNil(t, cv1V1)
require.Equal(t, dbft.ChangeViewType, cv1V1.Type())
// Step 13. The primary (at view 1) replica 0 decides to change its view
// (possible on timeout) and sends the ChangeView message.
s0.OnReceive(resp1V1)
s0.OnTimeout(r0.currHeight+1, 1)
cv0V1 := r0.tryRecv()
require.NotNil(t, cv0V1)
require.Equal(t, dbft.ChangeViewType, cv0V1.Type())
// Step 15. The backup (at view 1) replica 3 receives PrepareRequest of view
// 1 and broadcasts its PrepareResponse.
s3.OnReceive(reqV1)
resp3V1 := r3.tryRecv()
require.NotNil(t, resp3V1)
require.Equal(t, dbft.PrepareResponseType, resp3V1.Type())
// Step 16. The backup (at view 1) replica 3 collects M prepare messages and
// broadcasts the Commit message for view 1.
s3.OnReceive(resp1V1)
cm3V1 := r3.tryRecv()
require.NotNil(t, cm3V1)
require.Equal(t, dbft.CommitType, cm3V1.Type())
// Intermediate step D. It is needed to enable step 17 and to check that
// MoreThanFNodesCommittedOrLost works properly and counts Commit messages from
// any view.
s0.OnReceive(cm2V0)
s0.OnReceive(cm3V1)
// Step 17. The issue says that "The rest of undelivered messages eventually
// reaches their receivers, but it doesn't change the node's states.", but it's
// not true, the aim of the test is to show that replicas 0 and 1 still can
// commit at view 1 even after CV sent.
s0.OnReceive(resp3V1)
cm0V1 := r0.tryRecv()
require.NotNil(t, cm0V1)
require.Equal(t, dbft.CommitType, cm0V1.Type())
s1.OnReceive(cm0V1)
s1.OnReceive(resp3V1)
cm1V1 := r1.tryRecv()
require.NotNil(t, cm1V1)
require.Equal(t, dbft.CommitType, cm1V1.Type())
// Finally, send missing Commit message to replicas 0 and 1, they should accept
// the block.
require.Nil(t, r0.nextBlock())
s0.OnReceive(cm1V1)
require.NotNil(t, r0.nextBlock())
require.Nil(t, r1.nextBlock())
s1.OnReceive(cm3V1)
require.NotNil(t, r1.nextBlock())
}
func TestDBFT_OnReceiveCommitAMEV(t *testing.T) {
s := newTestState(2, 4)
t.Run("send preCommit after enough responses", func(t *testing.T) {
s.currHeight = 1
service, _ := dbft.New[crypto.Uint256](s.getAMEVOptions()...)
service.Start(0)
req := s.tryRecv()
require.NotNil(t, req)
resp := s.getPrepareResponse(1, req.Hash(), 0)
service.OnReceive(resp)
require.Nil(t, s.tryRecv())
resp = s.getPrepareResponse(0, req.Hash(), 0)
service.OnReceive(resp)
cm := s.tryRecv()
require.NotNil(t, cm)
require.Equal(t, dbft.PreCommitType, cm.Type())
require.EqualValues(t, s.currHeight+1, cm.Height())
require.EqualValues(t, 0, cm.ViewNumber())
require.EqualValues(t, s.myIndex, cm.ValidatorIndex())
require.NotNil(t, cm.Payload())
pub := s.pubs[s.myIndex]
require.NoError(t, service.PreHeader().Verify(pub, cm.GetPreCommit().Data()))
t.Run("send commit after enough preCommits", func(t *testing.T) {
s0 := s.copyWithIndex(0)
require.NoError(t, service.PreHeader().SetData(s0.privs[0]))
preC0 := s0.getPreCommit(0, service.PreHeader().Data(), 0)
service.OnReceive(preC0)
require.Nil(t, s.tryRecv())
require.Nil(t, s.nextPreBlock())
require.Nil(t, s.nextBlock())
s1 := s.copyWithIndex(1)
require.NoError(t, service.PreHeader().SetData(s1.privs[1]))
preC1 := s1.getPreCommit(1, service.PreHeader().Data(), 0)
service.OnReceive(preC1)
b := s.nextPreBlock()
require.NotNil(t, b)
require.Equal(t, []byte{0, 0, 0, 2}, b.Data()) // After SetData it's equal to node index.
require.Nil(t, s.nextBlock())
c := s.tryRecv()
require.NotNil(t, c)
require.Equal(t, dbft.CommitType, c.Type())
require.EqualValues(t, s.currHeight+1, c.Height())
require.EqualValues(t, 0, c.ViewNumber())
require.EqualValues(t, s.myIndex, c.ValidatorIndex())
require.NotNil(t, c.Payload())
t.Run("process block a after enough commitAcks", func(t *testing.T) {
s0 := s.copyWithIndex(0)
require.NoError(t, service.Header().Sign(s0.privs[0]))
c0 := s0.getAMEVCommit(0, service.Header().Signature())
service.OnReceive(c0)
require.Nil(t, s.tryRecv())
require.Nil(t, s.nextPreBlock())
require.Nil(t, s.nextBlock())
s1 := s.copyWithIndex(1)
require.NoError(t, service.Header().Sign(s1.privs[1]))
c1 := s1.getAMEVCommit(1, service.Header().Signature())
service.OnReceive(c1)
require.Nil(t, s.tryRecv())
require.Nil(t, s.nextPreBlock())
b := s.nextBlock()
require.NotNil(t, b)
require.Equal(t, s.currHeight+1, b.Index())
})
})
})
}
func TestDBFT_CachedMessages(t *testing.T) {
for _, amev := range []bool{false, true} {
t.Run(fmt.Sprintf("AMEV %t", amev), func(t *testing.T) {
s2 := newTestState(2, 4)
s2.currHeight = 1
s1 := newTestState(1, 4)
s1.currHeight = 1
opts := s2.getOptions()
if amev {
opts = s2.getAMEVOptions()
}
service2, _ := dbft.New[crypto.Uint256](opts...)
service2.Start(0)
opts = s1.getOptions()
if amev {
opts = s1.getAMEVOptions()
}
service1, _ := dbft.New[crypto.Uint256](opts...)
service1.Start(0)
req := s2.tryRecv()
require.NotNil(t, req) // Primary sends a request.
require.Equal(t, dbft.PrepareRequestType, req.Type())
require.Nil(t, s1.tryRecv()) // Backup waits.
cv0 := s1.getChangeView(0, 1)
cv3 := s1.getChangeView(3, 1)
service1.OnReceive(cv0)
service1.OnReceive(cv3)
service1.OnTimeout(s1.currHeight+1, 0)
cv := s1.tryRecv()
require.NotNil(t, cv)
require.Equal(t, dbft.ChangeViewType, cv.Type())
service1.OnTimeout(s1.currHeight+1, 1)
req = s1.tryRecv()
require.NotNil(t, req)
require.Equal(t, dbft.PrepareRequestType, req.Type())
resp := s1.getPrepareResponse(3, req.Hash(), 1)
service1.OnReceive(resp)
require.Nil(t, s1.tryRecv())
service2.OnReceive(resp) // From the future.
require.Nil(t, s2.tryRecv())
resp = s1.getPrepareResponse(0, req.Hash(), 1)
service2.OnReceive(resp) // From the future.
require.Nil(t, s2.tryRecv())
service1.OnReceive(resp)
cm := s1.tryRecv()
require.NotNil(t, cm)
service2.OnReceive(cm)
require.Nil(t, s2.tryRecv())
if amev {
require.Equal(t, dbft.PreCommitType, cm.Type())
require.EqualValues(t, s1.currHeight+1, cm.Height())
require.EqualValues(t, 1, cm.ViewNumber())
require.EqualValues(t, s1.myIndex, cm.ValidatorIndex())
require.NotNil(t, cm.Payload())
pub := s1.pubs[s1.myIndex]
require.NoError(t, service1.PreHeader().Verify(pub, cm.GetPreCommit().Data()))
} else {
require.Equal(t, dbft.CommitType, cm.Type())
require.EqualValues(t, s1.currHeight+1, cm.Height())
require.EqualValues(t, 1, cm.ViewNumber())
require.EqualValues(t, s1.myIndex, cm.ValidatorIndex())
require.NotNil(t, cm.Payload())
}
service2.OnReceive(cv0)
service2.OnReceive(cv3)
service2.OnTimeout(s2.currHeight+1, 0)
cv = s2.tryRecv()
require.NotNil(t, cv)
require.Equal(t, dbft.ChangeViewType, cv.Type())
require.Equal(t, 1, int(service2.ViewNumber))
// s2 has some PrepareResponses, but doesn't have a request.
service2.OnReceive(req)
resp = s2.tryRecv()
require.NotNil(t, resp)
require.Equal(t, dbft.PrepareResponseType, resp.Type())
cm = s2.tryRecv()
require.NotNil(t, cm)
if amev {
require.Equal(t, dbft.PreCommitType, cm.Type())
require.EqualValues(t, s2.currHeight+1, cm.Height())
require.EqualValues(t, 1, cm.ViewNumber())
require.EqualValues(t, s2.myIndex, cm.ValidatorIndex())
require.NotNil(t, cm.Payload())
pub := s1.pubs[s1.myIndex]
require.NoError(t, service1.PreHeader().Verify(pub, cm.GetPreCommit().Data()))
service2.OnReceive(s2.getPreCommit(0, service2.PreHeader().Data(), 1))
cm = s2.tryRecv()
require.NotNil(t, cm)
require.Equal(t, dbft.CommitType, cm.Type())
} else {