-
Notifications
You must be signed in to change notification settings - Fork 0
/
consensus.go
871 lines (682 loc) · 23.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
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
package isaacstates
import (
"context"
"sync"
"time"
"github.com/ProtoconNet/mitum2/base"
"github.com/ProtoconNet/mitum2/isaac"
"github.com/ProtoconNet/mitum2/storage"
"github.com/ProtoconNet/mitum2/util"
"github.com/ProtoconNet/mitum2/util/localtime"
"github.com/ProtoconNet/mitum2/util/valuehash"
"github.com/pkg/errors"
)
type ConsensusHandlerArgs struct {
*baseBallotHandlerArgs
ProposalProcessors *isaac.ProposalProcessors
GetManifestFunc func(base.Height) (base.Manifest, error)
WhenNewBlockSaved func(base.Height)
WhenNewBlockConfirmed func(base.Height)
}
func NewConsensusHandlerArgs() *ConsensusHandlerArgs {
return &ConsensusHandlerArgs{
baseBallotHandlerArgs: newBaseBallotHandlerArgs(),
GetManifestFunc: func(base.Height) (base.Manifest, error) {
return nil, util.ErrNotImplemented.Errorf("GetManifestFunc")
},
WhenNewBlockSaved: func(base.Height) {},
WhenNewBlockConfirmed: func(base.Height) {},
}
}
type ConsensusHandler struct {
*baseBallotHandler
args *ConsensusHandlerArgs
vplock sync.Mutex
}
type NewConsensusHandlerType struct {
*ConsensusHandler
}
func NewNewConsensusHandlerType(
local base.LocalNode,
params *isaac.LocalParams,
args *ConsensusHandlerArgs,
) *NewConsensusHandlerType {
baseBallotHandler := newBaseBallotHandler(StateConsensus, local, params, args.baseBallotHandlerArgs)
return &NewConsensusHandlerType{
ConsensusHandler: &ConsensusHandler{
baseBallotHandler: baseBallotHandler,
args: args,
},
}
}
func (h *NewConsensusHandlerType) new() (handler, error) {
return &ConsensusHandler{
baseBallotHandler: h.baseBallotHandler.new(),
args: h.args,
}, nil
}
func (st *ConsensusHandler) enter(from StateType, i switchContext) (func(), error) {
e := util.StringErrorFunc("failed to enter consensus state")
deferred, err := st.baseBallotHandler.enter(from, i)
if err != nil {
return nil, e(err, "")
}
var sctx consensusSwitchContext
switch j, ok := i.(consensusSwitchContext); {
case !ok:
return nil, e(nil, "invalid stateSwitchContext, not for consensus state; %T", i)
case j.vp == nil:
return nil, e(nil, "invalid stateSwitchContext, empty init voteproof")
default:
sctx = j
}
switch suf, found, err := st.args.NodeInConsensusNodesFunc(
st.local, sctx.vp.Point().Height()); {
case errors.Is(err, storage.ErrNotFound):
st.Log().Debug().
Dict("state_context", switchContextLog(sctx)).
Interface("height", sctx.vp.Point().Height()).
Msg("suffrage not found at entering consensus state; moves to syncing state")
return nil, newSyncingSwitchContext(StateConsensus, sctx.vp.Point().Height())
case err != nil:
return nil, e(err, "")
case suf == nil || suf.Len() < 1:
return nil, e(nil, "empty suffrage of init voteproof")
case !found:
st.Log().Debug().
Dict("state_context", switchContextLog(sctx)).
Interface("height", sctx.vp.Point().Height()).
Msg("local is not in consensus nodes at entering consensus state; moves to syncing state")
return nil, newSyncingSwitchContext(StateConsensus, sctx.vp.Point().Height())
}
switch lvps, found := st.voteproofs(sctx.vp.Point()); {
case !found:
return nil, e(nil, "last voteproofs not found")
default:
st.vplock.Lock()
return func() {
deferred()
defer st.vplock.Unlock()
var nsctx switchContext
switch err := st.newVoteproofWithLVPS(sctx.vp, lvps); {
case err == nil:
case !errors.As(err, &nsctx):
st.Log().Error().Err(err).Msg("failed to process enter voteproof; moves to broken state")
go st.switchState(newBrokenSwitchContext(StateConsensus, err))
default:
go st.switchState(nsctx)
}
}, nil
}
}
func (st *ConsensusHandler) exit(sctx switchContext) (func(), error) {
e := util.StringErrorFunc("failed to exit from consensus state")
deferred, err := st.baseBallotHandler.exit(sctx)
if err != nil {
return nil, e(err, "")
}
if err := st.args.ProposalProcessors.Cancel(); err != nil {
return nil, e(err, "failed to cancel proposal processors")
}
return deferred, nil
}
func (st *ConsensusHandler) processProposalFunc(ivp base.INITVoteproof) (func(context.Context) error, error) {
facthash := ivp.BallotMajority().Proposal()
l := st.Log().With().Stringer("fact", facthash).Logger()
l.Debug().Msg("trying to process proposal")
e := util.StringErrorFunc("failed to process proposal")
var process isaac.ProcessorProcessFunc
switch i, err := st.processProposalInternal(ivp); {
case err == nil:
if i == nil {
l.Debug().Msg("empty manifest; ignore")
return nil, nil
}
process = i
case errors.Is(err, isaac.ErrNotProposalProcessorProcessed):
// NOTE instead of moving next round, intended-wrong accept ballot.
return func(context.Context) error {
dummy := isaac.NewManifest(
ivp.Point().Height(),
ivp.BallotMajority().PreviousBlock(),
valuehash.RandomSHA256(),
valuehash.RandomSHA256(),
valuehash.RandomSHA256(),
valuehash.RandomSHA256(),
localtime.Now(),
)
if err = st.prepareACCEPTBallot(ivp, dummy, time.Nanosecond); err != nil {
return errors.WithMessage(err, "failed to prepare intended wrong accept ballot")
}
return nil
},
nil
default:
err = e(err, "")
l.Error().Err(err).Msg("failed to process proposal; moves to broken state")
return nil, newBrokenSwitchContext(StateConsensus, err)
}
return func(ctx context.Context) error {
manifest, err := process(ctx)
switch {
case err != nil:
return e(err, "")
case manifest == nil:
return nil
}
eavp := st.lastVoteproofs().ACCEPT()
if err := st.prepareACCEPTBallot(ivp, manifest, time.Nanosecond); err != nil {
l.Error().Err(err).Msg("failed to prepare accept ballot")
return e(err, "")
}
if eavp == nil || !eavp.Point().Point.Equal(ivp.Point().Point) {
return nil
}
ll := l.With().Dict("accept_voteproof", base.VoteproofLog(eavp)).Logger()
var sctx switchContext
switch saved, err := st.handleACCEPTVoteproofAfterProcessingProposal(manifest, eavp); {
case saved:
ll.Debug().Msg("new block saved by accept voteproof after processing proposal")
case err == nil:
return nil
case errors.As(err, &sctx):
default:
ll.Error().Err(err).Msg("failed to save new block by accept voteproof after processing proposal")
sctx = newBrokenSwitchContext(StateConsensus, errors.Wrap(err, "failed to save proposal"))
}
return sctx
}, nil
}
func (st *ConsensusHandler) processProposal(ivp base.INITVoteproof) (func(), error) {
f, err := st.processProposalFunc(ivp)
switch {
case err != nil:
return nil, err
case f == nil:
return func() {}, nil
}
return func() {
var sctx switchContext
switch err := f(st.ctx); {
case err == nil:
case errors.As(err, &sctx):
go st.switchState(sctx)
default:
go st.switchState(newBrokenSwitchContext(StateConsensus, err))
}
}, nil
}
func (st *ConsensusHandler) processProposalInternal(ivp base.INITVoteproof) (isaac.ProcessorProcessFunc, error) {
e := util.StringErrorFunc("failed to process proposal")
facthash := ivp.BallotMajority().Proposal()
var previous base.Manifest
switch m, err := st.args.GetManifestFunc(ivp.Point().Height() - 1); {
case err != nil:
return nil, e(err, "")
default:
previous = m
}
switch process, err := st.args.ProposalProcessors.Process(st.ctx, facthash, previous, ivp); {
case err != nil:
return nil, e(err, "")
case process == nil:
return nil, nil
default:
return func(ctx context.Context) (base.Manifest, error) {
switch manifest, err := process(ctx); {
case err != nil:
st.Log().Error().Err(err).Msg("failed to process proposal")
if errors.Is(err, context.Canceled) {
return nil, err
}
if err0 := st.args.ProposalProcessors.Cancel(); err0 != nil {
return nil, e(err0, "failed to cancel proposal processors")
}
return nil, err
case manifest == nil:
st.Log().Debug().Msg("empty manifest; already processed")
return nil, nil
default:
st.Log().Debug().Msg("proposal processed")
return manifest, nil
}
}, nil
}
}
func (st *ConsensusHandler) handleACCEPTVoteproofAfterProcessingProposal(
manifest base.Manifest, avp base.ACCEPTVoteproof,
) (saved bool, _ error) {
l := st.Log().With().Dict("accept_voteproof", base.VoteproofLog(avp)).Logger()
switch { // NOTE check last accept voteproof is the execpted
case avp.Result() != base.VoteResultMajority:
if err := st.args.ProposalProcessors.Cancel(); err != nil {
l.Error().Err(err).
Msg("expected accept voteproof is not majority result; cancel processor, but failed")
return false, err
}
l.Debug().Msg("expected accept voteproof is not majority result; ignore")
return false, nil
case !manifest.Hash().Equal(avp.BallotMajority().NewBlock()):
if err := st.args.ProposalProcessors.Cancel(); err != nil {
l.Error().Err(err).
Msg("expected accept voteproof has different new block; cancel processor, but failed")
return false, err
}
l.Debug().Msg("expected accept voteproof has different new block; moves to syncing")
return false, newSyncingSwitchContext(StateConsensus, avp.Point().Height())
default:
l.Debug().Msg("proposal processed and expected voteproof found")
}
var sctx switchContext
switch i, err := st.saveBlock(avp); {
case err == nil:
saved = i
case errors.As(err, &sctx):
default:
sctx = newBrokenSwitchContext(StateConsensus, errors.Wrap(err, "failed to save proposal"))
}
return saved, sctx
}
func (st *ConsensusHandler) prepareINITBallot(
bl base.INITBallot,
ids []util.TimerID,
initialWait time.Duration,
interval func(int, time.Duration) time.Duration,
) error {
go func() {
<-time.After(initialWait)
switch _, err := st.vote(bl); {
case err == nil:
case errors.Is(err, errFailedToVoteNotInConsensus):
st.Log().Debug().Err(err).Msg("failed to vote init ballot; moves to syncing state")
go st.switchState(newSyncingSwitchContext(StateConsensus, bl.Point().Height()-1))
default:
st.Log().Debug().Err(err).Msg("failed to vote init ballot; moves to broken state")
go st.switchState(newBrokenSwitchContext(StateConsensus, err))
}
}()
if err := st.broadcastINITBallot(bl, interval); err != nil {
return err
}
return st.timers.StartTimers(ids, true)
}
func (st *ConsensusHandler) newVoteproof(vp base.Voteproof) error {
st.vplock.Lock()
defer st.vplock.Unlock()
switch lvps, v, isnew := st.baseBallotHandler.setNewVoteproof(vp); {
case v == nil, !isnew:
return nil
default:
return st.newVoteproofWithLVPS(vp, lvps)
}
}
func (st *ConsensusHandler) newVoteproofWithLVPS(vp base.Voteproof, lvps LastVoteproofs) error {
if st.resolver != nil {
st.resolver.Cancel(vp.Point())
}
e := util.StringErrorFunc("failed to handle new voteproof")
switch keep, err := st.checkStuckVoteproof(vp, lvps); {
case err != nil:
return err
case !keep:
return nil
}
switch vp.Point().Stage() {
case base.StageINIT:
return st.newINITVoteproof(vp.(base.INITVoteproof), lvps) //nolint:forcetypeassert //...
case base.StageACCEPT:
return st.newACCEPTVoteproof(vp.(base.ACCEPTVoteproof), lvps) //nolint:forcetypeassert //...
default:
return e(nil, "invalid voteproof received, %T", vp)
}
}
func (st *ConsensusHandler) newINITVoteproof(ivp base.INITVoteproof, lvps LastVoteproofs) error {
c := lvps.Cap()
l := st.Log().With().
Dict("init_voteproof", base.VoteproofLog(ivp)).
Dict("last_voteproof", base.VoteproofLog(c)).
Logger()
l.Debug().Msg("new init voteproof received")
switch c.Point().Stage() { //nolint:exhaustive //...
case base.StageINIT:
return st.newINITVoteproofWithLastINITVoteproof(ivp, lvps)
case base.StageACCEPT:
return st.newINITVoteproofWithLastACCEPTVoteproof(ivp, lvps)
}
return nil
}
func (st *ConsensusHandler) newACCEPTVoteproof(avp base.ACCEPTVoteproof, lvps LastVoteproofs) error {
lvp := lvps.Cap()
l := st.Log().With().
Dict("accept_voteproof", base.VoteproofLog(avp)).
Dict("last_voteproof", base.VoteproofLog(lvp)).
Logger()
l.Debug().Msg("new accept voteproof received")
switch lvp.Point().Stage() { //nolint:exhaustive //...
case base.StageINIT:
return st.newACCEPTVoteproofWithLastINITVoteproof(avp, lvps)
case base.StageACCEPT:
return st.newACCEPTVoteproofWithLastACCEPTVoteproof(avp, lvps)
}
return nil
}
func (st *ConsensusHandler) newINITVoteproofWithLastINITVoteproof(
ivp base.INITVoteproof, lvps LastVoteproofs,
) error {
livp := lvps.Cap().(base.INITVoteproof) //nolint:forcetypeassert //...
l := st.Log().With().
Dict("last_init_voteproof", base.VoteproofLog(livp)).
Logger()
switch {
case ivp.Point().Height() > livp.Point().Height(): // NOTE higher height; moves to syncing state
l.Debug().Msg("higher init voteproof; moves to syncing state")
return newSyncingSwitchContext(StateConsensus, ivp.Point().Height()-1)
case ivp.Result() != base.VoteResultMajority: // NOTE new init voteproof has same height, but higher round
l.Debug().Msg("new init voteproof draw; moves to next round")
go st.nextRound(ivp, lvps.PreviousBlockForNextRound(ivp))
return nil
}
lavp := lvps.ACCEPT()
ll := st.Log().With().
Dict("init_voteproof", base.VoteproofLog(ivp)).
Dict("last_accept_voteproof", base.VoteproofLog(lavp)).
Logger()
if lavp == nil {
ll.Debug().Msg("empty last accept voteproof; moves to broken state")
return newBrokenSwitchContext(StateConsensus, errors.Errorf("empty last accept voteproof"))
}
if m := lavp.BallotMajority(); m == nil || !ivp.BallotMajority().PreviousBlock().Equal(m.NewBlock()) {
// NOTE local stored block is different with other nodes
ll.Debug().
Stringer("previous_block", ivp.BallotMajority().PreviousBlock()).
Stringer("new_block", m.NewBlock()).
Msg("previous block does not match with last accept voteproof; moves to syncing")
return newSyncingSwitchContext(StateConsensus, ivp.Point().Height()-1)
}
switch keep, err := st.checkSuffrageVoting(ivp); {
case err != nil:
return err
case !keep:
return nil
default:
go st.whenNewBlockConfirmed(lavp)
process, err := st.processProposal(ivp)
if err != nil {
return err
}
go process()
return nil
}
}
func (st *ConsensusHandler) newINITVoteproofWithLastACCEPTVoteproof(
ivp base.INITVoteproof, lvps LastVoteproofs,
) error {
lavp := lvps.Cap().(base.ACCEPTVoteproof) //nolint:forcetypeassert //...
l := st.Log().With().
Dict("init_voteproof", base.VoteproofLog(ivp)).
Dict("last_accept_voteproof", base.VoteproofLog(lavp)).
Logger()
switch expectedheight := lavp.Point().Height() + 1; {
case ivp.Point().Height() > expectedheight:
l.Debug().Msg("higher init voteproof; moves to syncing state")
return newSyncingSwitchContext(StateConsensus, ivp.Point().Height()-1)
case ivp.Result() == base.VoteResultDraw:
l.Debug().Msg("new init voteproof draw; moves to next round")
go st.nextRound(ivp, lvps.PreviousBlockForNextRound(ivp))
return nil
default:
if m := lavp.BallotMajority(); m == nil || !ivp.BallotMajority().PreviousBlock().Equal(m.NewBlock()) {
// NOTE local stored block is different with other nodes
l.Debug().
Stringer("previous_block", ivp.BallotMajority().PreviousBlock()).
Interface("majority", m).
Msg("previous block does not match with last accept voteproof; moves to syncing")
return newSyncingSwitchContext(StateConsensus, ivp.Point().Height()-1)
}
}
// NOTE suffrage sign voting
switch keep, err := st.checkSuffrageVoting(ivp); {
case err != nil:
return err
case !keep:
return nil
default:
go st.whenNewBlockConfirmed(lavp)
process, err := st.processProposal(ivp)
if err != nil {
return err
}
go process()
return nil
}
}
func (st *ConsensusHandler) newACCEPTVoteproofWithLastINITVoteproof(
avp base.ACCEPTVoteproof, lvps LastVoteproofs,
) error {
livp := lvps.Cap().(base.INITVoteproof) //nolint:forcetypeassert //...
switch {
case avp.Point().Point.Equal(livp.Point().Point): // NOTE expected accept voteproof
if avp.Result() == base.VoteResultMajority {
_, err := st.saveBlock(avp)
return err
}
go st.nextRound(avp, lvps.PreviousBlockForNextRound(avp))
return nil
case avp.Point().Height() > livp.Point().Height():
case avp.Result() == base.VoteResultDraw:
go st.nextRound(avp, lvps.PreviousBlockForNextRound(avp))
return nil
}
return newSyncingSwitchContext(StateConsensus, avp.Point().Height())
}
func (st *ConsensusHandler) newACCEPTVoteproofWithLastACCEPTVoteproof(
avp base.ACCEPTVoteproof, lvps LastVoteproofs,
) error {
lavp := lvps.Cap().(base.ACCEPTVoteproof) //nolint:forcetypeassert //...
l := st.Log().With().
Dict("last_accept_voteproof", base.VoteproofLog(lavp)).
Logger()
switch {
case avp.Point().Height() > lavp.Point().Height():
l.Debug().Msg("higher accept voteproof; moves to syncing state")
return newSyncingSwitchContext(StateConsensus, avp.Point().Height())
case avp.Result() == base.VoteResultDraw:
l.Debug().Msg("new accept voteproof draw; moves to next round")
go st.nextRound(avp, lvps.PreviousBlockForNextRound(avp))
return nil
default:
return newSyncingSwitchContext(StateConsensus, avp.Point().Height())
}
}
func (st *ConsensusHandler) nextRound(vp base.Voteproof, previousBlock util.Hash) {
l := st.Log().With().Dict("voteproof", base.VoteproofLog(vp)).Logger()
started := time.Now()
if previousBlock == nil {
l.Debug().Msg("failed to find previous block from last voteproofs; ignore to move next round")
return
}
var sctx switchContext
var bl base.INITBallot
switch i, err := st.baseBallotHandler.makeNextRoundBallot(vp, previousBlock, st.args.NodeInConsensusNodesFunc); {
case err == nil:
if i == nil {
return
}
bl = i
case errors.As(err, &sctx):
go st.switchState(sctx)
return
default:
l.Debug().Err(err).Msg("failed to prepare next round; moves to broken state")
go st.switchState(newBrokenSwitchContext(StateConsensus, err))
return
}
initialWait := time.Nanosecond
if d := time.Since(started); d < st.params.WaitPreparingNextRoundINITBallot() {
initialWait = st.params.WaitPreparingNextRoundINITBallot() - d
}
if err := st.prepareINITBallot(
bl,
[]util.TimerID{
timerIDBroadcastINITBallot,
timerIDBroadcastSuffrageConfirmBallot,
timerIDBroadcastACCEPTBallot,
},
initialWait,
func(i int, _ time.Duration) time.Duration {
lvp := st.lastVoteproofs().Cap()
if bl.Point().Compare(lvp.Point()) < 0 &&
bl.Point().Height() == lvp.Point().Height() &&
lvp.Point().Stage() == base.StageINIT &&
lvp.Result() == base.VoteResultMajority {
return 0
}
if i < 1 {
return initialWait
}
return st.params.IntervalBroadcastBallot()
},
); err != nil {
l.Error().Err(err).Msg("failed to prepare init ballot for next round")
return
}
l.Debug().Interface("ballot", bl).Msg("init ballot broadcasted for next round")
}
func (st *ConsensusHandler) prepareNextBlock(avp base.ACCEPTVoteproof) {
point := avp.Point().Point.NextHeight()
l := st.Log().With().Dict("voteproof", base.VoteproofLog(avp)).Object("point", point).Logger()
started := time.Now()
var sctx switchContext
var bl base.INITBallot
switch i, err := st.baseBallotHandler.makeNextBlockBallot(avp, st.args.NodeInConsensusNodesFunc); {
case err == nil:
if i == nil {
return
}
bl = i
case errors.As(err, &sctx):
go st.switchState(sctx)
return
default:
l.Debug().Err(err).Msg("failed to prepare next block; moves to broken state")
go st.switchState(newBrokenSwitchContext(StateConsensus, err))
return
}
initialWait := time.Nanosecond
if d := time.Since(started); d < st.params.WaitPreparingINITBallot() {
initialWait = st.params.WaitPreparingINITBallot() - d
}
if err := st.prepareINITBallot(
bl,
[]util.TimerID{
timerIDBroadcastINITBallot,
timerIDBroadcastSuffrageConfirmBallot,
timerIDBroadcastACCEPTBallot,
},
initialWait,
func(i int, _ time.Duration) time.Duration {
if i < 1 {
return initialWait
}
return st.params.IntervalBroadcastBallot()
},
); err != nil {
l.Error().Err(err).Msg("failed to prepare init ballot for next block")
return
}
l.Debug().Interface("ballot", bl).Msg("next init ballot broadcasted")
}
func (st *ConsensusHandler) saveBlock(avp base.ACCEPTVoteproof) (bool, error) {
facthash := avp.BallotMajority().Proposal()
l := st.Log().With().Dict("voteproof", base.VoteproofLog(avp)).Logger()
ll := l.With().Stringer("fact", facthash).Logger()
ll.Debug().Msg("expected accept voteproof; trying to save proposal")
switch err := st.args.ProposalProcessors.Save(context.Background(), facthash, avp); {
case err == nil:
ll.Debug().Msg("processed proposal saved; moves to next block")
go st.whenNewBlockSaved(avp)
go st.prepareNextBlock(avp)
return true, nil
case errors.Is(err, isaac.ErrProcessorAlreadySaved):
l.Debug().Msg("already saved")
return false, nil
case errors.Is(err, isaac.ErrNotProposalProcessorProcessed):
l.Debug().Msg("no processed proposal; moves to syncing state")
return false, newSyncingSwitchContext(StateConsensus, avp.Point().Height())
default:
ll.Error().Err(err).Msg("failed to save proposal; moves to broken state")
return false, newBrokenSwitchContext(StateConsensus, err)
}
}
func (st *ConsensusHandler) checkSuffrageVoting(ivp base.INITVoteproof) (bool, error) {
if _, ok := ivp.(base.WithdrawVoteproof); !ok {
return true, nil
}
switch t := ivp.Majority().(type) {
case isaac.INITBallotFact:
go st.prepareSuffrageConfirmBallot(ivp)
return false, nil
case isaac.SuffrageConfirmBallotFact:
return true, nil
default:
return false, errors.Errorf("expected SuffrageConfirmBallotFact, but %T", t)
}
}
func (st *ConsensusHandler) checkStuckVoteproof(
vp base.Voteproof,
lvps LastVoteproofs,
) (bool, error) {
if _, ok := vp.(base.StuckVoteproof); !ok {
return true, nil
}
lvp := lvps.Cap()
expectedheight := lvp.Point().Height()
l := st.Log().With().
Dict("init_voteproof", base.VoteproofLog(vp)).
Dict("last_voteproof", base.VoteproofLog(lvp)).
Logger()
if lvp.Point().Stage() == base.StageACCEPT {
expectedheight++
}
switch {
case vp.Point().Height() > expectedheight:
l.Debug().Msg("higher init stuck voteproof; moves to syncing state")
return false, newSyncingSwitchContext(StateConsensus, vp.Point().Height()-1)
default:
l.Debug().Msg("stuck voteproof; moves to next round")
go st.nextRound(vp, lvps.PreviousBlockForNextRound(vp))
return false, nil
}
}
func (st *ConsensusHandler) whenNewBlockSaved(vp base.ACCEPTVoteproof) {
if _, hasWithdraws := vp.(base.HasWithdraws); !hasWithdraws {
st.args.WhenNewBlockConfirmed(vp.Point().Height())
}
st.args.WhenNewBlockSaved(vp.Point().Height())
}
func (st *ConsensusHandler) whenNewBlockConfirmed(vp base.ACCEPTVoteproof) {
if _, ok := vp.(base.HasWithdraws); ok {
st.args.WhenNewBlockConfirmed(vp.Point().Height())
}
}
type consensusSwitchContext struct {
vp base.Voteproof
baseSwitchContext
}
func newConsensusSwitchContext(from StateType, vp base.Voteproof) (consensusSwitchContext, error) {
switch {
case vp == nil:
return consensusSwitchContext{}, errors.Errorf(
"invalid voteproof for consensus switch context; empty init voteproof")
case vp.Point().Stage() == base.StageINIT:
case vp.Point().Stage() == base.StageACCEPT:
if vp.Result() != base.VoteResultDraw {
return consensusSwitchContext{}, errors.Errorf(
"invalid voteproof for consensus switch context; vote result is not draw, %T", vp.Result())
}
}
return consensusSwitchContext{
baseSwitchContext: newBaseSwitchContext(StateConsensus, switchContextOKFuncCheckFrom(from)),
vp: vp,
}, nil
}