From 42ecfdd62da101d44672f1237c15c5de755faea9 Mon Sep 17 00:00:00 2001 From: cody-wang-cb Date: Mon, 10 Mar 2025 17:40:20 -0400 Subject: [PATCH] Make block times dynamic in flashblock builder (#482) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 📝 Summary Currently the block time is hard coded to assume the chain block time is 1s and flashblock time is 250ms. But for Base the chain block time is 2s. Thus fixing this by adding args to make it dynamic. --- ## ✅ I have completed the following steps: * [x] Run `make lint` * [x] Run `make test` * [x] Added tests (if applicable) --- crates/op-rbuilder/src/args.rs | 14 ++++++++++ .../src/integration/integration_test.rs | 9 +++++-- .../src/integration/op_rbuilder.rs | 22 +++++++++++++++ crates/op-rbuilder/src/main.rs | 2 ++ crates/op-rbuilder/src/payload_builder.rs | 27 ++++++++++++++++--- .../src/payload_builder_vanilla.rs | 20 ++++++++++++-- 6 files changed, 86 insertions(+), 8 deletions(-) diff --git a/crates/op-rbuilder/src/args.rs b/crates/op-rbuilder/src/args.rs index ee2b6e2a..461af0d4 100644 --- a/crates/op-rbuilder/src/args.rs +++ b/crates/op-rbuilder/src/args.rs @@ -24,4 +24,18 @@ pub struct OpRbuilderArgs { default_value = "127.0.0.1:1111" )] pub flashblocks_ws_url: String, + /// chain block time in milliseconds + #[arg( + long = "rollup.chain-block-time", + default_value = "1000", + env = "CHAIN_BLOCK_TIME" + )] + pub chain_block_time: u64, + /// flashblock block time in milliseconds + #[arg( + long = "rollup.flashblock-block-time", + default_value = "250", + env = "FLASHBLOCK_BLOCK_TIME" + )] + pub flashblock_block_time: u64, } diff --git a/crates/op-rbuilder/src/integration/integration_test.rs b/crates/op-rbuilder/src/integration/integration_test.rs index bde67222..deb75fe6 100644 --- a/crates/op-rbuilder/src/integration/integration_test.rs +++ b/crates/op-rbuilder/src/integration/integration_test.rs @@ -370,7 +370,9 @@ mod tests { .network_port(1235) .http_port(1238) .with_builder_private_key(BUILDER_PRIVATE_KEY) - .with_flashblocks_ws_url("localhost:1239"); + .with_flashblocks_ws_url("localhost:1239") + .with_chain_block_time(2000) + .with_flashbots_block_time(200); // create the validation reth node let reth_data_dir = std::env::temp_dir().join(Uuid::new_v4().to_string()); @@ -407,7 +409,7 @@ mod tests { let engine_api = EngineApi::new("http://localhost:1234").unwrap(); let validation_api = EngineApi::new("http://localhost:1236").unwrap(); - let mut generator = BlockGenerator::new(&engine_api, Some(&validation_api), false, 1, None); + let mut generator = BlockGenerator::new(&engine_api, Some(&validation_api), false, 2, None); generator.init().await?; let provider = ProviderBuilder::::default() @@ -434,6 +436,9 @@ mod tests { .find_log_line("Processing new chain commit") // no builder tx for flashblocks builder .await?; + // check there's 10 flashblocks log lines (2000ms / 200ms) + op_rbuilder.find_log_line("Building flashblock 9").await?; + // Process websocket messages let timeout_duration = Duration::from_secs(10); tokio::time::timeout(timeout_duration, async { diff --git a/crates/op-rbuilder/src/integration/op_rbuilder.rs b/crates/op-rbuilder/src/integration/op_rbuilder.rs index e201365d..28f9532d 100644 --- a/crates/op-rbuilder/src/integration/op_rbuilder.rs +++ b/crates/op-rbuilder/src/integration/op_rbuilder.rs @@ -25,6 +25,8 @@ pub struct OpRbuilderConfig { network_port: Option, builder_private_key: Option, flashblocks_ws_url: Option, + chain_block_time: Option, + flashbots_block_time: Option, } impl OpRbuilderConfig { @@ -66,6 +68,16 @@ impl OpRbuilderConfig { self.flashblocks_ws_url = Some(url.to_string()); self } + + pub fn with_chain_block_time(mut self, time: u64) -> Self { + self.chain_block_time = Some(time); + self + } + + pub fn with_flashbots_block_time(mut self, time: u64) -> Self { + self.flashbots_block_time = Some(time); + self + } } impl Service for OpRbuilderConfig { @@ -118,6 +130,16 @@ impl Service for OpRbuilderConfig { .arg(flashblocks_ws_url); } + if let Some(chain_block_time) = self.chain_block_time { + cmd.arg("--rollup.chain-block-time") + .arg(chain_block_time.to_string()); + } + + if let Some(flashbots_block_time) = self.flashbots_block_time { + cmd.arg("--rollup.flashblock-block-time") + .arg(flashbots_block_time.to_string()); + } + cmd } diff --git a/crates/op-rbuilder/src/main.rs b/crates/op-rbuilder/src/main.rs index fb9b3401..2bf83fc7 100644 --- a/crates/op-rbuilder/src/main.rs +++ b/crates/op-rbuilder/src/main.rs @@ -36,6 +36,8 @@ fn main() { .with_components(op_node.components().payload(CustomOpPayloadBuilder::new( builder_args.builder_signer, builder_args.flashblocks_ws_url, + builder_args.chain_block_time, + builder_args.flashblock_block_time, ))) .with_add_ons( OpAddOnsBuilder::default() diff --git a/crates/op-rbuilder/src/payload_builder.rs b/crates/op-rbuilder/src/payload_builder.rs index e6ecf074..ab145402 100644 --- a/crates/op-rbuilder/src/payload_builder.rs +++ b/crates/op-rbuilder/src/payload_builder.rs @@ -89,13 +89,22 @@ pub struct CustomOpPayloadBuilder { #[allow(dead_code)] builder_signer: Option, flashblocks_ws_url: String, + chain_block_time: u64, + flashblock_block_time: u64, } impl CustomOpPayloadBuilder { - pub fn new(builder_signer: Option, flashblocks_ws_url: String) -> Self { + pub fn new( + builder_signer: Option, + flashblocks_ws_url: String, + chain_block_time: u64, + flashblock_block_time: u64, + ) -> Self { Self { builder_signer, flashblocks_ws_url, + chain_block_time, + flashblock_block_time, } } } @@ -126,6 +135,8 @@ where ctx.provider().clone(), Arc::new(BasicOpReceiptBuilder::default()), self.flashblocks_ws_url.clone(), + self.chain_block_time, + self.flashblock_block_time, )) } @@ -199,6 +210,10 @@ pub struct OpPayloadBuilder { pub tx: mpsc::UnboundedSender, /// Node primitive types. pub receipt_builder: Arc>, + /// chain block time + pub chain_block_time: u64, + /// Flashblock block time + pub flashblock_block_time: u64, } impl OpPayloadBuilder { @@ -209,6 +224,8 @@ impl OpPayloadBuilder>, flashblocks_ws_url: String, + chain_block_time: u64, + flashblock_block_time: u64, ) -> Self { let (tx, rx) = mpsc::unbounded_channel(); let subscribers = Arc::new(Mutex::new(Vec::new())); @@ -225,6 +242,8 @@ impl OpPayloadBuilder, #[cfg(feature = "flashblocks")] flashblocks_ws_url: String, + #[cfg(feature = "flashblocks")] + chain_block_time: u64, + #[cfg(feature = "flashblocks")] + flashblock_block_time: u64, } impl CustomOpPayloadBuilder { #[cfg(feature = "flashblocks")] - pub fn new(builder_signer: Option, flashblocks_ws_url: String) -> Self { + pub fn new( + builder_signer: Option, + flashblocks_ws_url: String, + chain_block_time: u64, + flashblock_block_time: u64, + ) -> Self { Self { builder_signer, flashblocks_ws_url, + chain_block_time, + flashblock_block_time, } } #[cfg(not(feature = "flashblocks"))] - pub fn new(builder_signer: Option, _flashblocks_ws_url: String) -> Self { + pub fn new( + builder_signer: Option, + _flashblocks_ws_url: String, + _chain_block_time: u64, + _flashblock_block_time: u64, + ) -> Self { Self { builder_signer } } }