forked from 0xPolygon/pbft-consensus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
consensus.go
819 lines (668 loc) · 21.2 KB
/
consensus.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
package pbft
import (
"bytes"
"context"
"fmt"
"log"
"os"
"time"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
type RoundTimeout func(uint64) time.Duration
type Config struct {
// ProposalTimeout is the time to wait for the proposal
// from the validator. It defaults to Timeout
ProposalTimeout time.Duration
// Timeout is the time to wait for validation and
// round change messages
Timeout time.Duration
// Logger is the logger to output info
Logger *log.Logger
// Tracer is the OpenTelemetry tracer to log traces
Tracer trace.Tracer
// RoundTimeout is a function that calculates timeout based on a round number
RoundTimeout RoundTimeout
}
type ConfigOption func(*Config)
func WithTimeout(p time.Duration) ConfigOption {
return func(c *Config) {
c.Timeout = p
}
}
func WithProposalTimeout(p time.Duration) ConfigOption {
return func(c *Config) {
c.ProposalTimeout = p
}
}
func WithLogger(l *log.Logger) ConfigOption {
return func(c *Config) {
c.Logger = l
}
}
func WithTracer(t trace.Tracer) ConfigOption {
return func(c *Config) {
c.Tracer = t
}
}
func WithRoundTimeout(roundTimeout RoundTimeout) ConfigOption {
return func(c *Config) {
c.RoundTimeout = roundTimeout
}
}
const (
defaultTimeout = 2 * time.Second
maxTimeout = 300 * time.Second
maxTimeoutExponent = 8
)
func DefaultConfig() *Config {
return &Config{
Timeout: defaultTimeout,
ProposalTimeout: defaultTimeout,
Logger: log.New(os.Stderr, "", log.LstdFlags),
Tracer: trace.NewNoopTracerProvider().Tracer(""),
RoundTimeout: exponentialTimeout,
}
}
func (c *Config) ApplyOps(opts ...ConfigOption) {
for _, opt := range opts {
opt(c)
}
}
type SealedProposal struct {
Proposal *Proposal
CommittedSeals [][]byte
Proposer NodeID
Number uint64
}
type Backend interface {
// BuildProposal builds a proposal for the current round (used if proposer)
BuildProposal() (*Proposal, error)
// Validate validates a raw proposal (used if non-proposer)
Validate(*Proposal) error
// Insert inserts the sealed proposal
Insert(p *SealedProposal) error
// Height returns the height for the current round
Height() uint64
// ValidatorSet returns the validator set for the current round
ValidatorSet() ValidatorSet
// Init is used to signal the backend that a new round is going to start.
Init(*RoundInfo)
// IsStuck returns whether the pbft is stucked
IsStuck(num uint64) (uint64, bool)
// ValidateCommit is used to validate that a given commit is valid
ValidateCommit(from NodeID, seal []byte) error
}
// RoundInfo is the information about the round
type RoundInfo struct {
IsProposer bool
Proposer NodeID
Locked bool
}
// Pbft represents the PBFT consensus mechanism object
type Pbft struct {
// Output logger
logger *log.Logger
// Config is the configuration of the consensus
config *Config
// inter is the interface with the runtime
backend Backend
// state is the reference to the current state machine
state *currentState
// validator is the signing key for this instance
validator SignKey
// ctx is the current execution context for an pbft round
ctx context.Context
// msgQueue is a queue that stores all the incomming gossip messages
msgQueue *msgQueue
// updateCh is a channel used to notify when a new gossip message arrives
updateCh chan struct{}
// Transport is the interface for the gossip transport
transport Transport
// tracer is a reference to the OpenTelemetry tracer
tracer trace.Tracer
// calculates timeout for a specific round
roundTimeout RoundTimeout
forceTimeoutCh bool
}
type SignKey interface {
NodeID() NodeID
Sign(b []byte) ([]byte, error)
}
// New creates a new instance of the PBFT state machine
func New(validator SignKey, transport Transport, opts ...ConfigOption) *Pbft {
config := DefaultConfig()
config.ApplyOps(opts...)
p := &Pbft{
validator: validator,
state: newState(),
transport: transport,
msgQueue: newMsgQueue(),
updateCh: make(chan struct{}),
config: config,
logger: config.Logger,
tracer: config.Tracer,
roundTimeout: config.RoundTimeout,
}
p.logger.Printf("[INFO] validator key: addr=%s\n", p.validator.NodeID())
return p
}
func (p *Pbft) SetBackend(backend Backend) error {
p.backend = backend
// set the next current sequence for this iteration
p.setSequence(p.backend.Height())
// set the current set of validators
p.state.validators = p.backend.ValidatorSet()
return nil
}
// start starts the PBFT consensus state machine
func (p *Pbft) Run(ctx context.Context) {
p.ctx = ctx
// the iteration always starts with the AcceptState.
// AcceptState stages will reset the rest of the message queues.
p.setState(AcceptState)
// start the trace span
spanCtx, span := p.tracer.Start(context.Background(), fmt.Sprintf("Sequence-%d", p.state.view.Sequence))
defer span.End()
// loop until we reach the a finish state
for p.getState() != DoneState && p.getState() != SyncState {
select {
case <-ctx.Done():
return
default:
}
// Start the state machine loop
p.runCycle(spanCtx)
}
}
// runCycle represents the PBFT state machine loop
func (p *Pbft) runCycle(ctx context.Context) {
// Log to the console
if p.state.view != nil {
p.logger.Printf("[DEBUG] cycle: state=%s, sequence=%d, round=%d", p.getState(), p.state.view.Sequence, p.state.view.Round)
}
// Based on the current state, execute the corresponding section
switch p.getState() {
case AcceptState:
p.runAcceptState(ctx)
case ValidateState:
p.runValidateState(ctx)
case RoundChangeState:
p.runRoundChangeState(ctx)
case CommitState:
p.runCommitState(ctx)
case DoneState:
panic("BUG: We cannot iterate on DoneState")
}
}
func (p *Pbft) setSequence(sequence uint64) {
p.state.view = &View{
Round: 0,
Sequence: sequence,
}
}
// runAcceptState runs the Accept state loop
//
// The Accept state always checks the snapshot, and the validator set. If the current node is not in the validators set,
// it moves back to the Sync state. On the other hand, if the node is a validator, it calculates the proposer.
// If it turns out that the current node is the proposer, it builds a proposal, and sends preprepare and then prepare messages.
func (p *Pbft) runAcceptState(ctx context.Context) { // start new round
_, span := p.tracer.Start(ctx, "AcceptState")
defer span.End()
p.logger.Printf("[INFO] accept state: sequence %d", p.state.view.Sequence)
if !p.state.validators.Includes(p.validator.NodeID()) {
// we are not a validator anymore, move back to sync state
p.logger.Print("[INFO] we are not a validator anymore")
p.setState(SyncState)
return
}
// reset round messages
p.state.resetRoundMsgs()
p.state.CalcProposer()
isProposer := p.state.proposer == p.validator.NodeID()
p.backend.Init(&RoundInfo{
Proposer: p.state.proposer,
IsProposer: isProposer,
Locked: p.state.locked,
})
// log the current state of this span
span.SetAttributes(
attribute.Bool("isproposer", isProposer),
attribute.Bool("locked", p.state.locked),
attribute.String("proposer", string(p.state.proposer)),
)
var err error
if isProposer {
p.logger.Printf("[INFO] we are the proposer")
if !p.state.locked {
// since the state is not locked, we need to build a new proposal
p.state.proposal, err = p.backend.BuildProposal()
if err != nil {
p.logger.Printf("[ERROR] failed to build proposal: %v", err)
p.setState(RoundChangeState)
return
}
// calculate how much time do we have to wait to gossip the proposal
delay := time.Until(p.state.proposal.Time)
select {
case <-time.After(delay):
case <-p.ctx.Done():
return
}
}
// send the preprepare message
p.sendPreprepareMsg()
// send the prepare message since we are ready to move the state
p.sendPrepareMsg()
// move to validation state for new prepare messages
p.setState(ValidateState)
return
}
p.logger.Printf("[INFO] proposer calculated: proposer=%s, sequence=%d", p.state.proposer, p.state.view.Sequence)
// we are NOT a proposer for this height/round. Then, we have to wait
// for a pre-prepare message from the proposer
timeout := p.roundTimeout(p.state.view.Round)
// We only need to wait here for one type of message, the Prepare message from the proposer.
// However, since we can receive bad Prepare messages we have to wait (or timeout) until
// we get the message from the correct proposer.
for p.getState() == AcceptState {
msg, ok := p.getNextMessage(span, timeout)
if !ok {
return
}
if msg == nil {
p.setState(RoundChangeState)
continue
}
// TODO: Validate that the fields required for Preprepare are set (Proposal and Hash)
if msg.From != p.state.proposer {
p.logger.Printf("[ERROR] msg received from wrong proposer: expected=%s, found=%s", p.state.proposer, msg.From)
continue
}
// retrieve the proposal, the backend MUST validate that the hash belongs to the proposal
proposal := &Proposal{
Data: msg.Proposal,
Hash: msg.Hash,
}
if err := p.backend.Validate(proposal); err != nil {
p.logger.Printf("[ERROR] failed to validate proposal. Error message: %v", err)
p.setState(RoundChangeState)
return
}
if p.state.locked {
// the state is locked, we need to receive the same proposal
if p.state.proposal.Equal(proposal) {
// fast-track and send a commit message and wait for validations
p.sendCommitMsg()
p.setState(ValidateState)
} else {
p.handleStateErr(errIncorrectLockedProposal)
}
} else {
p.state.proposal = proposal
p.sendPrepareMsg()
p.setState(ValidateState)
}
}
}
// runValidateState implements the Validate state loop.
//
// The Validate state is rather simple - all nodes do in this state is read messages and add them to their local snapshot state
func (p *Pbft) runValidateState(ctx context.Context) { // start new round
ctx, span := p.tracer.Start(ctx, "ValidateState")
defer span.End()
hasCommitted := false
sendCommit := func(span trace.Span) {
// at this point either we have enough prepare messages
// or commit messages so we can lock the proposal
p.state.lock()
if !hasCommitted {
// send the commit message
p.sendCommitMsg()
hasCommitted = true
span.AddEvent("Commit")
}
}
timeout := p.roundTimeout(p.state.view.Round)
for p.getState() == ValidateState {
_, span := p.tracer.Start(ctx, "ValidateState")
msg, ok := p.getNextMessage(span, timeout)
if !ok {
// closing
span.End()
return
}
if msg == nil {
// timeout
p.setState(RoundChangeState)
span.End()
return
}
// the message must have our local hash
if !bytes.Equal(msg.Hash, p.state.proposal.Hash) {
p.logger.Print(fmt.Sprintf("[WARN]: incorrect hash in %s message", msg.Type.String()))
continue
}
switch msg.Type {
case MessageReq_Prepare:
p.state.addPrepared(msg)
case MessageReq_Commit:
if err := p.backend.ValidateCommit(msg.From, msg.Seal); err != nil {
p.logger.Printf("[ERROR]: failed to validate commit: %v", err)
continue
}
p.state.addCommitted(msg)
default:
panic(fmt.Errorf("BUG: Unexpected message type: %s in %s", msg.Type, p.getState()))
}
if p.state.numPrepared() > p.state.NumValid() {
// we have received enough pre-prepare messages
sendCommit(span)
}
if p.state.numCommitted() > p.state.NumValid() {
// we have received enough commit messages
sendCommit(span)
// change to commit state just to get out of the loop
p.setState(CommitState)
}
// set the attributes of this span once it is done
p.setStateSpanAttributes(span)
span.End()
}
}
func spanAddEventMessage(typ string, span trace.Span, msg *MessageReq) {
span.AddEvent("Message", trace.WithAttributes(
// where was the message generated
attribute.String("typ", typ),
// type of message
attribute.String("msg", msg.Type.String()),
// from address of the sender
attribute.String("from", string(msg.From)),
// view sequence
attribute.Int64("sequence", int64(msg.View.Sequence)),
// round sequence
attribute.Int64("round", int64(msg.View.Round)),
))
}
func (p *Pbft) setStateSpanAttributes(span trace.Span) {
attr := []attribute.KeyValue{}
// number of committed messages
attr = append(attr, attribute.Int64("committed", int64(p.state.numCommitted())))
// number of prepared messages
attr = append(attr, attribute.Int64("prepared", int64(p.state.numPrepared())))
// number of change state messages per round
for round, msgs := range p.state.roundMessages {
attr = append(attr, attribute.Int64(fmt.Sprintf("roundchange_%d", round), int64(len(msgs))))
}
span.SetAttributes(attr...)
}
func (p *Pbft) runCommitState(ctx context.Context) {
_, span := p.tracer.Start(ctx, "CommitState")
defer span.End()
committedSeals := p.state.getCommittedSeals()
proposal := p.state.proposal.Copy()
// at this point either if it works or not we need to unlock the state
// to allow for other proposals to be produced if it insertion fails
p.state.unlock()
pp := &SealedProposal{
Proposal: proposal,
CommittedSeals: committedSeals,
Proposer: p.state.proposer,
Number: p.state.view.Sequence,
}
if err := p.backend.Insert(pp); err != nil {
// start a new round with the state unlocked since we need to
// be able to propose/validate a different proposal
p.logger.Printf("[ERROR] failed to insert proposal. Error message: %v", err)
p.handleStateErr(errFailedToInsertProposal)
} else {
// move to done state to finish the current iteration of the state machine
p.setState(DoneState)
}
}
var (
errIncorrectLockedProposal = fmt.Errorf("locked proposal is incorrect")
errVerificationFailed = fmt.Errorf("proposal verification failed")
errFailedToInsertProposal = fmt.Errorf("failed to insert proposal")
)
func (p *Pbft) handleStateErr(err error) {
p.state.err = err
p.setState(RoundChangeState)
}
func (p *Pbft) runRoundChangeState(ctx context.Context) {
ctx, span := p.tracer.Start(ctx, "RoundChange")
defer span.End()
sendRoundChange := func(round uint64) {
p.logger.Printf("[DEBUG] local round change: round=%d", round)
// set the new round
p.state.view.Round = round
// clean the round
p.state.cleanRound(round)
// send the round change message
p.sendRoundChange()
}
sendNextRoundChange := func() {
sendRoundChange(p.state.view.Round + 1)
}
checkTimeout := func() {
// At this point we might be stuck in the network if:
// - We have advanced the round but everyone else passed.
// - We are removing those messages since they are old now.
if bestHeight, stucked := p.backend.IsStuck(p.state.view.Sequence); stucked {
span.AddEvent("OutOfSync", trace.WithAttributes(
// our local height
attribute.Int64("local", int64(p.state.view.Sequence)),
// the best remote height
attribute.Int64("remote", int64(bestHeight)),
))
p.setState(SyncState)
return
}
// otherwise, it seems that we are in sync
// and we should start a new round
sendNextRoundChange()
}
// if the round was triggered due to an error, we send our own
// next round change
if err := p.state.getErr(); err != nil {
p.logger.Printf("[DEBUG] round change handle error. Error message: %v", err)
sendNextRoundChange()
} else {
// otherwise, it is due to a timeout in any stage
// First, we try to sync up with any max round already available
if maxRound, ok := p.state.maxRound(); ok {
p.logger.Printf("[DEBUG] round change, max round=%d", maxRound)
sendRoundChange(maxRound)
} else {
// otherwise, do your best to sync up
checkTimeout()
}
}
// create a timer for the round change
timeout := p.roundTimeout(p.state.view.Round)
for p.getState() == RoundChangeState {
_, span := p.tracer.Start(ctx, "RoundChangeState")
msg, ok := p.getNextMessage(span, timeout)
if !ok {
// closing
span.End()
return
}
if msg == nil {
p.logger.Print("[DEBUG] round change timeout")
checkTimeout()
// update the timeout duration
timeout = p.roundTimeout(p.state.view.Round)
span.End()
continue
}
// we only expect RoundChange messages right now
num := p.state.AddRoundMessage(msg)
if num == p.state.NumValid() {
// start a new round inmediatly
p.state.view.Round = msg.View.Round
p.setState(AcceptState)
} else if num == p.state.MaxFaultyNodes()+1 {
// weak certificate, try to catch up if our round number is smaller
if p.state.view.Round < msg.View.Round {
// update timer
timeout = p.roundTimeout(p.state.view.Round)
sendRoundChange(msg.View.Round)
}
}
p.setStateSpanAttributes(span)
span.End()
}
}
// --- communication wrappers ---
func (p *Pbft) sendRoundChange() {
p.gossip(MessageReq_RoundChange)
}
func (p *Pbft) sendPreprepareMsg() {
p.gossip(MessageReq_Preprepare)
}
func (p *Pbft) sendPrepareMsg() {
p.gossip(MessageReq_Prepare)
}
func (p *Pbft) sendCommitMsg() {
p.gossip(MessageReq_Commit)
}
func (p *Pbft) gossip(msgType MsgType) {
msg := &MessageReq{
Type: msgType,
From: p.validator.NodeID(),
}
if msgType != MessageReq_RoundChange {
// Except for round change message in which we are deciding on the proposer,
// the rest of the consensus message require the hash:
// 1. Preprepare: notify the validators of the proposal + hash
// 2. Prepare + Commit: safe check to only include messages from our round.
msg.Hash = p.state.proposal.Hash
}
// add View
msg.View = p.state.view.Copy()
// if we are sending a preprepare message we need to include the proposal
if msg.Type == MessageReq_Preprepare {
msg.SetProposal(p.state.proposal.Data)
}
// if the message is commit, we need to add the committed seal
if msg.Type == MessageReq_Commit {
// seal the hash of the proposal
seal, err := p.validator.Sign(p.state.proposal.Hash)
if err != nil {
p.logger.Printf("[ERROR] failed to commit seal. Error message: %v", err)
return
}
msg.Seal = seal
}
if msg.Type != MessageReq_Preprepare {
// send a copy to ourselves so that we can process this message as well
msg2 := msg.Copy()
msg2.From = p.validator.NodeID()
p.PushMessage(msg2)
}
if err := p.transport.Gossip(msg); err != nil {
p.logger.Printf("[ERROR] failed to gossip. Error message: %v", err)
}
}
func (p *Pbft) GetState() PbftState {
return p.getState()
}
// getState returns the current PBFT state
func (p *Pbft) getState() PbftState {
return p.state.getState()
}
// isState checks if the node is in the passed in state
func (p *Pbft) IsState(s PbftState) bool {
return p.state.getState() == s
}
func (p *Pbft) SetState(s PbftState) {
p.setState(s)
}
// setState sets the PBFT state
func (p *Pbft) setState(s PbftState) {
p.logger.Printf("[DEBUG] state change: '%s'", s)
p.state.setState(s)
}
// forceTimeout sets the forceTimeoutCh flag to true
func (p *Pbft) forceTimeout() {
p.forceTimeoutCh = true
}
// getNextMessage reads a new message from the message queue
func (p *Pbft) getNextMessage(span trace.Span, timeout time.Duration) (*MessageReq, bool) {
timeoutCh := time.After(timeout)
for {
msg, discards := p.msgQueue.readMessageWithDiscards(p.getState(), p.state.view)
// send the discard messages
for _, msg := range discards {
spanAddEventMessage("dropMessage", span, msg)
}
if msg != nil {
// add the event to the span
spanAddEventMessage("message", span, msg)
return msg, true
}
if p.forceTimeoutCh {
p.forceTimeoutCh = false
return nil, true
}
// wait until there is a new message or
// someone closes the stopCh (i.e. timeout for round change)
select {
case <-timeoutCh:
span.AddEvent("Timeout")
return nil, true
case <-p.ctx.Done():
return nil, false
case <-p.updateCh:
}
}
}
// PushMessage pushes a new message to the message queue
func (p *Pbft) PushMessage(msg *MessageReq) {
if err := msg.Validate(); err != nil {
p.logger.Printf("[ERROR]: failed to validate msg: %v", err)
return
}
p.msgQueue.pushMessage(msg)
select {
case p.updateCh <- struct{}{}:
default:
}
}
// exponentialTimeout calculates the timeout duration depending on the current round.
// Round acts as an exponent when determining timeout (2^round).
func exponentialTimeout(round uint64) time.Duration {
timeout := defaultTimeout
// limit exponent to be in range of maxTimeout (<=8) otherwise use maxTimeout
// this prevents calculating timeout that is greater than maxTimeout and
// possible overflow for calculating timeout for rounds >33 since duration is in nanoseconds stored in int64
if round <= maxTimeoutExponent {
timeout += time.Duration(1<<round) * time.Second
} else {
timeout = maxTimeout
}
return timeout
}
// Calculate max faulty nodes in order to have Byzantine-fault tollerant system.
// Formula explanation:
// N -> number of nodes in PBFT
// F -> number of faulty nodes
// N = 3 * F + 1 => F = (N - 1) / 3
// PBFT tolerates 1 failure with 4 nodes
// 4 = 3 * 1 + 1
// To tolerate 2 failures, PBFT requires 7 nodes
// 7 = 3 * 2 + 1
// It should always take the floor of the result
func MaxFaultyNodes(nodesCount int) int {
if nodesCount <= 0 {
return 0
}
return (nodesCount - 1) / 3
}
// Calculates quorum size (namely the number of required messages of some type in order to proceed to the next state in PolyBFT state machine).
// It is calculated by formula:
// 2 * F + 1, where F denotes maximum count of faulty nodes in order to have Byzantine fault tollerant property satisfied.
func QuorumSize(nodesCount int) int {
return 2*MaxFaultyNodes(nodesCount) + 1
}