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
let =
Alpha_context.Block_header.protocol_data_encoding
let =
Apply_results.block_metadata_encoding_with_legacy_attestation_name
let = Apply_results.block_metadata_encoding
type operation_data = Alpha_context.packed_protocol_data =
| Operation_data :
'kind Alpha_context.Operation.protocol_data
-> operation_data
let operation_data_encoding = Alpha_context.Operation.protocol_data_encoding
let operation_data_encoding_with_legacy_attestation_name =
Alpha_context.Operation.protocol_data_encoding_with_legacy_attestation_name
type operation_receipt = Apply_results.packed_operation_metadata =
| Operation_metadata :
'kind Apply_results.operation_metadata
-> operation_receipt
| No_operation_metadata : operation_receipt
let operation_receipt_encoding = Apply_results.operation_metadata_encoding
let operation_receipt_encoding_with_legacy_attestation_name =
Apply_results.operation_metadata_encoding_with_legacy_attestation_name
let operation_data_and_receipt_encoding =
Apply_results.operation_data_and_metadata_encoding
let operation_data_and_receipt_encoding_with_legacy_attestation_name =
Apply_results
.operation_data_and_metadata_encoding_with_legacy_attestation_name
type operation = Alpha_context.packed_operation = {
shell : Operation.shell_header;
protocol_data : operation_data;
}
let acceptable_pass = Alpha_context.Operation.acceptable_pass
let max_block_length = Alpha_context.Block_header.max_header_length
let max_operation_data_length =
Alpha_context.Constants.max_operation_data_length
let validation_passes =
let open Alpha_context.Constants in
Updater.
[
{max_size = 2048 * 2048; max_op = Some 2048};
{max_size = 32 * 1024; max_op = None};
{
max_size = max_anon_ops_per_block * 1024;
max_op = Some max_anon_ops_per_block;
};
{max_size = 512 * 1024; max_op = None};
]
let rpc_services =
Alpha_services.register () ;
Services_registration.get_rpc_services ()
type validation_state = Validate.validation_state
type application_state = Apply.application_state
(** Circumstances and relevant information for [begin_validation] and
[begin_application] below. *)
type mode =
| Application of block_header
| Partial_validation of block_header
| Construction of {
predecessor_hash : Block_hash.t;
timestamp : Time.t;
block_header_data : block_header_data;
}
| Partial_construction of {
predecessor_hash : Block_hash.t;
timestamp : Time.t;
}
(** Initialize the consensus rights by first slot for modes that are
about the validation/application of a block: application, partial
validation, and full construction.
In these modes, attestations must point to the predecessor's level
and preattestations, if any, to the block's level. *)
let init_consensus_rights_for_block ctxt mode ~predecessor_level =
let open Lwt_result_syntax in
let open Alpha_context in
let* ctxt, attestations_map =
Baking.attesting_rights_by_first_slot ctxt predecessor_level
in
let*? can_contain_preattestations =
match mode with
| Construction _ | Partial_construction _ -> ok true
| Application | Partial_validation ->
let open Result_syntax in
let* locked_round =
Fitness.locked_round_from_raw block_header.shell.fitness
in
return (Option.is_some locked_round)
in
let* ctxt, allowed_preattestations =
if can_contain_preattestations then
let* ctxt, preattestations_map =
Baking.attesting_rights_by_first_slot ctxt (Level.current ctxt)
in
return (ctxt, Some preattestations_map)
else return (ctxt, None)
in
let ctxt =
Consensus.initialize_consensus_operation
ctxt
~allowed_attestations:(Some attestations_map)
~allowed_preattestations
in
return ctxt
(** Initialize the consensus rights for a mempool (partial
construction mode).
In the mempool, there are three allowed levels for both
attestations and preattestations: [predecessor_level - 1] (aka the
grandparent's level), [predecessor_level] (that is, the level of
the mempool's head), and [predecessor_level + 1] (aka the current
level in ctxt). *)
let init_consensus_rights_for_mempool ctxt ~predecessor_level =
let open Lwt_result_syntax in
let open Alpha_context in
let ctxt =
Consensus.initialize_consensus_operation
ctxt
~allowed_attestations:None
~allowed_preattestations:None
in
let cycle = (Level.current ctxt).cycle in
let* ctxt = Stake_distribution.load_sampler_for_cycle ctxt cycle in
match Level.pred ctxt predecessor_level with
| Some gp_level when Cycle.(gp_level.cycle <> cycle) ->
Stake_distribution.load_sampler_for_cycle ctxt gp_level.cycle
| Some _ | None -> return ctxt
let prepare_ctxt ctxt mode ~(predecessor : Block_header.shell_header) =
let open Lwt_result_syntax in
let open Alpha_context in
let level, timestamp =
match mode with
| Application | Partial_validation ->
(block_header.shell.level, block_header.shell.timestamp)
| Construction {timestamp; _} | Partial_construction {timestamp; _} ->
(Int32.succ predecessor.level, timestamp)
in
let* ctxt, migration_balance_updates, migration_operation_results =
prepare ctxt ~level ~predecessor_timestamp:predecessor.timestamp ~timestamp
in
let*? predecessor_raw_level = Raw_level.of_int32 predecessor.level in
let predecessor_level = Level.from_raw ctxt predecessor_raw_level in
let* ctxt = Delegate.prepare_stake_distribution ctxt in
let* ctxt =
match mode with
| Application _ | Partial_validation _ | Construction _ ->
init_consensus_rights_for_block ctxt mode ~predecessor_level
| Partial_construction _ ->
init_consensus_rights_for_mempool ctxt ~predecessor_level
in
Dal_apply.initialisation ~level:predecessor_level ctxt >>=? fun ctxt ->
return
( ctxt,
migration_balance_updates,
migration_operation_results,
predecessor_level )
let begin_validation ctxt chain_id mode ~predecessor =
let open Lwt_result_syntax in
let open Alpha_context in
let* ( ctxt,
_migration_balance_updates,
_migration_operation_results,
predecessor_level ) =
prepare_ctxt ctxt ~predecessor mode
in
let predecessor_timestamp = predecessor.timestamp in
let predecessor_fitness = predecessor.fitness in
match mode with
| Application ->
let*? fitness = Fitness.from_raw block_header.shell.fitness in
Validate.begin_application
ctxt
chain_id
~predecessor_level
~predecessor_timestamp
block_header
fitness
| Partial_validation ->
let*? fitness = Fitness.from_raw block_header.shell.fitness in
Validate.begin_partial_validation
ctxt
chain_id
~predecessor_level
~predecessor_timestamp
block_header
fitness
| Construction {predecessor_hash; timestamp; } ->
let*? predecessor_round = Fitness.round_from_raw predecessor_fitness in
let*? round =
Round.round_of_timestamp
(Constants.round_durations ctxt)
~predecessor_timestamp
~predecessor_round
~timestamp
in
Validate.begin_full_construction
ctxt
chain_id
~predecessor_level
~predecessor_round
~predecessor_timestamp
~predecessor_hash
round
block_header_data.contents
| Partial_construction _ ->
let*? predecessor_round = Fitness.round_from_raw predecessor_fitness in
return
(Validate.begin_partial_construction
ctxt
chain_id
~predecessor_level
~predecessor_round)
let validate_operation = Validate.validate_operation
let finalize_validation = Validate.finalize_block
type error += Cannot_apply_in_partial_validation
let () =
register_error_kind
`Permanent
~id:"main.begin_application.cannot_apply_in_partial_validation"
~title:"cannot_apply_in_partial_validation"
~description:
"Cannot instantiate an application state using the 'Partial_validation' \
mode."
~pp:(fun ppf () ->
Format.fprintf
ppf
"Cannot instantiate an application state using the \
'Partial_validation' mode.")
Data_encoding.(empty)
(function Cannot_apply_in_partial_validation -> Some () | _ -> None)
(fun () -> Cannot_apply_in_partial_validation)
let begin_application ctxt chain_id mode ~predecessor =
let open Lwt_result_syntax in
let open Alpha_context in
let* ( ctxt,
migration_balance_updates,
migration_operation_results,
predecessor_level ) =
prepare_ctxt ctxt ~predecessor mode
in
let predecessor_timestamp = predecessor.timestamp in
let predecessor_fitness = predecessor.fitness in
match mode with
| Application ->
Apply.begin_application
ctxt
chain_id
~migration_balance_updates
~migration_operation_results
~predecessor_fitness
block_header
| Partial_validation _ -> tzfail Cannot_apply_in_partial_validation
| Construction {predecessor_hash; timestamp; ; _} ->
let*? predecessor_round = Fitness.round_from_raw predecessor_fitness in
Apply.begin_full_construction
ctxt
chain_id
~migration_balance_updates
~migration_operation_results
~predecessor_timestamp
~predecessor_level
~predecessor_round
~predecessor_hash
~timestamp
block_header_data.contents
| Partial_construction {predecessor_hash; _} ->
Apply.begin_partial_construction
ctxt
chain_id
~migration_balance_updates
~migration_operation_results
~predecessor_hash
~predecessor_fitness
let apply_operation = Apply.apply_operation
let finalize_application = Apply.finalize_block
let compare_operations (oph1, op1) (oph2, op2) =
Alpha_context.Operation.compare (oph1, op1) (oph2, op2)
let init chain_id ctxt =
let level = block_header.Block_header.level in
let timestamp = block_header.timestamp in
let predecessor = block_header.predecessor in
let typecheck_smart_contract (ctxt : Alpha_context.context)
(script : Alpha_context.Script.t) =
let allow_forged_in_storage =
false
in
Script_ir_translator.parse_script
ctxt
~elab_conf:Script_ir_translator_config.(make ~legacy:true ())
~allow_forged_in_storage
script
>>=? fun (Ex_script (Script parsed_script), ctxt) ->
Script_ir_translator.extract_lazy_storage_diff
ctxt
Optimized
parsed_script.storage_type
parsed_script.storage
~to_duplicate:Script_ir_translator.no_lazy_storage_id
~to_update:Script_ir_translator.no_lazy_storage_id
~temporary:false
>>=? fun (storage, lazy_storage_diff, ctxt) ->
Script_ir_translator.unparse_data
ctxt
Optimized
parsed_script.storage_type
storage
>|=? fun (storage, ctxt) ->
let storage = Alpha_context.Script.lazy_expr storage in
(({script with storage}, lazy_storage_diff), ctxt)
in
Alpha_context.Raw_level.of_int32 level >>?= fun raw_level ->
let init_fitness =
Alpha_context.Fitness.create_without_locked_round
~level:raw_level
~round:Alpha_context.Round.zero
~predecessor_round:Alpha_context.Round.zero
in
Alpha_context.prepare_first_block
chain_id
~typecheck_smart_contract
~typecheck_smart_rollup:Sc_rollup_operations.validate_untyped_parameters_ty
~level
~timestamp
~predecessor
ctxt
>>=? fun ctxt ->
let cache_nonce =
Alpha_context.Cache.cache_nonce_from_block_header
block_header
({
payload_hash = Block_payload_hash.zero;
payload_round = Alpha_context.Round.zero;
per_block_votes =
{
liquidity_baking_vote =
Alpha_context.Per_block_votes.Per_block_vote_pass;
adaptive_issuance_vote =
Alpha_context.Per_block_votes.Per_block_vote_pass;
};
seed_nonce_hash = None;
proof_of_work_nonce =
Bytes.make Constants_repr.proof_of_work_nonce_size '0';
}
: Alpha_context.Block_header.contents)
in
Alpha_context.Cache.Admin.sync ctxt cache_nonce >>= fun ctxt ->
return
(Alpha_context.finalize ctxt (Alpha_context.Fitness.to_raw init_fitness))
let value_of_key ~chain_id:_ ~predecessor_context:ctxt ~predecessor_timestamp
~predecessor_level:pred_level ~predecessor_fitness:_ ~predecessor:_
~timestamp =
let level = Int32.succ pred_level in
Alpha_context.prepare ctxt ~level ~predecessor_timestamp ~timestamp
>>=? fun (ctxt, _, _) -> return (Apply.value_of_key ctxt)
module Mempool = struct
include Mempool_validation
let init ctxt chain_id ~head_hash ~(head : Block_header.shell_header) =
let open Lwt_result_syntax in
let open Alpha_context in
let* ( ctxt,
_migration_balance_updates,
_migration_operation_results,
head_level ) =
prepare_ctxt
ctxt
(Partial_construction
{predecessor_hash = head_hash; timestamp = head.timestamp})
~predecessor:head
in
let*? predecessor_round = Fitness.round_from_raw head.fitness in
return
(init
ctxt
chain_id
~predecessor_level:head_level
~predecessor_round
~predecessor_hash:head_hash)
end