package tezos-protocol-alpha

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

Source file full_staking_balance_repr.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
(*****************************************************************************)
(*                                                                           *)
(* SPDX-License-Identifier: MIT                                              *)
(* Copyright (c) 2023 Nomadic Labs, <contact@nomadic-labs.com>               *)
(*                                                                           *)
(*****************************************************************************)

(** This module is responsible for the construction, observation and encoding of
    full staking balances that are maintained to be used at cycle end to compute
    staking rights.

    The module will handle a lazy migration starting at protocol P that adds two
    new fields to the balance, the minimal delegated balance over the cycle and
    the last level at which it has been modified.
    As there is no trivial default value for Level_repr, the
    level_of_min_delegated is optional but the module must preserve the
    invariant that if a min_delegated_in_cycle has been stored, a level is
    stored with it.
*)

type t = {
  own_frozen : Tez_repr.t;
  staked_frozen : Tez_repr.t;
  delegated : Tez_repr.t;
  min_delegated_in_cycle : Tez_repr.t;
  level_of_min_delegated : Level_repr.t option;
}

let cycle_of_min_delegated (level_of_min_delegated : Level_repr.t option) =
  match level_of_min_delegated with
  | None -> Cycle_repr.root
  | Some l -> l.cycle

let init ~own_frozen ~staked_frozen ~delegated ~current_level =
  {
    own_frozen;
    staked_frozen;
    delegated;
    min_delegated_in_cycle = delegated;
    level_of_min_delegated = Some current_level;
  }

let encoding =
  let open Data_encoding in
  (* This encoding is backward-compatible with the encoding used in Oxford, so
     as to avoid a stitching in P. It will act as a lazy migration.
     The case in which [added_in_p] is [None] happen only for pre-existing
     values in the storage.
     For them, using [(delegated, None)] and using Cycle_repr.root when no level
     is set will behave correctly. *)
  let added_in_p =
    obj2
      (req "min_delegated_in_cycle" Tez_repr.encoding)
      (req "level_of_min_delegated" (option Level_repr.encoding))
  in
  conv
    (fun {
           own_frozen;
           staked_frozen;
           delegated;
           min_delegated_in_cycle;
           level_of_min_delegated;
         } ->
      ( own_frozen,
        staked_frozen,
        delegated,
        Some (min_delegated_in_cycle, level_of_min_delegated) ))
    (fun (own_frozen, staked_frozen, delegated, added_in_p_opt) ->
      let min_delegated_in_cycle, level_of_min_delegated =
        added_in_p_opt |> Option.value ~default:(delegated, None)
      in
      {
        own_frozen;
        staked_frozen;
        delegated;
        min_delegated_in_cycle;
        level_of_min_delegated;
      })
    (obj4
       (req "own_frozen" Tez_repr.encoding)
       (req "staked_frozen" Tez_repr.encoding)
       (req "delegated" Tez_repr.encoding)
       (varopt "min_delegated_in_cycle_and_level" added_in_p))

let voting_weight
    {
      own_frozen;
      staked_frozen;
      delegated;
      min_delegated_in_cycle = _;
      level_of_min_delegated = _;
    } =
  let open Result_syntax in
  let* frozen = Tez_repr.(own_frozen +? staked_frozen) in
  let+ all = Tez_repr.(frozen +? delegated) in
  Tez_repr.to_mutez all

let apply_slashing ~percentage
    {
      own_frozen;
      staked_frozen;
      delegated;
      min_delegated_in_cycle;
      level_of_min_delegated;
    } =
  let remaining_percentage = Percentage.neg percentage in
  let own_frozen =
    Tez_repr.mul_percentage ~rounding:`Down own_frozen remaining_percentage
  in
  let staked_frozen =
    Tez_repr.mul_percentage ~rounding:`Down staked_frozen remaining_percentage
  in
  {
    own_frozen;
    staked_frozen;
    delegated;
    min_delegated_in_cycle;
    level_of_min_delegated;
  }

let own_frozen
    {
      own_frozen;
      staked_frozen = _;
      delegated = _;
      min_delegated_in_cycle = _;
      level_of_min_delegated = _;
    } =
  own_frozen

let staked_frozen
    {
      own_frozen = _;
      staked_frozen;
      delegated = _;
      min_delegated_in_cycle = _;
      level_of_min_delegated = _;
    } =
  staked_frozen

let total_frozen
    {
      own_frozen;
      staked_frozen;
      delegated = _;
      min_delegated_in_cycle = _;
      level_of_min_delegated = _;
    } =
  Tez_repr.(own_frozen +? staked_frozen)

let current_delegated
    {
      own_frozen = _;
      staked_frozen = _;
      delegated;
      min_delegated_in_cycle = _;
      level_of_min_delegated = _;
    } =
  delegated

(* The minimum over the cycle is either:
     - the current delegated value if it didn't change during the cycle, i.e.
       [cycle_of_min_delegated] is not the current cycle;
     - or the stored [min_delegated_in_cycle] otherwise.
*)
let min_delegated_in_cycle ~current_cycle
    {
      own_frozen = _;
      staked_frozen = _;
      delegated;
      min_delegated_in_cycle;
      level_of_min_delegated;
    } =
  let cycle_of_min_delegated = cycle_of_min_delegated level_of_min_delegated in
  if Cycle_repr.(cycle_of_min_delegated < current_cycle) then delegated
  else (
    assert (Cycle_repr.(cycle_of_min_delegated = current_cycle)) ;
    min_delegated_in_cycle)

let current_total
    {
      own_frozen;
      staked_frozen;
      delegated;
      min_delegated_in_cycle = _;
      level_of_min_delegated = _;
    } =
  let open Result_syntax in
  let* total_frozen = Tez_repr.(own_frozen +? staked_frozen) in
  Tez_repr.(total_frozen +? delegated)

