Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions crates/op-rbuilder/src/builder/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ use tracing::{debug, info, trace};
use crate::{
backrun_bundle::BackrunBundlesPayloadCtx,
evm::OpBlockEvmFactory,
gas_limiter::AddressGasLimiter,
metrics::OpRBuilderMetrics,
gas_limiter::GasLimiters,
metrics::{OpRBuilderMetrics, record_tx_simulation_duration},
primitives::reth::{ExecutionInfo, TxnExecutionResult},
traits::PayloadTxsBounds,
};
Expand All @@ -66,8 +66,9 @@ pub struct OpPayloadBuilderCtx {
pub max_gas_per_txn: Option<u64>,
/// Maximum cumulative uncompressed (EIP-2718 encoded) block size in bytes.
pub max_uncompressed_block_size: Option<u64>,
/// Rate limiting based on gas. This is an optional feature.
pub address_gas_limiter: AddressGasLimiter,
/// Per-source gas rate limiters (one per tx source). `None` when the
/// limiter is disabled in config.
pub gas_limiters: Option<GasLimiters>,
/// Backrun bundles context.
pub backrun_ctx: BackrunBundlesPayloadCtx,
/// Skip reverted txs in subsequent flashblocks
Expand Down Expand Up @@ -560,9 +561,11 @@ impl OpPayloadBuilderCtx {
}
};

self.metrics
.tx_simulation_duration
.record(tx_simulation_start_time.elapsed());
record_tx_simulation_duration(
tx_simulation_start_time.elapsed(),
is_bundle_tx,
&result,
);
self.metrics.tx_byte_size.record(tx.inner().size() as f64);
num_txs_simulated += 1;

Expand All @@ -583,11 +586,11 @@ impl OpPayloadBuilderCtx {
);
}

