Source file injector_functor.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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
open Injector_common
open Injector_worker_types
open Injector_sigs
open Injector_errors
module Block_cache =
Aches_lwt.Lache.Make_result
(Aches.Rache.Transfer (Aches.Rache.LRU) (Block_hash))
module Int_cache =
Aches_lwt.Lache.Make_result
(Aches.Rache.Transfer
(Aches.Rache.LRU)
(struct
include Int
let hash x = x
end))
let confirmations = 2
(** Builds a client context from another client context but uses logging instead
of printing on stdout directly. This client context cannot make the injector
exit. *)
let injector_context (cctxt : #Client_context.full) : Client_context.full =
let log _channel msg = Logs_lwt.info (fun m -> m "%s" msg) in
object
inherit Client_context.proxy_context cctxt
inherit! Client_context.simple_printer log
method! exit code =
Format.ksprintf Stdlib.failwith "Injector client wants to exit %d" code
end
module Make (Parameters : PARAMETERS) = struct
module Tags = Injector_tags.Make (Parameters.Tag)
module Tags_table = Hashtbl.Make (Parameters.Tag)
module POperation = Parameters.Operation
module Inj_operation = Injector_operation.Make (POperation)
module Request = Request (Inj_operation)
module Inj_proto = Injector_protocol.Make (Parameters)
module type PROTOCOL_CLIENT =
PROTOCOL_CLIENT
with type state = Parameters.state
and type operation = Parameters.Operation.t
type proto_client = (module PROTOCOL_CLIENT)
(** Wrapper for unsigned operation with the protocol code to sign it. *)
module type Proto_unsigned_op = sig
module Proto_client : PROTOCOL_CLIENT
val value : Proto_client.unsigned_operation
end
type injected_info = {
op : Inj_operation.t;
oph : Operation_hash.t;
op_index : int;
}
type included_info = {
op : Inj_operation.t;
oph : Operation_hash.t;
op_index : int;
l1_block : Block_hash.t;
l1_level : int32;
}
type injected_l1_op_content = {
level : int32;
inj_ops : Inj_operation.Hash.t list;
signer_pkh : Signature.public_key_hash;
}
type included_l1_op_content = {
level : int32;
inj_ops : Inj_operation.Hash.t list;
}
type status =
| Pending of POperation.t
| Injected of injected_info
| Included of included_info
module Op_queue =
Disk_persistence.Make_queue
(struct
let name = "operations_queue"
end)
(Inj_operation.Hash)
(Inj_operation)
module Injected_operations = Inj_operation.Hash.Table
module Injected_ophs = Operation_hash.Table
(** The part of the state which gathers information about injected
operations (but not included). *)
type injected_state = {
injected_operations : injected_info Injected_operations.t;
(** A table mapping L1 manager operation hashes to the injection info for that
operation. *)
injected_ophs : injected_l1_op_content Injected_ophs.t;
(** A mapping of all L1 manager operations contained in a L1 batch (i.e. an L1
operation). *)
}
module Included_operations = Inj_operation.Hash.Table
module Included_in_blocks = Block_hash.Table
(** The part of the state which gathers information about
operations which are included in the L1 chain (but not confirmed). *)
type included_state = {
included_operations : included_info Included_operations.t;
included_in_blocks : included_l1_op_content Included_in_blocks.t;
}
type protocols = Tezos_shell_services.Chain_services.Blocks.protocols = {
current_protocol : Protocol_hash.t;
next_protocol : Protocol_hash.t;
}
type last_seen_head = {block_hash : Block_hash.t; level : int32}
(** The internal state of each injector worker. *)
type state = {
cctxt : Client_context.full;
(** The client context which is used to perform the injections. *)
l1_ctxt : Layer_1.t; (** Monitoring of L1 heads. *)
signers : signer list; (** The signers for this worker. *)
tags : Tags.t;
(** The tags of this worker, for both informative and identification
purposes. *)
strategy : injection_strategy;
(** The strategy of this worker for injecting the pending operations. *)
save_dir : string; (** Path to where save persistent state *)
queue : Op_queue.t;
(** The queue of pending operations for this injector. *)
injected : injected_state;
(** The information about injected operations. *)
included : included_state;
(** The information about included operations. {b Note}: Operations which
are confirmed are simply removed from the state and do not appear
anymore. *)
state : Parameters.state;
retention_period : int;
(** Number of blocks for which the injector keeps the included
information. *)
allowed_attempts : int;
(** The number of attempts that will be made to inject an operation. *)
injection_ttl : int;
(** The number of blocks after which an operation is retried when
injected but never included. *)
mutable last_seen_head : last_seen_head option;
(** Last L1 head that the injector has seen (used to compute
reorganizations). *)
mutable protocols : protocols;
(** The protocols of the head of the L1 chain. This information is used
to know with which protocol one should perform the
simulation/injection. NOTE: this value is updated before
[last_seen_head]. *)
mutable proto_client : proto_client;
(** The protocol client that should be used to simulate operations. *)
}
module Event = struct
include
Injector_events.Make (Parameters) (Tags) (POperation) (Inj_operation)
(Request)
let signers_alias signers = List.map (fun s -> s.alias) signers
let emit1 e state ?(signers = state.signers) x =
emit e (signers_alias signers, state.tags, x)
let emit2 e state ?(signers = state.signers) x y =
emit e (signers_alias signers, state.tags, x, y)
let emit3 e state ?(signers = state.signers) x y z =
emit e (signers_alias signers, state.tags, x, y, z)
end
let last_head_encoding =
let open Data_encoding in
conv
(fun {block_hash; level} -> (block_hash, level))
(fun (block_hash, level) -> {block_hash; level})
@@ obj2 (req "block_hash" Block_hash.encoding) (req "level" int32)
let read_last_head ~data_dir ~warn =
Disk_persistence.maybe_read_value
~warn
(Filename.concat data_dir "last_seen_head")
last_head_encoding
let set_last_head state head =
let open Lwt_result_syntax in
let+ () =
Disk_persistence.write_value
(Filename.concat state.save_dir "last_seen_head")
last_head_encoding
head
in
state.last_seen_head <- Some head
let init_injector cctxt l1_ctxt ~head_protocols ~data_dir state
~retention_period ~allowed_attempts ~injection_ttl ~signers strategy tags
=
let open Lwt_result_syntax in
let* signers = List.map_ep (get_signer cctxt) signers in
let* () =
Tezos_signer_backends.Encrypted.decrypt_list
cctxt
(List.map (fun k -> k.alias) signers)
in
let data_dir = Filename.concat data_dir "injector" in
let*! () = Lwt_utils_unix.create_dir data_dir in
let filter op_proj op =
Tags.mem (Parameters.operation_tag (op_proj op)) tags
in
let warn file error =
Event.(emit corrupted_operation_on_disk)
(List.map (fun s -> s.alias) signers, tags, file, error)
in
let warn_unreadable = Some warn in
let emit_event_loaded kind nb =
Event.(emit loaded_from_disk)
(List.map (fun s -> s.alias) signers, tags, nb, kind)
in
let* queue =
Op_queue.load_from_disk
~warn_unreadable
~capacity:50_000
~data_dir
~filter:(filter (fun op -> op.Inj_operation.operation))
in
let*! () = emit_event_loaded "operations_queue" @@ Op_queue.length queue in
let n =
Tags.fold (fun t acc -> acc + Parameters.table_estimated_size t) tags 0
in
let injected_operations = Injected_operations.create n in
let*! () =
emit_event_loaded "injected_operations"
@@ Injected_operations.length injected_operations
in
let included_operations =
Included_operations.create ((confirmations + retention_period) * n)
in
let*! () =
emit_event_loaded "included_operations"
@@ Included_operations.length included_operations
in
let injected_ophs = Injected_ophs.create n in
let*! () =
emit_event_loaded "injected_ophs" @@ Injected_ophs.length injected_ophs
in
let included_in_blocks =
Included_in_blocks.create ((confirmations + retention_period) * n)
in
let*! () =
emit_event_loaded "included_in_blocks"
@@ Included_in_blocks.length included_in_blocks
in
let*! last_seen_head = read_last_head ~data_dir ~warn in
let proto_client =
Inj_proto.proto_client_for_protocol head_protocols.next_protocol
in
return
{
cctxt = injector_context (cctxt :> #Client_context.full);
l1_ctxt;
signers;
tags;
strategy;
save_dir = data_dir;
queue;
injected = {injected_operations; injected_ophs};
included = {included_operations; included_in_blocks};
state;
retention_period;
allowed_attempts;
injection_ttl;
last_seen_head;
protocols = head_protocols;
proto_client;
}
(** We consider an operation to already exist in the injector if either:
- It is already in the queue
- It is injected but not included
- It is included but not confirmed. *)
let already_exists state op_hash =
Op_queue.find_opt state.queue op_hash <> None
|| Injected_operations.mem state.injected.injected_operations op_hash
||
match
Included_operations.find state.included.included_operations op_hash
with
| None -> false
| Some {l1_level; _} -> (
match state.last_seen_head with
| None -> false
| Some {level = head_level; _} ->
Int32.sub head_level l1_level < Int32.of_int confirmations)
(** Add an operation to the pending queue corresponding to the signer for this
operation. *)
let add_pending_operation ?(retry = false) state (op : Inj_operation.t) =
let open Lwt_result_syntax in
if already_exists state op.hash then
return_unit
else
let*! () =
Event.(emit1 (if retry then retry_operation else add_pending))
state
op.operation
in
Op_queue.replace state.queue op.hash op
(** Mark operations as injected (in [oph]). *)
let add_injected_operations state {pkh = signer_pkh; _} oph ~injection_level
operations =
let infos =
List.map
(fun (op_index, op) -> (op.Inj_operation.hash, {op; oph; op_index}))
operations
in
Injected_operations.replace_seq
state.injected.injected_operations
(List.to_seq infos) ;
Injected_ophs.replace
state.injected.injected_ophs
oph
{level = injection_level; inj_ops = List.map fst infos; signer_pkh}
(** [add_included_operations state oph l1_block l1_level operations] marks the
[operations] as included (in the L1 batch [oph]) in the Tezos block
[l1_block] of level [l1_level]. *)
let add_included_operations state l1_block l1_level
(operations : injected_info list) =
let infos =
List.map
(fun ({op; oph; op_index} : injected_info) ->
(op.Inj_operation.hash, {op; oph; op_index; l1_block; l1_level}))
operations
in
Included_operations.replace_seq
state.included.included_operations
(List.to_seq infos) ;
Included_in_blocks.replace
state.included.included_in_blocks
l1_block
{level = l1_level; inj_ops = List.map fst infos}
(** [remove state oph] removes the operations that correspond to the L1 batch
[oph] from the injected operations in the injector state. This function is
used to move operations from injected to included. *)
let remove_injected_operation state oph =
match Injected_ophs.find state.injected.injected_ophs oph with
| None ->
[]
| Some {inj_ops; _} ->
Injected_ophs.remove state.injected.injected_ophs oph ;
let removed =
List.fold_left
(fun removed moph ->
match
Injected_operations.find state.injected.injected_operations moph
with
| None -> removed
| Some info ->
Injected_operations.remove
state.injected.injected_operations
moph ;
info :: removed)
[]
inj_ops
in
List.rev removed
(** [forget_block state block] removes the included operations that correspond
to all the L1 batches included in [block]. This function is used,
e.g. when [block] is on an alternative chain in the case of a
reorganization. *)
let forget_block state block =
match Included_in_blocks.find state.included.included_in_blocks block with
| None ->
[]
| Some {inj_ops; _} ->
let () =
Included_in_blocks.remove state.included.included_in_blocks block
in
List.fold_left
(fun removed moph ->
match
Included_operations.find state.included.included_operations moph
with
| None -> removed
| Some info ->
Included_operations.remove
state.included.included_operations
moph ;
info :: removed)
[]
inj_ops
let fee_parameter_of_operations state ops =
List.fold_left
(fun acc {Inj_operation.operation; _} ->
let param = Parameters.fee_parameter state operation in
{
minimal_fees =
{mutez = Int64.max acc.minimal_fees.mutez param.minimal_fees.mutez};
minimal_nanotez_per_byte =
Q.max acc.minimal_nanotez_per_byte param.minimal_nanotez_per_byte;
minimal_nanotez_per_gas_unit =
Q.max
acc.minimal_nanotez_per_gas_unit
param.minimal_nanotez_per_gas_unit;
force_low_fee = acc.force_low_fee || param.force_low_fee;
fee_cap = {mutez = Int64.add acc.fee_cap.mutez param.fee_cap.mutez};
burn_cap = {mutez = Int64.add acc.burn_cap.mutez param.burn_cap.mutez};
})
{
minimal_fees = {mutez = 0L};
minimal_nanotez_per_byte = Q.zero;
minimal_nanotez_per_gas_unit = Q.zero;
force_low_fee = false;
fee_cap = {mutez = 0L};
burn_cap = {mutez = 0L};
}
ops
(** Returns the first half of the list [ops] if there is more than two
elements, or [None] otherwise. *)
let keep_half ops =
let total = List.length ops in
if total <= 1 then None else Some (List.take_n (total / 2) ops)
let available_signers state =
let used_signers =
Injected_ophs.fold
(fun _ {signer_pkh; _} signers_pkh ->
Signature.Public_key_hash.Set.add signer_pkh signers_pkh)
state.injected.injected_ophs
Signature.Public_key_hash.Set.empty
in
List.filter
(fun s -> not @@ Signature.Public_key_hash.Set.mem s.pkh used_signers)
state.signers
(** [simulate_operations ~must_succeed state operations] simulates the
injection of [operations] and returns a triple [(op, ops, results)] where
[op] is the packed operation with the adjusted limits, [ops] is the prefix
of [operations] which was considered (because it did not exceed the
quotas) and [results] are the results of the simulation. See
{!inject_operations_for_signer} for the specification of [must_succeed]. *)
let rec simulate_operations ~must_succeed state signer
(operations : Inj_operation.t list) =
let open Lwt_result_syntax in
let force =
match operations with
| [] -> assert false
| [_] ->
false
| _ -> (
match must_succeed with `All -> false | `At_least_one -> true)
in
let op_operations =
List.map (fun o -> o.Inj_operation.operation) operations
in
let*! () = Event.(emit2 simulating_operations) state op_operations force in
let fee_parameter = fee_parameter_of_operations state.state operations in
let module Proto_client = (val state.proto_client) in
let*! simulation_result =
Proto_client.simulate_operations
state.cctxt
~force
~source:signer.pkh
~src_pk:signer.pk
~successor_level:true
~fee_parameter
op_operations
in
match simulation_result with
| Error (`TzError trace) -> fail trace
| Error (`Exceeds_quotas trace) -> (
let*! () =
Event.(emit1 number_of_operations_in_queue)
state
(Op_queue.length state.queue)
in
match keep_half operations with
| None ->
fail
@@ TzTrace.cons
(Exn (Failure "Quotas exceeded when simulating one operation"))
trace
| Some operations ->
simulate_operations ~must_succeed state signer operations)
| Ok {operations_statuses; unsigned_operation} ->
let*? results =
List.combine
~when_different_lengths:
[
Exn
(Failure
"Injector: Not the same number of results as operations \
in simulation.");
]
operations
operations_statuses
in
let module Unsigned_op = struct
module Proto_client = Proto_client
let value = unsigned_operation
end in
return (results, (module Unsigned_op : Proto_unsigned_op))
let register_error state ?signers (op : Inj_operation.t) error =
let open Lwt_result_syntax in
Inj_operation.register_error op error ;
if op.errors.count > state.allowed_attempts then
let*! () =
Event.(emit3 ?signers discard_error_operation)
state
op.operation
op.errors.count
op.errors.last_error
in
Op_queue.remove state.queue op.hash
else
let*! () =
Event.(emit3 ?signers error_simulation_operation)
state
op.operation
op.errors.count
error
in
return_unit
let inject_on_node state ~nb signer (module Unsigned_op : Proto_unsigned_op) =
let open Lwt_result_syntax in
let* signed_op_bytes =
Unsigned_op.Proto_client.sign_operation
state.cctxt
signer.sk
Unsigned_op.value
in
let* oph =
Tezos_shell_services.Shell_services.Injection.operation
state.cctxt
~chain:state.cctxt#chain
signed_op_bytes
in
let*! () = Event.(emit2 ~signers:[signer] injected) state nb oph in
return oph
(** Inject the given [operations] in an L1 batch. If [must_succeed] is [`All]
then all the operations must succeed in the simulation of injection. If
[must_succeed] is [`At_least_one] at least one operation in the list
[operations] must be successful in the simulation. In any case, only
operations which are known as successful will be included in the injected L1
batch. {b Note}: [must_succeed = `At_least_one] allows to incrementally build
"or-batches" by iteratively removing operations that fail from the desired
batch. *)
let rec inject_operations_for_signer ~must_succeed state signer
(operations : Inj_operation.t list) =
let open Lwt_result_syntax in
let*! simulation_result =
trace (Step_failed "simulation")
@@ simulate_operations ~must_succeed state signer operations
in
let* () =
match simulation_result with
| Ok _ -> return_unit
| Error error ->
List.iter_es
(fun op -> register_error state ~signers:[signer] op error)
operations
in
let*? operations_results, raw_op = simulation_result in
let failure = ref false in
let* rev_non_failing_operations =
List.fold_left_es
(fun acc (op, {status; _}) ->
match status with
| Unsuccessful (Failed error) ->
failure := true ;
let+ () = register_error state ~signers:[signer] op error in
acc
| Successful
| Unsuccessful (Backtracked | Skipped | Other_branch | Never_included)
->
return (op :: acc))
[]
operations_results
in
if !failure then
let operations = List.rev rev_non_failing_operations in
inject_operations_for_signer ~must_succeed state signer operations
else
let+ oph =
trace (Step_failed "injection")
@@ inject_on_node ~nb:(List.length operations) state signer raw_op
in
let operations =
List.map
(fun (op, {index_in_batch; _}) -> (index_in_batch, op))
operations_results
in
(oph, operations)
(** Retrieve as many batch of operations from the queue while batch
size remains below the size limit. *)
let get_n_ops_batch_from_queue ~size_limit state n =
let exception
Reached_limit of
(int * Inj_operation.t list * int * Inj_operation.t list list)
in
let module Proto_client = (val state.proto_client) in
let min_size = Block_hash.size + Signature.size Signature.zero in
let op_size op =
Proto_client.operation_size op.Inj_operation.operation
+ Proto_client.operation_size_overhead
in
let _current_size, rev_current_ops, nb_batch, rev_ops_batch =
try
Op_queue.fold
(fun _oph
op
((current_size, rev_current_ops, nb_batch, rev_ops_batch) as acc) ->
if nb_batch = n then raise (Reached_limit acc) ;
let new_size = current_size + op_size op in
if new_size > size_limit then
let current_ops = List.rev rev_current_ops in
( min_size + op_size op,
[op],
nb_batch + 1,
current_ops :: rev_ops_batch )
else (new_size, op :: rev_current_ops, nb_batch, rev_ops_batch))
state.queue
(min_size, [], 0, [])
with Reached_limit acc -> acc
in
let rev_ops_batch =
if nb_batch < n then
let current_ops = List.rev rev_current_ops in
current_ops :: rev_ops_batch
else rev_ops_batch
in
List.rev rev_ops_batch
let ignore_ignorable_failing_operations state operations =
let open Lwt_result_syntax in
function
| Ok res -> return (`Injected res)
| Error err ->
let+ operations_to_drop =
List.fold_left_es
(fun to_drop op ->
let*! retry =
Parameters.retry_unsuccessful_operation
state.state
op.Inj_operation.operation
(Failed err)
in
match retry with
| Abort err -> fail err
| Retry -> return to_drop
| Forget -> return (op :: to_drop))
[]
operations
in
`Ignored operations_to_drop
(** [inject_pending_operations_round state ?size_limit signer]
injects operations from the pending queue [state.pending], whose
total size does not exceed [size_limit] using [signer]. Upon
successful injection, the operations are removed from the queue
and marked as injected. *)
let inject_pending_operations_round state signer operations_to_inject =
let open Lwt_result_syntax in
let*! () =
Event.(emit1 ~signers:[signer] considered_operations_info)
state
(List.map (fun o -> o.Inj_operation.operation) operations_to_inject)
in
let* injection_level =
match state.last_seen_head with
| None ->
let+ =
Tezos_shell_services.Shell_services.Blocks.Header.shell_header
state.cctxt
~chain:`Main
~block:(`Head 0)
()
in
header.level
| Some {level; _} -> return level
in
match operations_to_inject with
| [] -> return `Stop
| _ -> (
let*! () =
Event.(emit1 ~signers:[signer] injecting_pending)
state
(List.length operations_to_inject)
in
let must_succeed =
Parameters.batch_must_succeed
@@ List.map
(fun op -> op.Inj_operation.operation)
operations_to_inject
in
let*! res =
inject_operations_for_signer
~must_succeed
state
signer
operations_to_inject
in
let* res =
ignore_ignorable_failing_operations state operations_to_inject res
in
match res with
| `Injected (oph, injected_operations) ->
let* () =
List.iter_es
(fun (_index, op) ->
Op_queue.remove state.queue op.Inj_operation.hash)
injected_operations
in
let*! () =
Event.(emit2 injected_ops)
state
~signers:[signer]
oph
(List.map
(fun (_, o) -> o.Inj_operation.operation)
injected_operations)
in
let () =
add_injected_operations
state
signer
oph
~injection_level
injected_operations
in
let nb_operations = List.length injected_operations in
return (`Continue nb_operations)
| `Ignored operations_to_drop ->
let*! () =
Event.(emit1 ~signers:[signer] dropped_operations)
state
(List.map
(fun o -> o.Inj_operation.operation)
operations_to_drop)
in
let* () =
List.iter_es
(fun op -> Op_queue.remove state.queue op.Inj_operation.hash)
operations_to_drop
in
return (`Continue 0))
let inject_pending_operation_with_all_keys_or_no_op_left state
?(size_limit =
let module Proto_client = (val state.proto_client) in
Proto_client.max_operation_data_length) () =
let open Lwt_result_syntax in
let signers = available_signers state in
let ops_batch =
get_n_ops_batch_from_queue ~size_limit state (List.length signers)
in
let signers_and_ops = List.combine_drop signers ops_batch in
let*! res_list =
List.map_p
(fun (signer, operations_to_inject) ->
inject_pending_operations_round state signer operations_to_inject)
signers_and_ops
in
let*? total_np_op =
List.fold_left
(fun res injected_res ->
match (res, injected_res) with
| Ok n, Ok (`Continue n') -> Ok (n + n')
| Ok n, Ok `Stop -> Ok n
| Error err, _ | Ok _, Error err ->
Error err)
(Ok 0)
res_list
in
return total_np_op
(** Retrieve protocols of a block with a small cache. *)
let protocols_of_block =
let protos_cache = Block_cache.create 32 in
fun state block_hash ->
Block_cache.bind_or_put
protos_cache
block_hash
(fun block_hash ->
Tezos_shell_services.Shell_services.Blocks.protocols
state.cctxt
~chain:state.cctxt#chain
~block:(`Hash (block_hash, 0))
())
Lwt.return
(** [register_included_operation state block level op] marks the manager
operations contained in the L1 batch [op] as being included in the [block]
of level [level], by moving the successful ones from the "injected" state
to the "included" state, and re-queuing the operations that should be
retried. *)
let register_included_operation state block level oph =
let open Lwt_result_syntax in
let injected_infos = remove_injected_operation state oph in
match injected_infos with
| [] ->
return_unit
| _ ->
let* block_proto = protocols_of_block state block in
let module Proto_client =
(val Inj_proto.proto_client_for_protocol block_proto.current_protocol)
in
let* included, to_retry =
List.fold_left_es
(fun (included, to_retry) (info : injected_info) ->
let* status =
Proto_client.operation_status
state.state
block
oph
~index:info.op_index
in
match status with
| None ->
failwith
"Cannot get status for an operation which is not included \
in the block"
| Some Successful -> return (info :: included, to_retry)
| Some (Unsuccessful status) -> (
let*! retry =
Parameters.retry_unsuccessful_operation
state.state
info.op.operation
status
in
let* () =
match status with
| Failed error -> register_error state info.op error
| Other_branch | Backtracked | Skipped | Never_included ->
return_unit
in
match retry with
| Retry -> return (included, info.op :: to_retry)
| Forget -> return (included, to_retry)
| Abort err -> fail err))
([], [])
injected_infos
in
let*! () =
Event.(emit3 included)
state
block
level
(List.map
(fun (o : injected_info) -> o.op.Inj_operation.hash)
included)
in
add_included_operations state block level (List.rev included) ;
List.iter_es
(add_pending_operation ~retry:true state)
(List.rev to_retry)
(** Retrieve operation hashes of a block with a small LRU cache. *)
let manager_operations_hashes_of_block =
let blocks_ops_cache = Block_cache.create 32 in
fun state block_hash ->
let module Proto_client = (val state.proto_client) in
Block_cache.bind_or_put
blocks_ops_cache
block_hash
(fun block_hash ->
Tezos_shell_services.Shell_services.Blocks.Operation_hashes
.operation_hashes_in_pass
state.cctxt
~chain:state.cctxt#chain
~block:(`Hash (block_hash, 0))
Proto_client.manager_pass)
Lwt.return
(** [register_included_operations state (block, level)] marks the known (by
this injector) manager operations contained in [block] as being included. *)
let register_included_operations state (block_hash, level) =
let open Lwt_result_syntax in
let* operation_hashes =
manager_operations_hashes_of_block state block_hash
in
List.iter_es
(fun oph -> register_included_operation state block_hash level oph)
operation_hashes
(** [revert_included_operations state block] marks the known (by this injector)
manager operations contained in [block] as not being included any more,
typically in the case of a reorganization where [block] is on an alternative
chain. The operations are put back in the pending queue. *)
let revert_included_operations state block =
let open Lwt_result_syntax in
let revert_infos = forget_block state block in
let*! () =
Event.(emit1 revert_operations)
state
(List.map (fun o -> o.op.hash) revert_infos)
in
List.iter_es
(fun {op; _} ->
let*! requeue =
Parameters.retry_unsuccessful_operation
state.state
op.operation
Other_branch
in
match requeue with
| Retry -> add_pending_operation ~retry:true state op
| _ -> return_unit)
revert_infos
(** [register_confirmed_level state confirmed_level] is called when the level
[confirmed_level] is known as confirmed. In this case, the operations of
block which are below this level are also considered as confirmed and are
removed from the "included" state. These operations cannot be part of a
reorganization so there will be no need to re-inject them anymore. *)
let register_confirmed_level state confirmed_level =
let open Lwt_result_syntax in
let*! () = Event.(emit1 confirmed_level) state confirmed_level in
Included_in_blocks.iter_es
(fun block ({level = inclusion_level; _} : included_l1_op_content) ->
if
inclusion_level
<= Int32.sub confirmed_level (Int32.of_int state.retention_period)
then
let _removed_ops = forget_block state block in
return_unit
else return_unit)
state.included.included_in_blocks
(** Retry operations that are injected but never included after
[state.injection_ttl]. *)
let retry_expired_injected_operations state head_level =
let open Lwt_result_syntax in
let expired =
Injected_ophs.fold
(fun oph ({level = injection_level; _} : injected_l1_op_content) acc ->
if
head_level
> Int32.add injection_level (Int32.of_int state.injection_ttl)
then oph :: acc
else acc)
state.injected.injected_ophs
[]
in
let expired_infos =
List.fold_left
(fun acc oph ->
let injected_infos = remove_injected_operation state oph in
List.rev_append injected_infos acc)
[]
expired
in
List.iter_es
(fun (info : injected_info) ->
let*! () =
Event.(emit2 never_included)
state
info.op.operation
state.injection_ttl
in
let*! retry =
Parameters.retry_unsuccessful_operation
state.state
info.op.operation
Never_included
in
match retry with
| Abort err -> fail err
| Retry -> add_pending_operation ~retry:true state info.op
| Forget -> return_unit)
(List.rev expired_infos)
(** [on_new_tezos_head state head] is called when there is a new Tezos
head. It first reverts any blocks that are in the alternative branch of
the reorganization and then registers the effect of the new branch (the
newly included operation and confirmed operations). *)
let on_new_tezos_head state
({block_hash = head_hash; level = head_level} as head) =
let open Lwt_result_syntax in
let*! () = Event.(emit1 new_tezos_head) state head_hash in
let*! reorg =
match state.last_seen_head with
| None ->
return {Reorg.no_reorg with new_chain = [(head_hash, head_level)]}
| Some last_head ->
Layer_1.get_tezos_reorg_for_new_head
state.l1_ctxt
(`Head (last_head.block_hash, last_head.level))
(head_hash, head_level)
in
let* reorg =
match reorg with
| Error trace
when TzTrace.fold
(fun yes error ->
yes
||
match error with
| Layer_1.Cannot_find_predecessor _ -> true
| _ -> false)
false
trace ->
let*! () = Event.(emit1 cannot_compute_reorg) state head_hash in
return {Reorg.no_reorg with new_chain = [(head_hash, head_level)]}
| _ -> Lwt.return reorg
in
let* () =
List.iter_es
(fun (removed_block, _) ->
revert_included_operations state removed_block)
(List.rev reorg.old_chain)
in
let* () =
List.iter_es
(fun added_block -> register_included_operations state added_block)
reorg.new_chain
in
let* () = retry_expired_injected_operations state head_level in
let confirmed_level = Int32.sub head_level (Int32.of_int confirmations) in
let* () =
if confirmed_level >= 0l then
register_confirmed_level state confirmed_level
else return_unit
in
set_last_head state head
let on_inject state =
let open Lwt_result_syntax in
let* total_nb_injected_op =
inject_pending_operation_with_all_keys_or_no_op_left state ()
in
let*! () = Event.(emit1 total_injected_ops) state total_nb_injected_op in
let*! () =
Event.(emit1 number_of_operations_in_queue)
state
(Op_queue.length state.queue)
in
return ()
module Types = struct
type nonrec state = state
type parameters = {
signers : Signature.public_key_hash list;
cctxt : Client_context.full;
l1_ctxt : Layer_1.t;
head_protocols : protocols;
data_dir : string;
state : Parameters.state;
retention_period : int;
allowed_attempts : int;
injection_ttl : int;
strategy : injection_strategy;
}
end
module Name = struct
type t = Tags.t
let encoding = Tags.encoding
let base = Parameters.events_section @ ["injector"]
let pp = Tags.pp
let equal = Tags.equal
end
module Worker = Worker.MakeSingle (Name) (Request) (Types)
type worker = Worker.infinite Worker.queue Worker.t
let table = Worker.create_table Queue
let tags_table = Tags_table.create 7
module Handlers = struct
type self = worker
let on_request :
type r request_error.
worker ->
(r, request_error) Request.t ->
(r, request_error) result Lwt.t =
fun w request ->
let state = Worker.state w in
match request with
| Request.Add_pending op ->
protect @@ fun () -> add_pending_operation state op
| Request.New_tezos_head (block_hash, level) ->
protect @@ fun () -> on_new_tezos_head state {block_hash; level}
| Request.Inject -> protect @@ fun () -> on_inject state
type launch_error = error trace
let on_launch _w tags
Types.
{
signers;
cctxt;
l1_ctxt;
head_protocols;
data_dir;
state;
retention_period;
allowed_attempts;
injection_ttl;
strategy;
} =
trace (Step_failed "initialization")
@@ init_injector
cctxt
l1_ctxt
~head_protocols
~data_dir
state
~retention_period
~allowed_attempts
~injection_ttl
~signers
strategy
tags
let on_error (type a b) w st (r : (a, b) Request.t) (errs : b) :
unit tzresult Lwt.t =
let open Lwt_result_syntax in
let state = Worker.state w in
let request_view = Request.view r in
let emit_and_return_errors errs =
let*! () = Event.(emit3 request_failed) state request_view st errs in
return_unit
in
match r with
| Request.Add_pending _ -> emit_and_return_errors errs
| Request.New_tezos_head _ -> emit_and_return_errors errs
| Request.Inject -> emit_and_return_errors errs
let on_completion w r _ st =
let state = Worker.state w in
Event.(emit2 request_completed_debug) state (Request.view r) st
let on_no_request _ = Lwt.return_unit
let on_close w =
let state = Worker.state w in
Tags.iter (Tags_table.remove tags_table) state.tags ;
Lwt.return_unit
end
let has_tag_in ~tags state =
match tags with
| None ->
true
| Some tags -> not (Tags.disjoint state.tags tags)
let delay_strategy state f =
let open Lwt_syntax in
match state.strategy with
| `Each_block -> f ()
| `Delay_block delay_factor ->
let module Proto_client = (val state.proto_client) in
let time_until_next_block =
Proto_client.time_until_next_block state.state header
|> Ptime.Span.to_float_s
in
let delay = time_until_next_block *. delay_factor in
if delay <= 0. then f ()
else
let promise =
let* () = Event.(emit1 inject_wait) state delay in
let* () = Lwt_unix.sleep delay in
f ()
in
ignore promise ;
return_unit
let inject ?tags ? () =
let workers = Worker.list table in
let tags = Option.map Tags.of_list tags in
List.iter_p
(fun (_signer, w) ->
let open Lwt_syntax in
let worker_state = Worker.state w in
if has_tag_in ~tags worker_state then
delay_strategy worker_state header @@ fun () ->
let* _pushed = Worker.Queue.push_request w Request.Inject in
return_unit
else Lwt.return_unit)
workers
let notify_new_tezos_head h =
let open Lwt_syntax in
let workers = Worker.list table in
List.iter_p
(fun (_signer, w) ->
let* (_pushed : bool) =
Worker.Queue.push_request w (Request.New_tezos_head h)
in
return_unit)
workers
let protocols_of_head cctxt =
Tezos_shell_services.Shell_services.Blocks.protocols
cctxt
~chain:cctxt#chain
~block:(`Head 0)
()
let update_protocol ~next_protocol =
let workers = Worker.list table in
List.iter
(fun (_signer, w) ->
let state = Worker.state w in
let protocols =
{current_protocol = state.protocols.next_protocol; next_protocol}
in
state.proto_client <- Inj_proto.proto_client_for_protocol next_protocol ;
state.protocols <- protocols)
workers
(** Retrieve next protocol of a block with a small cache from protocol levels
to protocol hashes. *)
let next_protocol_of_block =
let proto_levels_cache = Int_cache.create 7 in
fun (cctxt : Client_context.full)
(block_hash, ( : Block_header.t)) ->
let open Lwt_result_syntax in
let proto_level = block_header.shell.proto_level in
Int_cache.bind_or_put
proto_levels_cache
proto_level
(fun _proto_level ->
let+ protos =
Tezos_shell_services.Shell_services.Blocks.protocols
cctxt
~chain:cctxt#chain
~block:(`Hash (block_hash, 0))
()
in
protos.next_protocol)
Lwt.return
let rec monitor_l1_chain (cctxt : #Client_context.full) l1_ctxt =
let open Lwt_result_syntax in
let*! res =
Layer_1.iter_heads l1_ctxt @@ fun (head_hash, ) ->
let head = (head_hash, header.shell.level) in
let* next_protocol =
next_protocol_of_block (cctxt :> Client_context.full) (head_hash, header)
in
update_protocol ~next_protocol ;
let*! () = notify_new_tezos_head head in
return_unit
in
let*! () =
match res with
| Error error -> Internal_event.Simple.emit Event.monitoring_error error
| Ok () -> Lwt.return_unit
in
monitor_l1_chain cctxt l1_ctxt
let rec async_monitor_l1_chain (cctxt : #Client_context.full) l1_ctxt =
Lwt.dont_wait
(fun () -> monitor_l1_chain cctxt l1_ctxt)
(fun exn ->
Format.eprintf
"Warning: Error in async monitoring (%s), restarting"
(Printexc.to_string exn) ;
async_monitor_l1_chain cctxt l1_ctxt)
let check_signer_is_not_already_registered registered_signers signers =
List.iter_ep
(fun pkh ->
fail_when (Signature.Public_key_hash.Set.mem pkh registered_signers)
@@ error_of_fmt
"key %a is already registered"
Signature.Public_key_hash.pp
pkh)
signers
let check_tag_is_not_already_registered registered_tags tags =
error_unless (Tags.disjoint tags registered_tags)
@@ error_of_fmt "tags %a is already registered" Tags.pp tags
let register_worker cctxt l1_ctxt head_protocols ~data_dir state
retention_period allowed_attempts injection_ttl signers strategy tags =
let open Lwt_result_syntax in
let* worker =
Worker.launch
table
tags
{
signers;
cctxt = (cctxt :> Client_context.full);
l1_ctxt;
head_protocols;
data_dir;
state;
retention_period;
allowed_attempts;
injection_ttl;
strategy;
}
(module Handlers)
in
let () = Tags.iter (fun tag -> Tags_table.add tags_table tag worker) tags in
return_unit
let register_disjoint_signers (cctxt : #Client_context.full) l1_ctxt ~data_dir
retention_period ~allowed_attempts ~injection_ttl state
(signers :
(Signature.public_key_hash list
* injection_strategy
* Parameters.Tag.t list)
list) =
let open Lwt_result_syntax in
let rec aux registered_signers registered_tags = function
| [] -> return_unit
| (signers, strategy, tags) :: rest ->
let* () =
check_signer_is_not_already_registered registered_signers signers
in
let tags = Tags.of_list tags in
let*? () = check_tag_is_not_already_registered registered_tags tags in
let*? () = Inj_proto.check_registered_proto_clients state in
let* head_protocols = protocols_of_head cctxt in
let* _ =
register_worker
cctxt
l1_ctxt
head_protocols
~data_dir
state
retention_period
allowed_attempts
injection_ttl
signers
strategy
tags
in
let registered_signers =
Signature.Public_key_hash.Set.add_seq
(List.to_seq signers)
registered_signers
in
let registered_tags = Tags.union tags registered_tags in
aux registered_signers registered_tags rest
in
aux Signature.Public_key_hash.Set.empty Tags.empty signers
let init (cctxt : #Client_context.full) ~data_dir ?(retention_period = 0)
?(allowed_attempts = 100) ?(injection_ttl = 120)
?(reconnection_delay = 2.0) state ~signers =
let open Lwt_result_syntax in
assert (retention_period >= 0) ;
assert (allowed_attempts >= 0) ;
assert (injection_ttl > 0) ;
let*! l1_ctxt = Layer_1.start ~name:"injector" ~reconnection_delay cctxt in
let* () =
register_disjoint_signers
(cctxt : #Client_context.full)
l1_ctxt
~data_dir
retention_period
~allowed_attempts
~injection_ttl
state
signers
in
async_monitor_l1_chain cctxt l1_ctxt ;
return_unit
let worker_of_tag tag =
let open Result_syntax in
match Tags_table.find_opt tags_table tag with
| None ->
Format.kasprintf
(fun s -> tzfail (No_worker_for_tag s))
"%a"
Parameters.Tag.pp
tag
| Some worker -> return worker
let add_pending_operation op =
let open Lwt_result_syntax in
let operation = Inj_operation.make op in
let*? w = worker_of_tag (Parameters.operation_tag op) in
let*! (_pushed : bool) =
Worker.Queue.push_request w (Request.Add_pending operation)
in
return operation.hash
let shutdown () =
let workers = Worker.list table in
List.iter_p (fun (_signer, w) -> Worker.shutdown w) workers
let op_status_in_worker state l1_hash =
match Op_queue.find_opt state.queue l1_hash with
| Some op -> Some (Pending op.operation)
| None -> (
match
Injected_operations.find state.injected.injected_operations l1_hash
with
| Some info -> Some (Injected info)
| None -> (
match
Included_operations.find
state.included.included_operations
l1_hash
with
| Some info -> Some (Included info)
| None -> None))
let operation_status l1_hash =
let workers = Worker.list table in
List.find_map
(fun (_signer, w) -> op_status_in_worker (Worker.state w) l1_hash)
workers
let register_proto_client = Inj_proto.register
end