Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
06e3306
perf(blockchain): run the beacon state transition while the engine ve…
MegaRedHand Oct 30, 2026
b95be16
perf(blockchain): time the payload's execution apart from the import'…
MegaRedHand Oct 30, 2026
088dfc2
perf(blockchain): time a gloas envelope's newPayload with the same me…
MegaRedHand Nov 2, 2026
ef449e7
Merge main into perf/engine-stf-overlap
MegaRedHand Nov 4, 2026
880fc79
Merge main into perf/engine-stf-overlap
MegaRedHand Nov 11, 2026
de6fc2b
Merge main into perf/engine-stf-overlap
MegaRedHand Nov 18, 2026
eea3acd
Merge main into perf/engine-stf-overlap
MegaRedHand Nov 18, 2026
1632eac
Merge main into perf/engine-stf-overlap
MegaRedHand Nov 18, 2026
20af928
Merge main into perf/engine-stf-overlap
MegaRedHand Nov 18, 2026
257ae54
refactor(blockchain): drop the dead engine asks and name the speculat…
MegaRedHand Nov 19, 2026
2f44d99
fix(fork_choice): check a used speculation in debug builds and never …
MegaRedHand Nov 19, 2026
e84a453
fix(blockchain): abort an abandoned engine request and keep a failed …
MegaRedHand Nov 19, 2026
1290c37
test(blockchain): drive the overlapped import against an answering ex…
MegaRedHand Nov 19, 2026
75f22b0
fix(fork_choice): speculate only on a block that passes on_block's pr…
MegaRedHand Nov 19, 2026
e46a632
refactor(fork_choice): decide a speculation's use from the engine on_…
MegaRedHand Nov 19, 2026
f38acb2
fix(blockchain): keep building the engine request inside the engine row
MegaRedHand Nov 19, 2026
ba404f3
test(fork_choice): pin the stray-parent premise and correct two specu…
MegaRedHand Nov 19, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/blockchain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ libssz.workspace = true
spawned-concurrency.workspace = true

tokio.workspace = true
tokio-util = { version = "0.7", default-features = false }
tokio-util = { version = "0.7", default-features = false, features = ["rt"] }

serde.workspace = true
thiserror.workspace = true
Expand Down
139 changes: 95 additions & 44 deletions crates/blockchain/src/beacon_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
//! Gloas is the exception: its payload is revealed in an envelope, so its
//! request ([`gloas_new_payload_request`]) needs the block and the envelope.

use std::time::Instant;

use ethlambda_engine::types::PayloadStatusValue;
use ethlambda_engine::{EngineClient, EngineError, PayloadStatusV1};
use ethlambda_state_transition::beacon::containers::{self, SignedBeaconBlock};
Expand Down Expand Up @@ -178,66 +180,115 @@ pub fn verdict(status: &PayloadStatusV1) -> PayloadValidity {
})
}

/// Asks the execution client about `block`'s payload.
/// [`NewPayloadRequest`] with its own copy of the payload, so it can move into
/// a spawned task.
///
/// `Ok(None)` means there was nothing to ask, which is not a failure:
/// [`PayloadValidity::NotRequired`] is the right verdict and the block imports.
/// `Err` means no answer was obtained after the client's whole retry ladder, and
/// `optimistic-sync.md` requires the caller not to import the block and not to
/// touch fork choice.
pub async fn ask(
client: &EngineClient,
block: &SignedBeaconBlock,
) -> Result<Option<PayloadValidity>, EngineError> {
let Some(request) = new_payload_request(block) else {
return Ok(None);
};
let status = client
.new_payload(
request.execution_payload,
&request.versioned_hashes,
request.parent_beacon_block_root,
&request.execution_requests,
)
.await?;
Ok(Some(verdict(&status)))
/// The chain actor sends the question from a task of its own and runs the
/// block's state transition meanwhile (see `process_block`), and a task cannot
/// borrow the block the actor goes on using. Only the payload is copied: the
/// rest of the request is already owned, and the block itself, which also
/// carries the attestations and the other operations, is never cloned.
pub struct OwnedNewPayloadRequest {
execution_payload: containers::deneb::ExecutionPayload,
versioned_hashes: Vec<Bytes32>,
parent_beacon_block_root: Root,
execution_requests: Vec<Vec<u8>>,
}

/// Asks the execution client about the payload `envelope` reveals for `block`,
/// with `engine_newPayloadV5`.
impl NewPayloadRequest<'_> {
/// Copies the borrowed payload so the request outlives the block.
pub fn into_owned(self) -> OwnedNewPayloadRequest {
OwnedNewPayloadRequest {
execution_payload: self.execution_payload.clone(),
versioned_hashes: self.versioned_hashes,
parent_beacon_block_root: self.parent_beacon_block_root,
execution_requests: self.execution_requests,
}
}
}

