Skip to content

Commit ba0d4f3

Browse files
dylanlottprestwich
authored andcommitted
wip: detect and sleep during 403 slots
1 parent ae48e92 commit ba0d4f3

File tree

3 files changed

+59
-26
lines changed

3 files changed

+59
-26
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 = { version = "0.3.4", features = ["perms"] }
2929

30-
signet-constants = { git = "https://github.com/init4tech/signet-sdk", rev = "b8251ff0fec7cb14ca87e6f95c14f56bc2593049" }
31-
signet-sim = { git = "https://github.com/init4tech/signet-sdk", rev = "b8251ff0fec7cb14ca87e6f95c14f56bc2593049" }
32-
signet-tx-cache = { git = "https://github.com/init4tech/signet-sdk", rev = "b8251ff0fec7cb14ca87e6f95c14f56bc2593049" }
33-
signet-types = { git = "https://github.com/init4tech/signet-sdk", rev = "b8251ff0fec7cb14ca87e6f95c14f56bc2593049" }
34-
signet-zenith = { git = "https://github.com/init4tech/signet-sdk", rev = "b8251ff0fec7cb14ca87e6f95c14f56bc2593049" }
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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl Simulator {
8282
block: BlockEnv,
8383
) -> eyre::Result<BuiltBlock> {
8484
let db = self.create_db().await.unwrap();
85-
85+
let number = block.number;
8686
let block_build: BlockBuild<_, NoOpInspector> = BlockBuild::new(
8787
db,
8888
constants,
@@ -94,10 +94,11 @@ impl Simulator {
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(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
@@ -155,8 +156,7 @@ impl Simulator {
155156

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 & 15 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

@@ -146,9 +147,7 @@ impl SubmitTask {
146147
error = ?maybe_error,
147148
"error in transaction simulation"
148149
);
149-
if let Err(e) = maybe_error {
150-
return Err(e);
151-
}
150+
maybe_error?;
152151
}
153152

154153
Ok(tx)
@@ -257,7 +256,6 @@ impl SubmitTask {
257256
nonce = ?tx.nonce,
258257
"sending transaction to network"
259258
);
260-
261259

262260
// assign the nonce and fill the rest of the values
263261
let SendableTx::Envelope(tx) = self.provider().fill(tx).await? else {
@@ -285,6 +283,9 @@ impl SubmitTask {
285283
return Ok(ControlFlow::Skip);
286284
}
287285

286+
// Okay so the code gets all the way to this log
287+
// but we don't see the tx hash in the logs or in the explorer,
288+
// not even as a failed TX, just not at all.
288289
info!(
289290
tx_hash = %tx.tx_hash(),
290291
ru_chain_id = %resp.req.ru_chain_id,
@@ -336,22 +337,31 @@ impl SubmitTask {
336337
let span = debug_span!("SubmitTask::retrying_handle_inbound", retries);
337338
debug!(retries, "number of retries");
338339

339-
let inbound_result = match self.handle_inbound(retries, block).instrument(span.clone()).await {
340+
let inbound_result = match self
341+
.handle_inbound(retries, block)
342+
.instrument(span.clone())
343+
.await
344+
{
340345
Ok(control_flow) => {
341346
debug!(?control_flow, retries, "successfully handled inbound block");
342347
control_flow
343348
}
344349
Err(err) => {
350+
// Log the retry attempt
345351
retries += 1;
346352
error!(error = %err, "error handling inbound block");
347353

354+
// Delay until next slot if we get a 403 error
348355
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);
356+
let (slot_number, _, end) = self.calculate_slot_window()?;
357+
let now = self.now();
358+
if end > now {
359+
let sleep_duration = std::time::Duration::from_secs(end - now);
360+
debug!(sleep_duration = ?sleep_duration, slot_number, "403 detected - sleeping until end of slot");
361+
tokio::time::sleep(sleep_duration).await;
362+
}
354363
}
364+
355365
ControlFlow::Retry
356366
}
357367
};
@@ -413,19 +423,36 @@ impl SubmitTask {
413423
}
414424

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

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.
452+
// This means we have encountered a new block, so reset the last block attempted
453+
last_block_attempted = block.block_number();
454+
debug!(last_block_attempted, "resetting last block attempted");
426455

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.
429456
if self.retrying_handle_inbound(&block, 3).await.is_err() {
430457
debug!("error handling inbound block");
431458
continue;

0 commit comments

Comments
 (0)