Skip to content

Commit 1f7f6f5

Browse files
nits
1 parent 3748eb9 commit 1f7f6f5

4 files changed

Lines changed: 57 additions & 30 deletions

File tree

consensus/src/marshal/coding/marshaled.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ use rand::Rng;
125125
use std::sync::{Arc, OnceLock};
126126
use tracing::{debug, warn};
127127

128+
/// Which marshal cache a verified coded block should land in.
129+
#[derive(Clone, Copy, Debug)]
130+
enum CodingPersistMode {
131+
/// Write to `verified_blocks` via `Mailbox::verified`.
132+
Verified,
133+
/// Write to `notarized_blocks` via `Mailbox::certified`.
134+
Certified,
135+
}
136+
128137
/// The [`CodingConfig`] used for genesis blocks. These blocks are never broadcasted in
129138
/// the proposal phase, and thus the configuration is irrelevant.
130139
const GENESIS_CODING_CONFIG: CodingConfig = CodingConfig {
@@ -299,6 +308,7 @@ where
299308
consensus_context: Context<Commitment, <Z::Scheme as CertificateScheme>::PublicKey>,
300309
commitment: Commitment,
301310
prefetched_block: Option<CodedBlock<B, C, H>>,
311+
persist: CodingPersistMode,
302312
) -> oneshot::Receiver<bool> {
303313
let mut marshal = self.marshal.clone();
304314
let mut application = self.application.clone();
@@ -424,9 +434,15 @@ where
424434
is_valid = validity_request => is_valid,
425435
};
426436
timer.observe();
427-
if application_valid && !marshal.verified(round, block).await {
428-
debug!(?round, "marshal unable to accept block");
429-
return;
437+
if application_valid {
438+
let persisted = match persist {
439+
CodingPersistMode::Verified => marshal.verified(round, block).await,
440+
CodingPersistMode::Certified => marshal.certified(round, block).await,
441+
};
442+
if !persisted {
443+
debug!(?round, "marshal unable to accept block");
444+
return;
445+
}
430446
}
431447
tx.send_lossy(application_valid);
432448
});
@@ -779,7 +795,8 @@ where
779795
// Kick off deferred verification early to hide verification latency behind
780796
// shard validity checks and network latency for collecting votes.
781797
let round = consensus_context.round;
782-
let task = self.deferred_verify(consensus_context, payload, None);
798+
let task =
799+
self.deferred_verify(consensus_context, payload, None, CodingPersistMode::Verified);
783800
self.verification_tasks.insert(round, payload, task);
784801

785802
match scheme.me() {
@@ -917,18 +934,13 @@ where
917934

918935
// Use the block's embedded context for verification, passing the
919936
// prefetched block to avoid fetching it again inside deferred_verify.
920-
let block_for_certify = block.clone();
921-
let verify_rx = marshaled.deferred_verify(embedded_context, payload, Some(block));
937+
let verify_rx = marshaled.deferred_verify(
938+
embedded_context,
939+
payload,
940+
Some(block),
941+
CodingPersistMode::Certified,
942+
);
922943
if let Ok(result) = verify_rx.await {
923-
if result
924-
&& !marshaled
925-
.marshal
926-
.certified(round, block_for_certify)
927-
.await
928-
{
929-
debug!(?round, "marshal unable to accept certified block");
930-
return;
931-
}
932944
tx.send_lossy(result);
933945
}
934946
});

consensus/src/marshal/standard/deferred.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ use crate::{
8181
standard::{
8282
validation::{
8383
fetch_parent, precheck_epoch_and_reproposal, verify_with_parent, Decision,
84+
PersistMode,
8485
},
8586
Standard,
8687
},
@@ -203,6 +204,7 @@ where
203204
&mut self,
204205
context: <Self as Automaton>::Context,
205206
block: B,
207+
persist: PersistMode,
206208
) -> oneshot::Receiver<bool> {
207209
let mut marshal = self.marshal.clone();
208210
let mut application = self.application.clone();
@@ -226,6 +228,7 @@ where
226228
&mut application,
227229
&mut marshal,
228230
&mut tx,
231+
persist,
229232
)
230233
.await
231234
{
@@ -500,7 +503,7 @@ where
500503

501504
// Begin the rest of the verification process asynchronously.
502505
let round = context.round;
503-
let task = marshaled.deferred_verify(context, block);
506+
let task = marshaled.deferred_verify(context, block, PersistMode::Verified);
504507
marshaled.verification_tasks.insert(round, digest, task);
505508

506509
tx.send_lossy(true);
@@ -595,18 +598,9 @@ where
595598
return;
596599
}
597600

598-
let block_for_certify = block.clone();
599-
let verify_rx = marshaled.deferred_verify(embedded_context, block);
601+
let verify_rx =
602+
marshaled.deferred_verify(embedded_context, block, PersistMode::Certified);
600603
if let Ok(result) = verify_rx.await {
601-
if result
602-
&& !marshaled
603-
.marshal
604-
.certified(round, block_for_certify)
605-
.await
606-
{
607-
debug!(?round, "marshal unable to accept certified block");
608-
return;
609-
}
610604
tx.send_lossy(result);
611605
}
612606
});

consensus/src/marshal/standard/inline.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use crate::{
5050
standard::{
5151
validation::{
5252
fetch_parent, precheck_epoch_and_reproposal, verify_with_parent, Decision,
53+
PersistMode,
5354
},
5455
Standard,
5556
},
@@ -411,6 +412,7 @@ where
411412
&mut application,
412413
&mut marshal,
413414
&mut tx,
415+
PersistMode::Verified,
414416
)
415417
.await
416418
{

consensus/src/marshal/standard/validation.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ pub(super) enum Decision<B> {
5858
Continue(B),
5959
}
6060

61+
/// Which marshal cache a verified block should land in.
62+
///
63+
/// Selected by the caller based on context: `Verified` for the initial verify
64+
/// path, `Certified` when the caller has a notarization for the block.
65+
#[derive(Clone, Copy, Debug)]
66+
pub(super) enum PersistMode {
67+
/// Write to `verified_blocks` via `Mailbox::verified`.
68+
Verified,
69+
/// Write to `notarized_blocks` via `Mailbox::certified`.
70+
Certified,
71+
}
72+
6173
/// Performs shared pre-checks used by both inline and deferred verification paths.
6274
///
6375
/// This enforces:
@@ -125,6 +137,7 @@ pub(super) async fn verify_with_parent<E, S, A, B>(
125137
application: &mut A,
126138
marshal: &mut Mailbox<S, Standard<B>>,
127139
tx: &mut oneshot::Sender<bool>,
140+
persist: PersistMode,
128141
) -> Option<bool>
129142
where
130143
E: Rng + Spawner + Metrics + Clock,
@@ -201,9 +214,15 @@ where
201214
valid = validity_request => valid,
202215
};
203216

204-
if application_valid && !marshal.verified(context.round, block).await {
205-
debug!(round = ?context.round, "marshal unable to accept block");
206-
return None;
217+
if application_valid {
218+
let persisted = match persist {
219+
PersistMode::Verified => marshal.verified(context.round, block).await,
220+
PersistMode::Certified => marshal.certified(context.round, block).await,
221+
};
222+
if !persisted {
223+
debug!(round = ?context.round, "marshal unable to accept block");
224+
return None;
225+
}
207226
}
208227
Some(application_valid)
209228
}

0 commit comments

Comments
 (0)