Skip to content

Commit 24aea66

Browse files
committed
wip: detect and sleep during 403 slots
1 parent c6b7703 commit 24aea66

File tree

3 files changed

+60
-25
lines changed

3 files changed

+60
-25
lines changed

Cargo.toml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,17 @@ integration = []
2727
[dependencies]
2828
init4-bin-base = "0.3"
2929

30-
signet-constants = { git = "https://github.com/init4tech/signet-sdk", branch = "main" }
31-
signet-sim = { git = "https://github.com/init4tech/signet-sdk", branch = "main" }
32-
signet-tx-cache = { git = "https://github.com/init4tech/signet-sdk", branch = "main" }
33-
signet-types = { git = "https://github.com/init4tech/signet-sdk", branch = "main" }
34-
signet-zenith = { git = "https://github.com/init4tech/signet-sdk", branch = "main" }
30+
# signet-constants = { path = "../signet-sdk/crates/constants" }
31+
# signet-sim = { path = "../signet-sdk/crates/sim" }
32+
# signet-tx-cache = { path = "../signet-sdk/crates/tx-cache" }
33+
# signet-types = { path = "../signet-sdk/crates/types" }
34+
# signet-zenith = { path = "../signet-sdk/crates/zenith" }
35+
36+
signet-constants = { git = "https://github.com/init4tech/signet-sdk", branch = "dylan/block-number" }
37+
signet-sim = { git = "https://github.com/init4tech/signet-sdk", branch = "dylan/block-number" }
38+
signet-tx-cache = { git = "https://github.com/init4tech/signet-sdk", branch = "dylan/block-number" }
39+
signet-types = { git = "https://github.com/init4tech/signet-sdk", branch = "dylan/block-number" }
40+
signet-zenith = { git = "https://github.com/init4tech/signet-sdk", branch = "dylan/block-number" }
3541

3642
trevm = { version = "0.20.10", features = ["concurrent-db", "test-utils"] }
3743

src/tasks/block/sim.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,18 @@ impl Simulator {
8787
db,
8888
constants,
8989
self.config.cfg_env(),
90-
block,
90+
block.clone(),
9191
finish_by,
9292
self.config.concurrency_limit,
9393
sim_items,
9494
self.config.rollup_block_gas_limit,
9595
);
9696

97-
let block = block_build.build().await;
98-
debug!(block = ?block, "finished block simulation");
97+
let mut built_block = block_build.build().await;
98+
built_block.set_block_number(block.number);
99+
debug!(block = ?built_block, "finished block simulation");
99100

100-
Ok(block)
101+
Ok(built_block)
101102
}
102103

103104
/// Spawns the simulator task, which handles the setup and sets the deadline
@@ -152,11 +153,10 @@ impl Simulator {
152153
error!("block_env channel closed");
153154
return;
154155
}
155-
156+
156157
// If no env, skip this run
157158
let Some(block_env) = self.block_env.borrow_and_update().clone() else { return };
158-
159-
debug!(block_env = ?block_env, "building on block");
159+
debug!(block_env = ?block_env, "building on block env");
160160

