From e84768a8db598897f67eec547b551e2334c02043 Mon Sep 17 00:00:00 2001 From: Sylvain Ribstein Date: Mon, 1 Aug 2022 14:51:41 +0200 Subject: [PATCH 1/5] test/helpers: remove duplicate check --- .../storage_benchmarks.ml | 3 +- .../lib_protocol/test/helpers/block.ml | 30 +++++-------------- .../lib_protocol/test/helpers/block.mli | 2 +- .../lib_protocol/test/helpers/context.ml | 3 +- 4 files changed, 11 insertions(+), 27 deletions(-) diff --git a/src/proto_alpha/lib_benchmarks_proto/storage_benchmarks.ml b/src/proto_alpha/lib_benchmarks_proto/storage_benchmarks.ml index 0a6478171a28..95790ffaa870 100644 --- a/src/proto_alpha/lib_benchmarks_proto/storage_benchmarks.ml +++ b/src/proto_alpha/lib_benchmarks_proto/storage_benchmarks.ml @@ -46,8 +46,7 @@ let default_raw_context () = (pkh, pk, amount, delegate_to, None)) initial_accounts in - Block.prepare_initial_context_params initial_accounts - >>=? fun (constants, _, _) -> + Block.prepare_initial_context_params () >>=? fun (constants, _, _) -> let parameters = Default_parameters.parameters_of_constants ~bootstrap_accounts diff --git a/src/proto_alpha/lib_protocol/test/helpers/block.ml b/src/proto_alpha/lib_protocol/test/helpers/block.ml index 3291ae729c4f..225edc0586c8 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/block.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/block.ml @@ -472,8 +472,7 @@ let prepare_initial_context_params ?consensus_threshold ?min_proposal_quorum ?blocks_per_cycle ?cycles_per_voting_period ?tx_rollup_enable ?tx_rollup_sunset_level ?tx_rollup_origination_size ?sc_rollup_enable ?sc_rollup_max_number_of_messages_per_commitment_period ?dal_enable - ?zk_rollup_enable ?hard_gas_limit_per_block ?nonce_revelation_threshold - initial_accounts = + ?zk_rollup_enable ?hard_gas_limit_per_block ?nonce_revelation_threshold () = let open Tezos_protocol_alpha_parameters in let constants = Default_parameters.constants_test in let min_proposal_quorum = @@ -586,21 +585,6 @@ let prepare_initial_context_params ?consensus_threshold ?min_proposal_quorum nonce_revelation_threshold; } in - (* Check there are at least minimal_stake tokens *) - Lwt.catch - (fun () -> - List.fold_left_es - (fun acc (_, amount, _) -> - Environment.wrap_tzresult @@ Tez.( +? ) acc amount >>?= fun acc -> - if acc >= constants.minimal_stake then raise Exit else return acc) - Tez.zero - initial_accounts - >>=? fun _ -> - failwith - "Insufficient tokens in initial accounts: the amount should be at \ - least minimal_stake") - (function Exit -> return_unit | exc -> raise exc) - >>=? fun () -> check_constants_consistency constants >>=? fun () -> let hash = Block_hash.of_b58check_exn @@ -622,9 +606,7 @@ let prepare_initial_context_params ?consensus_threshold ?min_proposal_quorum ~fitness ~operations_hash:Operation_list_list_hash.zero in - validate_initial_accounts initial_accounts constants.minimal_stake - (* Perhaps this could return a new type signifying its name *) - >|=? fun _initial_accounts -> (constants, shell, hash) + return (constants, shell, hash) (* if no parameter file is passed we check in the current directory where the test is run *) @@ -659,8 +641,10 @@ let genesis ?commitments ?consensus_threshold ?min_proposal_quorum ?zk_rollup_enable ?hard_gas_limit_per_block ?nonce_revelation_threshold - initial_accounts + () >>=? fun (constants, shell, hash) -> + validate_initial_accounts initial_accounts constants.minimal_stake + >>=? fun () -> initial_context ?commitments ?bootstrap_contracts @@ -686,8 +670,10 @@ let genesis ?commitments ?consensus_threshold ?min_proposal_quorum let alpha_context ?commitments ?min_proposal_quorum (initial_accounts : (Account.t * Tez.t * Signature.Public_key_hash.t option) list) = - prepare_initial_context_params ?min_proposal_quorum initial_accounts + prepare_initial_context_params ?min_proposal_quorum () >>=? fun (constants, shell, _hash) -> + validate_initial_accounts initial_accounts constants.minimal_stake + >>=? fun () -> initial_alpha_context ?commitments constants shell initial_accounts (********* Baking *************) diff --git a/src/proto_alpha/lib_protocol/test/helpers/block.mli b/src/proto_alpha/lib_protocol/test/helpers/block.mli index 768c698dc105..de0847d56f3f 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/block.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/block.mli @@ -289,7 +289,7 @@ val prepare_initial_context_params : ?zk_rollup_enable:bool -> ?hard_gas_limit_per_block:Gas.Arith.integral -> ?nonce_revelation_threshold:int32 -> - (Account.t * Tez.t * Signature.Public_key_hash.t option) list -> + unit -> ( Constants.Parametric.t * Block_header.shell_header * Block_hash.t, tztrace ) result diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.ml b/src/proto_alpha/lib_protocol/test/helpers/context.ml index 7f141b373138..6b37043bb631 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/context.ml @@ -552,8 +552,7 @@ let default_raw_context () = (pkh, pk, amount, delegate_to, None)) initial_accounts in - Block.prepare_initial_context_params initial_accounts - >>=? fun (constants, _, _) -> + Block.prepare_initial_context_params () >>=? fun (constants, _, _) -> let parameters = Default_parameters.parameters_of_constants ~bootstrap_accounts -- GitLab From cddc6a59eea7a0d5e3c6f71a42fbabdb8d7fb000 Mon Sep 17 00:00:00 2001 From: Sylvain Ribstein Date: Mon, 1 Aug 2022 15:19:55 +0200 Subject: [PATCH 2/5] test/sapling: use current alpha_ctxt instead of dummy one --- .../test/integration/michelson/test_sapling.ml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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 084f985f6a58..e338e9a186a5 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 @@ -648,12 +648,12 @@ module Interpreter_tests = struct list_addr in (let pkh = Context.Contract.pkh src1 in - (* dummy context used only for pack_data *) - Block.alpha_context - [(Account.activator_account, Tez.of_mutez_exn 100_000_000_000L, None)] - >>=? fun ctx -> - Script_ir_translator.pack_data ctx Script_typed_ir.key_hash_t pkh >>= wrap) - >>=? fun (bound_data, _) -> + Incremental.begin_construction b3 >>=? fun incr -> + let alpha_ctxt = Incremental.alpha_ctxt incr in + Script_ir_translator.pack_data alpha_ctxt Script_typed_ir.key_hash_t pkh + >>= wrap + >>=? fun (bound_data, _alpha_ctxt) -> return bound_data) + >>=? fun bound_data -> let hex_transac = to_hex (Tezos_sapling.Forge.forge_transaction -- GitLab From dcdef900e0104912672d6af4dbe98e54f025d6b9 Mon Sep 17 00:00:00 2001 From: Sylvain Ribstein Date: Mon, 1 Aug 2022 15:26:22 +0200 Subject: [PATCH 3/5] test: use bootstrap_accounts type when possible --- .../lib_protocol/test/helpers/block.ml | 43 +++++++------------ .../lib_protocol/test/helpers/block.mli | 4 +- .../lib_protocol/test/helpers/context.ml | 10 ++++- .../test/helpers/test_global_constants.ml | 10 ++++- .../test/integration/test_frozen_bonds.ml | 9 +++- .../test/integration/test_token.ml | 9 +++- .../test/unit/test_alpha_context.ml | 10 ++++- .../test/unit/test_consensus_key.ml | 10 ++++- 8 files changed, 70 insertions(+), 35 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/helpers/block.ml b/src/proto_alpha/lib_protocol/test/helpers/block.ml index 225edc0586c8..75e521f18d29 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/block.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/block.ml @@ -318,15 +318,8 @@ let check_constants_consistency constants = blocks_per_stake_snapshot") let prepare_main_init_params ?bootstrap_contracts commitments constants - initial_accounts = + bootstrap_accounts = let open Tezos_protocol_alpha_parameters in - let bootstrap_accounts = - List.map - (fun (Account.{pk; pkh; _}, amount, delegate_to) -> - Default_parameters.make_bootstrap_account - (pkh, pk, amount, delegate_to, None)) - initial_accounts - in let parameters = Default_parameters.parameters_of_constants ~bootstrap_accounts @@ -344,19 +337,19 @@ let prepare_main_init_params ?bootstrap_contracts commitments constants add ctxt protocol_param_key proto_params) let initial_context ?(commitments = []) ?bootstrap_contracts chain_id constants - header initial_accounts = + header bootstrap_accounts = prepare_main_init_params ?bootstrap_contracts commitments constants - initial_accounts + bootstrap_accounts >>= fun ctxt -> Main.init chain_id ctxt header >|= Environment.wrap_tzresult >|=? fun {context; _} -> context let initial_alpha_context ?(commitments = []) constants - (block_header : Block_header.shell_header) initial_accounts = - prepare_main_init_params commitments constants initial_accounts + (block_header : Block_header.shell_header) bootstrap_accounts = + prepare_main_init_params commitments constants bootstrap_accounts >>= fun ctxt -> let level = block_header.level in let timestamp = block_header.timestamp in @@ -445,21 +438,19 @@ let genesis_with_parameters parameters = context; } -let validate_initial_accounts - (initial_accounts : - (Account.t * Tez.t * Signature.Public_key_hash.t option) list) - minimal_stake = - if initial_accounts = [] then +let validate_bootstrap_accounts + (bootstrap_accounts : Parameters.bootstrap_account list) minimal_stake = + if bootstrap_accounts = [] then Stdlib.failwith "Must have one account with minimal_stake to bake" ; (* Check there are at least minimal_stake tokens *) Lwt.catch (fun () -> List.fold_left_es - (fun acc (_, amount, _) -> + (fun acc (Parameters.{amount; _} : Parameters.bootstrap_account) -> Environment.wrap_tzresult @@ Tez.( +? ) acc amount >>?= fun acc -> if acc >= minimal_stake then raise Exit else return acc) Tez.zero - initial_accounts + bootstrap_accounts >>=? fun _ -> failwith "Insufficient tokens in initial accounts: the amount should be at \ @@ -618,8 +609,7 @@ let genesis ?commitments ?consensus_threshold ?min_proposal_quorum ?tx_rollup_origination_size ?sc_rollup_enable ?sc_rollup_max_number_of_messages_per_commitment_period ?dal_enable ?zk_rollup_enable ?hard_gas_limit_per_block ?nonce_revelation_threshold - (initial_accounts : - (Account.t * Tez.t * Signature.Public_key_hash.t option) list) = + (bootstrap_accounts : Parameters.bootstrap_account list) = prepare_initial_context_params ?consensus_threshold ?min_proposal_quorum @@ -643,7 +633,7 @@ let genesis ?commitments ?consensus_threshold ?min_proposal_quorum ?nonce_revelation_threshold () >>=? fun (constants, shell, hash) -> - validate_initial_accounts initial_accounts constants.minimal_stake + validate_bootstrap_accounts bootstrap_accounts constants.minimal_stake >>=? fun () -> initial_context ?commitments @@ -651,7 +641,7 @@ let genesis ?commitments ?consensus_threshold ?min_proposal_quorum (Chain_id.of_block_hash hash) constants shell - initial_accounts + bootstrap_accounts >|=? fun context -> let contents = Forge.make_contents @@ -668,13 +658,12 @@ let genesis ?commitments ?consensus_threshold ?min_proposal_quorum } let alpha_context ?commitments ?min_proposal_quorum - (initial_accounts : - (Account.t * Tez.t * Signature.Public_key_hash.t option) list) = + (bootstrap_accounts : Parameters.bootstrap_account list) = prepare_initial_context_params ?min_proposal_quorum () >>=? fun (constants, shell, _hash) -> - validate_initial_accounts initial_accounts constants.minimal_stake + validate_bootstrap_accounts bootstrap_accounts constants.minimal_stake >>=? fun () -> - initial_alpha_context ?commitments constants shell initial_accounts + initial_alpha_context ?commitments constants shell bootstrap_accounts (********* Baking *************) diff --git a/src/proto_alpha/lib_protocol/test/helpers/block.mli b/src/proto_alpha/lib_protocol/test/helpers/block.mli index de0847d56f3f..18223a984120 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/block.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/block.mli @@ -141,7 +141,7 @@ val genesis : ?zk_rollup_enable:bool -> ?hard_gas_limit_per_block:Gas.Arith.integral -> ?nonce_revelation_threshold:int32 -> - (Account.t * Tez.tez * Signature.Public_key_hash.t option) list -> + Parameters.bootstrap_account list -> block tzresult Lwt.t val genesis_with_parameters : Parameters.t -> block tzresult Lwt.t @@ -153,7 +153,7 @@ val genesis_with_parameters : Parameters.t -> block tzresult Lwt.t val alpha_context : ?commitments:Commitment.t list -> ?min_proposal_quorum:int32 -> - (Account.t * Tez.tez * Signature.Public_key_hash.t option) list -> + Parameters.bootstrap_account list -> Alpha_context.t tzresult Lwt.t (** diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.ml b/src/proto_alpha/lib_protocol/test/helpers/context.ml index 6b37043bb631..186dd5475f4a 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/context.ml @@ -478,6 +478,14 @@ let init_gen tup ?rng_state ?commitments ?(initial_balances = []) (fun (a, _, _) -> Alpha_context.Contract.Implicit Account.(a.pkh)) accounts in + let bootstrap_accounts = + List.map + (fun (Account.{pkh; pk; _}, amount, delegate_to) -> + Tezos_protocol_alpha_parameters.Default_parameters + .make_bootstrap_account + (pkh, pk, amount, delegate_to, None)) + accounts + in Block.genesis ?commitments ?consensus_threshold @@ -501,7 +509,7 @@ let init_gen tup ?rng_state ?commitments ?(initial_balances = []) ?zk_rollup_enable ?hard_gas_limit_per_block ?nonce_revelation_threshold - accounts + bootstrap_accounts >|=? fun blk -> (blk, tup_get tup contracts) let init_n n = init_gen (TList n) diff --git a/src/proto_alpha/lib_protocol/test/helpers/test_global_constants.ml b/src/proto_alpha/lib_protocol/test/helpers/test_global_constants.ml index 915a57c0b537..165691248ddf 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/test_global_constants.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/test_global_constants.ml @@ -30,7 +30,15 @@ open Michelson_v1_primitives let create_context () = let accounts = Account.generate_accounts 2 in - Block.alpha_context accounts + let bootstrap_accounts = + List.map + (fun (Account.{pkh; pk; _}, amount, delegate_to) -> + Tezos_protocol_alpha_parameters.Default_parameters + .make_bootstrap_account + (pkh, pk, amount, delegate_to, None)) + accounts + in + Block.alpha_context bootstrap_accounts let expr_to_hash expr = let lexpr = Script_repr.lazy_expr expr 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 3704c98b187e..5f4d53116ea6 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 @@ -64,7 +64,14 @@ let mk_tx_rollup ?(nonce = nonce_zero) () = key hash of the account. *) let create_context () = let accounts = Account.generate_accounts 1 in - Block.alpha_context accounts >>=? fun ctxt -> + let bootstrap_accounts = + List.map + (fun (Account.{pk; pkh; _}, amount, delegate_to) -> + Default_parameters.make_bootstrap_account + (pkh, pk, amount, delegate_to, None)) + accounts + in + Block.alpha_context bootstrap_accounts >>=? fun ctxt -> match accounts with | [({pkh; _}, _, _)] -> return (ctxt, pkh) | _ -> (* Exactly one account has been generated. *) assert false diff --git a/src/proto_alpha/lib_protocol/test/integration/test_token.ml b/src/proto_alpha/lib_protocol/test/integration/test_token.ml index 1d3edd5f6c2f..bc8a80104956 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_token.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_token.ml @@ -42,7 +42,14 @@ let wrap e = e >|= Environment.wrap_tzresult key hash of the account. *) let create_context () = let accounts = Account.generate_accounts 1 in - Block.alpha_context accounts >>=? fun ctxt -> + let bootstrap_accounts = + List.map + (fun (Account.{pk; pkh; _}, amount, delegate_to) -> + Default_parameters.make_bootstrap_account + (pkh, pk, amount, delegate_to, None)) + accounts + in + Block.alpha_context bootstrap_accounts >>=? fun ctxt -> match accounts with | [({pkh; _}, _, _)] -> return (ctxt, pkh) | _ -> (* Exactly one account has been generated. *) assert false diff --git a/src/proto_alpha/lib_protocol/test/unit/test_alpha_context.ml b/src/proto_alpha/lib_protocol/test/unit/test_alpha_context.ml index 6a0b0a735d5e..954257696c7f 100644 --- a/src/proto_alpha/lib_protocol/test/unit/test_alpha_context.ml +++ b/src/proto_alpha/lib_protocol/test/unit/test_alpha_context.ml @@ -40,7 +40,15 @@ open Alpha_context (** Creates an Alpha_context without creating a full-fledged block *) let create () = let accounts = Account.generate_accounts 1 in - Block.alpha_context accounts + let bootstrap_accounts = + List.map + (fun (Account.{pk; pkh; _}, amount, delegate_to) -> + Tezos_protocol_alpha_parameters.Default_parameters + .make_bootstrap_account + (pkh, pk, amount, delegate_to, None)) + accounts + in + Block.alpha_context bootstrap_accounts let assert_equal_key_values ~loc kvs1 kvs2 = let sort_by_key_hash = diff --git a/src/proto_alpha/lib_protocol/test/unit/test_consensus_key.ml b/src/proto_alpha/lib_protocol/test/unit/test_consensus_key.ml index a6769ed0648b..c38a7e3831cb 100644 --- a/src/proto_alpha/lib_protocol/test/unit/test_consensus_key.ml +++ b/src/proto_alpha/lib_protocol/test/unit/test_consensus_key.ml @@ -41,7 +41,15 @@ let create () = | [(a1, _, _); (a2, _, _)] -> (a1, a2) | _ -> assert false in - let* ctxt = Block.alpha_context accounts in + let bootstrap_accounts = + List.map + (fun (Account.{pkh; pk; _}, balance, delegate_to) -> + Tezos_protocol_alpha_parameters.Default_parameters + .make_bootstrap_account + (pkh, pk, balance, delegate_to, None)) + accounts + in + let* ctxt = Block.alpha_context bootstrap_accounts in return (Alpha_context.Internal_for_tests.to_raw ctxt, a1, a2) module Consensus_key = struct -- GitLab From ab71cc42e169d1e8df6d08a611fb927568ef92d3 Mon Sep 17 00:00:00 2001 From: Sylvain Ribstein Date: Mon, 1 Aug 2022 15:40:02 +0200 Subject: [PATCH 4/5] test: improve Account.generate_accounts use `generate_accounts` were used to generate the list use for bootstrap account. I make that function only generating accounts and created a new function that make bootstrap account type from a list of account, with ?initial_balances and ?boostrap_delegations I also simplified the use of `boostrap_delegations` with the same logic os `initial_balances`. Which is the n account is delegated to the n element of the list `bootstrap_delegations`. --- .../lib_benchmark/execution_context.ml | 2 +- .../lib_benchmark/test/test_helpers.ml | 2 +- .../storage_benchmarks.ml | 38 +++++----- .../lib_protocol/test/helpers/account.ml | 73 +++++++++++-------- .../lib_protocol/test/helpers/account.mli | 47 ++++++++---- .../lib_protocol/test/helpers/context.ml | 63 ++++++---------- .../lib_protocol/test/helpers/context.mli | 6 +- .../test/helpers/liquidity_baking_machine.ml | 14 ++-- .../test/helpers/test_global_constants.ml | 13 +--- .../test/integration/consensus/test_baking.ml | 21 +++--- .../integration/consensus/test_delegation.ml | 36 ++++----- .../integration/operations/test_voting.ml | 6 +- .../test/integration/test_frozen_bonds.ml | 15 ++-- .../test/integration/test_sc_rollup_wasm.ml | 17 +++-- .../test/integration/test_token.ml | 15 ++-- .../test/pbt/test_refutation_game.ml | 2 +- .../test/unit/test_alpha_context.ml | 12 +-- .../test/unit/test_consensus_key.ml | 18 +---- .../test/unit/test_sc_rollup_storage.ml | 4 +- .../test/unit/test_sc_rollup_wasm.ml | 5 +- 20 files changed, 188 insertions(+), 221 deletions(-) diff --git a/src/proto_alpha/lib_benchmark/execution_context.ml b/src/proto_alpha/lib_benchmark/execution_context.ml index a44d25fc8721..472bf9fa8737 100644 --- a/src/proto_alpha/lib_benchmark/execution_context.ml +++ b/src/proto_alpha/lib_benchmark/execution_context.ml @@ -32,7 +32,7 @@ let initial_balance = 4_000_000_000_000L let context_init_memory ~rng_state = Context.init_n ~rng_state - ~initial_balances: + ~bootstrap_balances: [ initial_balance; initial_balance; diff --git a/src/proto_alpha/lib_benchmark/test/test_helpers.ml b/src/proto_alpha/lib_benchmark/test/test_helpers.ml index dfb174654cb1..5677355b0bb9 100644 --- a/src/proto_alpha/lib_benchmark/test/test_helpers.ml +++ b/src/proto_alpha/lib_benchmark/test/test_helpers.ml @@ -43,7 +43,7 @@ let typecheck_by_tezos = let context_init_memory ~rng_state = Context.init_n ~rng_state - ~initial_balances: + ~bootstrap_balances: [ 4_000_000_000_000L; 4_000_000_000_000L; diff --git a/src/proto_alpha/lib_benchmarks_proto/storage_benchmarks.ml b/src/proto_alpha/lib_benchmarks_proto/storage_benchmarks.ml index 95790ffaa870..bee763e6be4c 100644 --- a/src/proto_alpha/lib_benchmarks_proto/storage_benchmarks.ml +++ b/src/proto_alpha/lib_benchmarks_proto/storage_benchmarks.ml @@ -36,14 +36,11 @@ open Protocol (** Creates a dummy raw-context value. *) let default_raw_context () = - let initial_accounts = - Account.generate_accounts ~initial_balances:[100_000_000_000L] 1 - in + let open Lwt_result_syntax in + let*? initial_accounts = Account.generate_accounts 1 in let bootstrap_accounts = - List.map - (fun (Account.{pk; pkh; _}, amount, delegate_to) -> - Default_parameters.make_bootstrap_account - (pkh, pk, amount, delegate_to, None)) + Account.make_bootstrap_accounts + ~bootstrap_balances:[100_000_000_000L] initial_accounts in Block.prepare_initial_context_params () >>=? fun (constants, _, _) -> @@ -58,19 +55,22 @@ let default_raw_context () = Data_encoding.Binary.to_bytes_exn Data_encoding.json json in let protocol_param_key = ["protocol_parameters"] in - Tezos_protocol_environment.Context.( - let empty = Tezos_protocol_environment.Memory_context.empty in - add empty ["version"] (Bytes.of_string "genesis") >>= fun ctxt -> - add ctxt protocol_param_key proto_params) - >>= fun context -> + let*! context = + Tezos_protocol_environment.Context.( + let empty = Tezos_protocol_environment.Memory_context.empty in + let*! ctxt = add empty ["version"] (Bytes.of_string "genesis") in + add ctxt protocol_param_key proto_params) + in let typecheck ctxt script_repr = return ((script_repr, None), ctxt) in - Init_storage.prepare_first_block - Chain_id.zero - context - ~level:0l - ~timestamp:(Time.Protocol.of_seconds 1643125688L) - ~typecheck - >>= fun e -> Lwt.return @@ Environment.wrap_tzresult e + let*! e = + Init_storage.prepare_first_block + Chain_id.zero + context + ~level:0l + ~timestamp:(Time.Protocol.of_seconds 1643125688L) + ~typecheck + in + Lwt.return @@ Environment.wrap_tzresult e module String = struct type t = string diff --git a/src/proto_alpha/lib_protocol/test/helpers/account.ml b/src/proto_alpha/lib_protocol/test/helpers/account.ml index a157f16435bb..7706c0050b54 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/account.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/account.ml @@ -40,8 +40,9 @@ let random_seed ~rng_state = Bytes.init Hacl.Ed25519.sk_size (fun _i -> Char.chr (Random.State.int rng_state 256)) -let new_account ?seed () = - let pkh, pk, sk = Signature.generate_key ~algo:Ed25519 ?seed () in +let new_account ?(rng_state = Random.State.make_self_init ()) + ?(seed = random_seed ~rng_state) () = + let pkh, pk, sk = Signature.generate_key ~algo:Ed25519 ~seed () in let account = {pkh; pk; sk} in Signature.Public_key_hash.Table.add known_accounts pkh account ; account @@ -77,37 +78,9 @@ let dummy_account = let default_initial_balance = Tez.of_mutez_exn 4_000_000_000_000L -let generate_accounts ?rng_state ?(initial_balances = []) ?bootstrap_delegations - n : (t * Tez.t * Signature.Public_key_hash.t option) list = +let generate_accounts ?rng_state n : t list tzresult = Signature.Public_key_hash.Table.clear known_accounts ; - let amount i = - match List.nth_opt initial_balances i with - | None -> default_initial_balance - | Some a -> Tez.of_mutez_exn a - in - let rng_state = - match rng_state with - | None -> Random.State.make_self_init () - | Some state -> state - in - let delegate_to account = - Option.filter_map - (fun bootstrap_delegations -> - List.find_map - (fun (from_pkh, to_pkh) -> - if from_pkh = account then Some to_pkh else None) - bootstrap_delegations) - bootstrap_delegations - in - List.map - (fun i -> - let pkh, pk, sk = - Signature.generate_key ~algo:Ed25519 ~seed:(random_seed ~rng_state) () - in - let account = {pkh; pk; sk} in - Signature.Public_key_hash.Table.add known_accounts pkh account ; - (account, amount i, delegate_to pkh)) - (0 -- (n - 1)) + List.init ~when_negative_length:[] n (fun _i -> new_account ?rng_state ()) let commitment_secret = Blinded_public_key_hash.activation_code_of_hex @@ -127,3 +100,39 @@ let new_commitment ?seed () = let pkh_of_contract_exn = function | Contract.Implicit pkh -> pkh | Originated _ -> assert false + +let make_bootstrap_account ?(balance = default_initial_balance) + ?(delegate_to = None) ?(consensus_key = None) account = + Parameters. + { + public_key_hash = account.pkh; + public_key = Some account.pk; + amount = balance; + delegate_to; + consensus_key; + } + +let rec make_bootstrap_accounts ?(bootstrap_balances = []) + ?(bootstrap_delegations = []) ?(bootstrap_consensus_keys = []) accounts = + let decons_of_opt = function x :: xs -> (x, xs) | [] -> (None, []) in + let decons = function x :: xs -> (Some x, xs) | [] -> (None, []) in + match accounts with + | account :: accounts -> + let balance, bootstrap_balances = decons bootstrap_balances in + let delegate_to, bootstrap_delegations = + decons_of_opt bootstrap_delegations + in + let consensus_key, bootstrap_consensus_keys = + decons_of_opt bootstrap_consensus_keys + in + make_bootstrap_account + ?balance:(Option.map Tez.of_mutez_exn balance) + ~delegate_to + ~consensus_key + account + :: make_bootstrap_accounts + ~bootstrap_balances + ~bootstrap_delegations + ~bootstrap_consensus_keys + accounts + | [] -> [] diff --git a/src/proto_alpha/lib_protocol/test/helpers/account.mli b/src/proto_alpha/lib_protocol/test/helpers/account.mli index 6d3aeb79ca41..03f9c997c4b3 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/account.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/account.mli @@ -40,7 +40,10 @@ val activator_account : account val dummy_account : account -val new_account : ?seed:Bytes.t -> unit -> account +(** [new_account ?rng_state ?seed ()] creates a new account with the given [seed] (or + [rng_state] to generate the seed) and add it to the global account state. +*) +val new_account : ?rng_state:Random.State.t -> ?seed:Bytes.t -> unit -> account val add_account : t -> unit @@ -51,18 +54,11 @@ val find_alternate : Signature.Public_key_hash.t -> t (** 4.000.000.000 tez *) val default_initial_balance : Tez.t -(** [generate_accounts ?initial_balances n] : generates [n] random - accounts with the initial balance of the [i]th account given by the - [i]th value in the list [initial_balances] or otherwise - [default_initial_balance] tz (if the list is too short); and add them to the - global account state *) -val generate_accounts : - ?rng_state:Random.State.t -> - ?initial_balances:int64 list -> - ?bootstrap_delegations: - (Signature.Public_key_hash.t * Signature.Public_key_hash.t) list -> - int -> - (t * Tez.t * Signature.Public_key_hash.t option) list +(** [generate_accounts ?rng_state n] first frees the global account state then + generates [n] random accounts with [rng_state] to generate the seed and adds + them to the global account state. +*) +val generate_accounts : ?rng_state:Random.State.t -> int -> t list tzresult val commitment_secret : Blinded_public_key_hash.activation_code @@ -71,3 +67,28 @@ val new_commitment : (** Fails if the contract is not an implicit one *) val pkh_of_contract_exn : Contract.t -> Signature.Public_key_hash.t + +(** [make_bootstrap_account ~initial_balance ~delegate_to account] creates a + {!Parameters.bootstrap_account} from an account with the default or set + values. default [initial_balance] is [default_initial_balance], + [delegate_to] is [None] and [consensus_key] is [None]. +*) +val make_bootstrap_account : + ?balance:Tez.t -> + ?delegate_to:Signature.public_key_hash option -> + ?consensus_key:Signature.public_key option -> + t -> + Parameters.bootstrap_account + +(** [make_bootstrap_accounts ~bootstrap_balances ~bootstrap_delegations + ~bootstrap_consensus_keys accounts] combines the lists [accounts], + [bootstrap_balances], [bootstrap_delegations] and [bootstrap_consensus_keys] + to create a list of {!Parameters.bootstrap_account} using + [make_bootstrap_account]. +*) +val make_bootstrap_accounts : + ?bootstrap_balances:int64 list -> + ?bootstrap_delegations:Signature.public_key_hash option list -> + ?bootstrap_consensus_keys:Signature.public_key option list -> + t list -> + Parameters.bootstrap_account list diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.ml b/src/proto_alpha/lib_protocol/test/helpers/context.ml index 186dd5475f4a..de2e945b3e2f 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/context.ml @@ -456,34 +456,25 @@ let tup_get : type a r. (a, r) tup -> a list -> r = | TList _, l -> l | _ -> assert false -let init_gen tup ?rng_state ?commitments ?(initial_balances = []) - ?consensus_threshold ?min_proposal_quorum ?bootstrap_contracts - ?bootstrap_delegations ?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 init_gen tup ?rng_state ?commitments ?bootstrap_balances + ?bootstrap_delegations ?bootstrap_consensus_keys ?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 ?sc_rollup_max_number_of_messages_per_commitment_period ?dal_enable ?zk_rollup_enable ?hard_gas_limit_per_block ?nonce_revelation_threshold () = let n = tup_n tup in - let accounts = - Account.generate_accounts - ?rng_state - ~initial_balances - ?bootstrap_delegations - n - in + Account.generate_accounts ?rng_state n >>?= fun accounts -> let contracts = - List.map - (fun (a, _, _) -> Alpha_context.Contract.Implicit Account.(a.pkh)) - accounts + List.map (fun a -> Alpha_context.Contract.Implicit Account.(a.pkh)) accounts in let bootstrap_accounts = - List.map - (fun (Account.{pkh; pk; _}, amount, delegate_to) -> - Tezos_protocol_alpha_parameters.Default_parameters - .make_bootstrap_account - (pkh, pk, amount, delegate_to, None)) + Account.make_bootstrap_accounts + ?bootstrap_balances + ?bootstrap_delegations + ?bootstrap_consensus_keys accounts in Block.genesis @@ -522,20 +513,12 @@ let init3 = init_gen T3 let init_with_constants_gen tup constants = let n = tup_n tup in - let accounts = Account.generate_accounts n in + Account.generate_accounts n >>?= fun accounts -> let contracts = - List.map - (fun (a, _, _) -> Alpha_context.Contract.Implicit Account.(a.pkh)) - accounts + List.map (fun a -> Alpha_context.Contract.Implicit Account.(a.pkh)) accounts in + let bootstrap_accounts = Account.make_bootstrap_accounts accounts in let open Tezos_protocol_alpha_parameters in - let bootstrap_accounts = - List.map - (fun (acc, tez, delegate_to) -> - Default_parameters.make_bootstrap_account - (acc.Account.pkh, acc.Account.pk, tez, delegate_to, None)) - accounts - in let parameters = Default_parameters.parameters_of_constants ~bootstrap_accounts constants in @@ -549,21 +532,17 @@ 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 = - Account.generate_accounts ~initial_balances:[100_000_000_000L] 1 - in let open Tezos_protocol_alpha_parameters in + let initial_account = Account.new_account () in let bootstrap_accounts = - List.map - (fun (Account.{pk; pkh; _}, amount, delegate_to) -> - Default_parameters.make_bootstrap_account - (pkh, pk, amount, delegate_to, None)) - initial_accounts + Account.make_bootstrap_account + ~balance:(Tez.of_mutez_exn 100_000_000_000L) + initial_account in Block.prepare_initial_context_params () >>=? fun (constants, _, _) -> let parameters = Default_parameters.parameters_of_constants - ~bootstrap_accounts + ~bootstrap_accounts:[bootstrap_accounts] ~commitments:[] constants in diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.mli b/src/proto_alpha/lib_protocol/test/helpers/context.mli index 31b691bc5ff6..94e7042bf517 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/context.mli @@ -270,12 +270,12 @@ val tup_hd : ('a, 'elts) tup -> 'elts -> 'a type 'accounts init := ?rng_state:Random.State.t -> ?commitments:Commitment.t list -> - ?initial_balances:int64 list -> + ?bootstrap_balances:int64 list -> + ?bootstrap_delegations:Signature.Public_key_hash.t option list -> + ?bootstrap_consensus_keys:Signature.Public_key.t option list -> ?consensus_threshold:int -> ?min_proposal_quorum:int32 -> ?bootstrap_contracts:Parameters.bootstrap_contract list -> - ?bootstrap_delegations: - (Signature.Public_key_hash.t * Signature.Public_key_hash.t) list -> ?level:int32 -> ?cost_per_byte:Tez.t -> ?liquidity_baking_subsidy:Tez.t -> 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 72b5bdf89fb4..0c6963c60d5b 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 @@ -516,9 +516,9 @@ end let initial_xtz_repartition accounts_balances = let distributed_xtz = List.fold_left Int64.add 0L accounts_balances in let bootstrap1_xtz = Int64.sub total_xtz distributed_xtz in - let initial_balances = bootstrap1_xtz :: accounts_balances in - let n = List.length initial_balances in - (n, initial_balances) + let bootstrap_balances = bootstrap1_xtz :: accounts_balances in + let n = List.length bootstrap_balances in + (n, bootstrap_balances) (* --------------------------------------------------------------------------- *) @@ -854,11 +854,11 @@ 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 + let n, bootstrap_balances = initial_xtz_repartition accounts_balances in Context.init_n n ~consensus_threshold:0 - ~initial_balances + ~bootstrap_balances ~cost_per_byte:Tez.zero ~endorsing_reward_per_slot:Tez.zero ~baking_reward_bonus_per_slot:Tez.zero @@ -1181,9 +1181,9 @@ module SymbolicBaseMachine : end) let init ~invariant:_ ?(subsidy = default_subsidy) accounts_balances = - let _, initial_balances = initial_xtz_repartition accounts_balances in + let _, bootstrap_balances = initial_xtz_repartition accounts_balances in let len = Int64.of_int (List.length accounts_balances) in - match initial_balances with + match bootstrap_balances with | holder_xtz :: accounts -> let xtz_cpmm = Int64.( diff --git a/src/proto_alpha/lib_protocol/test/helpers/test_global_constants.ml b/src/proto_alpha/lib_protocol/test/helpers/test_global_constants.ml index 165691248ddf..341d5b63e5c5 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/test_global_constants.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/test_global_constants.ml @@ -29,16 +29,9 @@ open Micheline open Michelson_v1_primitives let create_context () = - let accounts = Account.generate_accounts 2 in - let bootstrap_accounts = - List.map - (fun (Account.{pkh; pk; _}, amount, delegate_to) -> - Tezos_protocol_alpha_parameters.Default_parameters - .make_bootstrap_account - (pkh, pk, amount, delegate_to, None)) - accounts - in - Block.alpha_context bootstrap_accounts + let open Lwt_result_syntax in + let*? accounts = Account.generate_accounts 2 in + Block.alpha_context (Account.make_bootstrap_accounts accounts) let expr_to_hash expr = let lexpr = Script_repr.lazy_expr expr 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 d029b34d9335..1ad7130585f7 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 @@ -311,7 +311,10 @@ let test_enough_active_stake_to_bake ~has_active_stake () = active balance is less or equal the staking balance (see [Delegate_sampler.select_distribution_for_cycle]). *) 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 () + Context.init2 + ~bootstrap_balances:[initial_bal1; tpr] + ~consensus_threshold:0 + () >>=? fun (b0, (account1, _account2)) -> let pkh1 = Context.Contract.pkh account1 in Context.get_constants (B b0) @@ -336,16 +339,11 @@ let test_enough_active_stake_to_bake ~has_active_stake () = let test_committee_sampling () = let test_distribution max_round distribution = - let initial_balances, bounds = List.split distribution in - let accounts = - Account.generate_accounts ~initial_balances (List.length initial_balances) - in + let bootstrap_balances, bounds = List.split distribution in + Account.generate_accounts (List.length bootstrap_balances) + >>?= fun accounts -> let bootstrap_accounts = - List.map - (fun (acc, tez, delegate_to) -> - Default_parameters.make_bootstrap_account - (acc.Account.pkh, acc.Account.pk, tez, delegate_to, None)) - accounts + Account.make_bootstrap_accounts ~bootstrap_balances accounts in let consensus_committee_size = max_round in assert ( @@ -368,8 +366,7 @@ let test_committee_sampling () = >|=? fun bakers -> let stats = Stdlib.Hashtbl.create 10 in Stdlib.List.iter2 - (fun (acc, _, _) bounds -> - Stdlib.Hashtbl.add stats acc.Account.pkh (bounds, 0)) + (fun acc bounds -> Stdlib.Hashtbl.add stats acc.Account.pkh (bounds, 0)) accounts bounds ; List.iter 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 101f50fb89c2..15fb13a3382c 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 @@ -304,32 +304,28 @@ let undelegated_originated_bootstrap_contract () = | Some _ -> failwith "Bootstrap contract should be undelegated (%s)" __LOC__ let delegated_implicit_bootstrap_contract () = - (* These values are fixed because we use a fixed RNG seed. *) - let from_pkh = - Signature.Public_key_hash.of_b58check_exn - "tz1TDZG4vFoA2xutZMYauUnS4HVucnAGQSpZ" + Account.generate_accounts 2 >>?= fun accounts -> + let to_pkh, from_pkh = + match accounts with + | [account1; account2] -> (account1.pkh, account2.pkh) + | _ -> assert false in - let to_pkh = - Signature.Public_key_hash.of_b58check_exn - "tz1MBWU1WkszFfkEER2pgn4ATKXE9ng7x1sR" + let bootstrap_delegations = [None; Some to_pkh] in + let bootstrap_accounts = + Account.make_bootstrap_accounts ~bootstrap_delegations accounts in - let bootstrap_delegations = [(from_pkh, to_pkh)] in - let rng_state = Random.State.make [|0|] in - Context.init2 ~rng_state ~bootstrap_delegations () - >>=? fun (b, (contract1, _contract2)) -> - Block.bake b >>=? fun b -> - Context.Contract.delegate_opt (B b) contract1 >>=? fun delegate0 -> - (match delegate0 with - | Some contract when contract = to_pkh -> return_unit - | Some _ | None -> - failwith "Bootstrap contract should be delegated (%s)" __LOC__) + Block.genesis bootstrap_accounts >>=? fun b -> + (Context.Contract.delegate_opt (B b) (Implicit from_pkh) >>=? function + | Some pkh when pkh = to_pkh -> return_unit + | Some _ | None -> + failwith "Bootstrap contract should be delegated (%s)." __LOC__) >>=? fun () -> (* Test delegation amount *) Incremental.begin_construction b >>=? fun i -> let ctxt = Incremental.alpha_ctxt i in - Delegate.delegated_balance ctxt to_pkh >>= fun result -> - Lwt.return @@ Environment.wrap_tzresult result >>=? fun amount -> - Assert.equal_tez ~loc:__LOC__ amount (Tez.of_mutez_exn 4000000000000L) + Delegate.delegated_balance ctxt to_pkh >|= Environment.wrap_tzresult + >>=? fun amount -> + Assert.equal_tez ~loc:__LOC__ amount (Tez.of_mutez_exn 4_000_000_000_000L) let tests_bootstrap_contracts = [ 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 6d1f908f3d35..8a2c400c9aba 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 @@ -846,7 +846,7 @@ let test_supermajority_in_proposal there_is_a_winner () = let initial_balance = 1L in context_init ~min_proposal_quorum - ~initial_balances:[initial_balance; initial_balance; initial_balance] + ~bootstrap_balances:[initial_balance; initial_balance; initial_balance] 10 () >>=? fun (b, delegates) -> @@ -901,7 +901,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 ~bootstrap_balances:[1L; half_tokens; half_tokens] 3 () >>=? fun (b, delegates) -> Context.get_constants (B b) >>=? fun {parametric = {min_proposal_quorum; _}; _} -> @@ -1088,7 +1088,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 ~bootstrap_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 5f4d53116ea6..86e261b18fe1 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 @@ -63,17 +63,12 @@ let mk_tx_rollup ?(nonce = nonce_zero) () = (** Creates a context with a single account. Returns the context and the public key hash of the account. *) let create_context () = - let accounts = Account.generate_accounts 1 in - let bootstrap_accounts = - List.map - (fun (Account.{pk; pkh; _}, amount, delegate_to) -> - Default_parameters.make_bootstrap_account - (pkh, pk, amount, delegate_to, None)) - accounts - in - Block.alpha_context bootstrap_accounts >>=? fun ctxt -> + let open Lwt_result_syntax in + let*? accounts = Account.generate_accounts 1 in + let bootstrap_accounts = Account.make_bootstrap_accounts accounts in + let* ctxt = Block.alpha_context bootstrap_accounts in match accounts with - | [({pkh; _}, _, _)] -> return (ctxt, pkh) + | [{pkh; _}] -> return (ctxt, pkh) | _ -> (* Exactly one account has been generated. *) assert false (** Creates a context, a user contract, and a delegate. diff --git a/src/proto_alpha/lib_protocol/test/integration/test_sc_rollup_wasm.ml b/src/proto_alpha/lib_protocol/test/integration/test_sc_rollup_wasm.ml index 22fd3c35d6be..f390ddc31b91 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_sc_rollup_wasm.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_sc_rollup_wasm.ml @@ -200,9 +200,9 @@ let check_chunks_count tree expected = else failwith "wrong chunks counter, expected %d, got %d" expected count let operator () = - match Account.generate_accounts 1 with - | [(account, _, _)] -> account - | _ -> assert false + let open Result_syntax in + let* accounts = Account.generate_accounts 1 in + match accounts with [operator] -> return operator | _ -> assert false let should_boot_complete_boot_sector boot_sector () = let open Tezos_scoru_wasm.Gather_floppies in @@ -258,7 +258,7 @@ let floppy_input i operator chunk = let should_interpret_empty_chunk () = let open Lwt_result_syntax in - let op = operator () in + let*? op = operator () in let chunk_size = Tezos_scoru_wasm.Gather_floppies.chunk_size in let origination_message = Data_encoding.Binary.to_string_exn @@ -286,8 +286,8 @@ let should_interpret_empty_chunk () = let should_refuse_chunks_with_incorrect_signature () = let open Lwt_result_syntax in - let good_op = operator () in - let bad_op = operator () in + let*? good_op = operator () in + let*? bad_op = operator () in let chunk_size = Tezos_scoru_wasm.Gather_floppies.chunk_size in let origination_message = Data_encoding.Binary.to_string_exn @@ -321,7 +321,7 @@ let should_refuse_chunks_with_incorrect_signature () = let should_boot_incomplete_boot_sector kernel () = let open Lwt_result_syntax in - let operator = operator () in + let*? operator = operator () in let chunk_size = Tezos_scoru_wasm.Gather_floppies.chunk_size in let initial_chunk, rem_chunks = let split_chunk s = @@ -448,13 +448,14 @@ let should_boot_computation_kernel () = return_unit let tests = + let open Lwt_result_syntax in [ Tztest.tztest "should boot a complete boot sector" `Quick @@ should_boot_complete_boot_sector (complete_boot_sector (Bytes.of_string @@ computation_kernel ())); ( Tztest.tztest "should boot an incomplete but too small boot sector" `Quick @@ fun () -> - let operator = operator () in + let*? operator = operator () in should_boot_complete_boot_sector (incomplete_boot_sector "\x00asm\x01\x00\x00\x00" operator) () ); diff --git a/src/proto_alpha/lib_protocol/test/integration/test_token.ml b/src/proto_alpha/lib_protocol/test/integration/test_token.ml index bc8a80104956..eac679e9495e 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_token.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_token.ml @@ -41,17 +41,12 @@ let wrap e = e >|= Environment.wrap_tzresult (** Creates a context with a single account. Returns the context and the public key hash of the account. *) let create_context () = - let accounts = Account.generate_accounts 1 in - let bootstrap_accounts = - List.map - (fun (Account.{pk; pkh; _}, amount, delegate_to) -> - Default_parameters.make_bootstrap_account - (pkh, pk, amount, delegate_to, None)) - accounts - in - Block.alpha_context bootstrap_accounts >>=? fun ctxt -> + let open Lwt_result_syntax in + let*? accounts = Account.generate_accounts 1 in + let bootstrap_accounts = Account.make_bootstrap_accounts accounts in + let* ctxt = Block.alpha_context bootstrap_accounts in match accounts with - | [({pkh; _}, _, _)] -> return (ctxt, pkh) + | [{pkh; _}] -> return (ctxt, pkh) | _ -> (* Exactly one account has been generated. *) assert false let random_amount () = diff --git a/src/proto_alpha/lib_protocol/test/pbt/test_refutation_game.ml b/src/proto_alpha/lib_protocol/test/pbt/test_refutation_game.ml index d25d1fde5c4e..84296607be17 100644 --- a/src/proto_alpha/lib_protocol/test/pbt/test_refutation_game.ml +++ b/src/proto_alpha/lib_protocol/test/pbt/test_refutation_game.ml @@ -239,7 +239,7 @@ let create_ctxt ~first_inputs = Context.init3 ~sc_rollup_enable:true ~consensus_threshold:0 - ~initial_balances:[100_000_000_000L; 100_000_000_000L; 100_000_000_000L] + ~bootstrap_balances:[100_000_000_000L; 100_000_000_000L; 100_000_000_000L] () in let* block, sc_rollup, genesis_info = diff --git a/src/proto_alpha/lib_protocol/test/unit/test_alpha_context.ml b/src/proto_alpha/lib_protocol/test/unit/test_alpha_context.ml index 954257696c7f..8e46477a80cc 100644 --- a/src/proto_alpha/lib_protocol/test/unit/test_alpha_context.ml +++ b/src/proto_alpha/lib_protocol/test/unit/test_alpha_context.ml @@ -39,15 +39,9 @@ open Alpha_context (** Creates an Alpha_context without creating a full-fledged block *) let create () = - let accounts = Account.generate_accounts 1 in - let bootstrap_accounts = - List.map - (fun (Account.{pk; pkh; _}, amount, delegate_to) -> - Tezos_protocol_alpha_parameters.Default_parameters - .make_bootstrap_account - (pkh, pk, amount, delegate_to, None)) - accounts - in + let open Lwt_result_syntax in + let*? accounts = Account.generate_accounts 1 in + let bootstrap_accounts = Account.make_bootstrap_accounts accounts in Block.alpha_context bootstrap_accounts let assert_equal_key_values ~loc kvs1 kvs2 = diff --git a/src/proto_alpha/lib_protocol/test/unit/test_consensus_key.ml b/src/proto_alpha/lib_protocol/test/unit/test_consensus_key.ml index c38a7e3831cb..1b3c2b7cdbbc 100644 --- a/src/proto_alpha/lib_protocol/test/unit/test_consensus_key.ml +++ b/src/proto_alpha/lib_protocol/test/unit/test_consensus_key.ml @@ -35,21 +35,9 @@ open Protocol let create () = let open Lwt_result_syntax in - let accounts = Account.generate_accounts 2 in - let a1, a2 = - match accounts with - | [(a1, _, _); (a2, _, _)] -> (a1, a2) - | _ -> assert false - in - let bootstrap_accounts = - List.map - (fun (Account.{pkh; pk; _}, balance, delegate_to) -> - Tezos_protocol_alpha_parameters.Default_parameters - .make_bootstrap_account - (pkh, pk, balance, delegate_to, None)) - accounts - in - let* ctxt = Block.alpha_context bootstrap_accounts in + let*? accounts = Account.generate_accounts 2 in + let a1, a2 = match accounts with [a1; a2] -> (a1, a2) | _ -> assert false in + let* ctxt = Block.alpha_context (Account.make_bootstrap_accounts accounts) in return (Alpha_context.Internal_for_tests.to_raw ctxt, a1, a2) module Consensus_key = struct 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 a9524f05ae08..4187fc86380d 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 @@ -43,7 +43,7 @@ let lift k = Lwt.map Environment.wrap_tzresult k credited with 100 000 tez. *) let new_context_with_stakers nb_stakers = let initial_balance = Int64.of_string "100_000_000_000" in - let*? initial_balances = + let*? bootstrap_balances = List.init ~when_negative_length:[] nb_stakers (fun _ -> initial_balance) in let sc_rollup_max_number_of_messages_per_commitment_period = @@ -52,7 +52,7 @@ let new_context_with_stakers nb_stakers = in let* b, contracts = Context.init_n - ~initial_balances + ~bootstrap_balances nb_stakers ~sc_rollup_max_number_of_messages_per_commitment_period () diff --git a/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_wasm.ml b/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_wasm.ml index b47364aa4e9d..2958d172ce9e 100644 --- a/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_wasm.ml +++ b/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_wasm.ml @@ -107,10 +107,9 @@ let test_initial_state_hash_wasm_pvm () = let test_incomplete_kernel_chunk_limit () = let open Lwt_result_syntax in + let*? accounts = Account.generate_accounts 1 in let operator = - match Account.generate_accounts 1 with - | [(account, _, _)] -> account - | _ -> assert false + match accounts with [account] -> account | _ -> assert false in let chunk_size = Tezos_scoru_wasm.Gather_floppies.chunk_size in let chunk_too_big = Bytes.make (chunk_size + 10) 'a' in -- GitLab From 4fee69e9e385066b33f6d446e670fdbfea65c742 Mon Sep 17 00:00:00 2001 From: Sylvain Ribstein Date: Mon, 1 Aug 2022 16:19:41 +0200 Subject: [PATCH 5/5] test: use Account.new_account when appropriate --- .../lib_benchmarks_proto/storage_benchmarks.ml | 12 ++++++------ .../test/integration/test_frozen_bonds.ml | 12 +++++------- .../test/integration/test_sc_rollup_wasm.ml | 16 ++++++---------- .../lib_protocol/test/integration/test_token.ml | 10 +++------- .../lib_protocol/test/unit/test_alpha_context.ml | 7 +++---- .../test/unit/test_local_contexts.ml | 7 ++++--- .../test/unit/test_sc_rollup_wasm.ml | 5 +---- 7 files changed, 28 insertions(+), 41 deletions(-) diff --git a/src/proto_alpha/lib_benchmarks_proto/storage_benchmarks.ml b/src/proto_alpha/lib_benchmarks_proto/storage_benchmarks.ml index bee763e6be4c..0b4b420c65a6 100644 --- a/src/proto_alpha/lib_benchmarks_proto/storage_benchmarks.ml +++ b/src/proto_alpha/lib_benchmarks_proto/storage_benchmarks.ml @@ -37,16 +37,16 @@ open Protocol (** Creates a dummy raw-context value. *) let default_raw_context () = let open Lwt_result_syntax in - let*? initial_accounts = Account.generate_accounts 1 in - let bootstrap_accounts = - Account.make_bootstrap_accounts - ~bootstrap_balances:[100_000_000_000L] - initial_accounts + let initial_account = Account.new_account () in + let bootstrap_account = + Account.make_bootstrap_account + ~balance:(Alpha_context.Tez.of_mutez_exn 100_000_000_000L) + initial_account in Block.prepare_initial_context_params () >>=? fun (constants, _, _) -> let parameters = Default_parameters.parameters_of_constants - ~bootstrap_accounts + ~bootstrap_accounts:[bootstrap_account] ~commitments:[] constants 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 86e261b18fe1..dc561b4beb3b 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 @@ -63,13 +63,11 @@ let mk_tx_rollup ?(nonce = nonce_zero) () = (** Creates a context with a single account. Returns the context and the public key hash of the account. *) let create_context () = - let open Lwt_result_syntax in - let*? accounts = Account.generate_accounts 1 in - let bootstrap_accounts = Account.make_bootstrap_accounts accounts in - let* ctxt = Block.alpha_context bootstrap_accounts in - match accounts with - | [{pkh; _}] -> return (ctxt, pkh) - | _ -> (* Exactly one account has been generated. *) assert false + let (Parameters.{public_key_hash; _} as bootstrap_account) = + Account.(new_account () |> make_bootstrap_account) + in + Block.alpha_context [bootstrap_account] >|=? fun ctxt -> + (ctxt, public_key_hash) (** Creates a context, a user contract, and a delegate. Returns the context, the user contract, the user account, and the diff --git a/src/proto_alpha/lib_protocol/test/integration/test_sc_rollup_wasm.ml b/src/proto_alpha/lib_protocol/test/integration/test_sc_rollup_wasm.ml index f390ddc31b91..44a75733d174 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_sc_rollup_wasm.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_sc_rollup_wasm.ml @@ -199,10 +199,7 @@ let check_chunks_count tree expected = if count = expected then Lwt_result.return () else failwith "wrong chunks counter, expected %d, got %d" expected count -let operator () = - let open Result_syntax in - let* accounts = Account.generate_accounts 1 in - match accounts with [operator] -> return operator | _ -> assert false +let operator = Account.new_account let should_boot_complete_boot_sector boot_sector () = let open Tezos_scoru_wasm.Gather_floppies in @@ -258,7 +255,7 @@ let floppy_input i operator chunk = let should_interpret_empty_chunk () = let open Lwt_result_syntax in - let*? op = operator () in + let op = operator () in let chunk_size = Tezos_scoru_wasm.Gather_floppies.chunk_size in let origination_message = Data_encoding.Binary.to_string_exn @@ -286,8 +283,8 @@ let should_interpret_empty_chunk () = let should_refuse_chunks_with_incorrect_signature () = let open Lwt_result_syntax in - let*? good_op = operator () in - let*? bad_op = operator () in + let good_op = operator () in + let bad_op = operator () in let chunk_size = Tezos_scoru_wasm.Gather_floppies.chunk_size in let origination_message = Data_encoding.Binary.to_string_exn @@ -321,7 +318,7 @@ let should_refuse_chunks_with_incorrect_signature () = let should_boot_incomplete_boot_sector kernel () = let open Lwt_result_syntax in - let*? operator = operator () in + let operator = operator () in let chunk_size = Tezos_scoru_wasm.Gather_floppies.chunk_size in let initial_chunk, rem_chunks = let split_chunk s = @@ -448,14 +445,13 @@ let should_boot_computation_kernel () = return_unit let tests = - let open Lwt_result_syntax in [ Tztest.tztest "should boot a complete boot sector" `Quick @@ should_boot_complete_boot_sector (complete_boot_sector (Bytes.of_string @@ computation_kernel ())); ( Tztest.tztest "should boot an incomplete but too small boot sector" `Quick @@ fun () -> - let*? operator = operator () in + let operator = operator () in should_boot_complete_boot_sector (incomplete_boot_sector "\x00asm\x01\x00\x00\x00" operator) () ); diff --git a/src/proto_alpha/lib_protocol/test/integration/test_token.ml b/src/proto_alpha/lib_protocol/test/integration/test_token.ml index eac679e9495e..8a2c7194de36 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_token.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_token.ml @@ -41,13 +41,9 @@ let wrap e = e >|= Environment.wrap_tzresult (** Creates a context with a single account. Returns the context and the public key hash of the account. *) let create_context () = - let open Lwt_result_syntax in - let*? accounts = Account.generate_accounts 1 in - let bootstrap_accounts = Account.make_bootstrap_accounts accounts in - let* ctxt = Block.alpha_context bootstrap_accounts in - match accounts with - | [{pkh; _}] -> return (ctxt, pkh) - | _ -> (* Exactly one account has been generated. *) assert false + let (Account.{pkh; _} as account) = Account.new_account () in + let bootstrap_account = Account.make_bootstrap_account account in + Block.alpha_context [bootstrap_account] >>=? fun ctxt -> return (ctxt, pkh) let random_amount () = match Tez.of_mutez (Int64.add 1L (Random.int64 100L)) with diff --git a/src/proto_alpha/lib_protocol/test/unit/test_alpha_context.ml b/src/proto_alpha/lib_protocol/test/unit/test_alpha_context.ml index 8e46477a80cc..fd5af2672971 100644 --- a/src/proto_alpha/lib_protocol/test/unit/test_alpha_context.ml +++ b/src/proto_alpha/lib_protocol/test/unit/test_alpha_context.ml @@ -39,10 +39,9 @@ open Alpha_context (** Creates an Alpha_context without creating a full-fledged block *) let create () = - let open Lwt_result_syntax in - let*? accounts = Account.generate_accounts 1 in - let bootstrap_accounts = Account.make_bootstrap_accounts accounts in - Block.alpha_context bootstrap_accounts + let account = Account.new_account () in + let bootstrap_account = Account.make_bootstrap_account account in + Block.alpha_context [bootstrap_account] let assert_equal_key_values ~loc kvs1 kvs2 = let sort_by_key_hash = diff --git a/src/proto_alpha/lib_protocol/test/unit/test_local_contexts.ml b/src/proto_alpha/lib_protocol/test/unit/test_local_contexts.ml index a78b30f96eee..d620a1618398 100644 --- a/src/proto_alpha/lib_protocol/test/unit/test_local_contexts.ml +++ b/src/proto_alpha/lib_protocol/test/unit/test_local_contexts.ml @@ -37,9 +37,10 @@ open Storage_functors module A = Alpha_context let create () = - let accounts = Account.generate_accounts 1 in - Block.alpha_context accounts >|=? fun alpha_ctxt -> - A.Internal_for_tests.to_raw alpha_ctxt + let account = Account.new_account () in + let bootstrap_account = Account.make_bootstrap_account account in + Block.alpha_context [bootstrap_account] >>=? fun alpha_ctxt -> + return @@ A.Internal_for_tests.to_raw alpha_ctxt (* /a/b/c *) let dir1 = ["a"; "b"; "c"] diff --git a/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_wasm.ml b/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_wasm.ml index 2958d172ce9e..a805ac17e900 100644 --- a/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_wasm.ml +++ b/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_wasm.ml @@ -107,10 +107,7 @@ let test_initial_state_hash_wasm_pvm () = let test_incomplete_kernel_chunk_limit () = let open Lwt_result_syntax in - let*? accounts = Account.generate_accounts 1 in - let operator = - match accounts with [account] -> account | _ -> assert false - in + let operator = Account.new_account () in let chunk_size = Tezos_scoru_wasm.Gather_floppies.chunk_size in let chunk_too_big = Bytes.make (chunk_size + 10) 'a' in let signature = Signature.sign operator.Account.sk chunk_too_big in -- GitLab