Skip to content

Commit 55edd74

Browse files
authored
refactor(gas_limiter): canonical + overlay design with epoch tag (#488)
* refactor(gas_limiter): canonical + overlay design with epoch tag * fixes
1 parent c2e80b6 commit 55edd74

7 files changed

Lines changed: 513 additions & 240 deletions

File tree

crates/op-rbuilder/src/builder/context.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::{
3535
},
3636
evm::OpBlockEvmFactory,
3737
hardforks::ActiveHardforks,
38-
limiter::AddressLimiter,
38+
limiter::{AddressLimiter, AddressLimiterGuard},
3939
metrics::OpRBuilderMetrics,
4040
primitives::reth::{ExecutionInfo, TxnExecutionResult},
4141
traits::PayloadTxsBounds,
@@ -64,8 +64,9 @@ pub struct OpPayloadBuilderCtx {
6464
pub max_gas_per_txn: Option<u64>,
6565
/// Maximum cumulative uncompressed (EIP-2718 encoded) block size in bytes.
6666
pub max_uncompressed_block_size: Option<u64>,
67-
/// Per-address rate limiting based on gas and/or compute time. This is an
68-
/// optional feature.
67+
/// Canonical per-address rate limiter (gas and/or compute time). Each
68+
/// payload job begins a per-build [`AddressLimiterGuard`] off this; the
69+
/// only direct mutation here is the per-block `refill_buckets`.
6970
pub address_limiter: AddressLimiter,
7071
/// Backrun bundle configuration.
7172
pub backrun_bundle_args: BackrunBundleArgs,
@@ -82,6 +83,7 @@ pub struct OpPayloadBuilderCtx {
8283
/// Container type that holds all necessities to build a new payload.
8384
/// This struct is constructed once per payload job.
8485
#[derive(derive_more::Constructor, derive_more::Deref)]
86+
#[allow(clippy::too_many_arguments)]
8587
pub struct OpPayloadJobCtx {
8688
/// Builder-lifetime configuration shared with all other in-flight jobs.
8789
#[deref]
@@ -98,6 +100,12 @@ pub struct OpPayloadJobCtx {
98100
cancel: FlashblockJobCancellation,
99101
/// Per-block view into the global backrun bundle pool.
100102
backrun_pool: Option<BackrunBundlePayloadPool>,
103+
/// Per-build guard onto the canonical [`AddressLimiter`] held by
104+
/// `builder_ctx`. Charges accumulate privately here and auto-commit back
105+
/// into the canonical when this ctx is dropped. Shadows the
106+
/// `address_limiter` field reached via `Deref` to `OpPayloadBuilderCtx`,
107+
/// so `self.address_limiter` inside this ctx always hits the guard.
108+
address_limiter: AddressLimiterGuard,
101109
}
102110

103111
impl OpPayloadJobCtx {

crates/op-rbuilder/src/builder/payload.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,8 @@ where
403403
.backrun_bundle_pool()
404404
.map(|pool| pool.block_pool(config.parent_header.number + 1));
405405

406+
let address_limiter = builder_ctx.address_limiter.begin();
407+
406408
Ok(OpPayloadJobCtx::new(
407409
Arc::clone(builder_ctx),
408410
evm_factory,
@@ -411,6 +413,7 @@ where
411413
hardforks,
412414
cancel,
413415
backrun_pool,
416+
address_limiter,
414417
))
415418
}
416419

@@ -442,6 +445,10 @@ where
442445
config.attributes.payload_attributes.id.to_string(),
443446
);
444447

448+
self.builder_ctx
449+
.address_limiter
450+
.refill_buckets(config.parent_header.number + 1);
451+
445452
let ctx = self
446453
.get_op_payload_job_ctx(config.clone(), payload_cancel.flashblock_child())
447454
.map_err(|e| PayloadBuilderError::Other(e.into()))?;
@@ -457,8 +464,6 @@ where
457464
ctx.enable_incremental_state_root,
458465
);
459466

460-
ctx.address_limiter.refresh(ctx.block_number());
461-
462467
// Phase 1: Build the fallback block.
463468
let fallback_span = if span.is_none() {
464469
tracing::Span::none()

0 commit comments

Comments
 (0)