/// Asks the execution client about a block's payload, with a request already
/// built by [`new_payload_request`], which is how the actor sends it from a
/// task while it computes the transition.
///
/// Never `Ok(None)`: unlike [`ask`] there is no "nothing to ask" reading, and a
/// block/envelope pair that does not match is an
/// [`EngineError::EnvelopeMismatch`], which the caller must treat as a refusal
/// to import.
pub async fn ask_envelope(
/// `Err` means no verdict: no answer after the client's whole retry ladder, or
/// a JSON-RPC error answer ([`EngineError::Rpc`]), which is not retried.
/// `optimistic-sync.md` then requires the caller not to import the block and
/// not to touch fork choice. There is no "nothing to ask" reading: a request
/// existing means there was something to ask, and a block with none (pre-merge,
/// gloas) is [`PayloadValidity::NotRequired`] without a call.
pub async fn ask_request(
client: &EngineClient,
block: &SignedBeaconBlock,
envelope: &containers::gloas::SignedExecutionPayloadEnvelope,
request: OwnedNewPayloadRequest,
) -> Result<PayloadValidity, EngineError> {
ask_envelope_status(client, block, envelope)
.await
.map(|status| verdict(&status))
let call = client.new_payload(
&request.execution_payload,
&request.versioned_hashes,
request.parent_beacon_block_root,
&request.execution_requests,
);
let status = timed_new_payload(call).await?;
Ok(verdict(&status))
}

/// [`ask_envelope`] with the engine's raw answer, for a caller that must tell
/// Awaits `call`, one `engine_newPayload*` request, and observes the time from
/// sending it to its answer as `lean_engine_new_payload_seconds`, under the
/// [`outcome_label`] of the verdict the answer reads as.
///
/// Every request this module sends goes through here, a block's payload and an
/// envelope's alike, so the metric is the execution client's time for every
/// payload it was asked about. A request refused before it is sent (an
/// envelope that does not match its block) never reaches it.
async fn timed_new_payload(
call: impl Future<Output = Result<PayloadStatusV1, EngineError>>,
) -> Result<PayloadStatusV1, EngineError> {
let sent = Instant::now();
let status = call.await;
let validity = status.as_ref().ok().map(verdict);
let outcome = outcome_label(validity.as_ref());
crate::metrics::observe_engine_new_payload(outcome, sent.elapsed());
status
}

/// The `outcome` an answer is counted under: its verdict, or `no_verdict`
/// when there is none. That is either the retry ladder running out without an
/// answer, or a JSON-RPC error answer ([`EngineError::Rpc`]), which ends the
/// call at its first attempt since asking again would get the same refusal.
///
/// `not_required` is listed for completeness: a status from the execution
/// client always reads as one of the other three verdicts.
pub fn outcome_label(verdict: Option<&PayloadValidity>) -> &'static str {
match verdict {
Some(PayloadValidity::Validated) => "valid",
Some(PayloadValidity::Optimistic) => "optimistic",
Some(PayloadValidity::Invalidated { .. }) => "invalidated",
Some(PayloadValidity::NotRequired) => "not_required",
None => "no_verdict",
}
}