if self
.address_gas_limiter
.consume_gas(tx.signer(), gas_used)
.is_err()
{
let gas_limiter = self
.gas_limiters
.as_ref()
.map(|l| if is_bundle_tx { &l.bundle } else { &l.mempool });
if gas_limiter.is_some_and(|l| l.consume_gas(tx.signer(), gas_used).is_err()) {
log_txn(TxnExecutionResult::MaxGasUsageExceeded);
best_txs.mark_invalid(tx.signer(), tx.nonce());
continue;
Expand Down Expand Up @@ -834,21 +837,19 @@ impl OpPayloadBuilderCtx {
continue;
}
};
self.metrics
.tx_simulation_duration
.record(br_simulation_start.elapsed());
record_tx_simulation_duration(br_simulation_start.elapsed(), true, &br_result);
self.metrics
.tx_byte_size
.record(bundle.backrun_tx.inner().size() as f64);
num_txs_simulated += 1;

let br_gas_used = br_result.gas_used();

if self
.address_gas_limiter
.consume_gas(bundle.backrun_tx.signer(), br_gas_used)
.is_err()
{
if self.gas_limiters.as_ref().is_some_and(|l| {
l.bundle
.consume_gas(bundle.backrun_tx.signer(), br_gas_used)
.is_err()
}) {
log_br_txn(TxnExecutionResult::MaxGasUsageExceeded);
continue;
}
Expand Down
16 changes: 9 additions & 7 deletions crates/op-rbuilder/src/builder/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
timing::{FlashblockScheduler, compute_slot_offset_ms},
},
evm::OpBlockEvmFactory,
gas_limiter::AddressGasLimiter,
gas_limiter::GasLimiters,
metrics::{OpRBuilderMetrics, record_flashblock_publish_timing},
primitives::reth::ExecutionInfo,
runtime_ext::RuntimeExt,
Expand Down Expand Up @@ -300,8 +300,8 @@ pub(super) struct OpPayloadBuilderInner<Pool, Client, BuilderTx> {
metrics: Arc<OpRBuilderMetrics>,
/// The end of builder transaction type
builder_tx: BuilderTx,
/// Rate limiting based on gas. This is an optional feature.
address_gas_limiter: AddressGasLimiter,
/// Per-source gas rate limiters. `None` when the limiter is disabled.
gas_limiters: Option<GasLimiters>,
/// Tokio task metrics for monitoring spawned tasks
task_metrics: Arc<FlashblocksTaskMetrics>,
/// Task executor used to offload blocking work.
Expand Down Expand Up @@ -339,7 +339,7 @@ impl<Pool, Client, BuilderTx> OpPayloadBuilder<Pool, Client, BuilderTx> {
task_metrics: Arc<FlashblocksTaskMetrics>,
executor: Runtime,
) -> Self {
let address_gas_limiter = AddressGasLimiter::new(config.gas_limiter_config.clone());
let gas_limiters = GasLimiters::from_args(&config.gas_limiter_config);
Self {
inner: Arc::new(OpPayloadBuilderInner {
evm_config,
Expand All @@ -351,7 +351,7 @@ impl<Pool, Client, BuilderTx> OpPayloadBuilder<Pool, Client, BuilderTx> {
config,
metrics,
builder_tx,
address_gas_limiter,
gas_limiters,
task_metrics,
executor,
}),
Expand Down Expand Up @@ -427,7 +427,7 @@ where
metrics: self.metrics.clone(),
max_gas_per_txn: self.config.max_gas_per_txn,
max_uncompressed_block_size: self.config.max_uncompressed_block_size,
address_gas_limiter: self.address_gas_limiter.clone(),
gas_limiters: self.gas_limiters.clone(),
backrun_ctx,
exclude_reverts_between_flashblocks: self.config.exclude_reverts_between_flashblocks,
enable_tx_tracking_debug_logs: self.config.enable_tx_tracking_debug_logs,
Expand Down Expand Up @@ -476,7 +476,9 @@ where
ctx.enable_incremental_state_root,
);

self.address_gas_limiter.refresh(ctx.block_number());
if let Some(limiters) = &self.gas_limiters {
limiters.refresh(ctx.block_number());
}

// Phase 1: Build the fallback block.
let fallback_span = if span.is_none() {
Expand Down
3 changes: 1 addition & 2 deletions crates/op-rbuilder/src/builder/syncer_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::{
backrun_bundle::{BackrunBundleArgs, BackrunBundleGlobalPool, BackrunBundlesPayloadCtx},
builder::{BuilderConfig, OpPayloadBuilderCtx},
evm::OpBlockEvmFactory,
gas_limiter::{AddressGasLimiter, args::GasLimiterArgs},
metrics::OpRBuilderMetrics,
traits::ClientBounds,
};
Expand Down Expand Up @@ -117,7 +116,7 @@ impl OpPayloadSyncerCtx {
metrics: self.metrics,
max_gas_per_txn: self.max_gas_per_txn,
max_uncompressed_block_size: self.max_uncompressed_block_size,
address_gas_limiter: AddressGasLimiter::new(GasLimiterArgs::default()),
gas_limiters: None,
backrun_ctx,
exclude_reverts_between_flashblocks: self.exclude_reverts_between_flashblocks,
enable_tx_tracking_debug_logs: self.enable_tx_tracking_debug_logs,
Expand Down
19 changes: 18 additions & 1 deletion crates/op-rbuilder/src/gas_limiter/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clap::Args;

#[derive(Debug, Clone, Default, PartialEq, Eq, Args)]
pub struct GasLimiterArgs {
/// Enable address-based gas rate limiting
/// Enable address-based gas rate limiting for public-mempool txs.
#[arg(long = "gas-limiter.enabled", env)]
pub gas_limiter_enabled: bool,

Expand All @@ -25,4 +25,21 @@ pub struct GasLimiterArgs {
/// How many blocks to wait before cleaning up stale buckets for addresses.
#[arg(long = "gas-limiter.cleanup-interval", env, default_value = "100")]
pub cleanup_interval: u64,

/// Per-address cap for the bundle limiter. Defaults to 10 million gas.
#[arg(
long = "gas-limiter.bundle-max-gas-per-address",
env,
default_value = "10000000"
)]
pub bundle_max_gas_per_address: u64,

/// Refill rate per block for the bundle limiter. Defaults to 1 million
/// gas per block.
#[arg(
long = "gas-limiter.bundle-refill-rate-per-block",
env,
default_value = "1000000"
)]
pub bundle_refill_rate_per_block: u64,
}
Loading
Loading