Skip to content

Commit 0688c00

Browse files
committed
fix retry logic bug
1 parent d337287 commit 0688c00

File tree

1 file changed

+55
-51
lines changed

1 file changed

+55
-51
lines changed

src/tasks/submit.rs

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,7 @@ impl SubmitTask {
140140

141141
// Simulate the transaction with a call to the host provider
142142
if let Some(maybe_error) = self.sim_with_call(&tx).await {
143-
warn!(
144-
error = ?maybe_error,
145-
"error in transaction simulation"
146-
);
143+
warn!(error = ?maybe_error, "error in transaction simulation");
147144
if let Err(e) = maybe_error {
148145
return Err(e);
149146
}
@@ -207,20 +204,11 @@ impl SubmitTask {
207204
) -> Result<TransactionRequest, eyre::Error> {
208205
// TODO: ENG-1082 Implement fills
209206
let fills = vec![];
210-
211207
// Extract the signature components from the response
212208
let (v, r, s) = extract_signature_components(&resp.sig);
213209

214-
// Bump gas with each retry to replace the previous
215-
// transaction while maintaining the same nonce
216-
// TODO: Clean this up if this works
217-
let gas_coefficient: u64 = (15 * (retry_count + 1)).try_into().unwrap();
218-
let gas_limit: u64 = 1_500_000 + (gas_coefficient * 1_000_000);
219-
let max_priority_fee_per_gas: u128 = (retry_count as u128);
220-
debug!(
221-
retry_count,
222-
gas_coefficient, gas_limit, max_priority_fee_per_gas, "calculated gas limit"
223-
);
210+
// Calculate gas limits based on retry attempts
211+
let (max_fee_per_gas, max_priority_fee_per_gas) = calculate_gas_limits(retry_count);
224212

225213
// manually retrieve nonce
226214
let nonce =
@@ -242,8 +230,9 @@ impl SubmitTask {
242230
.build_blob_tx(fills, header, v, r, s, block)?
243231
.with_from(self.provider().default_signer_address())
244232
.with_to(self.config.builder_helper_address)
245-
.with_gas_limit(gas_limit)
233+
.with_max_fee_per_gas(max_fee_per_gas.into())
246234
.with_max_priority_fee_per_gas(max_priority_fee_per_gas)
235+
.with_max_fee_per_blob_gas(max_fee_per_gas)
247236
.with_nonce(nonce);
248237

249238
debug!(?tx, "prepared transaction request");
@@ -258,9 +247,12 @@ impl SubmitTask {
258247
tx: TransactionRequest,
259248
) -> Result<ControlFlow, eyre::Error> {
260249
debug!(
250+
?tx.max_fee_per_gas,
251+
?tx.max_fee_per_blob_gas,
252+
?tx.max_priority_fee_per_gas,
253+
nonce = ?tx.nonce,
261254
host_block_number = %resp.req.host_block_number,
262255
gas_limit = %resp.req.gas_limit,
263-
nonce = ?tx.nonce,
264256
"sending transaction to network"
265257
);
266258

@@ -286,6 +278,12 @@ impl SubmitTask {
286278
// question mark unwraps join error, which would be an internal panic
287279
// then if let checks for rpc error
288280
if let Err(e) = fut.await? {
281+
let err_str = e.to_string();
282+
if err_str.contains("replacement transaction underpriced") {
283+
// bump gas and repeat
284+
debug!("UNDERPRICE ERROR DETECTED");
285+
return Ok(ControlFlow::Retry);
286+
}
289287
error!(error = %e, "Primary tx broadcast failed. Skipping transaction.");
290288
return Ok(ControlFlow::Skip);
291289
}
@@ -341,54 +339,42 @@ impl SubmitTask {
341339

342340
// Retry loop
343341
let result = loop {
342+
// Log the retry attempt
344343
let span = debug_span!("SubmitTask::retrying_handle_inbound", retries);
345344

346-
let inbound_result = match self
347-
.handle_inbound(retries, block)
348-
.instrument(span.clone())
349-
.await
350-
{
351-
Ok(control_flow) => {
352-
debug!(?control_flow, retries, "successfully handled inbound block");
353-
control_flow
354-
}
355-
Err(err) => {
356-
// Log the retry attempt
357-
retries += 1;
358-
359-
// Delay until next slot if we get a 403 error
360-
if err.to_string().contains("403 Forbidden") {
361-
let (slot_number, _, _) = self.calculate_slot_window()?;
362-
debug!(slot_number, ?block, "403 detected - not assigned to slot");
363-
return Ok(ControlFlow::Skip);
364-
} else {
365-
error!(error = %err, "error handling inbound block");
345+
let inbound_result =
346+
match self.handle_inbound(retries, block).instrument(span.clone()).await {
347+
Ok(control_flow) => {
348+
debug!(?control_flow, retries, "successfully handled inbound block");
349+
control_flow
366350
}
367-
368-
ControlFlow::Retry
369-
}
370-
};
351+
Err(err) => {
352+
retries += 1;
353+
// Delay until next slot if we get a 403 error
354+
if err.to_string().contains("403 Forbidden") {
355+
let (slot_number, _, _) = self.calculate_slot_window()?;
356+
debug!(slot_number, ?block, "403 detected - not assigned to slot");
357+
return Ok(ControlFlow::Skip);
358+
} else {
359+
error!(error = %err, "error handling inbound block");
360+
}
361+
362+
ControlFlow::Retry
363+
}
364+
};
371365

372366
let guard = span.entered();
373367

374368
match inbound_result {
375369
ControlFlow::Retry => {
370+
retries += 1;
376371
if retries > retry_limit {
377372
counter!("builder.building_too_many_retries").increment(1);
378373
debug!("retries exceeded - skipping block");
379374
return Ok(ControlFlow::Skip);
380375
}
381376
drop(guard);
382-
383-
// Detect a slot change and break out of the loop in that case too
384-
let (this_slot, start, end) = self.calculate_slot_window()?;
385-
if this_slot != current_slot {
386-
debug!("slot changed - skipping block");
387-
break inbound_result;
388-
}
389-
390-
// Otherwise retry the block
391-
debug!(retries, this_slot, start, end, "retrying block");
377+
debug!(retries, start, end, "retrying block");
392378
continue;
393379
}
394380
ControlFlow::Skip => {
@@ -471,3 +457,21 @@ impl SubmitTask {
471457
(sender, handle)
472458
}
473459
}
460+
461+
fn calculate_gas_limits(retry_count: usize) -> (u128, u128) {
462+
// Set a base gas amount
463+
let base_gas = 1_500_000;
464+
// Define a gas coefficient
465+
let gas_coefficient: u64 = 1 + (retry_count as u64);
466+
// Determine gas limit by multiplying base gas by the coefficient
467+
let max_fee_per_gas: u128 = (base_gas + (gas_coefficient * base_gas)) as u128;
468+
// Determine priority gas fee by multiplying retry count by the coefficient
469+
let max_priority_fee_per_gas: u128 =
470+
(1 + retry_count as u128) * (gas_coefficient as u128 * 100_000);
471+
472+
debug!(
473+
retry_count,
474+
gas_coefficient, max_fee_per_gas, max_priority_fee_per_gas, "calculated gas limits"
475+
);
476+
(max_fee_per_gas, max_priority_fee_per_gas)
477+
}

0 commit comments

Comments
 (0)