Skip to content

Commit 9f5eaef

Browse files
committed
refactor: fetch header in submit task instead of passing through from env task
1 parent d9fec67 commit 9f5eaef

File tree

6 files changed

+65
-74
lines changed

6 files changed

+65
-74
lines changed

bin/builder.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,17 @@ async fn main() -> eyre::Result<()> {
3535
let zenith = config.connect_zenith(host_provider.clone());
3636

3737
// Set up the metrics task
38-
let metrics = MetricsTask { host_provider };
38+
let metrics = MetricsTask { host_provider: host_provider.clone() };
3939
let (tx_channel, metrics_jh) = metrics.spawn();
4040

4141
// Make a Tx submission task
42-
let submit =
43-
SubmitTask { zenith, quincey, config: config.clone(), outbound_tx_channel: tx_channel };
42+
let submit = SubmitTask {
43+
zenith,
44+
quincey,
45+
config: config.clone(),
46+
outbound_tx_channel: tx_channel,
47+
host_provider: host_provider.clone(),
48+
};
4449

4550
// Set up tx submission
4651
let (submit_channel, submit_jh) = submit.spawn();

src/config.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,7 @@ impl BuilderConfig {
247247
/// Create an [`EnvTask`] using this config.
248248
pub async fn env_task(&self) -> EnvTask {
249249
let ru_provider = self.connect_ru_provider();
250-
let host_provider =
251-
self.connect_host_provider().await.expect("failed to configure host provider");
252-
EnvTask::new(self.clone(), ru_provider, host_provider)
250+
EnvTask::new(self.clone(), ru_provider)
253251
}
254252

255253
/// Spawn a new [`CacheSystem`] using this config. This contains the

src/tasks/block/sim.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,14 @@ impl Simulator {
176176
return;
177177
}
178178
let Some(sim_env) = self.sim_env.borrow_and_update().clone() else { return };
179-
info!(block_number = sim_env.signet.number, "new block environment received");
179+
info!(sim_env.block_env.number, "new block environment received");
180180

181181
// Calculate the deadline for this block simulation.
182182
// NB: This must happen _after_ taking a reference to the sim cache,
183183
// waiting for a new block, and checking current slot authorization.
184184
let finish_by = self.calculate_deadline();
185185
let sim_cache = cache.clone();
186-
match self.handle_build(constants, sim_cache, finish_by, sim_env.signet.clone()).await {
186+
match self.handle_build(constants, sim_cache, finish_by, sim_env.block_env.clone()).await {
187187
Ok(block) => {
188188
debug!(block = ?block.block_number(), tx_count = block.transactions().len(), "built simulated block");
189189
let _ = submit_sender.send(SimResult { block, env: sim_env });

src/tasks/cache/task.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ impl CacheTask {
4545
break;
4646
}
4747
if let Some(env) = self.env.borrow_and_update().as_ref() {
48-
basefee = env.signet.basefee;
49-
info!(basefee, env.signet.number, env.signet.timestamp, "rollup block env changed, clearing cache");
48+
basefee = env.block_env.basefee;
49+
info!(basefee, env.block_env.number, env.block_env.timestamp, "rollup block env changed, clearing cache");
5050
cache.clean(
51-
env.signet.number, env.signet.timestamp
51+
env.block_env.number, env.block_env.timestamp
5252
);
5353
}
5454
}

src/tasks/env.rs

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::config::{BuilderConfig, HostProvider, RuProvider};
1+
use crate::config::{BuilderConfig, RuProvider};
22
use alloy::{
33
consensus::Header,
4-
eips::{BlockId, BlockNumberOrTag, eip1559::BaseFeeParams},
4+
eips::eip1559::BaseFeeParams,
55
primitives::{B256, U256},
66
providers::Provider,
77
};
@@ -18,27 +18,21 @@ pub struct EnvTask {
1818
config: BuilderConfig,
1919
/// Rollup provider is used to get the latest rollup block header for simulation.
2020
ru_provider: RuProvider,
21-
/// Host provider is used to get the previous block header for gas estimation.
22-
host_provider: HostProvider,
2321
}
2422

2523
/// Contains a signet BlockEnv and its corresponding host Header.
2624
#[derive(Debug, Clone)]
2725
pub struct SimEnv {
2826
/// The signet block environment, for rollup block simulation.
29-
pub signet: BlockEnv,
30-
/// The host environment header, for host transaction submission pricing.
31-
pub host: Header,
27+
pub block_env: BlockEnv,
28+
/// The header of the previous rollup block.
29+
pub prev_header: Header,
3230
}
3331

3432
impl EnvTask {
3533
/// Create a new [`EnvTask`] with the given config and providers.
36-
pub const fn new(
37-
config: BuilderConfig,
38-
ru_provider: RuProvider,
39-
host_provider: HostProvider,
40-
) -> Self {
41-
Self { config, ru_provider, host_provider }
34+
pub const fn new(config: BuilderConfig, ru_provider: RuProvider) -> Self {
35+
Self { config, ru_provider }
4236
}
4337

4438
/// Construct a [`BlockEnv`] by from the previous block header.
@@ -89,56 +83,35 @@ impl EnvTask {
8983
info_span!("EnvTask::task_fut::loop", %block_hash, number = tracing::field::Empty);
9084

9185
// Get the rollup header for rollup block simulation environment configuration
92-
let rollup_header =
93-
match self.get_latest_rollup_header(&sender, block_hash, &span).await {
94-
Some(value) => value,
95-
None => continue,
96-
};
97-
debug!(?rollup_header.number, "pulled rollup block for simulation");
98-
99-
// Get the host header for blob transaction submission gas pricing
100-
let host_header = match self.get_host_header().await {
101-
Ok(header) => header,
102-
Err(_) => {
103-
error!("failed to get host header - skipping block");
86+
let rollup_header = match self
87+
.get_latest_rollup_header(&sender, block_hash, &span)
88+
.await
89+
{
90+
Some(value) => value,
91+
None => {
92+
// If we failed to get the rollup header, we skip this iteration.
93+
debug!(%block_hash, "failed to get rollup header - continuint to next block");
10494
continue;
10595
}
10696
};
107-
debug!(?host_header.base_fee_per_gas, "pulled previous host header for gas calculation");
97+
debug!(rollup_header.number, "pulled rollup block for simulation");
10898
span.record("rollup_block_number", rollup_header.number);
10999

110100
// Construct the block env using the previous block header
111-
let signet_env = self.construct_block_env(&host_header);
112-
debug!(
113-
block_number = signet_env.number,
114-
signet_env.basefee, "constructed signet block env"
115-
);
101+
let signet_env = self.construct_block_env(&rollup_header);
102+
debug!(signet_env.number, signet_env.basefee, "constructed signet block env");
116103

117-
if sender.send(Some(SimEnv { signet: signet_env, host: host_header })).is_err() {
104+
if sender
105+
.send(Some(SimEnv { block_env: signet_env, prev_header: rollup_header }))
106+
.is_err()
107+
{
118108
// The receiver has been dropped, so we can stop the task.
119109
debug!("receiver dropped, stopping task");
120110
break;
121111
}
122112
}
123113
}
124114

125-
/// Gets the latest host [`Header`].
126-
/// NB: This doesn't need to correlate perfectly with the rollup blocks,
127-
/// since we only use the previous host block [`Header`] for gas estimation.
128-
async fn get_host_header(&self) -> eyre::Result<Header> {
129-
let previous = self
130-
.host_provider
131-
.get_block(BlockId::Number(BlockNumberOrTag::Latest))
132-
.into_future()
133-
.await?;
134-
debug!(?previous, "got host block for hash");
135-
136-
match previous {
137-
Some(block) => Ok(block.header.inner),
138-
None => Err(eyre::eyre!("host block not found")),
139-
}
140-
}
141-
142115
/// Get latest rollup [`Header`] for the given block hash.
143116
async fn get_latest_rollup_header(
144117
&self,

src/tasks/submit.rs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use crate::{
22
config::{HostProvider, ZenithInstance},
33
quincey::Quincey,
4-
tasks::env::SimEnv,
54
utils::extract_signature_components,
65
};
76
use alloy::{
8-
consensus::{Header, SimpleCoder, constants::GWEI_TO_WEI},
9-
eips::BlockNumberOrTag,
7+
consensus::{constants::GWEI_TO_WEI, Header, SimpleCoder},
8+
eips::{BlockId, BlockNumberOrTag},
109
network::{TransactionBuilder, TransactionBuilder4844},
1110
primitives::{Bytes, FixedBytes, TxHash, U256},
1211
providers::{Provider as _, SendableTx, WalletProvider},
@@ -196,6 +195,8 @@ pub struct SubmitTask {
196195
pub config: crate::config::BuilderConfig,
197196
/// Channel over which to send pending transactions
198197
pub outbound_tx_channel: mpsc::UnboundedSender<TxHash>,
198+
/// Host provider for sending transactions and fetching block & header info
199+
pub host_provider: HostProvider,
199200
}
200201

201202
impl SubmitTask {
@@ -241,9 +242,8 @@ impl SubmitTask {
241242
retry_count: usize,
242243
resp: &SignResponse,
243244
block: &BuiltBlock,
244-
sim_env: &SimEnv,
245245
) -> eyre::Result<ControlFlow> {
246-
let tx = self.prepare_tx(retry_count, resp, block, sim_env).await?;
246+
let tx = self.prepare_tx(retry_count, resp, block).await?;
247247

248248
self.send_transaction(resp, tx).await
249249
}
@@ -255,10 +255,12 @@ impl SubmitTask {
255255
retry_count: usize,
256256
resp: &SignResponse,
257257
block: &BuiltBlock,
258-
sim_env: &SimEnv,
259258
) -> Result<TransactionRequest, eyre::Error> {
259+
// Get the latest host block header for gas estimation
260+
let host_header = self.latest_host_header().await?;
261+
260262
// Create the transaction request with the signature values
261-
let tx: TransactionRequest = self.new_tx_request(retry_count, resp, block, sim_env).await?;
263+
let tx: TransactionRequest = self.new_tx_request(retry_count, resp, block, host_header).await?;
262264

263265
// Simulate the transaction with a call to the host provider and report any errors
264266
if let Err(err) = self.sim_with_call(&tx).await {
@@ -268,6 +270,21 @@ impl SubmitTask {
268270
Ok(tx)
269271
}
270272

273+
/// Gets the host header from the host provider by fetching the latest block.
274+
async fn latest_host_header(&self) -> eyre::Result<Header> {
275+
let previous = self
276+
.host_provider
277+
.get_block(BlockId::Number(BlockNumberOrTag::Latest))
278+
.into_future()
279+
.await?;
280+
debug!(?previous, "got host block for hash");
281+
282+
match previous {
283+
Some(block) => Ok(block.header.inner),
284+
None => Err(eyre::eyre!("host block not found")),
285+
}
286+
}
287+
271288
/// Simulates the transaction with a call to the host provider to check for reverts.
272289
async fn sim_with_call(&self, tx: &TransactionRequest) -> eyre::Result<()> {
273290
match self.provider().call(tx.clone()).block(BlockNumberOrTag::Pending.into()).await {
@@ -286,7 +303,7 @@ impl SubmitTask {
286303
retry_count: usize,
287304
resp: &SignResponse,
288305
block: &BuiltBlock,
289-
sim_env: &SimEnv,
306+
host_header: Header,
290307
) -> Result<TransactionRequest, eyre::Error> {
291308
// manually retrieve nonce
292309
let nonce =
@@ -297,7 +314,7 @@ impl SubmitTask {
297314
let (v, r, s) = extract_signature_components(&resp.sig);
298315

299316
let (max_fee_per_gas, max_priority_fee_per_gas, max_fee_per_blob_gas) =
300-
calculate_gas(retry_count, sim_env.host.clone());
317+
calculate_gas(retry_count, host_header);
301318

302319
// Build the block header
303320
let header: BlockHeader = BlockHeader {
@@ -379,7 +396,6 @@ impl SubmitTask {
379396
&self,
380397
retry_count: usize,
381398
block: &BuiltBlock,
382-
sim_env: &SimEnv,
383399
) -> eyre::Result<ControlFlow> {
384400
info!(retry_count, txns = block.tx_count(), "handling inbound block");
385401
let Ok(sig_request) = self.construct_sig_request(block).await.inspect_err(|e| {
@@ -397,14 +413,13 @@ impl SubmitTask {
397413

398414
let signed = self.quincey.get_signature(&sig_request).await?;
399415

400-
self.submit_transaction(retry_count, &signed, block, sim_env).await
416+
self.submit_transaction(retry_count, &signed, block).await
401417
}
402418

403419
/// Handles the retry logic for the inbound block.
404420
async fn retrying_handle_inbound(
405421
&self,
406422
block: &BuiltBlock,
407-
sim_env: &SimEnv,
408423
retry_limit: usize,
409424
) -> eyre::Result<ControlFlow> {
410425
let mut retries = 0;
@@ -418,7 +433,7 @@ impl SubmitTask {
418433
let span = debug_span!("SubmitTask::retrying_handle_inbound", retries);
419434

420435
let inbound_result =
421-
match self.handle_inbound(retries, block, sim_env).instrument(span.clone()).await {
436+
match self.handle_inbound(retries, block).instrument(span.clone()).await {
422437
Ok(control_flow) => control_flow,
423438
Err(err) => {
424439
// Delay until next slot if we get a 403 error
@@ -520,7 +535,7 @@ impl SubmitTask {
520535
}
521536

522537
if let Err(e) =
523-
self.retrying_handle_inbound(&sim_result.block, &sim_result.env, 3).await
538+
self.retrying_handle_inbound(&sim_result.block, 3).await
524539
{
525540
error!(error = %e, "error handling inbound block");
526541
continue;

0 commit comments

Comments
 (0)