Source file baking_lib.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
open Protocol
open Alpha_context
open Baking_state
let create_state cctxt ?synchronize ?monitor_node_mempool ~config
~current_proposal delegates =
let open Lwt_result_syntax in
let chain = cctxt#chain in
let monitor_node_operations = monitor_node_mempool in
let*! operation_worker =
Operation_worker.create ?monitor_node_operations cctxt
in
Baking_scheduling.create_initial_state
cctxt
?synchronize
~chain
config
operation_worker
~current_proposal
delegates
let get_current_proposal cctxt ?cache () =
let open Lwt_result_syntax in
let* block_stream, _block_stream_stopper =
Node_rpc.monitor_heads cctxt ?cache ~chain:cctxt#chain ()
in
let*! current_head = Lwt_stream.peek block_stream in
match current_head with
| Some current_head -> return (block_stream, current_head)
| None -> failwith "head stream unexpectedly ended"
module Events = Baking_events.Lib
let preattest (cctxt : Protocol_client_context.full) ?(force = false) delegates
=
let open State_transitions in
let open Lwt_result_syntax in
let cache = Baking_cache.Block_cache.create 10 in
let* _, current_proposal = get_current_proposal cctxt ~cache () in
let config = Baking_configuration.make ~force () in
let* state = create_state cctxt ~config ~current_proposal delegates in
let proposal = state.level_state.latest_proposal in
let*! () =
Events.(
emit attempting_preattest_proposal state.level_state.latest_proposal)
in
let* () =
if force then return_unit
else
let*! proposal_acceptance =
is_acceptable_proposal_for_current_level state proposal
in
match proposal_acceptance with
| Invalid -> cctxt#error "Cannot preattest an invalid proposal"
| Outdated_proposal -> cctxt#error "Cannot preattest an outdated proposal"
| Valid_proposal -> return_unit
in
let consensus_list = make_consensus_list state proposal in
let*! () =
cctxt#message
"@[<v 2>Preattesting for:@ %a@]"
Format.(
pp_print_list
~pp_sep:pp_print_space
Baking_state.pp_consensus_key_and_delegate)
(List.map fst consensus_list)
in
Baking_actions.inject_consensus_vote state consensus_list `Preattestation
let attest (cctxt : Protocol_client_context.full) ?(force = false) delegates =
let open State_transitions in
let open Lwt_result_syntax in
let cache = Baking_cache.Block_cache.create 10 in
let* _, current_proposal = get_current_proposal cctxt ~cache () in
let config = Baking_configuration.make ~force () in
let* state = create_state cctxt ~config ~current_proposal delegates in
let proposal = state.level_state.latest_proposal in
let*! () =
Events.(emit attempting_attest_proposal state.level_state.latest_proposal)
in
let* () =
if force then return_unit
else
let*! proposal_acceptance =
is_acceptable_proposal_for_current_level state proposal
in
match proposal_acceptance with
| Invalid -> cctxt#error "Cannot attest an invalid proposal"
| Outdated_proposal -> cctxt#error "Cannot attest an outdated proposal"
| Valid_proposal -> return_unit
in
let consensus_list = make_consensus_list state proposal in
let*! () =
cctxt#message
"@[<v 2>Attesting for:@ %a@]"
Format.(
pp_print_list
~pp_sep:pp_print_space
Baking_state.pp_consensus_key_and_delegate)
(List.map fst consensus_list)
in
let* () =
Baking_state.may_record_new_state ~previous_state:state ~new_state:state
in
Baking_actions.inject_consensus_vote state consensus_list `Attestation
let bake_at_next_level state =
let open Lwt_result_syntax in
let cctxt = state.global_state.cctxt in
let*! baking_time =
Baking_scheduling.compute_next_potential_baking_time_at_next_level state
in
match baking_time with
| None -> cctxt#error "No baking slot found for the delegates"
| Some (timestamp, round) ->
let*! () =
cctxt#message
"Waiting until %a for round %a"
Timestamp.pp
timestamp
Round.pp
round
in
let*! () =
Option.value
~default:Lwt.return_unit
(Baking_scheduling.sleep_until timestamp)
in
return (Baking_state.Timeout (Time_to_bake_next_level {at_round = round}))
let first_automaton_event state =
match state.level_state.elected_block with
| None -> Lwt.return (Baking_scheduling.compute_bootstrap_event state)
| Some _elected_block ->
bake_at_next_level state
let attestations_attesting_power state attestations =
let get_attestation_voting_power {slot; _} =
match
Delegate_slots.voting_power state.level_state.delegate_slots ~slot
with
| None -> 0
| Some attesting_power -> attesting_power
in
List.sort_uniq compare attestations
|> List.fold_left
(fun power attestation ->
power + get_attestation_voting_power attestation)
0
let generic_attesting_power (filter : packed_operation list -> 'a list)
( : 'a -> consensus_content) state =
let current_mempool =
Operation_worker.get_current_operations state.global_state.operation_worker
in
let latest_proposal = state.level_state.latest_proposal in
let block_round = latest_proposal.block.round in
let shell_level = latest_proposal.block.shell.level in
let attestations =
filter (Operation_pool.Operation_set.elements current_mempool.consensus)
in
let attestations_in_mempool =
List.filter_map
(fun v ->
let consensus_content = extract v in
if
Round.(consensus_content.round = block_round)
&& Compare.Int32.(
Raw_level.to_int32 consensus_content.level = shell_level)
then Some consensus_content
else None)
attestations
in
let power = attestations_attesting_power state attestations_in_mempool in
(power, attestations)
let state_attesting_power =
generic_attesting_power
Operation_pool.filter_attestations
(fun
({
protocol_data = {contents = Single (Attestation consensus_content); _};
_;
} :
Kind.attestation operation)
-> consensus_content)
let do_action (state, action) =
let state_recorder ~new_state =
Baking_state.may_record_new_state ~previous_state:state ~new_state
in
Baking_actions.perform_action ~state_recorder state action
let propose_at_next_level ~minimal_timestamp state =
let open Lwt_result_syntax in
let cctxt = state.global_state.cctxt in
assert (Option.is_some state.level_state.elected_block) ;
if minimal_timestamp then
let* minimal_round, delegate =
match
Baking_scheduling.first_potential_round_at_next_level
state
~earliest_round:Round.zero
with
| None -> cctxt#error "No potential baking slot for the given delegates."
| Some first_potential_round -> return first_potential_round
in
let pool =
Operation_worker.get_current_operations
state.global_state.operation_worker
in
let kind = Baking_actions.Fresh pool in
let block_to_bake : Baking_actions.block_to_bake =
{
Baking_actions.predecessor = state.level_state.latest_proposal.block;
round = minimal_round;
delegate;
kind;
force_apply = state.global_state.config.force_apply;
}
in
let state_recorder ~new_state =
Baking_state.may_record_new_state ~previous_state:state ~new_state
in
let* state =
Baking_actions.perform_action
~state_recorder
state
(Inject_block {block_to_bake; updated_state = state})
in
let*! () =
cctxt#message
"Proposed block at round %a on top of %a "
Round.pp
block_to_bake.round
Block_hash.pp
block_to_bake.predecessor.hash
in
return state
else
let* event = bake_at_next_level state in
let* state =
let*! action = State_transitions.step state event in
do_action action
in
let*! () = cctxt#message "Proposal injected" in
return state
let attestation_quorum state =
let power, attestations = state_attesting_power state in
if
Compare.Int.(
power >= state.global_state.constants.parametric.consensus_threshold)
then Some (power, attestations)
else None
let propose (cctxt : Protocol_client_context.full) ?minimal_fees
?minimal_nanotez_per_gas_unit ?minimal_nanotez_per_byte ?force_apply ?force
?(minimal_timestamp = false) ? ?context_path ?state_recorder
delegates =
let open Lwt_result_syntax in
let cache = Baking_cache.Block_cache.create 10 in
let* _block_stream, current_proposal = get_current_proposal cctxt ~cache () in
let config =
Baking_configuration.make
?minimal_fees
?minimal_nanotez_per_gas_unit
?minimal_nanotez_per_byte
?context_path
?force_apply
?force
?extra_operations
?state_recorder
()
in
let* state = create_state cctxt ~config ~current_proposal delegates in
let* () =
Operation_worker.retrieve_pending_operations
cctxt
state.global_state.operation_worker
in
let* _ =
match state.level_state.elected_block with
| Some _ -> propose_at_next_level ~minimal_timestamp state
| None -> (
match attestation_quorum state with
| Some (_voting_power, attestation_qc) ->
let state =
{
state with
round_state =
{
state.round_state with
current_phase = Baking_state.Awaiting_attestations;
};
}
in
let latest_proposal = state.level_state.latest_proposal.block in
let candidate =
{
Operation_worker.hash = latest_proposal.hash;
round_watched = latest_proposal.round;
payload_hash_watched = latest_proposal.payload_hash;
}
in
let* state =
let*! action =
State_transitions.step
state
(Baking_state.Quorum_reached (candidate, attestation_qc))
in
do_action action
in
propose_at_next_level ~minimal_timestamp state
| None -> (
let*? event = Baking_scheduling.compute_bootstrap_event state in
let*! state, _action = State_transitions.step state event in
let latest_proposal = state.level_state.latest_proposal in
let open State_transitions in
let round = state.round_state.current_round in
let*! proposal_acceptance =
is_acceptable_proposal_for_current_level state latest_proposal
in
match proposal_acceptance with
| Invalid | Outdated_proposal -> (
match round_proposer state ~level:`Current round with
| Some {consensus_key_and_delegate; _} ->
let*! action =
State_transitions.propose_block_action
state
consensus_key_and_delegate
round
state.level_state.latest_proposal
in
let* state = do_action (state, action) in
let*! () =
cctxt#message
"Reproposed block at level %ld on round %a"
state.level_state.current_level
Round.pp
state.round_state.current_round
in
return state
| None -> cctxt#error "No slots for current round")
| Valid_proposal ->
cctxt#error
"Cannot propose: there's already a valid proposal for the \
current round %a"
Round.pp
round))
in
return_unit
let repropose (cctxt : Protocol_client_context.full) ?force ?force_round
delegates =
let open Lwt_result_syntax in
let open Baking_state in
let cache = Baking_cache.Block_cache.create 10 in
let* _block_stream, current_proposal = get_current_proposal cctxt ~cache () in
let config = Baking_configuration.make ?force () in
let* state = create_state cctxt ~config ~current_proposal delegates in
let*? event = Baking_scheduling.compute_bootstrap_event state in
let*! state, _action = State_transitions.step state event in
let latest_proposal = state.level_state.latest_proposal in
let open State_transitions in
let round =
match force_round with
| Some x -> x
| None -> state.round_state.current_round
in
let*! proposal_validity =
is_acceptable_proposal_for_current_level state latest_proposal
in
match proposal_validity with
| Invalid | Outdated_proposal -> (
match Baking_state.round_proposer state ~level:`Current round with
| Some {consensus_key_and_delegate; _} ->
let*! action =
State_transitions.propose_block_action
state
consensus_key_and_delegate
round
state.level_state.latest_proposal
in
let* state = do_action (state, action) in
let*! () =
cctxt#message
"Reproposed block at level %ld on round %a"
state.level_state.current_level
Round.pp
state.round_state.current_round
in
return_unit
| None -> cctxt#error "No slots for current round")
| Valid_proposal ->
cctxt#error
"Cannot propose: there's already a valid proposal for the current \
round %a"
Round.pp
round
let bake_using_automaton ~count config state heads_stream =
let open Lwt_result_syntax in
let cctxt = state.global_state.cctxt in
let* initial_event = first_automaton_event state in
let current_level = state.level_state.latest_proposal.block.shell.level in
let loop_state =
Baking_scheduling.create_loop_state
~heads_stream
state.global_state.operation_worker
in
let stop_on_next_level_block = function
| New_head_proposal proposal ->
Compare.Int32.(
proposal.block.shell.level >= Int32.(add current_level (of_int count)))
| _ -> false
in
let* event_opt =
Baking_scheduling.automaton_loop
~stop_on_event:stop_on_next_level_block
~config
~on_error:(fun err -> Lwt.return (Error err))
loop_state
state
initial_event
in
match event_opt with
| Some (New_head_proposal proposal) ->
let*! () =
cctxt#message
"Last injected block: %a (level %ld)"
Block_hash.pp
proposal.block.hash
proposal.block.shell.level
in
return_unit
| _ -> cctxt#error "Baking loop unexpectedly ended"
let rec baking_minimal_timestamp ~count state
(block_stream : proposal Lwt_stream.t) =
let open Lwt_result_syntax in
let cctxt = state.global_state.cctxt in
let latest_proposal = state.level_state.latest_proposal in
let own_attestations =
State_transitions.make_consensus_list state latest_proposal
in
let current_mempool =
Operation_worker.get_current_operations state.global_state.operation_worker
in
let attestations_in_mempool =
Operation_pool.(
filter_attestations (Operation_set.elements current_mempool.consensus))
|> List.filter_map
(fun
({
protocol_data =
{contents = Single (Attestation consensus_content); _};
_;
} :
Kind.attestation operation)
->
if
Round.(consensus_content.round = latest_proposal.block.round)
&& Compare.Int32.(
Raw_level.to_int32 consensus_content.level
= latest_proposal.block.shell.level)
then Some consensus_content
else None)
in
let total_voting_power =
List.fold_left
(fun attestations own -> snd own :: attestations)
attestations_in_mempool
own_attestations
|> attestations_attesting_power state
in
let consensus_threshold =
state.global_state.constants.parametric.consensus_threshold
in
let* () =
if Compare.Int.(total_voting_power < consensus_threshold) then
cctxt#error
"Delegates do not have enough voting power. Only %d is available while \
%d is required."
total_voting_power
consensus_threshold
else return_unit
in
let* minimal_round, delegate =
match
Baking_scheduling.first_potential_round_at_next_level
state
~earliest_round:Round.zero
with
| None -> cctxt#error "No potential baking slot for the given delegates."
| Some first_potential_round -> return first_potential_round
in
let* signed_attestations =
Baking_actions.sign_consensus_votes state own_attestations `Attestation
in
let pool =
Operation_pool.add_operations
current_mempool
(List.map (fun (_, x, _, _) -> x) signed_attestations)
in
let* own_dal_attestations = Baking_actions.get_dal_attestations state in
let* signed_dal_attestations =
Baking_actions.sign_dal_attestations state own_dal_attestations
in
let pool =
Operation_pool.add_operations
pool
(List.map
(fun (_delegate, op, _bitset, _published_level) -> op)
signed_dal_attestations)
in
let kind = Baking_actions.Fresh pool in
let block_to_bake : Baking_actions.block_to_bake =
{
Baking_actions.predecessor = latest_proposal.block;
round = minimal_round;
delegate;
kind;
force_apply = state.global_state.config.force_apply;
}
in
let state_recorder ~new_state =
Baking_state.may_record_new_state ~previous_state:state ~new_state
in
let* new_state =
Baking_actions.perform_action
~state_recorder
state
(Inject_block {block_to_bake; updated_state = state})
in
let*! () = cctxt#message "Injected block at minimal timestamp" in
if count <= 1 then return_unit
else
let*! () =
let attestation_level = Int32.succ latest_proposal.block.shell.level in
Lwt_stream.junk_while_s
(fun proposal ->
Lwt.return
Compare.Int32.(
proposal.Baking_state.block.shell.level <> attestation_level))
block_stream
in
let*! next_level_proposal =
let*! r = Lwt_stream.get block_stream in
match r with
| None -> cctxt#error "Stream unexpectedly ended"
| Some b -> Lwt.return b
in
let*! new_state, action =
State_transitions.step new_state (New_head_proposal next_level_proposal)
in
let* new_state =
match action with
| Update_to_level update ->
let* new_state, _preattest_action =
Baking_actions.update_to_level new_state update
in
return
{
new_state with
round_state =
{
Baking_state.current_round = Round.zero;
current_phase = Idle;
delayed_quorum = None;
};
}
| _ ->
assert false
in
baking_minimal_timestamp ~count:(pred count) new_state block_stream
let bake (cctxt : Protocol_client_context.full) ?minimal_fees
?minimal_nanotez_per_gas_unit ?minimal_nanotez_per_byte ?force_apply ?force
?(minimal_timestamp = false) ?
?(monitor_node_mempool = true) ?context_path ?dal_node_endpoint ?(count = 1)
?votes ?state_recorder delegates =
let open Lwt_result_syntax in
let config =
Baking_configuration.make
?minimal_fees
?minimal_nanotez_per_gas_unit
?minimal_nanotez_per_byte
?context_path
?force_apply
?force
?extra_operations
?dal_node_endpoint
?votes
?state_recorder
()
in
let cache = Baking_cache.Block_cache.create 10 in
let* block_stream, current_proposal = get_current_proposal cctxt ~cache () in
let* state =
create_state
cctxt
~monitor_node_mempool
~synchronize:(not minimal_timestamp)
~config
~current_proposal
delegates
in
let* () =
when_ monitor_node_mempool (fun () ->
Operation_worker.retrieve_pending_operations
cctxt
state.global_state.operation_worker)
in
if not minimal_timestamp then
bake_using_automaton ~count config state block_stream
else baking_minimal_timestamp ~count state block_stream