From 6e05ac1937507c48e09e82f2749ebedf337cb5af Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Fri, 8 Apr 2022 18:25:41 +0200 Subject: [PATCH 1/6] Proto/Tests: change init2 return value Wrap the two contracts in a pair to look more like `init` signature --- .../lib_protocol/test/helpers/context.ml | 2 +- .../lib_protocol/test/helpers/context.mli | 3 ++- .../test_global_constants_storage.ml | 2 +- .../integration/operations/test_transfer.ml | 20 +++++++++---------- .../integration/operations/test_tx_rollup.ml | 12 +++++------ 5 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.ml b/src/proto_alpha/lib_protocol/test/helpers/context.ml index 036c51671537..a020e02e4dd6 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/context.ml @@ -443,7 +443,7 @@ let init2 ?rng_state ?commitments ?(initial_balances = []) ?consensus_threshold 2 >|=? function | (_, []) | (_, [_]) -> assert false - | (b, contract_1 :: contract_2 :: _) -> (b, contract_1, contract_2) + | (b, contract_1 :: contract_2 :: _) -> (b, (contract_1, contract_2)) let init_with_constants constants n = let accounts = Account.generate_accounts n in diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.mli b/src/proto_alpha/lib_protocol/test/helpers/context.mli index 3a811002d3a6..776d00429573 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/context.mli @@ -271,7 +271,8 @@ val init2 : ?tx_rollup_sunset_level:int32 -> ?sc_rollup_enable:bool -> unit -> - (Block.t * Alpha_context.Contract.t * Alpha_context.Contract.t) tzresult Lwt.t + (Block.t * (Alpha_context.Contract.t * Alpha_context.Contract.t)) tzresult + Lwt.t val init_with_constants : Constants.parametric -> diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_global_constants_storage.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_global_constants_storage.ml index dd34a69fa667..3d384948ab29 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_global_constants_storage.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_global_constants_storage.ml @@ -60,7 +60,7 @@ let expr_to_hash expr = that values written to the global table of constants persist across blocks. *) let get_happy_path () = - Context.init2 () >>=? fun (b, alice, bob) -> + Context.init2 () >>=? fun (b, (alice, bob)) -> Incremental.begin_construction b >>=? fun b -> let expr_str = "Pair 3 7" in let expr = Expr.from_string expr_str in diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_transfer.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_transfer.ml index 79ff3bd90cc6..d84bcbf92392 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_transfer.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_transfer.ml @@ -79,7 +79,7 @@ let two_over_n_of_balance incr contract n = (********************) let single_transfer ?fee ?expect_failure amount = - Context.init2 () >>=? fun (b, contract_1, contract_2) -> + Context.init2 () >>=? fun (b, (contract_1, contract_2)) -> Incremental.begin_construction b >>=? fun b -> transfer_and_check_balances ~loc:__LOC__ @@ -143,7 +143,7 @@ let test_transfer_to_originate_with_fee () = (** Transfer from balance. *) let test_transfer_amount_of_contract_balance () = - Context.init2 () >>=? fun (b, contract_1, contract_2) -> + Context.init2 () >>=? fun (b, (contract_1, contract_2)) -> Context.Contract.pkh contract_1 >>=? fun pkh1 -> (* given that contract_1 no longer has a sufficient balance to bake, make sure it cannot be chosen as baker *) @@ -173,7 +173,7 @@ let test_transfers_to_self () = (** Forgot to add the valid transaction into the block. *) let test_missing_transaction () = - Context.init2 () >>=? fun (b, contract_1, contract_2) -> + Context.init2 () >>=? fun (b, (contract_1, contract_2)) -> (* given that contract_1 no longer has a sufficient balance to bake, make sure it cannot be chosen as baker *) Context.Contract.pkh contract_1 >>=? fun pkh1 -> @@ -318,7 +318,7 @@ let test_transfer_from_implicit_to_originated_contract () = (********************) let multiple_transfer n ?fee amount = - Context.init2 () >>=? fun (b, contract_1, contract_2) -> + Context.init2 () >>=? fun (b, (contract_1, contract_2)) -> Incremental.begin_construction b >>=? fun b -> n_transactions n b ?fee contract_1 contract_2 amount >>=? fun b -> Incremental.finalize_block b >>=? fun _ -> return_unit @@ -366,7 +366,7 @@ let test_block_with_multiple_transfers_with_without_fee () = (** Build a chain that has 10 blocks. *) let test_build_a_chain () = Context.init2 ~consensus_threshold:0 () - >>=? fun (b, contract_1, contract_2) -> + >>=? fun (b, (contract_1, contract_2)) -> let ten = of_int 10 in List.fold_left_es (fun b _ -> @@ -396,7 +396,7 @@ let test_empty_implicit () = (** Balance is too low to transfer. *) let test_balance_too_low fee () = - Context.init2 () >>=? fun (b, contract_1, contract_2) -> + Context.init2 () >>=? fun (b, (contract_1, contract_2)) -> Incremental.begin_construction b >>=? fun i -> Context.Contract.balance (I i) contract_1 >>=? fun balance1 -> Context.Contract.balance (I i) contract_2 >>=? fun balance2 -> @@ -475,7 +475,7 @@ let test_balance_too_low_two_transfers fee () = (** The counter is already used for the previous operation. *) let invalid_counter () = - Context.init2 () >>=? fun (b, contract_1, contract_2) -> + Context.init2 () >>=? fun (b, (contract_1, contract_2)) -> Incremental.begin_construction b >>=? fun b -> Op.transaction (I b) contract_1 contract_2 Tez.one >>=? fun op1 -> Op.transaction (I b) contract_1 contract_2 Tez.one >>=? fun op2 -> @@ -489,7 +489,7 @@ let invalid_counter () = (** Same as before but through a different way to perform this error. *) let test_add_the_same_operation_twice () = - Context.init2 () >>=? fun (b, contract_1, contract_2) -> + Context.init2 () >>=? fun (b, (contract_1, contract_2)) -> Incremental.begin_construction b >>=? fun b -> transfer_and_check_balances ~loc:__LOC__ b contract_1 contract_2 ten_tez >>=? fun (b, op_transfer) -> @@ -502,7 +502,7 @@ let test_add_the_same_operation_twice () = (** The counter is in the future *) let invalid_counter_in_the_future () = - Context.init2 () >>=? fun (b, contract_1, contract_2) -> + Context.init2 () >>=? fun (b, (contract_1, contract_2)) -> Incremental.begin_construction b >>=? fun b -> Context.Contract.counter (I b) contract_1 >>=? fun cpt -> let counter = Z.add cpt (Z.of_int 10) in @@ -515,7 +515,7 @@ let invalid_counter_in_the_future () = (** Check ownership. *) let test_ownership_sender () = - Context.init2 () >>=? fun (b, contract_1, contract_2) -> + Context.init2 () >>=? fun (b, (contract_1, contract_2)) -> Incremental.begin_construction b >>=? fun b -> (* get the manager of the contract_1 as a sender *) Context.Contract.manager (I b) contract_1 >>=? fun manager -> diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml index 2ae860039dfb..d6c21dec0270 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml @@ -257,7 +257,7 @@ let context_init2 ?tx_rollup_max_inboxes_count ?tx_rollup_hard_size_limit_per_message 2 >|=? function - | (b, contract_1 :: contract_2 :: _) -> (b, contract_1, contract_2) + | (b, contract_1 :: contract_2 :: _) -> (b, (contract_1, contract_2)) | (_, _) -> assert false (** [originate b contract] originates a tx_rollup from [contract], @@ -1558,7 +1558,7 @@ let test_finalization () = level, and ensures that this fails. It adds a commitment with the wrong batch count and ensures that that fails. *) let test_commitment_duplication () = - context_init2 () >>=? fun (b, contract1, contract2) -> + context_init2 () >>=? fun (b, (contract1, contract2)) -> let pkh1 = is_implicit_exn contract1 in originate b contract1 >>=? fun (b, tx_rollup) -> Context.Contract.balance (B b) contract1 >>=? fun balance -> @@ -1647,7 +1647,7 @@ let test_commitment_duplication () = return () let test_commit_current_inbox () = - context_init2 () >>=? fun (b, contract1, contract2) -> + context_init2 () >>=? fun (b, (contract1, contract2)) -> originate b contract1 >>=? fun (b, tx_rollup) -> (* In order to have a permissible commitment, we need a transaction. *) Incremental.begin_construction b >>=? fun i -> @@ -2378,7 +2378,7 @@ module Rejection = struct let init_with_deposit ?tx_rollup_hard_size_limit_per_message addr = init_l2_store () >>= fun store -> context_init2 ?tx_rollup_hard_size_limit_per_message () - >>=? fun (b, account, account2) -> + >>=? fun (b, (account, account2)) -> originate b account >>=? fun (b, tx_rollup) -> make_deposit b tx_rollup account addr >>=? fun (b, (deposit, _), ticket_hash) -> @@ -2607,7 +2607,7 @@ module Rejection = struct let open Error_monad_operators in let (_, _, addr) = gen_l2_account () in init_l2_store () >>= fun store -> - context_init2 () >>=? fun (b, contract1, contract2) -> + context_init2 () >>=? fun (b, (contract1, contract2)) -> originate b contract1 >>=? fun (b, tx_rollup) -> make_deposit b tx_rollup contract1 addr >>=? fun (b, (deposit_message, _), _ticket_hash) -> @@ -3558,7 +3558,7 @@ end rejecting commitments. *) let test_state () = let open Single_message_inbox in - context_init2 () >>=? fun (b, account1, account2) -> + context_init2 () >>=? fun (b, (account1, account2)) -> originate b account1 >>=? fun (b, tx_rollup) -> (* Submit bogus message three time to have three inboxes *) submit b tx_rollup account1 >>=? fun b -> -- GitLab From 31dfd89880756955768170f111bc452ca3542f7b Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Fri, 8 Apr 2022 19:03:30 +0200 Subject: [PATCH 2/6] Proto/Tests: simplify calls to Context.init Look at context.mli first. init1, init2, init3 are used whenever possible. init is renamed into init_n to discourage its usage. --- .../lib_benchmark/execution_context.ml | 3 +- .../lib_benchmark/test/test_helpers.ml | 3 +- .../lib_plugin/test/test_plugin.ml | 4 +- .../lib_protocol/test/helpers/context.ml | 81 ++------- .../lib_protocol/test/helpers/context.mli | 63 ++----- .../test/helpers/contract_helpers.ml | 7 +- .../test/helpers/liquidity_baking_machine.ml | 7 +- .../test/helpers/sapling_helpers.ml | 2 +- .../test/helpers/tx_rollup_l2_helpers.ml | 2 +- .../test/integration/consensus/test_baking.ml | 20 +-- .../consensus/test_deactivation.ml | 14 +- .../integration/consensus/test_delegation.ml | 159 ++++-------------- .../consensus/test_double_baking.ml | 38 ++--- .../consensus/test_double_endorsement.ml | 16 +- .../consensus/test_double_preendorsement.ml | 7 +- .../integration/consensus/test_endorsement.ml | 22 +-- .../consensus/test_helpers_rpcs.ml | 7 +- .../consensus/test_participation.ml | 7 +- .../consensus/test_preendorsement.ml | 2 +- .../consensus/test_preendorsement_functor.ml | 2 +- .../test/integration/consensus/test_seed.ml | 6 +- .../test/integration/gas/test_gas_levels.ml | 5 +- .../integration/michelson/test_annotations.ml | 3 +- .../test_global_constants_storage.ml | 5 - .../michelson/test_interpretation.ml | 2 +- .../michelson/test_patched_contracts.ml | 2 +- .../integration/michelson/test_sapling.ml | 14 +- .../michelson/test_ticket_accounting.ml | 8 +- .../michelson/test_ticket_balance_key.ml | 2 +- .../test_ticket_lazy_storage_diff.ml | 3 +- .../michelson/test_ticket_operations_diff.ml | 7 +- .../michelson/test_ticket_scanner.ml | 5 +- .../michelson/test_ticket_storage.ml | 2 +- .../integration/michelson/test_timelock.ml | 3 +- .../michelson/test_typechecking.ml | 7 +- .../integration/operations/test_activation.ml | 35 ++-- .../operations/test_combined_operations.ml | 113 +++++-------- .../operations/test_failing_noop.ml | 9 +- .../operations/test_origination.ml | 26 +-- .../integration/operations/test_reveal.ml | 12 +- .../integration/operations/test_sc_rollup.ml | 5 +- .../integration/operations/test_transfer.ml | 61 ++----- .../integration/operations/test_tx_rollup.ml | 14 +- .../integration/operations/test_voting.ml | 25 +-- .../test/integration/test_frozen_bonds.ml | 17 +- .../test/integration/test_liquidity_baking.ml | 34 ++-- .../test/pbt/test_carbonated_map.ml | 2 +- .../test/pbt/test_gas_properties.ml | 2 +- .../test/pbt/test_script_comparison.ml | 2 +- .../lib_protocol/test/unit/test_gas_monad.ml | 2 +- .../test/unit/test_sc_rollup_arith.ml | 2 +- .../test/unit/test_sc_rollup_inbox.ml | 2 +- .../test/unit/test_sc_rollup_storage.ml | 2 +- .../test/unit/test_tx_rollup_l2.ml | 2 +- 54 files changed, 302 insertions(+), 605 deletions(-) diff --git a/src/proto_alpha/lib_benchmark/execution_context.ml b/src/proto_alpha/lib_benchmark/execution_context.ml index 747318a66901..39df9a2c54f0 100644 --- a/src/proto_alpha/lib_benchmark/execution_context.ml +++ b/src/proto_alpha/lib_benchmark/execution_context.ml @@ -30,7 +30,7 @@ type context = Alpha_context.context * Script_interpreter.step_constants let initial_balance = 4_000_000_000_000L let context_init_memory ~rng_state = - Context.init + Context.init_n ~rng_state ~initial_balances: [ @@ -41,6 +41,7 @@ let context_init_memory ~rng_state = initial_balance; ] 5 + () >>=? fun (block, accounts) -> match accounts with | [bs1; bs2; bs3; bs4; bs5] -> diff --git a/src/proto_alpha/lib_benchmark/test/test_helpers.ml b/src/proto_alpha/lib_benchmark/test/test_helpers.ml index 189147add733..5baf5e4ce0fd 100644 --- a/src/proto_alpha/lib_benchmark/test/test_helpers.ml +++ b/src/proto_alpha/lib_benchmark/test/test_helpers.ml @@ -41,7 +41,7 @@ let print_script_expr_list fmtr (exprs : Protocol.Script_repr.expr list) = let typecheck_by_tezos = let context_init_memory ~rng_state = - Context.init + Context.init_n ~rng_state ~initial_balances: [ @@ -52,6 +52,7 @@ let typecheck_by_tezos = 4_000_000_000_000L; ] 5 + () >>=? fun (block, _accounts) -> Context.get_constants (B block) >>=? fun csts -> let minimal_block_delay = diff --git a/src/proto_alpha/lib_plugin/test/test_plugin.ml b/src/proto_alpha/lib_plugin/test/test_plugin.ml index 6f634d775d50..907ef219418b 100644 --- a/src/proto_alpha/lib_plugin/test/test_plugin.ml +++ b/src/proto_alpha/lib_plugin/test/test_plugin.ml @@ -52,10 +52,10 @@ let test_flush () = let filter_state = add_manager_restriction filter_state op_info.operation_hash op_info pkh in - Context.init 1 >>= function + Context.init1 () >>= function | Error e -> failwith "Error at context initialisation: %a" pp_print_trace e - | Ok (b, _contracts) -> ( + | Ok (b, _contract) -> ( let predecessor = Block_header. {shell = b.header.shell; protocol_data = Bytes.make 10 ' '} diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.ml b/src/proto_alpha/lib_protocol/test/helpers/context.ml index a020e02e4dd6..f6f4aa2bfd1c 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/context.ml @@ -354,12 +354,12 @@ module Tx_rollup = struct Tx_rollup_services.commitment rpc_ctxt ctxt tx_rollup end -let init ?rng_state ?commitments ?(initial_balances = []) ?consensus_threshold - ?min_proposal_quorum ?bootstrap_contracts ?level ?cost_per_byte - ?liquidity_baking_subsidy ?endorsing_reward_per_slot +let init_gen n get ?rng_state ?commitments ?(initial_balances = []) + ?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 ?tx_rollup_enable - ?tx_rollup_sunset_level ?tx_rollup_origination_size ?sc_rollup_enable n = + ?tx_rollup_sunset_level ?tx_rollup_origination_size ?sc_rollup_enable () = let accounts = Account.generate_accounts ?rng_state ~initial_balances n in let contracts = List.map @@ -385,65 +385,20 @@ let init ?rng_state ?commitments ?(initial_balances = []) ?consensus_threshold ?tx_rollup_origination_size ?sc_rollup_enable accounts - >|=? fun blk -> (blk, contracts) - -let init1 ?rng_state ?commitments ?(initial_balances = []) ?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 ?tx_rollup_enable ?tx_rollup_sunset_level - ?sc_rollup_enable () = - init - ?rng_state - ?commitments - ~initial_balances - ?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 - ?tx_rollup_enable - ?tx_rollup_sunset_level - ?sc_rollup_enable - 1 - >|=? function - | (_, []) -> assert false - | (b, contract_1 :: _) -> (b, contract_1) - -let init2 ?rng_state ?commitments ?(initial_balances = []) ?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 ?tx_rollup_enable ?tx_rollup_sunset_level - ?sc_rollup_enable () = - init - ?rng_state - ?commitments - ~initial_balances - ?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 - ?tx_rollup_enable - ?tx_rollup_sunset_level - ?sc_rollup_enable - 2 - >|=? function - | (_, []) | (_, [_]) -> assert false - | (b, contract_1 :: contract_2 :: _) -> (b, (contract_1, contract_2)) + >|=? fun blk -> (blk, get contracts) + +let init_n n = init_gen n (fun cs -> cs) + +let init1 = + init_gen 1 (function [contract_1] -> contract_1 | _ -> assert false) + +let init2 = + init_gen 2 (function + | [contract_1; contract_2] -> (contract_1, contract_2) + | _ -> assert false) + +let init3 = + init_gen 3 (function [c1; c2; c3] -> (c1, c2, c3) | _ -> assert false) let init_with_constants constants n = let accounts = Account.generate_accounts n in diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.mli b/src/proto_alpha/lib_protocol/test/helpers/context.mli index 776d00429573..2709a92c4316 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/context.mli @@ -202,9 +202,7 @@ module Tx_rollup : sig Tx_rollup_commitment.Submitted_commitment.t option tzresult Lwt.t end -(** [init n] : returns an initial block with [n] initialized accounts - and the associated implicit contracts *) -val init : +type 'accounts init := ?rng_state:Random.State.t -> ?commitments:Commitment.t list -> ?initial_balances:int64 list -> @@ -224,55 +222,28 @@ val init : ?tx_rollup_sunset_level:int32 -> ?tx_rollup_origination_size:int -> ?sc_rollup_enable:bool -> - int -> - (Block.t * Alpha_context.Contract.t list) tzresult Lwt.t + unit -> + (Block.t * 'accounts) tzresult Lwt.t + +(** [init_n n] : returns an initial block with [n] initialized accounts + and the associated implicit contracts *) +val init_n : int -> Alpha_context.Contract.t list init (** [init1] : returns an initial block with 1 initialized bootstrap account and the associated implicit contract *) -val init1 : - ?rng_state:Random.State.t -> - ?commitments:Commitment.t list -> - ?initial_balances:int64 list -> - ?consensus_threshold:int -> - ?min_proposal_quorum:int32 -> - ?level:int32 -> - ?cost_per_byte:Tez.t -> - ?liquidity_baking_subsidy:Tez.t -> - ?endorsing_reward_per_slot:Tez.t -> - ?baking_reward_bonus_per_slot:Tez.t -> - ?baking_reward_fixed_portion:Tez.t -> - ?origination_size:int -> - ?blocks_per_cycle:int32 -> - ?cycles_per_voting_period:int32 -> - ?tx_rollup_enable:bool -> - ?tx_rollup_sunset_level:int32 -> - ?sc_rollup_enable:bool -> - unit -> - (Block.t * Alpha_context.Contract.t) tzresult Lwt.t +val init1 : Alpha_context.Contract.t init (** [init2] : returns an initial block with 2 initialized bootstrap accounts and the associated implicit contracts *) -val init2 : - ?rng_state:Random.State.t -> - ?commitments:Commitment.t list -> - ?initial_balances:int64 list -> - ?consensus_threshold:int -> - ?min_proposal_quorum:int32 -> - ?level:int32 -> - ?cost_per_byte:Tez.t -> - ?liquidity_baking_subsidy:Tez.t -> - ?endorsing_reward_per_slot:Tez.t -> - ?baking_reward_bonus_per_slot:Tez.t -> - ?baking_reward_fixed_portion:Tez.t -> - ?origination_size:int -> - ?blocks_per_cycle:int32 -> - ?cycles_per_voting_period:int32 -> - ?tx_rollup_enable:bool -> - ?tx_rollup_sunset_level:int32 -> - ?sc_rollup_enable:bool -> - unit -> - (Block.t * (Alpha_context.Contract.t * Alpha_context.Contract.t)) tzresult - Lwt.t +val init2 : (Alpha_context.Contract.t * Alpha_context.Contract.t) init + +(** [init3] : returns an initial block with 3 initialized bootstrap accounts + and the associated implicit contracts *) +val init3 : + (Alpha_context.Contract.t + * Alpha_context.Contract.t + * Alpha_context.Contract.t) + init val init_with_constants : Constants.parametric -> diff --git a/src/proto_alpha/lib_protocol/test/helpers/contract_helpers.ml b/src/proto_alpha/lib_protocol/test/helpers/contract_helpers.ml index 503b632e6ae7..82a78014a6c7 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/contract_helpers.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/contract_helpers.ml @@ -30,12 +30,7 @@ open Error_monad_operators (** Initializes 2 addresses to do only operations plus one that will be used to bake. *) let init () = - Context.init ~consensus_threshold:0 3 >|=? fun (b, contracts) -> - let (src0, src1, src2) = - match contracts with - | src0 :: src1 :: src2 :: _ -> (src0, src1, src2) - | _ -> assert false - in + Context.init3 ~consensus_threshold:0 () >|=? fun (b, (src0, src1, src2)) -> let baker = match Alpha_context.Contract.is_implicit src0 with | Some v -> v diff --git a/src/proto_alpha/lib_protocol/test/helpers/liquidity_baking_machine.ml b/src/proto_alpha/lib_protocol/test/helpers/liquidity_baking_machine.ml index 1d7d4716c72a..5459e68cfd61 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/liquidity_baking_machine.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/liquidity_baking_machine.ml @@ -60,7 +60,7 @@ open Alpha_context automatically. 4. We introduce the [MACHINE_WITH_INIT] module type which is a superset of [MACHINE], extended with an [init] function - (analogous to {! Context.init}) to create an initial, mostly + (analogous to {! Context.init_n}) to create an initial, mostly blank state, and the [MachineBuilder.Make] functor that we can use to derive a machine with a [build] function. 5. We construct the [ConcreteMachine], that allows to @@ -387,7 +387,7 @@ let default_subsidy = let security_deposit = 640_000_000L -(* When calling [Context.init] with a list of initial balances, the +(* When calling [Context.init_n] with a list of initial balances, the sum of these balances should be equal to this constant. *) let total_xtz = 32_000_000_000_000L @@ -859,7 +859,7 @@ module ConcreteBaseMachine : let init ~invariant ?subsidy accounts_balances = let liquidity_baking_subsidy = Option.map Tez.of_mutez_exn subsidy in let (n, initial_balances) = initial_xtz_repartition accounts_balances in - Context.init + Context.init_n n ~consensus_threshold:0 ~initial_balances @@ -871,6 +871,7 @@ module ConcreteBaseMachine : ~blocks_per_cycle:10_000l ~cycles_per_voting_period:1l ?liquidity_baking_subsidy + () >>= function | (blk, holder :: accounts) -> let ctxt = Context.B blk in diff --git a/src/proto_alpha/lib_protocol/test/helpers/sapling_helpers.ml b/src/proto_alpha/lib_protocol/test/helpers/sapling_helpers.ml index b08dc98a604e..8339f0f73f9b 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/sapling_helpers.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/sapling_helpers.ml @@ -150,7 +150,7 @@ module Alpha_context_helpers = struct include Common let init () = - Context.init 1 >>=? fun (b, _) -> + Context.init1 () >>=? fun (b, _contract) -> Alpha_context.prepare b.context ~level:b.header.shell.level diff --git a/src/proto_alpha/lib_protocol/test/helpers/tx_rollup_l2_helpers.ml b/src/proto_alpha/lib_protocol/test/helpers/tx_rollup_l2_helpers.ml index c3869585dac8..496f781594cb 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/tx_rollup_l2_helpers.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/tx_rollup_l2_helpers.ml @@ -138,7 +138,7 @@ let gen_n_address n = let gen_n_ticket_hash n = let x = Lwt_main.run - ( Context.init n >>=? fun (_, contracts) -> + ( Context.init_n n () >>=? fun (_, contracts) -> let addressess = gen_n_address n in let tickets = List.map2 diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_baking.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_baking.ml index b978a4a24e82..cc083464c23c 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_baking.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_baking.ml @@ -50,7 +50,7 @@ open Alpha_context - Randomize the number of blocks baked after the n cycles baked previously. *) let test_cycle () = - Context.init ~consensus_threshold:0 5 >>=? fun (b, _) -> + Context.init_n ~consensus_threshold:0 5 () >>=? fun (b, _contracts) -> Context.get_constants (B b) >>=? fun csts -> let blocks_per_cycle = csts.parametric.blocks_per_cycle in let pp fmt x = Format.fprintf fmt "%ld" x in @@ -82,7 +82,7 @@ let wrap et = et >>= fun e -> Lwt.return (Environment.wrap_tzresult e) let test_bake_n_cycles n () = let open Block in let policy = By_round 0 in - Context.init ~consensus_threshold:0 1 >>=? fun (block, _contracts) -> + Context.init1 ~consensus_threshold:0 () >>=? fun (block, _contract) -> Block.bake_until_n_cycle_end ~policy n block >>=? fun _block -> return () (** Check that, after one or two voting periods, the voting power of a baker is @@ -91,7 +91,7 @@ let test_bake_n_cycles n () = let test_voting_power_cache () = let open Block in let policy = By_round 0 in - Context.init ~consensus_threshold:0 1 >>=? fun (genesis, _contracts) -> + Context.init1 ~consensus_threshold:0 () >>=? fun (genesis, _contract) -> Context.get_constants (B genesis) >>=? fun csts -> let blocks_per_voting_period = Int32.( @@ -152,9 +152,8 @@ let test_voting_power_cache () = (** test that after baking, one gets the baking reward fixed portion. *) let test_basic_baking_reward () = - Context.init ~consensus_threshold:0 1 >>=? fun (genesis, contracts) -> + Context.init1 ~consensus_threshold:0 () >>=? fun (genesis, baker) -> Block.bake genesis >>=? fun b -> - let baker = WithExceptions.Option.get ~loc:__LOC__ @@ List.hd contracts in Context.Contract.pkh baker >>=? fun baker_pkh -> Context.Contract.balance (B b) baker >>=? fun bal -> Context.Delegate.current_frozen_deposits (B b) baker_pkh @@ -189,7 +188,7 @@ let get_contract_for_pkh contracts pkh = [b2'] is build on top of [b1] by a different baker, using the same payload as [b2]. *) let test_rewards_block_and_payload_producer () = - Context.init ~consensus_threshold:1 10 >>=? fun (genesis, contracts) -> + Context.init_n ~consensus_threshold:1 10 () >>=? fun (genesis, contracts) -> Context.get_baker (B genesis) ~round:0 >>=? fun baker_b1 -> get_contract_for_pkh contracts baker_b1 >>=? fun baker_b1_contract -> Block.bake ~policy:(By_round 0) genesis >>=? fun b1 -> @@ -303,7 +302,7 @@ let test_rewards_block_and_payload_producer () = - a delegate that has no active stake cannot bake. *) let test_enough_active_stake_to_bake ~has_active_stake () = - Context.init 1 >>=? fun (b_for_constants, _) -> + Context.init1 () >>=? fun (b_for_constants, _contract) -> Context.get_constants (B b_for_constants) >>=? fun Constants.{parametric = {tokens_per_roll; _}; _} -> let tpr = Tez.to_mutez tokens_per_roll in @@ -312,11 +311,8 @@ let test_enough_active_stake_to_bake ~has_active_stake () = active balance is less or equal the staking balance (see [Delegate_storage.select_distribution_for_cycle]). *) let initial_bal1 = if has_active_stake then tpr else Int64.sub tpr 1L in - Context.init ~initial_balances:[initial_bal1; tpr] ~consensus_threshold:0 2 - >>=? fun (b0, accounts) -> - let (account1, _account2) = - match accounts with a1 :: a2 :: _ -> (a1, a2) | _ -> assert false - in + Context.init2 ~initial_balances:[initial_bal1; tpr] ~consensus_threshold:0 () + >>=? fun (b0, (account1, _account2)) -> Context.Contract.pkh account1 >>=? fun pkh1 -> Context.get_constants (B b0) >>=? fun Constants.{parametric = {baking_reward_fixed_portion; _}; _} -> diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_deactivation.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_deactivation.ml index 7c5218e60587..2c9daffd2ef7 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_deactivation.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_deactivation.ml @@ -39,8 +39,6 @@ open Protocol open Alpha_context open Test_tez -let account_pair = function [a1; a2] -> (a1, a2) | _ -> assert false - let wrap e = Lwt.return (Environment.wrap_tzresult e) (** Check that [Delegate.staking_balance] is the same as [Delegate.full_balance] @@ -87,8 +85,7 @@ let check_no_stake ~loc (b : Block.t) (account : Account.t) = own balance, and that its staking rights are consistent (check_stake). *) let test_simple_staking_rights () = - Context.init 2 >>=? fun (b, accounts) -> - let (a1, _a2) = account_pair accounts in + Context.init2 () >>=? fun (b, (a1, _a2)) -> Context.Contract.balance (B b) a1 >>=? fun balance -> Context.Contract.pkh a1 >>=? fun delegate1 -> Context.Delegate.current_frozen_deposits (B b) delegate1 @@ -110,8 +107,7 @@ let test_simple_staking_rights () = equals to its balance. Then both accounts have consistent staking rights. *) let test_simple_staking_rights_after_baking () = - Context.init ~consensus_threshold:0 2 >>=? fun (b, accounts) -> - let (a1, a2) = account_pair accounts in + Context.init2 ~consensus_threshold:0 () >>=? fun (b, (a1, a2)) -> Context.Contract.manager (B b) a1 >>=? fun m1 -> Context.Contract.manager (B b) a2 >>=? fun m2 -> Block.bake_n ~policy:(By_account m2.pkh) 5 b >>=? fun b -> @@ -130,8 +126,7 @@ let check_active_staking_balance ~loc ~deactivated b (m : Account.t) = if deactivated then check_no_stake ~loc b m else check_stake ~loc b m let run_until_deactivation () = - Context.init ~consensus_threshold:0 2 >>=? fun (b, accounts) -> - let (a1, a2) = account_pair accounts in + Context.init2 ~consensus_threshold:0 () >>=? fun (b, (a1, a2)) -> Context.Contract.balance (B b) a1 >>=? fun balance_start -> Context.Contract.manager (B b) a1 >>=? fun m1 -> Context.Contract.manager (B b) a2 >>=? fun m2 -> @@ -297,8 +292,7 @@ let test_deactivation_then_empty_then_self_delegation_then_recredit () = be activated. Again, consistency for baking rights are preserved for the first and third accounts. *) let test_delegation () = - Context.init ~consensus_threshold:0 2 >>=? fun (b, accounts) -> - let (a1, a2) = account_pair accounts in + Context.init2 ~consensus_threshold:0 () >>=? fun (b, (a1, a2)) -> let m3 = Account.new_account () in Account.add_account m3 ; Context.Contract.manager (B b) a1 >>=? fun m1 -> diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_delegation.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_delegation.ml index 2c65f914acbd..8696d98612e3 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_delegation.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_delegation.ml @@ -71,23 +71,14 @@ let expect_delegate_already_active_error i op = (** Bootstrap contracts delegate to themselves. *) let bootstrap_manager_is_bootstrap_delegate () = - Context.init 1 >>=? fun (b, bootstrap_contracts) -> - let bootstrap0 = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in + Context.init1 () >>=? fun (b, bootstrap0) -> Context.Contract.delegate (B b) bootstrap0 >>=? fun delegate0 -> Context.Contract.manager (B b) bootstrap0 >>=? fun manager0 -> Assert.equal_pkh ~loc:__LOC__ delegate0 manager0.pkh (** Bootstrap contracts cannot change their delegate. *) let bootstrap_delegate_cannot_change ~fee () = - Context.init 2 >>=? fun (b, bootstrap_contracts) -> - let bootstrap0 = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth bootstrap_contracts 0 - in - let bootstrap1 = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth bootstrap_contracts 1 - in + Context.init2 () >>=? fun (b, (bootstrap0, bootstrap1)) -> Context.Contract.pkh bootstrap0 >>=? fun pkh1 -> Incremental.begin_construction b ~policy:(Block.Excluding [pkh1]) >>=? fun i -> @@ -113,10 +104,7 @@ let bootstrap_delegate_cannot_change ~fee () = (** Bootstrap contracts cannot delete their delegation. *) let bootstrap_delegate_cannot_be_removed ~fee () = - Context.init 1 >>=? fun (b, bootstrap_contracts) -> - let bootstrap = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in + Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> Context.Contract.balance (I i) bootstrap >>=? fun balance -> Context.Contract.delegate (I i) bootstrap >>=? fun delegate -> @@ -139,13 +127,7 @@ let bootstrap_delegate_cannot_be_removed ~fee () = (** Contracts not registered as delegate can change their delegation. *) let delegate_can_be_changed_from_unregistered_contract ~fee () = - Context.init 2 >>=? fun (b, bootstrap_contracts) -> - let bootstrap0 = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in - let bootstrap1 = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth bootstrap_contracts 1 - in + Context.init2 () >>=? fun (b, (bootstrap0, bootstrap1)) -> let unregistered_account = Account.new_account () in let unregistered_pkh = Account.(unregistered_account.pkh) in let unregistered = Contract.implicit_contract unregistered_pkh in @@ -178,10 +160,7 @@ let delegate_can_be_changed_from_unregistered_contract ~fee () = (** Contracts not registered as delegate can delete their delegation. *) let delegate_can_be_removed_from_unregistered_contract ~fee () = - Context.init 1 >>=? fun (b, bootstrap_contracts) -> - let bootstrap = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in + Context.init1 () >>=? fun (b, bootstrap) -> let unregistered_account = Account.new_account () in let unregistered_pkh = Account.(unregistered_account.pkh) in let unregistered = Contract.implicit_contract unregistered_pkh in @@ -213,11 +192,8 @@ let delegate_can_be_removed_from_unregistered_contract ~fee () = (** Bootstrap keys are already registered as delegate keys. *) let bootstrap_manager_already_registered_delegate ~fee () = - Context.init 1 >>=? fun (b, bootstrap_contracts) -> + Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in Context.Contract.manager (I i) bootstrap >>=? fun manager -> let pkh = manager.pkh in let impl_contract = Contract.implicit_contract pkh in @@ -239,11 +215,8 @@ let bootstrap_manager_already_registered_delegate ~fee () = (** Bootstrap manager can be set as delegate of an originated contract (through origination operation). *) let delegate_to_bootstrap_by_origination ~fee () = - Context.init 1 >>=? fun (b, bootstrap_contracts) -> + Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in Context.Contract.manager (I i) bootstrap >>=? fun manager -> Context.Contract.balance (I i) bootstrap >>=? fun balance -> (* originate a contract with bootstrap's manager as delegate *) @@ -293,13 +266,13 @@ let delegate_to_bootstrap_by_origination ~fee () = Assert.balance_was_debited ~loc:__LOC__ (I i) bootstrap balance total_fee let undelegated_originated_bootstrap_contract () = - Context.init - 1 + Context.init1 ~bootstrap_contracts: [ Parameters.{delegate = None; amount = Tez.zero; script = Op.dummy_script}; ] - >>=? fun (b, _) -> + () + >>=? fun (b, _contract) -> Block.bake b >>=? fun b -> (* We know the address of the first originated bootstrap contract because we know the bootstrap origination nonce. This address corresponds to the first TF vesting contract on mainnnet. *) Lwt.return @@ Environment.wrap_tzresult @@ -450,11 +423,8 @@ let expect_unregistered_key pkh = function (unregistered key), and the fees are still debited. Using RPCs, we verify the contract has not been originated. *) let test_unregistered_delegate_key_init_origination ~fee () = - Context.init 1 >>=? fun (b, bootstrap_contracts) -> + Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in let unregistered_account = Account.new_account () in let unregistered_pkh = Account.(unregistered_account.pkh) in (* origination with delegate argument *) @@ -492,11 +462,8 @@ let test_unregistered_delegate_key_init_origination ~fee () = [Balance_too_low] is raised. Otherwise, fees are still debited. The implicit contract has no delegate. *) let test_unregistered_delegate_key_init_delegation ~fee () = - Context.init 1 >>=? fun (b, bootstrap_contracts) -> + Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in let unregistered_account = Account.new_account () in let unregistered_pkh = Account.(unregistered_account.pkh) in let impl_contract = Contract.implicit_contract unregistered_pkh in @@ -532,11 +499,8 @@ let test_unregistered_delegate_key_init_delegation ~fee () = raised. Otherwise, fees are not debited and the implicit contract delegate remains unchanged. *) let test_unregistered_delegate_key_switch_delegation ~fee () = - Context.init 1 >>=? fun (b, bootstrap_contracts) -> + Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in let bootstrap_pkh = Contract.is_implicit bootstrap |> WithExceptions.Option.get ~loc:__LOC__ in @@ -577,11 +541,8 @@ let test_unregistered_delegate_key_switch_delegation ~fee () = (** Same as [unregistered_delegate_key_init_origination] and credits [amount], no self-delegation. *) let test_unregistered_delegate_key_init_origination_credit ~fee ~amount () = - Context.init 1 >>=? fun (b, bootstrap_contracts) -> + Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in let unregistered_account = Account.new_account () in let unregistered_pkh = Account.(unregistered_account.pkh) in let impl_contract = Contract.implicit_contract unregistered_pkh in @@ -617,11 +578,8 @@ let test_unregistered_delegate_key_init_origination_credit ~fee ~amount () = (** Same as [unregistered_delegate_key_init_delegation] and credits the amount [amount] of the implicit contract. *) let test_unregistered_delegate_key_init_delegation_credit ~fee ~amount () = - Context.init 1 >>=? fun (b, bootstrap_contracts) -> + Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in let unregistered_account = Account.new_account () in let unregistered_pkh = Account.(unregistered_account.pkh) in let impl_contract = Contract.implicit_contract unregistered_pkh in @@ -660,11 +618,8 @@ let test_unregistered_delegate_key_init_delegation_credit ~fee ~amount () = (** Same as in [unregistered_delegate_key_switch_delegation] and credits the amount [amount] to the implicit contract. *) let test_unregistered_delegate_key_switch_delegation_credit ~fee ~amount () = - Context.init 1 >>=? fun (b, bootstrap_contracts) -> + Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in let bootstrap_pkh = Contract.is_implicit bootstrap |> WithExceptions.Option.get ~loc:__LOC__ in @@ -712,11 +667,8 @@ let test_unregistered_delegate_key_switch_delegation_credit ~fee ~amount () = no self-delegation. *) let test_unregistered_delegate_key_init_origination_credit_debit ~fee ~amount () = - Context.init 1 >>=? fun (b, bootstrap_contracts) -> + Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in let unregistered_account = Account.new_account () in let unregistered_pkh = Account.(unregistered_account.pkh) in let impl_contract = Contract.implicit_contract unregistered_pkh in @@ -757,11 +709,8 @@ let test_unregistered_delegate_key_init_origination_credit_debit ~fee ~amount () then debits the amount [amount] to the implicit contract. *) let test_unregistered_delegate_key_init_delegation_credit_debit ~amount ~fee () = - Context.init 1 >>=? fun (b, bootstrap_contracts) -> + Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in let unregistered_account = Account.new_account () in let unregistered_pkh = Account.(unregistered_account.pkh) in let impl_contract = Contract.implicit_contract unregistered_pkh in @@ -805,11 +754,8 @@ let test_unregistered_delegate_key_init_delegation_credit_debit ~amount ~fee () credits then debits the amount [amount] to the implicit contract. *) let test_unregistered_delegate_key_switch_delegation_credit_debit ~fee ~amount () = - Context.init 1 >>=? fun (b, bootstrap_contracts) -> + Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in let bootstrap_pkh = Contract.is_implicit bootstrap |> WithExceptions.Option.get ~loc:__LOC__ in @@ -860,7 +806,7 @@ let test_unregistered_delegate_key_switch_delegation_credit_debit ~fee ~amount (** Self-delegation with zero-balance contract should fail. *) let test_failed_self_delegation_no_transaction () = - Context.init 1 >>=? fun (b, _) -> + Context.init1 () >>=? fun (b, _contract) -> Incremental.begin_construction b >>=? fun i -> let account = Account.new_account () in let unregistered_pkh = Account.(account.pkh) in @@ -878,11 +824,8 @@ let test_failed_self_delegation_no_transaction () = is emptied). Self-delegation fails. *) let test_failed_self_delegation_emptied_implicit_contract amount () = (* create an implicit contract *) - Context.init 1 >>=? fun (b, bootstrap_contracts) -> + Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in let account = Account.new_account () in let unregistered_pkh = Account.(account.pkh) in let impl_contract = Contract.implicit_contract unregistered_pkh in @@ -906,11 +849,8 @@ let test_failed_self_delegation_emptied_implicit_contract amount () = tz, then it is delegated. The operation of debit of [amount] tz should fail as the contract is already delegated. *) let test_emptying_delegated_implicit_contract_fails amount () = - Context.init 1 >>=? fun (b, bootstrap_contracts) -> + Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in Context.Contract.manager (I i) bootstrap >>=? fun bootstrap_manager -> let account = Account.new_account () in let unregistered_pkh = Account.(account.pkh) in @@ -944,11 +884,8 @@ let test_emptying_delegated_implicit_contract_fails amount () = self-delegated. *) let test_valid_delegate_registration_init_delegation_credit amount () = (* create an implicit contract *) - Context.init 1 >>=? fun (b, bootstrap_contracts) -> + Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in let delegate_account = Account.new_account () in let delegate_pkh = Account.(delegate_account.pkh) in let impl_contract = Contract.implicit_contract delegate_pkh in @@ -990,11 +927,8 @@ let test_valid_delegate_registration_init_delegation_credit amount () = contract. *) let test_valid_delegate_registration_switch_delegation_credit amount () = (* create an implicit contract *) - Context.init 1 >>=? fun (b, bootstrap_contracts) -> + Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in let delegate_account = Account.new_account () in let delegate_pkh = Account.(delegate_account.pkh) in let impl_contract = Contract.implicit_contract delegate_pkh in @@ -1034,11 +968,8 @@ let test_valid_delegate_registration_switch_delegation_credit amount () = (** Create an implicit contract. *) let test_valid_delegate_registration_init_delegation_credit_debit amount () = (* create an implicit contract *) - Context.init 1 >>=? fun (b, bootstrap_contracts) -> + Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in let delegate_account = Account.new_account () in let delegate_pkh = Account.(delegate_account.pkh) in let impl_contract = Contract.implicit_contract delegate_pkh in @@ -1091,11 +1022,8 @@ let test_valid_delegate_registration_init_delegation_credit_debit amount () = be re-delegated to the latter contract. *) let test_valid_delegate_registration_switch_delegation_credit_debit amount () = (* create an implicit contract *) - Context.init 1 >>=? fun (b, bootstrap_contracts) -> + Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in let delegate_account = Account.new_account () in let delegate_pkh = Account.(delegate_account.pkh) in let impl_contract = Contract.implicit_contract delegate_pkh in @@ -1145,11 +1073,8 @@ let test_valid_delegate_registration_switch_delegation_credit_debit amount () = (** Second self-delegation should fail with implicit contract with some credit. *) let test_double_registration () = - Context.init 1 >>=? fun (b, bootstrap_contracts) -> + Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in let account = Account.new_account () in let pkh = Account.(account.pkh) in let impl_contract = Contract.implicit_contract pkh in @@ -1168,11 +1093,8 @@ let test_double_registration () = (** Second self-delegation should fail with implicit contract emptied after first self-delegation. *) let test_double_registration_when_empty () = - Context.init 1 >>=? fun (b, bootstrap_contracts) -> + Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in let account = Account.new_account () in let pkh = Account.(account.pkh) in let impl_contract = Contract.implicit_contract pkh in @@ -1196,11 +1118,8 @@ let test_double_registration_when_empty () = (** Second self-delegation should fail with implicit contract emptied then credited back after first self-delegation. *) let test_double_registration_when_recredited () = - Context.init 1 >>=? fun (b, bootstrap_contracts) -> + Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in let account = Account.new_account () in let pkh = Account.(account.pkh) in let impl_contract = Contract.implicit_contract pkh in @@ -1228,11 +1147,8 @@ let test_double_registration_when_recredited () = (** Self-delegation on unrevealed contract. *) let test_unregistered_and_unrevealed_self_delegate_key_init_delegation ~fee () = - Context.init 1 >>=? fun (b, bootstrap_contracts) -> + Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in let {Account.pkh; _} = Account.new_account () in let {Account.pkh = delegate_pkh; _} = Account.new_account () in let contract = Alpha_context.Contract.implicit_contract pkh in @@ -1252,11 +1168,8 @@ let test_unregistered_and_unrevealed_self_delegate_key_init_delegation ~fee () = (** Self-delegation on revealed but not registered contract. *) let test_unregistered_and_revealed_self_delegate_key_init_delegation ~fee () = - Context.init 1 >>=? fun (b, bootstrap_contracts) -> + Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in let {Account.pkh; pk; _} = Account.new_account () in let {Account.pkh = delegate_pkh; _} = Account.new_account () in let contract = Alpha_context.Contract.implicit_contract pkh in @@ -1278,11 +1191,8 @@ let test_unregistered_and_revealed_self_delegate_key_init_delegation ~fee () = (** Self-delegation emptying a fresh contract. *) let test_self_delegation_emptying_contract () = - Context.init 1 >>=? fun (b, bootstrap_contracts) -> + Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in let {Account.pkh; pk; _} = Account.new_account () in let {Account.pkh = delegate_pkh; _} = Account.new_account () in let contract = Alpha_context.Contract.implicit_contract pkh in @@ -1306,11 +1216,8 @@ let test_self_delegation_emptying_contract () = (** Self-delegation on revealed and registered contract. *) let test_registered_self_delegate_key_init_delegation () = - Context.init 1 >>=? fun (b, bootstrap_contracts) -> + Context.init1 () >>=? fun (b, bootstrap) -> Incremental.begin_construction b >>=? fun i -> - let bootstrap = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd bootstrap_contracts - in let {Account.pkh; _} = Account.new_account () in let {Account.pkh = delegate_pkh; pk = delegate_pk; _} = Account.new_account () diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_baking.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_baking.ml index 76ea9a0efb4d..5aacf11ca926 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_baking.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_baking.ml @@ -43,10 +43,7 @@ open Alpha_context (** Bake two block at the same level using the same policy (i.e. same baker). *) -let block_fork ?policy contracts b = - let (contract_a, contract_b) = - match contracts with x :: y :: _ -> (x, y) | _ -> assert false - in +let block_fork ?policy (contract_a, contract_b) b = Op.transaction (B b) contract_a contract_b Alpha_context.Tez.one_cent >>=? fun operation -> Block.bake ?policy ~operation b >>=? fun blk_a -> @@ -71,7 +68,7 @@ let double_baking ctxt ?(correct_order = true) bh1 bh2 = (** Simple scenario where two blocks are baked by a same baker and exposed by a double baking evidence operation. *) let test_valid_double_baking_evidence () = - Context.init ~consensus_threshold:0 2 >>=? fun (genesis, contracts) -> + Context.init2 ~consensus_threshold:0 () >>=? fun (genesis, contracts) -> Context.get_constants (B genesis) >>=? fun Constants.{parametric = {double_baking_punishment; _}; _} -> Context.get_first_different_bakers (B genesis) >>=? fun (baker1, baker2) -> @@ -111,7 +108,7 @@ let double_endorsement ctxt ?(correct_order = true) op1 op2 = Op.double_endorsement ctxt e1 e2 let test_valid_double_baking_followed_by_double_endorsing () = - Context.init ~consensus_threshold:0 2 >>=? fun (genesis, contracts) -> + Context.init2 ~consensus_threshold:0 () >>=? fun (genesis, contracts) -> Context.get_first_different_bakers (B genesis) >>=? fun (baker1, baker2) -> Block.bake genesis >>=? fun b -> Context.Delegate.current_frozen_deposits (B b) baker1 @@ -162,7 +159,7 @@ let block_fork_diff b = Block.bake ~policy:(By_account baker_2) b >|=? fun blk_b -> (blk_a, blk_b) let test_valid_double_endorsing_followed_by_double_baking () = - Context.init ~consensus_threshold:0 2 >>=? fun (genesis, contracts) -> + Context.init2 ~consensus_threshold:0 () >>=? fun (genesis, contracts) -> Context.get_first_different_bakers (B genesis) >>=? fun (baker1, baker2) -> block_fork_diff genesis >>=? fun (blk_1, blk_2) -> Context.Delegate.current_frozen_deposits (B genesis) baker1 @@ -213,7 +210,7 @@ let test_valid_double_endorsing_followed_by_double_baking () = baking evidence (and not the block producer, if different) receives the reward. *) let test_payload_producer_gets_evidence_rewards () = - Context.init ~consensus_threshold:0 10 >>=? fun (genesis, contracts) -> + Context.init_n ~consensus_threshold:0 10 () >>=? fun (genesis, contracts) -> Context.get_constants (B genesis) >>=? fun Constants. { @@ -222,7 +219,10 @@ let test_payload_producer_gets_evidence_rewards () = _; } -> Context.get_first_different_bakers (B genesis) >>=? fun (baker1, baker2) -> - block_fork ~policy:(By_account baker1) contracts genesis >>=? fun (b1, b2) -> + let c1_c2 = + match contracts with c1 :: c2 :: _ -> (c1, c2) | _ -> assert false + in + block_fork ~policy:(By_account baker1) c1_c2 genesis >>=? fun (b1, b2) -> double_baking (B b1) b1.header b2.header |> fun db_evidence -> Block.bake ~policy:(By_account baker2) ~operation:db_evidence b1 >>=? fun b_with_evidence -> @@ -287,7 +287,7 @@ let test_payload_producer_gets_evidence_rewards () = (** Check that a double baking operation fails if it exposes the same two blocks. *) let test_same_blocks () = - Context.init 2 >>=? fun (b, _contracts) -> + Context.init2 () >>=? fun (b, _contracts) -> Block.bake b >>=? fun ba -> double_baking (B ba) ba.header ba.header |> fun operation -> Block.bake ~operation ba >>= fun res -> @@ -296,7 +296,7 @@ let test_same_blocks () = (** Check that an double baking operation that is invalid due to incorrect ordering of the block headers fails. *) let test_incorrect_order () = - Context.init ~consensus_threshold:0 2 >>=? fun (genesis, contracts) -> + Context.init2 ~consensus_threshold:0 () >>=? fun (genesis, contracts) -> block_fork ~policy:(By_round 0) contracts genesis >>=? fun (blk_a, blk_b) -> double_baking (B genesis) ~correct_order:false blk_a.header blk_b.header |> fun operation -> @@ -306,7 +306,7 @@ let test_incorrect_order () = (** Check that a double baking operation exposing two blocks with different levels fails. *) let test_different_levels () = - Context.init ~consensus_threshold:0 2 >>=? fun (b, contracts) -> + Context.init2 ~consensus_threshold:0 () >>=? fun (b, contracts) -> block_fork ~policy:(By_round 0) contracts b >>=? fun (blk_a, blk_b) -> Block.bake blk_b >>=? fun blk_b_2 -> double_baking (B blk_a) blk_a.header blk_b_2.header |> fun operation -> @@ -316,7 +316,7 @@ let test_different_levels () = (** Check that a double baking operation exposing two yet-to-be-baked blocks fails. *) let test_too_early_double_baking_evidence () = - Context.init ~consensus_threshold:0 2 >>=? fun (genesis, contracts) -> + Context.init2 ~consensus_threshold:0 () >>=? fun (genesis, contracts) -> Block.bake_until_cycle_end genesis >>=? fun b -> block_fork ~policy:(By_round 0) contracts b >>=? fun (blk_a, blk_b) -> double_baking (B b) blk_a.header blk_b.header |> fun operation -> @@ -326,7 +326,7 @@ let test_too_early_double_baking_evidence () = (** Check that after [max_slashing_period * blocks_per_cycle + 1] blocks -- corresponding to 2 cycles --, it is not possible to create a double baking operation anymore. *) let test_too_late_double_baking_evidence () = - Context.init ~consensus_threshold:0 2 >>=? fun (b, contracts) -> + Context.init2 ~consensus_threshold:0 () >>=? fun (b, contracts) -> Context.get_constants (B b) >>=? fun Constants.{parametric = {max_slashing_period; _}; _} -> block_fork ~policy:(By_round 0) contracts b >>=? fun (blk_a, blk_b) -> @@ -339,7 +339,7 @@ let test_too_late_double_baking_evidence () = -- corresponding to 2 cycles --, it is still possible to create a double baking operation. *) let test_just_in_time_double_baking_evidence () = - Context.init ~consensus_threshold:0 2 >>=? fun (b, contracts) -> + Context.init2 ~consensus_threshold:0 () >>=? fun (b, contracts) -> Context.get_constants (B b) >>=? fun Constants.{parametric = {blocks_per_cycle; _}; _} -> block_fork ~policy:(By_round 0) contracts b >>=? fun (blk_a, blk_b) -> @@ -353,7 +353,7 @@ let test_just_in_time_double_baking_evidence () = (** Check that an invalid double baking evidence that exposes two block baking with same level made by different bakers fails. *) let test_different_delegates () = - Context.init 2 >>=? fun (b, _) -> + Context.init2 () >>=? fun (b, _contracts) -> Context.get_first_different_bakers (B b) >>=? fun (baker_1, baker_2) -> Block.bake ~policy:(By_account baker_1) b >>=? fun blk_a -> Block.bake ~policy:(By_account baker_2) b >>=? fun blk_b -> @@ -382,7 +382,7 @@ let test_wrong_signer () = >>=? fun header -> Block.Forge.set_baker baker_2 header |> Block.Forge.sign_header in - Context.init 2 >>=? fun (b, _) -> + Context.init2 () >>=? fun (b, _contracts) -> Context.get_first_different_bakers (B b) >>=? fun (baker_1, baker_2) -> Block.bake ~policy:(By_account baker_1) b >>=? fun blk_a -> let ts = Timestamp.of_seconds_string (Int64.to_string 10L) in @@ -397,8 +397,8 @@ let test_wrong_signer () = (** an evidence can only be accepted once (this also means that the same evidence doesn't lead to slashing the offender twice) *) let test_double_evidence () = - Context.init ~consensus_threshold:0 3 >>=? fun (blk, contracts) -> - block_fork contracts blk >>=? fun (blk_a, blk_b) -> + Context.init3 ~consensus_threshold:0 () >>=? fun (blk, (c1, c2, _c3)) -> + block_fork (c1, c2) blk >>=? fun (blk_a, blk_b) -> Block.bake_until_cycle_end blk_a >>=? fun blk -> double_baking (B blk) blk_a.header blk_b.header |> fun evidence -> Block.bake ~operation:evidence blk >>=? fun blk -> diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_endorsement.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_endorsement.ml index f4c22ef47f08..56a50dea5829 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_endorsement.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_endorsement.ml @@ -87,7 +87,7 @@ let double_endorsement ctxt ?(correct_order = true) op1 op2 = delegate and exposed by a double_endorsement operation. Also verify that punishment is operated. *) let test_valid_double_endorsement_evidence () = - Context.init ~consensus_threshold:0 2 >>=? fun (genesis, _) -> + Context.init2 ~consensus_threshold:0 () >>=? fun (genesis, _contracts) -> block_fork genesis >>=? fun (blk_1, blk_2) -> (* from blk_1 we bake blk_a and from blk_2 we bake blk_b so that the same delegate endorses blk_a and blk_b and these 2 form @@ -145,7 +145,7 @@ let test_valid_double_endorsement_evidence () = (** Say a delegate double-endorses twice and say the 2 evidences are timely included. Then the delegate can no longer bake. *) let test_two_double_endorsement_evidences_leadsto_no_bake () = - Context.init ~consensus_threshold:0 2 >>=? fun (genesis, _) -> + Context.init2 ~consensus_threshold:0 () >>=? fun (genesis, _contracts) -> block_fork genesis >>=? fun (blk_1, blk_2) -> Block.bake blk_1 >>=? fun blk_a -> Block.bake blk_2 >>=? fun blk_b -> @@ -183,7 +183,7 @@ let test_two_double_endorsement_evidences_leadsto_no_bake () = (** Check that an invalid double endorsement operation that exposes a valid endorsement fails. *) let test_invalid_double_endorsement () = - Context.init ~consensus_threshold:0 10 >>=? fun (genesis, _) -> + Context.init_n ~consensus_threshold:0 10 () >>=? fun (genesis, _contracts) -> Block.bake genesis >>=? fun b -> Op.endorsement ~endorsed_block:b (B genesis) () >>=? fun endorsement -> Block.bake ~operation:(Operation.pack endorsement) b >>=? fun b -> @@ -194,7 +194,7 @@ let test_invalid_double_endorsement () = (** Check that an double endorsement operation that is invalid due to incorrect ordering of the endorsements fails. *) let test_invalid_double_endorsement_variant () = - Context.init ~consensus_threshold:0 2 >>=? fun (genesis, _) -> + Context.init2 ~consensus_threshold:0 () >>=? fun (genesis, _contracts) -> Block.bake_until_cycle_end genesis >>=? fun b -> block_fork b >>=? fun (blk_1, blk_2) -> Block.bake blk_1 >>=? fun blk_a -> @@ -212,7 +212,7 @@ let test_invalid_double_endorsement_variant () = (** Check that a future-cycle double endorsement fails. *) let test_too_early_double_endorsement_evidence () = - Context.init ~consensus_threshold:0 2 >>=? fun (genesis, _) -> + Context.init2 ~consensus_threshold:0 () >>=? fun (genesis, _contracts) -> Block.bake_until_cycle_end genesis >>=? fun b -> block_fork b >>=? fun (blk_1, blk_2) -> Block.bake blk_1 >>=? fun blk_a -> @@ -226,7 +226,7 @@ let test_too_early_double_endorsement_evidence () = (** Check that after [max_slashing_period * blocks_per_cycle + 1], it is not possible to create a double_endorsement anymore. *) let test_too_late_double_endorsement_evidence () = - Context.init ~consensus_threshold:0 2 >>=? fun (genesis, _) -> + Context.init2 ~consensus_threshold:0 () >>=? fun (genesis, _contracts) -> Context.get_constants (B genesis) >>=? fun Constants. {parametric = {max_slashing_period; blocks_per_cycle; _}; _} -> @@ -244,7 +244,7 @@ let test_too_late_double_endorsement_evidence () = (** Check that an invalid double endorsement evidence that exposes two endorsements made by two different endorsers fails. *) let test_different_delegates () = - Context.init ~consensus_threshold:0 2 >>=? fun (genesis, _) -> + Context.init2 ~consensus_threshold:0 () >>=? fun (genesis, _contracts) -> Block.bake genesis >>=? fun genesis -> block_fork genesis >>=? fun (blk_1, blk_2) -> Block.bake blk_1 >>=? fun blk_a -> @@ -277,7 +277,7 @@ let test_different_delegates () = (** Check that a double endorsement evidence that exposes a ill-formed endorsement fails. *) let test_wrong_delegate () = - Context.init ~consensus_threshold:0 2 >>=? fun (genesis, _contracts) -> + Context.init2 ~consensus_threshold:0 () >>=? fun (genesis, _contracts) -> block_fork genesis >>=? fun (blk_1, blk_2) -> Block.bake blk_1 >>=? fun blk_a -> Block.bake blk_2 >>=? fun blk_b -> diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_preendorsement.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_preendorsement.ml index 047a1b79dc74..810c9d4e810c 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_preendorsement.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_preendorsement.ml @@ -69,7 +69,8 @@ end = struct ?(include_endorsement = false) ?(block_round = 0) ?(mk_evidence = fun ctxt p1 p2 -> Op.double_preendorsement ctxt p1 p2) ~loc () = - Context.init ~consensus_threshold:0 10 >>=? fun (genesis, _) -> + Context.init_n ~consensus_threshold:0 10 () + >>=? fun (genesis, _contracts) -> bake genesis >>=? fun b1 -> bake ~policy:(By_round 0) b1 >>=? fun b2_A -> Op.endorsement ~endorsed_block:b1 (B genesis) () >>=? fun e -> @@ -81,7 +82,7 @@ end = struct bake b1 ~operations:[op] >>= fun res -> invalid_denunciation loc res let max_slashing_period () = - Context.init ~consensus_threshold:0 1 >>=? fun (genesis, _) -> + Context.init1 ~consensus_threshold:0 () >>=? fun (genesis, _contract) -> Context.get_constants (B genesis) >>=? fun {parametric = {max_slashing_period; blocks_per_cycle; _}; _} -> return (max_slashing_period * Int32.to_int blocks_per_cycle) @@ -172,7 +173,7 @@ end = struct ?(pick_endorsers = fun ctxt -> pick_endorsers ctxt >>=? fun (a, _b) -> return (a, a)) ~loc () = - Context.init ~consensus_threshold:0 10 >>=? fun (genesis, contracts) -> + Context.init_n ~consensus_threshold:0 10 () >>=? fun (genesis, contracts) -> let addr = match List.hd contracts with None -> assert false | Some e -> e in diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_endorsement.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_endorsement.ml index 06a082b54e3c..292e00945721 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_endorsement.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_endorsement.ml @@ -38,7 +38,7 @@ open Protocol open Alpha_context let init_genesis ?policy () = - Context.init ~consensus_threshold:0 5 >>=? fun (genesis, _) -> + Context.init_n ~consensus_threshold:0 5 () >>=? fun (genesis, _contracts) -> Block.bake ?policy genesis >>=? fun b -> return (genesis, b) (** inject an endorsement and return the block with the endorsement and its @@ -62,7 +62,7 @@ let test_simple_endorsement () = (** Apply an endorsement with a negative slot. *) let test_negative_slot () = - Context.init 5 >>=? fun (genesis, _) -> + Context.init_n 5 () >>=? fun (genesis, _contracts) -> Block.bake genesis >>=? fun b -> Context.get_endorser (B b) >>=? fun (delegate, _slots) -> Lwt.catch @@ -80,7 +80,7 @@ let test_negative_slot () = (** Apply an endorsement with a non-normalized slot (that is, not the smallest possible). *) let test_non_normalized_slot () = - Context.init 5 >>=? fun (genesis, _) -> + Context.init_n 5 () >>=? fun (genesis, _contracts) -> Block.bake genesis >>=? fun b -> Context.get_endorsers (B b) >>=? fun endorsers_list -> (* find an endorsers with more than 1 slot *) @@ -312,7 +312,7 @@ let test_endorsement_threshold ~sufficient_threshold () = about 1 slot so we can get closer to the limit of [consensus_threshold]: we check that a block with endorsing power [consensus_threshold - 1] won't be baked. *) - Context.init 10 >>=? fun (genesis, _contracts) -> + Context.init_n 10 () >>=? fun (genesis, _contracts) -> Block.bake genesis >>=? fun b -> Context.get_constants (B b) >>=? fun {parametric = {consensus_threshold; _}; _} -> @@ -362,7 +362,7 @@ let test_fitness_gap () = Assert.equal_int32 ~loc:__LOC__ level_diff 1l let test_preendorsement_endorsement_same_level () = - Context.init ~consensus_threshold:0 1 >>=? fun (genesis, _) -> + Context.init1 ~consensus_threshold:0 () >>=? fun (genesis, _contract) -> Block.bake genesis >>=? fun b1 -> Incremental.begin_construction ~mempool_mode:true ~policy:(By_round 2) b1 >>=? fun i -> @@ -376,7 +376,7 @@ let test_preendorsement_endorsement_same_level () = (** Test for endorsement injection with wrong slot in mempool mode. This test is expected to fail *) let test_wrong_endorsement_slot_in_mempool_mode () = - Context.init ~consensus_threshold:1 5 >>=? fun (genesis, _) -> + Context.init_n ~consensus_threshold:1 5 () >>=? fun (genesis, _) -> Block.bake genesis >>=? fun b1 -> let module V = Plugin.RPC.Validators in (Context.get_endorsers (B b1) >>=? function @@ -409,7 +409,7 @@ let test_endorsement_for_next_round () = (** Endorsement of grandparent *) let test_endorsement_grandparent () = - Context.init ~consensus_threshold:0 1 >>=? fun (genesis, _) -> + Context.init1 ~consensus_threshold:0 () >>=? fun (genesis, _contract) -> Block.bake genesis >>=? fun b_gp -> Block.bake b_gp >>=? fun b -> Incremental.begin_construction ~mempool_mode:true b >>=? fun i -> @@ -425,7 +425,7 @@ let test_endorsement_grandparent () = (** Double inclusion of grandparent endorsement *) let test_double_endorsement_grandparent () = - Context.init ~consensus_threshold:0 1 >>=? fun (genesis, _) -> + Context.init1 ~consensus_threshold:0 () >>=? fun (genesis, _contract) -> Block.bake genesis >>=? fun b_gp -> Block.bake b_gp >>=? fun b -> Incremental.begin_construction ~mempool_mode:true b >>=? fun i -> @@ -448,7 +448,7 @@ let test_double_endorsement_grandparent () = (** Endorsement of grandparent on same slot as parent *) let test_endorsement_grandparent_same_slot () = - Context.init ~consensus_threshold:0 1 >>=? fun (genesis, _) -> + Context.init1 ~consensus_threshold:0 () >>=? fun (genesis, _contract) -> Block.bake genesis >>=? fun b_gp -> Block.bake b_gp >>=? fun b -> Incremental.begin_construction ~mempool_mode:true b >>=? fun i -> @@ -466,7 +466,7 @@ let test_endorsement_grandparent_same_slot () = (** Endorsement of grandparent in application mode should be rejected *) let test_endorsement_grandparent_application () = - Context.init ~consensus_threshold:0 1 >>=? fun (genesis, _) -> + Context.init1 ~consensus_threshold:0 () >>=? fun (genesis, _contract) -> Block.bake genesis >>=? fun b_gp -> Block.bake b_gp >>=? fun b -> Op.endorsement ~endorsed_block:b_gp (B genesis) () >>=? fun op -> @@ -478,7 +478,7 @@ let test_endorsement_grandparent_application () = (** Endorsement of grandparent in full construction mode should be rejected *) let test_endorsement_grandparent_full_construction () = - Context.init ~consensus_threshold:0 1 >>=? fun (genesis, _) -> + Context.init1 ~consensus_threshold:0 () >>=? fun (genesis, _contract) -> Block.bake genesis >>=? fun b_gp -> Block.bake b_gp >>=? fun b -> Incremental.begin_construction b >>=? fun i -> diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_helpers_rpcs.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_helpers_rpcs.ml index b70757335d55..78df0e7cc262 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_helpers_rpcs.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_helpers_rpcs.ml @@ -39,7 +39,7 @@ open Alpha_context Future levels or cycles are not tested because it's hard in this framework, using only RPCs, to fabricate them. *) let test_baking_rights () = - Context.init 2 >>=? fun (b, contracts) -> + Context.init2 () >>=? fun (b, (c1, _c2)) -> let open Plugin.RPC.Baking_rights in (* default max_round returns 65 results *) get Block.rpc_ctxt b ~all:true >>=? fun rights -> @@ -49,10 +49,7 @@ let test_baking_rights () = get Block.rpc_ctxt b ~all:true ~max_round >>=? fun rights -> assert (Compare.List_length_with.(rights = max_round + 1)) ; (* filtering by delegate *) - let d = - Option.bind (List.nth contracts 0) Contract.is_implicit - |> WithExceptions.Option.get ~loc:__LOC__ - in + let d = Contract.is_implicit c1 |> WithExceptions.Option.get ~loc:__LOC__ in get Block.rpc_ctxt b ~all:true ~delegates:[d] >>=? fun rights -> assert (List.for_all (fun {delegate; _} -> delegate = d) rights) ; (* filtering by cycle *) diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_participation.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_participation.ml index 64c36f2b1f67..0067b1b1902f 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_participation.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_participation.ml @@ -67,7 +67,7 @@ let bake_and_endorse_once (b_pred, b_cur) baker endorser = *) let test_participation ~sufficient_participation () = let n_accounts = 2 in - Context.init ~consensus_threshold:1 n_accounts >>=? fun (b0, accounts) -> + Context.init_n ~consensus_threshold:1 n_accounts () >>=? fun (b0, accounts) -> Context.get_constants (B b0) >>=? fun csts -> let blocks_per_cycle = Int32.to_int csts.parametric.blocks_per_cycle in let mpr = csts.parametric.minimal_participation_ratio in @@ -125,10 +125,7 @@ let test_participation ~sufficient_participation () = non-participating account. *) let test_participation_rpc () = let n_accounts = 2 in - Context.init ~consensus_threshold:1 n_accounts >>=? fun (b0, accounts) -> - let (account1, account2) = - match accounts with a1 :: a2 :: _ -> (a1, a2) | _ -> assert false - in + Context.init2 ~consensus_threshold:1 () >>=? fun (b0, (account1, account2)) -> Context.Contract.pkh account1 >>=? fun del1 -> Context.Contract.pkh account2 >>=? fun del2 -> Context.get_constants (B b0) >>=? fun csts -> diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_preendorsement.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_preendorsement.ml index 4c192060e655..5cb18b2483ff 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_preendorsement.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_preendorsement.ml @@ -39,7 +39,7 @@ open Alpha_context (****************************************************************) let init_genesis ?policy () = - Context.init ~consensus_threshold:0 5 >>=? fun (genesis, _) -> + Context.init_n ~consensus_threshold:0 5 () >>=? fun (genesis, _contracts) -> Block.bake ?policy genesis >>=? fun b -> return (genesis, b) (****************************************************************) diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_preendorsement_functor.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_preendorsement_functor.ml index 0478885ecae9..a1f93f13e021 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_preendorsement_functor.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_preendorsement_functor.ml @@ -57,7 +57,7 @@ end = struct ?(get_delegate_and_slot = fun _predpred _pred _curr -> return (None, None)) ?(post_process = Ok (fun _ -> return_unit)) ~loc () = - Context.init ~consensus_threshold:1 5 >>=? fun (genesis, _) -> + Context.init_n ~consensus_threshold:1 5 () >>=? fun (genesis, _contracts) -> bake genesis >>=? fun b1 -> Op.endorsement ~endorsed_block:b1 (B genesis) () >>=? fun endo -> let endo = Operation.pack endo in diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_seed.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_seed.ml index 90ce2028dd0a..6fcafa49efd3 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_seed.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_seed.ml @@ -39,7 +39,7 @@ open Protocol (** Baking [blocks_per_commitment] blocks without a [seed_nonce_hash] commitment fails with an "Invalid commitment in block header" error. *) let test_no_commitment () = - Context.init ~consensus_threshold:0 5 >>=? fun (b, _) -> + Context.init_n ~consensus_threshold:0 5 () >>=? fun (b, _contracts) -> Context.get_constants (B b) >>=? fun {parametric = {blocks_per_commitment; _}; _} -> let blocks_per_commitment = Int32.to_int blocks_per_commitment in @@ -63,7 +63,7 @@ let test_no_commitment () = - revealing twice produces an error *) let test_revelation_early_wrong_right_twice () = let open Assert in - Context.init ~consensus_threshold:0 5 >>=? fun (b, _) -> + Context.init_n ~consensus_threshold:0 5 () >>=? fun (b, _contracts) -> Context.get_constants (B b) >>=? fun csts -> let tip = csts.parametric.seed_nonce_revelation_tip in let blocks_per_commitment = @@ -145,7 +145,7 @@ let test_revelation_early_wrong_right_twice () = let test_revelation_missing_and_late () = let open Context in let open Assert in - Context.init ~consensus_threshold:0 5 >>=? fun (b, _) -> + Context.init_n ~consensus_threshold:0 5 () >>=? fun (b, _contracts) -> get_constants (B b) >>=? fun csts -> let blocks_per_commitment = Int32.to_int csts.parametric.blocks_per_commitment diff --git a/src/proto_alpha/lib_protocol/test/integration/gas/test_gas_levels.ml b/src/proto_alpha/lib_protocol/test/integration/gas/test_gas_levels.ml index e61e2e6db1b1..54fe48a4537b 100644 --- a/src/proto_alpha/lib_protocol/test/integration/gas/test_gas_levels.ml +++ b/src/proto_alpha/lib_protocol/test/integration/gas/test_gas_levels.ml @@ -49,7 +49,7 @@ let succeed x = match x with Ok _ -> true | _ -> false let failed x = not (succeed x) let dummy_context () = - Context.init ~consensus_threshold:0 1 >>=? fun (block, _) -> + Context.init1 ~consensus_threshold:0 () >>=? fun (block, _contract) -> Raw_context.prepare ~level:Int32.zero ~predecessor_timestamp:Time.Protocol.epoch @@ -255,8 +255,7 @@ let originate_contract block source script = Block.bake ~operation block >>=? fun block -> return (block, dst) let init_block to_originate = - Context.init ~consensus_threshold:0 1 >>=? fun (block, contracts) -> - let src = WithExceptions.Option.get ~loc:__LOC__ @@ List.hd contracts in + Context.init1 ~consensus_threshold:0 () >>=? fun (block, src) -> (*** originate contracts ***) let rec full_originate block originated = function | [] -> return (block, List.rev originated) diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_annotations.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_annotations.ml index 83eda41acd50..a6defcc353e2 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_annotations.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_annotations.ml @@ -61,9 +61,8 @@ let contract_factory_with_annotations = let lazy_none = Script.lazy_expr (Expr.from_string "None") let init_and_originate contract_code_string = - Context.init ~consensus_threshold:0 1 >>=? fun (b, contracts) -> + Context.init1 ~consensus_threshold:0 () >>=? fun (b, source) -> Incremental.begin_construction b >>=? fun inc -> - let source = WithExceptions.Option.get ~loc:__LOC__ @@ List.hd contracts in let code = Expr.toplevel_from_string contract_code_string in let script = Script.{code = lazy_expr code; storage = lazy_none} in Op.contract_origination (I inc) source ~script >>=? fun (operation, addr) -> diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_global_constants_storage.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_global_constants_storage.ml index 3d384948ab29..1ad510b6c707 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_global_constants_storage.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_global_constants_storage.ml @@ -40,11 +40,6 @@ let get_next_context b = Incremental.begin_construction b >>=? fun b -> return (Incremental.alpha_ctxt b) -let register_two_contracts ?consensus_threshold () = - Context.init ?consensus_threshold 2 >|=? function - | (_, []) | (_, [_]) -> assert false - | (b, contract_1 :: contract_2 :: _) -> (b, contract_1, contract_2) - let assert_proto_error_id loc id result = let test err = (Error_monad.find_info_of_error err).id diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_interpretation.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_interpretation.ml index 631f4901db68..8525753db6fe 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_interpretation.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_interpretation.ml @@ -39,7 +39,7 @@ open Script_interpreter open Error_monad_operators let test_context () = - Context.init 3 >>=? fun (b, _cs) -> + Context.init3 () >>=? fun (b, _cs) -> Incremental.begin_construction b >>=? fun v -> return (Incremental.alpha_ctxt v) diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_patched_contracts.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_patched_contracts.ml index b952cb4e9b47..ebfb0880b777 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_patched_contracts.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_patched_contracts.ml @@ -162,7 +162,7 @@ module Legacy_patch_test (Patches : LEGACY_SCRIPT_PATCHES) : (* Number 3 below controls how many accounts should be created. This number shouldn't be too small or the context won't have enough tokens to form a roll. *) - let* (block, _) = Context.init 3 in + let* (block, _contracts) = Context.init3 () in let* inc = Incremental.begin_construction block in let ctxt = Incremental.alpha_ctxt inc in let* _ = diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_sapling.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_sapling.ml index 53da0dca16e0..704381c37c1a 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_sapling.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_sapling.ml @@ -47,7 +47,7 @@ module Raw_context_tests = struct constant value `uncommitted` for which we know the corresponding root and tests that the returned root is as expected. *) let commitments_add_uncommitted () = - Context.init 1 >>=? fun (b, _) -> + Context.init1 () >>=? fun (b, _contract) -> Raw_context.prepare b.context ~level:b.header.shell.level @@ -86,7 +86,7 @@ module Raw_context_tests = struct however committing to disk twice the same nf causes a storage error by trying to initialize the same key twice. *) let nullifier_double () = - Context.init 1 >>=? fun (b, _) -> + Context.init1 () >>=? fun (b, _contract) -> Raw_context.prepare b.context ~level:b.header.shell.level @@ -115,7 +115,7 @@ module Raw_context_tests = struct memory). We then check that nullifier_mem answers true for those two lists and false for a third one. *) let nullifier_test () = - Context.init 1 >>=? fun (b, _) -> + Context.init1 () >>=? fun (b, _contract) -> Raw_context.prepare b.context ~level:b.header.shell.level @@ -170,7 +170,7 @@ module Raw_context_tests = struct let cm_cipher_test () = Random.self_init () ; let memo_size = Random.int 200 in - Context.init 1 >>=? fun (b, _) -> + Context.init1 () >>=? fun (b, _contract) -> Raw_context.prepare b.context ~level:b.header.shell.level @@ -212,7 +212,7 @@ module Raw_context_tests = struct let list_insertion_test () = Random.self_init () ; let memo_size = Random.int 200 in - Context.init 1 >>=? fun (b, _) -> + Context.init1 () >>=? fun (b, _contract) -> Raw_context.prepare b.context ~level:b.header.shell.level @@ -287,7 +287,7 @@ module Raw_context_tests = struct (Int32.to_int Sapling_storage.Roots.size + 10) (fun _ -> gen_root ()) in - Context.init 1 >>=? fun (b, _) -> + Context.init1 () >>=? fun (b, _contract) -> Raw_context.prepare b.context ~level:b.header.shell.level @@ -347,7 +347,7 @@ module Raw_context_tests = struct >>=? fun _ -> return_unit let test_get_memo_size () = - Context.init 1 >>=? fun (b, _) -> + Context.init1 () >>=? fun (b, _contract) -> Raw_context.prepare b.context ~level:b.header.shell.level diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_accounting.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_accounting.ml index 16e5ec3b5938..5abb58844153 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_accounting.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_accounting.ml @@ -159,8 +159,7 @@ let make_alloc big_map_id alloc updates = let init () = let open Lwt_result_syntax in - let* (block, contracts) = Context.init 1 in - let source = WithExceptions.Option.get ~loc:__LOC__ @@ List.hd contracts in + let* (block, source) = Context.init1 () in let* (operation, originated) = Op.contract_origination (B block) source ~script:Op.dummy_script in @@ -170,10 +169,7 @@ let init () = (** Initializes one address for operations and one baker. *) let init_for_operation () = - Context.init ~consensus_threshold:0 2 >|=? fun (block, contracts) -> - let (src0, src1) = - match contracts with src0 :: src1 :: _ -> (src0, src1) | _ -> assert false - in + Context.init2 ~consensus_threshold:0 () >|=? fun (block, (src0, src1)) -> let baker = match Alpha_context.Contract.is_implicit src0 with | Some v -> v diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_balance_key.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_balance_key.ml index d0a972d392e4..49fbaf045aa0 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_balance_key.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_balance_key.ml @@ -40,7 +40,7 @@ let ( let* ) m f = m >>=? f let wrap m = m >|= Environment.wrap_tzresult let new_ctxt () = - let* (block, _) = Context.init 1 in + let* (block, _contract) = Context.init1 () in let* incr = Incremental.begin_construction block in return @@ Incremental.alpha_ctxt incr diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_lazy_storage_diff.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_lazy_storage_diff.ml index c9818c6eb9b2..6cce95cf1cd2 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_lazy_storage_diff.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_lazy_storage_diff.ml @@ -133,8 +133,7 @@ let make_alloc big_map_id alloc updates = (Update {init = Lazy_storage.Alloc alloc; updates}) let init () = - let* (block, contracts) = Context.init 1 in - let source = WithExceptions.Option.get ~loc:__LOC__ @@ List.hd contracts in + let* (block, source) = Context.init1 () in let* (operation, originated) = Op.contract_origination (B block) source ~script:Op.dummy_script in diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_operations_diff.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_operations_diff.ml index 924b502223d8..986c0c83fc2a 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_operations_diff.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_operations_diff.ml @@ -180,11 +180,8 @@ let string_token ~ticketer content = (** Initializes one address for operations and one baker. *) let init ?tx_rollup_enable () = - Context.init ?tx_rollup_enable ~consensus_threshold:0 2 - >|=? fun (block, contracts) -> - let (src0, src1) = - match contracts with src0 :: src1 :: _ -> (src0, src1) | _ -> assert false - in + Context.init2 ?tx_rollup_enable ~consensus_threshold:0 () + >|=? fun (block, (src0, src1)) -> let baker = match Alpha_context.Contract.is_implicit src0 with | Some v -> v diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_scanner.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_scanner.ml index a1601223180d..b7203e61048c 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_scanner.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_scanner.ml @@ -41,7 +41,7 @@ let ( let* ) m f = m >>=? f let wrap m = m >|= Environment.wrap_tzresult let new_ctxt () = - let* (block, _) = Context.init 1 in + let* (block, _contract) = Context.init1 () in let* incr = Incremental.begin_construction block in return @@ Incremental.alpha_ctxt incr @@ -166,8 +166,7 @@ let make_string_tickets ctxt ticketer_amounts = ([], ctxt) let tickets_from_big_map_ref ~pre_populated value_exp = - let* (block, contracts) = Context.init 1 in - let source = WithExceptions.Option.get ~loc:__LOC__ @@ List.hd contracts in + let* (block, source) = Context.init1 () in let* (operation, originated) = Op.contract_origination (B block) source ~script:Op.dummy_script in diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_storage.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_storage.ml index 6056bd184956..006ce63f75f6 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_storage.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_storage.ml @@ -40,7 +40,7 @@ let ( let* ) m f = m >>=? f let wrap m = m >|= Environment.wrap_tzresult let make_context () = - let* (block, _) = Context.init 1 in + let* (block, _contract) = Context.init1 () in let* incr = Incremental.begin_construction block in return (Incremental.alpha_ctxt incr) diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_timelock.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_timelock.ml index 3ef50a35a565..4bba8a7ac094 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_timelock.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_timelock.ml @@ -75,8 +75,7 @@ let contract_test () = Incremental.add_operation incr operation >>=? fun incr -> Incremental.finalize_block incr >|=? fun b -> (dst, b) in - Context.init ~consensus_threshold:0 3 >>=? fun (b, contracts) -> - let src = match contracts with hd :: _ -> hd | _ -> assert false in + Context.init3 ~consensus_threshold:0 () >>=? fun (b, (src, _c2, _c3)) -> originate_contract "contracts/timelock.tz" "0xaa" src b >>=? fun (dst, b) -> let (public, secret) = Timelock.gen_rsa_keys () in let locked_value = Timelock.gen_locked_value public in diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_typechecking.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_typechecking.ml index 978e10fe7f48..9919530c8444 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_typechecking.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_typechecking.ml @@ -51,7 +51,7 @@ let test_unparse_view () = let script = Script.{code = lazy_expr contract_expr; storage = lazy_expr storage_expr} in - Context.init 3 >>=? fun (b, _cs) -> + Context.init3 () >>=? fun (b, _cs) -> Incremental.begin_construction b >>=? fun v -> let ctx = Incremental.alpha_ctxt v in Script_ir_translator.parse_and_unparse_script_unaccounted @@ -66,13 +66,12 @@ let test_unparse_view () = Alcotest.(check bytes) "didn't match" bef aft |> return let test_context () = - Context.init ~consensus_threshold:0 3 >>=? fun (b, _cs) -> + Context.init3 ~consensus_threshold:0 () >>=? fun (b, _cs) -> Incremental.begin_construction b >>=? fun v -> return (Incremental.alpha_ctxt v) let test_context_with_nat_nat_big_map () = - Context.init 3 >>=? fun (b, contracts) -> - let source = WithExceptions.Option.get ~loc:__LOC__ @@ List.hd contracts in + Context.init3 () >>=? fun (b, (source, _c2, _c3)) -> Op.contract_origination (B b) source ~script:Op.dummy_script >>=? fun (operation, originated) -> Block.bake ~operation b >>=? fun b -> diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_activation.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_activation.ml index 0304dc2ddd63..318e08c656a5 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_activation.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_activation.ml @@ -336,18 +336,18 @@ let secrets () = (** Helper: Create a genesis block with predefined commitments, accounts and balances. *) let activation_init () = - Context.init ~consensus_threshold:0 ~commitments 1 >|=? fun (b, cs) -> - secrets () |> fun ss -> (b, cs, ss) + Context.init1 ~consensus_threshold:0 ~commitments () >|=? fun (b, c) -> + secrets () |> fun ss -> (b, c, ss) (** Verify the genesis block created by [activation_init] can be baked. *) let test_simple_init_with_commitments () = - activation_init () >>=? fun (blk, _contracts, _secrets) -> + activation_init () >>=? fun (blk, _contract, _secrets) -> Block.bake blk >>=? fun _ -> return_unit (** A single activation *) let test_single_activation () = - activation_init () >>=? fun (blk, _contracts, secrets) -> + activation_init () >>=? fun (blk, _contract, secrets) -> let ({account; activation_code; amount = expected_amount; _} as _first_one) = WithExceptions.Option.get ~loc:__LOC__ @@ List.hd secrets in @@ -369,7 +369,7 @@ let test_single_activation () = (** 10 activations, one per bake. *) let test_multi_activation_1 () = - activation_init () >>=? fun (blk, _contracts, secrets) -> + activation_init () >>=? fun (blk, _contract, secrets) -> List.fold_left_es (fun blk {account; activation_code; amount = expected_amount; _} -> Op.activation (B blk) account activation_code >>=? fun operation -> @@ -386,7 +386,7 @@ let test_multi_activation_1 () = (** All of the 10 activations occur in one bake. *) let test_multi_activation_2 () = - activation_init () >>=? fun (blk, _contracts, secrets) -> + activation_init () >>=? fun (blk, _contract, secrets) -> List.fold_left_es (fun ops {account; activation_code; _} -> Op.activation (B blk) account activation_code >|=? fun op -> op :: ops) @@ -406,13 +406,10 @@ let test_multi_activation_2 () = (** Transfer with activated account. *) let test_activation_and_transfer () = - activation_init () >>=? fun (blk, contracts, secrets) -> + activation_init () >>=? fun (blk, bootstrap_contract, secrets) -> let ({account; activation_code; _} as _first_one) = WithExceptions.Option.get ~loc:__LOC__ @@ List.hd secrets in - let bootstrap_contract = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd contracts - in let first_contract = Contract.implicit_contract account in Op.activation (B blk) account activation_code >>=? fun operation -> Block.bake ~operation blk >>=? fun blk -> @@ -432,13 +429,10 @@ let test_activation_and_transfer () = (** Transfer to an unactivated account and then activating it. *) let test_transfer_to_unactivated_then_activate () = - activation_init () >>=? fun (blk, contracts, secrets) -> + activation_init () >>=? fun (blk, bootstrap_contract, secrets) -> let ({account; activation_code; amount} as _first_one) = WithExceptions.Option.get ~loc:__LOC__ @@ List.hd secrets in - let bootstrap_contract = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd contracts - in let unactivated_commitment_contract = Contract.implicit_contract account in Context.Contract.balance (B blk) bootstrap_contract >>=? fun b_amount -> b_amount /? 2L >>?= fun b_half_amount -> @@ -467,7 +461,7 @@ let test_transfer_to_unactivated_then_activate () = (** Invalid pkh activation: expected to fail as the context does not contain any commitment. *) let test_invalid_activation_with_no_commitments () = - Context.init 1 >>=? fun (blk, _) -> + Context.init1 () >>=? fun (blk, _contract) -> let secrets = secrets () in let ({account; activation_code; _} as _first_one) = WithExceptions.Option.get ~loc:__LOC__ @@ List.hd secrets @@ -478,7 +472,7 @@ let test_invalid_activation_with_no_commitments () = (** Wrong activation: wrong secret given in the operation. *) let test_invalid_activation_wrong_secret () = - activation_init () >>=? fun (blk, _, secrets) -> + activation_init () >>=? fun (blk, _contract, secrets) -> let ({account; _} as _first_one) = WithExceptions.Option.get ~loc:__LOC__ @@ List.nth secrets 0 in @@ -492,7 +486,7 @@ let test_invalid_activation_wrong_secret () = (** Invalid pkh activation : expected to fail as the context does not contain an associated commitment. *) let test_invalid_activation_inexistent_pkh () = - activation_init () >>=? fun (blk, _, secrets) -> + activation_init () >>=? fun (blk, _contract, secrets) -> let ({activation_code; _} as _first_one) = WithExceptions.Option.get ~loc:__LOC__ @@ List.hd secrets in @@ -507,7 +501,7 @@ let test_invalid_activation_inexistent_pkh () = (** Invalid pkh activation : expected to fail as the commitment has already been claimed. *) let test_invalid_double_activation () = - activation_init () >>=? fun (blk, _, secrets) -> + activation_init () >>=? fun (blk, _contract, secrets) -> let ({account; activation_code; _} as _first_one) = WithExceptions.Option.get ~loc:__LOC__ @@ List.hd secrets in @@ -520,13 +514,10 @@ let test_invalid_double_activation () = (** Transfer from an unactivated commitment account. *) let test_invalid_transfer_from_unactivated_account () = - activation_init () >>=? fun (blk, contracts, secrets) -> + activation_init () >>=? fun (blk, bootstrap_contract, secrets) -> let ({account; _} as _first_one) = WithExceptions.Option.get ~loc:__LOC__ @@ List.hd secrets in - let bootstrap_contract = - WithExceptions.Option.get ~loc:__LOC__ @@ List.hd contracts - in let unactivated_commitment_contract = Contract.implicit_contract account in (* No activation *) Op.transaction diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_combined_operations.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_combined_operations.ml index 43fc555cde62..faff59040f8f 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_combined_operations.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_combined_operations.ml @@ -52,10 +52,7 @@ let gas_limit = Alpha_context.Gas.Arith.integral_of_int_exn 3000 (** Groups ten transactions between the same parties. *) let test_multiple_transfers () = - Context.init 3 >>=? fun (blk, contracts) -> - let (c1, c2, c3) = - match contracts with [c1; c2; c3] -> (c1, c2, c3) | _ -> assert false - in + Context.init3 () >>=? fun (blk, (c1, c2, c3)) -> List.map_es (fun _ -> Op.transaction ~gas_limit (B blk) c1 c2 Tez.one) (1 -- 10) @@ -84,10 +81,7 @@ let test_multiple_transfers () = (** Groups ten delegated originations. *) let test_multiple_origination_and_delegation () = - Context.init 2 >>=? fun (blk, contracts) -> - let (c1, c2) = - match contracts with [c1; c2] -> (c1, c2) | _ -> assert false - in + Context.init2 () >>=? fun (blk, (c1, c2)) -> let n = 10 in Context.get_constants (B blk) >>=? fun {parametric = {origination_size; cost_per_byte; _}; _} -> @@ -172,10 +166,7 @@ let expect_failure = function Checks that the receipt is consistent. Variant without fees. *) let test_failing_operation_in_the_middle () = - Context.init 2 >>=? fun (blk, contracts) -> - let (c1, c2) = - match contracts with [c1; c2] -> (c1, c2) | _ -> assert false - in + Context.init2 () >>=? fun (blk, (c1, c2)) -> Op.transaction ~gas_limit ~fee:Tez.zero (B blk) c1 c2 Tez.one >>=? fun op1 -> Op.transaction ~gas_limit ~fee:Tez.zero (B blk) c1 c2 Test_tez.max_tez >>=? fun op2 -> @@ -221,10 +212,7 @@ let test_failing_operation_in_the_middle () = Checks that the receipt is consistent. Variant with fees, that should be spent even in case of failure. *) let test_failing_operation_in_the_middle_with_fees () = - Context.init 2 >>=? fun (blk, contracts) -> - let (c1, c2) = - match contracts with [c1; c2] -> (c1, c2) | _ -> assert false - in + Context.init2 () >>=? fun (blk, (c1, c2)) -> Op.transaction ~fee:Tez.one (B blk) c1 c2 Tez.one >>=? fun op1 -> Op.transaction ~fee:Tez.one (B blk) c1 c2 Test_tez.max_tez >>=? fun op2 -> Op.transaction ~fee:Tez.one (B blk) c1 c2 Tez.one >>=? fun op3 -> @@ -273,55 +261,45 @@ let test_failing_operation_in_the_middle_with_fees () = return_unit let test_wrong_signature_in_the_middle () = - Context.init 2 >>=? function - | (_, []) | (_, [_]) -> assert false - | (blk, c1 :: c2 :: _) -> - Op.transaction ~gas_limit ~fee:Tez.one (B blk) c1 c2 Tez.one - >>=? fun op1 -> - Op.transaction ~gas_limit ~fee:Tez.one (B blk) c2 c1 Tez.one - >>=? fun op2 -> - Incremental.begin_construction blk >>=? fun inc -> - (* Make legit transfers, performing reveals *) - Incremental.add_operation inc op1 >>=? fun inc -> - Incremental.add_operation inc op2 >>=? fun inc -> - (* Make c2 reach counter 5 *) - Op.transaction ~gas_limit ~fee:Tez.one (I inc) c2 c1 Tez.one - >>=? fun op -> - Incremental.add_operation inc op >>=? fun inc -> - Op.transaction ~gas_limit ~fee:Tez.one (I inc) c2 c1 Tez.one - >>=? fun op -> - Incremental.add_operation inc op >>=? fun inc -> - Op.transaction ~gas_limit ~fee:Tez.one (I inc) c2 c1 Tez.one - >>=? fun op -> - Incremental.add_operation inc op >>=? fun inc -> - (* Cook transactions for actual test *) - Op.transaction ~gas_limit ~fee:Tez.one (I inc) c1 c2 Tez.one - >>=? fun op1 -> - Op.transaction ~gas_limit ~fee:Tez.one (I inc) c1 c2 Tez.one - >>=? fun op2 -> - Op.transaction ~gas_limit ~fee:Tez.one (I inc) c1 c2 Tez.one - >>=? fun op3 -> - Op.transaction ~gas_limit ~fee:Tez.one (I inc) c2 c1 Tez.one - >>=? fun spurious_operation -> - let operations = [op1; op2; op3] in - Op.combine_operations ~spurious_operation ~source:c1 (I inc) operations - >>=? fun operation -> - let expect_apply_failure = function - | Environment.Ecoproto_error err :: _ -> - Assert.test_error_encodings err ; - let error_info = - Error_monad.find_info_of_error (Environment.wrap_tzerror err) - in - if error_info.title = "Inconsistent sources in operation pack" then - return_unit - else failwith "unexpected error" - | _ -> - failwith - "Packed operation has invalid source in the middle : operation \ - expected to fail." - in - Incremental.add_operation ~expect_apply_failure inc operation - >>=? fun _inc -> return_unit + Context.init2 () >>=? fun (blk, (c1, c2)) -> + Op.transaction ~gas_limit ~fee:Tez.one (B blk) c1 c2 Tez.one >>=? fun op1 -> + Op.transaction ~gas_limit ~fee:Tez.one (B blk) c2 c1 Tez.one >>=? fun op2 -> + Incremental.begin_construction blk >>=? fun inc -> + (* Make legit transfers, performing reveals *) + Incremental.add_operation inc op1 >>=? fun inc -> + Incremental.add_operation inc op2 >>=? fun inc -> + (* Make c2 reach counter 5 *) + Op.transaction ~gas_limit ~fee:Tez.one (I inc) c2 c1 Tez.one >>=? fun op -> + Incremental.add_operation inc op >>=? fun inc -> + Op.transaction ~gas_limit ~fee:Tez.one (I inc) c2 c1 Tez.one >>=? fun op -> + Incremental.add_operation inc op >>=? fun inc -> + Op.transaction ~gas_limit ~fee:Tez.one (I inc) c2 c1 Tez.one >>=? fun op -> + Incremental.add_operation inc op >>=? fun inc -> + (* Cook transactions for actual test *) + Op.transaction ~gas_limit ~fee:Tez.one (I inc) c1 c2 Tez.one >>=? fun op1 -> + Op.transaction ~gas_limit ~fee:Tez.one (I inc) c1 c2 Tez.one >>=? fun op2 -> + Op.transaction ~gas_limit ~fee:Tez.one (I inc) c1 c2 Tez.one >>=? fun op3 -> + Op.transaction ~gas_limit ~fee:Tez.one (I inc) c2 c1 Tez.one + >>=? fun spurious_operation -> + let operations = [op1; op2; op3] in + Op.combine_operations ~spurious_operation ~source:c1 (I inc) operations + >>=? fun operation -> + let expect_apply_failure = function + | Environment.Ecoproto_error err :: _ -> + Assert.test_error_encodings err ; + let error_info = + Error_monad.find_info_of_error (Environment.wrap_tzerror err) + in + if error_info.title = "Inconsistent sources in operation pack" then + return_unit + else failwith "unexpected error" + | _ -> + failwith + "Packed operation has invalid source in the middle : operation \ + expected to fail." + in + Incremental.add_operation ~expect_apply_failure inc operation >>=? fun _inc -> + return_unit let expect_inconsistent_counters list = if @@ -339,10 +317,7 @@ let expect_inconsistent_counters list = list let test_inconsistent_counters () = - Context.init 2 >>=? fun (blk, contracts) -> - let (c1, c2) = - match contracts with [c1; c2] -> (c1, c2) | _ -> assert false - in + Context.init2 () >>=? fun (blk, (c1, c2)) -> Op.transaction ~gas_limit ~fee:Tez.one (B blk) c1 c2 Tez.one >>=? fun op1 -> Op.transaction ~gas_limit ~fee:Tez.one (B blk) c2 c1 Tez.one >>=? fun op2 -> Incremental.begin_construction blk >>=? fun inc -> diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_failing_noop.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_failing_noop.ml index 8a7c8efe0f1f..95bd9ea38a68 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_failing_noop.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_failing_noop.ml @@ -40,16 +40,9 @@ open Protocol open Alpha_context -let register_one_contract () = - Context.init 1 >>=? fun (b, contracts) -> - let contract = - List.nth contracts 0 |> WithExceptions.Option.get ~loc:__LOC__ - in - return (b, contract) - (** try to apply a failing_noop and assert that the operation fails *) let failing_noop_must_fail_when_injected () = - register_one_contract () >>=? fun (blk, contract) -> + Context.init1 () >>=? fun (blk, contract) -> Contract.is_implicit contract |> function | None -> Alcotest.fail "only implicit accounts can sign" | Some source -> diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_origination.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_origination.ml index 3d9a6a7c4704..17059e11c40f 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_origination.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_origination.ml @@ -65,11 +65,7 @@ let total_fees_for_origination ?(fee = Tez.zero) ?(credit = Tez.zero) b = fees instantaneously. So to see that the fees are subtracted, we need that the bake is done by another delegated. *) let test_origination_balances ~loc:_ ?(fee = Tez.zero) ?(credit = Tez.zero) () = - Context.init 2 >>=? fun (b, contracts) -> - let source = WithExceptions.Option.get ~loc:__LOC__ @@ List.hd contracts in - let contract_for_bake = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 1 - in + Context.init2 () >>=? fun (b, (source, contract_for_bake)) -> Context.Contract.pkh source >>=? fun pkh_for_orig -> Context.Contract.pkh contract_for_bake >>=? fun pkh_for_bake -> Op.contract_origination (B b) source ~fee ~credit ~script:Op.dummy_script @@ -98,11 +94,7 @@ let test_origination_balances ~loc:_ ?(fee = Tez.zero) ?(credit = Tez.zero) () = meaning that this contract is spendable; delegatable default is set to true meaning that this contract is able to delegate. *) let register_origination ?(fee = Tez.zero) ?(credit = Tez.zero) () = - Context.init 2 >>=? fun (b, contracts) -> - let source = WithExceptions.Option.get ~loc:__LOC__ @@ List.hd contracts in - let contract_for_bake = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 1 - in + Context.init2 () >>=? fun (b, (source, contract_for_bake)) -> Context.Contract.pkh source >>=? fun source_pkh -> Context.Contract.pkh contract_for_bake >>=? fun pkh_for_bake -> Op.contract_origination (B b) source ~fee ~credit ~script:Op.dummy_script @@ -158,13 +150,7 @@ let test_pay_fee () = (** Create an originate contract where the contract does not have enough tez to pay for the fee. *) let test_not_tez_in_contract_to_pay_fee () = - Context.init 2 >>=? fun (b, contracts) -> - let contract_1 = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 0 - in - let contract_2 = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 1 - in + Context.init2 () >>=? fun (b, (contract_1, contract_2)) -> Incremental.begin_construction b >>=? fun inc -> (* transfer everything but one tez from 1 to 2 and check balance of 1 *) Context.Contract.balance (I inc) contract_1 >>=? fun balance -> @@ -188,8 +174,7 @@ let test_not_tez_in_contract_to_pay_fee () = (* Set the endorser of the block as manager/delegate of the originated account. *) let register_contract_get_endorser () = - Context.init 1 >>=? fun (b, contracts) -> - let contract = WithExceptions.Option.get ~loc:__LOC__ @@ List.hd contracts in + Context.init1 () >>=? fun (b, contract) -> Incremental.begin_construction b >>=? fun inc -> Context.get_endorser (I inc) >|=? fun (account_endorser, _slots) -> (inc, contract, account_endorser) @@ -210,8 +195,7 @@ let test_multiple_originations () = (** Cannot originate two contracts with the same context's counter. *) let test_counter () = - Context.init 1 >>=? fun (b, contracts) -> - let contract = WithExceptions.Option.get ~loc:__LOC__ @@ List.hd contracts in + Context.init1 () >>=? fun (b, contract) -> Incremental.begin_construction b >>=? fun inc -> Op.contract_origination (I inc) diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_reveal.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_reveal.ml index 7af3e8f9fbbc..7235c59d19d1 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_reveal.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_reveal.ml @@ -41,8 +41,7 @@ open Test_tez let ten_tez = of_int 10 let test_simple_reveal () = - Context.init ~consensus_threshold:0 1 >>=? fun (blk, contracts) -> - let c = WithExceptions.Option.get ~loc:__LOC__ @@ List.hd contracts in + Context.init1 ~consensus_threshold:0 () >>=? fun (blk, c) -> let new_c = Account.new_account () in let new_contract = Alpha_context.Contract.implicit_contract new_c.pkh in (* Create the contract *) @@ -60,8 +59,7 @@ let test_simple_reveal () = | false -> Stdlib.failwith "New contract revelation failed." let test_empty_account_on_reveal () = - Context.init ~consensus_threshold:0 1 >>=? fun (blk, contracts) -> - let c = WithExceptions.Option.get ~loc:__LOC__ @@ List.hd contracts in + Context.init1 ~consensus_threshold:0 () >>=? fun (blk, c) -> let new_c = Account.new_account () in let new_contract = Alpha_context.Contract.implicit_contract new_c.pkh in let amount = Tez.one_mutez in @@ -82,8 +80,7 @@ let test_empty_account_on_reveal () = | true -> Stdlib.failwith "Empty account still exists and is revealed." let test_not_enough_found_for_reveal () = - Context.init 1 >>=? fun (blk, contracts) -> - let c = WithExceptions.Option.get ~loc:__LOC__ @@ List.hd contracts in + Context.init1 () >>=? fun (blk, c) -> let new_c = Account.new_account () in let new_contract = Alpha_context.Contract.implicit_contract new_c.pkh in (* Create the contract *) @@ -99,8 +96,7 @@ let test_not_enough_found_for_reveal () = Assert.proto_error_with_info ~loc:__LOC__ res "Balance too low" let test_transfer_fees_emptying_after_reveal_batched () = - Context.init 1 >>=? fun (blk, contracts) -> - let c = WithExceptions.Option.get ~loc:__LOC__ @@ List.hd contracts in + Context.init1 () >>=? fun (blk, c) -> let new_c = Account.new_account () in let new_contract = Alpha_context.Contract.implicit_contract new_c.pkh in (* Create the contract *) diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_sc_rollup.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_sc_rollup.ml index 468484621844..cacd247bda80 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_sc_rollup.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_sc_rollup.ml @@ -57,10 +57,7 @@ let context_init n = rollup when the feature flag is deactivated and checks that it fails. *) let test_disable_feature_flag () = - let* (b, contracts) = Context.init 1 in - let contract = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 0 - in + let* (b, contract) = Context.init1 () in let* i = Incremental.begin_construction b in let kind = Sc_rollup.Kind.Example_arith in let* (op, _) = Op.sc_rollup_origination (I i) contract kind "" in diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_transfer.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_transfer.ml index d84bcbf92392..b658448b9b09 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_transfer.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_transfer.ml @@ -115,8 +115,7 @@ let test_transfer_zero_tez () = (** Transfer zero tez from an implicit contract. *) let test_transfer_zero_implicit () = - Context.init 1 >>=? fun (b, contracts) -> - let dest = WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 0 in + Context.init1 () >>=? fun (b, dest) -> let account = Account.new_account () in Incremental.begin_construction b >>=? fun i -> let src = Contract.implicit_contract account.Account.pkh in @@ -126,10 +125,7 @@ let test_transfer_zero_implicit () = (** Transfer to originated contract. *) let test_transfer_to_originate_with_fee () = - Context.init 1 >>=? fun (b, contracts) -> - let contract = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 0 - in + Context.init1 () >>=? fun (b, contract) -> Incremental.begin_construction b >>=? fun b -> two_over_n_of_balance b contract 10L >>=? fun fee -> (* originated contract, paying a fee to originated this contract *) @@ -158,10 +154,7 @@ let test_transfer_amount_of_contract_balance () = (** Transfer to oneself. *) let test_transfers_to_self () = - Context.init 1 >>=? fun (b, contracts) -> - let contract = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 0 - in + Context.init1 () >>=? fun (b, contract) -> Incremental.begin_construction b >>=? fun b -> two_over_n_of_balance b contract 3L >>=? fun amount -> transfer_to_itself_and_check_balances ~loc:__LOC__ b contract amount @@ -188,8 +181,7 @@ let test_missing_transaction () = (** Transfer zero tez to an implicit contract, with fee equals balance of src. *) let test_transfer_zero_implicit_with_bal_src_as_fee () = - Context.init 1 >>=? fun (b, contracts) -> - let dest = WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 0 in + Context.init1 () >>=? fun (b, dest) -> let account = Account.new_account () in Incremental.begin_construction b >>=? fun i -> let src = Contract.implicit_contract account.Account.pkh in @@ -203,8 +195,7 @@ let test_transfer_zero_implicit_with_bal_src_as_fee () = (** Transfer zero tez to an originated contract, with fee equals balance of src. *) let test_transfer_zero_to_originated_with_bal_src_as_fee () = - Context.init 1 >>=? fun (b, contracts) -> - let dest = WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 0 in + Context.init1 () >>=? fun (b, dest) -> let account = Account.new_account () in Incremental.begin_construction b >>=? fun i -> let src = Contract.implicit_contract account.Account.pkh in @@ -221,8 +212,7 @@ let test_transfer_zero_to_originated_with_bal_src_as_fee () = (** Transfer one tez to an implicit contract, with fee equals balance of src. *) let test_transfer_one_to_implicit_with_bal_src_as_fee () = - Context.init 1 >>=? fun (b, contracts) -> - let dest = WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 0 in + Context.init1 () >>=? fun (b, dest) -> let account = Account.new_account () in Incremental.begin_construction b >>=? fun i -> let src = Contract.implicit_contract account.Account.pkh in @@ -245,10 +235,7 @@ let test_transfer_one_to_implicit_with_bal_src_as_fee () = (** Implicit to Implicit. *) let test_transfer_from_implicit_to_implicit_contract () = - Context.init 1 >>=? fun (b, contracts) -> - let bootstrap_contract = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 0 - in + Context.init1 () >>=? fun (b, bootstrap_contract) -> let account_a = Account.new_account () in let account_b = Account.new_account () in Incremental.begin_construction b >>=? fun b -> @@ -282,13 +269,8 @@ let test_transfer_from_implicit_to_implicit_contract () = (** Implicit to originated. *) let test_transfer_from_implicit_to_originated_contract () = - Context.init 1 >>=? fun (b, contracts) -> - let bootstrap_contract = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 0 - in - let contract = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 0 - in + Context.init1 () >>=? fun (b, bootstrap_contract) -> + let contract = bootstrap_contract in let account = Account.new_account () in let src = Contract.implicit_contract account.Account.pkh in Incremental.begin_construction b >>=? fun b -> @@ -339,7 +321,7 @@ let test_block_with_multiple_transfers_pay_fee () = 2- Apply multiple transfers without fees; 3- Apply multiple transfers with fees. *) let test_block_with_multiple_transfers_with_without_fee () = - Context.init 8 >>=? fun (b, contracts) -> + Context.init_n 8 () >>=? fun (b, contracts) -> let contracts = Array.of_list contracts in Incremental.begin_construction b >>=? fun b -> let hundred = of_int 100 in @@ -383,8 +365,7 @@ let test_build_a_chain () = (** Transferring zero tez is forbidden in implicit contract. *) let test_empty_implicit () = - Context.init 1 >>=? fun (b, contracts) -> - let dest = WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 0 in + Context.init1 () >>=? fun (b, dest) -> let account = Account.new_account () in Incremental.begin_construction b >>=? fun incr -> let src = Contract.implicit_contract account.Account.pkh in @@ -431,16 +412,7 @@ let test_balance_too_low fee () = 3- Add another transfer that send tez from a zero balance contract; 4- Catch the expected error: Balance_too_low. *) let test_balance_too_low_two_transfers fee () = - Context.init 3 >>=? fun (b, contracts) -> - let contract_1 = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 0 - in - let contract_2 = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 1 - in - let contract_3 = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 2 - in + Context.init3 () >>=? fun (b, (contract_1, contract_2, contract_3)) -> Incremental.begin_construction b >>=? fun i -> Context.Contract.balance (I i) contract_1 >>=? fun balance -> balance /? 3L >>?= fun res -> @@ -545,7 +517,7 @@ let random_contract contract_array = (** Transfer by randomly choose amount 10 contracts, and randomly choose the amount in the source contract. *) let test_random_transfer () = - Context.init 10 >>=? fun (b, contracts) -> + Context.init_n 10 () >>=? fun (b, contracts) -> let contracts = Array.of_list contracts in let source = random_contract contracts in let dest = random_contract contracts in @@ -569,7 +541,7 @@ let test_random_multi_transactions () = (*********************************************************************) let test_bad_entrypoint () = - Context.init 1 >>=? fun (b, _cs) -> + Context.init1 () >>=? fun (b, _c) -> Incremental.begin_construction b >>=? fun v -> let ctxt = Incremental.alpha_ctxt v in let storage = "Unit" in @@ -596,7 +568,7 @@ let test_bad_entrypoint () = Alcotest.failf "Unexpected error: %a" Error_monad.pp_print_trace errs let test_bad_parameter () = - Context.init 1 >>=? fun (b, _cs) -> + Context.init1 () >>=? fun (b, _c) -> Incremental.begin_construction b >>=? fun v -> let ctxt = Incremental.alpha_ctxt v in let storage = "Unit" in @@ -623,9 +595,8 @@ let test_bad_parameter () = let transfer_to_itself_with_no_such_entrypoint () = let entrypoint = Entrypoint.of_string_strict_exn "bad entrypoint" in - Context.init 1 >>=? fun (b, contract) -> + Context.init1 () >>=? fun (b, addr) -> Incremental.begin_construction b >>=? fun i -> - let addr = match contract with [hd] -> hd | _ -> assert false in Op.transaction (B b) addr addr Tez.one ~entrypoint >>=? fun transaction -> let expect_failure = function | Environment.Ecoproto_error (Script_tc_errors.No_such_entrypoint _ as e) diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml index d6c21dec0270..635a8b8ee993 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml @@ -612,11 +612,8 @@ let message_result context_hash withdraws = (** [test_origination] originates a transaction rollup and checks that it burns the expected quantity of xtz. *) let test_origination () = - Context.init ~tx_rollup_enable:true ~tx_rollup_sunset_level:Int32.max_int 1 - >>=? fun (b, contracts) -> - let contract = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 0 - in + Context.init1 ~tx_rollup_enable:true ~tx_rollup_sunset_level:Int32.max_int () + >>=? fun (b, contract) -> Context.get_constants (B b) >>=? fun {parametric = {tx_rollup_origination_size; cost_per_byte; _}; _} -> Context.Contract.balance (B b) contract >>=? fun balance -> @@ -636,11 +633,8 @@ let test_origination () = (** [test_two_originations] originates two transaction rollups in the same operation and checks that they have a different address. *) let test_two_originations () = - Context.init ~tx_rollup_enable:true ~tx_rollup_sunset_level:Int32.max_int 1 - >>=? fun (b, contracts) -> - let contract = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 0 - in + Context.init1 ~tx_rollup_enable:true ~tx_rollup_sunset_level:Int32.max_int () + >>=? fun (b, contract) -> Incremental.begin_construction b >>=? fun i -> Op.tx_rollup_origination (I i) contract >>=? fun (op1, _false_tx_rollup1) -> (* tx_rollup1 and tx_rollup2 are equal and both are false. The addresses are diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_voting.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_voting.ml index 57bbc0be0300..2e68b6853558 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_voting.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_voting.ml @@ -235,7 +235,7 @@ let context_init = `blocks_per_voting_period <= preserved_cycles * blocks_per_cycle.` We also set baking and endorsing rewards to zero in order to ease accounting of exact baker stake. *) - Context.init + Context.init_n ~blocks_per_cycle:4l ~cycles_per_voting_period:1l ~consensus_threshold:0 @@ -247,7 +247,7 @@ let context_init = let test_successful_vote num_delegates () = let open Alpha_context in let min_proposal_quorum = Int32.(of_int @@ (100_00 / num_delegates)) in - context_init ~min_proposal_quorum num_delegates >>=? fun (b, _) -> + context_init ~min_proposal_quorum num_delegates () >>=? fun (b, _) -> (* no ballots in proposal period *) assert_empty_ballots b __LOC__ >>=? fun () -> (* Last baked block is first block of period Proposal *) @@ -482,7 +482,7 @@ let get_expected_participation_ema power voter_power old_participation_ema = in exploration, go back to proposal period. *) let test_not_enough_quorum_in_exploration num_delegates () = let min_proposal_quorum = Int32.(of_int @@ (100_00 / num_delegates)) in - context_init ~min_proposal_quorum num_delegates >>=? fun (b, delegates) -> + context_init ~min_proposal_quorum num_delegates () >>=? fun (b, delegates) -> (* proposal period *) let open Alpha_context in assert_period ~expected_kind:Proposal b __LOC__ >>=? fun () -> @@ -538,7 +538,7 @@ let test_not_enough_quorum_in_exploration num_delegates () = In promotion period, go back to proposal period. *) let test_not_enough_quorum_in_promotion num_delegates () = let min_proposal_quorum = Int32.(of_int @@ (100_00 / num_delegates)) in - context_init ~min_proposal_quorum num_delegates >>=? fun (b, delegates) -> + context_init ~min_proposal_quorum num_delegates () >>=? fun (b, delegates) -> assert_period ~expected_kind:Proposal b __LOC__ >>=? fun () -> let proposer = WithExceptions.Option.get ~loc:__LOC__ @@ List.nth delegates 0 @@ -612,7 +612,7 @@ let test_not_enough_quorum_in_promotion num_delegates () = (** Identical proposals (identified by their hash) must be counted as one. *) let test_multiple_identical_proposals_count_as_one () = - context_init 1 >>=? fun (b, delegates) -> + context_init 1 () >>=? fun (b, delegates) -> assert_period ~expected_kind:Proposal b __LOC__ >>=? fun () -> let proposer = WithExceptions.Option.get ~loc:__LOC__ @@ List.hd delegates in Op.proposals (B b) proposer [Protocol_hash.zero; Protocol_hash.zero] @@ -640,7 +640,7 @@ let test_multiple_identical_proposals_count_as_one () = expected_weight_proposer | None -> failwith "%s - Missing proposal" __LOC__ -(** Assume the initial balance of accounts allocated by Context.init is at +(** Assume the initial balance of accounts allocated by Context.init_n is at least 4 times the value of the tokens_per_roll constant. *) let test_supermajority_in_proposal there_is_a_winner () = let min_proposal_quorum = 0l in @@ -649,6 +649,7 @@ let test_supermajority_in_proposal there_is_a_winner () = ~min_proposal_quorum ~initial_balances:[initial_balance; initial_balance; initial_balance] 10 + () >>=? fun (b, delegates) -> Context.get_constants (B b) >>=? fun {parametric = {tokens_per_roll; _}; _} -> let del1 = WithExceptions.Option.get ~loc:__LOC__ @@ List.nth delegates 0 in @@ -700,7 +701,7 @@ let test_supermajority_in_proposal there_is_a_winner () = let test_quorum_in_proposal has_quorum () = let total_tokens = 32_000_000_000_000L in let half_tokens = Int64.div total_tokens 2L in - context_init ~initial_balances:[1L; half_tokens; half_tokens] 3 + context_init ~initial_balances:[1L; half_tokens; half_tokens] 3 () >>=? fun (b, delegates) -> Context.get_constants (B b) >>=? fun {parametric = {min_proposal_quorum; _}; _} -> @@ -733,7 +734,7 @@ let test_quorum_in_proposal has_quorum () = reached. Otherwise, it remains in proposal period. *) let test_supermajority_in_exploration supermajority () = let min_proposal_quorum = Int32.(of_int @@ (100_00 / 100)) in - context_init ~min_proposal_quorum 100 >>=? fun (b, delegates) -> + context_init ~min_proposal_quorum 100 () >>=? fun (b, delegates) -> let del1 = WithExceptions.Option.get ~loc:__LOC__ @@ List.nth delegates 0 in let proposal = protos.(0) in Op.proposals (B b) del1 [proposal] >>=? fun ops1 -> @@ -777,7 +778,7 @@ let test_supermajority_in_exploration supermajority () = proposals. *) let test_no_winning_proposal num_delegates () = let min_proposal_quorum = Int32.(of_int @@ (100_00 / num_delegates)) in - context_init ~min_proposal_quorum num_delegates >>=? fun (b, _) -> + context_init ~min_proposal_quorum num_delegates () >>=? fun (b, _) -> (* beginning of proposal, denoted by _p1; take a snapshot of the active delegates and their voting power from listings *) get_delegates_and_power_from_listings b >>=? fun (delegates_p1, _power_p1) -> @@ -799,7 +800,7 @@ let test_no_winning_proposal num_delegates () = maximum quorum cap. *) let test_quorum_capped_maximum num_delegates () = let min_proposal_quorum = Int32.(of_int @@ (100_00 / num_delegates)) in - context_init ~min_proposal_quorum num_delegates >>=? fun (b, delegates) -> + context_init ~min_proposal_quorum num_delegates () >>=? fun (b, delegates) -> (* set the participation EMA to 100% *) Context.Vote.set_participation_ema b 100_00l >>= fun b -> Context.get_constants (B b) >>=? fun {parametric = {quorum_max; _}; _} -> @@ -839,7 +840,7 @@ let test_quorum_capped_maximum num_delegates () = minimum quorum cap. *) let test_quorum_capped_minimum num_delegates () = let min_proposal_quorum = Int32.(of_int @@ (100_00 / num_delegates)) in - context_init ~min_proposal_quorum num_delegates >>=? fun (b, delegates) -> + context_init ~min_proposal_quorum num_delegates () >>=? fun (b, delegates) -> (* set the participation EMA to 0% *) Context.Vote.set_participation_ema b 0l >>= fun b -> Context.get_constants (B b) >>=? fun {parametric = {quorum_min; _}; _} -> @@ -888,7 +889,7 @@ let test_voting_power_updated_each_voting_period () = let init_bal2 = 48_000_000_000L in let init_bal3 = 40_000_000_000L in (* Create three accounts with different amounts *) - context_init ~initial_balances:[init_bal1; init_bal2; init_bal3] 3 + context_init ~initial_balances:[init_bal1; init_bal2; init_bal3] 3 () >>=? fun (genesis, contracts) -> let con1 = WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 0 in let con2 = WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 1 in diff --git a/src/proto_alpha/lib_protocol/test/integration/test_frozen_bonds.ml b/src/proto_alpha/lib_protocol/test/integration/test_frozen_bonds.ml index 47b223b4067c..8e5af11ece3b 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_frozen_bonds.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_frozen_bonds.ml @@ -308,16 +308,13 @@ let test_total_stake ~user_is_delegate () = These rpcs call the functions [Contract.get_frozen_bonds] and [Contract.get_balance_and_frozen_bonds] already tested in previous tests. *) let test_rpcs () = - Context.init 1 >>=? fun (blk, contracts) -> - match contracts with - | [contract] -> - Context.Contract.frozen_bonds (B blk) contract >>=? fun frozen_bonds -> - Assert.equal_tez ~loc:__LOC__ frozen_bonds Tez.zero >>=? fun () -> - Context.Contract.balance_and_frozen_bonds (B blk) contract - >>=? fun balance_and_frozen_bonds -> - Context.Contract.balance (B blk) contract >>=? fun balance -> - Assert.equal_tez ~loc:__LOC__ balance_and_frozen_bonds balance - | _ -> (* Exactly one account has been generated. *) assert false + Context.init1 () >>=? fun (blk, contract) -> + Context.Contract.frozen_bonds (B blk) contract >>=? fun frozen_bonds -> + Assert.equal_tez ~loc:__LOC__ frozen_bonds Tez.zero >>=? fun () -> + Context.Contract.balance_and_frozen_bonds (B blk) contract + >>=? fun balance_and_frozen_bonds -> + Context.Contract.balance (B blk) contract >>=? fun balance -> + Assert.equal_tez ~loc:__LOC__ balance_and_frozen_bonds balance (** A helper to test a particular delegation/freezing scenario *) let test_scenario scenario = diff --git a/src/proto_alpha/lib_protocol/test/integration/test_liquidity_baking.ml b/src/proto_alpha/lib_protocol/test/integration/test_liquidity_baking.ml index 0e1e98d69524..5eb5f12b0e22 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_liquidity_baking.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_liquidity_baking.ml @@ -71,7 +71,7 @@ let expected_lqt_hash = (* Test that the scripts of the Liquidity Baking contracts (CPMM and LQT) have the expected hashes. *) let liquidity_baking_origination () = - Context.init 1 >>=? fun (blk, _contracts) -> + Context.init1 () >>=? fun (blk, _contract) -> Context.get_liquidity_baking_cpmm_address (B blk) >>=? fun cpmm_address -> Context.Contract.script_hash (B blk) cpmm_address >>=? fun cpmm_hash -> Lwt.return @@ Environment.wrap_tzresult @@ -97,7 +97,7 @@ let liquidity_baking_origination () = (* Test that the CPMM address in storage is correct *) let liquidity_baking_cpmm_address () = - Context.init 1 >>=? fun (blk, _contracts) -> + Context.init1 () >>=? fun (blk, _contract) -> Context.get_liquidity_baking_cpmm_address (B blk) >>=? fun liquidity_baking -> Assert.equal ~loc:__LOC__ @@ -110,7 +110,7 @@ let liquidity_baking_cpmm_address () = (* Test that after [n] blocks, the liquidity baking CPMM contract is credited [n] times the subsidy amount. *) let liquidity_baking_subsidies n () = - Context.init ~consensus_threshold:0 1 >>=? fun (blk, _contracts) -> + Context.init1 ~consensus_threshold:0 () >>=? fun (blk, _contract) -> Context.get_liquidity_baking_cpmm_address (B blk) >>=? fun liquidity_baking -> Context.Contract.balance (B blk) liquidity_baking >>=? fun old_balance -> Block.bake_n n blk >>=? fun blk -> @@ -129,7 +129,7 @@ let liquidity_baking_subsidies n () = More precisely, after the sunset, the total amount credited to the subsidy is only proportional to the sunset level and in particular it does not depend on [n]. *) let liquidity_baking_sunset_level n () = - Context.init ~consensus_threshold:0 1 >>=? fun (blk, _contracts) -> + Context.init1 ~consensus_threshold:0 () >>=? fun (blk, _contract) -> Context.get_liquidity_baking_cpmm_address (B blk) >>=? fun liquidity_baking -> Context.get_constants (B blk) >>=? fun csts -> let sunset = csts.parametric.liquidity_baking_sunset_level in @@ -152,7 +152,7 @@ let liquidity_baking_sunset_level n () = (* Expected level is roughly 2*(log(1-1/(2*p)) / log(0.999)) where [p] is the proportion [LB_off / (LB_on + LB_off)]. *) let liquidity_baking_toggle ~n_vote_on ~n_vote_off ~n_vote_pass expected_level bake_after () = - Context.init ~consensus_threshold:0 1 >>=? fun (blk, _contracts) -> + Context.init1 ~consensus_threshold:0 () >>=? fun (blk, _contract) -> Context.get_liquidity_baking_cpmm_address (B blk) >>=? fun liquidity_baking -> Context.Contract.balance (B blk) liquidity_baking >>=? fun old_balance -> Context.get_liquidity_baking_subsidy (B blk) @@ -198,7 +198,7 @@ let liquidity_baking_toggle_60 n () = (* 50% of blocks have liquidity_baking_toggle_vote = LB_off. Subsidy should not be stopped. *) let liquidity_baking_toggle_50 n () = - Context.init ~consensus_threshold:0 1 >>=? fun (blk, _contracts) -> + Context.init1 ~consensus_threshold:0 () >>=? fun (blk, _contract) -> Context.get_liquidity_baking_cpmm_address (B blk) >>=? fun liquidity_baking -> Context.get_constants (B blk) >>=? fun csts -> let sunset = csts.parametric.liquidity_baking_sunset_level in @@ -228,7 +228,7 @@ let liquidity_baking_toggle_50 n () = n_votes with LB_on, check that the subsidy flows. *) let liquidity_baking_restart n_votes n () = - Context.init ~consensus_threshold:0 1 >>=? fun (blk, _contracts) -> + Context.init1 ~consensus_threshold:0 () >>=? fun (blk, _contract) -> Context.get_liquidity_baking_cpmm_address (B blk) >>=? fun liquidity_baking -> Block.bake_n ~liquidity_baking_toggle_vote:LB_off n_votes blk >>=? fun blk -> Context.Contract.balance (B blk) liquidity_baking @@ -254,7 +254,7 @@ let liquidity_baking_restart n_votes n () = (* Test that the toggle EMA in block metadata is correct. *) let liquidity_baking_toggle_ema n_vote_on n_vote_off level bake_after expected_toggle_ema () = - Context.init ~consensus_threshold:0 1 >>=? fun (blk, _contracts) -> + Context.init1 ~consensus_threshold:0 () >>=? fun (blk, _contract) -> let rec bake_escaping blk i = if i < level then Block.bake_n ~liquidity_baking_toggle_vote:LB_on n_vote_on blk @@ -283,7 +283,7 @@ let liquidity_baking_toggle_ema_threshold () = liquidity_baking_toggle_ema 0 1 1386 1 1_001_000_000 () let liquidity_baking_storage n () = - Context.init ~consensus_threshold:0 1 >>=? fun (blk, _contracts) -> + Context.init1 ~consensus_threshold:0 () >>=? fun (blk, _contract) -> Context.get_liquidity_baking_cpmm_address (B blk) >>=? fun liquidity_baking -> Context.get_liquidity_baking_subsidy (B blk) >>=? fun subsidy -> let expected_storage = @@ -311,7 +311,7 @@ let liquidity_baking_storage n () = >>=? fun () -> return_unit let liquidity_baking_balance_update () = - Context.init ~consensus_threshold:0 1 >>=? fun (blk, _contracts) -> + Context.init1 ~consensus_threshold:0 () >>=? fun (blk, _contract) -> Context.get_liquidity_baking_cpmm_address (B blk) >>=? fun liquidity_baking -> Context.get_constants (B blk) >>=? fun csts -> let sunset = csts.parametric.liquidity_baking_sunset_level in @@ -370,7 +370,7 @@ let get_balance_update_in_result result = | _ -> assert false let liquidity_baking_origination_result_cpmm_address () = - Context.init 1 >>=? fun (blk, _contracts) -> + Context.init1 () >>=? fun (blk, _contract) -> Context.get_liquidity_baking_cpmm_address (B blk) >>=? fun cpmm_address_in_storage -> Block.bake_n_with_origination_results 1 blk @@ -387,7 +387,7 @@ let liquidity_baking_origination_result_cpmm_address () = >>=? fun () -> return_unit let liquidity_baking_origination_result_cpmm_balance () = - Context.init 1 >>=? fun (blk, _contracts) -> + Context.init1 () >>=? fun (blk, _contract) -> Block.bake_n_with_origination_results 1 blk >>=? fun (_blk, origination_results) -> let result = get_cpmm_result origination_results in @@ -396,7 +396,7 @@ let liquidity_baking_origination_result_cpmm_balance () = >>=? fun () -> return_unit let liquidity_baking_origination_result_lqt_address () = - Context.init 1 >>=? fun (blk, _contracts) -> + Context.init1 () >>=? fun (blk, _contract) -> Block.bake_n_with_origination_results 1 blk >>=? fun (_blk, origination_results) -> let result = get_lqt_result origination_results in @@ -411,7 +411,7 @@ let liquidity_baking_origination_result_lqt_address () = >>=? fun () -> return_unit let liquidity_baking_origination_result_lqt_balance () = - Context.init 1 >>=? fun (blk, _contracts) -> + Context.init1 () >>=? fun (blk, _contract) -> Block.bake_n_with_origination_results 1 blk >>=? fun (_blk, origination_results) -> let result = get_lqt_result origination_results in @@ -431,7 +431,7 @@ let liquidity_baking_origination_result_lqt_balance () = (* Test that with no contract at the tzBTC address and the level low enough to indicate we're not on mainnet, three contracts are originated in stitching. *) let liquidity_baking_origination_test_migration () = - Context.init 1 >>=? fun (blk, _contracts) -> + Context.init1 () >>=? fun (blk, _contract) -> Block.bake_n_with_origination_results 1 blk >>=? fun (_blk, origination_results) -> let num_results = List.length origination_results in @@ -439,8 +439,8 @@ let liquidity_baking_origination_test_migration () = (* Test that with no contract at the tzBTC address and the level high enough to indicate we could be on mainnet, no contracts are originated in stitching. *) let liquidity_baking_origination_no_tzBTC_mainnet_migration () = - Context.init ~consensus_threshold:0 ~level:1_437_862l 1 - >>=? fun (blk, _contracts) -> + Context.init1 ~consensus_threshold:0 ~level:1_437_862l () + >>=? fun (blk, _contract) -> (* By baking a bit we also check that the subsidy application with no CPMM present does nothing rather than stopping the chain.*) Block.bake_n_with_origination_results 64 blk >>=? fun (_blk, origination_results) -> diff --git a/src/proto_alpha/lib_protocol/test/pbt/test_carbonated_map.ml b/src/proto_alpha/lib_protocol/test/pbt/test_carbonated_map.ml index e0a588968a30..030c6fea96ef 100644 --- a/src/proto_alpha/lib_protocol/test/pbt/test_carbonated_map.ml +++ b/src/proto_alpha/lib_protocol/test/pbt/test_carbonated_map.ml @@ -39,7 +39,7 @@ let wrap m = m >|= Environment.wrap_tzresult let new_ctxt () = let ( let* ) m f = m >>=? f in - let* (block, _) = Context.init 1 in + let* (block, _contract) = Context.init1 () in let* incr = Incremental.begin_construction block in return @@ Incremental.alpha_ctxt incr diff --git a/src/proto_alpha/lib_protocol/test/pbt/test_gas_properties.ml b/src/proto_alpha/lib_protocol/test/pbt/test_gas_properties.ml index 4eee27d8fca8..13809ed741db 100644 --- a/src/proto_alpha/lib_protocol/test/pbt/test_gas_properties.ml +++ b/src/proto_alpha/lib_protocol/test/pbt/test_gas_properties.ml @@ -91,7 +91,7 @@ let test_consume_commutes (start, cost1, cost2) = let context_arb : Alpha_context.t QCheck.arbitrary = QCheck.always (Lwt_main.run - ( Context.init 1 >>=? fun (b, _contracts) -> + ( Context.init1 () >>=? fun (b, _contract) -> Incremental.begin_construction b >|=? fun inc -> let state = Incremental.validation_state inc in Alpha_context.Gas.set_limit diff --git a/src/proto_alpha/lib_protocol/test/pbt/test_script_comparison.ml b/src/proto_alpha/lib_protocol/test/pbt/test_script_comparison.ml index dc0c058730dc..fb74fb9289d5 100644 --- a/src/proto_alpha/lib_protocol/test/pbt/test_script_comparison.ml +++ b/src/proto_alpha/lib_protocol/test/pbt/test_script_comparison.ml @@ -202,7 +202,7 @@ let assert_return x = assert_ok (Lwt_main.run x) let ctxt = assert_return - ( Context.init 3 >>=? fun (b, _cs) -> + ( Context.init3 () >>=? fun (b, _cs) -> Incremental.begin_construction b >>=? fun v -> return (Incremental.alpha_ctxt v) ) diff --git a/src/proto_alpha/lib_protocol/test/unit/test_gas_monad.ml b/src/proto_alpha/lib_protocol/test/unit/test_gas_monad.ml index 27a82fe49119..31d4dfd25f0b 100644 --- a/src/proto_alpha/lib_protocol/test/unit/test_gas_monad.ml +++ b/src/proto_alpha/lib_protocol/test/unit/test_gas_monad.ml @@ -38,7 +38,7 @@ module GM = Gas_monad let ten_milligas = Gas.fp_of_milligas_int 10 let new_context ~limit = - Context.init 1 >>=? fun (b, _contracts) -> + Context.init1 () >>=? fun (b, _contract) -> Incremental.begin_construction b >|=? fun inc -> let state = Incremental.validation_state inc in Gas.set_limit state.ctxt limit diff --git a/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_arith.ml b/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_arith.ml index 24fe441bc5e4..0ddece927568 100644 --- a/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_arith.ml +++ b/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_arith.ml @@ -36,7 +36,7 @@ open Sc_rollup_arith.ProtocolImplementation open Alpha_context let create_context () = - Context.init 1 >>=? fun (block, _) -> return block.context + Context.init1 () >>=? fun (block, _contract) -> return block.context let setup boot_sector f = create_context () >>=? fun ctxt -> diff --git a/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_inbox.ml b/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_inbox.ml index a822d26016b6..59b777f9fce9 100644 --- a/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_inbox.ml +++ b/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_inbox.ml @@ -45,7 +45,7 @@ let level = Raw_level_repr.of_int32 0l |> function Ok x -> x | _ -> assert false let create_context () = - Context.init 1 >>=? fun (block, _) -> return block.context + Context.init1 () >>=? fun (block, _contract) -> return block.context let test_empty () = let inbox = empty rollup level in diff --git a/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_storage.ml b/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_storage.ml index 7fa91a47bde9..7b689e88cadc 100644 --- a/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_storage.ml +++ b/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_storage.ml @@ -38,7 +38,7 @@ open Lwt_result_syntax let lift k = Lwt.map Environment.wrap_tzresult k let new_context () = - let* (b, _contracts) = Context.init 1 in + let* (b, _contract) = Context.init1 () in Incremental.begin_construction b >|=? fun inc -> let state = Incremental.validation_state inc in let ctxt = state.ctxt in diff --git a/src/proto_alpha/lib_protocol/test/unit/test_tx_rollup_l2.ml b/src/proto_alpha/lib_protocol/test/unit/test_tx_rollup_l2.ml index ea0e34c2c93e..2255140f8348 100644 --- a/src/proto_alpha/lib_protocol/test/unit/test_tx_rollup_l2.ml +++ b/src/proto_alpha/lib_protocol/test/unit/test_tx_rollup_l2.ml @@ -326,7 +326,7 @@ end) let gen_n_ticket_hash n = let x = Lwt_main.run - ( Context.init n >>=? fun (_b, contracts) -> + ( Context.init_n n () >>=? fun (_b, contracts) -> let addressess = gen_n_address n in let tickets = List.map2 -- GitLab From 2e93155d17d02a43ac0a84dcc727cfa87713c9ea Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Fri, 8 Apr 2022 19:23:29 +0200 Subject: [PATCH 3/6] Proto/Tests: generalize init with GADT tuple --- .../lib_protocol/test/helpers/context.ml | 39 +++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.ml b/src/proto_alpha/lib_protocol/test/helpers/context.ml index f6f4aa2bfd1c..64e5de8f16de 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/context.ml @@ -354,12 +354,34 @@ module Tx_rollup = struct Tx_rollup_services.commitment rpc_ctxt ctxt tx_rollup end -let init_gen n get ?rng_state ?commitments ?(initial_balances = []) +type (_, _) tup = + | T1 : ('a, 'a) tup + | T2 : ('a, 'a * 'a) tup + | T3 : ('a, 'a * 'a * 'a) tup + | TList : int -> ('a, 'a list) tup + +let tup_n : type a r. (a, r) tup -> int = function + | T1 -> 1 + | T2 -> 2 + | T3 -> 3 + | TList n -> n + +let tup_get : type a r. (a, r) tup -> a list -> r = + fun tup list -> + match (tup, list) with + | (T1, [v]) -> v + | (T2, [v1; v2]) -> (v1, v2) + | (T3, [v1; v2; v3]) -> (v1, v2, v3) + | (TList _, l) -> l + | _ -> assert false + +let init_gen tup ?rng_state ?commitments ?(initial_balances = []) ?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 ?tx_rollup_enable ?tx_rollup_sunset_level ?tx_rollup_origination_size ?sc_rollup_enable () = + let n = tup_n tup in let accounts = Account.generate_accounts ?rng_state ~initial_balances n in let contracts = List.map @@ -385,20 +407,15 @@ let init_gen n get ?rng_state ?commitments ?(initial_balances = []) ?tx_rollup_origination_size ?sc_rollup_enable accounts - >|=? fun blk -> (blk, get contracts) + >|=? fun blk -> (blk, tup_get tup contracts) -let init_n n = init_gen n (fun cs -> cs) +let init_n n = init_gen (TList n) -let init1 = - init_gen 1 (function [contract_1] -> contract_1 | _ -> assert false) +let init1 = init_gen T1 -let init2 = - init_gen 2 (function - | [contract_1; contract_2] -> (contract_1, contract_2) - | _ -> assert false) +let init2 = init_gen T2 -let init3 = - init_gen 3 (function [c1; c2; c3] -> (c1, c2, c3) | _ -> assert false) +let init3 = init_gen T3 let init_with_constants constants n = let accounts = Account.generate_accounts n in -- GitLab From a8279f71006b2024b380acfa84ecc1d13fd2ffaa Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Fri, 8 Apr 2022 19:40:04 +0200 Subject: [PATCH 4/6] Proto/Tests: simplify Context.Contract.pkh No need for Lwt-tzresult --- .../lib_protocol/test/helpers/context.ml | 6 ++--- .../lib_protocol/test/helpers/context.mli | 2 +- .../lib_protocol/test/helpers/op.ml | 4 ++-- .../test/integration/consensus/test_baking.ml | 6 ++--- .../consensus/test_deactivation.ml | 4 ++-- .../integration/consensus/test_delegation.ml | 2 +- .../consensus/test_participation.ml | 8 +++---- .../operations/test_combined_operations.ml | 4 ++-- .../operations/test_origination.ml | 8 +++---- .../integration/operations/test_transfer.ml | 6 ++--- .../integration/operations/test_voting.ml | 22 +++++++++---------- 11 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.ml b/src/proto_alpha/lib_protocol/test/helpers/context.ml index 64e5de8f16de..67d3a806692b 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/context.ml @@ -258,9 +258,9 @@ module Contract = struct let equal a b = Alpha_context.Contract.compare a b = 0 let pkh c = - Alpha_context.Contract.is_implicit c |> function - | Some p -> return p - | None -> failwith "pkh: only for implicit contracts" + match Alpha_context.Contract.is_implicit c with + | Some p -> p + | None -> Stdlib.failwith "pkh: only for implicit contracts" let balance ctxt contract = Alpha_services.Contract.balance rpc_ctxt ctxt contract diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.mli b/src/proto_alpha/lib_protocol/test/helpers/context.mli index 2709a92c4316..c1ec692cebfe 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/context.mli @@ -117,7 +117,7 @@ module Contract : sig val equal : Contract.t -> Contract.t -> bool - val pkh : Contract.t -> public_key_hash tzresult Lwt.t + val pkh : Contract.t -> public_key_hash (** Returns the balance of a contract, by default the main balance. If the contract is implicit the frozen balances are available too: diff --git a/src/proto_alpha/lib_protocol/test/helpers/op.ml b/src/proto_alpha/lib_protocol/test/helpers/op.ml index 4e3368c7d3ce..efd574bc1567 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/op.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/op.ml @@ -453,7 +453,7 @@ let seed_nonce_revelation ctxt level nonce = } let proposals ctxt (pkh : Contract.t) proposals = - Context.Contract.pkh pkh >>=? fun source -> + let source = Context.Contract.pkh pkh in Context.Vote.get_current_period ctxt >>=? fun {voting_period = {index; _}; _} -> let op = Proposals {source; period = index; proposals} in @@ -461,7 +461,7 @@ let proposals ctxt (pkh : Contract.t) proposals = sign account.sk ctxt (Contents_list (Single op)) let ballot ctxt (pkh : Contract.t) proposal ballot = - Context.Contract.pkh pkh >>=? fun source -> + let source = Context.Contract.pkh pkh in Context.Vote.get_current_period ctxt >>=? fun {voting_period = {index; _}; _} -> let op = Ballot {source; period = index; proposal; ballot} in diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_baking.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_baking.ml index cc083464c23c..63f7b3b2de7a 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_baking.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_baking.ml @@ -154,7 +154,7 @@ let test_voting_power_cache () = let test_basic_baking_reward () = Context.init1 ~consensus_threshold:0 () >>=? fun (genesis, baker) -> Block.bake genesis >>=? fun b -> - Context.Contract.pkh baker >>=? fun baker_pkh -> + let baker_pkh = Context.Contract.pkh baker in Context.Contract.balance (B b) baker >>=? fun bal -> Context.Delegate.current_frozen_deposits (B b) baker_pkh >>=? fun frozen_deposit -> @@ -170,7 +170,7 @@ let get_contract_for_pkh contracts pkh = let rec find_contract = function | [] -> assert false | c :: t -> - Context.Contract.pkh c >>=? fun c_pkh -> + let c_pkh = Context.Contract.pkh c in if Signature.Public_key_hash.equal c_pkh pkh then return c else find_contract t in @@ -313,7 +313,7 @@ let test_enough_active_stake_to_bake ~has_active_stake () = let initial_bal1 = if has_active_stake then tpr else Int64.sub tpr 1L in Context.init2 ~initial_balances:[initial_bal1; tpr] ~consensus_threshold:0 () >>=? fun (b0, (account1, _account2)) -> - Context.Contract.pkh account1 >>=? fun pkh1 -> + let pkh1 = Context.Contract.pkh account1 in Context.get_constants (B b0) >>=? fun Constants.{parametric = {baking_reward_fixed_portion; _}; _} -> Block.bake ~policy:(By_account pkh1) b0 >>= fun b1 -> diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_deactivation.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_deactivation.ml index 2c9daffd2ef7..84544c955cbc 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_deactivation.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_deactivation.ml @@ -87,7 +87,7 @@ let check_no_stake ~loc (b : Block.t) (account : Account.t) = let test_simple_staking_rights () = Context.init2 () >>=? fun (b, (a1, _a2)) -> Context.Contract.balance (B b) a1 >>=? fun balance -> - Context.Contract.pkh a1 >>=? fun delegate1 -> + let delegate1 = Context.Contract.pkh a1 in Context.Delegate.current_frozen_deposits (B b) delegate1 >>=? fun frozen_deposits -> let expected_initial_balance = @@ -112,7 +112,7 @@ let test_simple_staking_rights_after_baking () = Context.Contract.manager (B b) a2 >>=? fun m2 -> Block.bake_n ~policy:(By_account m2.pkh) 5 b >>=? fun b -> Context.Contract.balance (B b) a1 >>=? fun balance -> - Context.Contract.pkh a1 >>=? fun delegate1 -> + let delegate1 = Context.Contract.pkh a1 in Context.Delegate.current_frozen_deposits (B b) delegate1 >>=? fun frozen_deposits -> balance +? frozen_deposits >>?= fun full_balance -> diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_delegation.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_delegation.ml index 8696d98612e3..2b46c3e97674 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_delegation.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_delegation.ml @@ -79,7 +79,7 @@ let bootstrap_manager_is_bootstrap_delegate () = (** Bootstrap contracts cannot change their delegate. *) let bootstrap_delegate_cannot_change ~fee () = Context.init2 () >>=? fun (b, (bootstrap0, bootstrap1)) -> - Context.Contract.pkh bootstrap0 >>=? fun pkh1 -> + let pkh1 = Context.Contract.pkh bootstrap0 in Incremental.begin_construction b ~policy:(Block.Excluding [pkh1]) >>=? fun i -> Context.Contract.manager (I i) bootstrap1 >>=? fun manager1 -> diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_participation.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_participation.ml index 0067b1b1902f..dd930553708e 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_participation.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_participation.ml @@ -81,8 +81,8 @@ let test_participation ~sufficient_participation () = let (account1, account2) = match accounts with a1 :: a2 :: _ -> (a1, a2) | _ -> assert false in - Context.Contract.pkh account1 >>=? fun del1 -> - Context.Contract.pkh account2 >>=? fun del2 -> + let del1 = Context.Contract.pkh account1 in + let del2 = Context.Contract.pkh account2 in Block.bake ~policy:(By_account del1) b0 >>=? fun b1 -> (* To separate concerns, only [del1] bakes: this way, we don't need to consider baking rewards for [del2]. Delegate [del2] endorses only @@ -126,8 +126,8 @@ let test_participation ~sufficient_participation () = let test_participation_rpc () = let n_accounts = 2 in Context.init2 ~consensus_threshold:1 () >>=? fun (b0, (account1, account2)) -> - Context.Contract.pkh account1 >>=? fun del1 -> - Context.Contract.pkh account2 >>=? fun del2 -> + let del1 = Context.Contract.pkh account1 in + let del2 = Context.Contract.pkh account2 in Context.get_constants (B b0) >>=? fun csts -> let blocks_per_cycle = Int32.to_int csts.parametric.blocks_per_cycle in let Constants.{numerator; denominator} = diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_combined_operations.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_combined_operations.ml index faff59040f8f..ac158b005df2 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_combined_operations.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_combined_operations.ml @@ -58,7 +58,7 @@ let test_multiple_transfers () = (1 -- 10) >>=? fun ops -> Op.combine_operations ~source:c1 (B blk) ops >>=? fun operation -> - Context.Contract.pkh c3 >>=? fun baker_pkh -> + let baker_pkh = Context.Contract.pkh c3 in Incremental.begin_construction ~policy:(By_account baker_pkh) blk >>=? fun inc -> Context.Contract.balance (I inc) c1 >>=? fun c1_old_balance -> @@ -85,7 +85,7 @@ let test_multiple_origination_and_delegation () = let n = 10 in Context.get_constants (B blk) >>=? fun {parametric = {origination_size; cost_per_byte; _}; _} -> - Context.Contract.pkh c2 >>=? fun delegate_pkh -> + let delegate_pkh = Context.Contract.pkh c2 in (* Deploy n smart contracts with dummy scripts from c1 *) List.map_es (fun i -> diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_origination.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_origination.ml index 17059e11c40f..2385c1aff1bf 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_origination.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_origination.ml @@ -66,8 +66,8 @@ let total_fees_for_origination ?(fee = Tez.zero) ?(credit = Tez.zero) b = the bake is done by another delegated. *) let test_origination_balances ~loc:_ ?(fee = Tez.zero) ?(credit = Tez.zero) () = Context.init2 () >>=? fun (b, (source, contract_for_bake)) -> - Context.Contract.pkh source >>=? fun pkh_for_orig -> - Context.Contract.pkh contract_for_bake >>=? fun pkh_for_bake -> + let pkh_for_orig = Context.Contract.pkh source in + let pkh_for_bake = Context.Contract.pkh contract_for_bake in Op.contract_origination (B b) source ~fee ~credit ~script:Op.dummy_script >>=? fun (operation, new_contract) -> total_fees_for_origination ~fee ~credit b >>=? fun total_fee -> @@ -95,8 +95,8 @@ let test_origination_balances ~loc:_ ?(fee = Tez.zero) ?(credit = Tez.zero) () = set to true meaning that this contract is able to delegate. *) let register_origination ?(fee = Tez.zero) ?(credit = Tez.zero) () = Context.init2 () >>=? fun (b, (source, contract_for_bake)) -> - Context.Contract.pkh source >>=? fun source_pkh -> - Context.Contract.pkh contract_for_bake >>=? fun pkh_for_bake -> + let source_pkh = Context.Contract.pkh source in + let pkh_for_bake = Context.Contract.pkh contract_for_bake in Op.contract_origination (B b) source ~fee ~credit ~script:Op.dummy_script >>=? fun (operation, originated) -> Block.bake ~operation ~policy:(By_account pkh_for_bake) b >>=? fun b -> diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_transfer.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_transfer.ml index b658448b9b09..c4f27750fb37 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_transfer.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_transfer.ml @@ -140,7 +140,7 @@ let test_transfer_to_originate_with_fee () = (** Transfer from balance. *) let test_transfer_amount_of_contract_balance () = Context.init2 () >>=? fun (b, (contract_1, contract_2)) -> - Context.Contract.pkh contract_1 >>=? fun pkh1 -> + let pkh1 = Context.Contract.pkh contract_1 in (* given that contract_1 no longer has a sufficient balance to bake, make sure it cannot be chosen as baker *) Incremental.begin_construction b ~policy:(Block.Excluding [pkh1]) @@ -169,7 +169,7 @@ let test_missing_transaction () = Context.init2 () >>=? fun (b, (contract_1, contract_2)) -> (* given that contract_1 no longer has a sufficient balance to bake, make sure it cannot be chosen as baker *) - Context.Contract.pkh contract_1 >>=? fun pkh1 -> + let pkh1 = Context.Contract.pkh contract_1 in Incremental.begin_construction b ~policy:(Block.Excluding [pkh1]) >>=? fun b -> two_over_n_of_balance b contract_1 6L >>=? fun amount -> @@ -521,7 +521,7 @@ let test_random_transfer () = let contracts = Array.of_list contracts in let source = random_contract contracts in let dest = random_contract contracts in - Context.Contract.pkh source >>=? fun source_pkh -> + let source_pkh = Context.Contract.pkh source in (* given that source may not have a sufficient balance for the transfer + to bake, make sure it cannot be chosen as baker *) Incremental.begin_construction b ~policy:(Block.Excluding [source_pkh]) diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_voting.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_voting.ml index 2e68b6853558..255af2a744bf 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_voting.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_voting.ml @@ -212,7 +212,7 @@ let get_power b delegates loc = Context.Vote.get_listings (B b) >>=? fun l -> List.map_es (fun delegate -> - Context.Contract.pkh delegate >>=? fun pkh -> + let pkh = Context.Contract.pkh delegate in match List.find_opt (fun (del, _) -> del = pkh) l with | None -> failwith "%s - Missing delegate" loc | Some (_, power) -> return power) @@ -358,7 +358,7 @@ let test_successful_vote num_delegates () = | l -> List.iter_es (fun delegate -> - Context.Contract.pkh delegate >>=? fun pkh -> + let pkh = Context.Contract.pkh delegate in match List.find_opt (fun (del, _) -> del = pkh) l with | None -> failwith "%s - Missing delegate" __LOC__ | Some (_, Vote.Yay) -> return_unit @@ -413,7 +413,7 @@ let test_successful_vote num_delegates () = | l -> List.iter_es (fun delegate -> - Context.Contract.pkh delegate >>=? fun pkh -> + let pkh = Context.Contract.pkh delegate in match List.find_opt (fun (del, _) -> del = pkh) l with | None -> failwith "%s - Missing delegate" __LOC__ | Some (_, Vote.Yay) -> return_unit @@ -621,7 +621,7 @@ let test_multiple_identical_proposals_count_as_one () = (* compute the weight of proposals *) Context.Vote.get_proposals (B b) >>=? fun ps -> (* compute the voting power of proposer *) - Context.Contract.pkh proposer >>=? fun pkh -> + let pkh = Context.Contract.pkh proposer in Context.Vote.get_listings (B b) >>=? fun l -> (match List.find_opt (fun (del, _) -> del = pkh) l with | None -> failwith "%s - Missing delegate" __LOC__ @@ -655,8 +655,9 @@ let test_supermajority_in_proposal there_is_a_winner () = let del1 = WithExceptions.Option.get ~loc:__LOC__ @@ List.nth delegates 0 in let del2 = WithExceptions.Option.get ~loc:__LOC__ @@ List.nth delegates 1 in let del3 = WithExceptions.Option.get ~loc:__LOC__ @@ List.nth delegates 2 in - List.map_es (fun del -> Context.Contract.pkh del) [del1; del2; del3] - >>=? fun pkhs -> + let pkhs = + List.map (fun del -> Context.Contract.pkh del) [del1; del2; del3] + in let policy = Block.Excluding pkhs in Op.transaction (B b) @@ -707,8 +708,7 @@ let test_quorum_in_proposal has_quorum () = >>=? fun {parametric = {min_proposal_quorum; _}; _} -> let del1 = WithExceptions.Option.get ~loc:__LOC__ @@ List.nth delegates 0 in let del2 = WithExceptions.Option.get ~loc:__LOC__ @@ List.nth delegates 1 in - List.map_es (fun del -> Context.Contract.pkh del) [del1; del2] - >>=? fun pkhs -> + let pkhs = List.map (fun del -> Context.Contract.pkh del) [del1; del2] in let policy = Block.Excluding pkhs in let quorum = if has_quorum then Int64.of_int32 min_proposal_quorum @@ -895,9 +895,9 @@ let test_voting_power_updated_each_voting_period () = let con2 = WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 1 in let con3 = WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 2 in (* Get the key hashes of the bakers *) - Context.Contract.pkh con1 >>=? fun baker1 -> - Context.Contract.pkh con2 >>=? fun baker2 -> - Context.Contract.pkh con3 >>=? fun baker3 -> + let baker1 = Context.Contract.pkh con1 in + let baker2 = Context.Contract.pkh con2 in + let baker3 = Context.Contract.pkh con3 in (* Retrieve balance of con1 *) let open Test_tez in Context.Contract.balance (B genesis) con1 >>=? fun balance1 -> -- GitLab From 1f1f67a7f0cb8d221d9c9ad85ac66e8cea0eaf0e Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Mon, 11 Apr 2022 08:57:15 +0200 Subject: [PATCH 5/6] Proto/Tests: simplify calls to Context.init_with_constants init_with_constants1, init_with_constants2 are used whenever possible. init_with_constants is renamed into init_with_constants_n to discourage its usage. --- .../lib_protocol/test/helpers/context.ml | 12 ++++- .../lib_protocol/test/helpers/context.mli | 10 +++- .../consensus/test_double_endorsement.ml | 23 ++------ .../consensus/test_frozen_deposits.ml | 54 ++++++------------- .../test/integration/consensus/test_seed.ml | 11 +--- .../michelson/test_block_time_instructions.ml | 2 +- .../integration/operations/test_sc_rollup.ml | 2 +- .../integration/operations/test_tx_rollup.ml | 23 +++----- 8 files changed, 47 insertions(+), 90 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.ml b/src/proto_alpha/lib_protocol/test/helpers/context.ml index 67d3a806692b..b881b3ed7a07 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/context.ml @@ -417,7 +417,8 @@ let init2 = init_gen T2 let init3 = init_gen T3 -let init_with_constants constants n = +let init_with_constants_gen tup constants = + let n = tup_n tup in let accounts = Account.generate_accounts n in let contracts = List.map @@ -435,7 +436,14 @@ let init_with_constants constants n = let parameters = Default_parameters.parameters_of_constants ~bootstrap_accounts constants in - Block.genesis_with_parameters parameters >|=? fun blk -> (blk, contracts) + Block.genesis_with_parameters parameters >|=? fun blk -> + (blk, tup_get tup contracts) + +let init_with_constants_n consts n = init_with_constants_gen (TList n) consts + +let init_with_constants1 = init_with_constants_gen T1 + +let init_with_constants2 = init_with_constants_gen T2 let default_raw_context () = let initial_accounts = diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.mli b/src/proto_alpha/lib_protocol/test/helpers/context.mli index c1ec692cebfe..7c6d2bbd2207 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/context.mli @@ -245,11 +245,19 @@ val init3 : * Alpha_context.Contract.t) init -val init_with_constants : +val init_with_constants_n : Constants.parametric -> int -> (Block.t * Alpha_context.Contract.t list) tzresult Lwt.t +val init_with_constants1 : + Constants.parametric -> (Block.t * Alpha_context.Contract.t) tzresult Lwt.t + +val init_with_constants2 : + Constants.parametric -> + (Block.t * (Alpha_context.Contract.t * Alpha_context.Contract.t)) tzresult + Lwt.t + (** [default_raw_context] returns a [Raw_context.t] for use in tests below [Alpha_context] *) val default_raw_context : unit -> Raw_context.t tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_endorsement.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_endorsement.ml index 56a50dea5829..9c65e938bc6c 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_endorsement.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_endorsement.ml @@ -49,22 +49,6 @@ let block_fork b = (* Tests *) (****************************************************************) -let get_first_2_accounts_contracts contracts = - let ((contract1, account1), (contract2, account2)) = - match contracts with - | [a1; a2] -> - ( ( a1, - Contract.is_implicit a1 |> function - | None -> assert false - | Some pkh -> pkh ), - ( a2, - Contract.is_implicit a2 |> function - | None -> assert false - | Some pkh -> pkh ) ) - | _ -> assert false - in - ((contract1, account1), (contract2, account2)) - let order_endorsements ~correct_order op1 op2 = let oph1 = Operation.hash op1 in let oph2 = Operation.hash op2 in @@ -362,10 +346,9 @@ let test_freeze_more_with_low_balance = {numerator = 1; denominator = 2}; } in - Context.init_with_constants constants 2 >>=? fun (genesis, contracts) -> - let ((_contract1, account1), (_contract2, account2)) = - get_first_2_accounts_contracts contracts - in + Context.init_with_constants2 constants >>=? fun (genesis, (c1, c2)) -> + let account1 = Context.Contract.pkh c1 in + let account2 = Context.Contract.pkh c2 in (* we empty the available balance of [account1]. *) Context.Delegate.info (B genesis) account1 >>=? fun info1 -> Op.transaction diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml index 1b15f0387965..ceab5304647e 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml @@ -46,21 +46,8 @@ let constants = origination_size = 0; } -let get_first_2_accounts_contracts contracts = - let ((contract1, account1), (contract2, account2)) = - match contracts with - | [a1; a2] -> - ( ( a1, - Contract.is_implicit a1 |> function - | None -> assert false - | Some pkh -> pkh ), - ( a2, - Contract.is_implicit a2 |> function - | None -> assert false - | Some pkh -> pkh ) ) - | _ -> assert false - in - ((contract1, account1), (contract2, account2)) +let get_first_2_accounts_contracts (a1, a2) = + ((a1, Context.Contract.pkh a1), (a2, Context.Contract.pkh a2)) (* Terminology: @@ -80,7 +67,7 @@ let get_first_2_accounts_contracts contracts = - full balance = spendable balance + frozen deposits; obtained with Delegate.full_balance *) let test_invariants () = - Context.init_with_constants constants 2 >>=? fun (genesis, contracts) -> + Context.init_with_constants2 constants >>=? fun (genesis, contracts) -> let ((contract1, account1), (contract2, _account2)) = get_first_2_accounts_contracts contracts in @@ -141,7 +128,7 @@ let test_invariants () = Assert.equal_tez ~loc:__LOC__ new_frozen_deposits expected_new_frozen_deposits let test_set_limit balance_percentage () = - Context.init_with_constants constants 2 >>=? fun (genesis, contracts) -> + Context.init_with_constants2 constants >>=? fun (genesis, contracts) -> let ((contract1, account1), (_contract2, account2)) = get_first_2_accounts_contracts contracts in @@ -199,7 +186,7 @@ let test_set_limit balance_percentage () = Assert.equal_tez ~loc:__LOC__ frozen_deposits limit let test_set_too_high_limit () = - Context.init_with_constants constants 2 >>=? fun (genesis, contracts) -> + Context.init_with_constants2 constants >>=? fun (genesis, contracts) -> let ((contract1, _account1), _) = get_first_2_accounts_contracts contracts in let max_limit = Tez.of_mutez_exn @@ -228,7 +215,7 @@ let test_set_too_high_limit () = Incremental.finalize_block b >>=? fun _ -> return_unit let test_unset_limit () = - Context.init_with_constants constants 2 >>=? fun (genesis, contracts) -> + Context.init_with_constants2 constants >>=? fun (genesis, contracts) -> let ((contract1, account1), (_contract2, account2)) = get_first_2_accounts_contracts contracts in @@ -275,7 +262,7 @@ let test_unset_limit () = >>=? fun () -> return_unit let test_cannot_bake_with_zero_deposits () = - Context.init_with_constants constants 2 >>=? fun (genesis, contracts) -> + Context.init_with_constants2 constants >>=? fun (genesis, contracts) -> let ((contract1, account1), (_contract2, account2)) = get_first_2_accounts_contracts contracts in @@ -308,7 +295,7 @@ let test_cannot_bake_with_zero_deposits () = Assert.error ~loc:__LOC__ b1 (fun _ -> true) let test_deposits_after_stake_removal () = - Context.init_with_constants constants 2 >>=? fun (genesis, contracts) -> + Context.init_with_constants2 constants >>=? fun (genesis, contracts) -> let ((contract1, account1), (contract2, account2)) = get_first_2_accounts_contracts contracts in @@ -376,7 +363,7 @@ let test_deposits_after_stake_removal () = Assert.equal_tez ~loc:__LOC__ frozen_deposits_2 expected_new_frozen_deposits_2 let test_unfreeze_deposits_after_deactivation () = - Context.init_with_constants constants 2 >>=? fun (genesis, contracts) -> + Context.init_with_constants2 constants >>=? fun (genesis, contracts) -> let ((contract1, account1), (_contract2, account2)) = get_first_2_accounts_contracts contracts in @@ -422,7 +409,7 @@ let test_unfreeze_deposits_after_deactivation () = loop genesis cycles_to_bake >>=? fun _b -> return_unit let test_frozen_deposits_with_delegation () = - Context.init_with_constants constants 2 >>=? fun (genesis, contracts) -> + Context.init_with_constants2 constants >>=? fun (genesis, contracts) -> let ((_contract1, account1), (contract2, account2)) = get_first_2_accounts_contracts contracts in @@ -482,7 +469,7 @@ let test_frozen_deposits_with_delegation () = loop b cycles_to_bake >>=? fun _b -> return_unit let test_frozen_deposits_with_overdelegation () = - Context.init_with_constants constants 2 >>=? fun (genesis, contracts) -> + Context.init_with_constants2 constants >>=? fun (genesis, contracts) -> let ((contract1, account1), (contract2, account2)) = get_first_2_accounts_contracts contracts in @@ -561,7 +548,7 @@ let test_frozen_deposits_with_overdelegation () = let test_set_limit_with_overdelegation () = let constants = {constants with frozen_deposits_percentage = 10} in - Context.init_with_constants constants 2 >>=? fun (genesis, contracts) -> + Context.init_with_constants2 constants >>=? fun (genesis, contracts) -> let ((contract1, account1), (contract2, account2)) = get_first_2_accounts_contracts contracts in @@ -629,20 +616,9 @@ let test_set_limit_with_overdelegation () = (** This test fails when [to_cycle] in [Delegate.freeze_deposits] is smaller than [new_cycle + preserved_cycles]. *) let test_error_is_thrown_when_smaller_upper_bound_for_frozen_window () = - Context.init_with_constants constants 2 >>=? fun (genesis, contracts) -> - let ((contract1, account1), (contract2, _account2)) = - match contracts with - | [a1; a2] -> - ( ( a1, - Contract.is_implicit a1 |> function - | None -> assert false - | Some pkh -> pkh ), - ( a2, - Contract.is_implicit a2 |> function - | None -> assert false - | Some pkh -> pkh ) ) - | _ -> assert false - in + Context.init_with_constants2 constants >>=? fun (genesis, contracts) -> + let (contract1, contract2) = contracts in + let account1 = Context.Contract.pkh contract1 in (* [account2] delegates (through [new_account]) to [account1] its spendable balance. The point is to make [account1] have a lot of staking balance so that, after [preserved_cycles] when the active stake reflects this increase diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_seed.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_seed.ml index 6fcafa49efd3..85cf20c0e645 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_seed.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_seed.ml @@ -188,15 +188,8 @@ let test_unrevealed () = minimal_participation_ratio = Constants.{numerator = 0; denominator = 1}; } in - Context.init_with_constants constants 2 >>=? fun (b, accounts) -> - let (account1, account2) = - match accounts with a1 :: a2 :: _ -> (a1, a2) | _ -> assert false - in - let (_delegate1, delegate2) = - match (Contract.is_implicit account1, Contract.is_implicit account2) with - | (Some d, Some d') -> (d, d') - | _ -> assert false - in + Context.init_with_constants2 constants >>=? fun (b, (_account1, account2)) -> + let delegate2 = Context.Contract.pkh account2 in (* Delegate 2 will add a nonce but never reveals it *) Context.get_constants (B b) >>=? fun csts -> let blocks_per_commitment = diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_block_time_instructions.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_block_time_instructions.ml index 09ae3ed90c6d..44a4a6a8887e 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_block_time_instructions.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_block_time_instructions.ml @@ -37,7 +37,7 @@ open Alpha_context let context_with_constants constants = let open Lwt_result_syntax in - let* (block, _contracts) = Context.init_with_constants constants 1 in + let* (block, _contracts) = Context.init_with_constants1 constants in let+ incremental = Incremental.begin_construction block in Incremental.alpha_ctxt incremental diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_sc_rollup.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_sc_rollup.ml index cacd247bda80..9f7c76e9ee52 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_sc_rollup.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_sc_rollup.ml @@ -44,7 +44,7 @@ let err x = Exn (Sc_rollup_test_error x) [sc_rollup_enable] constant is set to true. It returns the created context and [n] contracts. *) let context_init n = - Context.init_with_constants + Context.init_with_constants_n { Context.default_test_constants with sc_rollup_enable = true; diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml index 635a8b8ee993..3d69805c47b9 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml @@ -64,13 +64,9 @@ let is_implicit_exn x = (** [test_disable_feature_flag] try to originate a tx rollup with the feature flag is deactivated and check it fails *) let test_disable_feature_flag () = - Context.init_with_constants + Context.init_with_constants1 {Context.default_test_constants with tx_rollup_enable = false} - 1 - >>=? fun (b, contracts) -> - let contract = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 0 - in + >>=? fun (b, contract) -> Incremental.begin_construction b >>=? fun i -> Op.tx_rollup_origination (I i) contract >>=? fun (op, _tx_rollup) -> Incremental.add_operation @@ -82,17 +78,13 @@ let test_disable_feature_flag () = (** [test_sunset] try to originate a tx rollup after the sunset and check that it fails *) let test_sunset () = - Context.init_with_constants + Context.init_with_constants1 { Context.default_test_constants with tx_rollup_enable = true; tx_rollup_sunset_level = 0l; } - 1 - >>=? fun (b, contracts) -> - let contract = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 0 - in + >>=? fun (b, contract) -> Incremental.begin_construction b >>=? fun i -> Op.tx_rollup_origination (I i) contract >>=? fun (op, _tx_rollup) -> Incremental.add_operation @@ -203,7 +195,7 @@ let context_init ?(tx_rollup_max_inboxes_count = 2100) ?(tx_rollup_finality_period = 1) ?(tx_rollup_origination_size = 60_000) ?(cost_per_byte = Tez.zero) ?(tx_rollup_hard_size_limit_per_message = 5_000) n = - Context.init_with_constants + Context.init_with_constants_n { Context.default_test_constants with consensus_threshold = 0; @@ -1936,10 +1928,7 @@ let test_full_inbox () = tx_rollup_max_inboxes_count = 15; } in - Context.init_with_constants constants 1 >>=? fun (b, contracts) -> - let contract = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 0 - in + Context.init_with_constants1 constants >>=? fun (b, contract) -> originate b contract >>=? fun (b, tx_rollup) -> let range start top = let rec aux n acc = if n < start then acc else aux (n - 1) (n :: acc) in -- GitLab From 32ec8e7b76c1b1cac4fb5952cfdb94f39e4198aa Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Mon, 11 Apr 2022 09:15:14 +0200 Subject: [PATCH 6/6] Proto/Tests: further simplifications of calls to Context.init_with_constants_n init_with_constants_n is actually never used generically --- .../lib_protocol/test/helpers/context.ml | 9 +++ .../lib_protocol/test/helpers/context.mli | 13 ++++ .../integration/operations/test_sc_rollup.ml | 45 +++++-------- .../integration/operations/test_tx_rollup.ml | 64 ++++++------------- 4 files changed, 59 insertions(+), 72 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.ml b/src/proto_alpha/lib_protocol/test/helpers/context.ml index b881b3ed7a07..07c5f3953a5b 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/context.ml @@ -360,6 +360,15 @@ type (_, _) tup = | T3 : ('a, 'a * 'a * 'a) tup | TList : int -> ('a, 'a list) tup +let tup_hd : type a r. (a, r) tup -> r -> a = + fun tup elts -> + match (tup, elts) with + | (T1, v) -> v + | (T2, (v, _)) -> v + | (T3, (v, _, _)) -> v + | (TList _, v :: _) -> v + | (TList _, []) -> assert false + let tup_n : type a r. (a, r) tup -> int = function | T1 -> 1 | T2 -> 2 diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.mli b/src/proto_alpha/lib_protocol/test/helpers/context.mli index 7c6d2bbd2207..6f4b244732f6 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/context.mli @@ -202,6 +202,14 @@ module Tx_rollup : sig Tx_rollup_commitment.Submitted_commitment.t option tzresult Lwt.t end +type (_, _) tup = + | T1 : ('a, 'a) tup + | T2 : ('a, 'a * 'a) tup + | T3 : ('a, 'a * 'a * 'a) tup + | TList : int -> ('a, 'a list) tup + +val tup_hd : ('a, 'elts) tup -> 'elts -> 'a + type 'accounts init := ?rng_state:Random.State.t -> ?commitments:Commitment.t list -> @@ -245,6 +253,11 @@ val init3 : * Alpha_context.Contract.t) init +val init_with_constants_gen : + (Alpha_context.Contract.t, 'contracts) tup -> + Constants.parametric -> + (Block.t * 'contracts) tzresult Lwt.t + val init_with_constants_n : Constants.parametric -> int -> diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_sc_rollup.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_sc_rollup.ml index 9f7c76e9ee52..d890fa07fdea 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_sc_rollup.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_sc_rollup.ml @@ -40,18 +40,18 @@ exception Sc_rollup_test_error of string let err x = Exn (Sc_rollup_test_error x) -(** [context_init n] initializes a context for testing in which the +(** [context_init tup] initializes a context for testing in which the [sc_rollup_enable] constant is set to true. It returns the created - context and [n] contracts. *) -let context_init n = - Context.init_with_constants_n + context and contracts. *) +let context_init tup = + Context.init_with_constants_gen + tup { Context.default_test_constants with sc_rollup_enable = true; consensus_threshold = 0; sc_rollup_challenge_window_in_blocks = 10; } - n (** [test_disable_feature_flag ()] tries to originate a smart contract rollup when the feature flag is deactivated and checks that it @@ -105,11 +105,9 @@ let test_sc_rollups_all_well_defined () = all_names_are_valid () (** Initializes the context and originates a SCORU. *) -let init_and_originate n = - let* (ctxt, contracts) = context_init n in - let contract = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 0 - in +let init_and_originate tup = + let* (ctxt, contracts) = context_init tup in + let contract = Context.tup_hd tup contracts in let kind = Sc_rollup.Kind.Example_arith in let* (operation, rollup) = Op.sc_rollup_origination (B ctxt) contract kind "" @@ -147,10 +145,8 @@ let dummy_commitment = (** [test_publish_and_cement] creates a rollup, publishes a commitment and then 20 blocks later cements that commitment *) let test_publish_and_cement () = - let* (ctxt, contracts, rollup) = init_and_originate 2 in - let contract = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 1 - in + let* (ctxt, contracts, rollup) = init_and_originate Context.T2 in + let (_, contract) = contracts in let* operation = Op.sc_rollup_publish (B ctxt) contract rollup dummy_commitment in @@ -169,10 +165,8 @@ let test_publish_and_cement () = without waiting for the challenge period to elapse. We check that this fails with the correct error. *) let test_cement_fails_if_premature () = - let* (ctxt, contracts, rollup) = init_and_originate 2 in - let contract = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 1 - in + let* (ctxt, contracts, rollup) = init_and_originate Context.T2 in + let (_, contract) = contracts in let* operation = Op.sc_rollup_publish (B ctxt) contract rollup dummy_commitment in @@ -197,10 +191,8 @@ let test_cement_fails_if_premature () = publishes two different commitments with the same staker. We check that the second publish fails. *) let test_publish_fails_on_backtrack () = - let* (ctxt, contracts, rollup) = init_and_originate 2 in - let contract = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 1 - in + let* (ctxt, contracts, rollup) = init_and_originate Context.T2 in + let (_, contract) = contracts in let commitment1 = dummy_commitment in let commitment2 = {dummy_commitment with number_of_ticks = number_of_ticks_exn 3001l} @@ -227,13 +219,8 @@ let test_publish_fails_on_backtrack () = cement one of the commitments; it checks that this fails because the commitment is contested. *) let test_cement_fails_on_conflict () = - let* (ctxt, contracts, rollup) = init_and_originate 3 in - let contract1 = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 1 - in - let contract2 = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 2 - in + let* (ctxt, contracts, rollup) = init_and_originate Context.T3 in + let (_, contract1, contract2) = contracts in let commitment1 = dummy_commitment in let commitment2 = {dummy_commitment with number_of_ticks = number_of_ticks_exn 3001l} diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml index 3d69805c47b9..46ee8d89f26c 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml @@ -186,16 +186,17 @@ let inbox_burn state size = the inbox. *) let burn_per_byte state = inbox_burn state 1 -(** [context_init n] initializes a context with no consensus rewards +(** [context_init tup] initializes a context with no consensus rewards to not interfere with balances prediction. It returns the created - context and [n] contracts. *) + context and contracts. *) let context_init ?(tx_rollup_max_inboxes_count = 2100) ?(tx_rollup_rejection_max_proof_size = 30_000) ?(tx_rollup_max_ticket_payload_size = 10_240) ?(tx_rollup_finality_period = 1) ?(tx_rollup_origination_size = 60_000) ?(cost_per_byte = Tez.zero) ?(tx_rollup_hard_size_limit_per_message = 5_000) - n = - Context.init_with_constants_n + tup = + Context.init_with_constants_gen + tup { Context.default_test_constants with consensus_threshold = 0; @@ -214,7 +215,6 @@ let context_init ?(tx_rollup_max_inboxes_count = 2100) tx_rollup_max_ticket_payload_size; cost_per_byte; } - n (** [context_init1] initializes a context with no consensus rewards to not interfere with balances prediction. It returns the created @@ -231,10 +231,7 @@ let context_init1 ?tx_rollup_max_inboxes_count ?tx_rollup_origination_size ?cost_per_byte ?tx_rollup_hard_size_limit_per_message - 1 - >|=? function - | (b, contract_1 :: _) -> (b, contract_1) - | (_, _) -> assert false + Context.T1 (** [context_init2] initializes a context with no consensus rewards to not interfere with balances prediction. It returns the created @@ -247,10 +244,7 @@ let context_init2 ?tx_rollup_max_inboxes_count ?tx_rollup_max_ticket_payload_size ?cost_per_byte ?tx_rollup_hard_size_limit_per_message - 2 - >|=? function - | (b, contract_1 :: contract_2 :: _) -> (b, (contract_1, contract_2)) - | (_, _) -> assert false + Context.T2 (** [originate b contract] originates a tx_rollup from [contract], and returns the new block and the tx_rollup address. *) @@ -1415,10 +1409,7 @@ let test_valid_deposit_invalid_amount () = let test_deposit_too_many_tickets () = let too_many = Z.succ (Z.of_int64 Int64.max_int) in let (_, _, pkh) = gen_l2_account () in - context_init 1 >>=? fun (block, accounts) -> - let account1 = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth accounts 0 - in + context_init1 () >>=? fun (block, account1) -> originate block account1 >>=? fun (block, tx_rollup) -> Nat_ticket.init_deposit too_many block tx_rollup account1 >>=? fun (operation, b, deposit_contract) -> @@ -1455,11 +1446,9 @@ let test_deposit_by_non_internal_operation () = (** Test that block finalization changes gas rates *) let test_finalization () = - context_init ~tx_rollup_max_inboxes_count:5_000 2 >>=? fun (b, contracts) -> - let filler = WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 0 in - let contract = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 0 - in + context_init2 ~tx_rollup_max_inboxes_count:5_000 () >>=? fun (b, contracts) -> + let (contract, _) = contracts in + let filler = contract in originate b contract >>=? fun (b, tx_rollup) -> Context.get_constants (B b) >>=? fun {parametric = {tx_rollup_hard_size_limit_per_inbox; _}; _} -> @@ -2919,10 +2908,7 @@ module Rejection = struct (** Test that rejection successfully fails when there's no commitment to reject *) let test_no_commitment () = - context_init 1 >>=? fun (b, contracts) -> - let contract = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth contracts 0 - in + context_init1 () >>=? fun (b, contract) -> originate b contract >>=? fun (b, tx_rollup) -> let message = "bogus" in Op.tx_rollup_submit_batch (B b) contract tx_rollup message @@ -3787,15 +3773,13 @@ let test_state_message_storage_preallocation () = return_unit module Withdraw = struct - (** [context_init_withdraw n] initializes a context with [n + 1] accounts, one rollup and a + (** [context_init_withdraw tup] initializes a context with [tup] accounts, one rollup and a withdrawal recipient contract. *) let context_init_withdraw ?tx_rollup_origination_size - ?(amount = Z.of_int64 @@ Tx_rollup_l2_qty.to_int64 Nat_ticket.amount) n = - context_init ?tx_rollup_origination_size (n + 1) - >>=? fun (block, accounts) -> - let account1 = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth accounts 0 - in + ?(amount = Z.of_int64 @@ Tx_rollup_l2_qty.to_int64 Nat_ticket.amount) tup + = + context_init ?tx_rollup_origination_size tup >>=? fun (block, accounts) -> + let account1 = Context.tup_hd tup accounts in originate block account1 >>=? fun (block, tx_rollup) -> Nat_ticket.init_deposit amount block tx_rollup account1 >>=? fun (operation, block, deposit_contract) -> @@ -3819,7 +3803,7 @@ module Withdraw = struct (** [context_init1_withdraw] initializes a context with one account, one rollup and a withdrawal recipient contract. *) let context_init1_withdraw () = - context_init_withdraw 0 + context_init_withdraw Context.T1 >>=? fun ( account1, _accounts, tx_rollup, @@ -3831,16 +3815,13 @@ module Withdraw = struct (** [context_init2_withdraw] initializes a context with two accounts, one rollup and a withdrawal recipient contract. *) let context_init2_withdraw () = - context_init_withdraw 1 + context_init_withdraw Context.T2 >>=? fun ( account1, - accounts, + (_, account2), tx_rollup, deposit_contract, withdraw_contract, b ) -> - let account2 = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth accounts 1 - in return (account1, account2, tx_rollup, deposit_contract, withdraw_contract, b) @@ -4957,10 +4938,7 @@ module Withdraw = struct we overflow. *) let max = Int64.(sub max_int 1L) in let (_, _, pkh) = gen_l2_account () in - context_init 1 >>=? fun (b, accounts) -> - let account1 = - WithExceptions.Option.get ~loc:__LOC__ @@ List.nth accounts 0 - in + context_init1 () >>=? fun (b, account1) -> originate b account1 >>=? fun (b, tx_rollup) -> let pkh_str = Tx_rollup_l2_address.to_b58check pkh in Nat_ticket.init_deposit_contract (Z.of_int64 max) b account1 -- GitLab