161161
match self.handle_build(constants, sim_cache, finish_by, block_env).await {
162162
Ok(block) => {

src/tasks/submit.rs

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ impl SubmitTask {
111111
async fn next_host_block_height(&self) -> eyre::Result<u64> {
112112
let result = self.provider().get_block_number().await?;
113113
let next = result.checked_add(1).ok_or_else(|| eyre!("next host block height overflow"))?;
114+
debug!(next, "next host block height");
114115
Ok(next)
115116
}
116117

@@ -257,7 +258,6 @@ impl SubmitTask {
257258
nonce = ?tx.nonce,
258259
"sending transaction to network"
259260
);
260-
261261

262262
// assign the nonce and fill the rest of the values
263263
let SendableTx::Envelope(tx) = self.provider().fill(tx).await? else {
@@ -285,12 +285,15 @@ impl SubmitTask {
285285
return Ok(ControlFlow::Skip);
286286
}
287287

288+
// Okay so the code gets all the way to this log
289+
// but we don't see the tx hash in the logs or in the explorer,
290+
// not even as a failed TX, just not at all.
288291
info!(
289292
tx_hash = %tx.tx_hash(),
290293
ru_chain_id = %resp.req.ru_chain_id,
291294
gas_limit = %resp.req.gas_limit,
292295
"dispatched to network"
293-
);
296+
);
294297

295298
Ok(ControlFlow::Done)
296299
}
@@ -336,22 +339,31 @@ impl SubmitTask {
336339
let span = debug_span!("SubmitTask::retrying_handle_inbound", retries);
337340
debug!(retries, "number of retries");
338341

339-
let inbound_result = match self.handle_inbound(retries, block).instrument(span.clone()).await {
342+
let inbound_result = match self
343+
.handle_inbound(retries, block)
344+
.instrument(span.clone())
345+
.await
346+
{
340347
Ok(control_flow) => {
341348
debug!(?control_flow, retries, "successfully handled inbound block");
342349
control_flow
343350
}
344351
Err(err) => {
352+
// Log the retry attempt
345353
retries += 1;
346354
error!(error = %err, "error handling inbound block");
347355

356+
// Delay until next slot if we get a 403 error
348357
if err.to_string().contains("403") {
349-
debug!("403 error - skipping block");
350-
let (slot_number, start, end) = self.calculate_slot_window()?;
351-
debug!(slot_number, start, end, "403 sleep until skipping block");
352-
// TODO: Sleep until the end of the next slot and return retry
353-
return Ok(ControlFlow::Done);
358+
let (slot_number, _, end) = self.calculate_slot_window()?;
359+
let now = self.now();
360+
if end > now {
361+
let sleep_duration = std::time::Duration::from_secs(end - now);
362+
debug!(sleep_duration = ?sleep_duration, slot_number, "403 detected - sleeping until end of slot");
363+
tokio::time::sleep(sleep_duration).await;
364+
}
354365
}
366+
355367
ControlFlow::Retry
356368
}
357369
};
@@ -413,19 +425,36 @@ impl SubmitTask {
413425
}
414426

415427
/// Task future for the submit task
428+
/// NB: This task assumes that the simulator will only send it blocks for
429+
/// slots that it's assigned.
416430
async fn task_future(self, mut inbound: mpsc::UnboundedReceiver<BuiltBlock>) {
431+
// Holds a reference to the last block we attempted to submit
432+
let mut last_block_attempted: u64 = 0;
433+
417434
loop {
435+
// Wait to receive a new block
418436
let Some(block) = inbound.recv().await else {
419437
debug!("upstream task gone");
420438
break;
421439
};
422-
debug!(?block, "submit channel received block");
440+
debug!(block_number = block.block_number(), ?block, "submit channel received block");
441+
442+
// Check if a block number was set and skip if not
443+
if block.block_number() == 0 {
444+
debug!("block number is 0 - skipping");
445+
continue;
446+
}
447+
448+
// Only attempt each block number once
449+
if block.block_number() == last_block_attempted {
450+
debug!("block number is unchanged from last attempt - skipping");
451+
continue;
452+
}
423453

424-
// TODO: Pass a BlockEnv to this function to give retrying handle inbound access to the block
425-
// env and thus the block number so that we can be sure that we try for only our assigned slots.
454+
// This means we have encountered a new block, so reset the last block attempted
455+
last_block_attempted = block.block_number();
456+
debug!(last_block_attempted, "resetting last block attempted");
426457

427-
// Instead this needs to fire off a task that attempts to land the block for the given slot
428-
// Once that slot is up, it's invalid for the next anyway, so this job can be ephemeral.
429458
if self.retrying_handle_inbound(&block, 3).await.is_err() {
430459
debug!("error handling inbound block");
431460
continue;

0 commit comments

Comments
 (0)