Source file incremental.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
open Protocol
module Proto_Nonce = Nonce
open Alpha_context
type t = {
predecessor : Block.t;
state : validation_state * application_state;
rev_operations : Operation.packed list;
rev_tickets : operation_receipt list;
header : Block_header.t;
delegate : Account.t;
constants : Constants.Parametric.t;
}
type incremental = t
let predecessor {predecessor; _} = predecessor
let {; _} = header
let rev_tickets {rev_tickets; _} = rev_tickets
let validation_state {state = vs, _; _} = vs
let level st = st.header.shell.level
let alpha_ctxt {state = _, application_state; _} = application_state.ctxt
let rpc_context st =
let fitness = (header st).shell.fitness in
let result = Alpha_context.finalize (alpha_ctxt st) fitness in
{
Environment.Updater.block_hash = Block_hash.zero;
block_header = {st.header.shell with fitness = result.fitness};
context = result.context;
}
let rpc_ctxt =
new Environment.proto_rpc_context_of_directory
rpc_context
Plugin.RPC.rpc_services
let set_alpha_ctxt st ctxt =
{st with state = (fst st.state, {(snd st.state) with ctxt})}
let begin_validation_and_application ctxt chain_id mode ~predecessor =
let open Lwt_result_syntax in
let* validation_state = begin_validation ctxt chain_id mode ~predecessor in
let* application_state = begin_application ctxt chain_id mode ~predecessor in
return (validation_state, application_state)
let begin_construction ?timestamp ?seed_nonce_hash ?(mempool_mode = false)
?(policy = Block.By_round 0) ?liquidity_baking_toggle_vote
?adaptive_issuance_vote (predecessor : Block.t) =
let open Lwt_result_wrap_syntax in
let* delegate, _consensus_key, round, real_timestamp =
Block.get_next_baker ~policy predecessor
in
let* delegate = Account.find delegate in
let*?@ payload_round = Round.of_int round in
let timestamp = Option.value ~default:real_timestamp timestamp in
let* seed_nonce_hash =
match seed_nonce_hash with
| Some _hash -> return seed_nonce_hash
| None -> (
let+ level =
Plugin.RPC.current_level ~offset:1l Block.rpc_ctxt predecessor
in
match level with
| {expected_commitment = true; _} ->
Some (fst (Proto_Nonce.generate ()))
| {expected_commitment = false; _} -> None)
in
let shell : Block_header.shell_header =
{
predecessor = predecessor.hash;
proto_level = predecessor.header.shell.proto_level;
validation_passes = predecessor.header.shell.validation_passes;
fitness = predecessor.header.shell.fitness;
timestamp;
level = predecessor.header.shell.level;
context = Context_hash.zero;
operations_hash = Operation_list_list_hash.zero;
}
in
let* contents =
Block.Forge.contents
?seed_nonce_hash
?adaptive_issuance_vote
?liquidity_baking_toggle_vote
~payload_hash:Block_payload_hash.zero
~payload_round
shell
in
let mode =
if mempool_mode then
Partial_construction {predecessor_hash = predecessor.hash; timestamp}
else
let =
{Block_header.contents; signature = Signature.zero}
in
Construction
{predecessor_hash = predecessor.hash; timestamp; block_header_data}
in
let =
{Block_header.shell; protocol_data = {contents; signature = Signature.zero}}
in
let*@ state =
begin_validation_and_application
predecessor.context
Chain_id.zero
mode
~predecessor:predecessor.header.shell
in
return
{
predecessor;
state;
rev_operations = [];
rev_tickets = [];
header;
delegate;
constants = predecessor.constants;
}
let detect_script_failure :
type kind. kind Apply_results.operation_metadata -> _ =
let open Result_syntax in
let rec detect_script_failure :
type kind. kind Apply_results.contents_result_list -> _ =
let open Apply_results in
let open Apply_operation_result in
let open Apply_internal_results in
let detect_script_failure_single (type kind)
(Manager_operation_result
{operation_result; internal_operation_results; _} :
kind Kind.manager Apply_results.contents_result) =
let detect_script_failure (type kind)
(result : (kind, _, _) operation_result) =
match result with
| Applied _ -> return_unit
| Skipped _ -> assert false
| Backtracked (_, None) ->
return_unit
| Backtracked (_, Some errs) -> fail (Environment.wrap_tztrace errs)
| Failed (_, errs) -> fail (Environment.wrap_tztrace errs)
in
let* () = detect_script_failure operation_result in
List.iter_e
(fun (Internal_operation_result (_, r)) -> detect_script_failure r)
internal_operation_results
in
function
| Single_result (Manager_operation_result _ as res) ->
detect_script_failure_single res
| Single_result _ -> return_unit
| Cons_result (res, rest) ->
let* () = detect_script_failure_single res in
detect_script_failure rest
in
fun {contents} -> detect_script_failure contents
let check_operation_size ?(check_size = true) op =
if check_size then
let operation_size =
Data_encoding.Binary.length
Operation.encoding_with_legacy_attestation_name
op
in
if operation_size > Constants_repr.max_operation_data_length then
raise
(invalid_arg
(Format.sprintf
"The operation size is %d: it exceeds the constant maximum size \
%d."
operation_size
Constants_repr.max_operation_data_length))
let validate_operation ?expect_failure ?check_size st op =
let open Lwt_result_wrap_syntax in
check_operation_size ?check_size op ;
let validation_state, application_state = st.state in
let oph = Operation.hash_packed op in
let*!@ res = validate_operation validation_state oph op in
match (expect_failure, res) with
| Some _, Ok _ -> failwith "Error expected while validating operation"
| Some f, Error err ->
let* () = f err in
return st
| None, Error err -> fail err
| None, Ok validation_state ->
return {st with state = (validation_state, application_state)}
let add_operation ?expect_failure ?expect_apply_failure ?allow_manager_failure
?check_size st op =
let open Lwt_result_wrap_syntax in
let open Apply_results in
let* st = validate_operation ?expect_failure ?check_size st op in
match expect_failure with
| Some _ ->
return st
| None -> (
let validation_state, application_state = st.state in
let oph = Operation.hash_packed op in
let*@ application_state, metadata =
apply_operation application_state oph op
in
let st =
{
st with
state = (validation_state, application_state);
rev_operations = op :: st.rev_operations;
rev_tickets = metadata :: st.rev_tickets;
}
in
match allow_manager_failure with
| Some true -> return st
| None | Some false -> (
match (expect_apply_failure, metadata) with
| None, No_operation_metadata -> return st
| None, Operation_metadata result ->
let*? () = detect_script_failure result in
return st
| Some _, No_operation_metadata ->
failwith "Error expected while adding operation"
| Some f, Operation_metadata result -> (
match detect_script_failure result with
| Ok _ -> failwith "Error expected while adding operation"
| Error err ->
let* () = f err in
return st)))
let finalize_validation_and_application (validation_state, application_state)
=
let open Lwt_result_syntax in
let* () = finalize_validation validation_state in
finalize_application application_state shell_header
let finalize_block st =
let open Lwt_result_wrap_syntax in
let operations = List.rev st.rev_operations in
let operations_hash =
Operation_list_list_hash.compute
[Operation_list_hash.compute (List.map Operation.hash_packed operations)]
in
let =
{
st.header.shell with
level = Int32.succ st.header.shell.level;
operations_hash;
}
in
let*@ validation_result, _ =
finalize_validation_and_application st.state (Some shell_header)
in
let operations = List.rev st.rev_operations in
let operations_hash =
Operation_list_list_hash.compute
[Operation_list_hash.compute (List.map Operation.hash_packed operations)]
in
let =
{
st.header with
shell =
{
st.header.shell with
level = Int32.succ st.header.shell.level;
operations_hash;
fitness = validation_result.fitness;
};
}
in
let hash = Block_header.hash header in
return
{
Block.hash;
header;
operations;
context = validation_result.context;
constants = st.constants;
}
let assert_validate_operation_fails expect_failure op block =
let open Lwt_result_syntax in
let* i = begin_construction block in
let* (_i : incremental) = validate_operation ~expect_failure i op in
return_unit