Skip to content

feat: block selection policy #242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 5 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tokio::signal::unix::{SignalKind, signal as unix_signal};
use tracing::{Level, info};

use crate::{
DebugClient, ProxyLayer, RollupBoostServer, RpcClient,
BlockSelectionPolicy, DebugClient, ProxyLayer, RollupBoostServer, RpcClient,
client::rpc::{BuilderArgs, L2ClientArgs},
debug_api::ExecutionMode,
init_metrics, init_tracing,
Expand Down Expand Up @@ -87,6 +87,9 @@ pub struct Args {
/// Execution mode to start rollup boost with
#[arg(long, env, default_value = "enabled")]
pub execution_mode: ExecutionMode,

#[arg(long, env)]
pub block_selection_policy: Option<BlockSelectionPolicy>,
}

impl Args {
Expand Down Expand Up @@ -160,6 +163,7 @@ impl Args {
l2_client,
builder_client,
self.execution_mode,
self.block_selection_policy,
probes,
self.health_check_interval,
self.max_unsafe_interval,
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ pub use health::*;

mod payload;
pub use payload::*;

mod selection;
pub use selection::*;
23 changes: 22 additions & 1 deletion src/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,27 @@ impl OpExecutionPayloadEnvelope {
OpExecutionPayloadEnvelope::V4(_) => PayloadVersion::V4,
}
}

pub fn gas_used(&self) -> u64 {
match self {
OpExecutionPayloadEnvelope::V3(payload) => {
payload
.execution_payload
.payload_inner
.payload_inner
.gas_used
}

OpExecutionPayloadEnvelope::V4(payload) => {
payload
.execution_payload
.payload_inner
.payload_inner
.payload_inner
.gas_used
}
}
}
}

impl From<OpExecutionPayloadEnvelope> for ExecutionPayload {
Expand Down Expand Up @@ -107,7 +128,7 @@ impl PayloadVersion {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PayloadSource {
L2,
Builder,
Expand Down
109 changes: 109 additions & 0 deletions src/selection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use crate::{OpExecutionPayloadEnvelope, PayloadSource};
use serde::{Deserialize, Serialize};

/// Defines the strategy for choosing between the builder block and the L2 client block
/// during block production.
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, clap::ValueEnum)]
pub enum BlockSelectionPolicy {
/// Selects the block based on gas usage.
///
/// If the builder block uses less than 10% of the gas used by the L2 client block,
/// the L2 block is selected instead. This prevents propagation of valid but empty
/// builder blocks and mitigates issues where the builder is not receiving enough
/// transactions due to networking or peering failures.
GasUsed,
}

impl BlockSelectionPolicy {
pub fn select_block(
&self,
builder_payload: OpExecutionPayloadEnvelope,
l2_payload: OpExecutionPayloadEnvelope,
) -> (OpExecutionPayloadEnvelope, PayloadSource) {
match self {
BlockSelectionPolicy::GasUsed => {
let builder_gas = builder_payload.gas_used() as f64;
let l2_gas = l2_payload.gas_used() as f64;

// Select the L2 block if the builder block uses less than 10% of the gas.
// This avoids selecting empty or severely underfilled blocks,
if builder_gas < l2_gas * 0.1 {
(l2_payload, PayloadSource::L2)
} else {
(builder_payload, PayloadSource::Builder)
}
}
}
}
}

#[cfg(test)]
mod tests {
use super::*;

use op_alloy_rpc_types_engine::OpExecutionPayloadEnvelopeV4;

#[test]
fn test_gas_used_policy_select_l2_block() -> eyre::Result<()> {
let execution_payload = r#"{"executionPayload":{"parentHash":"0xe927a1448525fb5d32cb50ee1408461a945ba6c39bd5cf5621407d500ecc8de9","feeRecipient":"0x0000000000000000000000000000000000000000","stateRoot":"0x10f8a0830000e8edef6d00cc727ff833f064b1950afd591ae41357f97e543119","receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","prevRandao":"0xe0d8b4521a7da1582a713244ffb6a86aa1726932087386e2dc7973f43fc6cb24","blockNumber":"0x1","gasLimit":"0x2ffbd2","gasUsed":"0x0","timestamp":"0x1235","extraData":"0xd883010d00846765746888676f312e32312e30856c696e7578","baseFeePerGas":"0x342770c0","blockHash":"0x44d0fa5f2f73a938ebb96a2a21679eb8dea3e7b7dd8fd9f35aa756dda8bf0a8a","transactions":[],"withdrawals":[],"blobGasUsed":"0x0","excessBlobGas":"0x0","withdrawalsRoot":"0x123400000000000000000000000000000000000000000000000000000000babe"},"blockValue":"0x0","blobsBundle":{"commitments":[],"proofs":[],"blobs":[]},"shouldOverrideBuilder":false,"parentBeaconBlockRoot":"0xdead00000000000000000000000000000000000000000000000000000000beef","executionRequests":["0xdeadbeef"]}"#;
let mut builder_payload: OpExecutionPayloadEnvelopeV4 =
serde_json::from_str(execution_payload)?;
let mut l2_payload = builder_payload.clone();

let gas_used = 1000000000;
l2_payload
.execution_payload
.payload_inner
.payload_inner
.payload_inner
.gas_used = gas_used;

builder_payload
.execution_payload
.payload_inner
.payload_inner
.payload_inner
.gas_used = (gas_used as f64 * 0.09) as u64;

let builder_payload = OpExecutionPayloadEnvelope::V4(builder_payload);
let l2_payload = OpExecutionPayloadEnvelope::V4(l2_payload);

let selected_payload =
BlockSelectionPolicy::GasUsed.select_block(builder_payload, l2_payload);

assert_eq!(selected_payload.1, PayloadSource::L2);
Ok(())
}

#[test]
fn test_gas_used_policy_select_builder_block() -> eyre::Result<()> {
let execution_payload = r#"{"executionPayload":{"parentHash":"0xe927a1448525fb5d32cb50ee1408461a945ba6c39bd5cf5621407d500ecc8de9","feeRecipient":"0x0000000000000000000000000000000000000000","stateRoot":"0x10f8a0830000e8edef6d00cc727ff833f064b1950afd591ae41357f97e543119","receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","prevRandao":"0xe0d8b4521a7da1582a713244ffb6a86aa1726932087386e2dc7973f43fc6cb24","blockNumber":"0x1","gasLimit":"0x2ffbd2","gasUsed":"0x0","timestamp":"0x1235","extraData":"0xd883010d00846765746888676f312e32312e30856c696e7578","baseFeePerGas":"0x342770c0","blockHash":"0x44d0fa5f2f73a938ebb96a2a21679eb8dea3e7b7dd8fd9f35aa756dda8bf0a8a","transactions":[],"withdrawals":[],"blobGasUsed":"0x0","excessBlobGas":"0x0","withdrawalsRoot":"0x123400000000000000000000000000000000000000000000000000000000babe"},"blockValue":"0x0","blobsBundle":{"commitments":[],"proofs":[],"blobs":[]},"shouldOverrideBuilder":false,"parentBeaconBlockRoot":"0xdead00000000000000000000000000000000000000000000000000000000beef","executionRequests":["0xdeadbeef"]}"#;
let mut builder_payload: OpExecutionPayloadEnvelopeV4 =
serde_json::from_str(execution_payload)?;
let mut l2_payload = builder_payload.clone();

let gas_used = 1000000000;
l2_payload
.execution_payload
.payload_inner
.payload_inner
.payload_inner
.gas_used = gas_used;

builder_payload
.execution_payload
.payload_inner
.payload_inner
.payload_inner
.gas_used = (gas_used as f64 * 0.1) as u64;

let builder_payload = OpExecutionPayloadEnvelope::V4(builder_payload);
let l2_payload = OpExecutionPayloadEnvelope::V4(l2_payload);

let selected_payload =
BlockSelectionPolicy::GasUsed.select_block(builder_payload, l2_payload);

assert_eq!(selected_payload.1, PayloadSource::Builder);
Ok(())
}
}
11 changes: 9 additions & 2 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::BlockSelectionPolicy;
use crate::debug_api::ExecutionMode;
use crate::{
HealthHandle,
Expand Down Expand Up @@ -35,6 +36,7 @@ pub struct RollupBoostServer {
pub l2_client: Arc<RpcClient>,
pub builder_client: Arc<RpcClient>,
pub payload_trace_context: Arc<PayloadTraceContext>,
block_selection_policy: Option<BlockSelectionPolicy>,
health_handle: JoinHandle<()>,
execution_mode: Arc<Mutex<ExecutionMode>>,
probes: Arc<Probes>,
Expand All @@ -45,6 +47,7 @@ impl RollupBoostServer {
l2_client: RpcClient,
builder_client: RpcClient,
initial_execution_mode: ExecutionMode,
block_selection_policy: Option<BlockSelectionPolicy>,
probes: Arc<Probes>,
health_check_interval: u64,
max_unsafe_interval: u64,
Expand All @@ -60,6 +63,7 @@ impl RollupBoostServer {
Self {
l2_client: Arc::new(l2_client),
builder_client: Arc::new(builder_client),
block_selection_policy,
payload_trace_context: Arc::new(PayloadTraceContext::new()),
execution_mode: Arc::new(Mutex::new(initial_execution_mode)),
probes,
Expand Down Expand Up @@ -178,13 +182,15 @@ impl RollupBoostServer {
l2_payload.inspect_err(|_| self.probes.set_health(Health::ServiceUnavailable))?;
self.probes.set_health(Health::Healthy);

if let Ok(Some(payload)) = builder_payload {
if let Ok(Some(builder_payload)) = builder_payload {
// If execution mode is set to DryRun, fallback to the l2_payload,
// otherwise prefer the builder payload
if self.execution_mode().is_dry_run() {
(l2_payload, PayloadSource::L2)
} else if let Some(selection_policy) = &self.block_selection_policy {
selection_policy.select_block(builder_payload, l2_payload)
} else {
(payload, PayloadSource::Builder)
(builder_payload, PayloadSource::Builder)
}
} else {
// Only update the health status if the builder payload fails
Expand Down Expand Up @@ -600,6 +606,7 @@ mod tests {
l2_client,
builder_client,
ExecutionMode::Enabled,
None,
probes,
60,
5,
Expand Down
Loading