Source file roll_storage_legacy.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
type error +=
| Consume_roll_change
| No_roll_for_delegate
| No_stake_snapshot_for_cycle of Cycle_repr.t
| Unregistered_delegate of Signature.Public_key_hash.t
let () =
let open Data_encoding in
register_error_kind
`Permanent
~id:"contract.manager.consume_roll_change"
~title:"Consume roll change"
~description:"Change is not enough to consume a roll."
~pp:(fun ppf () ->
Format.fprintf ppf "Not enough change to consume a roll.")
empty
(function Consume_roll_change -> Some () | _ -> None)
(fun () -> Consume_roll_change) ;
register_error_kind
`Permanent
~id:"contract.manager.no_roll_for_delegate"
~title:"No roll for delegate"
~description:"Delegate has no roll."
~pp:(fun ppf () -> Format.fprintf ppf "Delegate has no roll.")
empty
(function No_roll_for_delegate -> Some () | _ -> None)
(fun () -> No_roll_for_delegate) ;
register_error_kind
`Permanent
~id:"contract.manager.no_stake_snapshot_for_cycle"
~title:"No roll snapshot for cycle"
~description:
"A snapshot of the rolls distribution does not exist for this cycle."
~pp:(fun ppf c ->
Format.fprintf
ppf
"A snapshot of the rolls distribution does not exist for cycle %a"
Cycle_repr.pp
c)
(obj1 (req "cycle" Cycle_repr.encoding))
(function No_stake_snapshot_for_cycle c -> Some c | _ -> None)
(fun c -> No_stake_snapshot_for_cycle c) ;
register_error_kind
`Permanent
~id:"contract.manager.unregistered_delegate_legacy"
~title:"Unregistered delegate"
~description:"A contract cannot be delegated to an unregistered delegate"
~pp:(fun ppf k ->
Format.fprintf
ppf
"The provided public key (with hash %a) is not registered as valid \
delegate key."
Signature.Public_key_hash.pp
k)
(obj1 (req "hash" Signature.Public_key_hash.encoding))
(function Unregistered_delegate k -> Some k | _ -> None)
(fun k -> Unregistered_delegate k)
let get_contract_delegate ctxt contract =
Storage.Contract.Delegate.find ctxt contract
let delegate_pubkey ctxt delegate =
Storage.Contract.Manager.find ctxt (Contract_repr.implicit_contract delegate)
>>=? function
| None | Some (Manager_repr.Hash _) -> fail (Unregistered_delegate delegate)
| Some (Manager_repr.Public_key pk) -> return pk
let fold ctxt ~f init =
Storage.Roll_legacy.Next.get ctxt >>=? fun last ->
let[@coq_struct "roll"] rec loop ctxt roll acc =
if Roll_repr_legacy.(roll = last) then return acc
else
Storage.Roll_legacy.Owner.find ctxt roll >>=? function
| None -> loop ctxt (Roll_repr_legacy.succ roll) acc
| Some delegate ->
f roll delegate acc >>=? fun acc ->
loop ctxt (Roll_repr_legacy.succ roll) acc
in
loop ctxt Roll_repr_legacy.first init
let get_change ctxt delegate =
Storage.Roll_legacy.Delegate_change.find ctxt delegate
>|=? Option.value ~default:Tez_repr.zero
module Delegate = struct
let fresh_roll ctxt =
Storage.Roll_legacy.Next.get ctxt >>=? fun roll ->
Storage.Roll_legacy.Next.update ctxt (Roll_repr_legacy.succ roll)
>|=? fun ctxt -> (roll, ctxt)
let get_limbo_roll ctxt =
Storage.Roll_legacy.Limbo.find ctxt >>=? function
| None ->
fresh_roll ctxt >>=? fun (roll, ctxt) ->
Storage.Roll_legacy.Limbo.init ctxt roll >|=? fun ctxt -> (roll, ctxt)
| Some roll -> return (roll, ctxt)
let consume_roll_change ctxt delegate =
let tokens_per_roll = Constants_storage.tokens_per_roll ctxt in
Storage.Roll_legacy.Delegate_change.get ctxt delegate >>=? fun change ->
record_trace Consume_roll_change Tez_repr.(change -? tokens_per_roll)
>>?= fun new_change ->
Storage.Roll_legacy.Delegate_change.update ctxt delegate new_change
let recover_roll_change ctxt delegate =
let tokens_per_roll = Constants_storage.tokens_per_roll ctxt in
Storage.Roll_legacy.Delegate_change.get ctxt delegate >>=? fun change ->
Tez_repr.(change +? tokens_per_roll) >>?= fun new_change ->
Storage.Roll_legacy.Delegate_change.update ctxt delegate new_change
let pop_roll_from_delegate ctxt delegate =
recover_roll_change ctxt delegate >>=? fun ctxt ->
Storage.Roll_legacy.Limbo.find ctxt >>=? fun limbo_head ->
Storage.Roll_legacy.Delegate_roll_list.find ctxt delegate >>=? function
| None -> fail No_roll_for_delegate
| Some roll ->
Storage.Roll_legacy.Owner.remove_existing ctxt roll >>=? fun ctxt ->
Storage.Roll_legacy.Successor.find ctxt roll >>=? fun successor_roll ->
Storage.Roll_legacy.Delegate_roll_list.add_or_remove
ctxt
delegate
successor_roll
>>= fun ctxt ->
Storage.Roll_legacy.Successor.add_or_remove ctxt roll limbo_head
>>= fun ctxt ->
Storage.Roll_legacy.Limbo.add ctxt roll >|= fun ctxt ->
ok (roll, ctxt)
let create_roll_in_delegate ctxt delegate delegate_pk =
consume_roll_change ctxt delegate >>=? fun ctxt ->
Storage.Roll_legacy.Delegate_roll_list.find ctxt delegate
>>=? fun delegate_head ->
get_limbo_roll ctxt >>=? fun (roll, ctxt) ->
Storage.Roll_legacy.Owner.init ctxt roll delegate_pk >>=? fun ctxt ->
Storage.Roll_legacy.Successor.find ctxt roll >>=? fun limbo_successor ->
Storage.Roll_legacy.Limbo.add_or_remove ctxt limbo_successor >>= fun ctxt ->
Storage.Roll_legacy.Successor.add_or_remove ctxt roll delegate_head
>>= fun ctxt ->
Storage.Roll_legacy.Delegate_roll_list.add ctxt delegate roll
>|= ok
let ensure_inited ctxt delegate =
Storage.Roll_legacy.Delegate_change.mem ctxt delegate >>= function
| true -> return ctxt
| false ->
Storage.Roll_legacy.Delegate_change.init ctxt delegate Tez_repr.zero
let is_inactive ctxt delegate =
Storage.Contract.Inactive_delegate.mem
ctxt
(Contract_repr.implicit_contract delegate)
>>= fun inactive ->
if inactive then return inactive
else
Storage.Contract.Delegate_desactivation.find
ctxt
(Contract_repr.implicit_contract delegate)
>|=? function
| Some last_active_cycle ->
let ({Level_repr.cycle = current_cycle; _} : Level_repr.t) =
Raw_context.current_level ctxt
in
Cycle_repr.(last_active_cycle < current_cycle)
| None ->
false
let add_amount ctxt delegate amount =
ensure_inited ctxt delegate >>=? fun ctxt ->
let tokens_per_roll = Constants_storage.tokens_per_roll ctxt in
Storage.Roll_legacy.Delegate_change.get ctxt delegate >>=? fun change ->
Tez_repr.(amount +? change) >>?= fun change ->
Storage.Roll_legacy.Delegate_change.update ctxt delegate change
>>=? fun ctxt ->
delegate_pubkey ctxt delegate >>=? fun delegate_pk ->
let[@coq_struct "change"] rec loop ctxt change =
if Tez_repr.(change < tokens_per_roll) then return ctxt
else
Tez_repr.(change -? tokens_per_roll) >>?= fun change ->
create_roll_in_delegate ctxt delegate delegate_pk >>=? fun ctxt ->
loop ctxt change
in
is_inactive ctxt delegate >>=? fun inactive ->
if inactive then return ctxt
else
loop ctxt change >>=? fun ctxt ->
Storage.Roll_legacy.Delegate_roll_list.find ctxt delegate
>>=? fun rolls ->
match rolls with
| None -> return ctxt
| Some _ ->
Storage.Legacy_active_delegates_with_rolls.add ctxt delegate >|= ok
let remove_amount ctxt delegate amount =
let tokens_per_roll = Constants_storage.tokens_per_roll ctxt in
let[@coq_struct "change"] rec loop ctxt change =
if Tez_repr.(amount <= change) then return (ctxt, change)
else
pop_roll_from_delegate ctxt delegate >>=? fun (_, ctxt) ->
Tez_repr.(change +? tokens_per_roll) >>?= fun change -> loop ctxt change
in
Storage.Roll_legacy.Delegate_change.get ctxt delegate >>=? fun change ->
is_inactive ctxt delegate >>=? fun inactive ->
(if inactive then return (ctxt, change)
else
loop ctxt change >>=? fun (ctxt, change) ->
Storage.Roll_legacy.Delegate_roll_list.find ctxt delegate
>>=? fun rolls ->
match rolls with
| None ->
Storage.Legacy_active_delegates_with_rolls.remove ctxt delegate
>|= fun ctxt -> ok (ctxt, change)
| Some _ -> return (ctxt, change))
>>=? fun (ctxt, change) ->
Tez_repr.(change -? amount) >>?= fun change ->
Storage.Roll_legacy.Delegate_change.update ctxt delegate change
let set_inactive ctxt delegate =
ensure_inited ctxt delegate >>=? fun ctxt ->
let tokens_per_roll = Constants_storage.tokens_per_roll ctxt in
Storage.Roll_legacy.Delegate_change.get ctxt delegate >>=? fun change ->
Storage.Contract.Inactive_delegate.add
ctxt
(Contract_repr.implicit_contract delegate)
>>= fun ctxt ->
Storage.Legacy_active_delegates_with_rolls.remove ctxt delegate
>>= fun ctxt ->
let[@coq_struct "change"] rec loop ctxt change =
Storage.Roll_legacy.Delegate_roll_list.find ctxt delegate >>=? function
| None -> return (ctxt, change)
| Some _roll ->
pop_roll_from_delegate ctxt delegate >>=? fun (_, ctxt) ->
Tez_repr.(change +? tokens_per_roll) >>?= fun change ->
loop ctxt change
in
loop ctxt change >>=? fun (ctxt, change) ->
Storage.Roll_legacy.Delegate_change.update ctxt delegate change
let set_active ctxt delegate =
is_inactive ctxt delegate >>=? fun inactive ->
let current_cycle = (Raw_context.current_level ctxt).cycle in
let preserved_cycles = Constants_storage.preserved_cycles ctxt in
Storage.Contract.Delegate_desactivation.find
ctxt
(Contract_repr.implicit_contract delegate)
>>=? fun current_expiration ->
let expiration =
match current_expiration with
| None -> Cycle_repr.add current_cycle (1 + (2 * preserved_cycles))
| Some current_expiration ->
let delay =
if inactive then 1 + (2 * preserved_cycles)
else 1 + preserved_cycles
in
let updated = Cycle_repr.add current_cycle delay in
Cycle_repr.max current_expiration updated
in
Storage.Contract.Delegate_desactivation.add
ctxt
(Contract_repr.implicit_contract delegate)
expiration
>>= fun ctxt ->
if not inactive then return ctxt
else
ensure_inited ctxt delegate >>=? fun ctxt ->
let tokens_per_roll = Constants_storage.tokens_per_roll ctxt in
Storage.Roll_legacy.Delegate_change.get ctxt delegate >>=? fun change ->
Storage.Contract.Inactive_delegate.remove
ctxt
(Contract_repr.implicit_contract delegate)
>>= fun ctxt ->
delegate_pubkey ctxt delegate >>=? fun delegate_pk ->
let[@coq_struct "change"] rec loop ctxt change =
if Tez_repr.(change < tokens_per_roll) then return ctxt
else
Tez_repr.(change -? tokens_per_roll) >>?= fun change ->
create_roll_in_delegate ctxt delegate delegate_pk >>=? fun ctxt ->
loop ctxt change
in
loop ctxt change >>=? fun ctxt ->
Storage.Roll_legacy.Delegate_roll_list.find ctxt delegate
>>=? fun rolls ->
match rolls with
| None -> return ctxt
| Some _ ->
Storage.Legacy_active_delegates_with_rolls.add ctxt delegate >|= ok
end
module Contract = struct
let add_amount c contract amount =
get_contract_delegate c contract >>=? function
| None -> return c
| Some delegate -> Delegate.add_amount c delegate amount
let remove_amount c contract amount =
get_contract_delegate c contract >>=? function
| None -> return c
| Some delegate -> Delegate.remove_amount c delegate amount
end