Skip to content

feat(l2): configure block max gas limit#4211

Merged
avilagaston9 merged 22 commits into
mainfrom
feat/l2/make_gas_limit_configurable
Sep 16, 2025
Merged

feat(l2): configure block max gas limit#4211
avilagaston9 merged 22 commits into
mainfrom
feat/l2/make_gas_limit_configurable

Conversation

@avilagaston9

@avilagaston9 avilagaston9 commented Aug 29, 2025

Copy link
Copy Markdown
Contributor

Caution

Merge after #4224

Motivation

We need a way to limit the gas used per block to avoid overloading our prover.

Description

  • Adds a new CLI parameter: max_gas_limit.
  • Updates the payload_builder to check this parameter as well.

How to test

Set a low max_gas_limit, start the L2, and watch in the monitor how the initial deposits are distributed across different blocks, ensuring that the gas used per block is lower than the configured amount.

Closes #4207
Closes #2526

@github-actions

github-actions Bot commented Aug 29, 2025

Copy link
Copy Markdown

Lines of code report

Total lines added: 61
Total lines removed: 0
Total lines changed: 61

Detailed view
+--------------------------------------------------------------+-------+------+
| File                                                         | Lines | Diff |
+--------------------------------------------------------------+-------+------+
| ethrex/cmd/ethrex/bench/build_block_benchmark.rs             | 233   | +4   |
+--------------------------------------------------------------+-------+------+
| ethrex/cmd/ethrex/initializers.rs                            | 392   | +3   |
+--------------------------------------------------------------+-------+------+
| ethrex/cmd/ethrex/l2/initializers.rs                         | 249   | +4   |
+--------------------------------------------------------------+-------+------+
| ethrex/cmd/ethrex/l2/options.rs                              | 788   | +11  |
+--------------------------------------------------------------+-------+------+
| ethrex/cmd/ethrex_replay/src/cli.rs                          | 785   | +1   |
+--------------------------------------------------------------+-------+------+
| ethrex/crates/blockchain/payload.rs                          | 655   | +3   |
+--------------------------------------------------------------+-------+------+
| ethrex/crates/blockchain/smoke_test.rs                       | 240   | +1   |
+--------------------------------------------------------------+-------+------+
| ethrex/crates/common/types/constants.rs                      | 16    | +1   |
+--------------------------------------------------------------+-------+------+
| ethrex/crates/l2/networking/rpc/rpc.rs                       | 197   | +2   |
+--------------------------------------------------------------+-------+------+
| ethrex/crates/l2/sequencer/block_producer.rs                 | 196   | +5   |
+--------------------------------------------------------------+-------+------+
| ethrex/crates/l2/sequencer/block_producer/payload_builder.rs | 353   | +12  |
+--------------------------------------------------------------+-------+------+
| ethrex/crates/l2/sequencer/configs.rs                        | 90    | +1   |
+--------------------------------------------------------------+-------+------+
| ethrex/crates/networking/rpc/engine/fork_choice.rs           | 357   | +1   |
+--------------------------------------------------------------+-------+------+
| ethrex/crates/networking/rpc/eth/fee_market.rs               | 229   | +6   |
+--------------------------------------------------------------+-------+------+
| ethrex/crates/networking/rpc/rpc.rs                          | 567   | +4   |
+--------------------------------------------------------------+-------+------+
| ethrex/crates/networking/rpc/utils.rs                        | 375   | +2   |
+--------------------------------------------------------------+-------+------+

@avilagaston9 avilagaston9 force-pushed the feat/l2/make_gas_limit_configurable branch from 6e19c69 to a804002 Compare September 2, 2025 15:09
@avilagaston9 avilagaston9 changed the base branch from main to fix/l2/payload_builder September 2, 2025 15:10
@avilagaston9 avilagaston9 marked this pull request as ready for review September 2, 2025 15:20
@avilagaston9 avilagaston9 requested a review from a team as a code owner September 2, 2025 15:20
@avilagaston9 avilagaston9 moved this to In Review in ethrex_l2 Sep 2, 2025
@avilagaston9 avilagaston9 force-pushed the feat/l2/make_gas_limit_configurable branch from 58ca44a to a804002 Compare September 2, 2025 16:42
Base automatically changed from fix/l2/payload_builder to main September 9, 2025 13:49
sumitvekariya pushed a commit to sumitvekariya/ethrex that referenced this pull request Sep 9, 2025
…lass#4224)

**Motivation**

Noticed a weird behavior while testing lambdaclass#4211. Once the configured
`max_gas_limit` was reached, no further deposits were included in
subsequent blocks until a new batch is started. This happens because we
first update the `privileged_range`, and if the transaction is later
rejected (for example, due to the gas limit), we don’t remove the nonce
from the range. As a result, when the next block starts, we try to add
the tx again, but since the nonce was already included to the range, the
transaction is marked as out-of-order.

#### Why haven’t we seen the error before?

There is an `if` check that prevents the error from appearing in our
daily usage:

