Source file delegate_services.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
open Alpha_context
type error += Balance_rpc_non_delegate of public_key_hash
let () =
register_error_kind
`Temporary
~id:"delegate_service.balance_rpc_on_non_delegate"
~title:"Balance request for an unregistered delegate"
~description:"The account whose balance was requested is not a delegate."
~pp:(fun ppf pkh ->
Format.fprintf
ppf
"The implicit account (%a) whose balance was requested is not a \
registered delegate. To get the balance of this account you can use \
the ../context/contracts/%a/balance RPC."
Signature.Public_key_hash.pp
pkh
Signature.Public_key_hash.pp
pkh)
Data_encoding.(obj1 (req "pkh" Signature.Public_key_hash.encoding))
(function Balance_rpc_non_delegate pkh -> Some pkh | _ -> None)
(fun pkh -> Balance_rpc_non_delegate pkh)
type info = {
full_balance : Tez.t;
current_frozen_deposits : Tez.t;
frozen_deposits : Tez.t;
staking_balance : Tez.t;
frozen_deposits_limit : Tez.t option;
delegated_contracts : Contract.t list;
delegated_balance : Tez.t;
deactivated : bool;
grace_period : Cycle.t;
voting_power : int32;
}
let info_encoding =
let open Data_encoding in
conv
(fun {
full_balance;
current_frozen_deposits;
frozen_deposits;
staking_balance;
frozen_deposits_limit;
delegated_contracts;
delegated_balance;
deactivated;
grace_period;
voting_power;
} ->
( full_balance,
current_frozen_deposits,
frozen_deposits,
staking_balance,
frozen_deposits_limit,
delegated_contracts,
delegated_balance,
deactivated,
grace_period,
voting_power ))
(fun ( full_balance,
current_frozen_deposits,
frozen_deposits,
staking_balance,
frozen_deposits_limit,
delegated_contracts,
delegated_balance,
deactivated,
grace_period,
voting_power ) ->
{
full_balance;
current_frozen_deposits;
frozen_deposits;
staking_balance;
frozen_deposits_limit;
delegated_contracts;
delegated_balance;
deactivated;
grace_period;
voting_power;
})
(obj10
(req "full_balance" Tez.encoding)
(req "current_frozen_deposits" Tez.encoding)
(req "frozen_deposits" Tez.encoding)
(req "staking_balance" Tez.encoding)
(opt "frozen_deposits_limit" Tez.encoding)
(req "delegated_contracts" (list Contract.encoding))
(req "delegated_balance" Tez.encoding)
(req "deactivated" bool)
(req "grace_period" Cycle.encoding)
(req "voting_power" int32))
let participation_info_encoding =
let open Data_encoding in
conv
(fun {
Delegate.expected_cycle_activity;
minimal_cycle_activity;
missed_slots;
missed_levels;
remaining_allowed_missed_slots;
expected_endorsing_rewards;
} ->
( expected_cycle_activity,
minimal_cycle_activity,
missed_slots,
missed_levels,
remaining_allowed_missed_slots,
expected_endorsing_rewards ))
(fun ( expected_cycle_activity,
minimal_cycle_activity,
missed_slots,
missed_levels,
remaining_allowed_missed_slots,
expected_endorsing_rewards ) ->
{
expected_cycle_activity;
minimal_cycle_activity;
missed_slots;
missed_levels;
remaining_allowed_missed_slots;
expected_endorsing_rewards;
})
(obj6
(req "expected_cycle_activity" int31)
(req "minimal_cycle_activity" int31)
(req "missed_slots" int31)
(req "missed_levels" int31)
(req "remaining_allowed_missed_slots" int31)
(req "expected_endorsing_rewards" Tez.encoding))
module S = struct
let raw_path = RPC_path.(open_root / "context" / "delegates")
open Data_encoding
type list_query = {active : bool; inactive : bool}
let list_query : list_query RPC_query.t =
let open RPC_query in
query (fun active inactive -> {active; inactive})
|+ flag "active" (fun t -> t.active)
|+ flag "inactive" (fun t -> t.inactive)
|> seal
let list_delegate =
RPC_service.get_service
~description:"Lists all registered delegates."
~query:list_query
~output:(list Signature.Public_key_hash.encoding)
raw_path
let path = RPC_path.(raw_path /: Signature.Public_key_hash.rpc_arg)
let info =
RPC_service.get_service
~description:"Everything about a delegate."
~query:RPC_query.empty
~output:info_encoding
path
let full_balance =
RPC_service.get_service
~description:
"Returns the full balance (in mutez) of a given delegate, including \
the frozen deposits. It does not include its delegated balance."
~query:RPC_query.empty
~output:Tez.encoding
RPC_path.(path / "full_balance")
let current_frozen_deposits =
RPC_service.get_service
~description:
"Returns the current amount of the frozen deposits (in mutez)."
~query:RPC_query.empty
~output:Tez.encoding
RPC_path.(path / "current_frozen_deposits")
let frozen_deposits =
RPC_service.get_service
~description:
"Returns the initial amount (that is, at the beginning of a cycle) of \
the frozen deposits (in mutez). This amount is the same as the \
current amount of the frozen deposits, unless the delegate has been \
punished."
~query:RPC_query.empty
~output:Tez.encoding
RPC_path.(path / "frozen_deposits")
let staking_balance =
RPC_service.get_service
~description:
"Returns the total amount of tokens (in mutez) delegated to a given \
delegate. This includes the balances of all the contracts that \
delegate to it, but also the balance of the delegate itself and its \
frozen deposits."
~query:RPC_query.empty
~output:Tez.encoding
RPC_path.(path / "staking_balance")
let frozen_deposits_limit =
RPC_service.get_service
~description:
"Returns the frozen deposits limit for the given delegate or none if \
no limit is set."
~query:RPC_query.empty
~output:(Data_encoding.option Tez.encoding)
RPC_path.(path / "frozen_deposits_limit")
let delegated_contracts =
RPC_service.get_service
~description:
"Returns the list of contracts that delegate to a given delegate."
~query:RPC_query.empty
~output:(list Contract.encoding)
RPC_path.(path / "delegated_contracts")
let delegated_balance =
RPC_service.get_service
~description:
"Returns the sum (in mutez) of all balances of all the contracts that \
delegate to a given delegate. This excludes the delegate's own \
balance and its frozen deposits."
~query:RPC_query.empty
~output:Tez.encoding
RPC_path.(path / "delegated_balance")
let deactivated =
RPC_service.get_service
~description:
"Tells whether the delegate is currently tagged as deactivated or not."
~query:RPC_query.empty
~output:bool
RPC_path.(path / "deactivated")
let grace_period =
RPC_service.get_service
~description:
"Returns the cycle by the end of which the delegate might be \
deactivated if she fails to execute any delegate action. A \
deactivated delegate might be reactivated (without loosing any stake) \
by simply re-registering as a delegate. For deactivated delegates, \
this value contains the cycle at which they were deactivated."
~query:RPC_query.empty
~output:Cycle.encoding
RPC_path.(path / "grace_period")
let voting_power =
RPC_service.get_service
~description:
"The number of rolls in the vote listings for a given delegate"
~query:RPC_query.empty
~output:Data_encoding.int32
RPC_path.(path / "voting_power")
let participation =
RPC_service.get_service
~description:
"Returns cycle and level participation information. In particular this \
indicates, in the field 'expected_cycle_activity', the number of \
slots the delegate is expected to have in the cycle based on its \
active stake. The field 'minimal_cycle_activity' indicates the \
minimal endorsing slots in the cycle required to get endorsing \
rewards. It is computed based on 'expected_cycle_activity. The fields \
'missed_slots' and 'missed_levels' indicate the number of missed \
endorsing slots and missed levels (for endorsing) in the cycle so \
far. 'missed_slots' indicates the number of missed endorsing slots in \
the cycle so far. The field 'remaining_allowed_missed_slots' \
indicates the remaining amount of endorsing slots that can be missed \
in the cycle before forfeiting the rewards. Finally, \
'expected_endorsing_rewards' indicates the endorsing rewards that \
will be distributed at the end of the cycle if activity at that point \
will be greater than the minimal required; if the activity is already \
known to be below the required minimum, then the rewards are zero."
~query:RPC_query.empty
~output:participation_info_encoding
RPC_path.(path / "participation")
end
let register () =
let open Services_registration in
register0 ~chunked:true S.list_delegate (fun ctxt q () ->
Delegate.list ctxt >>= fun delegates ->
match q with
| {active = true; inactive = false} ->
List.filter_es
(fun pkh -> Delegate.deactivated ctxt pkh >|=? not)
delegates
| {active = false; inactive = true} ->
List.filter_es (fun pkh -> Delegate.deactivated ctxt pkh) delegates
| _ -> return delegates) ;
register1 ~chunked:false S.info (fun ctxt pkh () () ->
Delegate.check_delegate ctxt pkh >>=? fun () ->
Delegate.full_balance ctxt pkh >>=? fun full_balance ->
Delegate.frozen_deposits ctxt pkh >>=? fun frozen_deposits ->
Delegate.staking_balance ctxt pkh >>=? fun staking_balance ->
Delegate.frozen_deposits_limit ctxt pkh >>=? fun frozen_deposits_limit ->
Delegate.delegated_contracts ctxt pkh >>= fun delegated_contracts ->
Delegate.delegated_balance ctxt pkh >>=? fun delegated_balance ->
Delegate.deactivated ctxt pkh >>=? fun deactivated ->
Delegate.grace_period ctxt pkh >>=? fun grace_period ->
Vote.get_voting_power_free ctxt pkh >|=? fun voting_power ->
{
full_balance;
current_frozen_deposits = frozen_deposits.current_amount;
frozen_deposits = frozen_deposits.initial_amount;
staking_balance;
frozen_deposits_limit;
delegated_contracts;
delegated_balance;
deactivated;
grace_period;
voting_power;
}) ;
register1 ~chunked:false S.full_balance (fun ctxt pkh () () ->
trace (Balance_rpc_non_delegate pkh) (Delegate.check_delegate ctxt pkh)
>>=? fun () -> Delegate.full_balance ctxt pkh) ;
register1 ~chunked:false S.current_frozen_deposits (fun ctxt pkh () () ->
Delegate.check_delegate ctxt pkh >>=? fun () ->
Delegate.frozen_deposits ctxt pkh >>=? fun deposits ->
return deposits.current_amount) ;
register1 ~chunked:false S.frozen_deposits (fun ctxt pkh () () ->
Delegate.check_delegate ctxt pkh >>=? fun () ->
Delegate.frozen_deposits ctxt pkh >>=? fun deposits ->
return deposits.initial_amount) ;
register1 ~chunked:false S.staking_balance (fun ctxt pkh () () ->
Delegate.check_delegate ctxt pkh >>=? fun () ->
Delegate.staking_balance ctxt pkh) ;
register1 ~chunked:false S.frozen_deposits_limit (fun ctxt pkh () () ->
Delegate.check_delegate ctxt pkh >>=? fun () ->
Delegate.frozen_deposits_limit ctxt pkh) ;
register1 ~chunked:true S.delegated_contracts (fun ctxt pkh () () ->
Delegate.check_delegate ctxt pkh >>=? fun () ->
Delegate.delegated_contracts ctxt pkh >|= ok) ;
register1 ~chunked:false S.delegated_balance (fun ctxt pkh () () ->
Delegate.check_delegate ctxt pkh >>=? fun () ->
Delegate.delegated_balance ctxt pkh) ;
register1 ~chunked:false S.deactivated (fun ctxt pkh () () ->
Delegate.check_delegate ctxt pkh >>=? fun () ->
Delegate.deactivated ctxt pkh) ;
register1 ~chunked:false S.grace_period (fun ctxt pkh () () ->
Delegate.check_delegate ctxt pkh >>=? fun () ->
Delegate.grace_period ctxt pkh) ;
register1 ~chunked:false S.voting_power (fun ctxt pkh () () ->
Delegate.check_delegate ctxt pkh >>=? fun () ->
Vote.get_voting_power_free ctxt pkh) ;
register1 ~chunked:false S.participation (fun ctxt pkh () () ->
Delegate.check_delegate ctxt pkh >>=? fun () ->
Delegate.delegate_participation_info ctxt pkh)
let list ctxt block ?(active = true) ?(inactive = false) () =
RPC_context.make_call0 S.list_delegate ctxt block {active; inactive} ()
let info ctxt block pkh = RPC_context.make_call1 S.info ctxt block pkh () ()
let full_balance ctxt block pkh =
RPC_context.make_call1 S.full_balance ctxt block pkh () ()
let current_frozen_deposits ctxt block pkh =
RPC_context.make_call1 S.current_frozen_deposits ctxt block pkh () ()
let frozen_deposits ctxt block pkh =
RPC_context.make_call1 S.frozen_deposits ctxt block pkh () ()
let staking_balance ctxt block pkh =
RPC_context.make_call1 S.staking_balance ctxt block pkh () ()
let frozen_deposits_limit ctxt block pkh =
RPC_context.make_call1 S.frozen_deposits_limit ctxt block pkh () ()
let delegated_contracts ctxt block pkh =
RPC_context.make_call1 S.delegated_contracts ctxt block pkh () ()
let delegated_balance ctxt block pkh =
RPC_context.make_call1 S.delegated_balance ctxt block pkh () ()
let deactivated ctxt block pkh =
RPC_context.make_call1 S.deactivated ctxt block pkh () ()
let grace_period ctxt block pkh =
RPC_context.make_call1 S.grace_period ctxt block pkh () ()
let voting_power ctxt block pkh =
RPC_context.make_call1 S.voting_power ctxt block pkh () ()
let participation ctxt block pkh =
RPC_context.make_call1 S.participation ctxt block pkh () ()