Skip to content

Commit 1990579

Browse files
committed
gas calculation refactors
1 parent ba696d4 commit 1990579

File tree

2 files changed

+41
-34
lines changed

2 files changed

+41
-34
lines changed

src/tasks/block/sim.rs

Lines changed: 3 additions & 4 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-
let number = block.number;
85+
8686
let block_build: BlockBuild<_, NoOpInspector> = BlockBuild::new(
8787
db,
8888
constants,
@@ -94,9 +94,8 @@ impl Simulator {
9494
self.config.rollup_block_gas_limit,
9595
);
9696

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

101100
Ok(built_block)
102101
}

src/tasks/submit.rs

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
utils::extract_signature_components,
55
};
66
use alloy::{
7-
consensus::SimpleCoder,
7+
consensus::{SimpleCoder, constants::GWEI_TO_WEI},
88
eips::BlockNumberOrTag,
99
network::{TransactionBuilder, TransactionBuilder4844},
1010
primitives::{FixedBytes, TxHash, U256},
@@ -21,12 +21,19 @@ use init4_bin_base::deps::{
2121
use signet_sim::BuiltBlock;
2222
use signet_types::{SignRequest, SignResponse};
2323
use signet_zenith::{
24-
BundleHelper::{self, BlockHeader, FillPermit2, submitCall},
24+
BundleHelper::{self, submitCall, BlockHeader, FillPermit2},
2525
Zenith::{self, IncorrectHostBlock},
2626
};
2727
use std::time::{Instant, UNIX_EPOCH};
2828
use tokio::{sync::mpsc, task::JoinHandle};
2929

30+
/// Base maximum fee per gas to use as a starting point for retry bumps
31+
pub const BASE_FEE_PER_GAS: u128 = 10 * GWEI_TO_WEI as u128;
32+
/// Base max priority fee per gas to use as a starting point for retry bumps
33+
pub const BASE_MAX_PRIORITY_FEE_PER_GAS: u128 = 2 * GWEI_TO_WEI as u128;
34+
/// Base maximum fee per blob gas to use as a starting point for retry bumps
35+
pub const BASE_MAX_FEE_PER_BLOB_GAS: u128 = 1 * GWEI_TO_WEI as u128;
36+
3037
macro_rules! spawn_provider_send {
3138
($provider:expr, $tx:expr) => {
3239
{
@@ -88,7 +95,7 @@ impl SubmitTask {
8895
})
8996
}
9097

91-
/// Builds blob transaction from the provided header and signature values
98+
/// Builds blob transaction and encodes the sidecar for it from the provided header and signature values
9299
fn build_blob_tx(
93100
&self,
94101
fills: Vec<FillPermit2>,
@@ -187,17 +194,23 @@ impl SubmitTask {
187194
) -> Result<TransactionRequest, eyre::Error> {
188195
// TODO: ENG-1082 Implement fills
189196
let fills = vec![];
197+
198+
// manually retrieve nonce
199+
let nonce =
200+
self.provider().get_transaction_count(self.provider().default_signer_address()).await?;
201+
debug!(nonce, "assigned nonce");
202+
190203
// Extract the signature components from the response
191204
let (v, r, s) = extract_signature_components(&resp.sig);
192205

193206
// Calculate gas limits based on retry attempts
194207
let (max_fee_per_gas, max_priority_fee_per_gas, max_fee_per_blob_gas) =
195-
calculate_gas_limits(retry_count);
196-
197-
// manually retrieve nonce // TODO: Maybe this should be done in Env task and passed through elsewhere
198-
let nonce =
199-
self.provider().get_transaction_count(self.provider().default_signer_address()).await?;
200-
debug!(nonce, "assigned nonce");
208+
calculate_gas_limits(
209+
retry_count,
210+
BASE_FEE_PER_GAS,
211+
BASE_MAX_PRIORITY_FEE_PER_GAS,
212+
BASE_MAX_FEE_PER_BLOB_GAS,
213+
);
201214

202215
// Build the block header
203216
let header: BlockHeader = BlockHeader {
@@ -249,9 +262,8 @@ impl SubmitTask {
249262

250263
if let Err(e) = fut.await? {
251264
// Detect and handle transaction underprice errors
252-
if matches!(e, TransportError::ErrorResp(ref err) if err.code == -32000 && err.message.contains("replacement transaction underpriced"))
253-
{
254-
debug!(?tx, "underpriced transaction error - retrying tx with gas bump");
265+
if matches!(e, TransportError::ErrorResp(ref err) if err.code == -32603) {
266+
debug!(tx_hash = ?tx.hash(), "underpriced transaction error - retrying tx with gas bump");
255267
return Ok(ControlFlow::Retry);
256268
}
257269

@@ -303,7 +315,7 @@ impl SubmitTask {
303315
) -> eyre::Result<ControlFlow> {
304316
let mut retries = 0;
305317
let building_start_time = Instant::now();
306-
let (current_slot, start, end) = self.calculate_slot_window()?;
318+
let (current_slot, start, end) = self.calculate_slot_window();
307319
debug!(current_slot, start, end, "calculating target slot window");
308320

309321
// Retry loop
@@ -317,7 +329,7 @@ impl SubmitTask {
317329
Err(err) => {
318330
// Delay until next slot if we get a 403 error
319331
if err.to_string().contains("403 Forbidden") {
320-
let (slot_number, _, _) = self.calculate_slot_window()?;
332+
let (slot_number, _, _) = self.calculate_slot_window();
321333
debug!(slot_number, "403 detected - skipping slot");
322334
return Ok(ControlFlow::Skip);
323335
} else {
@@ -363,11 +375,11 @@ impl SubmitTask {
363375
}
364376

365377
/// Calculates and returns the slot number and its start and end timestamps for the current instant.
366-
fn calculate_slot_window(&self) -> eyre::Result<(u64, u64, u64)> {
378+
fn calculate_slot_window(&self) -> (u64, u64, u64) {
367379
let now_ts = self.now();
368380
let current_slot = self.config.slot_calculator.calculate_slot(now_ts);
369381
let (start, end) = self.config.slot_calculator.calculate_slot_window(current_slot);
370-
Ok((current_slot, start, end))
382+
(current_slot, start, end)
371383
}
372384

373385
/// Returns the current timestamp in seconds since the UNIX epoch.
@@ -397,12 +409,6 @@ impl SubmitTask {
397409
};
398410
debug!(block_number = block.block_number(), ?block, "submit channel received block");
399411

400-
// Check if a block number was set and skip if not
401-
if block.block_number() == 0 {
402-
debug!("block number is 0 - skipping");
403-
continue;
404-
}
405-
406412
// Only attempt each block number once
407413
if block.block_number() == last_block_attempted {
408414
debug!("block number is unchanged from last attempt - skipping");
@@ -429,18 +435,20 @@ impl SubmitTask {
429435
}
430436
}
431437

432-
fn calculate_gas_limits(retry_count: usize) -> (u128, u128, u128) {
433-
let base_fee_per_gas: u128 = 100_000_000_000;
434-
let base_priority_fee_per_gas: u128 = 2_000_000_000;
435-
let base_fee_per_blob_gas: u128 = 1_000_000_000;
436-
438+
// Returns gas parameters based on retry counts. This uses
439+
fn calculate_gas_limits(
440+
retry_count: usize,
441+
base_max_fee_per_gas: u128,
442+
base_max_priority_fee_per_gas: u128,
443+
base_max_fee_per_blob_gas: u128,
444+
) -> (u128, u128, u128) {
437445
let bump_multiplier = 1150u128.pow(retry_count as u32); // 15% bump
438446
let blob_bump_multiplier = 2000u128.pow(retry_count as u32); // 100% bump (double each time) for blob gas
439447
let bump_divisor = 1000u128.pow(retry_count as u32);
440448

441-
let max_fee_per_gas = base_fee_per_gas * bump_multiplier / bump_divisor;
442-
let max_priority_fee_per_gas = base_priority_fee_per_gas * bump_multiplier / bump_divisor;
443-
let max_fee_per_blob_gas = base_fee_per_blob_gas * blob_bump_multiplier / bump_divisor;
449+
let max_fee_per_gas = base_max_fee_per_gas * bump_multiplier / bump_divisor;
450+
let max_priority_fee_per_gas = base_max_priority_fee_per_gas * bump_multiplier / bump_divisor;
451+
let max_fee_per_blob_gas = base_max_fee_per_blob_gas * blob_bump_multiplier / bump_divisor;
444452

445453
debug!(
446454
retry_count,

0 commit comments

Comments
 (0)