Skip to content

Commit 9da0142

Browse files
committed
refactor: simplify slot_still_valid
1 parent ecba9cd commit 9da0142

File tree

1 file changed

+31
-35
lines changed

1 file changed

+31
-35
lines changed

src/tasks/submit/task.rs

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ macro_rules! spawn_provider_send {
3737
};
3838
}
3939

40+
macro_rules! check_slot_still_valid {
41+
($self:expr, $initial_slot:expr) => {
42+
if !$self.slot_still_valid($initial_slot) {
43+
debug!(
44+
current_slot =
45+
$self.config.slot_calculator.current_slot().expect("host chain has started"),
46+
initial_slot = $initial_slot,
47+
"slot changed before submission - skipping block"
48+
);
49+
counter!("builder.slot_missed").increment(1);
50+
return Ok(ControlFlow::Skip);
51+
}
52+
};
53+
}
54+
4055
/// Control flow for transaction submission.
4156
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
4257
pub enum ControlFlow {
@@ -130,18 +145,9 @@ impl SubmitTask {
130145
retry_limit: usize,
131146
) -> eyre::Result<ControlFlow> {
132147
let submitting_start_time = Instant::now();
133-
let now = utils::now();
134148

135149
let (expected_slot, window) = self.get_expected_slot_and_window();
136150

137-
debug!(
138-
expected_slot,
139-
start = window.start,
140-
end = window.end,
141-
now,
142-
"calculating target slot window"
143-
);
144-
145151
let mut req = bumpable.req().clone();
146152

147153
// Retry loop
@@ -151,27 +157,26 @@ impl SubmitTask {
151157
"SubmitTask::retrying_send",
152158
retries = bumpable.bump_count(),
153159
nonce = bumpable.req().nonce,
160+
expected_slot,
161+
start = window.start,
162+
end = window.end,
163+
now = utils::now(),
154164
);
155165

156-
let inbound_result = match self.send_transaction(req).instrument(span.clone()).await {
157-
Ok(control_flow) => control_flow,
158-
Err(error) => {
159-
if let Some(value) = self.slot_still_valid(expected_slot) {
160-
return value;
161-
}
162-
// Log error and retry
163-
error!(%error, "error handling inbound block");
164-
ControlFlow::Retry
165-
}
166-
};
166+
// Check at the top of the loop if the slot is still valid. This
167+
// will prevent unnecessary retries if the slot has changed.
168+
check_slot_still_valid!(self, expected_slot);
167169

168-
let guard = span.entered();
170+
let inbound_result = self
171+
.send_transaction(req)
172+
.instrument(span.clone())
173+
.await
174+
.inspect_err(|e| error!(error = %e, "sending transaction"))
175+
.unwrap_or(ControlFlow::Retry);
169176

177+
let guard = span.entered();
170178
match inbound_result {
171179
ControlFlow::Retry => {
172-
if let Some(value) = self.slot_still_valid(expected_slot) {
173-
return value;
174-
}
175180
// bump the req
176181
req = bumpable.bumped();
177182
if bumpable.bump_count() > retry_limit {
@@ -221,17 +226,8 @@ impl SubmitTask {
221226
}
222227

223228
/// Checks if a slot is still valid during submission retries.
224-
fn slot_still_valid(&self, initial_slot: usize) -> Option<Result<ControlFlow, eyre::Error>> {
225-
let current_slot =
226-
self.config.slot_calculator.current_slot().expect("host chain has started");
227-
if current_slot != initial_slot {
228-
// If the slot has changed, skip the block
229-
debug!(current_slot, initial_slot, "slot changed before submission - skipping block");
230-
counter!("builder.slot_missed").increment(1);
231-
return Some(Ok(ControlFlow::Skip));
232-
}
233-
debug!(current_slot, "slot still valid - continuing submission");
234-
None
229+
fn slot_still_valid(&self, initial_slot: usize) -> bool {
230+
initial_slot == self.config.slot_calculator.current_slot().expect("host chain has started")
235231
}
236232

237233
/// Task future for the submit task. This function runs the main loop of the task.

0 commit comments

Comments
 (0)