```Rust
// Check if we have enough space for the StateDiff to run more transactions
if acc_size_without_accounts + size_accounts_diffs + SIMPLE_TX_STATE_DIFF_SIZE
> safe_bytes_per_blob {
	warn!("No more StateDiff space to run transactions");
	break;
};
```

This is an early return that checks if we have enough space for a simple
transfer in our `stateDiff`, and stops adding transactions to the block
if we don’t. But, it can happen that we have enough space for a simple
transfer, but the transaction we are trying to add occupies more space
than that, making us restore the previous state in another check a[ few
lines
later](https://github.com/lambdaclass/ethrex/blob/0cae720162fd889eb18417010e5f091c6d097d35/crates/l2/sequencer/block_producer/payload_builder.rs#L215).

<!-- 
#### How to trigger the error

The easiest way to trigger the error is to comment out the early return,
allowing the `payload_builder` to attempt restoring the previous state.
Then start the `L2` and `prover` with:

```
make init
make init-prover
```

and run a load test with:
```
cargo run --release --manifest-path ./tooling/load_test/Cargo.toml -- -k ./fixtures/keys/private_keys_l1.txt -t eth-transfers -n "http://localhost:1729"
```

Observe the following behavior in the monitor:

After a block reaches its max `stateDiff` size allowed, the subsequent
blocks contain 0 transactions until a new batch is started.
-->

**Description**

Moves the update of `privileged_range` after all other checks have
passed.


Issues lambdaclass#4248 and lambdaclass#4243 were created.
Closes None
Copilot AI review requested due to automatic review settings September 9, 2025 14:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a configurable maximum gas limit for L2 blocks to prevent overloading the prover by limiting the gas consumption per block.

  • Adds a new CLI parameter max_gas_limit to configure the maximum gas allowed per block
  • Updates the payload builder to enforce this gas limit during transaction selection
  • Fixes a minor bug in privileged transaction counting logic

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
crates/l2/sequencer/configs.rs Adds max_gas_limit field to BlockProducerConfig
crates/l2/sequencer/block_producer/payload_builder.rs Updates payload building to respect the max gas limit and fixes privileged tx counting
crates/l2/sequencer/block_producer.rs Threads the max gas limit parameter through the block producer
cmd/ethrex/l2/options.rs Adds CLI option for configuring the maximum gas limit

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread crates/l2/sequencer/block_producer/payload_builder.rs Outdated
Comment thread cmd/ethrex/l2/options.rs Outdated
help = "Maximum gas limit for the L2 blocks.",
help_heading = "Block producer options"
)]
pub max_gas_limit: Option<u64>,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not block_gas_limit?

@ilitteri ilitteri left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a minor suggestion

@MegaRedHand MegaRedHand left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes LGTM. I think we should also signal the gas limit in the header of the block. I'm not sure if we have special-cased it in the L2, but on the L1 the client sets a "desired gas limit", to which it will converge with enough time, moving the gas limit (and target) a bit on each block.

Comment thread crates/l2/sequencer/block_producer/payload_builder.rs Outdated
Comment thread crates/l2/sequencer/block_producer/payload_builder.rs Outdated
Comment thread cmd/ethrex/l2/options.rs Outdated

@MegaRedHand MegaRedHand left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@avilagaston9

Copy link
Copy Markdown
Contributor Author

The PR was changed to not only use a configured_gas_limit in fill_transactions(), but also to use it as the node’s target_gas_limit.

@tomip01 tomip01 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment I just realized now

Comment thread crates/l2/sequencer/block_producer.rs Outdated
rollup_store,
// FIXME: Initialize properly to the last privileged nonce in the chain
last_privileged_nonce: None,
block_gas_limit: block_gas_limit.unwrap_or(DEFAULT_BUILDER_GAS_CEIL),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I am wrong but we are always defaulting it to DEFAULT_BUILDER_GAS_CEIL if None.

We could change from Option<u64> to u64 in the BlockProducerOptions. And add default_value_t = DEFAULT_BUILDER_GAS_CEIL, in the clap options

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done be164a7!

@avilagaston9 avilagaston9 added this pull request to the merge queue Sep 16, 2025
Merged via the queue into main with commit e1b03c8 Sep 16, 2025
44 checks passed
@avilagaston9 avilagaston9 deleted the feat/l2/make_gas_limit_configurable branch September 16, 2025 15:13
@github-project-automation github-project-automation Bot moved this from In Review to Done in ethrex_l2 Sep 16, 2025
github-merge-queue Bot pushed a commit that referenced this pull request Sep 16, 2025
> [!CAUTION]
> Merge after #4211

**Motivation**

We need a way to limit the gas used per batch to prevent overloading our
prover.


**Description**

- Adds a new CLI parameter: `batch_gas_limit`.
- Updates the `l1_committer` to ensure that the batch never exceeds this
limit if set.

Closes #4208
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

L2 Rollup client

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

L2: make gas_limit per block configurable Support changing the gas_limit on the L2 after genesis

5 participants