Skip to content

Commit

Permalink
Make block times dynamic in flashblock builder (#482)
Browse files Browse the repository at this point in the history
## 📝 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.

<!--- (Optional) Why is this change required? What problem does it
solve? Remove this section if not applicable. -->

---

## ✅ I have completed the following steps:

* [x] Run `make lint`
* [x] Run `make test`
* [x] Added tests (if applicable)
  • Loading branch information
cody-wang-cb authored Mar 10, 2025
1 parent df005d3 commit 42ecfdd
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 8 deletions.
14 changes: 14 additions & 0 deletions crates/op-rbuilder/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
9 changes: 7 additions & 2 deletions crates/op-rbuilder/src/integration/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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::<Identity, Identity, Optimism>::default()
Expand All @@ -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 {
Expand Down
22 changes: 22 additions & 0 deletions crates/op-rbuilder/src/integration/op_rbuilder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub struct OpRbuilderConfig {
network_port: Option<u16>,
builder_private_key: Option<String>,
flashblocks_ws_url: Option<String>,
chain_block_time: Option<u64>,
flashbots_block_time: Option<u64>,
}

impl OpRbuilderConfig {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 2 additions & 0 deletions crates/op-rbuilder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
27 changes: 23 additions & 4 deletions crates/op-rbuilder/src/payload_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,22 @@ pub struct CustomOpPayloadBuilder {
#[allow(dead_code)]
builder_signer: Option<Signer>,
flashblocks_ws_url: String,
chain_block_time: u64,
flashblock_block_time: u64,
}

impl CustomOpPayloadBuilder {
pub fn new(builder_signer: Option<Signer>, flashblocks_ws_url: String) -> Self {
pub fn new(
builder_signer: Option<Signer>,
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,
}
}
}
Expand Down Expand Up @@ -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,
))
}

Expand Down Expand Up @@ -199,6 +210,10 @@ pub struct OpPayloadBuilder<Pool, Client, EvmConfig, N: NodePrimitives> {
pub tx: mpsc::UnboundedSender<String>,
/// Node primitive types.
pub receipt_builder: Arc<dyn OpReceiptBuilder<N::SignedTx, Receipt = N::Receipt>>,
/// chain block time
pub chain_block_time: u64,
/// Flashblock block time
pub flashblock_block_time: u64,
}

impl<Pool, Client, EvmConfig, N: NodePrimitives> OpPayloadBuilder<Pool, Client, EvmConfig, N> {
Expand All @@ -209,6 +224,8 @@ impl<Pool, Client, EvmConfig, N: NodePrimitives> OpPayloadBuilder<Pool, Client,
client: Client,
receipt_builder: Arc<dyn OpReceiptBuilder<N::SignedTx, Receipt = N::Receipt>>,
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()));
Expand All @@ -225,6 +242,8 @@ impl<Pool, Client, EvmConfig, N: NodePrimitives> OpPayloadBuilder<Pool, Client,
client,
tx,
receipt_builder,
chain_block_time,
flashblock_block_time,
}
}

Expand Down Expand Up @@ -346,8 +365,8 @@ where
return Ok(());
}

// Right now it assumes a 1 second block time (TODO)
let gas_per_batch = ctx.block_gas_limit() / 4;
let gas_per_batch =
ctx.block_gas_limit() / (self.chain_block_time / self.flashblock_block_time);
let mut total_gas_per_batch = gas_per_batch;

let mut flashblock_count = 0;
Expand Down Expand Up @@ -410,7 +429,7 @@ where
total_gas_per_batch += gas_per_batch;
flashblock_count += 1;

std::thread::sleep(std::time::Duration::from_millis(250));
std::thread::sleep(std::time::Duration::from_millis(self.flashblock_block_time));
}
}

Expand Down
20 changes: 18 additions & 2 deletions crates/op-rbuilder/src/payload_builder_vanilla.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,35 @@ pub struct CustomOpPayloadBuilder {
builder_signer: Option<Signer>,
#[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<Signer>, flashblocks_ws_url: String) -> Self {
pub fn new(
builder_signer: Option<Signer>,
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<Signer>, _flashblocks_ws_url: String) -> Self {
pub fn new(
builder_signer: Option<Signer>,
_flashblocks_ws_url: String,
_chain_block_time: u64,
_flashblock_block_time: u64,
) -> Self {
Self { builder_signer }
}
}
Expand Down

0 comments on commit 42ecfdd

Please sign in to comment.