Skip to content

Commit 35126e3

Browse files
committed
feat: add a couple checks to the env task
1 parent 14a7e35 commit 35126e3

File tree

7 files changed

+101
-21
lines changed

7 files changed

+101
-21
lines changed

bin/builder.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ async fn main() -> eyre::Result<()> {
2626
// RU WS connection is invalid.
2727
let (ru_provider, host_provider) =
2828
tokio::try_join!(config.connect_ru_provider(), config.connect_host_provider(),)?;
29+
let quincey = config.connect_quincey().await?;
2930

3031
// Spawn the EnvTask
31-
let env_task = EnvTask::new(config.clone(), host_provider.clone(), ru_provider.clone());
32+
let env_task =
33+
EnvTask::new(config.clone(), host_provider.clone(), quincey, ru_provider.clone());
3234
let (block_env, env_jh) = env_task.spawn();
3335

3436
// Spawn the cache system

src/macros.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ macro_rules! span_info {
2424

2525
}
2626

27+
/// Helper macro to log a warning event within a span that is not currently
28+
/// entered.
29+
macro_rules! span_warn {
30+
($span:expr, $($arg:tt)*) => {
31+
span_scoped!($span, warn!($($arg)*))
32+
};
33+
}
34+
2735
/// Helper macro to log a warning event within a span that is not currently
2836
/// entered.
2937
macro_rules! span_error {
@@ -34,7 +42,7 @@ macro_rules! span_error {
3442

3543
/// Helper macro to unwrap a result or continue the loop with a tracing event.
3644
macro_rules! res_unwrap_or_continue {
37-
($result:expr, $span:expr, $level:ident!($($arg:tt)*)) => {
45+
($result:expr, $span:expr, $level:ident!($($arg:tt)*) $(,)?) => {
3846
match $result {
3947
Ok(value) => value,
4048
Err(err) => {

src/quincey.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
use alloy::signers::Signer;
1+
use alloy::{
2+
primitives::{Address, B256, U256},
3+
signers::Signer,
4+
};
25
use eyre::bail;
36
use init4_bin_base::{perms::SharedToken, utils::signer::LocalOrAws};
47
use reqwest::Client;
8+
use signet_constants::SignetSystemConstants;
59
use signet_types::{SignRequest, SignResponse};
610
use tracing::{debug, info, instrument, trace};
711

@@ -35,6 +39,16 @@ impl Quincey {
3539
Self::Owned(client)
3640
}
3741

42+
/// Returns `true` if the signer is local.
43+
pub const fn is_local(&self) -> bool {
44+
matches!(self, Self::Owned(_))
45+
}
46+
47+
/// Returns `true` if the signer is remote.
48+
pub const fn is_remote(&self) -> bool {
49+
matches!(self, Self::Remote { .. })
50+
}
51+
3852
async fn sup_owned(&self, sig_request: &SignRequest) -> eyre::Result<SignResponse> {
3953
let Self::Owned(signer) = &self else { eyre::bail!("not an owned client") };
4054

@@ -77,4 +91,27 @@ impl Quincey {
7791
Self::Remote { .. } => self.sup_remote(sig_request).await,
7892
}
7993
}
94+
95+
/// Perform a preflight check to ensure that the Quincey service will
96+
/// be able to sign a request with the provided parameters at this
97+
/// point in time.
98+
#[instrument(skip(self, constants))]
99+
pub async fn preflight_check(
100+
&self,
101+
constants: &SignetSystemConstants,
102+
host_block_number: u64,
103+
) -> eyre::Result<()> {
104+
if self.is_local() {
105+
return Ok(());
106+
}
107+
let req = SignRequest {
108+
host_block_number: U256::from(host_block_number),
109+
host_chain_id: U256::from(constants.host_chain_id()),
110+
ru_chain_id: U256::from(constants.ru_chain_id()),
111+
gas_limit: U256::ZERO,
112+
ru_reward_address: Address::ZERO,
113+
contents: B256::ZERO,
114+
};
115+
self.sup_remote(&req).await.map(|_| ())
116+
}
80117
}

src/tasks/env.rs

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::{
22
config::{BuilderConfig, HostProvider, RuProvider},
3+
quincey::Quincey,
34
tasks::block::cfg::SignetCfgEnv,
45
};
56
use alloy::{
@@ -31,21 +32,6 @@ pub type SimRollupEnv = RollupEnv<RollupAlloyDatabaseProvider, NoOpInspector>;
3132
/// Type aliases for simulation environments.
3233
pub type SimHostEnv = HostEnv<HostAlloyDatabaseProvider, NoOpInspector>;
3334

34-
/// A task that constructs a BlockEnv for the next block in the rollup chain.
35-
#[derive(Debug, Clone)]
36-
pub struct EnvTask {
37-
/// Builder configuration values.
38-
config: BuilderConfig,
39-
40-
/// Host provider is used to get the latest host block header for
41-
/// constructing the next block environment.
42-
host_provider: HostProvider,
43-
44-
/// Rollup provider is used to get the latest rollup block header for
45-
/// simulation.
46-
ru_provider: RuProvider,
47-
}
48-
4935
/// An environment for simulating a block.
5036
#[derive(Debug, Clone)]
5137
pub struct Environment {
@@ -196,14 +182,33 @@ impl SimEnv {
196182
}
197183
}
198184

185+
/// A task that constructs a BlockEnv for the next block in the rollup chain.
186+
#[derive(Debug, Clone)]
187+
pub struct EnvTask {
188+
/// Builder configuration values.
189+
config: BuilderConfig,
190+
191+
/// Host provider is used to get the latest host block header for
192+
/// constructing the next block environment.
193+
host_provider: HostProvider,
194+
195+
/// Quincey instance for slot checking.
196+
quincey: Quincey,
197+
198+
/// Rollup provider is used to get the latest rollup block header for
199+
/// simulation.
200+
ru_provider: RuProvider,
201+
}
202+
199203
impl EnvTask {
200204
/// Create a new [`EnvTask`] with the given config and providers.
201205
pub const fn new(
202206
config: BuilderConfig,
203207
host_provider: HostProvider,
208+
quincey: Quincey,
204209
ru_provider: RuProvider,
205210
) -> Self {
206-
Self { config, host_provider, ru_provider }
211+
Self { config, host_provider, quincey, ru_provider }
207212
}
208213

209214
/// Construct a [`BlockEnv`] for the next host block from the previous host header.
@@ -271,11 +276,23 @@ impl EnvTask {
271276

272277
let span = info_span!("SimEnv", %host_block_number, %rollup_header.hash, %rollup_header.number);
273278

279+
let (host_block_res, quincey_res) = tokio::join!(
280+
self.host_provider.get_block_by_number(host_block_number.into()),
281+
self.quincey.preflight_check(&self.config.constants, host_block_number)
282+
);
283+
284+
res_unwrap_or_continue!(
285+
quincey_res,
286+
span,
287+
error!("error checking quincey slot - skipping block submission"),
288+
);
289+
274290
let host_block_opt = res_unwrap_or_continue!(
275-
self.host_provider.get_block_by_number(host_block_number.into()).await,
291+
host_block_res,
276292
span,
277293
error!("error fetching previous host block - skipping block submission")
278294
);
295+
279296
let host_header = opt_unwrap_or_continue!(
280297
host_block_opt,
281298
span,
@@ -284,6 +301,16 @@ impl EnvTask {
284301
.header
285302
.inner;
286303

304+
if rollup_header.timestamp != host_header.timestamp {
305+
span_warn!(
306+
span,
307+
rollup_timestamp = rollup_header.timestamp,
308+
host_timestamp = host_header.timestamp,
309+
"rollup block timestamp differs from host block timestamp. - skipping block submission"
310+
);
311+
continue;
312+
}
313+
287314
// Construct the block env using the previous block header
288315
let rollup_env = self.construct_rollup_env(rollup_header.into());
289316
let host_env = self.construct_host_env(host_header);

tests/block_builder_test.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ async fn test_handle_build() {
4343
let ru_provider = RootProvider::<Ethereum>::new_http(anvil_instance.endpoint_url());
4444
let host_provider = config.connect_host_provider().await.unwrap();
4545

46+
// Create a quincey client
47+
let quincey = config.connect_quincey().await.unwrap();
48+
4649
let block_env =
47-
EnvTask::new(config.clone(), host_provider.clone(), ru_provider.clone()).spawn().0;
50+
EnvTask::new(config.clone(), host_provider.clone(), quincey, ru_provider.clone()).spawn().0;
4851

4952
let block_builder =
5053
Simulator::new(&config, host_provider.clone(), ru_provider.clone(), block_env);

tests/cache.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ async fn test_bundle_poller_roundtrip() -> eyre::Result<()> {
1515
let (block_env, _jh) = EnvTask::new(
1616
config.clone(),
1717
config.connect_host_provider().await?,
18+
config.connect_quincey().await?,
1819
config.connect_ru_provider().await?,
1920
)
2021
.spawn();

tests/env.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ async fn test_bundle_poller_roundtrip() -> eyre::Result<()> {
99
setup_logging();
1010

1111
let config = setup_test_config().unwrap();
12+
1213
let (mut env_watcher, _jh) = EnvTask::new(
1314
config.clone(),
1415
config.connect_host_provider().await?,
16+
config.connect_quincey().await?,
1517
config.connect_ru_provider().await?,
1618
)
1719
.spawn();

0 commit comments

Comments
 (0)