From 573eb9ffeb4d36eb7a2b2c08c4ed75ad3e25beea Mon Sep 17 00:00:00 2001 From: Mathias Bourgoin Date: Tue, 16 Jan 2024 17:04:34 +0100 Subject: [PATCH 1/3] Proto/AI: Introduce two feature flags for AI Used in following commits --- .../files/alpha__constants.ksy | 6 ++++++ .../files/alpha__constants__parametric.ksy | 6 ++++++ .../files/alpha__parameters.ksy | 6 ++++++ .../lib_parameters/default_parameters.ml | 2 ++ .../lib_protocol/alpha_context.mli | 2 ++ .../lib_protocol/constants_parametric_repr.ml | 20 +++++++++++++++---- .../constants_parametric_repr.mli | 6 ++++++ .../lib_protocol/constants_storage.ml | 5 +++++ .../lib_protocol/constants_storage.mli | 4 ++++ src/proto_alpha/lib_protocol/raw_context.ml | 2 ++ ...t) RPC regression tests- misc_protocol.out | 3 ++- ...t) RPC regression tests- misc_protocol.out | 3 ++- ...y) RPC regression tests- misc_protocol.out | 3 ++- ...r) RPC regression tests- misc_protocol.out | 3 ++- ...c) RPC regression tests- misc_protocol.out | 3 ++- 15 files changed, 65 insertions(+), 9 deletions(-) diff --git a/contrib/kaitai-struct-files/files/alpha__constants.ksy b/contrib/kaitai-struct-files/files/alpha__constants.ksy index 934d4bb5dc8f..df425343fc0e 100644 --- a/contrib/kaitai-struct-files/files/alpha__constants.ksy +++ b/contrib/kaitai-struct-files/files/alpha__constants.ksy @@ -355,6 +355,12 @@ seq: - id: autostaking_enable type: u1 enum: bool +- id: adaptive_issuance_force_activation + type: u1 + enum: bool +- id: ns_enable + type: u1 + enum: bool - id: direct_ticket_spending_enable type: u1 enum: bool diff --git a/contrib/kaitai-struct-files/files/alpha__constants__parametric.ksy b/contrib/kaitai-struct-files/files/alpha__constants__parametric.ksy index 752688d91747..bd992556c3fe 100644 --- a/contrib/kaitai-struct-files/files/alpha__constants__parametric.ksy +++ b/contrib/kaitai-struct-files/files/alpha__constants__parametric.ksy @@ -327,6 +327,12 @@ seq: - id: autostaking_enable type: u1 enum: bool +- id: adaptive_issuance_force_activation + type: u1 + enum: bool +- id: ns_enable + type: u1 + enum: bool - id: direct_ticket_spending_enable type: u1 enum: bool diff --git a/contrib/kaitai-struct-files/files/alpha__parameters.ksy b/contrib/kaitai-struct-files/files/alpha__parameters.ksy index 9904127c7c66..acdd2515c22f 100644 --- a/contrib/kaitai-struct-files/files/alpha__parameters.ksy +++ b/contrib/kaitai-struct-files/files/alpha__parameters.ksy @@ -605,6 +605,12 @@ seq: - id: autostaking_enable type: u1 enum: bool +- id: adaptive_issuance_force_activation + type: u1 + enum: bool +- id: ns_enable + type: u1 + enum: bool - id: direct_ticket_spending_enable type: u1 enum: bool diff --git a/src/proto_alpha/lib_parameters/default_parameters.ml b/src/proto_alpha/lib_parameters/default_parameters.ml index 03f75600a512..ad0ec9a000ed 100644 --- a/src/proto_alpha/lib_parameters/default_parameters.ml +++ b/src/proto_alpha/lib_parameters/default_parameters.ml @@ -295,6 +295,8 @@ let constants_mainnet = }; activation_vote_enable = false; autostaking_enable = true; + force_activation = false; + ns_enable = false; }; (* TODO: https://gitlab.com/tezos/tezos/-/issues/6668 Enable once at least the following is done: diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index d3dc9f7ef7fb..7ab3ce623728 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -874,6 +874,8 @@ module Constants : sig adaptive_rewards_params : adaptive_rewards_params; activation_vote_enable : bool; autostaking_enable : bool; + force_activation : bool; + ns_enable : bool; } type issuance_weights = { diff --git a/src/proto_alpha/lib_protocol/constants_parametric_repr.ml b/src/proto_alpha/lib_protocol/constants_parametric_repr.ml index b66fc7a8e6b9..cfc9a65b6653 100644 --- a/src/proto_alpha/lib_protocol/constants_parametric_repr.ml +++ b/src/proto_alpha/lib_protocol/constants_parametric_repr.ml @@ -163,6 +163,8 @@ type adaptive_issuance = { adaptive_rewards_params : adaptive_rewards_params; activation_vote_enable : bool; autostaking_enable : bool; + force_activation : bool; + ns_enable : bool; } type issuance_weights = { @@ -437,19 +439,25 @@ let adaptive_issuance_encoding = adaptive_rewards_params; activation_vote_enable; autostaking_enable; + force_activation; + ns_enable; } -> ( global_limit_of_staking_over_baking, edge_of_staking_over_delegation, launch_ema_threshold, adaptive_rewards_params, activation_vote_enable, - autostaking_enable )) + autostaking_enable, + force_activation, + ns_enable )) (fun ( global_limit_of_staking_over_baking, edge_of_staking_over_delegation, launch_ema_threshold, adaptive_rewards_params, activation_vote_enable, - autostaking_enable ) -> + autostaking_enable, + force_activation, + ns_enable ) -> { global_limit_of_staking_over_baking; edge_of_staking_over_delegation; @@ -457,14 +465,18 @@ let adaptive_issuance_encoding = adaptive_rewards_params; activation_vote_enable; autostaking_enable; + force_activation; + ns_enable; }) - (obj6 + (obj8 (req "global_limit_of_staking_over_baking" uint8) (req "edge_of_staking_over_delegation" uint8) (req "adaptive_issuance_launch_ema_threshold" int32) (req "adaptive_rewards_params" adaptive_rewards_params_encoding) (req "adaptive_issuance_activation_vote_enable" bool) - (req "autostaking_enable" bool)) + (req "autostaking_enable" bool) + (req "adaptive_issuance_force_activation" bool) + (req "ns_enable" bool)) let issuance_weights_encoding = let open Data_encoding in diff --git a/src/proto_alpha/lib_protocol/constants_parametric_repr.mli b/src/proto_alpha/lib_protocol/constants_parametric_repr.mli index 66a7d0d02728..ff4b3a1b26c2 100644 --- a/src/proto_alpha/lib_protocol/constants_parametric_repr.mli +++ b/src/proto_alpha/lib_protocol/constants_parametric_repr.mli @@ -147,6 +147,12 @@ type adaptive_issuance = { (* If set to true, a stake/unstake/finalize operation will be triggered for all delegate at end of cycle. *) bool; + force_activation : + (* For testing purposes. If set to true, the adaptive issuance feature is + enabled without waiting to reach the launch_ema_threshold.*) + bool; + ns_enable : (* If set to true, enables the NS feature *) + bool; } type issuance_weights = { diff --git a/src/proto_alpha/lib_protocol/constants_storage.ml b/src/proto_alpha/lib_protocol/constants_storage.ml index 87755106dee6..83da05502109 100644 --- a/src/proto_alpha/lib_protocol/constants_storage.ml +++ b/src/proto_alpha/lib_protocol/constants_storage.ml @@ -262,5 +262,10 @@ let adaptive_issuance_activation_vote_enable c = let adaptive_issuance_autostaking_enable c = (adaptive_issuance c).autostaking_enable +let adaptive_issuance_force_activation c = + (adaptive_issuance c).force_activation + +let adaptive_issuance_ns_enable c = (adaptive_issuance c).ns_enable + let direct_ticket_spending_enable c = (Raw_context.constants c).direct_ticket_spending_enable diff --git a/src/proto_alpha/lib_protocol/constants_storage.mli b/src/proto_alpha/lib_protocol/constants_storage.mli index 77fa0186cf98..78d43459778f 100644 --- a/src/proto_alpha/lib_protocol/constants_storage.mli +++ b/src/proto_alpha/lib_protocol/constants_storage.mli @@ -159,4 +159,8 @@ val adaptive_issuance_rewards_params : val adaptive_issuance_autostaking_enable : Raw_context.t -> bool +val adaptive_issuance_force_activation : Raw_context.t -> bool + +val adaptive_issuance_ns_enable : Raw_context.t -> bool + val direct_ticket_spending_enable : Raw_context.t -> bool diff --git a/src/proto_alpha/lib_protocol/raw_context.ml b/src/proto_alpha/lib_protocol/raw_context.ml index 424600b31c3c..6ae22b069247 100644 --- a/src/proto_alpha/lib_protocol/raw_context.ml +++ b/src/proto_alpha/lib_protocol/raw_context.ml @@ -1165,6 +1165,8 @@ let prepare_first_block ~level ~timestamp _chain_id ctxt = activation_vote_enable = c.adaptive_issuance.activation_vote_enable; autostaking_enable = c.adaptive_issuance.autostaking_enable; + force_activation = false; + ns_enable = false; } in let issuance_weights : Constants_parametric_repr.issuance_weights = diff --git a/tezt/tests/expected/RPC_test.ml/Alpha- (mode client) RPC regression tests- misc_protocol.out b/tezt/tests/expected/RPC_test.ml/Alpha- (mode client) RPC regression tests- misc_protocol.out index 7c82c383618a..87840fd9f9e3 100644 --- a/tezt/tests/expected/RPC_test.ml/Alpha- (mode client) RPC regression tests- misc_protocol.out +++ b/tezt/tests/expected/RPC_test.ml/Alpha- (mode client) RPC regression tests- misc_protocol.out @@ -74,7 +74,8 @@ "center_dz": { "numerator": "1", "denominator": "2" }, "radius_dz": { "numerator": "1", "denominator": "50" } }, "adaptive_issuance_activation_vote_enable": false, - "autostaking_enable": true, "direct_ticket_spending_enable": false } + "autostaking_enable": true, "adaptive_issuance_force_activation": false, + "ns_enable": false, "direct_ticket_spending_enable": false } ./octez-client rpc get /chains/main/blocks/head/helpers/baking_rights [ { "level": 2, "delegate": "[PUBLIC_KEY_HASH]", diff --git a/tezt/tests/expected/RPC_test.ml/Alpha- (mode light) RPC regression tests- misc_protocol.out b/tezt/tests/expected/RPC_test.ml/Alpha- (mode light) RPC regression tests- misc_protocol.out index 456ddf0b760b..703252316c0e 100644 --- a/tezt/tests/expected/RPC_test.ml/Alpha- (mode light) RPC regression tests- misc_protocol.out +++ b/tezt/tests/expected/RPC_test.ml/Alpha- (mode light) RPC regression tests- misc_protocol.out @@ -74,7 +74,8 @@ "center_dz": { "numerator": "1", "denominator": "2" }, "radius_dz": { "numerator": "1", "denominator": "50" } }, "adaptive_issuance_activation_vote_enable": false, - "autostaking_enable": true, "direct_ticket_spending_enable": false } + "autostaking_enable": true, "adaptive_issuance_force_activation": false, + "ns_enable": false, "direct_ticket_spending_enable": false } ./octez-client --mode light rpc get /chains/main/blocks/head/helpers/baking_rights [ { "level": 2, "delegate": "[PUBLIC_KEY_HASH]", diff --git a/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy) RPC regression tests- misc_protocol.out b/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy) RPC regression tests- misc_protocol.out index 75146fa3223a..0c5903016d5c 100644 --- a/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy) RPC regression tests- misc_protocol.out +++ b/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy) RPC regression tests- misc_protocol.out @@ -74,7 +74,8 @@ "center_dz": { "numerator": "1", "denominator": "2" }, "radius_dz": { "numerator": "1", "denominator": "50" } }, "adaptive_issuance_activation_vote_enable": false, - "autostaking_enable": true, "direct_ticket_spending_enable": false } + "autostaking_enable": true, "adaptive_issuance_force_activation": false, + "ns_enable": false, "direct_ticket_spending_enable": false } ./octez-client --mode proxy rpc get /chains/main/blocks/head/helpers/baking_rights [ { "level": 2, "delegate": "[PUBLIC_KEY_HASH]", diff --git a/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy_server_data_dir) RPC regression tests- misc_protocol.out b/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy_server_data_dir) RPC regression tests- misc_protocol.out index 3ee7bcdc9144..5ff3277de7e5 100644 --- a/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy_server_data_dir) RPC regression tests- misc_protocol.out +++ b/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy_server_data_dir) RPC regression tests- misc_protocol.out @@ -74,7 +74,8 @@ "center_dz": { "numerator": "1", "denominator": "2" }, "radius_dz": { "numerator": "1", "denominator": "50" } }, "adaptive_issuance_activation_vote_enable": false, - "autostaking_enable": true, "direct_ticket_spending_enable": false } + "autostaking_enable": true, "adaptive_issuance_force_activation": false, + "ns_enable": false, "direct_ticket_spending_enable": false } ./octez-client rpc get /chains/main/blocks/head/helpers/baking_rights [ { "level": 3, "delegate": "[PUBLIC_KEY_HASH]", diff --git a/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy_server_rpc) RPC regression tests- misc_protocol.out b/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy_server_rpc) RPC regression tests- misc_protocol.out index 3ee7bcdc9144..5ff3277de7e5 100644 --- a/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy_server_rpc) RPC regression tests- misc_protocol.out +++ b/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy_server_rpc) RPC regression tests- misc_protocol.out @@ -74,7 +74,8 @@ "center_dz": { "numerator": "1", "denominator": "2" }, "radius_dz": { "numerator": "1", "denominator": "50" } }, "adaptive_issuance_activation_vote_enable": false, - "autostaking_enable": true, "direct_ticket_spending_enable": false } + "autostaking_enable": true, "adaptive_issuance_force_activation": false, + "ns_enable": false, "direct_ticket_spending_enable": false } ./octez-client rpc get /chains/main/blocks/head/helpers/baking_rights [ { "level": 3, "delegate": "[PUBLIC_KEY_HASH]", -- GitLab From 5ec74ac0a9ff6ad688be016178f8bc844062d7b8 Mon Sep 17 00:00:00 2001 From: Mathias Bourgoin Date: Thu, 18 Jan 2024 10:51:41 +0100 Subject: [PATCH 2/3] Proto/AI: Use force_activation feature flag to bypass ema vote check Will be used in tests in following commits --- src/proto_alpha/lib_protocol/init_storage.ml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/proto_alpha/lib_protocol/init_storage.ml b/src/proto_alpha/lib_protocol/init_storage.ml index e5958cf63513..8d5ad4dff4f4 100644 --- a/src/proto_alpha/lib_protocol/init_storage.ml +++ b/src/proto_alpha/lib_protocol/init_storage.ml @@ -209,7 +209,14 @@ let prepare_first_block chain_id ctxt ~typecheck_smart_contract let*! ctxt = Storage.Pending_migration.Balance_updates.add ctxt balance_updates in - return ctxt + if Constants_storage.adaptive_issuance_force_activation ctxt then + let ctxt = Raw_context.set_adaptive_issuance_enable ctxt in + let* ctxt = + let current_cycle = (Level_storage.current ctxt).cycle in + Storage.Adaptive_issuance.Activation.update ctxt (Some current_cycle) + in + return ctxt + else return ctxt let prepare ctxt ~level ~predecessor_timestamp ~timestamp = let open Lwt_result_syntax in -- GitLab From 525c79c6b71d8b63a173ce69220f8dd3dcb36fad Mon Sep 17 00:00:00 2001 From: Mathias Bourgoin Date: Thu, 18 Jan 2024 10:52:15 +0100 Subject: [PATCH 3/3] Proto/AI/Tests: Add a simple test with force_activation feature flag set --- .../test_adaptive_issuance_launch.ml | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/src/proto_alpha/lib_protocol/test/integration/test_adaptive_issuance_launch.ml b/src/proto_alpha/lib_protocol/test/integration/test_adaptive_issuance_launch.ml index cbb75e645266..9af0c2c7c22f 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_adaptive_issuance_launch.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_adaptive_issuance_launch.ml @@ -461,8 +461,96 @@ let test_does_not_launch_without_feature_flag threshold vote_duration () = in return_unit +(* Test that with force_activation feature flag set, the feature activates + without waiting for the activation vote *) +let test_launch_without_vote () = + let open Lwt_result_wrap_syntax in + (* Initialize the state with a single delegate. *) + let constants = + let default_constants = Default_parameters.constants_test in + let issuance_weights = + { + Default_parameters.constants_test.issuance_weights with + base_total_issued_per_minute = Tez.zero; + } + in + let adaptive_issuance = + {default_constants.adaptive_issuance with force_activation = true} + in + let consensus_threshold = 0 in + { + default_constants with + consensus_threshold; + issuance_weights; + adaptive_issuance; + } + in + let* block, delegate = Context.init_with_constants1 constants in + let delegate_pkh = Context.Contract.pkh delegate in + let* block = Block.bake block in + + (* AI should be activated and launch cycle is current cycle (0) *) + let* launch_cycle_opt = + Context.get_adaptive_issuance_launch_cycle (B block) + in + let* launch_cycle = Assert.get_some ~loc:__LOC__ launch_cycle_opt in + let* () = Assert.equal_int32 ~loc:__LOC__ (Cycle.to_int32 launch_cycle) 0l in + + let* () = + assert_total_frozen_stake + ~loc:__LOC__ + block + (Protocol.Alpha_context.Tez.of_mutez_exn 200_000_000_000L) + in + (* feature flag is set, AI should be active, let's use the stake function to check *) + let* operation = + stake + (B block) + delegate + (Protocol.Alpha_context.Tez.of_mutez_exn 180_000_000_000L) + in + let* block = Block.bake ~operation block in + (* Wait until total frozen stake is updated *) + let start_cycle = Block.current_cycle block in + let* block = + Block.bake_while + ~invariant:(fun block -> + assert_total_frozen_stake + ~loc:__LOC__ + block + (Protocol.Alpha_context.Tez.of_mutez_exn 200_000_000_000L)) + (fun block -> + let current_cycle = Block.current_cycle block in + Protocol.Alpha_context.Cycle.( + current_cycle <= add start_cycle constants.preserved_cycles)) + block + in + let* block = Block.bake block in + + let* () = + assert_total_frozen_stake + ~loc:__LOC__ + block + (Protocol.Alpha_context.Tez.of_mutez_exn 380_000_000_000L) + in + let* () = + assert_voting_power + ~loc:__LOC__ + block + delegate_pkh + ~ai_enabled:true + ~expected_staked:380_000_000_000L + ~expected_delegated:0L + ~expected_ext_staked:0L + in + return_unit + let tests = [ + Tztest.tztest + "Launch with force_activation feature flag set activates AI immediately" + `Quick + test_launch_without_vote; Tztest.tztest "the EMA reaches the vote threshold at the expected level and adaptive \ issuance launches (very low threshold, vote enabled)" -- GitLab