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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
open Alpha_context
type error += Balance_rpc_non_delegate of public_key_hash
type error += Not_registered of Signature.Public_key_hash.t
let () =
register_error_kind
`Temporary
~id:"delegate.not_registered"
~title:"Not a registered delegate"
~description:
"The provided public key hash is not the address of a registered \
delegate."
~pp:(fun ppf pkh ->
Format.fprintf
ppf
"The provided public key hash (%a) is not the address of a registered \
delegate. If you own this account and want to register it as a \
delegate, use a delegation operation to delegate the account to \
itself."
Signature.Public_key_hash.pp
pkh)
Data_encoding.(obj1 (req "pkh" Signature.Public_key_hash.encoding))
(function Not_registered pkh -> Some pkh | _ -> None)
(fun pkh -> Not_registered pkh)
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_info : Vote.delegate_info;
active_consensus_key : Signature.Public_key_hash.t;
pending_consensus_keys : (Cycle.t * Signature.Public_key_hash.t) list;
}
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_info;
active_consensus_key;
pending_consensus_keys;
} ->
( ( full_balance,
current_frozen_deposits,
frozen_deposits,
staking_balance,
frozen_deposits_limit,
delegated_contracts,
delegated_balance,
deactivated,
grace_period ),
(voting_info, (active_consensus_key, pending_consensus_keys)) ))
(fun ( ( full_balance,
current_frozen_deposits,
frozen_deposits,
staking_balance,
frozen_deposits_limit,
delegated_contracts,
delegated_balance,
deactivated,
grace_period ),
(voting_info, (active_consensus_key, pending_consensus_keys)) ) ->
{
full_balance;
current_frozen_deposits;
frozen_deposits;
staking_balance;
frozen_deposits_limit;
delegated_contracts;
delegated_balance;
deactivated;
grace_period;
voting_info;
active_consensus_key;
pending_consensus_keys;
})
(merge_objs
(obj9
(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))
(merge_objs
Vote.delegate_info_encoding
(obj2
(req "active_consensus_key" Signature.Public_key_hash.encoding)
(dft
"pending_consensus_keys"
(list
(obj2
(req "cycle" Cycle.encoding)
(req "pkh" Signature.Public_key_hash.encoding)))
[]))))
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;
with_minimal_stake : bool;
without_minimal_stake : bool;
}
let list_query : list_query RPC_query.t =
let open RPC_query in
query (fun active inactive with_minimal_stake without_minimal_stake ->
{active; inactive; with_minimal_stake; without_minimal_stake})
|+ flag "active" (fun t -> t.active)
|+ flag "inactive" (fun t -> t.inactive)
|+ flag "with_minimal_stake" (fun t -> t.with_minimal_stake)
|+ flag "without_minimal_stake" (fun t -> t.without_minimal_stake)
|> seal
let list_delegate =
RPC_service.get_service
~description:
"Lists all registered delegates by default. The arguments `active`, \
`inactive`, `with_minimal_stake`, and `without_minimal_stake` allow \
to enumerate only the delegates that are active, inactive, have at \
least a minimal stake to participate in consensus and in governance, \
or do not have such a minimal stake, respectively. Note, setting \
these arguments to false has no effect."
~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 and the frozen bonds. 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, its \
frozen deposits, and its frozen bonds."
~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, its frozen deposits and its frozen bonds."
~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 voting power in the vote listings for a given delegate."
~query:RPC_query.empty
~output:Data_encoding.int64
RPC_path.(path / "voting_power")
let voting_info =
RPC_service.get_service
~description:
"Returns the delegate info (e.g. voting power) found in the listings \
of the current voting period."
~query:RPC_query.empty
~output:Vote.delegate_info_encoding
RPC_path.(path / "voting_info")
let consensus_key =
RPC_service.get_service
~description:
"The active consensus key for a given delegate and the pending \
consensus keys."
~query:RPC_query.empty
~output:
Data_encoding.(
obj2
(req "active" Signature.Public_key_hash.encoding)
(dft
"pendings"
(list
(obj2
(req "cycle" Cycle.encoding)
(req "pkh" Signature.Public_key_hash.encoding)))
[]))
RPC_path.(path / "consensus_key")
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 check_delegate_registered ctxt pkh =
Delegate.registered ctxt pkh >>= function
| true -> return_unit
| false -> tzfail (Not_registered pkh)
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
| {active = false; inactive = false; _}
| {active = true; inactive = true; _} ->
return delegates)
>>=? fun delegates ->
let minimal_stake = Constants.minimal_stake ctxt in
match q with
| {with_minimal_stake = true; without_minimal_stake = false; _} ->
List.filter_es
(fun pkh ->
Delegate.staking_balance ctxt pkh >|=? fun staking_balance ->
Tez.(staking_balance >= minimal_stake))
delegates
| {with_minimal_stake = false; without_minimal_stake = true; _} ->
List.filter_es
(fun pkh ->
Delegate.staking_balance ctxt pkh >|=? fun staking_balance ->
Tez.(staking_balance < minimal_stake))
delegates
| {with_minimal_stake = true; without_minimal_stake = true; _}
| {with_minimal_stake = false; without_minimal_stake = false; _} ->
return delegates) ;
register1 ~chunked:false S.info (fun ctxt pkh () () ->
check_delegate_registered 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.last_cycle_before_deactivation ctxt pkh >>=? fun grace_period ->
Vote.get_delegate_info ctxt pkh >>=? fun voting_info ->
Delegate.Consensus_key.active_pubkey ctxt pkh >>=? fun consensus_key ->
Delegate.Consensus_key.pending_updates ctxt pkh >|=? fun pendings ->
{
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_info;
active_consensus_key = consensus_key.consensus_pkh;
pending_consensus_keys = pendings;
}) ;
register1 ~chunked:false S.full_balance (fun ctxt pkh () () ->
trace (Balance_rpc_non_delegate pkh) (check_delegate_registered ctxt pkh)
>>=? fun () -> Delegate.full_balance ctxt pkh) ;
register1 ~chunked:false S.current_frozen_deposits (fun ctxt pkh () () ->
check_delegate_registered ctxt pkh >>=? fun () ->
Delegate.frozen_deposits ctxt pkh >>=? fun deposits ->
return deposits.current_amount) ;
register1 ~chunked:false S.frozen_deposits (fun ctxt pkh () () ->
check_delegate_registered ctxt pkh >>=? fun () ->
Delegate.frozen_deposits ctxt pkh >>=? fun deposits ->
return deposits.initial_amount) ;
register1 ~chunked:false S.staking_balance (fun ctxt pkh () () ->
check_delegate_registered ctxt pkh >>=? fun () ->
Delegate.staking_balance ctxt pkh) ;
register1 ~chunked:false S.frozen_deposits_limit (fun ctxt pkh () () ->
check_delegate_registered ctxt pkh >>=? fun () ->
Delegate.frozen_deposits_limit ctxt pkh) ;
register1 ~chunked:true S.delegated_contracts (fun ctxt pkh () () ->
check_delegate_registered ctxt pkh >>=? fun () ->
Delegate.delegated_contracts ctxt pkh >|= ok) ;
register1 ~chunked:false S.delegated_balance (fun ctxt pkh () () ->
check_delegate_registered ctxt pkh >>=? fun () ->
Delegate.delegated_balance ctxt pkh) ;
register1 ~chunked:false S.deactivated (fun ctxt pkh () () ->
check_delegate_registered ctxt pkh >>=? fun () ->
Delegate.deactivated ctxt pkh) ;
register1 ~chunked:false S.grace_period (fun ctxt pkh () () ->
check_delegate_registered ctxt pkh >>=? fun () ->
Delegate.last_cycle_before_deactivation ctxt pkh) ;
register1 ~chunked:false S.voting_power (fun ctxt pkh () () ->
check_delegate_registered ctxt pkh >>=? fun () ->
Vote.get_voting_power_free ctxt pkh) ;
register1 ~chunked:false S.voting_info (fun ctxt pkh () () ->
check_delegate_registered ctxt pkh >>=? fun () ->
Vote.get_delegate_info ctxt pkh) ;
register1 ~chunked:false S.consensus_key (fun ctxt pkh () () ->
Delegate.Consensus_key.active_pubkey ctxt pkh >>=? fun pk ->
Delegate.Consensus_key.pending_updates ctxt pkh >>=? fun pendings ->
return (pk.consensus_pkh, pendings)) ;
register1 ~chunked:false S.participation (fun ctxt pkh () () ->
check_delegate_registered ctxt pkh >>=? fun () ->
Delegate.participation_info ctxt pkh)
let list ctxt block ?(active = true) ?(inactive = false)
?(with_minimal_stake = true) ?(without_minimal_stake = false) () =
RPC_context.make_call0
S.list_delegate
ctxt
block
{active; inactive; with_minimal_stake; without_minimal_stake}
()
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 voting_info ctxt block pkh =
RPC_context.make_call1 S.voting_info ctxt block pkh () ()
let consensus_key ctxt block pkh =
RPC_context.make_call1 S.consensus_key ctxt block pkh () ()
let participation ctxt block pkh =
RPC_context.make_call1 S.participation ctxt block pkh () ()