-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqbft.go
868 lines (705 loc) · 20.2 KB
/
qbft.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
package qbft
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"math"
"time"
)
type Timer interface {
TimeCh() <-chan time.Time
SetTimeout(n time.Duration)
}
type Config struct {
Transport Transport
Logger *log.Logger
Signer Signer
Timer Timer
RoundTimeout time.Duration
}
func DefaultConfig() *Config {
return &Config{
Logger: log.New(ioutil.Discard, "", log.LstdFlags),
Timer: newDefaultTimer(),
RoundTimeout: 10 * time.Second,
}
}
func (c *Config) ApplyOps(opts ...ConfigOption) {
for _, opt := range opts {
opt(c)
}
}
type ConfigOption func(*Config)
func WithRoundTimeout(timeout time.Duration) ConfigOption {
return func(c *Config) {
c.RoundTimeout = timeout
}
}
func WithTimer(t Timer) ConfigOption {
return func(c *Config) {
c.Timer = t
}
}
func WithTransport(t Transport) ConfigOption {
return func(c *Config) {
c.Transport = t
}
}
func WithLogger(l *log.Logger) ConfigOption {
return func(c *Config) {
c.Logger = l
}
}
func WithSigner(s Signer) ConfigOption {
return func(c *Config) {
c.Signer = s
}
}
type Signer interface {
SignMessage(msg []byte) ([]byte, error)
RecoverSigner(msg, signature []byte) (NodeID, error)
}
func New(localID NodeID, opts ...ConfigOption) *QBFT {
cfg := DefaultConfig()
cfg.ApplyOps(opts...)
q := &QBFT{
config: cfg,
msgCh: make(chan QBFTMessageWithRecipient, 1000), // for testing
state: newState(cfg.Timer),
localID: localID,
}
return q
}
func (q *QBFT) Start() {
for {
select {
case msg := <-q.config.Transport.Recv():
// decode any signedContainer from the message
if proposal := msg.Message.ProposalMessage; proposal != nil {
if err := q.recoverSigner2(&proposal.Payload); err != nil {
panic(err)
}
for _, msg := range proposal.RoundChangeCertificate {
if err := q.recoverSigner2(&msg.Payload); err != nil {
panic(err)
}
}
} else if prepare := msg.Message.PrepareMessage; prepare != nil {
if err := q.recoverSigner2(&prepare.Payload); err != nil {
panic(err)
}
} else if commit := msg.Message.CommitMessage; commit != nil {
if err := q.recoverSigner2(&commit.Payload); err != nil {
panic(err)
}
} else if roundChange := msg.Message.RoundChangeMessage; roundChange != nil {
if err := q.recoverSigner2(&roundChange.Payload); err != nil {
panic(err)
}
} else {
panic("TODO")
}
q.msgCh <- *msg
case <-q.closeCh:
return
}
}
}
// QBFT is a BFT consensus protocol
type QBFT struct {
config *Config
backend Backend
state *state
localID NodeID
closeCh chan struct{}
msgCh chan QBFTMessageWithRecipient
resCh chan struct{}
stopCh chan struct{}
}
func (q *QBFT) readMsgs() <-chan QBFTMessageWithRecipient {
return q.msgCh
}
func (q *QBFT) SetBackend(b Backend) {
q.backend = b
q.state.resetState(b.ValidatorSet())
q.startNewRound(0)
q.resCh = make(chan struct{})
}
func (q *QBFT) Close() {
close(q.closeCh)
}
func (q *QBFT) Run() chan struct{} {
q.stopCh = make(chan struct{})
go q.runImpl()
return q.resCh
}
func (q *QBFT) Stop() {
close(q.stopCh)
// wait for the runImpl to finish
<-q.resCh
}
func (q *QBFT) runImpl() {
// There are two conditions to do processing:
// 1. A new message has arrived for the sequence.
// 2. Round change timeout.
msgs := q.readMsgs()
if err := q.buildInitialProposal(); err != nil {
// log it
panic(err)
}
for {
select {
case msg := <-msgs:
q.handleMessage(&msg)
case <-q.state.timer.TimeCh():
q.handleMessage(nil)
case <-q.stopCh:
q.config.Logger.Printf("[INFO]: Node is out")
close(q.resCh)
return
}
// check if the channel has been closed due to an execution
// of a commit message which created a sealed proposal.
select {
case <-q.resCh:
return
default:
}
}
}
func (q *QBFT) buildInitialProposal() error {
proposer := q.state.validators.CalculateProposer(q.state.round)
if proposer != q.localID {
return nil
}
block, digest, err := q.backend.BuildProposal(q.state.round)
if err != nil {
return err
}
if block == nil {
return fmt.Errorf("empty proposal")
}
// TODO: Validate that the fields are correct.
proposal := &Proposal{
Height: q.backend.Height(),
Round: 0,
Digest: digest,
}
signedProposal, err := q.signMessage(UnsignedPayload{Proposal: proposal})
if err != nil {
return err
}
q.config.Logger.Print("_ build proposal _")
proposalMsg := ProposalMessage{
Payload: signedProposal,
ProposedBlock: block,
}
ownMsg := q.multicast(QBFTMessagePayload{ProposalMessage: &proposalMsg})
if err := q.uponProposal(ownMsg); err != nil {
return err
}
return nil
}
func (q *QBFT) handleMessage(m *QBFTMessageWithRecipient) {
if m == nil {
q.startNewRoundAndSendRoundChange(q.state.round + 1)
return
}
msg := m.Message
if m.Message.Height() != q.backend.Height() {
// TODO: handle this with the message queue
q.config.Logger.Printf("[INFO]: Out of order message, height=%d, typ=%s", m.Message.Height(), m.Message.typ())
return
}
if msg.PrepareMessage != nil {
if err := q.uponPrepare(*m); err != nil {
panic(err)
}
} else if msg.ProposalMessage != nil {
if err := q.uponProposal(*m); err != nil {
panic(err)
}
} else if msg.CommitMessage != nil {
if err := q.uponCommit(*m); err != nil {
panic(err)
}
} else if msg.RoundChangeMessage != nil {
if err := q.uponRoundChange(*m); err != nil {
panic(err)
}
}
}
func (q *QBFT) recoverSigners2(msgs *[]SignedContainer) error {
for _, msg := range *msgs {
signer, err := q.config.Signer.RecoverSigner([]byte{}, msg.Signature)
if err != nil {
return err
}
msg.sender = signer
}
return nil
}
func (q *QBFT) recoverSigner2(msg *SignedContainer) error {
signer, err := q.config.Signer.RecoverSigner([]byte{}, msg.Signature)
if err != nil {
return err
}
msg.sender = signer
return nil
}
func (q *QBFT) recoverSigner(msg SignedContainer) (NodeID, error) {
return q.config.Signer.RecoverSigner([]byte{}, msg.Signature)
}
// getExtendedRCC returns the highest round change set
func (q *QBFT) getExtendedRCC() (*messageSet[RoundChangeMessage], uint64) {
var rccSet *messageSet[RoundChangeMessage]
var maxRound uint64
// Using the 'messageSet' routing we ensure already that:
// 1. all messages are sent by different senders.
// 2. all messages are from the same round.
// 3. senders all part of the activa validator set for this height.
for round, msgSet := range q.state.roundChangeMessages {
// there is not enough quorum for this round
if !msgSet.isQuorum(q.state.quorum) {
continue
}
for _, msg := range msgSet.messageMap {
// the round number of the Round-Change message is either:
// 1. higher than the current round
// 2. equal to the current round provided that no Proposal message for the round
// has been accepted
roundChange := msg.Payload.UnsignedPayload.RoundChange
if roundChange.Round < q.state.round && (roundChange.Round == q.state.round && q.state.acceptedPB != nil) {
continue
}
// the prepared-certificate is valid
if !q.isValidPC(roundChange.PreparedCertificate, q.state.round, q.backend.Height()) {
continue
}
// TODO: The block hash included in all of the messages in the prepared certificate
// corresponds to the hash of the proposed block included in the round-change message
}
// the prepared-certificate is valid
// q.isValidPC(nil, 0, 0)
if rccSet == nil || maxRound < round {
rccSet = msgSet
maxRound = round
}
}
return rccSet, maxRound
}
func (q *QBFT) startNewRound(round uint64) {
q.config.Logger.Printf("start new round, round=%d", round)
// TODO: unit test that the timeout is set with a round change
if round == 0 || round > q.state.round {
// double the timeout for each round
timeout := q.config.RoundTimeout.Seconds() + math.Pow(2, float64(round))
q.state.setTimeout(time.Duration(int64(timeout)) * time.Second)
}
q.state.round = round
q.state.acceptedPB = nil
q.state.commitSent = false
}
func (q *QBFT) startNewRoundAndSendRoundChange(newRound uint64) error {
q.config.Logger.Printf("[INFO] round change timeout, round=%d", newRound)
q.config.Logger.Printf("[INFO] proposer for round=%d is %s", newRound, q.backend.ValidatorSet().CalculateProposer(newRound))
q.startNewRound(newRound)
roundChange, err := q.signMessage(UnsignedPayload{RoundChange: &RoundChange{
Height: q.backend.Height(),
Round: newRound,
PreparedCertificate: q.state.latestPC,
}})
if err != nil {
return err
}
roundChangeMessage := RoundChangeMessage{
Payload: roundChange,
LatestProposedBlock: q.state.acceptedPB,
}
msg := q.multicast(QBFTMessagePayload{RoundChangeMessage: &roundChangeMessage})
if err := q.uponRoundChange(msg); err != nil {
return err
}
return nil
}
func (q *QBFT) uponRoundChange(m QBFTMessageWithRecipient) error {
q.config.Logger.Printf("[DEBUG] received 'round-change' message: from=%s, height=%d, round=%d", m.Sender, m.Message.Height(), m.Message.Round())
if len(m.Message.RoundChangeMessage.Payload.UnsignedPayload.RoundChange.PreparedCertificate) != 0 {
// if it is included, it should be correct
if !q.isValidPC(m.Message.RoundChangeMessage.Payload.UnsignedPayload.RoundChange.PreparedCertificate, 100, q.backend.Height()) {
panic("it should not happen")
}
}
if !q.state.addMessage(m) {
return nil
}
rcc, round := q.getExtendedRCC()
if rcc == nil {
return nil
}
q.config.Logger.Printf("[INFO]: there is an extended rcc round %d", round)
q.startNewRound(round)
q.config.Logger.Printf("[INFO]: New proposer, %s", q.backend.ValidatorSet().CalculateProposer(round))
if q.backend.ValidatorSet().CalculateProposer(round) != q.localID {
return nil
}
q.config.Logger.Printf("[INFO]: I am proposer for round change!")
// we are the proposer for this round change
// check if any of the round change messages includes a proposal for a block
var (
block *Block
maxRound uint64
)
for _, msg := range rcc.messageMap {
proposedBlock := msg.LatestProposedBlock
if proposedBlock == nil {
continue
}
if block != nil && proposedBlock.RoundNumber > maxRound {
block = proposedBlock
maxRound = proposedBlock.RoundNumber
}
}
if block == nil {
var err error
block, _, err = q.backend.BuildProposal(round)
if err != nil {
return err
}
}
proposal := Proposal{
Height: q.backend.Height(),
Round: round,
}
signedProposal, err := q.signMessage(UnsignedPayload{Proposal: &proposal})
if err != nil {
return err
}
q.config.Logger.Print("built round change proposal proposal")
proposalMsg := ProposalMessage{
Payload: signedProposal,
ProposedBlock: block,
RoundChangeCertificate: []*RoundChangeMessage{},
}
for _, msg := range rcc.messageMap {
proposalMsg.RoundChangeCertificate = append(proposalMsg.RoundChangeCertificate, &msg)
}
ownMsg := q.multicast(QBFTMessagePayload{ProposalMessage: &proposalMsg})
if err := q.uponProposal(ownMsg); err != nil {
return err
}
return nil
}
func (q *QBFT) hasPartialRoundChangeQuorum() (bool, uint64) {
// there is at least a partial quorum of nodes that are already
// in a higher round.
currentRound := q.state.round
senders := map[NodeID]struct{}{}
var minRound uint64
var minRoundSet bool
for round, msgsPerRound := range q.state.roundChangeMessages {
if round <= currentRound {
continue
}
if !minRoundSet && round < minRound {
minRound = round
minRoundSet = true
}
for sender := range msgsPerRound.messageMap {
senders[sender] = struct{}{}
}
}
if len(senders) > int(q.state.quorum)/2 {
// TODO
return true, minRound
}
return false, minRound
}
func (q *QBFT) isValidPC(pc []SignedContainer, rlimit, height uint64) bool {
if len(pc) == 0 {
return true
}
var (
proposalFound bool
expectedRound *uint64
totalVotingPower uint64
)
alreadyVoted := map[NodeID]struct{}{}
for _, container := range pc {
msg := container.UnsignedPayload
var msgRound uint64
if msg.Prepare != nil {
msgRound = msg.Prepare.Round
// Only one pre per validator allowed
if _, ok := alreadyVoted[container.sender]; ok {
return false
}
alreadyVoted[container.sender] = struct{}{}
// All the senders must exist in the validator set
votingPower, ok := q.state.validators.Exists(container.sender)
if !ok {
return false
}
totalVotingPower += votingPower
} else if msg.Proposal != nil {
msgRound = msg.Proposal.Round
// only one proposal allowed in the pc
if proposalFound {
return false
}
proposalFound = true
// proposal is sent by the proposer
if q.state.validators.CalculateProposer(msgRound) != container.sender {
return false
}
} else {
return false
}
// all the messages in the pc are for the same round
if expectedRound == nil {
expectedRound = &msgRound
} else if *expectedRound != msgRound {
return false
}
}
// the voting power of all the senders in the PC must be higher
// than the
if totalVotingPower < q.state.quorum {
return false
}
// the round included in all the messages is lower than rlimit
if *expectedRound >= rlimit {
return false
}
return true
}
func (q *QBFT) uponProposal(m QBFTMessageWithRecipient) error {
q.config.Logger.Printf("[DEBUG] received 'proposal' message: from=%s, height=%d, round=%d", m.Sender, m.Message.Height(), m.Message.Round())
if q.state.acceptedPB != nil {
return nil
}
msg := m.Message.ProposalMessage
var prepare *PrepareMessage
if msg.Payload.UnsignedPayload.Proposal.Round == 0 {
// the proposer is expected
if msg.Payload.sender != q.state.validators.CalculateProposer(0) {
return fmt.Errorf("unexpected proposer")
}
// save proposal in the state to calculate QB
q.state.addMessage(m)
// proposal for round 0
signedPrepare, err := q.signMessage(UnsignedPayload{Prepare: &Prepare{
Height: q.backend.Height(),
Round: 0,
}})
if err != nil {
return err
}
q.state.acceptedPB = msg.ProposedBlock
prepare = &PrepareMessage{
Payload: signedPrepare,
}
} else {
// special fallback case for rounds != 0
round := msg.Payload.UnsignedPayload.Proposal.Round
// the proposer is expected
if msg.Payload.sender != q.state.validators.CalculateProposer(round) {
return fmt.Errorf("unexpected proposer")
}
// there is a quorum of rcc and messages are signed
// by different validators (the quorum is on the payload part)
// TODO: optimize
sigs := []SignedContainer{}
for _, msg := range msg.RoundChangeCertificate {
sigs = append(sigs, msg.Payload)
}
ok := q.isQuorum(sigs, func(msg SignedContainer) bool {
roundChange := msg.UnsignedPayload.RoundChange
if roundChange.Height != q.backend.Height() {
return false
}
if roundChange.Round != round {
return false
}
return true
})
if !ok {
return fmt.Errorf("failed to validate roudn change justification")
}
// find the round-change with the highest round
var rccBlock *Block
for _, roundChangeMsg := range msg.RoundChangeCertificate {
if roundChangeMsg.LatestProposedBlock == nil {
continue
}
pc := roundChangeMsg.Payload.UnsignedPayload.RoundChange.PreparedCertificate
if !q.isValidPC(pc, 1000, q.backend.Height()) {
continue
}
if rccBlock == nil || rccBlock.RoundNumber < roundChangeMsg.LatestProposedBlock.RoundNumber {
rccBlock = roundChangeMsg.LatestProposedBlock
}
}
if rccBlock != nil {
// validate that this block is the one provided in the proposal
if !bytes.Equal(rccBlock.Body, msg.ProposedBlock.Body) {
panic("bad")
}
}
q.startNewRound(msg.ProposedBlock.RoundNumber)
q.state.addMessage(m)
q.state.acceptedPB = msg.ProposedBlock
signedPrepare, err := q.signMessage(UnsignedPayload{Prepare: &Prepare{
Height: q.backend.Height(),
Round: msg.ProposedBlock.RoundNumber,
}})
if err != nil {
return err
}
prepare = &PrepareMessage{
Payload: signedPrepare,
}
}
// send any prepare message
ownMsg := q.multicast(QBFTMessagePayload{PrepareMessage: prepare})
if err := q.uponPrepare(ownMsg); err != nil {
return err
}
return nil
}
func (q *QBFT) isQuorum(msgs []SignedContainer, checkFn func(SignedContainer) bool) bool {
alreadyVoted := map[NodeID]struct{}{}
totalVotingPower := uint64(0)
for _, msg := range msgs {
if !checkFn(msg) {
return false
}
// Only one pre per validator allowed
//if _, ok := alreadyVoted[msg.sender]; ok {
// fmt.Println("H")
// return false
//}
alreadyVoted[msg.sender] = struct{}{}
// All the senders must exist in the validator set
votingPower, ok := q.state.validators.Exists(msg.sender)
if !ok {
return false
}
totalVotingPower += votingPower
}
if totalVotingPower < q.state.quorum {
return false
}
return true
}
func (q *QBFT) uponPrepare(msg QBFTMessageWithRecipient) error {
// dummy way to insert the data and call it without a message
q.config.Logger.Printf("[DEBUG] received 'prepare' message: from=%s, height=%d, round=%d", msg.Sender, msg.Message.Height(), msg.Message.Round())
if !q.state.addMessage(msg) {
return nil
}
// check if there is quorum for the updated round
if !q.state.preparedMessages.atRound(q.state.round).isQuorum(q.state.quorum) {
return nil
}
// we have not seen yet the proposal
if q.state.acceptedPB == nil {
return nil
}
// check if the commit message was not sent yet
if q.state.commitSent {
return nil
}
// send commit message
signedCommit, err := q.signMessage(UnsignedPayload{Commit: &Commit{
Height: q.backend.Height(),
Round: q.state.round,
}})
if err != nil {
return err
}
// find valid proposal message and prepare messages for this round
latestPC := []SignedContainer{}
proposalMap := q.state.proposalMessages.atRound(q.state.round).messageMap
if len(proposalMap) != 1 {
return fmt.Errorf("only one proposal per round expected but %d found", len(proposalMap))
}
for _, proposal := range proposalMap {
latestPC = append(latestPC, proposal.Payload)
}
for _, prepare := range q.state.preparedMessages.atRound(q.state.round).messageMap {
latestPC = append(latestPC, prepare.Payload)
}
q.state.latestPC = latestPC
commitMsg := CommitMessage{
Payload: signedCommit,
}
q.state.commitSent = true
ownMsg := q.multicast(QBFTMessagePayload{CommitMessage: &commitMsg})
if err := q.uponCommit(ownMsg); err != nil {
return err
}
return nil
}
func (q *QBFT) uponCommit(msg QBFTMessageWithRecipient) error {
q.config.Logger.Printf("[DEBUG] received 'commit' message: from=%s, height=%d, round=%d", msg.Sender, msg.Message.Height(), msg.Message.Round())
if !q.state.addMessage(msg) {
return nil
}
// check if there is quorum of commits
if !q.state.commitMessages.atRound(q.state.round).isQuorum(q.state.quorum) {
return nil
}
if q.state.acceptedPB == nil {
return nil
}
if q.state.finalisedBlockSent {
return nil
}
q.config.Logger.Printf("[INFO]: quorum of commits")
q.config.Logger.Print("Insert proposal")
sealedProposal := &SealedProposal{
Block: q.state.acceptedPB.Copy(),
}
q.state.finalisedBlockSent = true
q.backend.Insert(sealedProposal)
q.finish()
return nil
}
func (q *QBFT) finish() {
close(q.resCh)
}
func (q *QBFT) signMessage(msg UnsignedPayload) (SignedContainer, error) {
signature, err := q.config.Signer.SignMessage(nil)
if err != nil {
return SignedContainer{}, err
}
return SignedContainer{UnsignedPayload: msg, Signature: signature, sender: q.localID}, nil
}
func (q *QBFT) multicast(msg QBFTMessagePayload) QBFTMessageWithRecipient {
q.config.Logger.Printf("[INFO] multicast message: type=%s, round=%d, height=%d", msg.typ(), msg.Round(), msg.Height())
err := q.config.Transport.Send(&QBFTMessageWithRecipient{
Message: msg,
Sender: q.localID,
})
if err != nil {
q.config.Logger.Printf("[INFO] failed to send message: %v", err)
}
return QBFTMessageWithRecipient{
Message: msg,
Sender: q.localID,
}
}
// isValidRoundChange returns `true` if and only if `sPayload` is a valid signed RoundChange
// payload for height `height`, round `round` under the assumption that the current set
// of validators is `validators`.
func (q *QBFT) isValidRoundChange(msg SignedContainer) error {
return nil
}
func getQuorumNumbers(nodes uint64) (quorum uint64, faulty uint64) {
// https://arxiv.org/pdf/1909.10194.pdf
quorum = uint64(math.Ceil(float64(nodes*2) / 3))
faulty = uint64(math.Floor(float64(nodes-1) / 3))
return
}