Source file operation_pool.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
open Protocol
open Alpha_context
type 'collection t = {
consensus : 'collection;
votes : 'collection;
anonymous : 'collection;
managers : 'collection;
}
let compare_op op1 op2 =
try Stdlib.compare op1 op2
with _ ->
Operation_hash.compare
(Alpha_context.Operation.hash_packed op1)
(Alpha_context.Operation.hash_packed op2)
module Prioritized_operation = struct
type t = Prioritized of int * packed_operation | Low of packed_operation
let extern ?(priority = 1) op = Prioritized (priority, op)
let node op = Low op
let packed = function Prioritized (_, op) | Low op -> op
let compare_priority t1 t2 =
match (t1, t2) with
| Prioritized _, Low _ -> 1
| Low _, Prioritized _ -> -1
| Low _, Low _ -> 0
| Prioritized (p0, _), Prioritized (p1, _) -> Compare.Int.compare p0 p1
let compare a b =
let c = compare_priority a b in
if c <> 0 then c else compare_op (packed a) (packed b)
end
module Operation_set = Set.Make (struct
type t = packed_operation
let compare = compare_op
end)
module Prioritized_operation_set = struct
include Set.Make (struct
type t = Prioritized_operation.t
let compare = Prioritized_operation.compare
end)
let operations set = elements set |> List.map Prioritized_operation.packed
end
type pool = Operation_set.t t
type ordered_pool = packed_operation list t
let ordered_pool_encoding =
let open Data_encoding in
conv
(fun {consensus; votes; anonymous; managers} ->
(consensus, votes, anonymous, managers))
(fun (consensus, votes, anonymous, managers) ->
{consensus; votes; anonymous; managers})
(obj4
(req
"ordered_consensus"
(list (dynamic_size Operation.encoding_with_legacy_attestation_name)))
(req
"ordered_votes"
(list (dynamic_size Operation.encoding_with_legacy_attestation_name)))
(req
"ordered_anonymouns"
(list (dynamic_size Operation.encoding_with_legacy_attestation_name)))
(req
"ordered_managers"
(list (dynamic_size Operation.encoding_with_legacy_attestation_name))))
type payload = {
votes_payload : packed_operation list;
anonymous_payload : packed_operation list;
managers_payload : packed_operation list;
}
let empty_payload =
{votes_payload = []; anonymous_payload = []; managers_payload = []}
let payload_encoding =
let open Data_encoding in
conv
(fun {votes_payload; anonymous_payload; managers_payload} ->
(votes_payload, anonymous_payload, managers_payload))
(fun (votes_payload, anonymous_payload, managers_payload) ->
{votes_payload; anonymous_payload; managers_payload})
(obj3
(req
"votes_payload"
(list (dynamic_size Operation.encoding_with_legacy_attestation_name)))
(req
"anonymous_payload"
(list (dynamic_size Operation.encoding_with_legacy_attestation_name)))
(req
"managers_payload"
(list (dynamic_size Operation.encoding_with_legacy_attestation_name))))
let pp_payload fmt {votes_payload; anonymous_payload; managers_payload} =
Format.fprintf
fmt
"[votes: %d, anonymous: %d, managers: %d]"
(List.length votes_payload)
(List.length anonymous_payload)
(List.length managers_payload)
let empty =
{
consensus = Operation_set.empty;
votes = Operation_set.empty;
anonymous = Operation_set.empty;
managers = Operation_set.empty;
}
let empty_ordered = {consensus = []; votes = []; anonymous = []; managers = []}
let pp_pool fmt {consensus; votes; anonymous; managers} =
Format.fprintf
fmt
"[consensus: %d, votes: %d, anonymous: %d, managers: %d]"
(Operation_set.cardinal consensus)
(Operation_set.cardinal votes)
(Operation_set.cardinal anonymous)
(Operation_set.cardinal managers)
let pp_ordered_pool fmt {consensus; votes; anonymous; managers} =
Format.fprintf
fmt
"[consensus: %d, votes: %d, anonymous: %d, managers: %d]"
(List.length consensus)
(List.length votes)
(List.length anonymous)
(List.length managers)
let classify op =
let pass = Main.acceptable_pass op in
match pass with
| None -> `Bad
| Some pass ->
let open Operation_repr in
if pass = consensus_pass then `Consensus
else if pass = voting_pass then `Votes
else if pass = anonymous_pass then `Anonymous
else if pass = manager_pass then `Managers
else `Bad
let add_operation_to_pool add classify pool operation =
match classify operation with
| `Consensus ->
let consensus = add operation pool.consensus in
{pool with consensus}
| `Votes ->
let votes = add operation pool.votes in
{pool with votes}
| `Anonymous ->
let anonymous = add operation pool.anonymous in
{pool with anonymous}
| `Managers ->
let managers = add operation pool.managers in
{pool with managers}
| `Bad -> pool
let add_operation = add_operation_to_pool Operation_set.add classify
let add_operations pool ops = List.fold_left add_operation pool ops
type consensus_filter = {
level : int32;
round : Round.t;
payload_hash : Block_payload_hash.t;
}
(** From a pool of operations [operation_pool], the function filters
out the attestations that are different from the [current_level],
the [current_round] or the optional [current_block_payload_hash],
as well as preattestations. *)
let filter_with_relevant_consensus_ops ~(attestation_filter : consensus_filter)
~(preattestation_filter : consensus_filter option) operation_set =
Operation_set.filter
(fun {protocol_data; _} ->
match (protocol_data, preattestation_filter) with
| Operation_data {contents = Single (Preattestation _); _}, None -> false
| ( Operation_data
{
contents =
Single (Preattestation {level; round; block_payload_hash; _});
_;
},
Some
{level = level'; round = round'; payload_hash = block_payload_hash'}
) ->
Compare.Int32.(Raw_level.to_int32 level = level')
&& Round.(round = round')
&& Block_payload_hash.(block_payload_hash = block_payload_hash')
| ( Operation_data
{
contents =
Single (Attestation {level; round; block_payload_hash; _});
_;
},
_ ) ->
Compare.Int32.(Raw_level.to_int32 level = attestation_filter.level)
&& Round.(round = attestation_filter.round)
&& Block_payload_hash.(
block_payload_hash = attestation_filter.payload_hash)
| _ -> true)
operation_set
let unpack_preattestation packed_preattestation =
let {shell; protocol_data = Operation_data data} = packed_preattestation in
match data with
| {contents = Single (Preattestation _); _} ->
Some ({shell; protocol_data = data} : Kind.preattestation Operation.t)
| _ -> None
let unpack_attestation packed_attestation =
let {shell; protocol_data = Operation_data data} = packed_attestation in
match data with
| {contents = Single (Attestation _); _} ->
Some ({shell; protocol_data = data} : Kind.attestation Operation.t)
| _ -> None
let unpack_dal_attestation packed_dal_attestation =
let {shell; protocol_data = Operation_data data} = packed_dal_attestation in
match data with
| {contents = Single (Dal_attestation _); _} ->
Some ({shell; protocol_data = data} : Kind.dal_attestation Operation.t)
| _ -> None
let filter_preattestations ops =
List.filter_map
(function
| {
shell = {branch};
protocol_data =
Operation_data
({contents = Single (Preattestation _); _} as content);
_;
} ->
Some
({shell = {branch}; protocol_data = content}
: Kind.preattestation operation)
| _ -> None)
ops
let filter_attestations ops =
List.filter_map
(function
| {
shell = {branch};
protocol_data =
Operation_data ({contents = Single (Attestation _); _} as content);
_;
} ->
Some
({shell = {branch}; protocol_data = content}
: Kind.attestation operation)
| _ -> None)
ops
let ordered_to_list_list {consensus; votes; anonymous; managers} =
[consensus; votes; anonymous; managers]
let ordered_of_list_list = function
| [consensus; votes; anonymous; managers] ->
Some {consensus; votes; anonymous; managers}
| _ -> None
let payload_of_ordered_pool {votes; anonymous; managers; _} =
{
votes_payload = votes;
anonymous_payload = anonymous;
managers_payload = managers;
}
let ordered_pool_of_payload ~consensus_operations
{votes_payload; anonymous_payload; managers_payload} =
{
consensus = consensus_operations;
votes = votes_payload;
anonymous = anonymous_payload;
managers = managers_payload;
}
let = function
| [consensus; votes_payload; anonymous_payload; managers_payload] ->
let preattestations, attestations, dal_attestations =
List.fold_left
(fun ( (preattestations : Kind.preattestation Operation.t list),
(attestations : Kind.attestation Operation.t list),
(dal_attestations : Kind.dal_attestation Operation.t list) )
packed_op ->
let {shell; protocol_data = Operation_data data} = packed_op in
match data with
| {contents = Single (Preattestation _); _} ->
( {shell; protocol_data = data} :: preattestations,
attestations,
dal_attestations )
| {contents = Single (Attestation _); _} ->
( preattestations,
{shell; protocol_data = data} :: attestations,
dal_attestations )
| {contents = Single (Dal_attestation _); _} ->
( preattestations,
attestations,
{shell; protocol_data = data} :: dal_attestations )
| _ ->
(preattestations, attestations, dal_attestations))
([], [], [])
consensus
in
let preattestations =
if preattestations = [] then None else Some preattestations
in
let payload = {votes_payload; anonymous_payload; managers_payload} in
Some (preattestations, attestations, dal_attestations, payload)
| _ -> None
let filter_pool p {consensus; votes; anonymous; managers} =
{
consensus = Operation_set.filter p consensus;
votes = Operation_set.filter p votes;
anonymous = Operation_set.filter p anonymous;
managers = Operation_set.filter p managers;
}
module Prioritized = struct
type nonrec t = Prioritized_operation_set.t t
let of_operation_set (operation_set : Operation_set.t) =
Operation_set.fold
(fun elt set ->
Prioritized_operation_set.add (Prioritized_operation.node elt) set)
operation_set
Prioritized_operation_set.empty
let of_pool (pool : pool) : t =
{
consensus = of_operation_set pool.consensus;
votes = of_operation_set pool.votes;
anonymous = of_operation_set pool.anonymous;
managers = of_operation_set pool.managers;
}
let add_operation =
add_operation_to_pool Prioritized_operation_set.add (fun op ->
classify (Prioritized_operation.packed op))
let add_external_operation pool priority operation =
add_operation pool (Prioritized_operation.extern ~priority operation)
let add_operations prioritized_pool operations =
List.fold_left add_operation prioritized_pool operations
let merge_external_operations pool
(external_operations : packed_operation list) =
List.fold_left_i
(fun i pool op -> add_external_operation pool (-i) op)
pool
external_operations
let filter p {consensus; votes; anonymous; managers} =
let filter =
Prioritized_operation_set.filter (fun pop ->
p (Prioritized_operation.packed pop))
in
{
consensus = filter consensus;
votes = filter votes;
anonymous = filter anonymous;
managers = filter managers;
}
end