Source file block_validation.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
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
open Block_validator_errors
open Validation_errors
module Event = struct
include Internal_event.Simple
let inherited_inconsistent_cache_apply =
declare_1
~section:["block"; "validation"]
~name:"block_validation_inconsistent_cache_apply"
~msg:"applied block {hash} with an inconsistent cache: reloading cache"
~level:Warning
~pp1:Block_hash.pp
("hash", Block_hash.encoding)
let inherited_inconsistent_cache_preapply =
declare_1
~section:["block"; "validation"]
~name:"block_validation_inconsistent_cache_preapply"
~msg:
"preapplied block on top of {pred_hash} with an inconsistent cache: \
reloading cache"
~level:Warning
~pp1:Block_hash.pp
("pred_hash", Block_hash.encoding)
end
type operation = {
hash : Operation_hash.t;
operation : Operation.t;
check_signature : bool;
}
let mk_operation ?known_valid_operation_set operation =
let hash = Operation.hash operation in
let check_signature =
match known_valid_operation_set with
| None -> true
| Some known_valid -> not (Operation_hash.Set.mem hash known_valid)
in
{hash; operation; check_signature}
let operation_encoding =
let open Data_encoding in
conv
(fun {hash; operation; check_signature} ->
(hash, operation, check_signature))
(fun (hash, operation, check_signature) ->
{hash; operation; check_signature})
(obj3
(req "hash" Operation_hash.encoding)
(req "operation" Operation.encoding)
(req "check_signature" bool))
type validation_store = {
resulting_context_hash : Context_hash.t;
timestamp : Time.Protocol.t;
message : string option;
max_operations_ttl : int;
last_finalized_block_level : Int32.t;
last_preserved_block_level : Int32.t;
}
let validation_store_encoding =
let open Data_encoding in
conv
(fun {
resulting_context_hash;
timestamp;
message;
max_operations_ttl;
last_finalized_block_level;
last_preserved_block_level;
} ->
( resulting_context_hash,
timestamp,
message,
max_operations_ttl,
last_finalized_block_level,
last_preserved_block_level ))
(fun ( resulting_context_hash,
timestamp,
message,
max_operations_ttl,
last_finalized_block_level,
last_preserved_block_level ) ->
{
resulting_context_hash;
timestamp;
message;
max_operations_ttl;
last_finalized_block_level;
last_preserved_block_level;
})
(obj6
(req "resulting_context_hash" Context_hash.encoding)
(req "timestamp" Time.Protocol.encoding)
(req "message" (option string))
(req "max_operations_ttl" int31)
(req "last_finalized_block_level" int32)
(req "last_preserved_block_level" int32))
type operation_metadata = Metadata of Bytes.t | Too_large_metadata
let operation_metadata_equal m1 m2 =
match (m1, m2) with
| Metadata b1, Metadata b2 -> Bytes.equal b1 b2
| Too_large_metadata, Too_large_metadata -> true
| Metadata _, Too_large_metadata | Too_large_metadata, Metadata _ -> false
let operation_metadata_encoding =
let open Data_encoding in
def
"operation_metadata"
~title:"operation_metadata"
~description:"An operation metadata"
(union
~tag_size:`Uint8
[
case
~title:"metadata"
~description:"Content of an operation's metadata."
(Tag 0)
bytes
(function Metadata bytes -> Some bytes | _ -> None)
(fun bytes -> Metadata bytes);
case
~title:"too_large_metadata"
~description:
"Nothing, as this operation metadata was declared to be too large \
to be considered."
(Tag 1)
unit
(function Too_large_metadata -> Some () | _ -> None)
(fun () -> Too_large_metadata);
])
type ops_metadata =
| No_metadata_hash of operation_metadata list list
| Metadata_hash of (operation_metadata * Operation_metadata_hash.t) list list
let shell =
Shell_header_hash.hash_bytes
[Data_encoding.Binary.to_bytes_exn Block_header.shell_header_encoding shell]
type result = {
shell_header_hash : Shell_header_hash.t;
validation_store : validation_store;
block_metadata : Bytes.t * Block_metadata_hash.t option;
ops_metadata : ops_metadata;
}
type apply_result = {
result : result;
cache : Tezos_protocol_environment.Context.cache;
}
let check_proto_environment_version_increasing block_hash before after =
let open Result_syntax in
if Protocol.compare_version before after <= 0 then return_unit
else
tzfail
(invalid_block
block_hash
(Invalid_protocol_environment_transition (before, after)))
let update_testchain_status ctxt ~predecessor_hash timestamp =
let open Lwt_syntax in
let* tc = Context_ops.get_test_chain ctxt in
match tc with
| Not_running -> Lwt.return ctxt
| Running {expiration; _} ->
if Time.Protocol.(expiration <= timestamp) then
Context_ops.add_test_chain ctxt Not_running
else Lwt.return ctxt
| Forking {protocol; expiration} ->
let genesis =
Context_ops.compute_testchain_genesis ctxt predecessor_hash
in
let chain_id = Chain_id.of_block_hash genesis in
Context_ops.add_test_chain
ctxt
(Running {chain_id; genesis; protocol; expiration})
let init_test_chain chain_id ctxt =
let open Lwt_result_syntax in
let*! tc = Context_ops.get_test_chain ctxt in
match tc with
| Not_running | Running _ -> assert false
| Forking {protocol; _} ->
let* (module Proto_test) =
match Registered_protocol.get protocol with
| Some proto -> return proto
| None -> tzfail (Missing_test_protocol protocol)
in
let test_ctxt = ctxt in
let*! () =
Validation_events.(emit new_protocol_initialisation protocol)
in
let* {context = test_ctxt; _} =
Proto_test.init chain_id test_ctxt forked_header.Block_header.shell
in
let*! test_ctxt = Context_ops.add_test_chain test_ctxt Not_running in
let*! test_ctxt = Context_ops.add_protocol test_ctxt protocol in
Lwt_result.ok
@@ Context_ops.commit_test_chain_genesis test_ctxt forked_header
let result_encoding =
let open Data_encoding in
let ops_metadata_encoding =
union
~tag_size:`Uint8
[
case
~title:"no metadata hash"
(Tag 0)
(list (list operation_metadata_encoding))
(function
| No_metadata_hash ops_metadata -> Some ops_metadata | _ -> None)
(fun ops_metadata -> No_metadata_hash ops_metadata);
case
~title:"metadata hash"
(Tag 1)
(list
(list
(tup2
operation_metadata_encoding
Operation_metadata_hash.encoding)))
(function
| Metadata_hash ops_metadata -> Some ops_metadata | _ -> None)
(fun ops_metadata -> Metadata_hash ops_metadata);
]
in
conv
(fun {; validation_store; block_metadata; ops_metadata} ->
(shell_header_hash, validation_store, block_metadata, ops_metadata))
(fun (, validation_store, block_metadata, ops_metadata) ->
{shell_header_hash; validation_store; block_metadata; ops_metadata})
(obj4
(req "shell_header_hash" Shell_header_hash.encoding)
(req "validation_store" validation_store_encoding)
(req "block_metadata" (tup2 bytes (option Block_metadata_hash.encoding)))
(req "ops_metadata" ops_metadata_encoding))
let preapply_result_encoding :
(Block_header.shell_header * error Preapply_result.t list) Data_encoding.t =
let open Data_encoding in
obj2
(req "shell_header" Block_header.shell_header_encoding)
(req
"preapplied_operations_result"
(list (Preapply_result.encoding Tezos_rpc.Error.encoding)))
let may_force_protocol_upgrade ~user_activated_upgrades ~level
(validation_result : Tezos_protocol_environment.validation_result) =
let open Lwt_syntax in
match
Block_header.get_forced_protocol_upgrade ~user_activated_upgrades ~level
with
| None -> return validation_result
| Some hash ->
let* context =
Tezos_protocol_environment.Context.set_protocol
validation_result.context
hash
in
return {validation_result with context}
(** Applies user activated updates based either on block level or on
voted protocols *)
let may_patch_protocol ~user_activated_upgrades
~user_activated_protocol_overrides ~level
(validation_result : Tezos_protocol_environment.validation_result) =
let open Lwt_syntax in
let context = validation_result.context in
let* protocol = Context_ops.get_protocol context in
match
Block_header.get_voted_protocol_overrides
~user_activated_protocol_overrides
protocol
with
| None ->
may_force_protocol_upgrade
~user_activated_upgrades
~level
validation_result
| Some replacement_protocol ->
let* context =
Tezos_protocol_environment.Context.set_protocol
validation_result.context
replacement_protocol
in
return {validation_result with context}
module Make (Proto : Protocol_plugin.T) = struct
type 'operation_data preapplied_operation = {
hash : Operation_hash.t;
raw : Operation.t;
check_signature : bool;
protocol_data : 'operation_data;
}
type preapply_state = {
validation_state : Proto.validation_state;
application_state : Proto.application_state;
applied :
(Proto.operation_data preapplied_operation * Proto.operation_receipt) list;
live_blocks : Block_hash.Set.t;
live_operations : Operation_hash.Set.t;
}
type preapply_result =
| Applied of preapply_state * Proto.operation_receipt
| Branch_delayed of error list
| Branch_refused of error list
| Refused of error list
| Outdated
let ~( : Block_header.t)
~predecessor_resulting_context_hash hash ( : Block_header.t) =
let open Lwt_result_syntax in
let* () =
fail_unless
(Int32.succ predecessor_block_header.shell.level
= block_header.shell.level)
(invalid_block hash
@@ Invalid_level
{
expected = Int32.succ predecessor_block_header.shell.level;
found = block_header.shell.level;
})
in
let* () =
fail_unless
Time.Protocol.(
predecessor_block_header.shell.timestamp
< block_header.shell.timestamp)
(invalid_block hash Non_increasing_timestamp)
in
let* () =
fail_unless
Fitness.(
predecessor_block_header.shell.fitness < block_header.shell.fitness)
(invalid_block hash Non_increasing_fitness)
in
let* () =
fail_unless
Compare.List_length_with.(
Proto.validation_passes = block_header.shell.validation_passes)
(invalid_block
hash
(Unexpected_number_of_validation_passes
block_header.shell.validation_passes))
in
let is_consistent =
match Proto.expected_context_hash with
| Predecessor_resulting_context ->
Context_hash.equal
block_header.shell.context
predecessor_resulting_context_hash
| Resulting_context ->
true
in
let* () =
fail_unless
is_consistent
(Validation_errors.Inconsistent_hash
(predecessor_resulting_context_hash, block_header.shell.context))
in
return_unit
let block_hash ( : Block_header.t) =
let open Lwt_result_syntax in
match
Data_encoding.Binary.of_bytes_opt
Proto.block_header_data_encoding
block_header.protocol_data
with
| None -> tzfail (invalid_block block_hash Cannot_parse_block_header)
| Some protocol_data ->
return
({shell = block_header.shell; protocol_data} : Proto.block_header)
let check_one_operation_quota block_hash pass ops quota =
let open Lwt_result_syntax in
let* () =
fail_unless
(match quota.Tezos_protocol_environment.max_op with
| None -> true
| Some max -> Compare.List_length_with.(ops <= max))
(let max = Option.value ~default:~-1 quota.max_op in
invalid_block
block_hash
(Too_many_operations {pass; found = List.length ops; max}))
in
List.iter_ep
(fun {hash; operation; _} ->
let size = Data_encoding.Binary.length Operation.encoding operation in
fail_unless
(size <= Proto.max_operation_data_length)
(invalid_block
block_hash
(Oversized_operation
{operation = hash; size; max = Proto.max_operation_data_length})))
ops
let check_operation_quota block_hash operations =
let combined =
match
List.combine
~when_different_lengths:()
operations
Proto.validation_passes
with
| Ok combined -> combined
| Error () ->
raise (Invalid_argument "Block_validation.check_operation_quota")
in
List.iteri_ep
(fun i (ops, quota) ->
let pass = i + 1 in
check_one_operation_quota block_hash pass ops quota)
combined
let parse_operations block_hash operations =
List.mapi_es
(fun pass ->
let open Lwt_result_syntax in
List.map_es (fun {hash; operation; check_signature} ->
match
Data_encoding.Binary.of_bytes_opt
Proto.operation_data_encoding_with_legacy_attestation_name
operation.Operation.proto
with
| None ->
tzfail (invalid_block block_hash (Cannot_parse_operation hash))
| Some protocol_data ->
let op = {Proto.shell = operation.shell; protocol_data} in
let allowed_pass = Proto.acceptable_pass op in
let is_pass_consistent =
match allowed_pass with
| None -> false
| Some n -> Int.equal pass n
in
let* () =
fail_unless
is_pass_consistent
(invalid_block
block_hash
(Unallowed_pass {operation = hash; pass; allowed_pass}))
in
return (hash, op, check_signature)))
operations
let compute_metadata ~operation_metadata_size_limit proto_env_version
block_data ops_metadata =
let open Lwt_result_syntax in
let should_include_metadata_hashes =
match proto_env_version with
| Protocol.V0 -> false
| Protocol.(V1 | V2 | V3 | V4 | V5 | V6 | V7 | V8 | V9 | V10 | V11 | V12)
->
true
in
let block_metadata =
let metadata =
Data_encoding.Binary.to_bytes_exn
Proto.block_header_metadata_encoding_with_legacy_attestation_name
block_data
in
let metadata_hash_opt =
if should_include_metadata_hashes then
Some (Block_metadata_hash.hash_bytes [metadata])
else None
in
(metadata, metadata_hash_opt)
in
let* ops_metadata =
try[@time.duration_lwt metadata_serde_check]
let metadata_list_list =
List.map
(List.map (fun receipt ->
let bytes =
Data_encoding.Binary.to_bytes_exn
Proto
.operation_receipt_encoding_with_legacy_attestation_name
receipt
in
let _ =
Data_encoding.Binary.of_bytes_exn
Proto
.operation_receipt_encoding_with_legacy_attestation_name
bytes
in
let metadata =
match operation_metadata_size_limit with
| Shell_limits.Unlimited -> Metadata bytes
| Limited size_limit ->
if Bytes.length bytes < size_limit then Metadata bytes
else Too_large_metadata
in
(bytes, metadata)))
ops_metadata
in
if [@time.duration_lwt metadata_hash] should_include_metadata_hashes
then
return
(Metadata_hash
(List.map
(List.map (fun (bytes, metadata) ->
let metadata_hash =
Operation_metadata_hash.hash_bytes [bytes]
in
(metadata, metadata_hash)))
metadata_list_list))
else
return (No_metadata_hash (List.map (List.map snd) metadata_list_list))
with exn ->
trace
Validation_errors.Cannot_serialize_operation_metadata
(tzfail (Exn exn))
in
return (block_metadata, ops_metadata)
let prepare_context predecessor_block_metadata_hash
predecessor_ops_metadata_hash ( : Proto.block_header)
predecessor_context predecessor_hash =
let open Lwt_result_syntax in
let*! context =
update_testchain_status
predecessor_context
~predecessor_hash
block_header.shell.timestamp
in
let*! context =
match predecessor_block_metadata_hash with
| None -> Lwt.return context
| Some hash ->
Context_ops.add_predecessor_block_metadata_hash context hash
in
let*! context =
match predecessor_ops_metadata_hash with
| None -> Lwt.return context
| Some hash -> Context_ops.add_predecessor_ops_metadata_hash context hash
in
return context
let proto_apply_operations chain_id context cache
( : Block_header.t) block_hash
operations =
let open Lwt_result_syntax in
trace
(invalid_block block_hash Economic_protocol_error)
(let* state =
(Proto.begin_application
context
chain_id
(Application block_header)
~predecessor:predecessor_block_header.shell
~cache [@time.duration_lwt application_beginning])
in
let* state, ops_metadata =
(List.fold_left_es
(fun (state, acc) ops ->
let* state, ops_metadata =
List.fold_left_es
(fun (state, acc) (oph, op, _check_signature) ->
let* state, op_metadata =
Proto.apply_operation state oph op
in
return (state, op_metadata :: acc))
(state, [])
ops
in
return (state, List.rev ops_metadata :: acc))
(state, [])
operations [@time.duration_lwt operations_application])
in
let ops_metadata = List.rev ops_metadata in
let* validation_result, block_data =
(Proto.finalize_application
state
(Some block_header.shell) [@time.duration_lwt block_finalization])
in
return (validation_result, block_data, ops_metadata))
let may_init_new_protocol chain_id new_protocol
( : Proto.block_header) block_hash
(validation_result : Tezos_protocol_environment.validation_result) =
let open Lwt_result_syntax in
if Protocol_hash.equal new_protocol Proto.hash then
return
( validation_result,
Proto.environment_version,
Proto.expected_context_hash )
else
match Registered_protocol.get new_protocol with
| None ->
tzfail
(Unavailable_protocol {block = block_hash; protocol = new_protocol})
| Some (module NewProto) ->
let*? () =
check_proto_environment_version_increasing
block_hash
Proto.environment_version
NewProto.environment_version
in
let*! () =
Validation_events.(emit new_protocol_initialisation new_protocol)
in
let* validation_result =
NewProto.init chain_id validation_result.context block_header.shell
in
return
( validation_result,
NewProto.environment_version,
NewProto.expected_context_hash )
let apply ?(simulate = false) ?cached_result chain_id ~cache
~user_activated_upgrades ~user_activated_protocol_overrides
~operation_metadata_size_limit ~max_operations_ttl
~( : Block_header.t)
~predecessor_block_metadata_hash ~predecessor_ops_metadata_hash
~predecessor_context ~predecessor_resulting_context_hash
~( : Block_header.t) operations =
let open Lwt_result_syntax in
let block_hash = Block_header.hash block_header in
let = hash_shell_header block_header.shell in
match cached_result with
| Some (({result; _} as cached_result), context)
when Shell_header_hash.equal result.shell_header_hash shell_header_hash ->
let*! () = Validation_events.(emit using_preapply_result block_hash) in
let*! context_hash =
if simulate then
Lwt.return
@@ Context_ops.hash
~time:block_header.shell.timestamp
?message:result.validation_store.message
context
else
Context_ops.commit
~time:block_header.shell.timestamp
?message:result.validation_store.message
context
in
assert (
Context_hash.equal
context_hash
result.validation_store.resulting_context_hash) ;
return cached_result
| Some _ | None ->
let* () =
check_block_header
~predecessor_block_header
~predecessor_resulting_context_hash
block_hash
block_header
in
let* = parse_block_header block_hash block_header in
let* () = check_operation_quota block_hash operations in
let predecessor_hash = Block_header.hash predecessor_block_header in
let* operations =
(parse_operations
block_hash
operations [@time.duration_lwt operations_parsing])
in
let* context =
prepare_context
predecessor_block_metadata_hash
predecessor_ops_metadata_hash
block_header
predecessor_context
predecessor_hash
in
let* validation_result, block_metadata, ops_metadata =
proto_apply_operations
chain_id
context
cache
predecessor_block_header
block_header
block_hash
operations
in
let*! validation_result =
may_patch_protocol
~user_activated_upgrades
~user_activated_protocol_overrides
~level:block_header.shell.level
validation_result
in
let context = validation_result.context in
let*! new_protocol = Context_ops.get_protocol context in
let expected_proto_level =
if Protocol_hash.equal new_protocol Proto.hash then
predecessor_block_header.shell.proto_level
else (predecessor_block_header.shell.proto_level + 1) mod 256
in
let* () =
fail_when
(block_header.shell.proto_level <> expected_proto_level)
(invalid_block
block_hash
(Invalid_proto_level
{
found = block_header.shell.proto_level;
expected = expected_proto_level;
}))
in
let* () =
fail_when
Fitness.(validation_result.fitness <> block_header.shell.fitness)
(invalid_block
block_hash
(Invalid_fitness
{
expected = block_header.shell.fitness;
found = validation_result.fitness;
}))
in
let* validation_result, new_protocol_env_version, expected_context_hash
=
may_init_new_protocol
chain_id
new_protocol
block_header
block_hash
validation_result
in
let max_operations_ttl =
max
0
(min (max_operations_ttl + 1) validation_result.max_operations_ttl)
in
let validation_result = {validation_result with max_operations_ttl} in
let* block_metadata, ops_metadata =
compute_metadata
~operation_metadata_size_limit
new_protocol_env_version
block_metadata
ops_metadata
in
let (Context {cache; _}) = validation_result.context in
let context = validation_result.context in
let*! resulting_context_hash =
if simulate then
Lwt.return
@@ Context_ops.hash
~time:block_header.shell.timestamp
?message:validation_result.message
context
else
Context_ops.commit
~time:block_header.shell.timestamp
?message:validation_result.message
context [@time.duration_lwt context_commitment] [@time.flush]
in
let* () =
let is_context_consistent =
match expected_context_hash with
| Predecessor_resulting_context ->
true
| Resulting_context ->
Context_hash.equal
resulting_context_hash
block_header.shell.context
in
fail_unless
is_context_consistent
(Validation_errors.Inconsistent_hash
(resulting_context_hash, block_header.shell.context))
in
let validation_store =
{
resulting_context_hash;
timestamp = block_header.shell.timestamp;
message = validation_result.message;
max_operations_ttl = validation_result.max_operations_ttl;
last_finalized_block_level =
validation_result.last_finalized_block_level;
last_preserved_block_level =
validation_result.last_preserved_block_level;
}
in
return
{
result =
{
shell_header_hash = hash_shell_header block_header.shell;
validation_store;
block_metadata;
ops_metadata;
};
cache;
}
let recompute_metadata chain_id ~cache
~( : Block_header.t)
~predecessor_block_metadata_hash ~predecessor_ops_metadata_hash
~predecessor_context ~( : Block_header.t) operations =
let open Lwt_result_syntax in
let block_hash = Block_header.hash block_header in
let* = parse_block_header block_hash block_header in
let predecessor_hash = Block_header.hash predecessor_block_header in
let* context =
prepare_context
predecessor_block_metadata_hash
predecessor_ops_metadata_hash
block_header
predecessor_context
predecessor_hash
in
let* operations = parse_operations block_hash operations in
let* validation_result, block_metadata, ops_metadata =
proto_apply_operations
chain_id
context
cache
predecessor_block_header
block_header
block_hash
operations
in
let context = validation_result.context in
let*! new_protocol = Context_ops.get_protocol context in
let* _validation_result, new_protocol_env_version, _expected_context_hash =
may_init_new_protocol
chain_id
new_protocol
block_header
block_hash
validation_result
in
compute_metadata
~operation_metadata_size_limit:Unlimited
new_protocol_env_version
block_metadata
ops_metadata
let preapply_operation pv op =
let open Lwt_syntax in
if Operation_hash.Set.mem op.hash pv.live_operations then return Outdated
else
let+ r =
protect (fun () ->
let operation : Proto.operation =
{shell = op.raw.shell; protocol_data = op.protocol_data}
in
let open Lwt_result_syntax in
let* validation_state =
Proto.validate_operation
~check_signature:op.check_signature
pv.validation_state
op.hash
operation
in
let* application_state, receipt =
Proto.apply_operation pv.application_state op.hash operation
in
return (validation_state, application_state, receipt))
in
match r with
| Ok (validation_state, application_state, receipt) -> (
let pv =
{
validation_state;
application_state;
applied = (op, receipt) :: pv.applied;
live_blocks = pv.live_blocks;
live_operations =
Operation_hash.Set.add op.hash pv.live_operations;
}
in
match
Data_encoding.Binary.(
of_bytes_exn
Proto.operation_receipt_encoding_with_legacy_attestation_name
(to_bytes_exn
Proto.operation_receipt_encoding_with_legacy_attestation_name
receipt))
with
| receipt -> Applied (pv, receipt)
| exception exn ->
Refused
[Validation_errors.Cannot_serialize_operation_metadata; Exn exn]
)
| Error trace -> (
match classify_trace trace with
| Branch -> Branch_refused trace
| Permanent -> Refused trace
| Temporary -> Branch_delayed trace
| Outdated -> Outdated)
(** Doesn't depend on heavy [Registered_protocol.T] for testability. *)
let safe_binary_of_bytes (encoding : 'a Data_encoding.t) (bytes : bytes) :
'a tzresult =
let open Result_syntax in
match Data_encoding.Binary.of_bytes_opt encoding bytes with
| None -> tzfail Parse_error
| Some protocol_data -> return protocol_data
let parse_unsafe (proto : bytes) : Proto.operation_data tzresult =
safe_binary_of_bytes
Proto.operation_data_encoding_with_legacy_attestation_name
proto
let parse {hash; operation = raw; check_signature} =
let open Result_syntax in
let size = Data_encoding.Binary.length Operation.encoding raw in
if size > Proto.max_operation_data_length then
tzfail (Oversized_operation {size; max = Proto.max_operation_data_length})
else
let* protocol_data = parse_unsafe raw.proto in
return {hash; check_signature; raw; protocol_data}
let preapply ~chain_id ~cache ~user_activated_upgrades
~user_activated_protocol_overrides ~operation_metadata_size_limit
~protocol_data ~live_blocks ~live_operations ~timestamp
~predecessor_context ~predecessor_resulting_context_hash
~( : Block_header.shell_header) ~predecessor_hash
~predecessor_max_operations_ttl ~predecessor_block_metadata_hash
~predecessor_ops_metadata_hash ~operations =
let open Lwt_result_syntax in
let context = predecessor_context in
let*! context =
update_testchain_status context ~predecessor_hash timestamp
in
let should_metadata_be_present =
let is_from_genesis = predecessor_shell_header.validation_passes = 0 in
Protocol.(
match Proto.environment_version with
| V0 -> false
| V1 | V2 | V3 | V4 | V5 | V6 | V7 | V8 | V9 | V10 | V11 | V12 -> true)
&& not is_from_genesis
in
let* context =
match predecessor_block_metadata_hash with
| None ->
if should_metadata_be_present then
tzfail (Missing_block_metadata_hash predecessor_hash)
else return context
| Some hash ->
Lwt_result.ok
@@ Context_ops.add_predecessor_block_metadata_hash context hash
in
let* context =
match predecessor_ops_metadata_hash with
| None ->
if should_metadata_be_present then
tzfail (Missing_operation_metadata_hashes predecessor_hash)
else return context
| Some hash ->
Lwt_result.ok
@@ Context_ops.add_predecessor_ops_metadata_hash context hash
in
let mode =
Proto.Construction
{predecessor_hash; timestamp; block_header_data = protocol_data}
in
let* validation_state =
Proto.begin_validation
context
chain_id
mode
~predecessor:predecessor_shell_header
~cache
in
let* application_state =
Proto.begin_application
context
chain_id
mode
~predecessor:predecessor_shell_header
~cache
in
let preapply_state =
{
validation_state;
application_state;
applied = [];
live_blocks;
live_operations;
}
in
let apply_operation_with_preapply_result preapp t receipts op =
let open Preapply_result in
let*! r = preapply_operation t op in
match r with
| Applied (t, receipt) ->
let applied = (op.hash, op.raw) :: preapp.applied in
Lwt.return ({preapp with applied}, t, receipt :: receipts)
| Branch_delayed errors ->
let branch_delayed =
Operation_hash.Map.add
op.hash
(op.raw, errors)
preapp.branch_delayed
in
Lwt.return ({preapp with branch_delayed}, t, receipts)
| Branch_refused errors ->
let branch_refused =
Operation_hash.Map.add
op.hash
(op.raw, errors)
preapp.branch_refused
in
Lwt.return ({preapp with branch_refused}, t, receipts)
| Refused errors ->
let refused =
Operation_hash.Map.add op.hash (op.raw, errors) preapp.refused
in
Lwt.return ({preapp with refused}, t, receipts)
| Outdated -> Lwt.return (preapp, t, receipts)
in
let*! ( validation_passes,
validation_result_list_rev,
receipts_rev,
validation_state ) =
List.fold_left_s
(fun ( acc_validation_passes,
acc_validation_result_rev,
receipts,
acc_validation_state )
operations ->
let*! new_validation_result, new_validation_state, rev_receipts =
List.fold_left_s
(fun (acc_validation_result, acc_validation_state, receipts)
operation ->
match parse operation with
| Error _ ->
Lwt.return
(acc_validation_result, acc_validation_state, receipts)
| Ok op ->
apply_operation_with_preapply_result
acc_validation_result
acc_validation_state
receipts
op)
(Preapply_result.empty, acc_validation_state, [])
operations
in
let new_validation_result =
{
new_validation_result with
applied = List.rev new_validation_result.applied;
}
in
Lwt.return
( acc_validation_passes + 1,
new_validation_result :: acc_validation_result_rev,
List.rev rev_receipts :: receipts,
new_validation_state ))
(0, [], [], preapply_state)
operations
in
let validation_result_list = List.rev validation_result_list_rev in
let applied_ops_metadata = List.rev receipts_rev in
let preapply_state = validation_state in
let operations_hash =
Operation_list_list_hash.compute
(List.rev_map
(fun r ->
Operation_list_hash.compute
(List.map fst r.Preapply_result.applied))
validation_result_list_rev)
in
let level = Int32.succ predecessor_shell_header.level in
let : Block_header.shell_header =
{
level;
proto_level = predecessor_shell_header.proto_level;
predecessor = predecessor_hash;
timestamp;
validation_passes;
operations_hash;
context = Context_hash.zero ;
fitness = [];
}
in
let* () = Proto.finalize_validation preapply_state.validation_state in
let* validation_result, =
Proto.finalize_application
preapply_state.application_state
(Some shell_header)
in
let*! validation_result =
may_patch_protocol
~user_activated_upgrades
~user_activated_protocol_overrides
~level
validation_result
in
let*! protocol =
Tezos_protocol_environment.Context.get_protocol validation_result.context
in
let proto_level =
if Protocol_hash.equal protocol Proto.hash then
predecessor_shell_header.proto_level
else (predecessor_shell_header.proto_level + 1) mod 256
in
let : Block_header.shell_header =
{shell_header with proto_level; fitness = validation_result.fitness}
in
let* ( validation_result,
cache,
new_protocol_env_version,
proto_expected_context_hash ) =
if Protocol_hash.equal protocol Proto.hash then
let (Tezos_protocol_environment.Context.Context {cache; _}) =
validation_result.context
in
return
( validation_result,
cache,
Proto.environment_version,
Proto.expected_context_hash )
else
match Registered_protocol.get protocol with
| None ->
tzfail
(Block_validator_errors.Unavailable_protocol
{block = predecessor_hash; protocol})
| Some (module NewProto) ->
let*? () =
check_proto_environment_version_increasing
Block_hash.zero
Proto.environment_version
NewProto.environment_version
in
let* validation_result =
NewProto.init chain_id validation_result.context shell_header
in
let (Tezos_protocol_environment.Context.Context {cache; _}) =
validation_result.context
in
let*! () =
Validation_events.(emit new_protocol_initialisation NewProto.hash)
in
return
( validation_result,
cache,
NewProto.environment_version,
NewProto.expected_context_hash )
in
let context = validation_result.context in
let resulting_context_hash =
Context_ops.hash
?message:validation_result.message
~time:timestamp
context
in
let =
match proto_expected_context_hash with
| Resulting_context -> resulting_context_hash
| Predecessor_resulting_context -> predecessor_resulting_context_hash
in
let preapply_result =
({shell_header with context = header_context_hash}, validation_result_list)
in
let* block_metadata, ops_metadata =
compute_metadata
~operation_metadata_size_limit
new_protocol_env_version
block_header_metadata
applied_ops_metadata
in
let max_operations_ttl =
max
0
(min
(predecessor_max_operations_ttl + 1)
validation_result.max_operations_ttl)
in
let result =
let validation_store =
{
resulting_context_hash;
timestamp;
message = validation_result.message;
max_operations_ttl;
last_finalized_block_level =
validation_result.last_finalized_block_level;
last_preserved_block_level =
validation_result.last_preserved_block_level;
}
in
let result =
{
shell_header_hash = hash_shell_header (fst preapply_result);
validation_store;
block_metadata;
ops_metadata;
}
in
{result; cache}
in
return (preapply_result, (result, context))
let precheck block_hash chain_id ~( : Block_header.t)
~predecessor_block_hash ~predecessor_context
~predecessor_resulting_context_hash ~cache
~( : Block_header.t) operations =
let open Lwt_result_syntax in
let* () =
check_block_header
~predecessor_block_header
~predecessor_resulting_context_hash
block_hash
block_header
in
let* = parse_block_header block_hash block_header in
let* () = check_operation_quota block_hash operations in
let*! context =
update_testchain_status
predecessor_context
~predecessor_hash:predecessor_block_hash
block_header.shell.timestamp
in
let* operations = parse_operations block_hash operations in
let* state =
Proto.begin_validation
context
chain_id
(Application block_header)
~predecessor:predecessor_block_header.shell
~cache
in
let* state =
List.fold_left_es
(fun state ops ->
List.fold_left_es
(fun state (oph, op, check_signature) ->
Proto.validate_operation ~check_signature state oph op)
state
ops)
state
operations
in
let* () = Proto.finalize_validation state in
return_unit
let precheck chain_id ~( : Block_header.t)
~predecessor_block_hash ~predecessor_context
~predecessor_resulting_context_hash ~cache
~( : Block_header.t) operations =
let block_hash = Block_header.hash block_header in
trace (invalid_block block_hash Economic_protocol_error)
@@ precheck
block_hash
chain_id
~predecessor_block_header
~predecessor_block_hash
~predecessor_context
~predecessor_resulting_context_hash
~cache
~block_header
operations
end
let assert_no_duplicate_operations block_hash live_operations operations =
let open Result_syntax in
let exception Duplicate of block_error in
try
return
(List.fold_left
(List.fold_left
(fun live_operations {hash; operation = _; check_signature = _} ->
if Operation_hash.Set.mem hash live_operations then
raise (Duplicate (Replayed_operation hash))
else Operation_hash.Set.add hash live_operations))
live_operations
operations)
with Duplicate err -> tzfail (invalid_block block_hash err)
let assert_operation_liveness block_hash live_blocks operations =
let open Result_syntax in
let exception Outdated of block_error in
try
return
(List.iter
(List.iter (fun {hash; operation; check_signature = _} ->
if
not
(Block_hash.Set.mem
operation.Operation.shell.branch
live_blocks)
then
let error =
Outdated_operation
{
operation = hash;
originating_block = operation.shell.branch;
}
in
raise (Outdated error)))
operations)
with Outdated err -> tzfail (invalid_block block_hash err)
let check_liveness ~live_blocks ~live_operations block_hash operations =
let open Result_syntax in
let* (_ : Operation_hash.Set.t) =
assert_no_duplicate_operations block_hash live_operations operations
in
assert_operation_liveness block_hash live_blocks operations
type apply_environment = {
max_operations_ttl : int;
chain_id : Chain_id.t;
predecessor_block_header : Block_header.t;
predecessor_context : Tezos_protocol_environment.Context.t;
predecessor_resulting_context_hash : Context_hash.t;
predecessor_block_metadata_hash : Block_metadata_hash.t option;
predecessor_ops_metadata_hash : Operation_metadata_list_list_hash.t option;
user_activated_upgrades : User_activated.upgrades;
user_activated_protocol_overrides : User_activated.protocol_overrides;
operation_metadata_size_limit : Shell_limits.operation_metadata_size_limit;
}
let recompute_metadata chain_id ~ ~predecessor_context
~predecessor_block_metadata_hash ~predecessor_ops_metadata_hash ~cache
block_hash operations =
let open Lwt_result_syntax in
let*! pred_protocol_hash = Context_ops.get_protocol predecessor_context in
let* (module Proto) =
Protocol_plugin.proto_with_validation_plugin ~block_hash pred_protocol_hash
in
let module Block_validation = Make (Proto) in
Block_validation.recompute_metadata
chain_id
~predecessor_block_header
~predecessor_block_metadata_hash
~predecessor_ops_metadata_hash
~predecessor_context
~cache
~block_header
operations
let recompute_metadata ~chain_id ~ ~predecessor_context
~predecessor_block_metadata_hash ~predecessor_ops_metadata_hash
~ ~operations ~cache =
let open Lwt_result_syntax in
let block_hash = Block_header.hash block_header in
let*! r =
recompute_metadata
chain_id
~predecessor_block_header
~predecessor_context
~predecessor_block_metadata_hash
~predecessor_ops_metadata_hash
~cache
block_hash
block_header
operations
in
match r with
| Error (Exn (Unix.Unix_error (errno, fn, msg)) :: _) ->
tzfail (System_error {errno = Unix.error_message errno; fn; msg})
| (Ok _ | Error _) as res -> Lwt.return res
let precheck ~chain_id ~ ~predecessor_block_hash
~predecessor_context ~predecessor_resulting_context_hash ~cache
operations =
let open Lwt_result_syntax in
let block_hash = Block_header.hash block_header in
let*! pred_protocol_hash = Context_ops.get_protocol predecessor_context in
let* (module Proto) =
Protocol_plugin.proto_with_validation_plugin ~block_hash pred_protocol_hash
in
let module Block_validation = Make (Proto) in
Block_validation.precheck
chain_id
~predecessor_block_header
~predecessor_block_hash
~predecessor_context
~predecessor_resulting_context_hash
~cache
~block_header
operations
let apply ?simulate ?cached_result ?(should_precheck = true)
{
chain_id;
user_activated_upgrades;
user_activated_protocol_overrides;
operation_metadata_size_limit;
max_operations_ttl;
;
predecessor_block_metadata_hash;
predecessor_ops_metadata_hash;
predecessor_context;
predecessor_resulting_context_hash;
} ~cache block_hash operations =
let open Lwt_result_syntax in
let*! pred_protocol_hash = Context_ops.get_protocol predecessor_context in
let* (module Proto) =
Protocol_plugin.proto_with_validation_plugin ~block_hash pred_protocol_hash
in
let* () =
if should_precheck && Proto.(compare environment_version V7 >= 0) then
precheck
~chain_id
~predecessor_block_header
~predecessor_block_hash:block_header.Block_header.shell.predecessor
~predecessor_context
~predecessor_resulting_context_hash
~cache
block_header
operations
else return_unit
in
let module Block_validation = Make (Proto) in
Block_validation.apply
?simulate
?cached_result
chain_id
~user_activated_upgrades
~user_activated_protocol_overrides
~operation_metadata_size_limit
~max_operations_ttl
~predecessor_block_header
~predecessor_block_metadata_hash
~predecessor_ops_metadata_hash
~predecessor_context
~predecessor_resulting_context_hash
~cache
~block_header
operations
let retry_with_force_loaded_cache ?(event = fun () -> Lwt.return_unit) ~cache f
=
let open Lwt_result_syntax in
let*! r =
let*! r = f ~cache in
match r with
| Error (Validation_errors.Inconsistent_hash _ :: _) ->
let*! () = event () in
f ~cache:`Force_load
| (Ok _ | Error _) as res -> Lwt.return res
in
match r with
| Error (Exn (Unix.Unix_error (errno, fn, msg)) :: _) ->
tzfail (System_error {errno = Unix.error_message errno; fn; msg})
| (Ok _ | Error _) as res -> Lwt.return res
let apply ?simulate ?cached_result ?should_precheck apply_environment ~cache
operations =
let block_hash = Block_header.hash block_header in
let event () = Event.(emit inherited_inconsistent_cache_apply) block_hash in
let f ~cache =
apply
?simulate
?cached_result
?should_precheck
apply_environment
~cache
block_hash
block_header
operations
in
retry_with_force_loaded_cache ~event ~cache f
let preapply ~chain_id ~user_activated_upgrades
~user_activated_protocol_overrides ~operation_metadata_size_limit ~timestamp
~protocol_data ~live_blocks ~live_operations ~predecessor_context
~predecessor_resulting_context_hash ~
~predecessor_hash ~predecessor_max_operations_ttl
~predecessor_block_metadata_hash ~predecessor_ops_metadata_hash ~cache
operations =
let open Lwt_result_syntax in
let*! protocol = Context_ops.get_protocol predecessor_context in
let* (module Proto) =
Protocol_plugin.proto_with_validation_plugin
~block_hash:predecessor_hash
protocol
in
let module Block_validation = Make (Proto) in
let* protocol_data =
match
Data_encoding.Binary.of_bytes_opt
Proto.block_header_data_encoding
protocol_data
with
| None -> failwith "Invalid block header"
| Some protocol_data -> return protocol_data
in
Block_validation.preapply
~chain_id
~cache
~user_activated_upgrades
~user_activated_protocol_overrides
~operation_metadata_size_limit
~protocol_data
~live_blocks
~live_operations
~timestamp
~predecessor_context
~predecessor_resulting_context_hash
~predecessor_shell_header
~predecessor_hash
~predecessor_max_operations_ttl
~predecessor_block_metadata_hash
~predecessor_ops_metadata_hash
~operations
let preapply ~chain_id ~user_activated_upgrades
~user_activated_protocol_overrides ~operation_metadata_size_limit ~timestamp
~protocol_data ~live_blocks ~live_operations ~predecessor_context
~predecessor_resulting_context_hash ~
~predecessor_hash ~predecessor_max_operations_ttl
~predecessor_block_metadata_hash ~predecessor_ops_metadata_hash ~cache
operations =
let event () =
Event.(emit inherited_inconsistent_cache_preapply) predecessor_hash
in
let f ~cache =
preapply
~chain_id
~user_activated_upgrades
~user_activated_protocol_overrides
~operation_metadata_size_limit
~timestamp
~protocol_data
~live_blocks
~live_operations
~predecessor_context
~predecessor_resulting_context_hash
~predecessor_shell_header
~predecessor_hash
~predecessor_max_operations_ttl
~predecessor_block_metadata_hash
~predecessor_ops_metadata_hash
~cache
operations
in
retry_with_force_loaded_cache ~event ~cache f