Source file stake_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
330
331
332
module Selected_distribution_for_cycle = struct
module Cache_client = struct
type cached_value = (Signature.Public_key_hash.t * Stake_repr.t) list
let namespace = Cache_repr.create_namespace "stake_distribution"
let cache_index = 1
let value_of_identifier ctxt identifier =
let cycle = Cycle_repr.of_string_exn identifier in
Storage.Stake.Selected_distribution_for_cycle.get ctxt cycle
end
module Cache = (val Cache_repr.register_exn (module Cache_client))
let identifier_of_cycle cycle = Format.asprintf "%a" Cycle_repr.pp cycle
let init ctxt cycle stakes =
let open Lwt_result_syntax in
let id = identifier_of_cycle cycle in
let* ctxt =
Storage.Stake.Selected_distribution_for_cycle.init ctxt cycle stakes
in
let size = 1 in
let*? ctxt = Cache.update ctxt id (Some (stakes, size)) in
return ctxt
let get ctxt cycle =
let open Lwt_result_syntax in
let id = identifier_of_cycle cycle in
let* value_opt = Cache.find ctxt id in
match value_opt with
| None -> Storage.Stake.Selected_distribution_for_cycle.get ctxt cycle
| Some v -> return v
let find ctxt cycle =
let open Lwt_result_syntax in
let id = identifier_of_cycle cycle in
let* value_opt = Cache.find ctxt id in
match value_opt with
| None -> Storage.Stake.Selected_distribution_for_cycle.find ctxt cycle
| Some _ as some_v -> return some_v
let remove_existing ctxt cycle =
let open Lwt_result_syntax in
let id = identifier_of_cycle cycle in
let*? ctxt = Cache.update ctxt id None in
Storage.Stake.Selected_distribution_for_cycle.remove_existing ctxt cycle
end
let get_full_staking_balance ctxt delegate =
let open Lwt_result_syntax in
let+ staking_balance_opt = Storage.Stake.Staking_balance.find ctxt delegate in
Option.value staking_balance_opt ~default:Full_staking_balance_repr.zero
let get_initialized_stake ctxt delegate =
let open Lwt_result_syntax in
let* balance_opt = Storage.Stake.Staking_balance.find ctxt delegate in
match balance_opt with
| Some staking_balance -> return (staking_balance, ctxt)
| None ->
let balance = Full_staking_balance_repr.zero in
let* ctxt = Storage.Stake.Staking_balance.init ctxt delegate balance in
return (balance, ctxt)
let has_minimal_stake ctxt
{Full_staking_balance_repr.own_frozen; staked_frozen; delegated} =
let open Result_syntax in
let open Tez_repr in
let minimal_stake = Constants_storage.minimal_stake ctxt in
let sum =
let* frozen = own_frozen +? staked_frozen in
frozen +? delegated
in
match sum with
| Error _sum_overflows ->
true
| Ok staking_balance -> Tez_repr.(staking_balance >= minimal_stake)
let has_minimal_stake_and_frozen_stake ctxt
({own_frozen; _} as full_staking_balance : Full_staking_balance_repr.t) =
let minimal_frozen_stake = Constants_storage.minimal_frozen_stake ctxt in
Tez_repr.(own_frozen >= minimal_frozen_stake)
&& has_minimal_stake ctxt full_staking_balance
let update_stake ~f ctxt delegate =
let open Lwt_result_syntax in
let* staking_balance_before, ctxt = get_initialized_stake ctxt delegate in
let*? staking_balance = f staking_balance_before in
let* ctxt =
Storage.Stake.Staking_balance.update ctxt delegate staking_balance
in
let had_minimal_stake_before =
has_minimal_stake ctxt staking_balance_before
in
let has_minimal_stake_after = has_minimal_stake ctxt staking_balance in
match (had_minimal_stake_before, has_minimal_stake_after) with
| true, false ->
let* inactive = Delegate_activation_storage.is_inactive ctxt delegate in
if inactive then
return ctxt
else
let*! ctxt =
Storage.Stake.Active_delegates_with_minimal_stake.remove ctxt delegate
in
return ctxt
| false, true ->
let* inactive = Delegate_activation_storage.is_inactive ctxt delegate in
if inactive then
return ctxt
else
let*! ctxt =
Storage.Stake.Active_delegates_with_minimal_stake.add ctxt delegate ()
in
return ctxt
| false, false | true, true -> return ctxt
let remove_delegated_stake ctxt delegate amount =
let open Result_syntax in
update_stake ctxt delegate ~f:(fun {own_frozen; staked_frozen; delegated} ->
let+ delegated = Tez_repr.(delegated -? amount) in
Full_staking_balance_repr.make ~own_frozen ~staked_frozen ~delegated)
let remove_own_frozen_stake ctxt delegate amount =
let open Result_syntax in
update_stake ctxt delegate ~f:(fun {own_frozen; staked_frozen; delegated} ->
let+ own_frozen = Tez_repr.(own_frozen -? amount) in
Full_staking_balance_repr.make ~own_frozen ~staked_frozen ~delegated)
let remove_staked_frozen_stake ctxt delegate amount =
let open Result_syntax in
update_stake ctxt delegate ~f:(fun {own_frozen; staked_frozen; delegated} ->
let+ staked_frozen = Tez_repr.(staked_frozen -? amount) in
Full_staking_balance_repr.make ~own_frozen ~staked_frozen ~delegated)
let remove_frozen_stake_only_call_from_token ctxt staker amount =
match staker with
| Frozen_staker_repr.Baker delegate ->
remove_own_frozen_stake ctxt delegate amount
| Single_staker {staker = _; delegate} | Shared_between_stakers {delegate} ->
remove_staked_frozen_stake ctxt delegate amount
let add_delegated_stake ctxt delegate amount =
let open Result_syntax in
update_stake ctxt delegate ~f:(fun {own_frozen; staked_frozen; delegated} ->
let+ delegated = Tez_repr.(delegated +? amount) in
Full_staking_balance_repr.make ~own_frozen ~staked_frozen ~delegated)
let add_own_frozen_stake ctxt delegate amount =
let open Result_syntax in
update_stake ctxt delegate ~f:(fun {own_frozen; staked_frozen; delegated} ->
let+ own_frozen = Tez_repr.(own_frozen +? amount) in
Full_staking_balance_repr.make ~own_frozen ~staked_frozen ~delegated)
let add_staked_frozen_stake ctxt delegate amount =
let open Result_syntax in
update_stake ctxt delegate ~f:(fun {own_frozen; staked_frozen; delegated} ->
let+ staked_frozen = Tez_repr.(staked_frozen +? amount) in
Full_staking_balance_repr.make ~own_frozen ~staked_frozen ~delegated)
let add_frozen_stake_only_call_from_token ctxt staker amount =
match staker with
| Frozen_staker_repr.Baker delegate ->
add_own_frozen_stake ctxt delegate amount
| Single_staker {staker = _; delegate} | Shared_between_stakers {delegate} ->
add_staked_frozen_stake ctxt delegate amount
let set_inactive ctxt delegate =
let open Lwt_syntax in
let* ctxt = Delegate_activation_storage.set_inactive ctxt delegate in
Storage.Stake.Active_delegates_with_minimal_stake.remove ctxt delegate
let set_active ctxt delegate =
let open Lwt_result_syntax in
let* ctxt, inactive = Delegate_activation_storage.set_active ctxt delegate in
if not inactive then return ctxt
else
let* staking_balance, ctxt = get_initialized_stake ctxt delegate in
if has_minimal_stake ctxt staking_balance then
let*! ctxt =
Storage.Stake.Active_delegates_with_minimal_stake.add ctxt delegate ()
in
return ctxt
else return ctxt
let snapshot ctxt =
let open Lwt_result_syntax in
let* index = Storage.Stake.Last_snapshot.get ctxt in
let* ctxt = Storage.Stake.Last_snapshot.update ctxt (index + 1) in
let* ctxt = Storage.Stake.Staking_balance.snapshot ctxt index in
Storage.Stake.Active_delegates_with_minimal_stake.snapshot ctxt index
let max_snapshot_index = Storage.Stake.Last_snapshot.get
let set_selected_distribution_for_cycle ctxt cycle stakes total_stake =
let open Lwt_result_syntax in
let stakes = List.sort (fun (_, x) (_, y) -> Stake_repr.compare y x) stakes in
let* ctxt = Selected_distribution_for_cycle.init ctxt cycle stakes in
let*! ctxt = Storage.Stake.Total_active_stake.add ctxt cycle total_stake in
let*! ctxt = Storage.Stake.Staking_balance.Snapshot.clear ctxt in
let*! ctxt =
Storage.Stake.Active_delegates_with_minimal_stake.Snapshot.clear ctxt
in
Storage.Stake.Last_snapshot.update ctxt 0
let clear_cycle ctxt cycle =
let open Lwt_result_syntax in
let* ctxt = Storage.Stake.Total_active_stake.remove_existing ctxt cycle in
Selected_distribution_for_cycle.remove_existing ctxt cycle
let fold_on_active_delegates_with_minimal_stake_es ctxt ~f ~order ~init =
let open Lwt_result_syntax in
Storage.Stake.Active_delegates_with_minimal_stake.fold
ctxt
~order
~init:(Ok init)
~f:(fun delegate () acc ->
let*? acc in
f delegate acc)
let fold_snapshot ctxt ~index ~f ~init =
let open Lwt_result_syntax in
Storage.Stake.Active_delegates_with_minimal_stake.fold_snapshot
ctxt
index
~order:`Sorted
~init
~f:(fun delegate () acc ->
let* stake =
Storage.Stake.Staking_balance.Snapshot.get ctxt (index, delegate)
in
f (delegate, stake) acc)
let clear_at_cycle_end ctxt ~new_cycle =
let max_slashing_period = Constants_repr.max_slashing_period in
match Cycle_repr.sub new_cycle max_slashing_period with
| None -> return ctxt
| Some cycle_to_clear -> clear_cycle ctxt cycle_to_clear
let fold_on_active_delegates_with_minimal_stake_s =
Storage.Stake.Active_delegates_with_minimal_stake.fold
let get_selected_distribution = Selected_distribution_for_cycle.get
let find_selected_distribution = Selected_distribution_for_cycle.find
let get_selected_distribution_as_map ctxt cycle =
let open Lwt_result_syntax in
let+ stakes = Selected_distribution_for_cycle.get ctxt cycle in
List.fold_left
(fun map (pkh, stake) -> Signature.Public_key_hash.Map.add pkh stake map)
Signature.Public_key_hash.Map.empty
stakes
let prepare_stake_distribution ctxt =
let open Lwt_result_syntax in
let level = Level_storage.current ctxt in
let+ stake_distribution = get_selected_distribution_as_map ctxt level.cycle in
Raw_context.init_stake_distribution_for_current_cycle ctxt stake_distribution
let get_total_active_stake = Storage.Stake.Total_active_stake.get
let remove_contract_delegated_stake ctxt contract amount =
let open Lwt_result_syntax in
let* delegate_opt = Contract_delegate_storage.find ctxt contract in
match delegate_opt with
| None -> return ctxt
| Some delegate -> remove_delegated_stake ctxt delegate amount
let add_contract_delegated_stake ctxt contract amount =
let open Lwt_result_syntax in
let* delegate_opt = Contract_delegate_storage.find ctxt contract in
match delegate_opt with
| None -> return ctxt
| Some delegate -> add_delegated_stake ctxt delegate amount
module For_RPC = struct
let get_staking_balance ctxt delegate =
let open Lwt_result_syntax in
let* {own_frozen; staked_frozen; delegated} =
Storage.Stake.Staking_balance.get ctxt delegate
in
let*? frozen = Tez_repr.(own_frozen +? staked_frozen) in
let*? staking_balance = Tez_repr.(frozen +? delegated) in
return staking_balance
end
module Internal_for_tests = struct
let get ctxt delegate =
let open Lwt_result_syntax in
let*! result =
Storage.Stake.Active_delegates_with_minimal_stake.mem ctxt delegate
in
match result with
| true -> For_RPC.get_staking_balance ctxt delegate
| false -> return Tez_repr.zero
end