Skip to content

Commit e3296c8

Browse files
authored
feat: improve tracing when failing to retrieve a host block (#141)
* feat: improve tracing when failing to retrieve a host block * chore: improve log method * fix: improve some traces * chore: DRY * lint: clippy
1 parent f95fe3e commit e3296c8

File tree

1 file changed

+63
-41
lines changed

1 file changed

+63
-41
lines changed

src/tasks/submit/task.rs

Lines changed: 63 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,42 @@ 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+
61+
/// Helper macro to spawn a tokio task that broadcasts a tx.
2662
macro_rules! spawn_provider_send {
2763
($provider:expr, $tx:expr) => {
2864
{
@@ -37,6 +73,7 @@ macro_rules! spawn_provider_send {
3773
};
3874
}
3975

76+
/// Helper macro to check if the slot is still valid before submitting a block.
4077
macro_rules! check_slot_still_valid {
4178
($self:expr, $initial_slot:expr) => {
4279
if !$self.slot_still_valid($initial_slot) {
@@ -265,18 +302,17 @@ impl SubmitTask {
265302
// Fetch the previous host block, not the current host block which is currently being built
266303
let prev_host_block = host_block_number - 1;
267304

268-
let Ok(Some(prev_host)) = self
269-
.provider()
270-
.get_block_by_number(prev_host_block.into())
271-
.into_future()
272-
.instrument(span.clone())
273-
.await
274-
else {
275-
span.in_scope(|| {
276-
warn!(ru_block_number, host_block_number, "failed to get previous host block")
277-
});
278-
continue;
279-
};
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+
);
280316

281317
// Prep the span we'll use for the transaction submission
282318
let submission_span = debug_span!(
@@ -295,39 +331,25 @@ impl SubmitTask {
295331
self.config.clone(),
296332
self.constants.clone(),
297333
);
298-
let bumpable = match prep
299-
.prep_transaction(&prev_host.header)
300-
.instrument(submission_span.clone())
301-
.await
302-
{
303-
Ok(bumpable) => bumpable,
304-
Err(error) => {
305-
submission_span.in_scope(|| {
306-
error!(%error, "failed to prepare transaction for submission");
307-
});
308-
continue;
309-
}
310-
};
334+
let bumpable = res_unwrap_or_continue!(
335+
prep.prep_transaction(&prev_host.header).instrument(submission_span.clone()).await,
336+
submission_span,
337+
error!("failed to prepare transaction for submission - skipping block submission")
338+
);
311339

312340
// Simulate the transaction to check for reverts
313-
if let Err(error) =
314-
self.sim_with_call(bumpable.req()).instrument(submission_span.clone()).await
315-
{
316-
submission_span.in_scope(|| {
317-
error!(%error, "simulation failed for transaction");
318-
});
319-
continue;
320-
};
341+
res_unwrap_or_continue!(
342+
self.sim_with_call(bumpable.req()).instrument(submission_span.clone()).await,
343+
submission_span,
344+
error!("simulation failed for transaction - skipping block submission")
345+
);
321346

322347
// Now send the transaction
323-
if let Err(error) =
324-
self.retrying_send(bumpable, 3).instrument(submission_span.clone()).await
325-
{
326-
submission_span.in_scope(|| {
327-
error!(%error, "error dispatching block to host chain");
328-
});
329-
continue;
330-
}
348+
let _ = res_unwrap_or_continue!(
349+
self.retrying_send(bumpable, 3).instrument(submission_span.clone()).await,
350+
submission_span,
351+
error!("error dispatching block to host chain")
352+
);
331353
}
332354
}
333355

0 commit comments

Comments
 (0)