Skip to content

Commit 4fc9a71

Browse files
committed
fix: make block query cutoff properly configurable
1 parent fa557af commit 4fc9a71

File tree

7 files changed

+45
-21
lines changed

7 files changed

+45
-21
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ signet-constants = { version = "0.16.0-rc.0" }
2424
signet-sim = { version = "0.16.0-rc.0" }
2525
signet-tx-cache = { version = "0.16.0-rc.0" }
2626
signet-types = { version = "0.16.0-rc.0" }
27+
signet-bundle = { version = "0.16.0-rc.0" }
2728
signet-zenith = { version = "0.16.0-rc.0" }
2829
signet-block-processor = { git = "https://github.com/init4tech/node-components", tag = "v0.16.0-rc.2" }
2930
signet-genesis = { git = "https://github.com/init4tech/node-components", tag = "v0.16.0-rc.2" }
@@ -64,12 +65,12 @@ alloy-chains = "0.2"
6465
# comment / uncomment for local dev
6566
# [patch.crates-io]
6667
# signet-constants = { path = "../signet-sdk/crates/constants" }
68+
# signet-sim = { path = "../signet-sdk/crates/sim" }
69+
# signet-tx-cache = { path = "../signet-sdk/crates/tx-cache" }
6770
# signet-types = { path = "../signet-sdk/crates/types" }
6871
# signet-zenith = { path = "../signet-sdk/crates/zenith" }
69-
# signet-sim = { path = "../signet-sdk/crates/sim" }
72+
7073
# signet-evm = { path = "../signet-sdk/crates/evm" }
7174
# signet-extract = { path = "../signet-sdk/crates/extract" }
7275
# signet-journal = { path = "../signet-sdk/crates/journal" }
73-
# signet-tx-cache = { path = "../signet-sdk/crates/tx-cache" }
7476
# signet-bundle = { path = "../signet-sdk/crates/bundle" }
75-
# init4-bin-base = { path = "../bin-base" }

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Finally, if it's non-empty, the submit task attempts to get a signature for the
8686
The Builder is configured via environment variables. The following values are supported for configuration.
8787

8888
Key | Required | Description
89-
----------------------------- | -------- | ------------------------------------------------------------------------------
89+
----------------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------
9090
`RUST_LOG` | No | The log level of the builder
9191
`CHAIN_NAME` | No | The chain name ("pecorino", or the corresponding name)
9292
`HOST_RPC_URL` | Yes | RPC endpoint for the host chain
@@ -97,7 +97,8 @@ Key | Required | Description
9797
`FLASHBOTS_ENDPOINT` | No | Flashbots API to submit blocks to
9898
`ROLLUP_BLOCK_GAS_LIMIT` | No | Override for rollup block gas limit
9999
`MAX_HOST_GAS_COEFFICIENT` | No | Optional maximum host gas coefficient, as a percentage, to use when building blocks
100-
`BUILDER_KEY` | Yes | AWS KMS key ID _or_ local private key for builder signing
100+
`BUILDER_KEY` | Yes | AWS KMS key ID _or_ local private key for builder signin
101+
`BLOCK_QUERY_CUTOFF_BUFFER` | Yes | Number of milliseconds before the end of the slot to stop querying for new transactions and start the block signing and submission process
101102
`AWS_ACCESS_KEY_ID` | No | AWS secret access key ID (required if not using `BUILDER_KEY`)
102103
`AWS_SECRET_ACCESS_KEY` | No | AWS secret access key (required if not using `BUILDER_KEY`)
103104
`AWS_DEFAULT_REGION` | No | AWS region for the KMS key in question (required if not using `BUILDER_KEY`)

src/config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@ pub struct BuilderConfig {
155155
)]
156156
pub max_host_gas_coefficient: Option<u8>,
157157

158+
/// Number of seconds before the end of the slot to stop querying for new blocks
159+
#[from_env(
160+
var = "BLOCK_QUERY_CUTOFF_BUFFER",
161+
desc = "Number of milliseconds before the end of the slot to stop querying for new transactions and start the block signing and submission process",
162+
default = 3000
163+
)]
164+
pub block_query_cutoff_buffer: u64,
165+
158166
/// The slot calculator for the builder.
159167
pub slot_calculator: SlotCalculator,
160168

