package tezos-protocol-alpha

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file adaptive_issuance_storage.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2023 Nomadic Labs, <contact@nomadic-labs.com>               *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

(* Default reward coefficient when AI is not in effect, chosen so that
   rewards * coeff = rewards *)
let default_reward = Q.one

(* Default bonus value *)
let default_bonus = 0L

(* Order of magnitude of the total supply in mutez
   Approximately 2^50 *)
let bonus_unit = 1_000_000_000_000_000L

let ratio_to_bonus q = Q.(q * of_int64 bonus_unit |> to_int64)

type error += Undetermined_issuance_coeff_for_cycle of Cycle_repr.t

let () =
  let open Data_encoding in
  let undetermined_issuance_coeff_for_cycle_description =
    "Issuance 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_issuance_coeff_for_cycle"
    ~title:"Undetermined issuance coeff for cycle"
    ~description:undetermined_issuance_coeff_for_cycle_description
    ~pp:(fun ppf cycle ->
      Format.fprintf
        ppf
        "%s (cycle %a)"
        undetermined_issuance_coeff_for_cycle_description
        Cycle_repr.pp
        cycle)
    (obj1 (req "Undetermined_issuance_coeff_for_cycle" Cycle_repr.encoding))
    (function
      | Undetermined_issuance_coeff_for_cycle cycle -> Some cycle | _ -> None)
    (fun cycle -> Undetermined_issuance_coeff_for_cycle cycle)

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
    fail_unless
      Cycle_repr.(
        ctxt_cycle <= cycle && cycle <= add ctxt_cycle preserved_cycles)
      (Undetermined_issuance_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_issuance_enable ctxt in
  if ai_enable then
    (* Even if AI is enabled, the storage can be empty: this is the case for
       the first 5 cycles after AI is enabled *)
    let* k_opt = Storage.Issuance_coeff.find ctxt cycle in
    return (Option.value ~default:default_reward k_opt)
  else return default_reward

let get_reward_bonus ctxt ~cycle =
  let open Lwt_result_syntax in
  match cycle with
  | None -> return default_bonus
  | Some cycle ->
      let ai_enable = Constants_storage.adaptive_issuance_enable ctxt in
      if ai_enable then
        let* k_opt = Storage.Issuance_bonus.find ctxt cycle in
        return (Option.value ~default:default_bonus k_opt)
      else return default_bonus

let load_reward_coeff ctxt ~cycle =
  let open Lwt_result_syntax in
  let* new_reward = get_reward_coeff ctxt ~cycle in
  let ctxt =
    Raw_context.update_reward_coeff_for_current_cycle ctxt new_reward
  in
  return ctxt

let compute_reward_coeff_ratio =
  let q_1600 = Q.of_int 1600 in
  fun ~stake_ratio ~bonus ~issuance_ratio_max ~issuance_ratio_min ->
    let q_bonus = Q.(div (of_int64 bonus) (of_int64 bonus_unit)) in
    let inv_f = Q.(mul (mul stake_ratio stake_ratio) q_1600) in
    let f = Q.inv inv_f (* f = 1/1600 * (1/x)^2 = yearly issuance rate *) in
    let f = Q.add f q_bonus in
    (* f is truncated so that 0.05% <= f <= 5% *)
    let f = Q.(min f issuance_ratio_max) in
    let f = Q.(max f issuance_ratio_min) in
    f

let compute_bonus ~seconds_per_cycle ~total_supply ~total_frozen_stake
    ~previous_bonus ~reward_params =
  let Constants_parametric_repr.
        {
          issuance_ratio_min;
          issuance_ratio_max;
          max_bonus;
          growth_rate;
          center_dz;
          radius_dz;
        } =
    reward_params
  in
  let q_total_supply = Tez_repr.to_mutez total_supply |> Q.of_int64 in
  let q_total_frozen_stake =
    Tez_repr.to_mutez total_frozen_stake |> Q.of_int64
  in
  let stake_ratio =
    Q.div q_total_frozen_stake q_total_supply (* = portion of frozen stake *)
  in
  let base_reward_coeff_ratio =
    compute_reward_coeff_ratio
      ~stake_ratio
      ~bonus:0L
      ~issuance_ratio_max
      ~issuance_ratio_min
  in
  let base_reward_coeff_dist_to_max =
    ratio_to_bonus Q.(issuance_ratio_max - base_reward_coeff_ratio)
  in
  (* The bonus reward is truncated between [0] and [max_bonus] *)
  (* It is done in a way that the bonus does not increase if the coeff
     would already be above the [reward_pct_max] *)
  let max_bonus = Compare.Int64.min base_reward_coeff_dist_to_max max_bonus in
  (* [dist] is the distance from [stake_ratio] to [48%,52%] *)
  let unsigned_dist =
    Q.(max zero (abs (stake_ratio - center_dz) - radius_dz))
  in
  let dist_q =
    if Compare.Q.(stake_ratio >= center_dz) then Q.neg unsigned_dist
    else unsigned_dist
  in
  let dist = ratio_to_bonus dist_q in
  let new_bonus =
    Int64.(add previous_bonus (mul dist (mul growth_rate seconds_per_cycle)))
  in
  let new_bonus = Compare.Int64.max new_bonus 0L in
  let new_bonus = Compare.Int64.min new_bonus max_bonus in
  new_bonus

let compute_coeff =
  let q_min_per_year = Q.of_int 525600 in
  fun ~base_total_issued_per_minute
      ~total_supply
      ~total_frozen_stake
      ~bonus
      ~reward_params ->
    if Compare.Int64.equal (Tez_repr.to_mutez base_total_issued_per_minute) 0L
    then Q.one
    else
      let Constants_parametric_repr.{issuance_ratio_min; issuance_ratio_max; _}
          =
        reward_params
      in
      let q_total_supply = Tez_repr.to_mutez total_supply |> Q.of_int64 in
      let q_total_frozen_stake =
        Tez_repr.to_mutez total_frozen_stake |> Q.of_int64
      in
      let stake_ratio =
        Q.div
          q_total_frozen_stake
          q_total_supply (* = portion of frozen stake *)
      in
      let q_base_total_issued_per_minute =
        Tez_repr.to_mutez base_total_issued_per_minute |> Q.of_int64
      in
      let f =
        compute_reward_coeff_ratio
          ~stake_ratio
          ~bonus
          ~issuance_ratio_max
          ~issuance_ratio_min
      in
      let f = Q.div f q_min_per_year (* = issuance rate per minute *) in
      let f = Q.mul f q_total_supply (* = issuance per minute *) in
      Q.div f q_base_total_issued_per_minute

let compute_and_store_reward_coeff_at_cycle_end ctxt ~new_cycle =
  let open Lwt_result_syntax in
  let ai_enable = Constants_storage.adaptive_issuance_enable ctxt in
  if not ai_enable then return ctxt
  else
    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 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
    let base_total_issued_per_minute =
      (Constants_storage.issuance_weights ctxt).base_total_issued_per_minute
    in
    let total_frozen_stake = Stake_repr.get_frozen total_stake in
    let* previous_bonus = get_reward_bonus ctxt ~cycle:before_for_cycle in
    let blocks_per_cycle =
      Constants_storage.blocks_per_cycle ctxt |> Int64.of_int32
    in
    let minimal_block_delay =
      Constants_storage.minimal_block_delay ctxt |> Period_repr.to_seconds
    in
    let seconds_per_cycle = Int64.mul blocks_per_cycle minimal_block_delay in
    let bonus =
      compute_bonus
        ~seconds_per_cycle
        ~total_supply
        ~total_frozen_stake
        ~previous_bonus
        ~reward_params
    in
    let coeff =
      compute_coeff
        ~base_total_issued_per_minute
        ~total_supply
        ~total_frozen_stake
        ~bonus
        ~reward_params
    in
    let*! ctxt = Storage.Issuance_bonus.add ctxt for_cycle bonus in
    let*! ctxt = Storage.Issuance_coeff.add ctxt for_cycle coeff in
    return ctxt

let clear_outdated_reward_data ctxt ~new_cycle =
  let open Lwt_syntax in
  match Cycle_repr.sub new_cycle 2 with
  | None -> Lwt.return ctxt
  | Some cycle ->
      let* ctxt = Storage.Issuance_coeff.remove ctxt cycle in
      Storage.Issuance_bonus.remove ctxt cycle

let update_stored_rewards_at_cycle_end ctxt ~new_cycle =
  let open Lwt_result_syntax in
  let* ctxt = compute_and_store_reward_coeff_at_cycle_end ctxt ~new_cycle in
  let*! ctxt = clear_outdated_reward_data ctxt ~new_cycle in
  load_reward_coeff ctxt ~cycle:new_cycle

let load_reward_coeff ctxt =
  load_reward_coeff ctxt ~cycle:(Raw_context.current_level ctxt).cycle

let init ctxt =
  let open Lwt_result_syntax in
  let* ctxt = Storage.Adaptive_issuance.Launch_ema.init ctxt 0l in
  Storage.Adaptive_issuance.Activation.init ctxt None

let activate ctxt ~cycle =
  Storage.Adaptive_issuance.Activation.update ctxt (Some cycle)

let launch_cycle ctxt = Storage.Adaptive_issuance.Activation.get ctxt

let set_adaptive_issuance_enable ctxt =
  let open Lwt_result_syntax in
  let+ enable =
    let+ launch_cycle = launch_cycle ctxt in
    match launch_cycle with
    | None -> false
    | Some launch_cycle ->
        let current_cycle = (Level_storage.current ctxt).cycle in
        Cycle_repr.(current_cycle >= launch_cycle)
  in
  if enable then Raw_context.set_adaptive_issuance_enable ctxt else ctxt

let update_ema ctxt ~vote =
  let open Lwt_result_syntax in
  let* old_ema = Storage.Adaptive_issuance.Launch_ema.get ctxt in
  let* old_ema =
    Per_block_votes_repr.Adaptive_issuance_launch_EMA.of_int32 old_ema
  in
  let new_ema =
    Per_block_votes_repr.compute_new_adaptive_issuance_ema
      ~per_block_vote:vote
      old_ema
  in
  let* ctxt =
    Storage.Adaptive_issuance.Launch_ema.update
      ctxt
      (Per_block_votes_repr.Adaptive_issuance_launch_EMA.to_int32 new_ema)
  in
  let* launch_cycle = launch_cycle ctxt in
  let open Constants_storage in
  let+ ctxt, launch_cycle =
    if
      Per_block_votes_repr.Adaptive_issuance_launch_EMA.(
        new_ema < adaptive_issuance_launch_ema_threshold ctxt)
    then return (ctxt, launch_cycle)
    else
      match launch_cycle with
      | Some _ ->
          (* the feature is already set to launch, do nothing to avoid postponing it. *)
          return (ctxt, launch_cycle)
      | 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 + max_slashing_period ctxt in
          let cycle = Cycle_repr.add current_cycle delay in
          let+ ctxt = activate ctxt ~cycle in
          (ctxt, Some cycle)
  in
  (ctxt, launch_cycle, new_ema)

module For_RPC = struct
  let get_reward_coeff = get_reward_coeff
end
OCaml

Innovation. Community. Security.