package octez-protocol-017-PtNairob-libs

  1. Overview
  2. Docs
Octez protocol 017-PtNairob libraries

Install

Dune Dependency

Authors

Maintainers

Sources

octez-19.0.tar.gz
sha256=c6df840ebbf115e454db949028c595bec558a59a66cade73b52a6d099d6fa4d4
sha512=d8aee903b9fe130d73176bc8ec38b78c9ff65317da3cb4f3415f09af0c625b4384e7498201fdb61aa39086a7d5d409d0ab3423f9bc3ab989a680cf444a79bc13

doc/src/octez-protocol-017-PtNairob-libs.test-helpers/block.ml.html

Source file block.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
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com>     *)
(* Copyright (c) 2020 Metastate AG <hello@metastate.dev>                     *)
(* Copyright (c) 2022 Trili Tech  <contact@trili.tech>                       *)
(*                                                                           *)
(* 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
module Proto_Nonce = Nonce (* Renamed otherwise is masked by Alpha_context *)
open Alpha_context

(* This type collects a block and the context that results from its application *)
type t = {
  hash : Block_hash.t;
  header : Block_header.t;
  operations : Operation.packed list;
  context : Tezos_protocol_environment.Context.t;
}

type block = t

let rpc_context block =
  {
    Environment.Updater.block_hash = block.hash;
    block_header = block.header.shell;
    context = block.context;
  }

let rpc_ctxt =
  new Environment.proto_rpc_context_of_directory
    rpc_context
    Plugin.RPC.rpc_services

(******** Policies ***********)

(* Policies are functions that take a block and return a tuple
   [(account, level, timestamp)] for the [forge_header] function. *)

(* This type is used only to provide a simpler interface to the exterior. *)
type baker_policy =
  | By_round of int
  | By_account of public_key_hash
  | Excluding of public_key_hash list

type baking_mode = Application | Baking

let get_next_baker_by_round round block =
  Plugin.RPC.Baking_rights.get rpc_ctxt ~all:true ~max_round:(round + 1) block
  >|=? fun bakers ->
  let {Plugin.RPC.Baking_rights.delegate = pkh; consensus_key; timestamp; _} =
    WithExceptions.Option.get ~loc:__LOC__
    @@ List.find
         (fun {Plugin.RPC.Baking_rights.round = r; _} ->
           Round.to_int32 r = Int32.of_int round)
         bakers
  in
  ( pkh,
    consensus_key,
    round,
    WithExceptions.Option.to_exn ~none:(Failure "") timestamp )

let get_next_baker_by_account pkh block =
  Plugin.RPC.Baking_rights.get rpc_ctxt ~delegates:[pkh] block
  >>=? fun bakers ->
  (match List.hd bakers with
  | Some b -> return b
  | None -> failwith "No slots found for %a" Signature.Public_key_hash.pp pkh)
  >>=? fun {
             Plugin.RPC.Baking_rights.delegate = pkh;
             consensus_key;
             timestamp;
             round;
             _;
           } ->
  Environment.wrap_tzresult (Round.to_int round) >>?= fun round ->
  return
    ( pkh,
      consensus_key,
      round,
      WithExceptions.Option.to_exn ~none:(Failure __LOC__) timestamp )

let get_next_baker_excluding excludes block =
  Plugin.RPC.Baking_rights.get rpc_ctxt block >>=? fun bakers ->
  let {
    Plugin.RPC.Baking_rights.delegate = pkh;
    consensus_key;
    timestamp;
    round;
    _;
  } =
    WithExceptions.Option.get ~loc:__LOC__
    @@ List.find
         (fun {Plugin.RPC.Baking_rights.consensus_key; _} ->
           not
             (List.mem
                ~equal:Signature.Public_key_hash.equal
                consensus_key
                excludes))
         bakers
  in
  Environment.wrap_tzresult (Round.to_int round) >>?= fun round ->
  return
    ( pkh,
      consensus_key,
      round,
      WithExceptions.Option.to_exn ~none:(Failure "") timestamp )

let dispatch_policy = function
  | By_round r -> get_next_baker_by_round r
  | By_account a -> get_next_baker_by_account a
  | Excluding al -> get_next_baker_excluding al

let get_next_baker ?(policy = By_round 0) = dispatch_policy policy

let get_round (b : t) =
  let fitness = b.header.shell.fitness in
  Fitness.(from_raw fitness >|? round) |> Environment.wrap_tzresult

module Forge = struct
  type header = {
    baker : public_key_hash;
    consensus_key : public_key_hash;
    (* the signer of the block *)
    shell : Block_header.shell_header;
    contents : Block_header.contents;
  }

  let default_proof_of_work_nonce =
    Bytes.create Constants.proof_of_work_nonce_size

  let make_contents ?(proof_of_work_nonce = default_proof_of_work_nonce)
      ~payload_hash ~payload_round
      ?(liquidity_baking_toggle_vote = Liquidity_baking.LB_pass)
      ~seed_nonce_hash () =
    Block_header.
      {
        payload_hash;
        payload_round;
        proof_of_work_nonce;
        seed_nonce_hash;
        liquidity_baking_toggle_vote;
      }

  let make_shell ~level ~predecessor ~timestamp ~fitness ~operations_hash =
    Tezos_base.Block_header.
      {
        level;
        predecessor;
        timestamp;
        fitness;
        operations_hash;
        (* We don't care of the following values, only the shell validates them. *)
        proto_level = 0;
        validation_passes = 0;
        context = Context_hash.zero;
      }

  let set_seed_nonce_hash seed_nonce_hash
      {baker; consensus_key; shell; contents} =
    {baker; consensus_key; shell; contents = {contents with seed_nonce_hash}}

  let set_baker baker ?(consensus_key = baker) header =
    {header with baker; consensus_key}

  let sign_header {consensus_key; shell; contents; _} =
    Account.find consensus_key >|=? fun signer_account ->
    let unsigned_bytes =
      Data_encoding.Binary.to_bytes_exn
        Block_header.unsigned_encoding
        (shell, contents)
    in
    let signature =
      Signature.sign
        ~watermark:Block_header.(to_watermark (Block_header Chain_id.zero))
        signer_account.sk
        unsigned_bytes
    in
    Block_header.{shell; protocol_data = {contents; signature}}

  let classify_operations operations =
    let validation_passes_len = List.length Main.validation_passes in
    let t = Array.make validation_passes_len [] in
    List.iter
      (fun (op : packed_operation) ->
        match Main.acceptable_pass op with
        | None -> ()
        | Some pass -> t.(pass) <- op :: t.(pass))
      operations ;
    let t = Array.map List.rev t in
    Array.to_list t

  let forge_header ?(locked_round = None) ?(payload_round = None)
      ?(policy = By_round 0) ?timestamp ?(operations = [])
      ?liquidity_baking_toggle_vote pred =
    let pred_fitness =
      match Fitness.from_raw pred.header.shell.fitness with
      | Ok pred_fitness -> pred_fitness
      | _ -> assert false
    in
    let predecessor_round = Fitness.round pred_fitness in
    dispatch_policy policy pred
    >>=? fun (delegate, consensus_key, round, expected_timestamp) ->
    let timestamp = Option.value ~default:expected_timestamp timestamp in
    let level = Int32.succ pred.header.shell.level in
    Raw_level.of_int32 level |> Environment.wrap_tzresult >>?= fun raw_level ->
    Round.of_int round |> Environment.wrap_tzresult >>?= fun round ->
    Fitness.create ~level:raw_level ~predecessor_round ~round ~locked_round
    >|? Fitness.to_raw |> Environment.wrap_tzresult
    >>?= fun fitness ->
    (Plugin.RPC.current_level ~offset:1l rpc_ctxt pred >|=? function
     | {expected_commitment = true; _} -> Some (fst (Proto_Nonce.generate ()))
     | {expected_commitment = false; _} -> None)
    >|=? fun seed_nonce_hash ->
    let hashes = List.map Operation.hash_packed operations in
    let operations_hash =
      Operation_list_list_hash.compute [Operation_list_hash.compute hashes]
    in
    let shell =
      make_shell
        ~level
        ~predecessor:pred.hash
        ~timestamp
        ~fitness
        ~operations_hash
    in
    let operations = classify_operations operations in
    let non_consensus_operations =
      List.concat (match List.tl operations with None -> [] | Some l -> l)
    in
    let hashes = List.map Operation.hash_packed non_consensus_operations in
    let payload_round =
      match payload_round with None -> round | Some r -> r
    in
    let payload_hash =
      Block_payload.hash
        ~predecessor_hash:shell.predecessor
        ~payload_round
        hashes
    in
    let contents =
      make_contents
        ~seed_nonce_hash
        ?liquidity_baking_toggle_vote
        ~payload_hash
        ~payload_round
        ()
    in
    {baker = delegate; consensus_key; shell; contents}

  (* compatibility only, needed by incremental *)
  let contents ?(proof_of_work_nonce = default_proof_of_work_nonce)
      ?seed_nonce_hash
      ?(liquidity_baking_toggle_vote = Liquidity_baking.LB_pass) ~payload_hash
      ~payload_round () =
    {
      Block_header.proof_of_work_nonce;
      seed_nonce_hash;
      liquidity_baking_toggle_vote;
      payload_hash;
      payload_round;
    }
end

(********* Genesis creation *************)

(* Hard-coded context key *)
let protocol_param_key = ["protocol_parameters"]

let check_constants_consistency constants =
  let open Constants.Parametric in
  let {
    blocks_per_cycle;
    blocks_per_commitment;
    nonce_revelation_threshold;
    blocks_per_stake_snapshot;
    _;
  } =
    constants
  in
  Error_monad.unless (blocks_per_commitment <= blocks_per_cycle) (fun () ->
      failwith
        "Inconsistent constants : blocks_per_commitment must be less than \
         blocks_per_cycle")
  >>=? fun () ->
  Error_monad.unless (nonce_revelation_threshold <= blocks_per_cycle) (fun () ->
      failwith
        "Inconsistent constants : nonce_revelation_threshold must be less than \
         blocks_per_cycle")
  >>=? fun () ->
  Error_monad.unless (blocks_per_cycle >= blocks_per_stake_snapshot) (fun () ->
      failwith
        "Inconsistent constants : blocks_per_cycle must be superior than \
         blocks_per_stake_snapshot")

let prepare_main_init_params ?bootstrap_contracts commitments constants
    bootstrap_accounts =
  let open Tezos_protocol_017_PtNairob_parameters in
  let parameters =
    Default_parameters.parameters_of_constants
      ~bootstrap_accounts
      ?bootstrap_contracts
      ~commitments
      constants
  in
  let json = Default_parameters.json_of_parameters parameters in
  let proto_params =
    Data_encoding.Binary.to_bytes_exn Data_encoding.json json
  in
  Tezos_protocol_environment.Context.(
    let empty = Tezos_protocol_environment.Memory_context.empty in
    add empty ["version"] (Bytes.of_string "genesis") >>= fun ctxt ->
    add ctxt protocol_param_key proto_params)

let initial_context ?(commitments = []) ?bootstrap_contracts chain_id constants
    header bootstrap_accounts =
  prepare_main_init_params
    ?bootstrap_contracts
    commitments
    constants
    bootstrap_accounts
  >>= fun ctxt ->
  Main.init chain_id ctxt header >|= Environment.wrap_tzresult
  >|=? fun {context; _} -> context

let initial_alpha_context ?(commitments = []) constants
    (block_header : Block_header.shell_header) bootstrap_accounts =
  prepare_main_init_params commitments constants bootstrap_accounts
  >>= fun ctxt ->
  let level = block_header.level in
  let timestamp = block_header.timestamp in
  let predecessor = block_header.predecessor in
  let typecheck (ctxt : Alpha_context.context) (script : Alpha_context.Script.t)
      =
    let allow_forged_in_storage =
      false
      (* There should be no forged value in bootstrap contracts. *)
    in
    Script_ir_translator.parse_script
      ctxt
      ~elab_conf:(Script_ir_translator_config.make ~legacy:true ())
      ~allow_forged_in_storage
      script
    >>=? fun (Ex_script (Script parsed_script), ctxt) ->
    Script_ir_translator.extract_lazy_storage_diff
      ctxt
      Optimized
      parsed_script.storage_type
      parsed_script.storage
      ~to_duplicate:Script_ir_translator.no_lazy_storage_id
      ~to_update:Script_ir_translator.no_lazy_storage_id
      ~temporary:false
    >>=? fun (storage, lazy_storage_diff, ctxt) ->
    Script_ir_translator.unparse_data
      ctxt
      Optimized
      parsed_script.storage_type
      storage
    >|=? fun (storage, ctxt) ->
    let storage = Alpha_context.Script.lazy_expr storage in
    (({script with storage}, lazy_storage_diff), ctxt)
  in
  Alpha_context.prepare_first_block
    ~typecheck
    ~level
    ~timestamp
    ~predecessor
    Chain_id.zero
    ctxt
  >|= Environment.wrap_tzresult

let genesis_with_parameters parameters =
  let hash =
    Block_hash.of_b58check_exn
      "BLockGenesisGenesisGenesisGenesisGenesisCCCCCeZiLHU"
  in
  let fitness =
    Fitness_repr.create_without_locked_round
      ~level:(Protocol.Raw_level_repr.of_int32_exn 0l)
      ~predecessor_round:Round_repr.zero
      ~round:Round_repr.zero
    |> Fitness_repr.to_raw
  in
  let shell =
    Forge.make_shell
      ~level:0l
      ~predecessor:hash
      ~timestamp:Time.Protocol.epoch
      ~fitness
      ~operations_hash:Operation_list_list_hash.zero
  in
  let contents =
    Forge.make_contents
      ~payload_hash:Block_payload_hash.zero
      ~payload_round:Round.zero
      ~seed_nonce_hash:None
      ()
  in
  let open Tezos_protocol_017_PtNairob_parameters in
  let json = Default_parameters.json_of_parameters parameters in
  let proto_params =
    Data_encoding.Binary.to_bytes_exn Data_encoding.json json
  in
  Tezos_protocol_environment.Context.(
    let empty = Tezos_protocol_environment.Memory_context.empty in
    add empty ["version"] (Bytes.of_string "genesis") >>= fun ctxt ->
    add ctxt protocol_param_key proto_params)
  >>= fun ctxt ->
  let chain_id = Chain_id.of_block_hash hash in
  Main.init chain_id ctxt shell >|= Environment.wrap_tzresult
  >|=? fun {context; _} ->
  {
    hash;
    header = {shell; protocol_data = {contents; signature = Signature.zero}};
    operations = [];
    context;
  }

let validate_bootstrap_accounts
    (bootstrap_accounts : Parameters.bootstrap_account list) minimal_stake =
  if bootstrap_accounts = [] then
    Stdlib.failwith "Must have one account with minimal_stake to bake" ;
  (* Check there are at least minimal_stake tokens *)
  Lwt.catch
    (fun () ->
      List.fold_left_es
        (fun acc (Parameters.{amount; _} : Parameters.bootstrap_account) ->
          Environment.wrap_tzresult @@ Tez.( +? ) acc amount >>?= fun acc ->
          if acc >= minimal_stake then raise Exit else return acc)
        Tez.zero
        bootstrap_accounts
      >>=? fun (_ : Tez.t) ->
      failwith
        "Insufficient tokens in initial accounts: the amount should be at \
         least minimal_stake")
    (function Exit -> return_unit | exc -> Lwt.reraise exc)

let prepare_initial_context_params ?consensus_threshold ?min_proposal_quorum
    ?level ?cost_per_byte ?liquidity_baking_subsidy ?endorsing_reward_per_slot
    ?baking_reward_bonus_per_slot ?baking_reward_fixed_portion ?origination_size
    ?blocks_per_cycle ?cycles_per_voting_period ?sc_rollup_enable
    ?sc_rollup_arith_pvm_enable ?dal_enable ?zk_rollup_enable
    ?hard_gas_limit_per_block ?nonce_revelation_threshold () =
  let open Tezos_protocol_017_PtNairob_parameters in
  let constants = Default_parameters.constants_test in
  let min_proposal_quorum =
    Option.value ~default:constants.min_proposal_quorum min_proposal_quorum
  in
  let cost_per_byte =
    Option.value ~default:constants.cost_per_byte cost_per_byte
  in
  let liquidity_baking_subsidy =
    Option.value
      ~default:constants.liquidity_baking_subsidy
      liquidity_baking_subsidy
  in
  let endorsing_reward_per_slot =
    Option.value
      ~default:constants.endorsing_reward_per_slot
      endorsing_reward_per_slot
  in
  let baking_reward_bonus_per_slot =
    Option.value
      ~default:constants.baking_reward_bonus_per_slot
      baking_reward_bonus_per_slot
  in
  let baking_reward_fixed_portion =
    Option.value
      ~default:constants.baking_reward_fixed_portion
      baking_reward_fixed_portion
  in
  let origination_size =
    Option.value ~default:constants.origination_size origination_size
  in
  let blocks_per_cycle =
    Option.value ~default:constants.blocks_per_cycle blocks_per_cycle
  in
  let cycles_per_voting_period =
    Option.value
      ~default:constants.cycles_per_voting_period
      cycles_per_voting_period
  in
  let consensus_threshold =
    Option.value ~default:constants.consensus_threshold consensus_threshold
  in
  let sc_rollup_enable =
    Option.value ~default:constants.sc_rollup.enable sc_rollup_enable
  in
  let sc_rollup_arith_pvm_enable =
    Option.value ~default:constants.sc_rollup.enable sc_rollup_arith_pvm_enable
  in
  let dal_enable =
    Option.value ~default:constants.dal.feature_enable dal_enable
  in
  let zk_rollup_enable =
    Option.value ~default:constants.zk_rollup.enable zk_rollup_enable
  in
  let hard_gas_limit_per_block =
    Option.value
      ~default:constants.hard_gas_limit_per_block
      hard_gas_limit_per_block
  in
  let nonce_revelation_threshold =
    Option.value
      ~default:constants.nonce_revelation_threshold
      nonce_revelation_threshold
  in
  let constants =
    {
      constants with
      endorsing_reward_per_slot;
      baking_reward_bonus_per_slot;
      baking_reward_fixed_portion;
      origination_size;
      blocks_per_cycle;
      cycles_per_voting_period;
      min_proposal_quorum;
      cost_per_byte;
      liquidity_baking_subsidy;
      consensus_threshold;
      tx_rollup = constants.tx_rollup;
      sc_rollup =
        {
          constants.sc_rollup with
          enable = sc_rollup_enable;
          arith_pvm_enable = sc_rollup_arith_pvm_enable;
        };
      dal = {constants.dal with feature_enable = dal_enable};
      zk_rollup = {constants.zk_rollup with enable = zk_rollup_enable};
      hard_gas_limit_per_block;
      nonce_revelation_threshold;
    }
  in
  check_constants_consistency constants >>=? fun () ->
  let hash =
    Block_hash.of_b58check_exn
      "BLockGenesisGenesisGenesisGenesisGenesisCCCCCeZiLHU"
  in
  let level = Option.value ~default:0l level in
  let fitness =
    Fitness_repr.create_without_locked_round
      ~level:(Protocol.Raw_level_repr.of_int32_exn level)
      ~predecessor_round:Round_repr.zero
      ~round:Round_repr.zero
    |> Fitness_repr.to_raw
  in
  let shell =
    Forge.make_shell
      ~level
      ~predecessor:hash
      ~timestamp:Time.Protocol.epoch
      ~fitness
      ~operations_hash:Operation_list_list_hash.zero
  in
  return (constants, shell, hash)

(* if no parameter file is passed we check in the current directory
   where the test is run *)
let genesis ?commitments ?consensus_threshold ?min_proposal_quorum
    ?bootstrap_contracts ?level ?cost_per_byte ?liquidity_baking_subsidy
    ?endorsing_reward_per_slot ?baking_reward_bonus_per_slot
    ?baking_reward_fixed_portion ?origination_size ?blocks_per_cycle
    ?cycles_per_voting_period ?sc_rollup_enable ?sc_rollup_arith_pvm_enable
    ?dal_enable ?zk_rollup_enable ?hard_gas_limit_per_block
    ?nonce_revelation_threshold
    (bootstrap_accounts : Parameters.bootstrap_account list) =
  prepare_initial_context_params
    ?consensus_threshold
    ?min_proposal_quorum
    ?level
    ?cost_per_byte
    ?liquidity_baking_subsidy
    ?endorsing_reward_per_slot
    ?baking_reward_bonus_per_slot
    ?baking_reward_fixed_portion
    ?origination_size
    ?blocks_per_cycle
    ?cycles_per_voting_period
    ?sc_rollup_enable
    ?sc_rollup_arith_pvm_enable
    ?dal_enable
    ?zk_rollup_enable
    ?hard_gas_limit_per_block
    ?nonce_revelation_threshold
    ()
  >>=? fun (constants, shell, hash) ->
  validate_bootstrap_accounts bootstrap_accounts constants.minimal_stake
  >>=? fun () ->
  initial_context
    ?commitments
    ?bootstrap_contracts
    (Chain_id.of_block_hash hash)
    constants
    shell
    bootstrap_accounts
  >|=? fun context ->
  let contents =
    Forge.make_contents
      ~payload_hash:Block_payload_hash.zero
      ~payload_round:Round.zero
      ~seed_nonce_hash:None
      ()
  in
  {
    hash;
    header = {shell; protocol_data = {contents; signature = Signature.zero}};
    operations = [];
    context;
  }

let alpha_context ?commitments ?min_proposal_quorum
    (bootstrap_accounts : Parameters.bootstrap_account list) =
  prepare_initial_context_params ?min_proposal_quorum ()
  >>=? fun (constants, shell, _hash) ->
  validate_bootstrap_accounts bootstrap_accounts constants.minimal_stake
  >>=? fun () ->
  initial_alpha_context ?commitments constants shell bootstrap_accounts

(********* Baking *************)

let begin_validation_and_application ctxt chain_id mode ~predecessor =
  let open Lwt_result_syntax in
  let* validation_state = begin_validation ctxt chain_id mode ~predecessor in
  let* application_state = begin_application ctxt chain_id mode ~predecessor in
  return (validation_state, application_state)

let get_application_vstate (pred : t) (operations : Protocol.operation trace) =
  Forge.forge_header pred ~operations >>=? fun header ->
  Forge.sign_header header >>=? fun header ->
  let open Environment.Error_monad in
  begin_validation_and_application
    pred.context
    Chain_id.zero
    (Application header)
    ~predecessor:pred.header.shell
  >|= Environment.wrap_tzresult

(* Note that by calling this function without [protocol_data], we
   force the mode to be partial construction. *)
let get_construction_vstate ?(policy = By_round 0) ?timestamp
    ?(protocol_data = None) (pred : t) =
  let open Protocol in
  dispatch_policy policy pred
  >>=? fun (_pkh, _ck, _round, expected_timestamp) ->
  let timestamp = Option.value ~default:expected_timestamp timestamp in
  let mode =
    match protocol_data with
    | None -> Partial_construction {predecessor_hash = pred.hash; timestamp}
    | Some block_header_data ->
        Construction
          {predecessor_hash = pred.hash; timestamp; block_header_data}
  in
  begin_validation_and_application
    pred.context
    Chain_id.zero
    mode
    ~predecessor:pred.header.shell
  >|= Environment.wrap_tzresult

let validate_and_apply_operation (validation_state, application_state) op =
  let open Lwt_result_syntax in
  let oph = Operation.hash_packed op in
  let* validation_state = validate_operation validation_state oph op in
  let* application_state, receipt = apply_operation application_state oph op in
  return ((validation_state, application_state), receipt)

let finalize_validation_and_application (validation_state, application_state)
    shell_header =
  let open Lwt_result_syntax in
  let* () = finalize_validation validation_state in
  finalize_application application_state shell_header

let detect_manager_failure :
    type kind. kind Apply_results.operation_metadata -> _ =
  let rec detect_manager_failure :
      type kind. kind Apply_results.contents_result_list -> _ =
    let open Apply_results in
    let open Apply_operation_result in
    let open Apply_internal_results in
    let detect_manager_failure_single (type kind)
        (Manager_operation_result
           {operation_result; internal_operation_results; _} :
          kind Kind.manager Apply_results.contents_result) =
      let detect_manager_failure (type kind)
          (result : (kind, _, _) operation_result) =
        match result with
        | Applied _ -> Ok ()
        | Skipped _ -> assert false
        | Backtracked (_, None) ->
            (* there must be another error for this to happen *)
            Ok ()
        | Backtracked (_, Some errs) -> Error errs
        | Failed (_, errs) -> Error errs
      in
      detect_manager_failure operation_result >>? fun () ->
      List.iter_e
        (fun (Internal_operation_result (_, r)) -> detect_manager_failure r)
        internal_operation_results
    in
    function
    | Single_result (Manager_operation_result _ as res) ->
        detect_manager_failure_single res
    | Single_result _ -> Ok ()
    | Cons_result (res, rest) ->
        detect_manager_failure_single res >>? fun () ->
        detect_manager_failure rest
  in
  fun {contents} -> detect_manager_failure contents

let apply_with_metadata ?(policy = By_round 0) ?(check_size = true) ~baking_mode
    ~allow_manager_failures header ?(operations = []) pred =
  let open Environment.Error_monad in
  ( (match baking_mode with
    | Application ->
        begin_validation_and_application
          pred.context
          Chain_id.zero
          (Application header)
          ~predecessor:pred.header.shell
        >|= Environment.wrap_tzresult
    | Baking ->
        get_construction_vstate
          ~policy
          ~protocol_data:(Some header.protocol_data)
          (pred : t))
  >>=? fun vstate ->
    List.fold_left_es
      (fun vstate op ->
        (if check_size then
         let operation_size =
           Data_encoding.Binary.length Operation.encoding op
         in
         if operation_size > Constants_repr.max_operation_data_length then
           raise
             (invalid_arg
                (Format.sprintf
                   "The operation size is %d, it exceeds the constant maximum \
                    size %d"
                   operation_size
                   Constants_repr.max_operation_data_length))) ;
        validate_and_apply_operation vstate op >>=? fun (state, result) ->
        if allow_manager_failures then return state
        else
          match result with
          | No_operation_metadata -> return state
          | Operation_metadata metadata ->
              detect_manager_failure metadata >>?= fun () -> return state)
      vstate
      operations
    >|= Environment.wrap_tzresult
    >>=? fun vstate ->
    finalize_validation_and_application vstate (Some header.shell)
    >|= Environment.wrap_tzresult
    >|=? fun (validation, result) -> (validation.context, result) )
  >|=? fun (context, result) ->
  let hash = Block_header.hash header in
  ({hash; header; operations; context}, result)

let apply header ?(operations = []) ?(allow_manager_failures = false) pred =
  apply_with_metadata
    header
    ~operations
    pred
    ~baking_mode:Application
    ~allow_manager_failures
  >>=? fun (t, _metadata) -> return t

let bake_with_metadata ?locked_round ?policy ?timestamp ?operation ?operations
    ?payload_round ?check_size ~baking_mode ?(allow_manager_failures = false)
    ?liquidity_baking_toggle_vote pred =
  let operations =
    match (operation, operations) with
    | Some op, Some ops -> Some (op :: ops)
    | Some op, None -> Some [op]
    | None, Some ops -> Some ops
    | None, None -> None
  in
  Forge.forge_header
    ?payload_round
    ?locked_round
    ?timestamp
    ?policy
    ?operations
    ?liquidity_baking_toggle_vote
    pred
  >>=? fun header ->
  Forge.sign_header header >>=? fun header ->
  apply_with_metadata
    ?policy
    ?check_size
    ~baking_mode
    ~allow_manager_failures
    header
    ?operations
    pred

let bake ?(baking_mode = Application) ?(allow_manager_failures = false)
    ?payload_round ?locked_round ?policy ?timestamp ?operation ?operations
    ?liquidity_baking_toggle_vote ?check_size pred =
  bake_with_metadata
    ?payload_round
    ~baking_mode
    ~allow_manager_failures
    ?locked_round
    ?policy
    ?timestamp
    ?operation
    ?operations
    ?liquidity_baking_toggle_vote
    ?check_size
    pred
  >>=? fun (t, (_metadata : block_header_metadata)) -> return t

(********** Cycles ****************)

(* This function is duplicated from Context to avoid a cyclic dependency *)
let get_constants b = Alpha_services.Constants.all rpc_ctxt b

let bake_n ?(baking_mode = Application) ?policy ?liquidity_baking_toggle_vote n
    b =
  List.fold_left_es
    (fun b _ -> bake ~baking_mode ?policy ?liquidity_baking_toggle_vote b)
    b
    (1 -- n)

let rec bake_while ?(baking_mode = Application) ?policy
    ?liquidity_baking_toggle_vote predicate b =
  let open Lwt_result_syntax in
  let* new_block = bake ~baking_mode ?policy ?liquidity_baking_toggle_vote b in
  if predicate new_block then
    (bake_while [@ocaml.tailcall])
      ~baking_mode
      ?policy
      ?liquidity_baking_toggle_vote
      predicate
      new_block
  else return b

let bake_until_level ?(baking_mode = Application) ?policy
    ?liquidity_baking_toggle_vote level b =
  bake_while
    ~baking_mode
    ?policy
    ?liquidity_baking_toggle_vote
    (fun b -> b.header.shell.level <= Raw_level.to_int32 level)
    b

let bake_n_with_all_balance_updates ?(baking_mode = Application) ?policy
    ?liquidity_baking_toggle_vote n b =
  List.fold_left_es
    (fun (b, balance_updates_rev) _ ->
      bake_with_metadata ~baking_mode ?policy ?liquidity_baking_toggle_vote b
      >>=? fun (b, metadata) ->
      let balance_updates_rev =
        List.rev_append metadata.balance_updates balance_updates_rev
      in
      let balance_updates_rev =
        List.fold_left
          (fun balance_updates_rev ->
            let open Apply_results in
            fun (Successful_manager_result r) ->
              match r with
              | Transaction_result (Transaction_to_sc_rollup_result _)
              | Reveal_result _ | Delegation_result _
              | Update_consensus_key_result _ | Set_deposits_limit_result _
              | Transfer_ticket_result _ | Dal_publish_slot_header_result _
              | Sc_rollup_originate_result _ | Sc_rollup_add_messages_result _
              | Sc_rollup_cement_result _ | Sc_rollup_publish_result _
              | Sc_rollup_refute_result _ | Sc_rollup_timeout_result _
              | Sc_rollup_execute_outbox_message_result _
              | Sc_rollup_recover_bond_result _ | Zk_rollup_origination_result _
              | Zk_rollup_publish_result _ | Zk_rollup_update_result _ ->
                  balance_updates_rev
              | Transaction_result
                  ( Transaction_to_contract_result {balance_updates; _}
                  | Transaction_to_tx_rollup_result {balance_updates; _}
                  | Transaction_to_zk_rollup_result {balance_updates; _} )
              | Origination_result {balance_updates; _}
              | Register_global_constant_result {balance_updates; _}
              | Increase_paid_storage_result {balance_updates; _} ->
                  List.rev_append balance_updates balance_updates_rev)
          balance_updates_rev
          metadata.implicit_operations_results
      in
      return (b, balance_updates_rev))
    (b, [])
    (1 -- n)
  >|=? fun (b, balance_updates_rev) -> (b, List.rev balance_updates_rev)

let bake_n_with_origination_results ?(baking_mode = Application) ?policy n b =
  List.fold_left_es
    (fun (b, origination_results_rev) _ ->
      bake_with_metadata ~baking_mode ?policy b >>=? fun (b, metadata) ->
      let origination_results_rev =
        List.fold_left
          (fun origination_results_rev ->
            let open Apply_results in
            function
            | Successful_manager_result (Reveal_result _)
            | Successful_manager_result (Delegation_result _)
            | Successful_manager_result (Update_consensus_key_result _)
            | Successful_manager_result (Transaction_result _)
            | Successful_manager_result (Register_global_constant_result _)
            | Successful_manager_result (Set_deposits_limit_result _)
            | Successful_manager_result (Increase_paid_storage_result _)
            | Successful_manager_result (Transfer_ticket_result _)
            | Successful_manager_result (Dal_publish_slot_header_result _)
            | Successful_manager_result (Sc_rollup_originate_result _)
            | Successful_manager_result (Sc_rollup_add_messages_result _)
            | Successful_manager_result (Sc_rollup_cement_result _)
            | Successful_manager_result (Sc_rollup_publish_result _)
            | Successful_manager_result (Sc_rollup_refute_result _)
            | Successful_manager_result (Sc_rollup_timeout_result _)
            | Successful_manager_result
                (Sc_rollup_execute_outbox_message_result _)
            | Successful_manager_result (Sc_rollup_recover_bond_result _)
            | Successful_manager_result (Zk_rollup_origination_result _)
            | Successful_manager_result (Zk_rollup_publish_result _)
            | Successful_manager_result (Zk_rollup_update_result _) ->
                origination_results_rev
            | Successful_manager_result (Origination_result x) ->
                Origination_result x :: origination_results_rev)
          origination_results_rev
          metadata.implicit_operations_results
      in
      return (b, origination_results_rev))
    (b, [])
    (1 -- n)
  >|=? fun (b, origination_results_rev) -> (b, List.rev origination_results_rev)

let bake_n_with_liquidity_baking_toggle_ema ?(baking_mode = Application) ?policy
    ?liquidity_baking_toggle_vote n b =
  let initial_ema = Liquidity_baking.Toggle_EMA.zero in
  List.fold_left_es
    (fun (b, _toggle_ema) _ ->
      bake_with_metadata ~baking_mode ?policy ?liquidity_baking_toggle_vote b
      >|=? fun (b, metadata) -> (b, metadata.liquidity_baking_toggle_ema))
    (b, initial_ema)
    (1 -- n)

let bake_until_cycle_end ?policy b =
  get_constants b >>=? fun Constants.{parametric = {blocks_per_cycle; _}; _} ->
  let current_level = b.header.shell.level in
  let current_level = Int32.rem current_level blocks_per_cycle in
  let delta = Int32.sub blocks_per_cycle current_level in
  bake_n ?policy (Int32.to_int delta) b

let bake_until_n_cycle_end ?policy n b =
  List.fold_left_es (fun b _ -> bake_until_cycle_end ?policy b) b (1 -- n)

let current_cycle b =
  get_constants b >>=? fun Constants.{parametric = {blocks_per_cycle; _}; _} ->
  let current_level = b.header.shell.level in
  let current_cycle = Int32.div current_level blocks_per_cycle in
  let current_cycle = Cycle.add Cycle.root (Int32.to_int current_cycle) in
  return current_cycle

let bake_until_cycle ?policy cycle (b : t) =
  let rec loop (b : t) =
    current_cycle b >>=? fun current_cycle ->
    if Cycle.equal cycle current_cycle then return b
    else bake_until_cycle_end ?policy b >>=? fun b -> loop b
  in
  loop b
OCaml

Innovation. Community. Security.