Skip to content

Commit 93d9a6c

Browse files
committed
refactor: make submit task a config option
1 parent 193e4d7 commit 93d9a6c

File tree

3 files changed

+62
-26
lines changed

3 files changed

+62
-26
lines changed

bin/builder.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
use builder::{
22
config::BuilderConfig,
33
service::serve_builder,
4-
tasks::{
5-
block::sim::Simulator, cache::CacheTasks, env::EnvTask, metrics::MetricsTask,
6-
submit::BuilderHelperTask,
7-
},
4+
tasks::{block::sim::Simulator, cache::CacheTasks, env::EnvTask, metrics::MetricsTask},
85
};
96
use init4_bin_base::{
107
deps::tracing::{info, info_span},
@@ -36,24 +33,16 @@ async fn main() -> eyre::Result<()> {
3633
let cache_system = cache_tasks.spawn();
3734

3835
// Prep providers and contracts
39-
let (host_provider, quincey) =
40-
tokio::try_join!(config.connect_host_provider(), config.connect_quincey())?;
41-
let zenith = config.connect_zenith(host_provider.clone());
36+
let host_provider = config.connect_host_provider().await?;
4237

4338
// Set up the metrics task
44-
let metrics = MetricsTask { host_provider: host_provider.clone() };
39+
let metrics = MetricsTask { host_provider };
4540
let (tx_channel, metrics_jh) = metrics.spawn();
4641

47-
// Make a Tx submission task
48-
let submit = BuilderHelperTask {
49-
zenith,
50-
quincey,
51-
config: config.clone(),
52-
outbound_tx_channel: tx_channel,
53-
};
54-
55-
// Set up tx submission
56-
let (submit_channel, submit_jh) = submit.spawn();
42+
// Set up the submit task. This will be either a Flashbots task or a
43+
// BuilderHelper task depending on whether a Flashbots endpoint is
44+
// configured.
45+
let (submit_channel, submit_jh) = config.spawn_submit_task(tx_channel).await?;
5746

5847
// Set up the simulator
5948
let sim = Simulator::new(&config, ru_provider.clone(), block_env);

src/config.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
use crate::{quincey::Quincey, tasks::block::cfg::SignetCfgEnv};
1+
use crate::{
2+
quincey::Quincey,
3+
tasks::{
4+
block::{cfg::SignetCfgEnv, sim::SimResult},
5+
submit::{BuilderHelperTask, FlashbotsTask},
6+
},
7+
};
28
use alloy::{
39
network::{Ethereum, EthereumWallet},
4-
primitives::Address,
10+
primitives::{Address, TxHash},
511
providers::{
612
Identity, ProviderBuilder, RootProvider,
713
fillers::{
@@ -24,7 +30,7 @@ use init4_bin_base::{
2430
use signet_constants::SignetSystemConstants;
2531
use signet_zenith::Zenith;
2632
use std::borrow::Cow;
27-
use tokio::join;
33+
use tokio::{join, sync::mpsc::UnboundedSender, task::JoinHandle};
2834

2935
/// Type alias for the provider used to simulate against rollup state.
3036
pub type RuProvider = RootProvider<Ethereum>;
@@ -168,7 +174,13 @@ pub struct BuilderConfig {
168174
impl BuilderConfig {
169175
/// Connect to the Builder signer.
170176
pub async fn connect_builder_signer(&self) -> Result<LocalOrAws, SignerError> {
171-
LocalOrAws::load(&self.builder_key, Some(self.host_chain_id)).await
177+
static ONCE: tokio::sync::OnceCell<LocalOrAws> = tokio::sync::OnceCell::const_new();
178+
179+
ONCE.get_or_try_init(|| async {
180+
LocalOrAws::load(&self.builder_key, Some(self.host_chain_id)).await
181+
})
182+
.await
183+
.cloned()
172184
}
173185

174186
/// Connect to the Sequencer signer.
@@ -282,4 +294,28 @@ impl BuilderConfig {
282294
.build(self.connect_builder_signer().await?)
283295
.ok_or_else(|| eyre::eyre!("Flashbots is not configured"))
284296
}
297+
298+
/// Spawn a submit task, either Flashbots or BuilderHelper depending on
299+
/// configuration.
300+
pub async fn spawn_submit_task(
301+
&self,
302+
tx_channel: UnboundedSender<TxHash>,
303+
) -> eyre::Result<(UnboundedSender<SimResult>, JoinHandle<()>)> {
304+
// If we have a flashbots endpoint, use that
305+
if self.flashbots.flashbots_endpoint.is_some() {
306+
// Make a Flashbots submission task
307+
let submit = FlashbotsTask::new(self.clone(), tx_channel).await?;
308+
309+
// Set up flashbots submission
310+
let (submit_channel, submit_jh) = submit.spawn();
311+
return Ok((submit_channel, submit_jh));
312+
}
313+
314+
// Make a Tx submission task
315+
let submit = BuilderHelperTask::new(self.clone(), tx_channel).await?;
316+
317+
// Set up tx submission
318+
let (submit_channel, submit_jh) = submit.spawn();
319+
Ok((submit_channel, submit_jh))
320+
}
285321
}

src/tasks/submit/builder_helper.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,27 @@ pub enum ControlFlow {
7373
#[derive(Debug)]
7474
pub struct BuilderHelperTask {
7575
/// Zenith
76-
pub zenith: ZenithInstance,
76+
zenith: ZenithInstance,
7777
/// Quincey
78-
pub quincey: Quincey,
78+
quincey: Quincey,
7979
/// Config
80-
pub config: crate::config::BuilderConfig,
80+
config: crate::config::BuilderConfig,
8181
/// Channel over which to send pending transactions
82-
pub outbound_tx_channel: mpsc::UnboundedSender<TxHash>,
82+
outbound_tx_channel: mpsc::UnboundedSender<TxHash>,
8383
}
8484

8585
impl BuilderHelperTask {
86+
/// Returns a new `BuilderHelperTask`
87+
pub async fn new(
88+
config: crate::config::BuilderConfig,
89+
outbound: mpsc::UnboundedSender<TxHash>,
90+
) -> eyre::Result<Self> {
91+
let (quincey, host_provider) =
92+
tokio::try_join!(config.connect_quincey(), config.connect_host_provider())?;
93+
let zenith = config.connect_zenith(host_provider);
94+
Ok(Self { zenith, quincey, config, outbound_tx_channel: outbound })
95+
}
96+
8697
/// Get the provider from the zenith instance
8798
const fn provider(&self) -> &HostProvider {
8899
self.zenith.provider()

0 commit comments

Comments
 (0)