Skip to content

Commit 4da0e6e

Browse files
committed
fix(consensus): Relax block receipt time check when the node is not synced
1 parent a17ed0c commit 4da0e6e

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

node/src/consensus/consensus_reducer.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ impl ConsensusState {
5555
hash: hash.clone(),
5656
block: block.clone(),
5757
};
58-
match state.prevalidate_block(&block) {
58+
let allow_block_too_late = allow_block_too_late(state, &block);
59+
60+
match state.prevalidate_block(&block, allow_block_too_late) {
5961
Ok(()) => {
6062
dispatcher.push(ConsensusAction::BlockPrevalidateSuccess { hash });
6163
}
@@ -334,3 +336,26 @@ impl ConsensusState {
334336
}
335337
}
336338
}
339+
340+
/// Decide if the time-reception check should be done for this block or not.
341+
///
342+
/// The check is skipped if the block's global_slot is greater than the
343+
/// current best tip and the difference greater than 2.
344+
///
345+
/// Ideally we would differentiate between requested blocks and blocks
346+
/// received from gossip, but this difference doesn't really exist
347+
/// in the WebRTC transport, hence this heuristic.
348+
fn allow_block_too_late(state: &crate::State, block: &ArcBlockWithHash) -> bool {
349+
let (has_greater_blobal_slot, diff_with_best_tip) = state
350+
.transition_frontier
351+
.best_tip()
352+
.map(|b| {
353+
(
354+
block.global_slot() > b.global_slot(),
355+
b.global_slot().abs_diff(block.global_slot()),
356+
)
357+
})
358+
.unwrap_or((false, 0));
359+
360+
has_greater_blobal_slot && diff_with_best_tip > 2
361+
}

node/src/state.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ impl State {
379379
pub fn prevalidate_block(
380380
&self,
381381
block: &ArcBlockWithHash,
382+
allow_block_too_late: bool,
382383
) -> Result<(), BlockPrevalidationError> {
383384
let Some((genesis, cur_global_slot)) =
384385
None.or_else(|| Some((self.genesis_block()?, self.cur_global_slot()?)))
@@ -403,7 +404,9 @@ impl State {
403404
current_global_slot: cur_global_slot,
404405
block_global_slot,
405406
});
406-
} else if cur_global_slot.saturating_sub(block_global_slot) > delta {
407+
} else if !allow_block_too_late
408+
&& cur_global_slot.saturating_sub(block_global_slot) > delta
409+
{
407410
// Too_late
408411
return Err(BlockPrevalidationError::ReceivedTooLate {
409412
current_global_slot: cur_global_slot,

0 commit comments

Comments
 (0)