-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathapp.rs
More file actions
818 lines (718 loc) · 29.8 KB
/
Copy pathapp.rs
File metadata and controls
818 lines (718 loc) · 29.8 KB
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
use alloy_rpc_types_engine::ExecutionPayloadV3;
use bytes::Bytes;
use color_eyre::eyre::{self, eyre, OptionExt};
use malachitebft_app_channel::app::engine::host::Next;
use malachitebft_app_channel::app::streaming::StreamContent;
use malachitebft_app_channel::app::types::core::{Round, Validity};
use malachitebft_app_channel::app::types::{LocallyProposedValue, ProposedValue};
use malachitebft_app_channel::{AppMsg, Channels, NetworkMsg};
use malachitebft_eth_cli::config::EmeraldConfig;
use malachitebft_eth_engine::engine::Engine;
use malachitebft_eth_engine::json_structures::ExecutionBlock;
use malachitebft_eth_types::EmeraldContext;
use ssz::{Decode, Encode};
use tokio::time::Instant;
use tracing::{debug, error, info, warn};
use crate::bootstrap::{initialize_state_from_existing_block, initialize_state_from_genesis};
use crate::payload::validate_execution_payload;
use crate::state::{decode_value, State};
use crate::sync_handler::get_decided_value_for_sync;
use crate::validators::read_validators_from_contract;
/// Handle ConsensusReady messages from the consensus engine
///
/// Notifies the application that consensus is ready.
///
/// The application MUST reply with a message to instruct
/// consensus to start at a given height.
pub async fn on_consensus_ready(
consensus_ready: AppMsg<EmeraldContext>,
state: &mut State,
engine: &Engine,
emerald_config: &EmeraldConfig,
) -> eyre::Result<()> {
let AppMsg::ConsensusReady { reply } = consensus_ready else {
unreachable!("on_consensus_ready called with non-ConsensusReady message");
};
info!("🟢🟢 Consensus is ready");
// Node start-up: https://hackmd.io/@danielrachi/engine_api#Node-startup
// Check compatibility with execution client
engine.check_capabilities().await?;
// Get latest decided height from local store
let latest_height_from_store = state.store.max_decided_value_height().await;
match latest_height_from_store {
Some(h) => {
initialize_state_from_existing_block(state, engine, h, emerald_config).await?;
info!(
"Starting from existing block at height {:?}. Current tip (consensus height): {:?} ",
h,
state.consensus_height
);
}
None => {
// Get the genesis block from the execution engine
initialize_state_from_genesis(state, engine).await?;
info!(
"Starting from genesis. Current tip (consensus height): {:?}",
state.consensus_height
);
}
}
// We can simply respond by telling the engine to start consensus
// at consensus_height (which tracks the tip where consensus will work)
if reply
.send((
state.consensus_height,
state
.get_validator_set(state.consensus_height)
.ok_or_eyre(format!(
"Validator set not found for height {}",
state.consensus_height
))?
.clone(),
))
.is_err()
{
error!("Failed to send ConsensusReady reply");
}
Ok(())
}
/// Handle StartedRound messages from the consensus engine
///
/// Notifies the application that a new consensus round has begun.
pub async fn on_started_round(
started_round: AppMsg<EmeraldContext>,
state: &mut State,
engine: &Engine,
emerald_config: &EmeraldConfig,
) -> eyre::Result<()> {
let AppMsg::StartedRound {
height,
round,
proposer,
role,
reply_value,
} = started_round
else {
unreachable!("on_started_round called with non-StartedRound message");
};
info!(%height, %round, %proposer, ?role, "🟢🟢 Started round");
// The consensus_height stored in state should match
// the one in the StartedRound message
if state.consensus_height != height {
warn!(
consensus_height = %state.consensus_height,
new_height = %height,
"Started round mismatch between state and message"
);
}
// We can use that opportunity to update our internal state
state.consensus_height = height;
state.consensus_round = round;
if state.consensus_round == Round::ZERO {
state.last_block_time = Instant::now();
}
let pending_parts = state
.store
.get_pending_proposal_parts(height, round)
.await?;
debug!(
%height,
%round,
"Found {} pending proposal parts, validating...",
pending_parts.len()
);
for parts in &pending_parts {
// Validate and store the pending proposal
let result = state
.process_complete_proposal_parts(parts, engine, &emerald_config.retry_config)
.await?;
if result.is_some() {
info!(
height = %parts.height,
round = %parts.round,
proposer = %parts.proposer,
"Moved valid pending proposal to undecided after validation"
);
}
// Remove the parts from pending regardless of validation outcome
state
.store
.remove_pending_proposal_parts(parts.clone())
.await?;
}
// If we have already built or seen values for this height and round,
// send them all back to consensus. This may happen when we are restarting after a crash.
let proposals = state.store.get_undecided_proposals(height, round).await?;
debug!(%height, %round, "Found {} undecided proposals", proposals.len());
if reply_value.send(proposals).is_err() {
error!("Failed to send undecided proposals");
}
Ok(())
}
/// Handle GetValue messages from the consensus engine
///
/// Requests the application to build a value for consensus to propose.
///
/// The application MUST reply to this message with the requested value
/// within the specified timeout duration.
pub async fn on_get_value(
get_value: AppMsg<EmeraldContext>,
state: &mut State,
channels: &Channels<EmeraldContext>,
engine: &Engine,
emerald_config: &EmeraldConfig,
) -> eyre::Result<()> {
let AppMsg::GetValue {
height,
round,
timeout,
reply,
} = get_value
else {
unreachable!("on_get_value called with non-GetValue message");
};
// NOTE: We can ignore the timeout as we are building the value right away.
// If we were let's say reaping as many txes from a mempool and executing them,
// then we would need to respect the timeout and stop at a certain point.
info!(%height, %round, "🟢🟢 Consensus is requesting a value to propose");
// Here it is important that, if we have previously built a value for this height and round,
// we send back the very same value.
let (proposal, bytes) = match state.get_previously_built_value(height, round).await? {
Some(proposal) => {
info!(value = %proposal.value.id(), "Re-using previously built value");
// Fetch the block data for the previously built value
let bytes = state
.store
.get_block_data(height, round, proposal.value.id())
.await?
.ok_or_else(|| eyre!("Block data not found for previously built value"))?;
(proposal, bytes)
}
None => {
// Check if the execution client is syncing and behind the consensus height
let (is_syncing, highest_chain_height) = engine.is_syncing().await?;
if is_syncing && highest_chain_height >= height.as_u64() {
warn!(
"⚠️ Execution client is syncing (current: {}, target: {}), waiting for timeout",
highest_chain_height,
height.as_u64()
);
tokio::time::sleep(timeout * 2).await; // Sleep long enough to trigger timeout_propose
return Ok(());
} else {
// If we have not previously built a value for that very same height and round,
// we need to create a new value to propose and send it back to consensus.
info!("Building a new value to propose");
// We need to ask the execution engine for a new value to
// propose. Then we send it back to consensus.
let latest_block = state.latest_block.expect("Head block hash is not set");
let execution_payload = engine
.generate_block(
&Some(latest_block),
&emerald_config.retry_config,
&emerald_config.fee_recipient,
state.get_fork(latest_block.timestamp),
)
.await?;
debug!("🌈 Got execution payload: {:?}", execution_payload);
// Store block in state and propagate to peers.
let bytes = Bytes::from(execution_payload.as_ssz_bytes());
debug!("🎁 block size: {:?}, height: {}", bytes.len(), height);
// Prepare block proposal.
let proposal: LocallyProposedValue<EmeraldContext> =
state.propose_value(height, round, bytes.clone()).await?;
(proposal, bytes)
}
}
};
// Send it to consensus
if reply.send(proposal.clone()).is_err() {
error!("Failed to send GetValue reply");
}
// The POL round is always nil when we propose a newly built value.
// See L15/L18 of the Tendermint algorithm.
let pol_round = Round::Nil;
// Now what's left to do is to break down the value to propose into parts,
// and send those parts over the network to our peers, for them to re-assemble the full value.
for stream_message in state.stream_proposal(proposal, bytes, pol_round) {
debug!(%height, %round, "Streaming proposal part: {stream_message:?}");
channels
.network
.send(NetworkMsg::PublishProposalPart(stream_message))
.await?;
}
debug!(%height, %round, "✅ Proposal sent");
Ok(())
}
/// Handle ReceivedProposalPart messages from the consensus engine
///
/// Notifies the application that consensus has received a proposal part over the network.
///
/// If this part completes the full proposal, the application MUST respond
/// with the complete proposed value. Otherwise, it MUST respond with `None`.
pub async fn on_received_proposal_part(
received_proposal_part: AppMsg<EmeraldContext>,
state: &mut State,
engine: &Engine,
emerald_config: &EmeraldConfig,
) -> eyre::Result<()> {
let AppMsg::ReceivedProposalPart { from, part, reply } = received_proposal_part else {
unreachable!("on_received_proposal_part called with non-ReceivedProposalPart message");
};
let (part_type, part_size) = match &part.content {
StreamContent::Data(part) => (part.get_type(), part.size_bytes()),
StreamContent::Fin => ("end of stream", 0),
};
debug!(
%from, %part.sequence, part.type = %part_type, part.size = %part_size,
"Received proposal part"
);
// Try to reassemble the proposal from received parts
let parts = state.reassemble_proposal(from, part).await?;
// If we have complete parts, validate and store the proposal
let proposed_value = match parts {
Some(parts) => {
state
.process_complete_proposal_parts(&parts, engine, &emerald_config.retry_config)
.await?
}
None => None,
};
if let Some(ref proposed_value) = proposed_value {
debug!("✅ Received complete proposal: {:?}", proposed_value);
}
if reply.send(proposed_value).is_err() {
error!("Failed to send ReceivedProposalPart reply");
}
Ok(())
}
/// Handle Decided messages from the consensus engine
///
/// Notifies the application that consensus has decided on a value.
///
/// This message includes a commit certificate containing the ID of
/// the value that was decided on, the height and round at which it was decided,
/// and the aggregated signatures of the validators that committed to it.
/// It also includes to the vote extensions received for that height.
///
/// In response to this message, the application MUST send a [`Next`]
/// message back to consensus, instructing it to either start the next height if
/// the application was able to commit the decided value, or to restart the current height
/// otherwise.
///
/// If the application does not reply, consensus will stall.
pub async fn on_decided(
decided: AppMsg<EmeraldContext>,
state: &mut State,
engine: &Engine,
emerald_config: &EmeraldConfig,
) -> eyre::Result<()> {
let AppMsg::Decided {
certificate, reply, ..
} = decided
else {
unreachable!("on_decided called with non-Decided message");
};
let height = certificate.height;
let round = certificate.round;
let value_id = certificate.value_id;
info!(
%height, %round, value = %certificate.value_id,
"🟢🟢 Consensus has decided on value"
);
// The consensus engine only sends Decided messages for values (proposals)
// that were completely received by the local node
let block_bytes = state
.get_block_data(height, round, value_id)
.await
.ok_or_eyre("app: certificate should have associated block data")?;
debug!("🎁 block size: {:?}, height: {}", block_bytes.len(), height);
// Decode bytes into execution payload (a block) and get relevant fields
let execution_payload = ExecutionPayloadV3::from_ssz_bytes(&block_bytes).unwrap();
let block_hash = execution_payload.payload_inner.payload_inner.block_hash;
let block_timestamp = execution_payload.timestamp();
let block_number = execution_payload.payload_inner.payload_inner.block_number;
let block_prev_randao = execution_payload.payload_inner.payload_inner.prev_randao;
let parent_block_hash = execution_payload.payload_inner.payload_inner.parent_hash;
let tx_count = execution_payload
.payload_inner
.payload_inner
.transactions
.len();
debug!("🦄 Block at height {height} contains {tx_count} transactions");
// Sanity check: verify payload.parent_hash == state.latest_block.block_hash
let latest_block_hash = state
.latest_block
.ok_or_eyre("missing latest block in state")?
.block_hash;
assert_eq!(latest_block_hash, parent_block_hash);
// Validate the execution payload (uses cache internally)
let validity = validate_execution_payload(
state.validated_cache_mut(),
&block_bytes,
height,
round,
engine,
&emerald_config.retry_config,
)
.await?;
if validity == Validity::Invalid {
return Err(eyre!("Block validation failed for hash: {}", block_hash));
}
debug!(
"💡 Block validated at height {} with hash: {}",
height, block_hash
);
// Notify the EL of the new block.
// Update the execution head state to this block.
let latest_valid_hash = engine
.set_latest_forkchoice_state(block_hash, &emerald_config.retry_config)
.await?;
debug!(
"🚀 Forkchoice updated to height {} for block hash={} and latest_valid_hash={}",
height, block_hash, latest_valid_hash
);
// When that happens, we store the decided value in our store
// TODO: we should return an error reply if commit fails
state.commit(certificate).await?;
// Calculate and log per-block statistics
let block_time_secs = state.previous_block_commit_time.elapsed().as_secs_f64();
state
.log_block_stats(height, tx_count, block_bytes.len(), block_time_secs)
.await?;
// Update previous_block_commit_time to track when this block was committed
// This is used to calculate per-block TPS for the next block
state.previous_block_commit_time = Instant::now();
// Save the latest block
state.latest_block = Some(ExecutionBlock {
block_hash,
block_number,
parent_hash: latest_block_hash,
timestamp: block_timestamp, // Note: This was a fix related to the sync reactor
prev_randao: block_prev_randao,
});
// Update consensus_height and consensus_round to track the tip of the blockchain
// After committing height H, the tip advances to H+1 where consensus will work next
state.consensus_height = height.increment();
state.consensus_round = Round::ZERO;
// Get the new validator set for the next height and update the local state
let new_validator_set =
read_validators_from_contract(engine.eth.url().as_ref(), &latest_valid_hash).await?;
debug!("🌈 Got validator set: {:?}", new_validator_set);
state.set_validator_set(state.consensus_height, new_validator_set);
// And then we instruct consensus to start the next height
if reply
.send(Next::Start(
state.consensus_height,
state
.get_validator_set(state.consensus_height)
.ok_or_eyre("Validator set not found for height {state.consensus_height}")?
.clone(),
))
.is_err()
{
error!("Failed to send Decided reply");
}
Ok(())
}
/// Handle ProcessSyncedValue messages from the consensus engine
///
/// Notifies the application that a value has been synced from the network.
/// This may happen when the node is catching up with the network.
///
/// If a value can be decoded from the bytes provided, then the application MUST reply
/// to this message with the decoded value. Otherwise, it MUST reply with `None`.
pub async fn on_process_synced_value(
process_synced_value: AppMsg<EmeraldContext>,
state: &mut State,
engine: &Engine,
emerald_config: &EmeraldConfig,
) -> eyre::Result<()> {
let AppMsg::ProcessSyncedValue {
height,
round,
proposer,
value_bytes,
reply,
} = process_synced_value
else {
unreachable!("on_process_synced_value called with non-ProcessSyncedValue message");
};
info!(%height, %round, "🟢🟢 Processing synced value");
let value = decode_value(value_bytes);
let block_bytes = value.extensions.clone();
// Validate the synced block
let validity = validate_execution_payload(
state.validated_cache_mut(),
&block_bytes,
height,
round,
engine,
&emerald_config.retry_config,
)
.await?;
if validity == Validity::Invalid {
// Reject invalid blocks - don't store or reply with them
if reply
.send(Some(ProposedValue {
height,
round,
valid_round: Round::Nil,
proposer,
value,
validity: Validity::Invalid,
}))
.is_err()
{
error!("Failed to send ProcessSyncedValue rejection reply");
}
return Ok(());
}
debug!(%height, "💡 Sync block validated");
let proposed_value: ProposedValue<EmeraldContext> = ProposedValue {
height,
round,
valid_round: Round::Nil,
proposer,
value,
validity: Validity::Valid,
};
if let Err(e) = state
.store_undecided_value(&proposed_value, block_bytes)
.await
{
error!(%height, %round, error = %e, "Failed to store synced value");
}
// Send to consensus to see if it has been decided on
if reply.send(Some(proposed_value)).is_err() {
error!(%height, %round, "Failed to send ProcessSyncedValue reply");
}
Ok(())
}
/// Handle GetDecidedValue messages from the consensus engine
///
/// Requests a previously decided value from the application's storage.
///
/// The application MUST respond with that value if available, or `None` otherwise.
pub async fn on_get_decided_value(
get_decided_value: AppMsg<EmeraldContext>,
state: &State,
engine: &Engine,
) -> eyre::Result<()> {
let AppMsg::GetDecidedValue { height, reply } = get_decided_value else {
unreachable!("on_decided_value called with non-GetDecidedValue message");
};
info!(%height, "🟢🟢 GetDecidedValue");
let earliest_height_available = state.get_earliest_height().await;
// Check if requested height is beyond our consensus height
let raw_decided_value = if (earliest_height_available..state.consensus_height).contains(&height)
{
let earliest_unpruned = state.get_earliest_unpruned_height().await;
get_decided_value_for_sync(&state.store, engine, height, earliest_unpruned).await?
} else {
info!(%height, consensus_height = %state.consensus_height, "Requested height is >= consensus height or < earliest_height_available.");
None
};
if reply.send(raw_decided_value).is_err() {
error!("Failed to send GetDecidedValue reply");
}
Ok(())
}
/// Handle GetHistoryMinHeight messages from the consensus engine
///
/// Requests the earliest height available in the history maintained by the application.
///
/// The application MUST respond with its earliest available height.
pub async fn on_get_history_min_height(
get_history_min_height: AppMsg<EmeraldContext>,
state: &State,
) -> eyre::Result<()> {
let AppMsg::GetHistoryMinHeight { reply } = get_history_min_height else {
unreachable!("on_get_history_min_height called with non-GetHistoryMinHeight message");
};
let min_height = state.get_earliest_height().await;
if reply.send(min_height).is_err() {
error!("Failed to send GetHistoryMinHeight reply");
}
Ok(())
}
/// Handle RestreamProposal messages from the consensus engine
///
/// Requests the application to re-stream a proposal that it has already seen.
///
/// The application MUST re-publish again all the proposal parts pertaining
/// to that value by sending [`NetworkMsg::PublishProposalPart`] messages through
/// the [`Channels::network`] channel.
pub async fn on_restream_proposal(
restream_proposal: AppMsg<EmeraldContext>,
state: &mut State,
channels: &mut Channels<EmeraldContext>,
) -> eyre::Result<()> {
let AppMsg::RestreamProposal {
height,
round,
valid_round,
address,
value_id,
} = restream_proposal
else {
unreachable!("on_restream_proposal called with non-RestreamProposal message");
};
// Look for a proposal at valid_round or round(should be already stored)
let proposal_round = if valid_round == Round::Nil {
round
} else {
valid_round
};
info!(%height, %proposal_round, "Restreaming existing proposal...");
//let (proposal, bytes) =
match state
.get_previous_proposal_by_value_and_proposer(height, round, value_id, address)
.await?
{
Some(proposal) => {
info!(value = %proposal.value.id(), "Re-using previously built value");
// Fetch the block data for the previously built value
let bytes = state
.store
.get_block_data(height, round, proposal.value.id())
.await?
.ok_or_else(|| eyre!("Block data not found for previously built value"))?;
// Now what's left to do is to break down the value to propose into parts,
// and send those parts over the network to our peers, for them to re-assemble the full value.
for stream_message in state.stream_proposal(proposal, bytes, proposal_round) {
debug!(%height, %round, "Streaming proposal part: {stream_message:?}");
channels
.network
.send(NetworkMsg::PublishProposalPart(stream_message))
.await?;
}
debug!(%height, %round, "✅ Re-sent proposal");
}
None => {
debug!(%height, %round, "✅ No proposal to re-send");
}
}
Ok(())
}
/// Handle ExtendVote messages from the consensus engine
///
/// ExtendVote allows the application to extend the pre-commit vote with arbitrary data.
///
/// When consensus is preparing to send a pre-commit vote, it first calls `ExtendVote`.
/// The application then returns a blob of data called a vote extension.
/// This data is opaque to the consensus algorithm but can contain application-specific information.
/// The proposer of the next block will receive all vote extensions along with the commit certificate.
pub async fn on_extended_vote(extended_vote: AppMsg<EmeraldContext>) -> eyre::Result<()> {
let AppMsg::ExtendVote { reply, .. } = extended_vote else {
unreachable!("on_extended_vote called with non-ExtendVote message");
};
if reply.send(None).is_err() {
error!("🔴 Failed to send ExtendVote reply");
}
Ok(())
}
/// Handle VerifyVoteExtension messages from the consensus engine
///
/// Verify a vote extension
///
/// If the vote extension is deemed invalid, the vote it was part of
/// will be discarded altogether.
pub async fn on_verify_vote_extention(
verify_vote_extenstion: AppMsg<EmeraldContext>,
) -> eyre::Result<()> {
let AppMsg::VerifyVoteExtension { reply, .. } = verify_vote_extenstion else {
unreachable!("on_verify_vote_extention called with non-VerifyVoteExtension message");
};
if reply.send(Ok(())).is_err() {
error!("🔴 Failed to send VerifyVoteExtension reply");
}
Ok(())
}
pub async fn process_consensus_message(
msg: AppMsg<EmeraldContext>,
state: &mut State,
channels: &mut Channels<EmeraldContext>,
engine: &Engine,
emerald_config: &EmeraldConfig,
) -> eyre::Result<()> {
match msg {
// The first message to handle is the `ConsensusReady` message, signaling to the app
// that Malachite is ready to start consensus
msg @ AppMsg::ConsensusReady { .. } => {
on_consensus_ready(msg, state, engine, emerald_config).await?;
}
// The next message to handle is the `StartRound` message, signaling to the app
// that consensus has entered a new round (including the initial round 0)
msg @ AppMsg::StartedRound { .. } => {
on_started_round(msg, state, engine, emerald_config).await?;
}
// At some point, we may end up being the proposer for that round, and the consensus engine
// will then ask us for a value to propose to the other validators.
msg @ AppMsg::GetValue { .. } => {
on_get_value(msg, state, channels, engine, emerald_config).await?;
}
// On the receiving end of these proposal parts (ie. when we are not the proposer),
// we need to process these parts and re-assemble the full value.
// To this end, we store each part that we receive and assemble the full value once we
// have all its constituent parts. Then we send that value back to consensus for it to
// consider and vote for or against it (ie. vote `nil`), depending on its validity.
msg @ AppMsg::ReceivedProposalPart { .. } => {
on_received_proposal_part(msg, state, engine, emerald_config).await?;
}
// After some time, consensus will finally reach a decision on the value
// to commit for the consensus_height, and will notify the application,
// providing it with a commit certificate which contains the ID of the value
// that was decided on as well as the set of commits for that value,
// ie. the precommits together with their (aggregated) signatures.
msg @ AppMsg::Decided { .. } => {
on_decided(msg, state, engine, emerald_config).await?;
}
// It may happen that our node is lagging behind its peers. In that case,
// a synchronization mechanism will automatically kick to try and catch up to
// our peers. When that happens, some of these peers will send us decided values
// for the heights in between the one we are currently at (included) and the one
// that they are at. When the engine receives such a value, it will forward to the application
// to decode it from its wire format and send back the decoded value to consensus.
msg @ AppMsg::ProcessSyncedValue { .. } => {
on_process_synced_value(msg, state, engine, emerald_config).await?;
}
// If, on the other hand, we are not lagging behind but are instead asked by one of
// our peer to help them catch up because they are the one lagging behind,
// then the engine might ask the application to provide with the value
// that was decided at some lower height. In that case, we fetch it from our store
// and send it to consensus.
msg @ AppMsg::GetDecidedValue { .. } => {
on_get_decided_value(msg, state, engine).await?;
}
// In order to figure out if we can help a peer that is lagging behind,
// the engine may ask us for the height of the earliest available value in our store.
msg @ AppMsg::GetHistoryMinHeight { .. } => {
on_get_history_min_height(msg, state).await?;
}
msg @ AppMsg::RestreamProposal { .. } => {
on_restream_proposal(msg, state, channels).await?;
}
msg @ AppMsg::ExtendVote { .. } => {
on_extended_vote(msg).await?;
}
msg @ AppMsg::VerifyVoteExtension { .. } => {
on_verify_vote_extention(msg).await?;
}
}
Ok(())
}
pub async fn run(
state: &mut State,
channels: &mut Channels<EmeraldContext>,
engine: Engine,
emerald_config: EmeraldConfig,
) -> eyre::Result<()> {
while let Some(msg) = channels.consensus.recv().await {
process_consensus_message(msg, state, channels, &engine, &emerald_config).await?;
}
// If we get there, it can only be because the channel we use to receive message
// from consensus has been closed, meaning that the consensus actor has died.
// We can do nothing but return an error here.
Err(eyre!("Consensus channel closed unexpectedly"))
}