let own_ratio
    {
      own_frozen;
      staked_frozen;
      delegated = _;
      min_delegated_in_cycle = _;
      level_of_min_delegated = _;
    } =
  if Tez_repr.(staked_frozen = zero) then (1L, 1L)
  else if Tez_repr.(own_frozen = zero) then (0L, 1L)
  else
    let own_frozen = Tez_repr.to_mutez own_frozen in
    let staked_frozen = Tez_repr.to_mutez staked_frozen in
    (own_frozen, Int64.add own_frozen staked_frozen)

let has_minimal_frozen_stake ~minimal_frozen_stake full_staking_balance =
  let own_frozen = own_frozen full_staking_balance in
  Tez_repr.(own_frozen >= minimal_frozen_stake)

(* The set of delegates to consider [Active_delegates_with_minimal_stake] is an
   over-approximation of participating delegates. It is maintained by
   {!Stake_storage}.
   To avoid having to do any maintenance at cycle end, we have to rely on values
   that do not change when crossing cycle boundaries: the current amount works,
   the minimal in a given cycle wouldn't. *)
let has_minimal_stake_to_be_considered ~minimal_stake full_staking_balance =
  match current_total full_staking_balance with
  | Error _total_overflows ->
      true
      (* If the total overflows, we are definitely over the minimal stake. *)
  | Ok staking_balance -> Tez_repr.(staking_balance >= minimal_stake)

let remove_delegated ~(current_level : Level_repr.t) ~amount
    {
      own_frozen;
      staked_frozen;
      delegated;
      min_delegated_in_cycle = old_min_delegated_in_cycle;
      level_of_min_delegated;
    } =
  let open Result_syntax in
  let+ delegated = Tez_repr.(delegated -? amount) in
  let cycle_of_min_delegated = cycle_of_min_delegated level_of_min_delegated in
  let current_cycle = current_level.cycle in
  let min_delegated_in_cycle, level_of_min_delegated =
    if Cycle_repr.(cycle_of_min_delegated < current_cycle) then
      (* after decrease *) (delegated, Some current_level)
    else (
      assert (Cycle_repr.(cycle_of_min_delegated = current_cycle)) ;
      let minimum = Tez_repr.min delegated old_min_delegated_in_cycle in
      ( minimum,
        if Tez_repr.(minimum = old_min_delegated_in_cycle) then
          level_of_min_delegated
        else Some current_level ))
  in
  {
    own_frozen;
    staked_frozen;
    delegated;
    min_delegated_in_cycle;
    level_of_min_delegated;
  }

let remove_own_frozen ~amount
    {
      own_frozen;
      staked_frozen;
      delegated;
      min_delegated_in_cycle;
      level_of_min_delegated;
    } =
  let open Result_syntax in
  let+ own_frozen = Tez_repr.(own_frozen -? amount) in
  {
    own_frozen;
    staked_frozen;
    delegated;
    min_delegated_in_cycle;
    level_of_min_delegated;
  }

let remove_staked_frozen ~amount
    {
      own_frozen;
      staked_frozen;
      delegated;
      min_delegated_in_cycle;
      level_of_min_delegated;
    } =
  let open Result_syntax in
  let+ staked_frozen = Tez_repr.(staked_frozen -? amount) in
  {
    own_frozen;
    staked_frozen;
    delegated;
    min_delegated_in_cycle;
    level_of_min_delegated;
  }

let add_delegated ~(current_level : Level_repr.t) ~amount
    {
      own_frozen;
      staked_frozen;
      delegated;
      min_delegated_in_cycle = old_min_delegated_in_cycle;
      level_of_min_delegated;
    } =
  let open Result_syntax in
  let cycle_of_min_delegated = cycle_of_min_delegated level_of_min_delegated in
  let current_cycle = current_level.cycle in
  let min_delegated_in_cycle, level_of_min_delegated =
    if Cycle_repr.(cycle_of_min_delegated < current_cycle) then
      (* before increase *) (delegated, Some current_level)
    else (
      assert (Cycle_repr.(cycle_of_min_delegated = current_cycle)) ;
      (old_min_delegated_in_cycle, level_of_min_delegated))
  in
  let+ delegated = Tez_repr.(delegated +? amount) in
  {
    own_frozen;
    staked_frozen;
    delegated;
    min_delegated_in_cycle;
    level_of_min_delegated;
  }

let add_own_frozen ~amount
    {
      own_frozen;
      staked_frozen;
      delegated;
      min_delegated_in_cycle;
      level_of_min_delegated;
    } =
  let open Result_syntax in
  let+ own_frozen = Tez_repr.(own_frozen +? amount) in
  {
    own_frozen;
    staked_frozen;
    delegated;
    min_delegated_in_cycle;
    level_of_min_delegated;
  }

let add_staked_frozen ~amount
    {
      own_frozen;
      staked_frozen;
      delegated;
      min_delegated_in_cycle;
      level_of_min_delegated;
    } =
  let open Result_syntax in
  let+ staked_frozen = Tez_repr.(staked_frozen +? amount) in
  {
    own_frozen;
    staked_frozen;
    delegated;
    min_delegated_in_cycle;
    level_of_min_delegated;
  }

module Internal_for_tests_and_RPCs = struct
  let min_delegated_in_cycle
      {
        own_frozen = _;
        staked_frozen = _;
        delegated = _;
        min_delegated_in_cycle;
        level_of_min_delegated = _;
      } =
    min_delegated_in_cycle

  let level_of_min_delegated
      {
        own_frozen = _;
        staked_frozen = _;
        delegated = _;
        min_delegated_in_cycle = _;
        level_of_min_delegated;
      } =
    level_of_min_delegated
end
OCaml

Innovation. Community. Security.