Skip to content

Commit 0f98034

Browse files
authored
refactor: move pre host fetching to the env task (#147)
* refactor: move pre host fetching to the env task * fix: tests
1 parent 22b76ca commit 0f98034

File tree

10 files changed

+129
-82
lines changed

10 files changed

+129
-82
lines changed

bin/builder.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use builder::{
22
config::BuilderConfig,
33
service::serve_builder,
4-
tasks::{block::sim::Simulator, cache::CacheTasks, metrics::MetricsTask, submit::SubmitTask},
4+
tasks::{
5+
block::sim::Simulator, cache::CacheTasks, env::EnvTask, metrics::MetricsTask,
6+
submit::SubmitTask,
7+
},
58
};
69
use init4_bin_base::{
710
deps::tracing::{info, info_span},
@@ -26,9 +29,13 @@ async fn main() -> eyre::Result<()> {
2629
let ru_provider = config.connect_ru_provider().await?;
2730

2831
// Spawn the EnvTask
29-
let env_task = config.env_task();
30-
let (block_env, env_jh) =
31-
env_task.await.expect("ws validity checked in connect_ru_provider above").spawn();
32+
let env_task = EnvTask::new(
33+
config.clone(),
34+
constants.clone(),
35+
config.connect_host_provider().await?,
36+
ru_provider.clone(),
37+
);
38+
let (block_env, env_jh) = env_task.spawn();
3239

3340
// Spawn the cache system
3441
let cache_tasks = CacheTasks::new(config.clone(), block_env.clone());

src/config.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use crate::{
2-
quincey::Quincey,
3-
tasks::{block::cfg::SignetCfgEnv, env::EnvTask},
4-
};
1+
use crate::{quincey::Quincey, tasks::block::cfg::SignetCfgEnv};
52
use alloy::{
63
network::{Ethereum, EthereumWallet},
74
primitives::Address,
@@ -248,12 +245,6 @@ impl BuilderConfig {
248245
Ok(Quincey::new_remote(client, url, token))
249246
}
250247

251-
/// Create an [`EnvTask`] using this config.
252-
pub async fn env_task(&self) -> eyre::Result<EnvTask> {
253-
let ru_provider = self.connect_ru_provider().await?;
254-
Ok(EnvTask::new(self.clone(), ru_provider))
255-
}
256-
257248
/// Create a [`SignetCfgEnv`] using this config.
258249
pub const fn cfg_env(&self) -> SignetCfgEnv {
259250
SignetCfgEnv { chain_id: self.ru_chain_id }

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#![deny(unused_must_use, rust_2018_idioms)]
1313
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
1414

15+
#[macro_use]
16+
mod macros;
17+
1518
/// Configuration for the Builder binary.
1619
pub mod config;
1720

src/macros.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/// Helper macro to log an event within a span that is not currently entered.
2+
macro_rules! span_scoped {
3+
($span:expr, $level:ident!($($arg:tt)*)) => {
4+
$span.in_scope(|| {
5+
$level!($($arg)*);
6+
});
7+
};
8+
}
9+
10+
/// Helper macro to unwrap a result or continue the loop with a tracing event.
11+
macro_rules! res_unwrap_or_continue {
12+
($result:expr, $span:expr, $level:ident!($($arg:tt)*)) => {
13+
match $result {
14+
Ok(value) => value,
15+
Err(err) => {
16+
span_scoped!($span, $level!(%err, $($arg)*));
17+
continue;
18+
}
19+
}
20+
};
21+
}
22+
23+
/// Helper macro to unwrap an option or continue the loop with a tracing event.
24+
macro_rules! opt_unwrap_or_continue {
25+
($option:expr, $span:expr, $level:ident!($($arg:tt)*)) => {
26+
match $option {
27+
Some(value) => value,
28+
None => {
29+
span_scoped!($span, $level!($($arg)*));
30+
continue;
31+
}
32+
}
33+
};
34+
}

src/tasks/env.rs

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
1-
use crate::config::{BuilderConfig, RuProvider};
1+
use crate::config::{BuilderConfig, HostProvider, RuProvider};
22
use alloy::{
33
consensus::Header,
44
eips::eip1559::BaseFeeParams,
55
primitives::{B256, U256},
66
providers::Provider,
77
};
88
use init4_bin_base::deps::tracing::{Instrument, debug, error, info_span};
9+
use signet_constants::SignetSystemConstants;
910
use tokio::{sync::watch, task::JoinHandle};
1011
use tokio_stream::StreamExt;
12+
use tracing::warn;
1113
use trevm::revm::{context::BlockEnv, context_interface::block::BlobExcessGasAndPrice};
1214

1315
/// A task that constructs a BlockEnv for the next block in the rollup chain.
1416
#[derive(Debug, Clone)]
1517
pub struct EnvTask {
1618
/// Builder configuration values.
1719
config: BuilderConfig,
18-
/// Rollup provider is used to get the latest rollup block header for simulation.
20+
21+
/// Signet system constants.
22+
constants: SignetSystemConstants,
23+
24+
/// Host provider is used to get the latest host block header for
25+
/// constructing the next block environment.
26+
host_provider: HostProvider,
27+
28+
/// Rollup provider is used to get the latest rollup block header for
29+
/// simulation.
1930
ru_provider: RuProvider,
2031
}
2132

@@ -26,12 +37,19 @@ pub struct SimEnv {
2637
pub block_env: BlockEnv,
2738
/// The header of the previous rollup block.
2839
pub prev_header: Header,
40+
/// The header of the previous host block.
41+
pub prev_host: Header,
2942
}
3043

3144
impl EnvTask {
3245
/// Create a new [`EnvTask`] with the given config and providers.
33-
pub const fn new(config: BuilderConfig, ru_provider: RuProvider) -> Self {
34-
Self { config, ru_provider }
46+
pub const fn new(
47+
config: BuilderConfig,
48+
constants: SignetSystemConstants,
49+
host_provider: HostProvider,
50+
ru_provider: RuProvider,
51+
) -> Self {
52+
Self { config, constants, host_provider, ru_provider }
3553
}
3654

3755
/// Construct a [`BlockEnv`] by from the previous block header.
@@ -74,10 +92,23 @@ impl EnvTask {
7492
while let Some(rollup_header) =
7593
headers.next().instrument(info_span!("EnvTask::task_fut::stream")).await
7694
{
77-
let span =
78-
info_span!("EnvTask::task_fut::loop", %rollup_header.hash, %rollup_header.number);
95+
let host_block_number =
96+
self.constants.rollup_block_to_host_block_num(rollup_header.number);
7997

80-
span.record("rollup_block_number", rollup_header.number);
98+
let span = info_span!("EnvTask::task_fut::loop", %host_block_number, %rollup_header.hash, %rollup_header.number);
99+
100+
let host_block_opt = res_unwrap_or_continue!(
101+
self.host_provider.get_block_by_number(host_block_number.into()).await,
102+
span,
103+
error!("error fetching previous host block - skipping block submission")
104+
);
105+
let prev_host = opt_unwrap_or_continue!(
106+
host_block_opt,
107+
span,
108+
warn!("previous host block not found - skipping block submission")
109+
)
110+
.header
111+
.inner;
81112

82113
// Construct the block env using the previous block header
83114
let signet_env = self.construct_block_env(&rollup_header);
@@ -88,7 +119,11 @@ impl EnvTask {
88119
);
89120

90121
if sender
91-
.send(Some(SimEnv { block_env: signet_env, prev_header: rollup_header.inner }))
122+
.send(Some(SimEnv {
123+
block_env: signet_env,
124+
prev_header: rollup_header.inner,
125+
prev_host,
126+
}))
92127
.is_err()
93128
{
94129
// The receiver has been dropped, so we can stop the task.

src/tasks/submit/task.rs

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -23,41 +23,6 @@ use signet_constants::SignetSystemConstants;
2323
use std::{ops::Range, time::Instant};
2424
use tokio::{sync::mpsc, task::JoinHandle};
2525

26-
/// Helper macro to log an event within a span that is not currently entered.
27-
macro_rules! span_scoped {
28-
($span:expr, $level:ident!($($arg:tt)*)) => {
29-
$span.in_scope(|| {
30-
$level!($($arg)*);
31-
});
32-
};
33-
}
34-
35-
/// Helper macro to unwrap a result or continue the loop with a tracing event.
36-
macro_rules! res_unwrap_or_continue {
37-
($result:expr, $span:expr, $level:ident!($($arg:tt)*)) => {
38-
match $result {
39-
Ok(value) => value,
40-
Err(err) => {
41-
span_scoped!($span, $level!(%err, $($arg)*));
42-
continue;
43-
}
44-
}
45-
};
46-
}
47-
48-
/// Helper macro to unwrap an option or continue the loop with a tracing event.
49-
macro_rules! opt_unwrap_or_continue {
50-
($option:expr, $span:expr, $level:ident!($($arg:tt)*)) => {
51-
match $option {
52-
Some(value) => value,
53-
None => {
54-
span_scoped!($span, $level!($($arg)*));
55-
continue;
56-
}
57-
}
58-
};
59-
}
60-
6126
/// Helper macro to spawn a tokio task that broadcasts a tx.
6227
macro_rules! spawn_provider_send {
6328
($provider:expr, $tx:expr) => {
@@ -299,21 +264,6 @@ impl SubmitTask {
299264
// drop guard before await
300265
drop(guard);
301266

302-
// Fetch the previous host block, not the current host block which is currently being built
303-
let prev_host_block = host_block_number - 1;
304-
305-
// If we encounter a provider error, log it and skip.
306-
let prev_host_resp_opt = res_unwrap_or_continue!(
307-
self.provider().get_block_by_number(prev_host_block.into()).await,
308-
span,
309-
error!("error fetching previous host block - skipping block submission")
310-
);
311-
let prev_host = opt_unwrap_or_continue!(
312-
prev_host_resp_opt,
313-
span,
314-
warn!(prev_host_block, "previous host block not found - skipping block submission")
315-
);
316-
317267
// Prep the span we'll use for the transaction submission
318268
let submission_span = debug_span!(
319269
parent: &span,
@@ -332,7 +282,9 @@ impl SubmitTask {
332282
self.constants.clone(),
333283
);
334284
let bumpable = res_unwrap_or_continue!(
335-
prep.prep_transaction(&prev_host.header).instrument(submission_span.clone()).await,
285+
prep.prep_transaction(&sim_result.env.prev_host)
286+
.instrument(submission_span.clone())
287+
.await,
336288
submission_span,
337289
error!("failed to prepare transaction for submission - skipping block submission")
338290
);

src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub fn populate_initial_gas(req: &mut TransactionRequest, prev_header: &Header)
4040
.expect("signet deployed after 1559 active") as u128;
4141
let blob_basefee = prev_header
4242
.next_block_blob_fee(BlobParams::prague())
43-
.expect("signet deployed after 4844 active");
43+
.expect("signet deployed after 7840 active");
4444

4545
req.max_priority_fee_per_gas = Some(STARTING_MPFPG);
4646
req.max_fee_per_gas = Some((base_fee_per_gas * 1025 / 1024) + STARTING_MPFPG);

tests/block_builder_test.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use alloy::{
88
signers::local::PrivateKeySigner,
99
};
1010
use builder::{
11-
tasks::block::sim::Simulator,
11+
tasks::{block::sim::Simulator, env::EnvTask},
1212
test_utils::{new_signed_tx, setup_logging, setup_test_config, test_block_env},
1313
};
1414
use signet_sim::SimCache;
@@ -42,7 +42,14 @@ async fn test_handle_build() {
4242
// Create a rollup provider
4343
let ru_provider = RootProvider::<Ethereum>::new_http(anvil_instance.endpoint_url());
4444

45-
let block_env = config.env_task().await.unwrap().spawn().0;
45+
let block_env = EnvTask::new(
46+
config.clone(),
47+
constants.clone(),
48+
config.connect_host_provider().await.unwrap(),
49+
ru_provider.clone(),
50+
)
51+
.spawn()
52+
.0;
4653

4754
let block_builder = Simulator::new(&config, ru_provider.clone(), block_env);
4855

tests/cache.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use builder::{
2-
tasks::cache::CacheTasks,
2+
tasks::{cache::CacheTasks, env::EnvTask},
33
test_utils::{setup_logging, setup_test_config},
44
};
55
use init4_bin_base::deps::tracing::warn;
6+
use signet_constants::SignetSystemConstants;
67
use std::time::Duration;
78

89
#[ignore = "integration test. This test will take >12 seconds to run, and requires Authz configuration env vars."]
@@ -12,7 +13,13 @@ async fn test_bundle_poller_roundtrip() -> eyre::Result<()> {
1213

1314
let config = setup_test_config().unwrap();
1415

15-
let (block_env, _jh) = config.env_task().await.unwrap().spawn();
16+
let (block_env, _jh) = EnvTask::new(
17+
config.clone(),
18+
SignetSystemConstants::pecorino(),
19+
config.connect_host_provider().await?,
20+
config.connect_ru_provider().await?,
21+
)
22+
.spawn();
1623
let cache_tasks = CacheTasks::new(config.clone(), block_env);
1724
let cache_system = cache_tasks.spawn();
1825

tests/env.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
1-
use builder::test_utils::{setup_logging, setup_test_config};
1+
use builder::{
2+
tasks::env::EnvTask,
3+
test_utils::{setup_logging, setup_test_config},
4+
};
5+
use signet_constants::SignetSystemConstants;
26

37
#[ignore = "integration test. This test will take between 0 and 12 seconds to run."]
48
#[tokio::test]
5-
async fn test_bundle_poller_roundtrip() {
9+
async fn test_bundle_poller_roundtrip() -> eyre::Result<()> {
610
setup_logging();
711

812
let config = setup_test_config().unwrap();
9-
let env_task = config.env_task();
10-
let (mut env_watcher, _jh) = env_task.await.unwrap().spawn();
13+
let (mut env_watcher, _jh) = EnvTask::new(
14+
config.clone(),
15+
SignetSystemConstants::pecorino(),
16+
config.connect_host_provider().await?,
17+
config.connect_ru_provider().await?,
18+
)
19+
.spawn();
1120

1221
env_watcher.changed().await.unwrap();
1322
let env = env_watcher.borrow_and_update();
1423
assert!(env.as_ref().is_some(), "Env should be Some");
24+
25+
Ok(())
1526
}

0 commit comments

Comments
 (0)