package octez-protocol-019-PtParisB-libs

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

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 Source License                                                       *)
(* Copyright (c) 2021 Nomadic Labs <contact@nomadic-labs.com>                *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

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
    (* current_round = proposal.round *)
    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
      (* An existing proposal was found at the same round: the
         proposal is bad and should be punished by the accuser *)
      let* () =
        Events.(
          emit
            proposal_for_round_already_seen
            (proposal.block.hash, current_round, previous_proposal.block.hash))
      in
      return Invalid
    else
      (* current_round = proposal.block.round ∧
         proposal.block.round <> previous_proposal.block.round
         =>
         proposal.block.round > previous_proposal.block.round

         The proposal has the expected round and the previous proposal
         is a predecessor therefore the proposal is valid *)
      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
  (* The branch is the latest finalized block. *)
  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

(* If we do not have any slots, we won't inject any operation but we
   will still participate to determine an elected block *)
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 =
    (* mark as applied if it is indeed applied or if this specific proposal was
       already marked as 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
    (* We do not preattest the first transition block *)
    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 =
      (* We have detected a new proposal that needs to be preattested.
         We switch to the `Awaiting_preattestations` phase. *)
      update_current_phase state Awaiting_preattestations
    in
    (* Here, we do not cancel pending signatures as it is already done
       in [handle_proposal]. *)
    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
    (* We do not vote for the first transition block *)
    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 =
      (* We have detected a new proposal that needs to be voted for.
         We switch to the `Awaiting_preattestations` phase. *)
      update_current_phase state Awaiting_preattestations
    in
    return (new_state, prepare_consensus_votes_action state proposal)

let extract_pqc 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 ->
            (* cannot happen if the map is correctly populated *)
            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, _ ->
      (* The proposal does not contain a PQC: no need to update *)
      state
  | Some {round = new_round; _}, Some {prequorum = {round = old_round; _}; _}
    when Round.(new_round < old_round) ->
      (* The proposal pqc is outdated, do not update *)
      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
  (* We need to avoid to send votes if we are in phases where consensus
     votes are already being forged. This is needed to avoid switching
     back from Awaiting_attestations to Awaiting_preattestations.
     Hypothesis: a fresh round's initial phase is Idle *)
  let may_vote state proposal =
    match state.round_state.current_phase with
    | Idle ->
        (* We prioritize the new and meaningful consensus vote signing
           by cancelling all pending forge and signing tasks. In this
           current context, preattesting means that it is now too late
           to include outdated consensus votes and blocks. However,
           active forge requests are still processed until
           completion. *)
        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
    (* The baker is ahead, a reorg may have happened. Do nothing:
       wait for the node to send us the branch's head. This new head
       should have a fitness that is greater than our current
       proposal and thus, its level should be at least the same as
       our current proposal's level. *)
    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
      (* The received head is a new proposal for the current level:
         let's check if it's a valid one for us. *)
      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 ->
          (* The proposal is invalid: we ignore it *)
          let* () = Events.(emit skipping_invalid_proposal ()) in
          do_nothing state
      | Outdated_proposal ->
          (* Check whether we need to update our attestable payload  *)
          let state =
            may_update_attestable_payload_with_internal_pqc state new_proposal
          in
          (* The proposal is outdated: we update to be able to extract
             its included attestations but we do not attest it *)
          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 -> (
          (* Valid_proposal => proposal.round = current_round *)
          (* Check whether we need to update our attestable payload  *)
          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
          (* We invalidate early attestations *)
          let new_round_state =
            {new_state.round_state with early_attestations = []}
          in
          let new_state = {new_state with round_state = new_round_state} in
          (* The proposal is valid but maybe we already locked on a payload *)
          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
                (* when the new head has the same payload as our
                   [locked_round], we accept it and vote for it *)
                may_vote new_state new_proposal
              else
                (* The payload is different *)
                match new_proposal.block.prequorum with
                | Some {round; _} when Round.(locked_round.round < round) ->
                    (* This PQC is above our locked_round, we can and vote for it *)
                    may_vote new_state new_proposal
                | _ ->
                    (* We shouldn't vote for proposal, but we
                       should at least watch (pre)quorums events on it
                       but only when it is applied otherwise we await
                       for the proposal to be applied. *)
                    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 ->
              (* Otherwise, we did not lock on any payload, thus we can
                 vote for it it *)
              may_vote new_state new_proposal)
  else
    (* Last case: new_proposal_level > current_level *)
    (* Possible scenarios:
       - we received a block for a next level
       - we received our own block
         This is where we update our [level_state] (and our [round_state]) *)
    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;
          (* Unlock values *)
          locked_round = None;
          attestable_payload = None;
          elected_block = None;
          delegate_slots;
          next_level_delegate_slots;
          next_level_proposed_round = None;
        }
      in
      (* recursive call with the up-to-date state to handle the new
         level proposals *)
      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
    (* If we are on a different branch, we also need to update our
       [round_state] accordingly.
       The recursive call to [handle_proposal] cannot end up
       with an invalid proposal as it's on a different branch, thus
       there is no need to backtrack to the former state as the new
       proposal must end up being the new [latest_proposal]. That's
       why we update it here. *)
    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
    (* TODO if the branch proposal is outdated, we should
       trigger an [End_of_round] to participate *)
    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
      (* The new branch contains a PQC (and we do not) or a better
         fitness, we switch. *)
      switch_branch state
  | Some _, None ->
      (* We have a better PQC, we don't switch as we are able to
         propose a better chain if we stay on our current one. *)
      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
        (* The other's branch PQC is lower than ours, do not
           switch *)
        do_nothing state
      else if Round.(current_pqc.round < new_pqc.round) then
        let* () = Events.(emit branch_proposal_has_better_prequorum ()) in
        (* Their PQC is better than ours: we switch *)
        switch_branch state
      else
        (* current_pqc.round = new_pqc *)
        (* There is a PQC on two branches with the same round and
           the same level but not the same predecessor : it's
           impossible unless if there was some double-baking. This
           shouldn't happen but do nothing anyway. *)
        let* () = Events.(emit branch_proposal_has_same_prequorum ()) in
        do_nothing state

(* Create a fresh block proposal containing the current operations of the
   mempool in [state] and the additional [attestations] for [delegate] at round
   [round]. *)
let prepare_block_to_bake ~attestations ?last_proposal
    ~(predecessor : block_info) state delegate round =
  (* The block to bake embeds the operations gathered by the
     worker. However, consensus operations that are not relevant for
     this block are filtered out. In the case of proposing a new fresh
     block, the block is supposed to carry only attestations for the
     previous level. *)
  let open Lwt_syntax in
  let operation_pool =
    (* 1. Fetch operations from the mempool. *)
    let current_mempool =
      let pool =
        Operation_worker.get_current_operations
          state.global_state.operation_worker
      in
      (* Considered the operations in the previous proposal as well *)
      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
    (* 2. Filter and only retain relevant attestations. *)
    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
    (* 3. Add the additional given [attestations].
         N.b. this is a set: there won't be duplicates *)
    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)
    (* This is used as a safety net by applying blocks on round > 0, in case
       validation-only did not produce a correct round-0 block. *)
  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 =
  (* TODO check if there is a trace where we could not have updated the level *)
  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
  (* Possible cases:
     1. There was a proposal but the PQC was not reached.
     2. There was a proposal and the PQC was reached. We repropose the
     [attestable_payload] if it exists, not the [locked_round] as it
     may be older. *)
  match state.level_state.attestable_payload with
  | None ->
      let* () = Events.(emit no_attestable_payload_fresh_block ()) in
      (* For case 1, we may re-inject with the same payload or a fresh
         one. We make the choice of baking a fresh one: the previous
         proposal may have been rejected because the block may have been
         valid but may be considered "bad" (censored operations, empty
         block, etc.) by the other validators. *)
      (* Invariant: there is no locked round if there is no attestable
         payload *)
      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
      (* For case 2, we re-inject the same block as [attestable_round]
         but we may add some left-overs attestations. Therefore, the
         operations we need to include are:
         - the proposal's included attestations
         - the potential missing new attestations for the
           previous block
         - the PQC of the attestable payload *)
      let consensus_operations =
        (* Fetch preattestations and attestations from the mempool
           (that could be missing from the proposal), filter, then add
           consensus operations of the proposal itself, and convert
           into [packed_operation trace]. *)
        let mempool_consensus_operations =
          (Operation_worker.get_current_operations
             state.global_state.operation_worker)
            .consensus
        in
        let all_consensus_operations =
          (* Add the proposal and pqc consensus operations to the
             mempool *)
          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
        (* This is used as a safety net by applying blocks on round > 0, in case
           validation-only did not produce a correct round-0 block. *)
      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
  (* We initialize the round's phase to Idle for the [handle_proposal]
     transition to trigger the preattestation action. *)
  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
  (* we need to check if we need to bake for this round or not *)
  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
      (* We don't have any delegate that may propose a new block for
         this round -- We will wait for preattestations when the next
         level block arrive. Meanwhile, we are idle *)
      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 not inject a block for the previous protocol! (Let the
           baker of the previous protocol do it.) *)
        do_nothing new_state
      else
        let* () =
          Events.(
            emit
              proposal_slot
              ( current_round,
                state.level_state.current_level,
                new_round,
                consensus_key_and_delegate ))
        in
        (* We have a delegate, we need to determine what to inject *)
        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
  (* It is now time to update the state level *)
  (* We need to keep track for which block we have 2f+1 *attestations*, that is,
     which will become the new predecessor_block *)
  (* Invariant: attestable_round >= round(elected block) >= locked_round *)
  let round_proposer_opt = round_proposer state ~level:`Next at_round in
  match (state.level_state.elected_block, round_proposer_opt) with
  | None, _ | _, None ->
      (* Unreachable: the [Time_to_prepare_next_level_block] event can only be
         triggered when we have a slot and an elected block *)
      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 -> (* Unreachable *) 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
        (* preattestations may be nil when [consensus_threshold] is 0 *);
      }
    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
      (* We were locked and did not trigger preemptive
         consensus votes: we need to start attesting now. *)
      return (new_state, prepare_attest_action new_state latest_proposal)
    else
      (* We already triggered preemptive attestation forging, we
         either have those already or we are waiting for them. *)
      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 ->
          (* The elected proposal has been applied. Record the elected block
             and transition to the Idle phase, as there is nothing left to do.
          *)
          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 ->
          (* A quorum has been reached, but the elected proposal has not been
             applied yet by the node. Transition in the `Awaiting_application`
             phase, and do not save the elected block in the state. Instead,
             keep track that an early quorum has been reached at this round.
             This avoids the proposer of a block at the next level to inject a
             proposal before the node has been applied the proposal elected at
             the current level. *)
          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 _ ->
          (* If we already have an elected block, do not update it: the
             earlier, the better. We do not need to record the quorum either,
             as this won't be used to elect a new block. *)
          (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
  (* This code is triggered only when in the `Awaiting_application`phase, which
     in turn is reached only if a quorum has been met but the
     block has not been applied by the node. As a consequence, the only
     thing that is left to do at this stage is to update the elected block
     and transition to the Idle phase, as there is nothing left to do. *)
  let new_state = {state with level_state = new_level_state} in
  let new_state =
    match new_state.level_state.elected_block with
    | None ->
        (* We do not have an elected block for this level. We need to update it. *)
        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 _ ->
        (* We already have an elected block at this level. We do not need to update
           it. The earlier, the better. *)
        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 ->
        (* An attestation is ready for injection but the prequorum has
           not been reached yet: we save them until then. *)
        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 ->
        (* For these three phases, we have necessarily already reached the
           prequorum: we are safe to inject. *)
        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

(* Hypothesis:
   - The state is not to be modified outside this module
     (NB: there are exceptions in Baking_actions: the corner cases
     [update_to_level] and [synchronize_round] and
     the hack used by [inject_block])

   - new_proposal's received blocks are expected to belong to our current
     round

   - [Prequorum_reached] can only be received when we've seen a new head

   - [Quorum_reached] can only be received when we've seen a
     [Prequorum_reached] *)
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
  (* Handle timeouts *)
  | _, Timeout (End_of_round {ending_round}) ->
      (* If the round is ending, stop everything currently going on and
         increment the round. *)
      end_of_round state ending_round
  | _, Timeout (Time_to_prepare_next_level_block {at_round}) ->
      (* If it is time to bake the next level, stop everything currently
         going on and propose the next level block *)
      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
  (* Unreachable cases *)
  | Idle, (Prequorum_reached _ | Quorum_reached _)
  | Awaiting_preattestations, Quorum_reached _
  | (Awaiting_application | Awaiting_attestations), Prequorum_reached _
  | Awaiting_application, Quorum_reached _ ->
      (* This cannot/should not happen *)
      do_nothing state
OCaml

Innovation. Community. Security.