diff --git a/src/proto_alpha/lib_benchmark/execution_context.ml b/src/proto_alpha/lib_benchmark/execution_context.ml index a44d25fc872136505947f21c3d718837b35a21e4..472bf9fa8737c0ddbd0ec9b3cc5357a0b1ceb2d2 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 dfb174654cb1078763e12e23f04305b91c9a703f..5677355b0bb90c5a4a4b4f3222ddcf73e66bc5c0 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 0a6478171a287c4481afd01fb1a4f72004b50892..0b4b420c65a668141a97059fdd104a3b61f4ac66 100644 --- a/src/proto_alpha/lib_benchmarks_proto/storage_benchmarks.ml +++ b/src/proto_alpha/lib_benchmarks_proto/storage_benchmarks.ml @@ -36,21 +36,17 @@ 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 + let open Lwt_result_syntax in + 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 - 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 - 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 + ~bootstrap_accounts:[bootstrap_account] ~commitments:[] constants in @@ -59,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 a157f16435bb62f4af2ed1b14ac453415ee34c29..7706c0050b54ea1f1edaa05fd12535321c7adae7 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 6d3aeb79ca41723ad84705ceffd2da9c6c231b8f..03f9c997c4b3b6018b4fb069f341fc80bcbe0eb4 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/block.ml b/src/proto_alpha/lib_protocol/test/helpers/block.ml index 3291ae729c4fafaa9b3e1ac0f82998c00a1d3a2e..75e521f18d298146de7a296059031fe57774a497 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 \ @@ -472,8 +463,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 +576,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 +597,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 *) @@ -636,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 @@ -659,15 +631,17 @@ 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_bootstrap_accounts bootstrap_accounts constants.minimal_stake + >>=? fun () -> initial_context ?commitments ?bootstrap_contracts (Chain_id.of_block_hash hash) constants shell - initial_accounts + bootstrap_accounts >|=? fun context -> let contents = Forge.make_contents @@ -684,11 +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) = - prepare_initial_context_params ?min_proposal_quorum initial_accounts + (bootstrap_accounts : Parameters.bootstrap_account list) = + prepare_initial_context_params ?min_proposal_quorum () >>=? fun (constants, shell, _hash) -> - initial_alpha_context ?commitments constants shell initial_accounts + validate_bootstrap_accounts bootstrap_accounts constants.minimal_stake + >>=? fun () -> + 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 768c698dc105717506118670e6e9350ed6e2232f..18223a9841209bb7db8c75b6dc827ff59d50ca46 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 (** @@ -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 7f141b3731387bb6be80d9fe745f78bf8d10ae04..de2e945b3e2fe09c1126b42a0b9e85bcd04f59a6 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/context.ml @@ -456,26 +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)) + List.map (fun a -> Alpha_context.Contract.Implicit Account.(a.pkh)) accounts + in + let bootstrap_accounts = + Account.make_bootstrap_accounts + ?bootstrap_balances + ?bootstrap_delegations + ?bootstrap_consensus_keys accounts in Block.genesis @@ -501,7 +500,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) @@ -514,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 @@ -541,22 +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 initial_accounts - >>=? fun (constants, _, _) -> + 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 31b691bc5ff6417ca4b68e33fdb027edf7dea414..94e7042bf51777e51778f2b13ad6e549d9084fe1 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 72b5bdf89fb4e067f0a48e24159bffa247dc6583..0c6963c60d5b9ec8fa5f004c5777bcb76744874f 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 915a57c0b537413a7a16aa2d604a1f6f2a381ed2..341d5b63e5c51600be6fa6c9ac18632a42be50cd 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,8 +29,9 @@ open Micheline open Michelson_v1_primitives let create_context () = - let accounts = Account.generate_accounts 2 in - Block.alpha_context 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 d029b34d9335f94c22330270cf2ef37dda642b9f..1ad7130585f749ae064da317496ffe7142476a9d 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 101f50fb89c2713ceb1f8e25c640c9aa53a52c4e..15fb13a3382c5810497d0cc1657afcb2116936b1 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/michelson/test_sapling.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_sapling.ml index 084f985f6a589497eb694063b7ae268a33488a1b..e338e9a186a5a5d618f24ee116976f1c194a7c66 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 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 6d1f908f3d357f50b302021dd51e218a3b050075..8a2c400c9abab72b231b7f4f83eb82f0fa43819d 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 3704c98b187e5275181a71d62be0eb2f89fb3b03..dc561b4beb3b31df34e4b88e04aa27b83a1d8f9d 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,11 +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 accounts = Account.generate_accounts 1 in - Block.alpha_context accounts >>=? fun ctxt -> - 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 22fd3c35d6be5787aa4db069dd8e4b4ffce02bf4..44a75733d174eb4005f0dc1ff98bd35fd8b4c032 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 () = - match Account.generate_accounts 1 with - | [(account, _, _)] -> account - | _ -> assert false +let operator = Account.new_account let should_boot_complete_boot_sector boot_sector () = let open Tezos_scoru_wasm.Gather_floppies in 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 1d3edd5f6c2f7db8e5f0bdc89c0ed23e73c6913a..8a2c7194de36239a08c6b1fe9c4244fcc0af105e 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_token.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_token.ml @@ -41,11 +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 accounts = Account.generate_accounts 1 in - Block.alpha_context accounts >>=? fun ctxt -> - 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/pbt/test_refutation_game.ml b/src/proto_alpha/lib_protocol/test/pbt/test_refutation_game.ml index d25d1fde5c4e876cd221be01eeca68e2908fe54d..84296607be1742492a63f05d36b98a2e3905723e 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 6a0b0a735d5e0746b528c189e76e30c96bb7affe..fd5af2672971057e40b25079e9aedd79fe4f7df4 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,8 +39,9 @@ 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 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_consensus_key.ml b/src/proto_alpha/lib_protocol/test/unit/test_consensus_key.ml index a6769ed0648b555e615c23e54ccd03c99e51316f..1b3c2b7cdbbcb5a036150b2dc5292fe9d3e6b5fd 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,13 +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* ctxt = Block.alpha_context 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_local_contexts.ml b/src/proto_alpha/lib_protocol/test/unit/test_local_contexts.ml index a78b30f96eeea8a5ba6d34bb6e0da881771200c1..d620a1618398ba7ddc1d550843327e01632bb25d 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_storage.ml b/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_storage.ml index a9524f05ae08a9182cf2b8d48b39f52c52006ee7..4187fc86380d765a805c8aade5d00cc1ed411a31 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 b47364aa4e9d3534dfec49466c8064fe02a39eb2..a805ac17e900eeb2d5a1d23c32b2484f54cf0233 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,11 +107,7 @@ let test_initial_state_hash_wasm_pvm () = let test_incomplete_kernel_chunk_limit () = let open Lwt_result_syntax in - let operator = - match Account.generate_accounts 1 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