src/tasks/block/sim.rs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ impl SimulatorTask {
117117
/// A `Result` containing the built block or an error.
118118
#[instrument(skip_all, fields(
119119
tx_count = sim_items.len(),
120-
millis_to_deadline = finish_by.duration_since(Instant::now()).as_millis()
120+
millis_to_deadline = finish_by.duration_since(Instant::now()).as_millis(),
121+
host_block_number = sim_env.rollup_block_number(),
122+
rollup_block_number = sim_env.host_block_number(),
121123
))]
122124
pub async fn handle_build(
123125
&self,
@@ -128,9 +130,12 @@ impl SimulatorTask {
128130
let concurrency_limit = self.config.concurrency_limit();
129131

130132
let rollup_env = sim_env.sim_rollup_env(self.constants(), self.ru_provider.clone());
131-
132133
let host_env = sim_env.sim_host_env(self.constants(), self.host_provider.clone());
133134

135+
let ru_number = rollup_env.block().number;
136+
let host_number = host_env.block().number;
137+
debug!(?ru_number, ?host_number, "starting block simulation");
138+
134139
let block_build = BlockBuild::new(
135140
rollup_env,
136141
host_env,
@@ -227,24 +232,31 @@ impl SimulatorTask {
227232
}
228233
}
229234

230-
/// Calculates the deadline for the current block simulation.
235+
/// Calculates the deadline for the current block simulation in milliseconds.
231236
///
232237
/// # Returns
233238
///
234-
/// An `Instant` representing the simulation deadline, as calculated by
235-
/// determining the time left in the current slot and adding that to the
236-
/// current timestamp in UNIX seconds.
239+
/// An `Instant` representing the simulation deadline as calculated by determining
240+
/// the milliseconds left in the current slot and adding that to the current
241+
/// timestamp in UNIX seconds.
237242
pub fn calculate_deadline(&self) -> Instant {
238-
// Get the current timepoint within the slot.
239-
let timepoint =
240-
self.slot_calculator().current_point_within_slot().expect("host chain has started");
243+
// Get the current number of milliseconds into the slot.
244+
let timepoint_ms =
245+
self.slot_calculator().current_point_within_slot_ms().expect("host chain has started");
246+
let slot_duration = Duration::from_secs(self.slot_calculator().slot_duration()).as_millis();
247+
let elapsed_in_slot = Duration::from_millis(timepoint_ms);
248+
let query_cutoff_buffer = Duration::from_millis(self.config.block_query_cutoff_buffer);
241249

242-
// We have the timepoint in seconds into the slot. To find out what's
243-
// remaining, we need to subtract it from the slot duration
244-
// we also subtract 3 seconds to account for the sequencer stopping signing.
245-
let remaining = (self.slot_calculator().slot_duration() - timepoint).saturating_sub(3);
250+
// To find the remaining slot time, subtract the timepoint from the slot duration.
251+
// Then subtract the block query cutoff buffer from the slot duration to account for
252+
// the sequencer stopping signing.
253+
let remaining = slot_duration
254+
.saturating_sub(elapsed_in_slot.as_millis())
255+
.saturating_sub(query_cutoff_buffer.as_millis());
246256

247-
let deadline = Instant::now() + Duration::from_secs(remaining);
257+
// The deadline is then calculated by adding the remaining time from this instant.
258+
// NB: Downcast is okay because u64 will work for 500 million+ years.
259+
let deadline = Instant::now() + Duration::from_millis(remaining as u64);
248260
deadline.max(Instant::now())
249261
}
250262
}

src/tasks/env.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
};
66
use alloy::{
77
consensus::Header,
8-
eips::eip1559::BaseFeeParams,
8+
eips::{BlockId, eip1559::BaseFeeParams},
99
network::Ethereum,
1010
primitives::{B256, U256},
1111
providers::{Provider, network::Network},
@@ -64,7 +64,7 @@ impl Environment {
6464

6565
/// Create a new [`AlloyDB`] for this environment using the given provider.
6666
pub fn alloy_db<N: Network, P: Provider<N>>(&self, provider: P) -> AlloyDB<N, P> {
67-
AlloyDB::new(provider, self.prev_header.number.into())
67+
AlloyDB::new(provider, BlockId::latest())
6868
}
6969
}
7070

src/test_utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub fn setup_test_config() -> &'static BuilderConfig {
5454
1740681556, // pecorino start timestamp as sane default
5555
0, 1,
5656
),
57+
block_query_cutoff_buffer: 3000,
5758
max_host_gas_coefficient: Some(80),
5859
constants: SignetSystemConstants::parmigiana(),
5960
}

0 commit comments

Comments
 (0)