diff --git a/src/proto_alpha/lib_protocol/adaptive_inflation_storage.ml b/src/proto_alpha/lib_protocol/adaptive_inflation_storage.ml index e29d402c7358e991f871ed3fedd9097863ed8844..b9b30eec684c94c6f2c16011dd186512781e88dd 100644 --- a/src/proto_alpha/lib_protocol/adaptive_inflation_storage.ml +++ b/src/proto_alpha/lib_protocol/adaptive_inflation_storage.ml @@ -42,8 +42,46 @@ let ratio_to_bonus q = Q.(q * of_int64 bonus_unit |> to_int64) let max_bonus = Int64.div bonus_unit 20L (* = 5% *) +type error += Undetermined_inflation_coeff_for_cycle of Cycle_repr.t + +let () = + let open Data_encoding in + let undetermined_inflation_coeff_for_cycle_description = + "Inflation coefficient is only determined for the current cycle and the \ + next [preserved_cycles] cycles to come. Requested cycle is not in this \ + window." + in + register_error_kind + `Permanent + ~id:"undetermined_inflation_coeff_for_cycle" + ~title:"Undetermined inflation coeff for cycle" + ~description:undetermined_inflation_coeff_for_cycle_description + ~pp:(fun ppf cycle -> + Format.fprintf + ppf + "%s (cycle %a)" + undetermined_inflation_coeff_for_cycle_description + Cycle_repr.pp + cycle) + (obj1 (req "Undetermined_inflation_coeff_for_cycle" Cycle_repr.encoding)) + (function + | Undetermined_inflation_coeff_for_cycle cycle -> Some cycle | _ -> None) + (fun cycle -> Undetermined_inflation_coeff_for_cycle cycle) + +let check_determined_cycle ctxt cycle = + let ai_enable = Constants_storage.adaptive_inflation_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 + fail_unless + Cycle_repr.( + ctxt_cycle <= cycle && cycle <= add ctxt_cycle preserved_cycles) + (Undetermined_inflation_coeff_for_cycle cycle) + else return_unit + let get_reward_coeff ctxt ~cycle = let open Lwt_result_syntax in + let* () = check_determined_cycle ctxt cycle in let ai_enable = Constants_storage.adaptive_inflation_enable ctxt in if ai_enable then (* Even if AI is enabled, the storage can be empty: this is the case for diff --git a/src/proto_alpha/lib_protocol/adaptive_inflation_storage.mli b/src/proto_alpha/lib_protocol/adaptive_inflation_storage.mli index 6702480230a53e03097d89fda6c7ce0d115b94c8..82116b087501508ad19c62039c1888ae0f8db109 100644 --- a/src/proto_alpha/lib_protocol/adaptive_inflation_storage.mli +++ b/src/proto_alpha/lib_protocol/adaptive_inflation_storage.mli @@ -68,10 +68,11 @@ val launch_cycle : Raw_context.t -> Cycle_repr.t option tzresult Lwt.t module For_RPC : sig (** [get_reward_coeff ctxt cycle] reads the reward coeff for the given cycle from the storage. - Returns [Q.one] if the given cycle is not between [current_cycle] and + + Fails if the given cycle is not between [current_cycle] and [current_cycle + preserved_cycles]. - If adaptive inflation has not been activated, or has been activated and the - given cycle is less than [preserved_cycles] after the activation cycle, + + If adaptive inflation has not been activated, then this function returns [Q.one]. Used only for RPCs. To get the actual rewards, use [Delegate_rewards]. *) val get_reward_coeff :