diff --git a/src/lib_shell_services/history_mode.ml b/src/lib_shell_services/history_mode.ml index 90254796cc0498c38464048c96f16c9c32fd1b5c..ac87314677ffa65c0e7bc7fed17178584c537c18 100644 --- a/src/lib_shell_services/history_mode.ml +++ b/src/lib_shell_services/history_mode.ml @@ -42,10 +42,9 @@ type t = | Rolling of additional_cycles option (* The default_offset value defines a window of stored cycles which is - suitable for baking services. It currently corresponds to 6 as we + suitable for baking services. It currently corresponds to 2 as we store 1 cycle below the last preserved block level of the current - head, which is set to [blocks_preservation_cycles] cycles in the - past. + head, which is set to [blocks_preservation_cycles] cycles in the past. TODO: https://gitlab.com/tezos/tezos/-/issues/1406 As this value is potentially both network and protocol specific, it could be lifted as a protocol value or an hardcoded node diff --git a/src/proto_alpha/lib_protocol/adaptive_issuance_services.ml b/src/proto_alpha/lib_protocol/adaptive_issuance_services.ml index 1386e85cdf8680a0d8c4e72ac3b563d7a28f11d2..7477b06ace46c83573bd73487453190a830c04ea 100644 --- a/src/proto_alpha/lib_protocol/adaptive_issuance_services.ml +++ b/src/proto_alpha/lib_protocol/adaptive_issuance_services.ml @@ -220,7 +220,7 @@ let collect_expected_rewards ~ctxt = } else (* This coeff is correct only when applied to Cycle lesser than - [preserved_cycles] after the current context, otherwise the coeff will + [issuance_modification_delay] after the current context, otherwise the coeff will not be set and thus we get the default values. *) let open Delegate.Rewards.For_RPC in let* coeff = get_reward_coeff ctxt ~cycle in @@ -261,7 +261,9 @@ let collect_expected_rewards ~ctxt = } in let queried_cycles = - Cycle.(ctxt_cycle ---> add ctxt_cycle csts.preserved_cycles) + Cycle.( + ctxt_cycle + ---> add ctxt_cycle (Constants.issuance_modification_delay ctxt)) in List.map_es reward_of_cycle queried_cycles diff --git a/src/proto_alpha/lib_protocol/adaptive_issuance_storage.ml b/src/proto_alpha/lib_protocol/adaptive_issuance_storage.ml index c9968fac1f91e023ba2073b269419e378dc1f414..7644e6df755087c12a318ba5a160a558bc0f12fd 100644 --- a/src/proto_alpha/lib_protocol/adaptive_issuance_storage.ml +++ b/src/proto_alpha/lib_protocol/adaptive_issuance_storage.ml @@ -66,10 +66,9 @@ let check_determined_cycle ctxt cycle = let ai_enable = Constants_storage.adaptive_issuance_enable ctxt in if ai_enable then let ctxt_cycle = (Raw_context.current_level ctxt).cycle in - let preserved_cycles = Constants_storage.preserved_cycles ctxt in + let cycles_delay = Constants_storage.issuance_modification_delay ctxt in fail_unless - Cycle_repr.( - ctxt_cycle <= cycle && cycle <= add ctxt_cycle preserved_cycles) + Cycle_repr.(ctxt_cycle <= cycle && cycle <= add ctxt_cycle cycles_delay) (Undetermined_issuance_coeff_for_cycle cycle) else return_unit @@ -242,8 +241,10 @@ let compute_and_store_reward_coeff_at_cycle_end ctxt ~new_cycle = let reward_params = Constants_storage.adaptive_issuance_rewards_params ctxt in - let preserved = Constants_storage.preserved_cycles ctxt in - let for_cycle = Cycle_repr.add new_cycle preserved in + let modification_delay = + Constants_storage.issuance_modification_delay ctxt + in + let for_cycle = Cycle_repr.add new_cycle modification_delay in let before_for_cycle = Cycle_repr.pred for_cycle in let* total_supply = Storage.Contract.Total_supply.get ctxt in let* total_stake = Stake_storage.get_total_active_stake ctxt for_cycle in @@ -369,9 +370,7 @@ let update_ema ctxt ~vote = | None -> (* set the feature to activate in a few cycles *) let current_cycle = (Level_storage.current ctxt).cycle in - let delay = - 1 + preserved_cycles ctxt + Constants_repr.max_slashing_period - in + let delay = adaptive_issuance_activation_delay ctxt in let cycle = Cycle_repr.add current_cycle delay in let+ ctxt = activate ctxt ~cycle in (ctxt, Some cycle) diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index abe5e3d38406c64f6df7038b1e64f98d4fa65ead..e4b23ce9463a0f24ef2a30d3c82dd9fc73a47c21 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -965,6 +965,10 @@ module Constants : sig val delegate_parameters_activation_delay : context -> int + val slashable_deposits_period : context -> int + + val issuance_modification_delay : context -> int + val blocks_per_cycle : context -> int32 val blocks_per_commitment : context -> int32 diff --git a/src/proto_alpha/lib_protocol/constants_storage.ml b/src/proto_alpha/lib_protocol/constants_storage.ml index 83da05502109b5754af672047e905e45de24ead0..4feb06a9a8d154bd32464acb3a192fdab8504051 100644 --- a/src/proto_alpha/lib_protocol/constants_storage.ml +++ b/src/proto_alpha/lib_protocol/constants_storage.ml @@ -40,6 +40,42 @@ let delegate_parameters_activation_delay c = let constants = Raw_context.constants c in constants.delegate_parameters_activation_delay +(** Issuance modification delay: + number of cycles after which the issuance rate -- computed from current stake + over total supply -- will be used. + + We use consensus_rights_delay so that the issuance rate in one cycle + corresponds to the "active" staking rights. +*) +let issuance_modification_delay c = + let constants = Raw_context.constants c in + constants.consensus_rights_delay + +(** Adaptive Issuance activation delay: + After the e.m.a. of AI votes reaches the threshold, we wait for this delay + before effectively activating AI. +*) +let adaptive_issuance_activation_delay c = + let constants = Raw_context.constants c in + 1 + constants.consensus_rights_delay + Constants_repr.max_slashing_period + +(** Tolerated inactivity period for delegates before being deactivated. *) +let tolerated_inactivity_period c = + let constants = Raw_context.constants c in + 1 + constants.consensus_rights_delay + +(** Delay between consensus key declaration by the delegate and the cycle where + it has to be used to sign on behalf of the delegate. *) +let consensus_key_activation_delay c = + let constants = Raw_context.constants c in + constants.consensus_rights_delay + +(** Number of cycles during which a misbehavior of the delegate will induce a + slashing of the funds that are currently in its frozen deposits. *) +let slashable_deposits_period c = + let constants = Raw_context.constants c in + constants.consensus_rights_delay + let blocks_per_cycle c = let constants = Raw_context.constants c in constants.blocks_per_cycle diff --git a/src/proto_alpha/lib_protocol/constants_storage.mli b/src/proto_alpha/lib_protocol/constants_storage.mli index f254c88e80023a789ec4d01e3755d338c4b6ee49..0c9ad1857919a7e9bb08776ed06841c786cd1221 100644 --- a/src/proto_alpha/lib_protocol/constants_storage.mli +++ b/src/proto_alpha/lib_protocol/constants_storage.mli @@ -164,3 +164,26 @@ 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 + +(** The following accessors are not actual parameters, but constants that + derive from the protocol parameter. *) + +(** Delay, in cycles, before the current state of the stake impacts the + issuance rate.*) +val issuance_modification_delay : Raw_context.t -> int + +(** Delay, in cycles, before activation of AI after the voting EMA threshold is + reached *) +val adaptive_issuance_activation_delay : Raw_context.t -> int + +(** Tolerated period of inactivity, in cycles, before a delegate is + deactivated *) +val tolerated_inactivity_period : Raw_context.t -> int + +(** Delay before the activation of a consensus key, in cycles *) +val consensus_key_activation_delay : Raw_context.t -> int + +(** Number of cycles during which a misbehavior of a delegate will induce a + slashing of the funds that are currently in its frozen deposit. *) + +val slashable_deposits_period : Raw_context.t -> int diff --git a/src/proto_alpha/lib_protocol/delegate_activation_storage.ml b/src/proto_alpha/lib_protocol/delegate_activation_storage.ml index b34e822262d074126292b45972b108f25bdcc67d..357d339d144a815618537265bc17428bc2e69221 100644 --- a/src/proto_alpha/lib_protocol/delegate_activation_storage.ml +++ b/src/proto_alpha/lib_protocol/delegate_activation_storage.ml @@ -59,13 +59,14 @@ let set_active ctxt delegate = let open Lwt_result_syntax in let* inactive = is_inactive ctxt delegate in let current_cycle = (Raw_context.current_level ctxt).cycle in + let tolerance = Constants_storage.tolerated_inactivity_period ctxt in let preserved_cycles = Constants_storage.preserved_cycles ctxt in (* We allow a number of cycles before a delegate is deactivated as follows: - - if the delegate is active, we give it at least `1 + preserved_cycles` + - if the delegate is active, we give it at least `tolerance` cycles after the current cycle before to be deactivated. - if the delegate is new or inactive, we give it additionally `preserved_cycles` because the delegate needs this number of cycles to - receive rights, so `1 + 2 * preserved_cycles` in total. *) + receive rights, so `tolerance + preserved_cycles` in total. *) let delegate_contract = Contract_repr.Implicit delegate in let* current_last_active_cycle = Storage.Contract.Delegate_last_cycle_before_deactivation.find @@ -74,10 +75,10 @@ let set_active ctxt delegate = in let last_active_cycle = match current_last_active_cycle with - | None -> Cycle_repr.add current_cycle (1 + (2 * preserved_cycles)) + | None -> Cycle_repr.add current_cycle (tolerance + preserved_cycles) | Some current_last_active_cycle -> let delay = - if inactive then 1 + (2 * preserved_cycles) else 1 + preserved_cycles + if inactive then tolerance + preserved_cycles else tolerance in let updated = Cycle_repr.add current_cycle delay in Cycle_repr.max current_last_active_cycle updated diff --git a/src/proto_alpha/lib_protocol/delegate_consensus_key.ml b/src/proto_alpha/lib_protocol/delegate_consensus_key.ml index 495293c7d76019d4624f9f40cf31b9cda97e1be8..230c091660d3362e1c77655df1a7cb41b2bcd42b 100644 --- a/src/proto_alpha/lib_protocol/delegate_consensus_key.ml +++ b/src/proto_alpha/lib_protocol/delegate_consensus_key.ml @@ -151,8 +151,10 @@ let raw_pending_updates ctxt ?up_to_cycle delegate = let last_cycle = match up_to_cycle with | None -> - let preserved_cycles = Constants_storage.preserved_cycles ctxt in - Cycle_repr.add first_cycle preserved_cycles + let cycles_delay = + Constants_storage.consensus_key_activation_delay ctxt + in + Cycle_repr.add first_cycle cycles_delay | Some cycle -> cycle in Cycle_repr.(first_cycle ---> last_cycle) @@ -194,8 +196,8 @@ let register_update ctxt delegate pk = let open Lwt_result_syntax in let update_cycle = let current_level = Raw_context.current_level ctxt in - let preserved_cycles = Constants_storage.preserved_cycles ctxt in - Cycle_repr.add current_level.cycle (preserved_cycles + 1) + let cycles_delay = Constants_storage.consensus_key_activation_delay ctxt in + Cycle_repr.add current_level.cycle (cycles_delay + 1) in let* () = let* first_active_cycle, active_pubkey = diff --git a/src/proto_alpha/lib_protocol/delegate_services.ml b/src/proto_alpha/lib_protocol/delegate_services.ml index 486f90e2543388178e3e7a0acf9f7833d53f75f9..ad13cc74a00576500d2954aab5ef559c4d32f485 100644 --- a/src/proto_alpha/lib_protocol/delegate_services.ml +++ b/src/proto_alpha/lib_protocol/delegate_services.ml @@ -629,12 +629,12 @@ let register () = register1 ~chunked:false S.unstaked_frozen_deposits (fun ctxt pkh () () -> let* () = check_delegate_registered ctxt pkh in let ctxt_cycle = (Alpha_context.Level.current ctxt).cycle in - let csts = (Constants.all ctxt).parametric in let last_unslashable_cycle = Option.value ~default:Cycle.root @@ Cycle.sub ctxt_cycle - (csts.preserved_cycles + Constants_repr.max_slashing_period) + (Constants.slashable_deposits_period ctxt + + Constants_repr.max_slashing_period) in let cycles = Cycle.(last_unslashable_cycle ---> ctxt_cycle) in let* requests = diff --git a/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml b/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml index 2c6300cb622d93325b75b63c3205b760cf272b1b..f14b5231cbbab34eff97b4b6290351cc45576dc1 100644 --- a/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml +++ b/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml @@ -146,7 +146,9 @@ let apply_and_clear_denunciations ctxt = | None -> current_cycle | Some previous_cycle -> previous_cycle in - let preserved_cycles = Constants_storage.preserved_cycles ctxt in + let slashable_deposits_period = + Constants_storage.slashable_deposits_period ctxt + in let global_limit_of_staking_over_baking_plus_two = let global_limit_of_staking_over_baking = Constants_storage.adaptive_issuance_global_limit_of_staking_over_baking @@ -283,7 +285,7 @@ let apply_and_clear_denunciations ctxt = in let* to_burn, to_reward = let oldest_slashable_cycle = - Cycle_repr.sub misbehaviour_cycle preserved_cycles + Cycle_repr.sub misbehaviour_cycle slashable_deposits_period |> Option.value ~default:Cycle_repr.root in let slashable_cycles = diff --git a/src/proto_alpha/lib_protocol/staking.ml b/src/proto_alpha/lib_protocol/staking.ml index 8e7800a6959d26f3f45b1c0216957c75aa1a07d4..e637b4f9d55846a7d024aa87298b06753a940bfe 100644 --- a/src/proto_alpha/lib_protocol/staking.ml +++ b/src/proto_alpha/lib_protocol/staking.ml @@ -142,9 +142,11 @@ let can_stake_from_unstake ctxt ~for_next_cycle_use_only_after_slashing if for_next_cycle_use_only_after_slashing then Cycle_repr.succ current_cycle else current_cycle in - let preserved_cycles = Constants_storage.preserved_cycles ctxt in + let slashable_deposits_period = + Constants_storage.slashable_deposits_period ctxt + in let oldest_slashable_cycle = - Cycle_repr.sub current_cycle (preserved_cycles + 1) + Cycle_repr.sub current_cycle (slashable_deposits_period + 1) |> Option.value ~default:Cycle_repr.root in return diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_attestation.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_attestation.ml index 7d7e4c3f36412bf42bb52810105dcdef71d614d7..12207188ba2cccd2b209ba00a0de48f3f82c9755 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_attestation.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_double_attestation.ml @@ -711,6 +711,7 @@ let test_freeze_more_with_low_balance = consensus_threshold = 0; origination_size = 0; preserved_cycles = 5; + consensus_rights_delay = 5; percentage_of_frozen_deposits_slashed_per_double_attestation = (* enforce that percentage is 50% in the test's params. *) Percentage.p50; 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 3fceadd3e376e6c0f4855938f0e849658ada5de0..2e90fbdf462f7e11b9ba0c6a3132423d0ba43168 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 @@ -121,8 +121,10 @@ let test_consensus_key_storage () = let* ctxt, del1, del2 = create () in let a1 = Account.new_account () in let a2 = Account.new_account () in - let preserved_cycles = Constants_storage.preserved_cycles ctxt in - let* () = Assert.equal_int ~loc:__LOC__ preserved_cycles 3 in + let consensus_key_activation_delay = + Constants_storage.consensus_key_activation_delay ctxt + in + let* () = Assert.equal_int ~loc:__LOC__ consensus_key_activation_delay 3 in let* () = let* active_pkh = Consensus_key.active_key ctxt del1.pkh in Assert.equal_pkh ~__LOC__ active_pkh.consensus_pkh del1.pkh diff --git a/src/proto_alpha/lib_protocol/unstake_requests_storage.ml b/src/proto_alpha/lib_protocol/unstake_requests_storage.ml index f4309bf55b388ed01d76dab2a5af24cf51d36c3c..ec1e78f7fdf94af2baeff651df968d0f575ceb74 100644 --- a/src/proto_alpha/lib_protocol/unstake_requests_storage.ml +++ b/src/proto_alpha/lib_protocol/unstake_requests_storage.ml @@ -68,9 +68,12 @@ let prepared_finalize_unstake_encoding : (req "finalizable" finalizable_encoding) (req "unfinalizable" stored_requests_encoding)) -let apply_slashes ~preserved_cycles slashing_history ~from_cycle amount = +let apply_slashes ~slashable_deposits_period slashing_history ~from_cycle amount + = let first_cycle_to_apply_slash = from_cycle in - let last_cycle_to_apply_slash = Cycle_repr.add from_cycle preserved_cycles in + let last_cycle_to_apply_slash = + Cycle_repr.add from_cycle slashable_deposits_period + in (* [slashing_history] is sorted so slashings always happen in the same order. *) List.fold_left (fun remain (slashing_cycle, slashing_percentage) -> @@ -91,9 +94,13 @@ let apply_slashes ~preserved_cycles slashing_history ~from_cycle amount = let prepare_finalize_unstake ctxt ~for_next_cycle_use_only_after_slashing contract = let open Lwt_result_syntax in - let preserved_cycles = Constants_storage.preserved_cycles ctxt in + let slashable_deposits_period = + Constants_storage.slashable_deposits_period ctxt + in let max_slashing_period = Constants_repr.max_slashing_period in - let preserved_plus_slashing = preserved_cycles + max_slashing_period in + let slashable_plus_denunciation_delay = + slashable_deposits_period + max_slashing_period + in let current_cycle = (Raw_context.current_level ctxt).cycle in let current_cycle = if for_next_cycle_use_only_after_slashing then Cycle_repr.succ current_cycle @@ -103,7 +110,7 @@ let prepare_finalize_unstake ctxt ~for_next_cycle_use_only_after_slashing match requests_opt with | None | Some {delegate = _; requests = []} -> return_none | Some {delegate; requests} -> ( - match Cycle_repr.sub current_cycle preserved_plus_slashing with + match Cycle_repr.sub current_cycle slashable_plus_denunciation_delay with | None (* no finalizable cycle *) -> return_some {finalizable = []; unfinalizable = {delegate; requests}} | Some greatest_finalizable_cycle -> @@ -122,7 +129,7 @@ let prepare_finalize_unstake ctxt ~for_next_cycle_use_only_after_slashing if Cycle_repr.(request_cycle <= greatest_finalizable_cycle) then let new_amount = apply_slashes - ~preserved_cycles + ~slashable_deposits_period slashing_history ~from_cycle:request_cycle request_amount @@ -162,7 +169,9 @@ module For_RPC = struct let current_level = Raw_context.current_level ctxt in let cycle_eras = Raw_context.cycle_eras ctxt in let is_last_of_cycle = Level_repr.last_of_cycle ~cycle_eras current_level in - let preserved_cycles = Constants_storage.preserved_cycles ctxt in + let slashable_deposits_period = + Constants_storage.slashable_deposits_period ctxt + in let* slashing_history_opt = Storage.Contract.Slashed_deposits.find ctxt @@ -239,7 +248,7 @@ module For_RPC = struct (fun (request_cycle, request_amount) -> let new_amount = apply_slashes - ~preserved_cycles + ~slashable_deposits_period slashing_history ~from_cycle:request_cycle request_amount diff --git a/src/proto_alpha/lib_protocol/unstaked_frozen_deposits_storage.ml b/src/proto_alpha/lib_protocol/unstaked_frozen_deposits_storage.ml index 2018e3c8cdb48edef85d7fe43b0179008bae4375..782d27216aded7fa1c6e1289c8f6e0d7b57d6402 100644 --- a/src/proto_alpha/lib_protocol/unstaked_frozen_deposits_storage.ml +++ b/src/proto_alpha/lib_protocol/unstaked_frozen_deposits_storage.ml @@ -25,9 +25,11 @@ let current_unslashable_cycle ctxt = let cycle = (Raw_context.current_level ctxt).cycle in - let preserved_cycles = Constants_storage.preserved_cycles ctxt in + let slashable_deposits_period = + Constants_storage.slashable_deposits_period ctxt + in let max_slashing_period = Constants_repr.max_slashing_period in - Cycle_repr.sub cycle (preserved_cycles + max_slashing_period) + Cycle_repr.sub cycle (slashable_deposits_period + max_slashing_period) let get_all ctxt contract = let open Lwt_result_syntax in diff --git a/tezt/tests/consensus_key.ml b/tezt/tests/consensus_key.ml index 59a377105c5a47c2f95ff5e3d21a6bb7447fabe4..e6cc9e6d41f27abaea4e5fc29691522f37305bb2 100644 --- a/tezt/tests/consensus_key.ml +++ b/tezt/tests/consensus_key.ml @@ -96,11 +96,16 @@ let test_update_consensus_key = let parameters = (* we update paramaters for faster testing: no need to wait 5 cycles for the consensus key to activate. *) - [ - (["blocks_per_cycle"], `Int blocks_per_cycle); - (["nonce_revelation_threshold"], `Int 2); - (["preserved_cycles"], `Int preserved_cycles); - ] + let p = + [ + (["blocks_per_cycle"], `Int blocks_per_cycle); + (["nonce_revelation_threshold"], `Int 2); + (["preserved_cycles"], `Int preserved_cycles); + ] + in + if Protocol.number protocol > Protocol.number Protocol.Oxford then + (["consensus_rights_delay"], `Int preserved_cycles) :: p + else p in let* parameter_file = Protocol.write_parameter_file ~base:(Right (protocol, None)) parameters @@ -625,11 +630,16 @@ let register ?(regression = true) title test = let parameters = (* we update paramaters for faster testing: no need to wait 5 cycles for the consensus key to activate. *) - [ - (["blocks_per_cycle"], `Int blocks_per_cycle); - (["nonce_revelation_threshold"], `Int 2); - (["preserved_cycles"], `Int preserved_cycles); - ] + let p = + [ + (["blocks_per_cycle"], `Int blocks_per_cycle); + (["nonce_revelation_threshold"], `Int 2); + (["preserved_cycles"], `Int preserved_cycles); + ] + in + if Protocol.number protocol > Protocol.number Protocol.Oxford then + (["consensus_rights_delay"], `Int preserved_cycles) :: p + else p in let* parameter_file = Protocol.write_parameter_file ~base:(Right (protocol, None)) parameters