Skip to content

Commit

Permalink
fix(engine): skip payload building on already canonical
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrasiuk committed Feb 5, 2024
1 parent 9d1642f commit 5acf39c
Showing 1 changed file with 41 additions and 35 deletions.
76 changes: 41 additions & 35 deletions crates/consensus/beacon/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ where
fn forkchoice_updated(
&mut self,
state: ForkchoiceState,
attrs: Option<EngineT::PayloadAttributes>,
mut attrs: Option<EngineT::PayloadAttributes>,
) -> RethResult<OnForkChoiceUpdated> {
trace!(target: "consensus::engine", ?state, "Received new forkchoice state update");
if state.head_block_hash.is_zero() {
Expand Down Expand Up @@ -376,40 +376,7 @@ where
Ok(outcome) => {
match &outcome {
CanonicalOutcome::AlreadyCanonical { header } => {
// On Optimism, the proposers are allowed to reorg their own chain at will.
cfg_if::cfg_if! {
if #[cfg(feature = "optimism")] {
if self.blockchain.chain_spec().is_optimism() {
debug!(
target: "consensus::engine",
fcu_head_num=?header.number,
current_head_num=?self.blockchain.canonical_tip().number,
"[Optimism] Allowing beacon reorg to old head"
);
let _ = self.update_head(header.clone());
self.listeners.notify(
BeaconConsensusEngineEvent::CanonicalChainCommitted(
Box::new(header.clone()),
elapsed,
),
);
} else {
debug!(
target: "consensus::engine",
fcu_head_num=?header.number,
current_head_num=?self.blockchain.canonical_tip().number,
"Ignoring beacon update to old head"
);
}
} else {
debug!(
target: "consensus::engine",
fcu_head_num=?header.number,
current_head_num=?self.blockchain.canonical_tip().number,
"Ignoring beacon update to old head"
);
}
}
self.on_head_already_canonical(header, &mut attrs);
}
CanonicalOutcome::Committed { head } => {
debug!(
Expand Down Expand Up @@ -473,6 +440,45 @@ where
Ok(OnForkChoiceUpdated::valid(status))
}

/// Invoked when head hash references a `VALID` block that is already canonical.
fn on_head_already_canonical(
&mut self,
header: &SealedHeader,
attrs: &mut Option<EngineT::PayloadAttributes>,
) {
// On Optimism, the proposers are allowed to reorg their own chain at will.
#[cfg(feature = "optimism")]
if self.blockchain.chain_spec().is_optimism() {
debug!(
target: "consensus::engine",
fcu_head_num=?header.number,
current_head_num=?self.blockchain.canonical_tip().number,
"[Optimism] Allowing beacon reorg to old head"
);
let _ = self.update_head(header.clone());
self.listeners.notify(BeaconConsensusEngineEvent::CanonicalChainCommitted(
Box::new(header.clone()),
elapsed,
));
return
}

// 2. Client software MAY skip an update of the forkchoice state and MUST NOT begin a
// payload build process if `forkchoiceState.headBlockHash` references a `VALID` ancestor
// of the head of canonical chain, i.e. the ancestor passed payload validation process
// and deemed `VALID`. In the case of such an event, client software MUST return
// `{payloadStatus: {status: VALID, latestValidHash: forkchoiceState.headBlockHash,
// validationError: null}, payloadId: null}`
attrs.take();

debug!(
target: "consensus::engine",
fcu_head_num=?header.number,
current_head_num=?self.blockchain.canonical_tip().number,
"Ignoring beacon update to old head"
);
}

/// Invoked when we receive a new forkchoice update message.
///
/// Returns `true` if the engine now reached its maximum block number, See
Expand Down

0 comments on commit 5acf39c

Please sign in to comment.