forked from flashbots/op-rbuilder
-
Notifications
You must be signed in to change notification settings - Fork 9
feat: track and optionally enforce resource limits during building #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
niran
wants to merge
14
commits into
main
Choose a base branch
from
limit-exec-time
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7d4dcd7
feat: add execution time limits to resource metering
niran ad848ab
feat: propagate execution time budget to flashblocks
niran ad408fc
test: integration coverage for execution-time limits
niran 36fa1de
refactor: model tx resource usage explicitly
niran c9073ce
Update comments
niran f9f20d1
refactor: minimize upstream diffs for execution time limits
niran 6f6d044
refactor: bundle Base-specific state into dedicated types
niran 72921cb
refactor: simplify BaseFlashblocksCtx to take u128 interval
niran e11c195
feat: add metrics and logging for execution time limit checks
niran 790bb25
feat: add --builder.enforce-resource-metering flag
niran 913a74a
refactor: move resource metering tests to base/ folder
niran 0543f3d
test: add coverage for non-enforcing resource metering mode
niran ce317cd
cargo fmt
niran c6f0aab
cargo clippy
niran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| //! Base-specific builder context. | ||
|
|
||
| use super::metrics::BaseMetrics; | ||
|
|
||
| /// Base-specific context for payload building. | ||
| /// Add this as a single field to OpPayloadBuilderCtx to minimize diff. | ||
| #[derive(Debug, Default, Clone)] | ||
| pub struct BaseBuilderCtx { | ||
| /// Block execution time limit in microseconds | ||
| pub block_execution_time_limit_us: u128, | ||
| /// Whether to enforce resource metering limits | ||
| pub enforce_limits: bool, | ||
| /// Base-specific metrics | ||
| pub metrics: BaseMetrics, | ||
| } | ||
|
|
||
| impl BaseBuilderCtx { | ||
| /// Create a new BaseBuilderCtx with the given execution time limit. | ||
| pub fn new(block_execution_time_limit_us: u128, enforce_limits: bool) -> Self { | ||
| Self { | ||
| block_execution_time_limit_us, | ||
| enforce_limits, | ||
| metrics: Default::default(), | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| //! Base-specific execution time tracking and limit checking. | ||
|
|
||
| use super::metrics::BaseMetrics; | ||
| use crate::resource_metering::ResourceMetering; | ||
| use alloy_primitives::TxHash; | ||
| use tracing::warn; | ||
|
|
||
| /// Base-specific execution state bundled into one type. | ||
| /// Add this as a single field to ExecutionInfo to minimize diff. | ||
| #[derive(Debug, Default, Clone)] | ||
| pub struct BaseExecutionState { | ||
| pub cumulative_execution_time_us: u128, | ||
| } | ||
|
|
||
| /// Base-specific transaction usage bundled into one type. | ||
| #[derive(Debug, Default, Clone, Copy)] | ||
| pub struct BaseTxUsage { | ||
| pub execution_time_us: u128, | ||
| } | ||
|
|
||
| /// Base-specific block limits bundled into one type. | ||
| #[derive(Debug, Clone, Copy)] | ||
| pub struct BaseBlockLimits { | ||
| pub execution_time_us: u128, | ||
| } | ||
|
|
||
| /// Result type for Base-specific limit checks. | ||
| #[derive(Debug)] | ||
| pub enum BaseLimitExceeded { | ||
| ExecutionTime { | ||
| tx_hash: TxHash, | ||
| cumulative_us: u128, | ||
| tx_us: u128, | ||
| limit_us: u128, | ||
| tx_gas: u64, | ||
| remaining_gas: u64, | ||
| }, | ||
| } | ||
|
|
||
| impl BaseLimitExceeded { | ||
| /// Returns the tx usage that caused the limit to be exceeded. | ||
| pub fn usage(&self) -> BaseTxUsage { | ||
| match self { | ||
| Self::ExecutionTime { tx_us, .. } => BaseTxUsage { | ||
| execution_time_us: *tx_us, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| /// Log and record metrics for this limit exceeded event. | ||
| /// | ||
| /// Only logs/records if this is the first tx to exceed the limit | ||
| /// (i.e., cumulative was within the limit before this tx). | ||
| pub fn log_and_record(&self, metrics: &BaseMetrics) { | ||
| match self { | ||
| Self::ExecutionTime { | ||
| tx_hash, | ||
| cumulative_us, | ||
| tx_us, | ||
| limit_us, | ||
| tx_gas, | ||
| remaining_gas, | ||
| } => { | ||
| // Only log/record for the first tx that exceeds the limit | ||
| if *cumulative_us > *limit_us { | ||
| return; | ||
| } | ||
|
|
||
| let remaining_us = limit_us.saturating_sub(*cumulative_us); | ||
| let exceeded_by_us = tx_us.saturating_sub(remaining_us); | ||
| warn!( | ||
| target: "payload_builder", | ||
| %tx_hash, | ||
| cumulative_us, | ||
| tx_us, | ||
| limit_us, | ||
| remaining_us, | ||
| exceeded_by_us, | ||
| tx_gas, | ||
| remaining_gas, | ||
| "Execution time limit exceeded" | ||
| ); | ||
| metrics.execution_time_limit_exceeded.increment(1); | ||
| metrics.execution_time_limit_tx_us.record(*tx_us as f64); | ||
| metrics | ||
| .execution_time_limit_remaining_us | ||
| .record(remaining_us as f64); | ||
| metrics | ||
| .execution_time_limit_exceeded_by_us | ||
| .record(exceeded_by_us as f64); | ||
| metrics.execution_time_limit_tx_gas.record(*tx_gas as f64); | ||
| metrics | ||
| .execution_time_limit_remaining_gas | ||
| .record(*remaining_gas as f64); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl BaseExecutionState { | ||
| /// Check if adding a tx would exceed Base-specific limits. | ||
| /// Call this AFTER the upstream is_tx_over_limits(). | ||
| /// Returns the usage for later recording via `record_tx`. | ||
| pub fn check_tx( | ||
| &self, | ||
| metering: &ResourceMetering, | ||
| tx_hash: &TxHash, | ||
| execution_time_limit_us: u128, | ||
| tx_gas: u64, | ||
| cumulative_gas_used: u64, | ||
| block_gas_limit: u64, | ||
| ) -> Result<BaseTxUsage, BaseLimitExceeded> { | ||
| let usage = BaseTxUsage::from_metering(metering, tx_hash); | ||
| let total = self | ||
| .cumulative_execution_time_us | ||
| .saturating_add(usage.execution_time_us); | ||
|
|
||
| if total > execution_time_limit_us { | ||
| let remaining_gas = block_gas_limit.saturating_sub(cumulative_gas_used); | ||
| return Err(BaseLimitExceeded::ExecutionTime { | ||
| tx_hash: *tx_hash, | ||
| cumulative_us: self.cumulative_execution_time_us, | ||
| tx_us: usage.execution_time_us, | ||
| limit_us: execution_time_limit_us, | ||
| tx_gas, | ||
| remaining_gas, | ||
| }); | ||
| } | ||
| Ok(usage) | ||
| } | ||
|
|
||
| /// Record that a transaction was included. | ||
| pub fn record_tx(&mut self, usage: &BaseTxUsage) { | ||
| self.cumulative_execution_time_us += usage.execution_time_us; | ||
| } | ||
| } | ||
|
|
||
| impl BaseTxUsage { | ||
| /// Get tx execution time from resource metering. | ||
| pub fn from_metering(metering: &ResourceMetering, tx_hash: &TxHash) -> Self { | ||
| let execution_time_us = metering | ||
| .get(tx_hash) | ||
| .map(|r| r.total_execution_time_us) | ||
| .unwrap_or(0); | ||
| Self { execution_time_us } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| //! Base-specific flashblocks context. | ||
|
|
||
| use super::context::BaseBuilderCtx; | ||
|
|
||
| /// Base-specific flashblocks context for per-batch execution time tracking. | ||
| /// Add this as a single field to FlashblocksExtraCtx to minimize diff. | ||
| #[derive(Debug, Default, Clone, Copy)] | ||
| pub struct BaseFlashblocksCtx { | ||
| /// Total execution time (us) limit for the current flashblock batch | ||
| pub target_execution_time_us: u128, | ||
| /// Execution time (us) limit per flashblock batch | ||
| pub execution_time_per_batch_us: u128, | ||
| /// Whether to enforce resource metering limits | ||
| pub enforce_limits: bool, | ||
| } | ||
|
|
||
| impl BaseFlashblocksCtx { | ||
| /// Create a new BaseFlashblocksCtx with the given execution time limit per batch. | ||
| pub fn new(execution_time_per_batch_us: u128, enforce_limits: bool) -> Self { | ||
| Self { | ||
| target_execution_time_us: execution_time_per_batch_us, | ||
| execution_time_per_batch_us, | ||
| enforce_limits, | ||
| } | ||
| } | ||
|
|
||
| /// Advance to the next batch, updating the target execution time. | ||
| /// | ||
| /// Unlike gas and DA, execution time does not carry over to the next batch. | ||
| pub fn next(self, cumulative_execution_time_us: u128) -> Self { | ||
| Self { | ||
| target_execution_time_us: cumulative_execution_time_us | ||
| + self.execution_time_per_batch_us, | ||
| ..self | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl From<&BaseFlashblocksCtx> for BaseBuilderCtx { | ||
| fn from(ctx: &BaseFlashblocksCtx) -> Self { | ||
| BaseBuilderCtx::new(ctx.target_execution_time_us, ctx.enforce_limits) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| //! Base-specific metrics. | ||
|
|
||
| use reth_metrics::{ | ||
| Metrics, | ||
| metrics::{Counter, Histogram}, | ||
| }; | ||
|
|
||
| /// Base-specific metrics for resource metering. | ||
| #[derive(Metrics, Clone)] | ||
| #[metrics(scope = "op_rbuilder_base")] | ||
| pub struct BaseMetrics { | ||
| /// Count of transactions excluded due to execution time limit | ||
| pub execution_time_limit_exceeded: Counter, | ||
| /// Histogram of tx execution time (us) that caused the limit to be exceeded | ||
| pub execution_time_limit_tx_us: Histogram, | ||
| /// Histogram of remaining execution time (us) when a tx was excluded | ||
| pub execution_time_limit_remaining_us: Histogram, | ||
cody-wang-cb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// Histogram of how much the tx exceeded the remaining time (us) | ||
| pub execution_time_limit_exceeded_by_us: Histogram, | ||
| /// Histogram of tx gas limit when excluded due to execution time limit | ||
| pub execution_time_limit_tx_gas: Histogram, | ||
| /// Histogram of remaining gas when excluded due to execution time limit | ||
| pub execution_time_limit_remaining_gas: Histogram, | ||
cody-wang-cb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| pub mod context; | ||
| pub mod execution; | ||
| pub mod flashblocks; | ||
| pub mod metrics; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.