/// Asks the execution client about the payload `envelope` reveals for `block`,
/// with `engine_newPayloadV5`, and hands back its raw answer.
///
/// Raw rather than read through [`verdict`], since the caller must tell
/// `INVALID_BLOCK_HASH` (the envelope's contents do not hash to its claimed
/// block hash, so only that envelope is bad) from `INVALID` (the payload the
/// hash names is bad).
/// hash names is bad). Never "nothing to ask": a block/envelope pair that does
/// not match is an [`EngineError::EnvelopeMismatch`], which the caller must
/// treat as a refusal to import.
pub async fn ask_envelope_status(
client: &EngineClient,
block: &SignedBeaconBlock,
envelope: &containers::gloas::SignedExecutionPayloadEnvelope,
) -> Result<PayloadStatusV1, EngineError> {
let request = gloas_new_payload_request(block, envelope)?;
client
.new_payload_v5(
request.execution_payload,
&request.versioned_hashes,
request.parent_beacon_block_root,
&request.execution_requests,
)
.await
let call = client.new_payload_v5(
request.execution_payload,
&request.versioned_hashes,
request.parent_beacon_block_root,
&request.execution_requests,
);
timed_new_payload(call).await
}

#[cfg(test)]
Expand Down
20 changes: 12 additions & 8 deletions crates/blockchain/src/beacon_envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@ impl BlockChainServer {
}

#[cfg(test)]
mod tests {
pub(crate) mod tests {
use std::path::PathBuf;
use std::sync::Arc;

Expand Down Expand Up @@ -2189,23 +2189,27 @@ mod tests {

/// What the mock execution client answers and has been asked.
#[derive(Default)]
struct MockState {
/// The `payloadStatus` object `engine_newPayloadV5` answers with.
pub(crate) struct MockState {
/// The `payloadStatus` object every `engine_newPayload*` answers with:
/// a gloas envelope's V5 and a pre-gloas block's V4 alike.
new_payload: String,
/// The `payloadStatus` object `engine_forkchoiceUpdated*` answers with.
forkchoice: String,
/// Request bodies, in arrival order.
requests: Vec<String>,
}

fn status_json(status: &str, latest_valid_hash: Option<H256>) -> String {
pub(crate) fn status_json(status: &str, latest_valid_hash: Option<H256>) -> String {
let hash = latest_valid_hash.map_or("null".to_string(), |hash| format!("\"{hash:?}\""));
format!(r#"{{"status":"{status}","latestValidHash":{hash},"validationError":null}}"#)
}

/// A hand-rolled JSON-RPC server answering by method name, like
/// `ethlambda-engine`'s wire smoke tests but for many requests.
async fn mock_engine(
///
/// Shared with the block import tests in `lib.rs`, which ask V4 about a
/// fulu block's payload, so every `engine_newPayload*` gets `new_payload`.
pub(crate) async fn mock_engine(
new_payload: String,
forkchoice: String,
) -> (EngineClient, Arc<std::sync::Mutex<MockState>>) {
Expand Down Expand Up @@ -2262,13 +2266,13 @@ mod tests {
let result = {
let mut state = shared.lock().unwrap();
state.requests.push(body.clone());
if body.contains("engine_newPayloadV5") {
if body.contains("engine_newPayload") {
state.new_payload.clone()
} else {
state.forkchoice.clone()
}
};
let result = if body.contains("engine_newPayloadV5") {
let result = if body.contains("engine_newPayload") {
result
} else {
format!(r#"{{"payloadStatus":{result},"payloadId":null}}"#)
Expand All @@ -2294,7 +2298,7 @@ mod tests {
(client, state)
}

fn methods(state: &Arc<std::sync::Mutex<MockState>>) -> Vec<String> {
pub(crate) fn methods(state: &Arc<std::sync::Mutex<MockState>>) -> Vec<String> {
state
.lock()
.unwrap()
Expand Down
Loading
Loading