diff --git a/src/proto_alpha/lib_protocol/test/helpers/constants_helpers.ml b/src/proto_alpha/lib_protocol/test/helpers/constants_helpers.ml new file mode 100644 index 0000000000000000000000000000000000000000..8ae4d2913f1baa18ada6a012aed36bf74defb5e6 --- /dev/null +++ b/src/proto_alpha/lib_protocol/test/helpers/constants_helpers.ml @@ -0,0 +1,162 @@ +(*****************************************************************************) +(* *) +(* SPDX-License-Identifier: MIT *) +(* Copyright (c) 2024 Nomadic Labs, *) +(* *) +(*****************************************************************************) + +type t = Protocol.Alpha_context.Constants.Parametric.t + +(* Warning: not a Set *) +module Set = struct + let consensus_rights_delay (c : t) consensus_rights_delay = + {c with consensus_rights_delay} + + let blocks_preservation_cycles (c : t) blocks_preservation_cycles = + {c with blocks_preservation_cycles} + + let delegate_parameters_activation_delay (c : t) + delegate_parameters_activation_delay = + {c with delegate_parameters_activation_delay} + + let blocks_per_cycle (c : t) blocks_per_cycle = {c with blocks_per_cycle} + + let blocks_per_commitment (c : t) blocks_per_commitment = + {c with blocks_per_commitment} + + let nonce_revelation_threshold (c : t) nonce_revelation_threshold = + {c with nonce_revelation_threshold} + + let cycles_per_voting_period (c : t) cycles_per_voting_period = + {c with cycles_per_voting_period} + + let hard_gas_limit_per_operation (c : t) hard_gas_limit_per_operation = + {c with hard_gas_limit_per_operation} + + let hard_gas_limit_per_block (c : t) hard_gas_limit_per_block = + {c with hard_gas_limit_per_block} + + let proof_of_work_threshold (c : t) proof_of_work_threshold = + {c with proof_of_work_threshold} + + let minimal_stake (c : t) minimal_stake = {c with minimal_stake} + + let minimal_frozen_stake (c : t) minimal_frozen_stake = + {c with minimal_frozen_stake} + + let vdf_difficulty (c : t) vdf_difficulty = {c with vdf_difficulty} + + let origination_size (c : t) origination_size = {c with origination_size} + + let cost_per_byte (c : t) cost_per_byte = {c with cost_per_byte} + + let hard_storage_limit_per_operation (c : t) hard_storage_limit_per_operation + = + {c with hard_storage_limit_per_operation} + + let quorum_min (c : t) quorum_min = {c with quorum_min} + + let quorum_max (c : t) quorum_max = {c with quorum_max} + + let min_proposal_quorum (c : t) min_proposal_quorum = + {c with min_proposal_quorum} + + let liquidity_baking_subsidy (c : t) liquidity_baking_subsidy = + {c with liquidity_baking_subsidy} + + let liquidity_baking_toggle_ema_threshold (c : t) + liquidity_baking_toggle_ema_threshold = + {c with liquidity_baking_toggle_ema_threshold} + + let max_operations_time_to_live (c : t) max_operations_time_to_live = + {c with max_operations_time_to_live} + + let minimal_block_delay (c : t) minimal_block_delay = + {c with minimal_block_delay} + + let delay_increment_per_round (c : t) delay_increment_per_round = + {c with delay_increment_per_round} + + let minimal_participation_ratio (c : t) minimal_participation_ratio = + {c with minimal_participation_ratio} + + let consensus_committee_size (c : t) consensus_committee_size = + {c with consensus_committee_size} + + let consensus_threshold (c : t) consensus_threshold = + {c with consensus_threshold} + + let limit_of_delegation_over_baking (c : t) limit_of_delegation_over_baking = + {c with limit_of_delegation_over_baking} + + let percentage_of_frozen_deposits_slashed_per_double_baking (c : t) + percentage_of_frozen_deposits_slashed_per_double_baking = + {c with percentage_of_frozen_deposits_slashed_per_double_baking} + + let percentage_of_frozen_deposits_slashed_per_double_attestation (c : t) + percentage_of_frozen_deposits_slashed_per_double_attestation = + {c with percentage_of_frozen_deposits_slashed_per_double_attestation} + + let max_slashing_per_block (c : t) max_slashing_per_block = + {c with max_slashing_per_block} + + let max_slashing_threshold (c : t) max_slashing_threshold = + {c with max_slashing_threshold} + + let testnet_dictator (c : t) testnet_dictator = {c with testnet_dictator} + + let initial_seed (c : t) initial_seed = {c with initial_seed} + + let cache_script_size (c : t) cache_script_size = {c with cache_script_size} + + let cache_stake_distribution_cycles (c : t) cache_stake_distribution_cycles = + {c with cache_stake_distribution_cycles} + + let cache_sampler_state_cycles (c : t) cache_sampler_state_cycles = + {c with cache_sampler_state_cycles} + + let dal (c : t) dal = {c with dal} + + let sc_rollup (c : t) sc_rollup = {c with sc_rollup} + + let zk_rollup (c : t) zk_rollup = {c with zk_rollup} + + let direct_ticket_spending_enable (c : t) direct_ticket_spending_enable = + {c with direct_ticket_spending_enable} + + let issuance_weights (c : t) issuance_weights = {c with issuance_weights} + + module Issuance_weights = struct + let base_total_issued_per_minute (c : t) base_total_issued_per_minute = + issuance_weights c {c.issuance_weights with base_total_issued_per_minute} + end + + let adaptive_issuance (c : t) adaptive_issuance = {c with adaptive_issuance} + + module Adaptive_issuance = struct + let activation_vote_enable (c : t) activation_vote_enable = + adaptive_issuance c {c.adaptive_issuance with activation_vote_enable} + + let autostaking_enable (c : t) autostaking_enable = + adaptive_issuance c {c.adaptive_issuance with autostaking_enable} + + let force_activation (c : t) force_activation = + adaptive_issuance c {c.adaptive_issuance with force_activation} + + let ns_enable (c : t) ns_enable = + adaptive_issuance c {c.adaptive_issuance with ns_enable} + + let launch_ema_threshold (c : t) launch_ema_threshold = + adaptive_issuance c {c.adaptive_issuance with launch_ema_threshold} + + let adaptive_rewards_params (c : t) adaptive_rewards_params = + adaptive_issuance c {c.adaptive_issuance with adaptive_rewards_params} + + module Adaptive_rewards_params = struct + let max_bonus (c : t) max_bonus = + adaptive_rewards_params + c + {c.adaptive_issuance.adaptive_rewards_params with max_bonus} + end + end +end diff --git a/src/proto_alpha/lib_protocol/test/helpers/scenario.ml b/src/proto_alpha/lib_protocol/test/helpers/scenario.ml new file mode 100644 index 0000000000000000000000000000000000000000..6d493167b1ea208dce11086e80392868fe97a0f4 --- /dev/null +++ b/src/proto_alpha/lib_protocol/test/helpers/scenario.ml @@ -0,0 +1,16 @@ +(*****************************************************************************) +(* *) +(* SPDX-License-Identifier: MIT *) +(* Copyright (c) 2024 Nomadic Labs, *) +(* *) +(*****************************************************************************) + +(** This module centralises all modules related to [Scenario] writing and + execution. Most scenario tests would use most if not all of them, so + they only need to [open Scenario]. *) + +include Scenario_base +include Scenario_op +include Scenario_dsl +include Scenario_begin +include Scenario_constants diff --git a/src/proto_alpha/lib_protocol/test/helpers/scenario_base.ml b/src/proto_alpha/lib_protocol/test/helpers/scenario_base.ml index 8121a1ac723b7fcc44fac9fdef4a9290191faf50..9bb65e47cedefe72575d808c0874d3634b919e6a 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/scenario_base.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/scenario_base.ml @@ -15,9 +15,6 @@ open Scenario_dsl open Log_helpers open Adaptive_issuance_helpers -(** Returns when the number of bootstrap accounts created by [Context.init_n n] is not equal to [n] *) -type error += Inconsistent_number_of_bootstrap_accounts - (** For [assert_failure], when expected error does not match the actual error. *) type error += Unexpected_error @@ -25,6 +22,12 @@ type error += Unexpected_error and the known [State.t] *) type t = Block.t * State.t +let log ?color s = + let open Lwt_result_syntax in + exec_unit (fun _ -> + Log.info ?color s ; + return_unit) + (* ======== Baking ======== *) (** After baking and applying rewards in state *) @@ -336,116 +339,6 @@ let exec_op f = return (block, state)) --> next_block -(* ======== Definition of basic actions ======== *) - -(** Initialize the test, given some initial parameters *) -let begin_test ~activate_ai ?(burn_rewards = false) ?(ns_enable_fork = false) - ?(constants : Protocol.Alpha_context.Constants.Parametric.t option) - ?(constants_list : - (string * Protocol.Alpha_context.Constants.Parametric.t) list option) - delegates_name_list : (unit, t) scenarios = - let f ns_enable = - (match (constants, constants_list) with - | None, None -> Stdlib.failwith "No constants provided to begin_test" - | Some _, Some _ -> - Stdlib.failwith - "You cannot provide ~constants and ~constants_list to begin_test" - | None, Some constants_list -> list_to_branch constants_list - | Some constants, None -> Action (fun () -> return constants)) - --> exec (fun (constants : Protocol.Alpha_context.Constants.Parametric.t) -> - let open Lwt_result_syntax in - Log.info ~color:begin_end_color "-- Begin test --" ; - let bootstrap = "__bootstrap__" in - let delegates_name_list = bootstrap :: delegates_name_list in - (* Override threshold value if activate *) - let constants = - if activate_ai then ( - Log.info ~color:event_color "Setting ai threshold to 0" ; - { - constants with - adaptive_issuance = - { - constants.adaptive_issuance with - launch_ema_threshold = 0l; - activation_vote_enable = true; - ns_enable; - }; - }) - else constants - in - let n = List.length delegates_name_list in - let* block, delegates = Context.init_with_constants_n constants n in - let*? init_level = Context.get_level (B block) in - let init_staked = Tez.of_mutez 200_000_000_000L in - let*? account_map = - List.fold_left2 - ~when_different_lengths: - [Inconsistent_number_of_bootstrap_accounts] - (fun account_map name contract -> - let liquid = - Tez.(Account.default_initial_balance -! init_staked) - in - let frozen_deposits = Frozen_tez.init init_staked name name in - let frozen_rights = - List.fold_left - (fun map cycle -> CycleMap.add cycle init_staked map) - CycleMap.empty - Cycle.( - root ---> add root constants.consensus_rights_delay) - in - let pkh = Context.Contract.pkh contract in - let account = - init_account - ~delegate:name - ~pkh - ~contract - ~parameters:default_params - ~liquid - ~frozen_deposits - ~frozen_rights - () - in - let account_map = String.Map.add name account account_map in - let balance, total_balance = - balance_and_total_balance_of_account name account_map - in - Log.debug - "Initial balance for %s:\n%a" - name - balance_pp - balance ; - Log.debug "Initial total balance: %a" Tez.pp total_balance ; - account_map) - String.Map.empty - delegates_name_list - delegates - in - let* total_supply = Context.get_total_supply (B block) in - let state = - State. - { - account_map; - total_supply; - constants; - param_requests = []; - activate_ai; - baking_policy = None; - last_level_rewards = init_level; - snapshot_balances = String.Map.empty; - saved_rate = None; - burn_rewards; - pending_operations = []; - pending_slashes = []; - double_signings = []; - } - in - let* () = check_all_balances block state in - return (block, state)) - in - if ns_enable_fork then - Tag "ns_enable = true" --> f true |+ Tag "ns_enable = false" --> f false - else f false - (* ======== Misc functions ========*) let check_failure_aux ?expected_error : diff --git a/src/proto_alpha/lib_protocol/test/helpers/scenario_begin.ml b/src/proto_alpha/lib_protocol/test/helpers/scenario_begin.ml new file mode 100644 index 0000000000000000000000000000000000000000..5c21b92705f07ab9ecd182c2feb0917033b8967e --- /dev/null +++ b/src/proto_alpha/lib_protocol/test/helpers/scenario_begin.ml @@ -0,0 +1,157 @@ +(*****************************************************************************) +(* *) +(* SPDX-License-Identifier: MIT *) +(* Copyright (c) 2024 Nomadic Labs, *) +(* *) +(*****************************************************************************) + +open State_account +open Scenario_dsl +open Scenario_base +open Log_helpers +open Adaptive_issuance_helpers +open Scenario_constants + +(** Returns when the number of bootstrap accounts created by [Context.init_n n] is not equal to [n] *) +type error += Inconsistent_number_of_bootstrap_accounts + +type starter_constants = Mainnet | Sandbox | Test + +let start ~(constants : starter_constants) : (unit, constants) scenarios = + let constants, name = + match constants with + | Mainnet -> (Default_parameters.constants_mainnet, "mainnet") + | Sandbox -> (Default_parameters.constants_sandbox, "sandbox") + | Test -> (Default_parameters.constants_test, "test") + in + Action + (fun () -> + Log.info ~color:begin_end_color "-- Begin test --" ; + Log.info "Loading constants_%s." name ; + return constants) + +let start_with ~(constants : constants) : (unit, constants) scenarios = + Action + (fun () -> + Log.info ~color:begin_end_color "-- Begin test --" ; + Log.info "Loading custom constants." ; + return constants) + +let start_with_list ~(constants : (string * constants) list) : + (unit, constants) scenarios = + match constants with + | [] -> + Stdlib.failwith + (Format.asprintf "%s: Cannot build scenarios from empty list" __LOC__) + | _ -> fold_tag (fun constants -> start_with ~constants) constants + +let activate_ai flag = + if flag then + log ~color:event_color "Setting ai threshold to 0" + --> set S.Adaptive_issuance.launch_ema_threshold 0l + --> set S.Adaptive_issuance.activation_vote_enable true + else Empty + +(** Initializes the constants for testing, with well chosen default values. + Recommended over [start] or [start_with] *) +let init_constants ?(default = Test) ?(reward_per_block = 0L) + ?(deactivate_dynamic = false) ?blocks_per_cycle + ?delegate_parameters_activation_delay () = + let base_total_issued_per_minute = Tez.of_mutez reward_per_block in + start ~constants:default + --> (* default for tests: 12 *) + set_opt S.blocks_per_cycle blocks_per_cycle + --> set_opt + S.delegate_parameters_activation_delay + delegate_parameters_activation_delay + --> set + S.issuance_weights + { + base_total_issued_per_minute; + baking_reward_fixed_portion_weight = 1; + baking_reward_bonus_weight = 0; + attesting_reward_weight = 0; + seed_nonce_revelation_tip_weight = 0; + vdf_revelation_tip_weight = 0; + } + --> set S.liquidity_baking_subsidy Tez.zero + --> set S.minimal_block_delay Protocol.Alpha_context.Period.one_minute + --> set S.cost_per_byte Tez.zero + --> set S.consensus_threshold 0 + --> (if deactivate_dynamic then + set + S.Adaptive_issuance.Adaptive_rewards_params.max_bonus + (Protocol.Issuance_bonus_repr.max_bonus_parameter_of_Q_exn Q.zero) + else Empty) + --> set S.Adaptive_issuance.ns_enable false + +(** Initialize the test, given some initial parameters *) +let begin_test ?(burn_rewards = false) delegates_name_list : + (constants, t) scenarios = + exec (fun (constants : constants) -> + let open Lwt_result_syntax in + let bootstrap = "__bootstrap__" in + let delegates_name_list = bootstrap :: delegates_name_list in + (* Override threshold value if activate *) + let n = List.length delegates_name_list in + let* block, delegates = Context.init_with_constants_n constants n in + let*? init_level = Context.get_level (B block) in + let init_staked = Tez.of_mutez 200_000_000_000L in + let*? account_map = + List.fold_left2 + ~when_different_lengths:[Inconsistent_number_of_bootstrap_accounts] + (fun account_map name contract -> + let liquid = Tez.(Account.default_initial_balance -! init_staked) in + let frozen_deposits = Frozen_tez.init init_staked name name in + let frozen_rights = + List.fold_left + (fun map cycle -> CycleMap.add cycle init_staked map) + CycleMap.empty + Cycle.(root ---> add root constants.consensus_rights_delay) + in + let pkh = Context.Contract.pkh contract in + let account = + init_account + ~delegate:name + ~pkh + ~contract + ~parameters:default_params + ~liquid + ~frozen_deposits + ~frozen_rights + () + in + let account_map = String.Map.add name account account_map in + let balance, total_balance = + balance_and_total_balance_of_account name account_map + in + Log.debug "Initial balance for %s:\n%a" name balance_pp balance ; + Log.debug "Initial total balance: %a" Tez.pp total_balance ; + account_map) + String.Map.empty + delegates_name_list + delegates + in + let* total_supply = Context.get_total_supply (B block) in + let state = + State. + { + account_map; + total_supply; + constants; + param_requests = []; + activate_ai = + constants.adaptive_issuance.activation_vote_enable + && constants.adaptive_issuance.launch_ema_threshold = 0l; + baking_policy = None; + last_level_rewards = init_level; + snapshot_balances = String.Map.empty; + saved_rate = None; + burn_rewards; + pending_operations = []; + pending_slashes = []; + double_signings = []; + } + in + let* () = check_all_balances block state in + return (block, state)) diff --git a/src/proto_alpha/lib_protocol/test/helpers/scenario_constants.ml b/src/proto_alpha/lib_protocol/test/helpers/scenario_constants.ml new file mode 100644 index 0000000000000000000000000000000000000000..266776606147f47cf8ee4ee2b39f367c101c2909 --- /dev/null +++ b/src/proto_alpha/lib_protocol/test/helpers/scenario_constants.ml @@ -0,0 +1,50 @@ +(*****************************************************************************) +(* *) +(* SPDX-License-Identifier: MIT *) +(* Copyright (c) 2024 Nomadic Labs, *) +(* *) +(*****************************************************************************) + +open Scenario_dsl + +type constants = Constants_helpers.t + +(* Not a Set *) +module S = Constants_helpers.Set + +let set : + (constants -> 'a -> constants) -> 'a -> (constants, constants) scenarios = + fun f x -> Action (fun csts -> return @@ f csts x) + +let set_opt : + (constants -> 'a -> constants) -> + 'a option -> + (constants, constants) scenarios = + fun f -> function None -> Empty | Some x -> set f x + +let sets : + (constants -> 'a -> constants) -> + (string * 'a) list -> + (constants, constants) scenarios = + fun f -> fold_tag (set f) + +let sets_f : + (constants -> 'a -> constants) -> + ('a -> string) -> + 'a list -> + (constants, constants) scenarios = + fun f f_tag -> fold_tag_f (set f) f_tag + +let branch_flag : + (constants -> bool -> constants) -> (constants, constants) scenarios = + fun f -> sets f [("true", true); ("false", false)] + +let branch_flags : + (constants -> bool -> constants) list -> (constants, constants) scenarios = + unfold branch_flag + +let sets_int : + (constants -> int -> constants) -> + int list -> + (constants, constants) scenarios = + fun f -> sets_f f string_of_int diff --git a/src/proto_alpha/lib_protocol/test/helpers/scenario_dsl.ml b/src/proto_alpha/lib_protocol/test/helpers/scenario_dsl.ml index efb4686a65925a28c72bd863537a00d8b748f08d..2f8b87ed57ea1dfbc33ce1ede0e66c4f25eaf967 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/scenario_dsl.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/scenario_dsl.ml @@ -130,20 +130,6 @@ let ( --> ) a b = concat a b (** Branching connector: creates two tests with different execution paths *) let ( |+ ) a b = branch a b -let list_to_branch (list : (string * 'a) list) : (unit, 'a) scenarios = - match list with - | [] -> - Stdlib.failwith - (Format.asprintf - "%s: Cannot build scenarios from\n empty list" - __LOC__) - | (tag, h) :: t -> - List.fold_left - (fun scenarios (tag, elt) -> - scenarios |+ Tag tag --> Action (fun () -> return elt)) - (Tag tag --> Action (fun () -> return h)) - t - (** Ends the test. Dump the state, returns [unit] *) let end_test : ('a, unit) scenarios = let open Lwt_result_syntax in @@ -180,3 +166,34 @@ let exec_unit f = (fun input -> let* () = f input in return input) + +(** [fold f l] folds [f] over [l], fails on empty list *) +let rec fold : ('a -> ('b, 'c) scenarios) -> 'a list -> ('b, 'c) scenarios = + fun f list -> + match list with + | [] -> Stdlib.failwith "Scenario_dsl.fold: empty list" + | [x] -> f x + | h :: t -> f h |+ fold f t + +(** [fold_tag f l] folds [f] over [l], [l] has a tag for each of its elements. + Fails on empty list. *) +let fold_tag : + ('a -> ('b, 'c) scenarios) -> (string * 'a) list -> ('b, 'c) scenarios = + fun f -> + let f (s, x) = Tag s --> f x in + fold f + +(** [fold_tag_f f tag l] folds [f] over [l], [tag] returns a tag for each element of [l]. + Fails on empty list. *) +let fold_tag_f : + ('a -> ('b, 'c) scenarios) -> + ('a -> string) -> + 'a list -> + ('b, 'c) scenarios = + fun f tag -> + let f x = Tag (tag x) --> f x in + fold f + +(** [unfold f l] maps [f] over [l], and runs them in order *) +let rec unfold : ('a -> ('b, 'b) scenarios) -> 'a list -> ('b, 'b) scenarios = + fun f -> function [] -> Empty | [x] -> f x | h :: t -> f h --> unfold f t diff --git a/src/proto_alpha/lib_protocol/test/integration/test_scenario_autostaking.ml b/src/proto_alpha/lib_protocol/test/integration/test_scenario_autostaking.ml index 2239559e442cccbd010a06211b7a3cce89755622..d539e73d82074aa7361b4d363e74e7a52b75e93b 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_scenario_autostaking.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_scenario_autostaking.ml @@ -15,10 +15,7 @@ open State_account open Tez_helpers.Ez_tez -open Scenario_dsl -open Scenario_base -open Scenario_op -open Test_scenario_base +open Scenario open Log_helpers let assert_balance_evolution ~loc ~for_accounts ~part ~name ~old_balance @@ -53,8 +50,10 @@ and delegator1 = "delegator1" and delegator2 = "delegator2" let setup ~activate_ai = - let constants = init_constants ~autostaking_enable:true () in - begin_test ~activate_ai ~constants [delegate] + init_constants () + --> set S.Adaptive_issuance.autostaking_enable true + --> Scenario_begin.activate_ai activate_ai + --> begin_test [delegate] --> add_account_with_funds delegator1 "__bootstrap__" @@ -142,11 +141,10 @@ let test_autostaking = let test_overdelegation = (* This test assumes that all delegate accounts created in [begin_test] begin with 4M tz, with 5% staked *) - let constants = init_constants ~autostaking_enable:true () in - begin_test - ~activate_ai:false - ~constants - ["delegate"; "faucet1"; "faucet2"; "faucet3"] + init_constants () + --> set S.Adaptive_issuance.autostaking_enable true + --> activate_ai false + --> begin_test ["delegate"; "faucet1"; "faucet2"; "faucet3"] --> add_account_with_funds "delegator_to_fund" "delegate" diff --git a/src/proto_alpha/lib_protocol/test/integration/test_scenario_base.ml b/src/proto_alpha/lib_protocol/test/integration/test_scenario_base.ml index d4f8a14ae7320d5f6437f39a845f6f8d5e81aed6..28aff544119cbe299f4df21ab462df68c89cc58c 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_scenario_base.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_scenario_base.ml @@ -16,9 +16,7 @@ open Adaptive_issuance_helpers open State_account open Tez_helpers.Ez_tez -open Scenario_dsl -open Scenario_base -open Scenario_op +open Scenario let default_param_wait, default_unstake_wait = let constants = Default_parameters.constants_test in @@ -38,61 +36,6 @@ let test_expected_error = [Inconsistent_number_of_bootstrap_accounts]) (exec (fun _ -> failwith ""))) -let init_constants ?reward_per_block ?(deactivate_dynamic = false) - ?blocks_per_cycle ?delegate_parameters_activation_delay ~autostaking_enable - () = - let reward_per_block = Option.value ~default:0L reward_per_block in - let base_total_issued_per_minute = Tez.of_mutez reward_per_block in - let default_constants = Default_parameters.constants_test in - (* default for tests: 12 *) - let blocks_per_cycle = - Option.value ~default:default_constants.blocks_per_cycle blocks_per_cycle - in - let delegate_parameters_activation_delay = - Option.value - ~default:default_constants.delegate_parameters_activation_delay - delegate_parameters_activation_delay - in - let issuance_weights = - Protocol.Alpha_context.Constants.Parametric. - { - base_total_issued_per_minute; - baking_reward_fixed_portion_weight = 1; - baking_reward_bonus_weight = 0; - attesting_reward_weight = 0; - seed_nonce_revelation_tip_weight = 0; - vdf_revelation_tip_weight = 0; - } - in - let liquidity_baking_subsidy = Tez.zero in - let minimal_block_delay = Protocol.Alpha_context.Period.one_minute in - let cost_per_byte = Tez.zero in - let consensus_threshold = 0 in - let adaptive_issuance = default_constants.adaptive_issuance in - let adaptive_rewards_params = - if deactivate_dynamic then - { - adaptive_issuance.adaptive_rewards_params with - max_bonus = - Protocol.Issuance_bonus_repr.max_bonus_parameter_of_Q_exn Q.zero; - } - else adaptive_issuance.adaptive_rewards_params - in - let adaptive_issuance = - {adaptive_issuance with adaptive_rewards_params; autostaking_enable} - in - { - default_constants with - delegate_parameters_activation_delay; - consensus_threshold; - issuance_weights; - minimal_block_delay; - cost_per_byte; - adaptive_issuance; - blocks_per_cycle; - liquidity_baking_subsidy; - } - (** Initialization of scenarios with 3 cases: - AI activated, staker = delegate - AI activated, staker != delegate @@ -100,15 +43,15 @@ let init_constants ?reward_per_block ?(deactivate_dynamic = false) Any scenario that begins with this will be triplicated. *) let init_scenario ?(force_ai = true) ?reward_per_block () = - let constants = - init_constants ?reward_per_block ~autostaking_enable:false () - in let init_params = {limit_of_staking_over_baking = Q.one; edge_of_baking_over_staking = Q.one} in let begin_test ~activate_ai ~self_stake = let name = if self_stake then "staker" else "delegate" in - begin_test ~activate_ai ~constants [name] + init_constants ?reward_per_block () + --> set S.Adaptive_issuance.autostaking_enable false + --> Scenario_begin.activate_ai activate_ai + --> begin_test [name] --> set_delegate_params name init_params --> set_baker "__bootstrap__" in diff --git a/src/proto_alpha/lib_protocol/test/integration/test_scenario_rewards.ml b/src/proto_alpha/lib_protocol/test/integration/test_scenario_rewards.ml index 4a042ae3452a1424bd1872469680148dddd3280f..9cf97ea447feac2cb9cbed816c8637f34a3681b0 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_scenario_rewards.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_scenario_rewards.ml @@ -16,15 +16,9 @@ open Adaptive_issuance_helpers open State_account open Tez_helpers.Ez_tez -open Scenario_dsl -open Scenario_base -open Scenario_op -open Test_scenario_base +open Scenario let test_wait_with_rewards = - let constants = - init_constants ~reward_per_block:1_000_000_000L ~autostaking_enable:false () - in let set_edge pct = let params = { @@ -34,7 +28,10 @@ let test_wait_with_rewards = in set_delegate_params "delegate" params in - begin_test ~activate_ai:true ~constants ["delegate"; "faucet"] + init_constants ~reward_per_block:1_000_000_000L () + --> set S.Adaptive_issuance.autostaking_enable false + --> activate_ai true + --> begin_test ["delegate"; "faucet"] --> set_baker "faucet" --> (Tag "edge = 0" --> set_edge 0. |+ Tag "edge = 0.24" --> set_edge 0.24 @@ -77,15 +74,11 @@ let test_wait_with_rewards = |+ Tag "cycle step" --> wait_n_cycles 10) let test_ai_curve_activation_time = - let constants = - init_constants - ~reward_per_block:1_000_000_000L - ~deactivate_dynamic:true - ~autostaking_enable:false - () - in - let pc = constants.consensus_rights_delay in - begin_test ~activate_ai:true ~burn_rewards:true ~constants [""] + let pc = Default_parameters.constants_test.consensus_rights_delay in + init_constants ~reward_per_block:1_000_000_000L ~deactivate_dynamic:true () + --> set S.Adaptive_issuance.autostaking_enable false + --> activate_ai true + --> begin_test ~burn_rewards:true [""] --> next_block --> save_current_rate (* before AI rate *) --> wait_ai_activation (* Rate remains unchanged right after AI activation, we must wait [pc + 1] cycles *) @@ -98,14 +91,7 @@ let test_ai_curve_activation_time = --> check_rate_evolution Q.gt let test_static = - let constants = - init_constants - ~reward_per_block:1_000_000_000L - ~deactivate_dynamic:true - ~autostaking_enable:false - () - in - let rate_var_lag = constants.consensus_rights_delay in + let rate_var_lag = Default_parameters.constants_test.consensus_rights_delay in let init_params = {limit_of_staking_over_baking = Q.one; edge_of_baking_over_staking = Q.one} in @@ -121,7 +107,10 @@ let test_static = let cycle_stable = save_current_rate --> next_cycle --> check_rate_evolution Q.equal in - begin_test ~activate_ai:true ~burn_rewards:true ~constants ["delegate"] + init_constants ~reward_per_block:1_000_000_000L ~deactivate_dynamic:true () + --> set S.Adaptive_issuance.autostaking_enable false + --> activate_ai true + --> begin_test ~burn_rewards:true ["delegate"] --> set_delegate_params "delegate" init_params --> save_current_rate --> wait_ai_activation (* We stake about 50% of the total supply *) diff --git a/src/proto_alpha/lib_protocol/test/integration/test_scenario_slashing.ml b/src/proto_alpha/lib_protocol/test/integration/test_scenario_slashing.ml index 5e85c36b61d5aec9cbedfe698b87367f703a703e..9e040ca8701697952bc4f4562c44cc94792ceb06 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_scenario_slashing.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_scenario_slashing.ml @@ -34,15 +34,12 @@ open Adaptive_issuance_helpers open State_account open Tez_helpers.Ez_tez -open Scenario_dsl -open Scenario_base -open Scenario_op -open Test_scenario_base +open Scenario +open Scenario_constants let fs = Format.asprintf let test_simple_slash = - let constants = init_constants ~autostaking_enable:false () in let any_slash delegate = Tag "double baking" --> double_bake delegate |+ Tag "double attesting" @@ -50,11 +47,11 @@ let test_simple_slash = |+ Tag "double preattesting" --> double_preattest ~other_bakers:("bootstrap2", "bootstrap3") delegate in - begin_test - ~activate_ai:true - ~ns_enable_fork:true - ~constants - ["delegate"; "bootstrap1"; "bootstrap2"; "bootstrap3"] + init_constants () + --> set S.Adaptive_issuance.autostaking_enable false + --> activate_ai true + --> branch_flag S.Adaptive_issuance.ns_enable + --> begin_test ["delegate"; "bootstrap1"; "bootstrap2"; "bootstrap3"] --> (Tag "No AI" --> next_cycle |+ Tag "Yes AI" --> next_block --> wait_ai_activation) --> any_slash "delegate" @@ -128,14 +125,11 @@ let check_is_not_forbidden baker = return input) let test_delegate_forbidden = - let constants = - init_constants ~blocks_per_cycle:30l ~autostaking_enable:false () - in - begin_test - ~activate_ai:false - ~ns_enable_fork:true - ~constants - ["delegate"; "bootstrap1"; "bootstrap2"] + init_constants ~blocks_per_cycle:30l () + --> set S.Adaptive_issuance.autostaking_enable false + --> activate_ai false + --> branch_flag S.Adaptive_issuance.ns_enable + --> begin_test ["delegate"; "bootstrap1"; "bootstrap2"] --> set_baker "bootstrap1" --> (Tag "Many double bakes" --> loop_action 14 (double_bake_ "delegate") @@ -176,12 +170,11 @@ let test_delegate_forbidden = --> check_is_forbidden "delegate") let test_slash_unstake = - let constants = init_constants ~autostaking_enable:false () in - begin_test - ~activate_ai:false - ~ns_enable_fork:true - ~constants - ["delegate"; "bootstrap1"; "bootstrap2"] + init_constants () + --> set S.Adaptive_issuance.autostaking_enable false + --> activate_ai false + --> branch_flag S.Adaptive_issuance.ns_enable + --> begin_test ["delegate"; "bootstrap1"; "bootstrap2"] --> set_baker "bootstrap1" --> next_cycle --> unstake "delegate" Half --> next_cycle --> double_bake "delegate" --> make_denunciations () --> (Empty |+ Tag "unstake twice" --> unstake "delegate" Half) @@ -190,14 +183,11 @@ let test_slash_unstake = let test_slash_monotonous_stake = let scenario ~offending_op ~op ~early_d = - let constants = - init_constants ~blocks_per_cycle:16l ~autostaking_enable:false () - in - begin_test - ~activate_ai:false - ~ns_enable_fork:true - ~constants - ["delegate"; "bootstrap1"] + init_constants ~blocks_per_cycle:16l () + --> set S.Adaptive_issuance.autostaking_enable false + --> activate_ai false + --> branch_flag S.Adaptive_issuance.ns_enable + --> begin_test ["delegate"; "bootstrap1"] --> next_cycle --> loop 6 @@ -243,11 +233,11 @@ let test_slash_monotonous_stake = --> make_denunciations () let test_slash_timing = - let constants = - init_constants ~blocks_per_cycle:8l ~autostaking_enable:false () - in - begin_test ~activate_ai:false ~ns_enable_fork:true ~constants ["delegate"] - --> next_cycle + init_constants ~blocks_per_cycle:8l () + --> set S.Adaptive_issuance.autostaking_enable false + --> activate_ai false + --> branch_flag S.Adaptive_issuance.ns_enable + --> begin_test ["delegate"] --> next_cycle --> (Tag "stake" --> stake "delegate" Half |+ Tag "unstake" --> unstake "delegate" Half) --> (Tag "with a first slash" --> double_bake "delegate" @@ -261,7 +251,6 @@ let test_slash_timing = --> double_bake "delegate" --> make_denunciations () --> next_cycle let init_scenario_with_delegators delegate_name faucet_name delegators_list = - let constants = init_constants ~autostaking_enable:false () in let rec init_delegators = function | [] -> Empty | (delegator, amount) :: t -> @@ -275,11 +264,11 @@ let init_scenario_with_delegators delegate_name faucet_name delegators_list = let init_params = {limit_of_staking_over_baking = Q.one; edge_of_baking_over_staking = Q.one} in - begin_test - ~activate_ai:true - ~ns_enable_fork:true - ~constants - [delegate_name; faucet_name] + init_constants () + --> set S.Adaptive_issuance.autostaking_enable false + --> activate_ai true + --> branch_flag S.Adaptive_issuance.ns_enable + --> begin_test [delegate_name; faucet_name] --> set_baker faucet_name --> set_delegate_params "delegate" init_params --> init_delegators delegators_list @@ -327,14 +316,14 @@ let test_many_slashes = --> slash "delegate" --> next_cycle))*) let test_no_shortcut_for_cheaters = - let constants = init_constants ~autostaking_enable:false () in let amount = Amount (Tez.of_mutez 333_000_000_000L) in - let consensus_rights_delay = constants.consensus_rights_delay in - begin_test - ~activate_ai:true - ~ns_enable_fork:false - ~constants - ["delegate"; "bootstrap1"] + let consensus_rights_delay = + Default_parameters.constants_test.consensus_rights_delay + in + init_constants () + --> set S.Adaptive_issuance.autostaking_enable false + --> activate_ai true + --> begin_test ["delegate"; "bootstrap1"] --> next_block --> wait_ai_activation --> stake "delegate" (Amount (Tez.of_mutez 1_800_000_000_000L)) --> next_cycle --> double_bake "delegate" --> make_denunciations () @@ -354,16 +343,16 @@ let test_no_shortcut_for_cheaters = --> check_snapshot_balances "init") let test_slash_correct_amount_after_stake_from_unstake = - let constants = init_constants ~autostaking_enable:false () in let amount_to_unstake = Amount (Tez.of_mutez 200_000_000_000L) in let amount_to_restake = Amount (Tez.of_mutez 100_000_000_000L) in let amount_expected_in_unstake_after_slash = Tez.of_mutez 50_000_000_000L in - let consensus_rights_delay = constants.consensus_rights_delay in - begin_test - ~activate_ai:true - ~ns_enable_fork:false - ~constants - ["delegate"; "bootstrap1"] + let consensus_rights_delay = + Default_parameters.constants_test.consensus_rights_delay + in + init_constants () + --> set S.Adaptive_issuance.autostaking_enable false + --> activate_ai true + --> begin_test ["delegate"; "bootstrap1"] --> next_block --> wait_ai_activation --> stake "delegate" (Amount (Tez.of_mutez 1_800_000_000_000L)) --> next_cycle @@ -383,20 +372,16 @@ let test_slash_correct_amount_after_stake_from_unstake = (* Test a non-zero request finalizes for a non-zero amount if it hasn't been slashed 100% *) let test_mini_slash = - let constants = init_constants ~autostaking_enable:false () in - (Tag "Yes AI" - --> begin_test - ~activate_ai:true - ~ns_enable_fork:false - ~constants - ["delegate"; "baker"] - --> next_block --> wait_ai_activation - |+ Tag "No AI" - --> begin_test - ~activate_ai:false - ~ns_enable_fork:false - ~constants - ["delegate"; "baker"]) + let consensus_rights_delay = + Default_parameters.constants_test.consensus_rights_delay + in + init_constants () + --> set S.Adaptive_issuance.autostaking_enable false + --> (Tag "Yes AI" --> activate_ai true + --> begin_test ["delegate"; "baker"] + --> next_block --> wait_ai_activation + |+ Tag "No AI" --> activate_ai false --> begin_test ["delegate"; "baker"] + ) --> unstake "delegate" (Amount Tez.one_mutez) --> set_baker "baker" --> next_cycle --> (Tag "5% slash" --> double_bake "delegate" --> make_denunciations () @@ -407,15 +392,14 @@ let test_mini_slash = --> next_cycle --> next_cycle --> check_balance_field "delegate" `Unstaked_frozen_total Tez.zero - --> wait_n_cycles (constants.consensus_rights_delay + 1) + --> wait_n_cycles (consensus_rights_delay + 1) let test_slash_rounding = - let constants = init_constants ~autostaking_enable:false () in - begin_test - ~activate_ai:true - ~ns_enable_fork:true - ~constants - ["delegate"; "baker"] + init_constants () + --> set S.Adaptive_issuance.autostaking_enable false + --> activate_ai true + --> branch_flag S.Adaptive_issuance.ns_enable + --> begin_test ["delegate"; "baker"] --> set_baker "baker" --> next_block --> wait_ai_activation --> unstake "delegate" (Amount (Tez.of_mutez 2L)) --> next_cycle --> double_bake "delegate" --> double_bake "delegate" diff --git a/src/proto_alpha/lib_protocol/test/integration/test_scenario_stake.ml b/src/proto_alpha/lib_protocol/test/integration/test_scenario_stake.ml index f5474e2eeaa4745d38a386ad3caac0acee13d4d9..b9f2dec685367ecfc04ddafafea8139e2b70b439 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_scenario_stake.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_scenario_stake.ml @@ -16,9 +16,7 @@ open Adaptive_issuance_helpers open State_account open Tez_helpers.Ez_tez -open Scenario_dsl -open Scenario_base -open Scenario_op +open Scenario open Test_scenario_base let fs = Format.asprintf @@ -58,11 +56,14 @@ let double_roundtrip = --> finalize "staker" --> next_cycle let shorter_roundtrip_for_baker = - let constants = init_constants ~autostaking_enable:false () in let amount = Amount (Tez.of_mutez 333_000_000_000L) in - let consensus_rights_delay = constants.consensus_rights_delay in - begin_test ~activate_ai:true ~constants ["delegate"] - --> next_block --> wait_ai_activation + let consensus_rights_delay = + Default_parameters.constants_test.consensus_rights_delay + in + init_constants () + --> set S.Adaptive_issuance.autostaking_enable false + --> activate_ai true --> begin_test ["delegate"] --> next_block + --> wait_ai_activation --> stake "delegate" (Amount (Tez.of_mutez 1_800_000_000_000L)) --> next_cycle --> snapshot_balances "init" ["delegate"] @@ -155,11 +156,13 @@ let odd_behavior = loop 20 one_cycle let change_delegate = - let constants = init_constants ~autostaking_enable:false () in let init_params = {limit_of_staking_over_baking = Q.one; edge_of_baking_over_staking = Q.one} in - begin_test ~activate_ai:true ~constants ["delegate1"; "delegate2"] + init_constants () + --> set S.Adaptive_issuance.autostaking_enable false + --> activate_ai true + --> begin_test ["delegate1"; "delegate2"] --> set_delegate_params "delegate1" init_params --> set_delegate_params "delegate2" init_params --> add_account_with_funds @@ -175,11 +178,12 @@ let change_delegate = --> stake "staker" Half let unset_delegate = - let constants = init_constants ~autostaking_enable:false () in let init_params = {limit_of_staking_over_baking = Q.one; edge_of_baking_over_staking = Q.one} in - begin_test ~activate_ai:true ~constants ["delegate"] + init_constants () + --> set S.Adaptive_issuance.autostaking_enable false + --> activate_ai true --> begin_test ["delegate"] --> set_delegate_params "delegate" init_params --> add_account_with_funds "staker" @@ -200,23 +204,6 @@ let unset_delegate = --> finalize_unstake "staker" let forbid_costaking = - let default_constants = - ("default protocol constants", init_constants ~autostaking_enable:false ()) - in - let small_delegate_parameter_constants = - ( "small delegate parameters delay", - init_constants - ~delegate_parameters_activation_delay:0 - ~autostaking_enable:false - () ) - in - let large_delegate_parameter_constants = - ( "large delegate parameters delay", - init_constants - ~delegate_parameters_activation_delay:10 - ~autostaking_enable:false - () ) - in let init_params = {limit_of_staking_over_baking = Q.one; edge_of_baking_over_staking = Q.one} in @@ -224,16 +211,17 @@ let forbid_costaking = {limit_of_staking_over_baking = Q.zero; edge_of_baking_over_staking = Q.one} in let amount = Amount (Tez.of_mutez 1_000_000L) in - (* init *) - begin_test - ~activate_ai:true - ~constants_list: - [ - default_constants; - small_delegate_parameter_constants; - large_delegate_parameter_constants; - ] - ["delegate"] + (* init constants *) + (Tag "default protocol constants" --> init_constants () + |+ Tag "small delegate parameters delay" + --> init_constants ~delegate_parameters_activation_delay:0 () + |+ Tag "large delegate parameters delay" + --> init_constants ~delegate_parameters_activation_delay:10 ()) + (* Set flags *) + --> set S.Adaptive_issuance.autostaking_enable false + --> activate_ai true + (* Start scenario *) + --> begin_test ["delegate"] --> set_delegate_params "delegate" init_params --> add_account_with_funds "staker"