Source file state_transitions.ml
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
open Protocol
open Alpha_context
open Baking_state
open Baking_actions
module Events = Baking_events.State_transitions
let do_nothing state = Lwt.return (state, Do_nothing)
type proposal_acceptance = Invalid | Outdated_proposal | Valid_proposal
let is_acceptable_proposal_for_current_level state
(proposal : Baking_state.proposal) =
let open Lwt_syntax in
let current_round = state.round_state.current_round in
if Round.(current_round < proposal.block.round) then
let* () =
Events.(
emit unexpected_proposal_round (current_round, proposal.block.round))
in
return Invalid
else if Round.(current_round > proposal.block.round) then
return Outdated_proposal
else
let previous_proposal = state.level_state.latest_proposal in
if
Round.(proposal.block.round = previous_proposal.block.round)
&& Block_hash.(proposal.block.hash <> previous_proposal.block.hash)
&& Block_hash.(
proposal.predecessor.hash = previous_proposal.predecessor.hash)
then
let* () =
Events.(
emit
proposal_for_round_already_seen
(proposal.block.hash, current_round, previous_proposal.block.hash))
in
return Invalid
else
return Valid_proposal
let make_consensus_vote_batch state proposal kind =
let level =
Raw_level.of_int32 state.level_state.current_level |> function
| Ok l -> l
| _ -> assert false
in
let round = proposal.block.round in
let block_payload_hash = proposal.block.payload_hash in
let batch_content = {level; round; block_payload_hash} in
let delegates_and_slots =
List.map
(fun delegate_slot ->
(delegate_slot.consensus_key_and_delegate, delegate_slot.first_slot))
(Delegate_slots.own_delegates state.level_state.delegate_slots)
in
let batch_branch = state.level_state.latest_proposal.predecessor.hash in
Baking_state.make_unsigned_consensus_vote_batch
kind
batch_content
~batch_branch
delegates_and_slots
let prepare_preattest_action state proposal =
let preattestations : unsigned_consensus_vote_batch =
make_consensus_vote_batch state proposal Baking_state.Preattestation
in
Prepare_preattestations {preattestations}
let prepare_consensus_votes_action state proposal =
let preattestations : unsigned_consensus_vote_batch =
make_consensus_vote_batch state proposal Baking_state.Preattestation
in
let attestations : unsigned_consensus_vote_batch =
make_consensus_vote_batch state proposal Baking_state.Attestation
in
Prepare_consensus_votes {preattestations; attestations}
let update_proposal ~is_proposal_applied state proposal =
let open Lwt_syntax in
let* () = Events.(emit updating_latest_proposal proposal.block.hash) in
let prev_proposal = state.level_state.latest_proposal in
let is_latest_proposal_applied =
is_proposal_applied
|| prev_proposal.block.hash = proposal.block.hash
&& state.level_state.is_latest_proposal_applied
in
let new_level_state =
{
state.level_state with
is_latest_proposal_applied;
latest_proposal = proposal;
}
in
return {state with level_state = new_level_state}
let may_update_proposal ~is_proposal_applied state (proposal : proposal) =
assert (
Compare.Int32.(
state.level_state.latest_proposal.block.shell.level
= proposal.block.shell.level)) ;
if
Round.(state.level_state.latest_proposal.block.round < proposal.block.round)
then update_proposal ~is_proposal_applied state proposal
else Lwt.return state
let preattest state proposal =
let open Lwt_syntax in
if Baking_state.is_first_block_in_protocol proposal then
let new_state = update_current_phase state Idle in
return (new_state, Do_nothing)
else
let* () = Events.(emit attempting_preattest_proposal proposal.block.hash) in
let new_state =
update_current_phase state Awaiting_preattestations
in
return (new_state, prepare_preattest_action state proposal)
let prepare_consensus_votes state proposal =
let open Lwt_syntax in
if Baking_state.is_first_block_in_protocol proposal then
let new_state = update_current_phase state Idle in
return (new_state, Do_nothing)
else
let* () = Events.(emit attempting_vote_proposal proposal.block.hash) in
let new_state =
update_current_phase state Awaiting_preattestations
in
return (new_state, prepare_consensus_votes_action state proposal)
let state (new_proposal : proposal) =
match new_proposal.block.prequorum with
| None -> None
| Some pqc ->
let add_voting_power acc (op : Kind.preattestation Operation.t) =
let open Protocol.Alpha_context.Operation in
let {
shell = _;
protocol_data = {contents = Single (Preattestation {slot; _}); _};
_;
} =
op
in
match
Delegate_slots.voting_power state.level_state.delegate_slots ~slot
with
| None ->
acc
| Some attesting_power -> acc + attesting_power
in
let voting_power =
List.fold_left add_voting_power 0 pqc.preattestations
in
let consensus_threshold =
state.global_state.constants.parametric.consensus_threshold
in
if Compare.Int.(voting_power >= consensus_threshold) then
Some (pqc.preattestations, pqc.round)
else None
let may_update_attestable_payload_with_internal_pqc state
(new_proposal : proposal) =
match
(new_proposal.block.prequorum, state.level_state.attestable_payload)
with
| None, _ ->
state
| Some {round = new_round; _}, Some {prequorum = {round = old_round; _}; _}
when Round.(new_round < old_round) ->
state
| Some better_prequorum, _ ->
assert (
Block_payload_hash.(
better_prequorum.block_payload_hash = new_proposal.block.payload_hash)) ;
assert (
Compare.Int32.(better_prequorum.level = new_proposal.block.shell.level)) ;
let new_attestable_payload =
Some {proposal = new_proposal; prequorum = better_prequorum}
in
let new_level_state =
{state.level_state with attestable_payload = new_attestable_payload}
in
{state with level_state = new_level_state}
let has_already_been_handled state new_proposal =
let current_proposal = state.level_state.latest_proposal in
Block_hash.(current_proposal.block.hash = new_proposal.block.hash)
&& state.level_state.is_latest_proposal_applied
let mark_awaiting_pqc state =
let new_round_state =
{state.round_state with awaiting_unlocking_pqc = true}
in
let new_state = {state with round_state = new_round_state} in
new_state
let rec handle_proposal ~is_proposal_applied state (new_proposal : proposal) =
let open Lwt_syntax in
let may_vote state proposal =
match state.round_state.current_phase with
| Idle ->
state.global_state.forge_worker_hooks.cancel_all_pending_tasks () ;
prepare_consensus_votes state proposal
| _ -> do_nothing state
in
let current_level = state.level_state.current_level in
let new_proposal_level = new_proposal.block.shell.level in
let current_proposal = state.level_state.latest_proposal in
if
is_proposal_applied
&& Block_hash.(current_proposal.block.hash = new_proposal.block.hash)
then
let new_level_state =
{state.level_state with is_latest_proposal_applied = true}
in
let new_state = {state with level_state = new_level_state} in
do_nothing new_state
else if Compare.Int32.(current_level > new_proposal_level) then
let* () =
Events.(emit baker_is_ahead_of_node (current_level, new_proposal_level))
in
do_nothing state
else if Compare.Int32.(current_level = new_proposal_level) then
if
Block_hash.(
current_proposal.predecessor.hash <> new_proposal.predecessor.hash)
then
let* () =
Events.(
emit
new_proposal_is_on_another_branch
(current_proposal.predecessor.hash, new_proposal.predecessor.hash))
in
may_switch_branch ~is_proposal_applied state new_proposal
else
let* proposal_acceptance =
is_acceptable_proposal_for_current_level state new_proposal
in
match proposal_acceptance with
| Invalid ->
let* () = Events.(emit skipping_invalid_proposal ()) in
do_nothing state
| Outdated_proposal ->
let state =
may_update_attestable_payload_with_internal_pqc state new_proposal
in
let* () = Events.(emit outdated_proposal new_proposal.block.hash) in
let* state =
may_update_proposal ~is_proposal_applied state new_proposal
in
do_nothing state
| Valid_proposal -> (
let new_state =
may_update_attestable_payload_with_internal_pqc state new_proposal
in
let* new_state =
may_update_proposal ~is_proposal_applied new_state new_proposal
in
let new_round_state =
{new_state.round_state with early_attestations = []}
in
let new_state = {new_state with round_state = new_round_state} in
match new_state.level_state.locked_round with
| Some locked_round -> (
if
Block_payload_hash.(
locked_round.payload_hash = new_proposal.block.payload_hash)
then
may_vote new_state new_proposal
else
match new_proposal.block.prequorum with
| Some {round; _} when Round.(locked_round.round < round) ->
may_vote new_state new_proposal
| _ ->
let new_state = mark_awaiting_pqc new_state in
let new_state =
update_current_phase new_state Awaiting_preattestations
in
return (new_state, Watch_prequorum))
| None ->
may_vote new_state new_proposal)
else
let* () = Events.(emit new_head_with_increasing_level ()) in
let new_level = new_proposal.block.shell.level in
let compute_new_state ~current_round ~delegate_slots
~next_level_delegate_slots =
let round_state =
{
current_round;
current_phase = Idle;
delayed_quorum = None;
early_attestations = [];
awaiting_unlocking_pqc = false;
}
in
let level_state =
{
current_level = new_level;
latest_proposal = new_proposal;
is_latest_proposal_applied = is_proposal_applied;
locked_round = None;
attestable_payload = None;
elected_block = None;
delegate_slots;
next_level_delegate_slots;
next_level_proposed_round = None;
}
in
handle_proposal
~is_proposal_applied
{state with level_state; round_state}
new_proposal
in
let action =
Update_to_level {new_level_proposal = new_proposal; compute_new_state}
in
return (state, action)
and may_switch_branch ~is_proposal_applied state new_proposal =
let open Lwt_syntax in
let switch_branch state =
let* () = Events.(emit switching_branch ()) in
let round_update =
{
Baking_actions.new_round_proposal = new_proposal;
handle_proposal =
(fun state -> handle_proposal ~is_proposal_applied state new_proposal);
}
in
let* new_state = update_proposal ~is_proposal_applied state new_proposal in
return (new_state, Synchronize_round round_update)
in
let current_attestable_payload = state.level_state.attestable_payload in
match (current_attestable_payload, new_proposal.block.prequorum) with
| None, Some _ | None, None ->
let* () = Events.(emit branch_proposal_has_better_fitness ()) in
switch_branch state
| Some _, None ->
let* () = Events.(emit branch_proposal_has_no_prequorum ()) in
do_nothing state
| Some {prequorum = current_pqc; _}, Some new_pqc ->
if Round.(current_pqc.round > new_pqc.round) then
let* () = Events.(emit branch_proposal_has_lower_prequorum ()) in
do_nothing state
else if Round.(current_pqc.round < new_pqc.round) then
let* () = Events.(emit branch_proposal_has_better_prequorum ()) in
switch_branch state
else
let* () = Events.(emit branch_proposal_has_same_prequorum ()) in
do_nothing state
let prepare_block_to_bake ~attestations ?last_proposal
~(predecessor : block_info) state delegate round =
let open Lwt_syntax in
let operation_pool =
let current_mempool =
let pool =
Operation_worker.get_current_operations
state.global_state.operation_worker
in
match last_proposal with
| Some proposal ->
let {
Operation_pool.votes_payload;
anonymous_payload;
managers_payload;
} =
proposal.payload
in
List.fold_left
Operation_pool.add_operations
pool
[votes_payload; anonymous_payload; managers_payload]
| None -> pool
in
let relevant_consensus_operations =
let attestation_filter =
{
Operation_pool.level = predecessor.shell.level;
round = predecessor.round;
payload_hash = predecessor.payload_hash;
}
in
Operation_pool.filter_with_relevant_consensus_ops
~attestation_filter
~preattestation_filter:None
current_mempool.consensus
in
let filtered_mempool =
{current_mempool with consensus = relevant_consensus_operations}
in
Operation_pool.add_operations
filtered_mempool
(List.map Operation.pack attestations)
in
let kind = Fresh operation_pool in
let* () = Events.(emit preparing_fresh_block (delegate, round)) in
let force_apply =
state.global_state.config.force_apply || Round.(round <> zero)
in
let block_to_bake : block_to_bake =
{predecessor; round; delegate; kind; force_apply}
in
return (Prepare_block {block_to_bake})
(** Create an inject action that will inject either a fresh block or the pre-emptively
forged block if it exists. *)
let propose_fresh_block_action ~attestations ?last_proposal
~(predecessor : block_info) state delegate round =
prepare_block_to_bake
~attestations
?last_proposal
~predecessor
state
delegate
round
let propose_block_action state delegate round ~last_proposal =
let open Lwt_syntax in
match state.level_state.attestable_payload with
| None ->
let* () = Events.(emit no_attestable_payload_fresh_block ()) in
assert (state.level_state.locked_round = None) ;
let attestations_in_last_proposal = last_proposal.block.quorum in
propose_fresh_block_action
~attestations:attestations_in_last_proposal
state
~last_proposal:last_proposal.block
~predecessor:last_proposal.predecessor
delegate
round
| Some {proposal; prequorum} ->
let* () = Events.(emit repropose_block proposal.block.payload_hash) in
let consensus_operations =
let mempool_consensus_operations =
(Operation_worker.get_current_operations
state.global_state.operation_worker)
.consensus
in
let all_consensus_operations =
List.fold_left
(fun set op -> Operation_pool.Operation_set.add op set)
mempool_consensus_operations
(List.map Operation.pack proposal.block.quorum
@ List.map Operation.pack prequorum.preattestations)
in
let attestation_filter =
{
Operation_pool.level = proposal.predecessor.shell.level;
round = proposal.predecessor.round;
payload_hash = proposal.predecessor.payload_hash;
}
in
let preattestation_filter =
Some
{
Operation_pool.level = prequorum.level;
round = prequorum.round;
payload_hash = prequorum.block_payload_hash;
}
in
Operation_pool.(
filter_with_relevant_consensus_ops
~attestation_filter
~preattestation_filter
all_consensus_operations
|> Operation_set.elements)
in
let payload_hash = proposal.block.payload_hash in
let payload_round = proposal.block.payload_round in
let payload = proposal.block.payload in
let kind =
Reproposal {consensus_operations; payload_hash; payload_round; payload}
in
let force_apply =
true
in
let block_to_bake =
{predecessor = proposal.predecessor; round; delegate; kind; force_apply}
in
return (Prepare_block {block_to_bake})
let end_of_round state current_round =
let open Lwt_syntax in
let new_round = Round.succ current_round in
let new_round_state =
{
current_round = new_round;
current_phase = Idle;
delayed_quorum = None;
early_attestations = [];
awaiting_unlocking_pqc = false;
}
in
let new_state = {state with round_state = new_round_state} in
match
round_proposer new_state ~level:`Current new_state.round_state.current_round
with
| None ->
let* () =
Events.(
emit
no_proposal_slot
(current_round, state.level_state.current_level, new_round))
in
let new_state = update_current_phase new_state Idle in
do_nothing new_state
| Some {consensus_key_and_delegate; _} ->
let latest_proposal = state.level_state.latest_proposal in
if Baking_state.is_first_block_in_protocol latest_proposal then
do_nothing new_state
else
let* () =
Events.(
emit
proposal_slot
( current_round,
state.level_state.current_level,
new_round,
consensus_key_and_delegate ))
in
let* action =
propose_block_action
new_state
consensus_key_and_delegate
new_round
~last_proposal:state.level_state.latest_proposal
in
return (new_state, action)
let time_to_prepare_next_level_block state at_round =
let open Lwt_syntax in
let round_proposer_opt = round_proposer state ~level:`Next at_round in
match (state.level_state.elected_block, round_proposer_opt) with
| None, _ | _, None ->
assert false
| Some elected_block, Some {consensus_key_and_delegate; _} ->
let attestations = elected_block.attestation_qc in
let new_level_state =
{state.level_state with next_level_proposed_round = Some at_round}
in
let new_state = {state with level_state = new_level_state} in
let* action =
propose_fresh_block_action
~attestations
~predecessor:elected_block.proposal.block
new_state
consensus_key_and_delegate
at_round
in
return (new_state, action)
let update_locked_round state round payload_hash =
let locked_round = Some {payload_hash; round} in
let new_level_state = {state.level_state with locked_round} in
{state with level_state = new_level_state}
let prepare_attest_action state proposal =
let attestations : unsigned_consensus_vote_batch =
make_consensus_vote_batch state proposal Baking_state.Attestation
in
Prepare_attestations {attestations}
let inject_early_arrived_attestations state =
let early_attestations = state.round_state.early_attestations in
match early_attestations with
| [] -> Lwt.return (state, Watch_quorum)
| first_signed_attestation :: _ as unbatched_signed_attestations -> (
let new_round_state = {state.round_state with early_attestations = []} in
let new_state = {state with round_state = new_round_state} in
let batch_branch = state.level_state.latest_proposal.predecessor.hash in
let batch_content =
let vote_consensus_content =
first_signed_attestation.unsigned_consensus_vote
.vote_consensus_content
in
{
level = vote_consensus_content.level;
round = vote_consensus_content.round;
block_payload_hash = vote_consensus_content.block_payload_hash;
}
in
make_signed_consensus_vote_batch
Attestation
batch_content
~batch_branch
unbatched_signed_attestations
|> function
| Ok signed_attestations ->
Lwt.return (new_state, Inject_attestations {signed_attestations})
| Error _err -> do_nothing new_state)
let prequorum_reached_when_awaiting_preattestations state candidate
preattestations =
let open Lwt_syntax in
let latest_proposal = state.level_state.latest_proposal in
if Block_hash.(candidate.Operation_worker.hash <> latest_proposal.block.hash)
then
let* () =
Events.(
emit
unexpected_prequorum_received
(candidate.hash, latest_proposal.block.hash))
in
do_nothing state
else
let prequorum =
{
level = latest_proposal.block.shell.level;
round = latest_proposal.block.round;
block_payload_hash = latest_proposal.block.payload_hash;
preattestations
;
}
in
let new_attestable_payload = {proposal = latest_proposal; prequorum} in
let new_level_state =
let level_state_with_new_payload =
{
state.level_state with
attestable_payload = Some new_attestable_payload;
}
in
match state.level_state.attestable_payload with
| None -> level_state_with_new_payload
| Some attestable_payload ->
if
Round.(
attestable_payload.prequorum.round
< new_attestable_payload.prequorum.round)
then level_state_with_new_payload
else state.level_state
in
let new_state = {state with level_state = new_level_state} in
let new_state =
update_locked_round
new_state
latest_proposal.block.round
latest_proposal.block.payload_hash
in
let new_state = update_current_phase new_state Awaiting_attestations in
if new_state.round_state.awaiting_unlocking_pqc then
return (new_state, prepare_attest_action new_state latest_proposal)
else
inject_early_arrived_attestations new_state
let quorum_reached_when_waiting_attestations state candidate attestation_qc =
let open Lwt_syntax in
let latest_proposal = state.level_state.latest_proposal in
let is_latest_proposal_applied =
state.level_state.is_latest_proposal_applied
in
if Block_hash.(candidate.Operation_worker.hash <> latest_proposal.block.hash)
then
let* () =
Events.(
emit
unexpected_quorum_received
(candidate.hash, latest_proposal.block.hash))
in
do_nothing state
else
let new_round_state, new_level_state =
match state.level_state.elected_block with
| None when is_latest_proposal_applied ->
let elected_block =
Some {proposal = latest_proposal; attestation_qc}
in
let new_level_state = {state.level_state with elected_block} in
let new_round_state = {state.round_state with current_phase = Idle} in
(new_round_state, new_level_state)
| None ->
let new_round_state =
{
state.round_state with
current_phase = Awaiting_application;
delayed_quorum = Some attestation_qc;
}
in
(new_round_state, state.level_state)
| Some _ ->
(state.round_state, state.level_state)
in
let new_state =
{state with round_state = new_round_state; level_state = new_level_state}
in
do_nothing new_state
let handle_expected_applied_proposal (state : Baking_state.t) =
let new_level_state =
{state.level_state with is_latest_proposal_applied = true}
in
let new_state = {state with level_state = new_level_state} in
let new_state =
match new_state.level_state.elected_block with
| None ->
let latest_proposal = state.level_state.latest_proposal in
let attestation_qc =
match state.round_state.delayed_quorum with
| None -> assert false
| Some attestation_qc -> attestation_qc
in
let elected_block = Some {proposal = latest_proposal; attestation_qc} in
let new_level_state = {state.level_state with elected_block} in
{state with level_state = new_level_state}
| Some _ ->
new_state
in
let new_state = update_current_phase new_state Idle in
do_nothing new_state
let handle_arriving_attestation state signed_attestation =
let open Lwt_syntax in
let {
vote_consensus_content =
{
level;
round = att_round;
block_payload_hash = att_payload_hash;
slot = _;
};
delegate;
_;
} =
signed_attestation.unsigned_consensus_vote
in
let att_level = Raw_level.to_int32 level in
let attestation_matches_proposal =
let {payload_hash; round = block_round; shell; _} =
state.level_state.latest_proposal.block
in
Block_payload_hash.(att_payload_hash = payload_hash)
&& Round.(att_round = block_round)
&& Compare.Int32.(shell.level = att_level)
in
if not attestation_matches_proposal then
let* () =
Events.(emit discarding_attestation (delegate, att_level, att_round))
in
do_nothing state
else
match state.round_state.current_phase with
| Awaiting_preattestations ->
let new_round_state =
{
state.round_state with
early_attestations =
signed_attestation :: state.round_state.early_attestations;
}
in
let new_state = {state with round_state = new_round_state} in
do_nothing new_state
| Idle | Awaiting_attestations | Awaiting_application ->
let signed_attestations =
make_singleton_consensus_vote_batch signed_attestation
in
Lwt.return (state, Inject_attestations {signed_attestations})
let handle_forge_event state forge_event =
match forge_event with
| Block_ready prepared_block ->
Lwt.return
( state,
Inject_block
{prepared_block; force_injection = false; asynchronous = true} )
| Preattestation_ready signed_preattestation ->
Lwt.return (state, Inject_preattestation {signed_preattestation})
| Attestation_ready signed_attestation ->
handle_arriving_attestation state signed_attestation
let step (state : Baking_state.t) (event : Baking_state.event) :
(Baking_state.t * Baking_actions.t) Lwt.t =
let open Lwt_syntax in
let phase = state.round_state.current_phase in
let* () = Events.(emit step_current_phase (phase, event)) in
match (phase, event) with
| _, Timeout (End_of_round {ending_round}) ->
end_of_round state ending_round
| _, Timeout (Time_to_prepare_next_level_block {at_round}) ->
time_to_prepare_next_level_block state at_round
| Idle, New_head_proposal proposal ->
let* () =
Events.(
emit
new_head
( proposal.block.hash,
proposal.block.shell.level,
proposal.block.round ))
in
handle_proposal ~is_proposal_applied:true state proposal
| Awaiting_application, New_head_proposal proposal ->
if
Block_hash.(
state.level_state.latest_proposal.block.hash <> proposal.block.hash)
then
let* () =
Events.(
emit
new_head
( proposal.block.hash,
proposal.block.shell.level,
proposal.block.round ))
in
let* () =
Events.(emit unexpected_new_head_while_waiting_for_application ())
in
handle_proposal ~is_proposal_applied:true state proposal
else
let* () =
Events.(emit applied_expected_proposal_received proposal.block.hash)
in
handle_expected_applied_proposal state
| Awaiting_attestations, New_head_proposal proposal
| Awaiting_preattestations, New_head_proposal proposal ->
let* () =
Events.(
emit
new_head
( proposal.block.hash,
proposal.block.shell.level,
proposal.block.round ))
in
let* () = Events.(emit new_head_while_waiting_for_qc ()) in
handle_proposal ~is_proposal_applied:true state proposal
| Idle, New_valid_proposal proposal ->
let* () =
Events.(
emit
new_valid_proposal
( proposal.block.hash,
proposal.block.shell.level,
proposal.block.round ))
in
handle_proposal ~is_proposal_applied:false state proposal
| _, New_forge_event (forge_event : forge_event) ->
let* () = Events.(emit new_forge_event forge_event) in
handle_forge_event state forge_event
| Awaiting_application, New_valid_proposal proposal
| Awaiting_attestations, New_valid_proposal proposal
| Awaiting_preattestations, New_valid_proposal proposal ->
let* () =
Events.(
emit
new_valid_proposal
( proposal.block.hash,
proposal.block.shell.level,
proposal.block.round ))
in
if has_already_been_handled state proposal then
let* () = Events.(emit valid_proposal_received_after_application ()) in
do_nothing state
else
let* () = Events.(emit new_valid_proposal_while_waiting_for_qc ()) in
handle_proposal ~is_proposal_applied:false state proposal
| Awaiting_preattestations, Prequorum_reached (candidate, preattestation_qc)
->
prequorum_reached_when_awaiting_preattestations
state
candidate
preattestation_qc
| Awaiting_attestations, Quorum_reached (candidate, attestation_qc) ->
quorum_reached_when_waiting_attestations state candidate attestation_qc
| Idle, (Prequorum_reached _ | Quorum_reached _)
| Awaiting_preattestations, Quorum_reached _
| (Awaiting_application | Awaiting_attestations), Prequorum_reached _
| Awaiting_application, Quorum_reached _ ->
do_nothing state