Skip to content

fix: don't build flashblocks with more gas than block gas limit #567

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 3 commits into from
Apr 25, 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
88 changes: 88 additions & 0 deletions crates/op-rbuilder/src/integration/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,4 +490,92 @@ mod tests {

Ok(())
}

#[tokio::test]
#[cfg(feature = "flashblocks")]
async fn integration_test_flashblocks_respects_gas_limit() -> eyre::Result<()> {
// This is a simple test using the integration framework to test that the chain
// produces blocks.
let mut framework =
IntegrationFramework::new("integration_test_flashblocks_respects_gas_limit").unwrap();

// we are going to use a genesis file pre-generated before the test
let mut genesis_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
genesis_path.push("../../genesis.json");
assert!(genesis_path.exists());

let block_time_ms = 1000;
let flashblock_time_ms = 100;

// create the builder
let builder_data_dir = std::env::temp_dir().join(Uuid::new_v4().to_string());
let op_rbuilder_config = OpRbuilderConfig::new()
.chain_config_path(genesis_path.clone())
.data_dir(builder_data_dir)
.auth_rpc_port(1244)
.network_port(1245)
.http_port(1248)
.with_builder_private_key(BUILDER_PRIVATE_KEY)
.with_flashblocks_ws_url("localhost:1249")
.with_chain_block_time(block_time_ms)
.with_flashbots_block_time(flashblock_time_ms);

// create the validation reth node
let reth_data_dir = std::env::temp_dir().join(Uuid::new_v4().to_string());
let reth = OpRethConfig::new()
.chain_config_path(genesis_path)
.data_dir(reth_data_dir)
.auth_rpc_port(1246)
.network_port(1247);

framework.start("op-reth", &reth).await.unwrap();

let op_rbuilder = framework
.start("op-rbuilder", &op_rbuilder_config)
.await
.unwrap();

let engine_api = EngineApi::new("http://localhost:1244").unwrap();
let validation_api = EngineApi::new("http://localhost:1246").unwrap();

let mut generator = BlockGenerator::new(
&engine_api,
Some(&validation_api),
false,
block_time_ms / 1000,
None,
);
generator.init().await?;

let provider = ProviderBuilder::<Identity, Identity, Optimism>::default()
.on_http("http://localhost:1248".parse()?);

// Delay the payload building by 4s, ensure that the correct number of flashblocks are built
let block_hash = generator.generate_block_with_delay(4).await?;

// query the block and the transactions inside the block
let block = provider
.get_block_by_hash(block_hash)
.await?
.expect("block");

for hash in block.transactions.hashes() {
let _ = provider
.get_transaction_receipt(hash)
.await?
.expect("receipt");
}

op_rbuilder
.find_log_line("Processing new chain commit") // no builder tx for flashblocks builder
.await?;

// check there's no more than 10 flashblocks log lines (2000ms / 200ms)
op_rbuilder.find_log_line("Building flashblock 9").await?;
op_rbuilder
.find_log_line("Skipping flashblock reached target=10 idx=10")
.await?;

Ok(())
}
}
2 changes: 1 addition & 1 deletion crates/op-rbuilder/src/integration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl ServiceInstance {
if contents.contains(pattern) {
Ok(())
} else {
Err(eyre::eyre!("Pattern not found in log file"))
Err(eyre::eyre!("Pattern not found in log file: {}", pattern))
}
}
}
Expand Down
22 changes: 18 additions & 4 deletions crates/op-rbuilder/src/payload_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ pub struct OpPayloadBuilder<Pool, Client> {
pub chain_block_time: u64,
/// Flashblock block time
pub flashblock_block_time: u64,
/// Number of flashblocks per block
pub flashblocks_per_block: u64,
/// The metrics for the builder
pub metrics: OpRBuilderMetrics,
}
Expand Down Expand Up @@ -262,6 +264,7 @@ impl<Pool, Client> OpPayloadBuilder<Pool, Client> {
tx,
chain_block_time,
flashblock_block_time,
flashblocks_per_block: chain_block_time / flashblock_block_time,
metrics: Default::default(),
}
}
Expand Down Expand Up @@ -423,8 +426,8 @@ where
return Ok(());
}

let gas_per_batch =
ctx.block_gas_limit() / (self.chain_block_time / self.flashblock_block_time);
let gas_per_batch = ctx.block_gas_limit() / self.flashblocks_per_block;

let mut total_gas_per_batch = gas_per_batch;

let mut flashblock_count = 0;
Expand Down Expand Up @@ -482,11 +485,22 @@ where
// Exit loop if channel closed or cancelled
match received {
Some(()) => {
if flashblock_count >= self.flashblocks_per_block {
tracing::info!(
target: "payload_builder",
"Skipping flashblock reached target={} idx={}",
self.flashblocks_per_block,
flashblock_count
);
continue;
}

// Continue with flashblock building
tracing::info!(
target: "payload_builder",
"Building flashblock {}",
"Building flashblock {} {}",
flashblock_count,
total_gas_per_batch,
);

let flashblock_build_start_time = Instant::now();
Expand All @@ -512,7 +526,7 @@ where
&mut info,
&mut db,
best_txs,
total_gas_per_batch,
total_gas_per_batch.min(ctx.block_gas_limit()),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This technically isn't necessary given we skip building the flashblock if it's out of the expected bounds. However this seems like a sensible check to keep IMO

)?;
ctx.metrics
.payload_tx_simulation_duration
Expand Down
18 changes: 14 additions & 4 deletions crates/op-rbuilder/src/tester/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,11 @@ impl<'a> BlockGenerator<'a> {
}

/// Helper function to submit a payload and update chain state
async fn submit_payload(&mut self, transactions: Option<Vec<Bytes>>) -> eyre::Result<B256> {
async fn submit_payload(
&mut self,
transactions: Option<Vec<Bytes>>,
block_building_delay_secs: u64,
) -> eyre::Result<B256> {
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
Expand Down Expand Up @@ -395,7 +399,8 @@ impl<'a> BlockGenerator<'a> {
}

if !self.no_tx_pool {
tokio::time::sleep(tokio::time::Duration::from_secs(self.block_time_secs)).await;
let sleep_time = self.block_time_secs + block_building_delay_secs;
tokio::time::sleep(tokio::time::Duration::from_secs(sleep_time)).await;
}

let payload = if let Some(flashblocks_service) = &self.flashblocks_service {
Expand Down Expand Up @@ -450,7 +455,11 @@ impl<'a> BlockGenerator<'a> {

/// Generate a single new block and return its hash
pub async fn generate_block(&mut self) -> eyre::Result<B256> {
self.submit_payload(None).await
self.submit_payload(None, 0).await
}

pub async fn generate_block_with_delay(&mut self, delay: u64) -> eyre::Result<B256> {
self.submit_payload(None, delay).await
}

/// Submit a deposit transaction to seed an account with ETH
Expand All @@ -473,7 +482,8 @@ impl<'a> BlockGenerator<'a> {
let signed_tx = signer.sign_tx(OpTypedTransaction::Deposit(deposit_tx))?;
let signed_tx_rlp = signed_tx.encoded_2718();

self.submit_payload(Some(vec![signed_tx_rlp.into()])).await
self.submit_payload(Some(vec![signed_tx_rlp.into()]), 0)
.await
}
}

Expand Down
Loading