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
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 current_round = state.round_state.current_round in
if Round.(current_round < proposal.block.round) then
Events.(
emit unexpected_proposal_round (current_round, proposal.block.round))
>>= fun () -> Lwt.return Invalid
else if Round.(current_round > proposal.block.round) then
Lwt.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
Events.(
emit
proposal_for_round_already_seen
(proposal.block.hash, current_round, previous_proposal.block.hash))
>>= fun () -> Lwt.return Invalid
else
Lwt.return Valid_proposal
let make_consensus_list state proposal =
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
SlotMap.fold
(fun _slot (consensus_key_and_delegate, slots) acc ->
( consensus_key_and_delegate,
{slot = slots.first_slot; level; round; block_payload_hash} )
:: acc)
state.level_state.delegate_slots.own_delegate_slots
[]
|> List.sort_uniq compare
let make_preendorse_action state proposal =
let preendorsements : (consensus_key_and_delegate * consensus_content) list =
make_consensus_list state proposal
in
Inject_preendorsements {preendorsements}
let update_proposal ~is_proposal_applied state proposal =
Events.(emit updating_latest_proposal proposal.block.hash) >>= fun () ->
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
Lwt.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 preendorse state proposal =
if Baking_state.is_first_block_in_protocol proposal then
let new_state = update_current_phase state Idle in
Lwt.return (new_state, Do_nothing)
else
Events.(emit attempting_preendorse_proposal proposal.block.hash)
>>= fun () ->
let new_state =
update_current_phase state Awaiting_preendorsements
in
Lwt.return (new_state, make_preendorse_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.preendorsement Operation.t) =
let open Protocol.Alpha_context.Operation in
let {
shell = _;
protocol_data = {contents = Single (Preendorsement {slot; _}); _};
_;
} =
op
in
match
SlotMap.find slot state.level_state.delegate_slots.all_delegate_slots
with
| None ->
acc
| Some {endorsing_power; _} -> acc + endorsing_power
in
let voting_power =
List.fold_left add_voting_power 0 pqc.preendorsements
in
let consensus_threshold =
state.global_state.constants.parametric.consensus_threshold
in
if Compare.Int.(voting_power >= consensus_threshold) then
Some (pqc.preendorsements, pqc.round)
else None
let may_update_endorsable_payload_with_internal_pqc state
(new_proposal : proposal) =
match
(new_proposal.block.prequorum, state.level_state.endorsable_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_endorsable_payload =
Some {proposal = new_proposal; prequorum = better_prequorum}
in
let new_level_state =
{state.level_state with endorsable_payload = new_endorsable_payload}
in
{state with level_state = new_level_state}
let may_update_is_latest_proposal_applied ~is_proposal_applied state
new_proposal =
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
new_state
else 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 rec handle_proposal ~is_proposal_applied state (new_proposal : proposal) =
let may_preendorse state proposal =
match state.round_state.current_phase with
| Idle -> preendorse 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
let state =
may_update_is_latest_proposal_applied
~is_proposal_applied
state
new_proposal
in
if Compare.Int32.(current_level > new_proposal_level) then
Events.(emit baker_is_ahead_of_node (current_level, new_proposal_level))
>>= fun () -> 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
Events.(
emit
new_proposal_is_on_another_branch
(current_proposal.predecessor.hash, new_proposal.predecessor.hash))
>>= fun () -> may_switch_branch ~is_proposal_applied state new_proposal
else
is_acceptable_proposal_for_current_level state new_proposal >>= function
| Invalid ->
Events.(emit skipping_invalid_proposal ()) >>= fun () ->
do_nothing state
| Outdated_proposal ->
let state =
may_update_endorsable_payload_with_internal_pqc state new_proposal
in
Events.(emit outdated_proposal new_proposal.block.hash) >>= fun () ->
may_update_proposal ~is_proposal_applied state new_proposal
>>= fun state -> do_nothing state
| Valid_proposal -> (
let new_state =
may_update_endorsable_payload_with_internal_pqc state new_proposal
in
may_update_proposal ~is_proposal_applied new_state new_proposal
>>= fun new_state ->
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_preendorse new_state new_proposal
else
match new_proposal.block.prequorum with
| Some {round; _} when Round.(locked_round.round < round) ->
may_preendorse new_state new_proposal
| _ ->
let new_state =
update_current_phase new_state Awaiting_preendorsements
in
Lwt.return (new_state, Watch_proposal))
| None ->
may_preendorse new_state new_proposal)
else
Events.(emit new_head_with_increasing_level ()) >>= fun () ->
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}
in
let level_state =
{
current_level = new_level;
latest_proposal = new_proposal;
is_latest_proposal_applied = is_proposal_applied;
locked_round = None;
endorsable_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
Lwt.return (state, action)
and may_switch_branch ~is_proposal_applied state new_proposal =
let switch_branch state =
Events.(emit switching_branch ()) >>= fun () ->
let round_update =
{
Baking_actions.new_round_proposal = new_proposal;
handle_proposal =
(fun state -> handle_proposal ~is_proposal_applied state new_proposal);
}
in
update_proposal ~is_proposal_applied state new_proposal >>= fun new_state ->
Lwt.return (new_state, Synchronize_round round_update)
in
let current_endorsable_payload = state.level_state.endorsable_payload in
match (current_endorsable_payload, new_proposal.block.prequorum) with
| None, Some _ | None, None ->
Events.(emit branch_proposal_has_better_fitness ()) >>= fun () ->
switch_branch state
| Some _, None ->
Events.(emit branch_proposal_has_no_prequorum ()) >>= fun () ->
do_nothing state
| Some {prequorum = current_pqc; _}, Some new_pqc ->
if Round.(current_pqc.round > new_pqc.round) then
Events.(emit branch_proposal_has_lower_prequorum ()) >>= fun () ->
do_nothing state
else if Round.(current_pqc.round < new_pqc.round) then
Events.(emit branch_proposal_has_better_prequorum ()) >>= fun () ->
switch_branch state
else
Events.(emit branch_proposal_has_same_prequorum ()) >>= fun () ->
do_nothing state
(** In the association map [delegate_slots], the function returns an
optional pair ([delegate], [endorsing_slot]) if for the current
[round], the validator [delegate] has a endorsing slot. *)
let round_proposer state delegate_slots round =
let round_mod =
Int32.to_int (Round.to_int32 round)
mod state.global_state.constants.parametric.consensus_committee_size
in
SlotMap.find
state.level_state.delegate_slots.all_slots_by_round.(round_mod)
delegate_slots
(** Inject a fresh block proposal containing the current operations of the
mempool in [state] and the additional [endorsements] and [dal_attestations]
for [delegate] at round [round]. *)
let propose_fresh_block_action ~endorsements ~dal_attestations ?last_proposal
~(predecessor : block_info) state delegate round =
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 endorsement_filter =
{
Operation_pool.level = predecessor.shell.level;
round = predecessor.round;
payload_hash = predecessor.payload_hash;
}
in
Operation_pool.filter_with_relevant_consensus_ops
~endorsement_filter
~preendorsement_filter:None
current_mempool.consensus
in
let filtered_mempool =
{current_mempool with consensus = relevant_consensus_operations}
in
let pool =
Operation_pool.add_operations
filtered_mempool
(List.map Operation.pack endorsements)
in
Operation_pool.add_operations
pool
(List.map Operation.pack dal_attestations)
in
let kind = Fresh operation_pool in
Events.(emit proposing_fresh_block (delegate, round)) >>= fun () ->
let force_apply =
state.global_state.config.force_apply || Round.(round <> zero)
in
let block_to_bake = {predecessor; round; delegate; kind; force_apply} in
let updated_state = update_current_phase state Idle in
Lwt.return @@ Inject_block {block_to_bake; updated_state}
let propose_block_action state delegate round (proposal : proposal) =
match state.level_state.endorsable_payload with
| None ->
Events.(emit no_endorsable_payload_fresh_block ()) >>= fun () ->
assert (state.level_state.locked_round = None) ;
let endorsements_in_last_proposal = proposal.block.quorum in
let dal_attestations_in_last_proposal = proposal.block.dal_attestations in
propose_fresh_block_action
~endorsements:endorsements_in_last_proposal
~dal_attestations:dal_attestations_in_last_proposal
state
~last_proposal:proposal.block
~predecessor:proposal.predecessor
delegate
round
| Some {proposal; prequorum} ->
Events.(emit repropose_block proposal.block.payload_hash) >>= fun () ->
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.preendorsements)
in
let endorsement_filter =
{
Operation_pool.level = proposal.predecessor.shell.level;
round = proposal.predecessor.round;
payload_hash = proposal.predecessor.payload_hash;
}
in
let preendorsement_filter =
Some
{
Operation_pool.level = prequorum.level;
round = prequorum.round;
payload_hash = prequorum.block_payload_hash;
}
in
Operation_pool.(
filter_with_relevant_consensus_ops
~endorsement_filter
~preendorsement_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
let updated_state = update_current_phase state Idle in
Lwt.return @@ Inject_block {block_to_bake; updated_state}
let end_of_round state current_round =
let new_round = Round.succ current_round in
let new_round_state = {state.round_state with current_round = new_round} in
let new_state = {state with round_state = new_round_state} in
match
round_proposer
new_state
new_state.level_state.delegate_slots.own_delegate_slots
new_state.round_state.current_round
with
| None ->
Events.(
emit
no_proposal_slot
(current_round, state.level_state.current_level, new_round))
>>= fun () ->
let new_state = update_current_phase new_state Idle in
do_nothing new_state
| Some (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
Events.(
emit
proposal_slot
(current_round, state.level_state.current_level, new_round, delegate))
>>= fun () ->
propose_block_action
new_state
delegate
new_round
state.level_state.latest_proposal
>>= fun action -> Lwt.return (new_state, action)
let time_to_bake_at_next_level state at_round =
let round_proposer_opt =
round_proposer
state
state.level_state.next_level_delegate_slots.own_delegate_slots
at_round
in
match (state.level_state.elected_block, round_proposer_opt) with
| None, _ | _, None ->
assert false
| Some elected_block, Some (delegate, _) ->
let endorsements = elected_block.endorsement_qc in
let dal_attestations =
[]
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
propose_fresh_block_action
~endorsements
~dal_attestations
~predecessor:elected_block.proposal.block
new_state
delegate
at_round
>>= fun action -> Lwt.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 make_endorse_action state proposal =
let endorsements : (consensus_key_and_delegate * consensus_content) list =
make_consensus_list state proposal
in
Inject_endorsements {endorsements}
let prequorum_reached_when_awaiting_preendorsements state candidate
preendorsements =
let latest_proposal = state.level_state.latest_proposal in
if Block_hash.(candidate.Operation_worker.hash <> latest_proposal.block.hash)
then
Events.(
emit
unexpected_prequorum_received
(candidate.hash, latest_proposal.block.hash))
>>= fun () -> 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;
preendorsements
;
}
in
let new_endorsable_payload = {proposal = latest_proposal; prequorum} in
let new_level_state =
let level_state_with_new_payload =
{
state.level_state with
endorsable_payload = Some new_endorsable_payload;
}
in
match state.level_state.endorsable_payload with
| None -> level_state_with_new_payload
| Some endorsable_payload ->
if
Round.(
endorsable_payload.prequorum.round
< new_endorsable_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_endorsements in
Lwt.return (new_state, make_endorse_action new_state latest_proposal)
let quorum_reached_when_waiting_endorsements state candidate endorsement_qc =
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
Events.(
emit
unexpected_quorum_received
(candidate.hash, latest_proposal.block.hash))
>>= fun () -> 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; endorsement_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 endorsement_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 endorsement_qc =
match state.round_state.delayed_quorum with
| None -> assert false
| Some endorsement_qc -> endorsement_qc
in
let elected_block = Some {proposal = latest_proposal; endorsement_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 step (state : Baking_state.t) (event : Baking_state.event) :
(Baking_state.t * Baking_actions.t) Lwt.t =
let phase = state.round_state.current_phase in
Events.(emit step_current_phase (phase, event)) >>= fun () ->
match (phase, event) with
| _, Timeout (End_of_round {ending_round}) ->
end_of_round state ending_round
| _, Timeout (Time_to_bake_next_level {at_round}) ->
time_to_bake_at_next_level state at_round
| Idle, New_head_proposal proposal ->
Events.(
emit
new_head
(proposal.block.hash, proposal.block.shell.level, proposal.block.round))
>>= fun () -> 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
Events.(
emit
new_head
( proposal.block.hash,
proposal.block.shell.level,
proposal.block.round ))
>>= fun () ->
Events.(emit unexpected_new_head_while_waiting_for_application ())
>>= fun () -> handle_proposal ~is_proposal_applied:true state proposal
else
Events.(emit applied_expected_proposal_received proposal.block.hash)
>>= fun () -> handle_expected_applied_proposal state
| Awaiting_endorsements, New_head_proposal proposal
| Awaiting_preendorsements, New_head_proposal proposal ->
Events.(
emit
new_head
(proposal.block.hash, proposal.block.shell.level, proposal.block.round))
>>= fun () ->
Events.(emit new_head_while_waiting_for_qc ()) >>= fun () ->
handle_proposal ~is_proposal_applied:true state proposal
| Idle, New_valid_proposal proposal ->
Events.(
emit
new_valid_proposal
(proposal.block.hash, proposal.block.shell.level, proposal.block.round))
>>= fun () -> handle_proposal ~is_proposal_applied:false state proposal
| Awaiting_application, New_valid_proposal proposal
| Awaiting_endorsements, New_valid_proposal proposal
| Awaiting_preendorsements, New_valid_proposal proposal ->
Events.(
emit
new_valid_proposal
(proposal.block.hash, proposal.block.shell.level, proposal.block.round))
>>= fun () ->
if has_already_been_handled state proposal then
Events.(emit valid_proposal_received_after_application ()) >>= fun () ->
do_nothing state
else
Events.(emit new_valid_proposal_while_waiting_for_qc ()) >>= fun () ->
handle_proposal ~is_proposal_applied:false state proposal
| Awaiting_preendorsements, Prequorum_reached (candidate, preendorsement_qc)
->
prequorum_reached_when_awaiting_preendorsements
state
candidate
preendorsement_qc
| Awaiting_endorsements, Quorum_reached (candidate, endorsement_qc) ->
quorum_reached_when_waiting_endorsements state candidate endorsement_qc
| Idle, (Prequorum_reached _ | Quorum_reached _)
| Awaiting_preendorsements, Quorum_reached _
| (Awaiting_application | Awaiting_endorsements), Prequorum_reached _
| Awaiting_application, Quorum_reached _ ->
do_nothing state