Skip to content

fix(p2p): bound envelopes-by-root answers by bytes and read them as they are sent - #958

Merged
pablodeymo merged 5 commits into
fix/envelopes-by-range-byte-budgetfrom
fix/envelopes-by-root-byte-budget
Dec 2, 2026
Merged

pablodeymo merged 5 commits into
fix/envelopes-by-range-byte-budgetfrom
fix/envelopes-by-root-byte-budget

Conversation

@pablodeymo

@pablodeymo pablodeymo commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #956. Review and merge that one first; this PR's diff is only the by-root part.

Motivation

#956 bounds execution_payload_envelopes_by_range (#895). execution_payload_envelopes_by_root has the same shape and was left open there:

  • It collected up to MAX_REQUEST_PAYLOADS (128) decoded envelopes in the P2P actor, with no bound on their total size.
  • It answered a root once per mention. A request naming one large block's root 128 times loaded about 1 GiB from a single stored envelope.

What changed

The by-root answer uses the by-range mechanism (req_resp/envelopes.rs, storage/src/store.rs)

  • The answer stops once ENVELOPES_BY_ROOT_BYTE_BUDGET has gone out. It is the range's 16 MiB of SSZ, for the same reasons. The specification allows it ("Clients MAY limit the number of payload envelopes in the response") and asks for "at least one payload envelope, if they have it", which stopping only once the budget is spent gives.
  • The handler only resolves each root to its block's slot (Store::execution_payload_envelopes_of). The codec reads each envelope row as it writes that chunk (EnvelopesByRoot), so the answer is never in memory whole and the P2P actor reads no envelope.
  • EnvelopeAnswer<I> is the budgeted answer both protocols now share. EnvelopesByRange and EnvelopesByRoot are aliases over where the envelopes come from. The budget, the oversized-envelope skip and the end-on-read-failure rule are written once.
  • A row is not decoded on the by-root path, since there is no chain rule to apply. It goes on the wire as stored.

A repeated root is answered once, at its first place. The answer is "a list [...] whose length is less than or equal to the number of requested" envelopes either way.

The requester stops reading an answer at a byte budget (req_resp/encoding.rs, beacon/encoding.rs)

  • A peer may send MAX_REQUEST_PAYLOADS chunks of MAX_PAYLOAD_SIZE to either envelope protocol, whatever was asked, and the reader held every decoded envelope until the stream ended: about 1.25 GiB for one request. The automated review listed this as present on main.
  • ChunkLimits gains max_bytes. Envelope answers are read under ENVELOPE_RESPONSE_BYTE_BUDGET, the servers' 16 MiB: the envelope that crosses it is kept, so the first always is, and one answer holds under 26 MiB.
  • A longer answer is cut, not refused, and reads as a short one. By range the requester already asks again for the slots after its last envelope (fix(p2p): bound envelopes-by-range answers by bytes and read them as they are sent #956); by root this node names one root a request.

Docs. docs/beacon_wire.md and CLAUDE.md.

Behaviour preserved

  • Unknown roots are left out, the answer follows the order asked, and no more than MAX_REQUEST_PAYLOADS roots are looked at.
  • By root there is still no chain rule: an envelope the canonical chain did not build on is served when asked for by name.
  • This node's own by-root requests name a single root, so its requester is never cut short and needs no change.

Limits

  • Another client that asks for many large envelopes by root in one request now gets a short answer and has to ask again for the rest. The specification allows the short answer; how each client handles it was not checked.
  • A by-root row goes out as stored, without the decode a by-range row gets in fix(p2p): bound envelopes-by-range answers by bytes and read them as they are sent #956, so a corrupt row would be sent as a chunk the requester cannot decode.
  • The block and data-column readers still read to their chunk limits with no byte cap; only the envelope reader is bounded here.
  • Not run against a live peer or a devnet.

Testing

  • make fmt, make lint and make test-node pass (2,853 passed, 0 failed, 9 ignored). make test-consensus and make test-ethrex were not run locally; the changed crates (ethlambda-storage, ethlambda-p2p) are in the test-node half.
  • New storage test: planning a by-root answer makes no read or existence check on the envelope table, each step reads one row in the order named, and roots with no block or no envelope are passed over.
  • New serving tests: a repeated root is answered once, the budget boundary and the first envelope under any budget, an envelope the chain did not build on is still served by name, and the answer round-trips through Codec::write_response and read_response.
  • New reader test: a four-envelope answer read under a budget exactly on an envelope boundary, one byte under, one byte over, and near zero; the first envelope is always read.

…hey are sent

`execution_payload_envelopes_by_root` collected up to
`MAX_REQUEST_PAYLOADS` (128) decoded envelopes in the P2P actor with no
byte bound, and answered a root once per mention. A request naming one
large block's root 128 times loaded about 1 GiB from a single stored
envelope.

- The answer stops once `ENVELOPES_BY_ROOT_BYTE_BUDGET` (the range's
  16 MiB of SSZ) has gone out, always carrying the first envelope it has.
- The handler resolves each root to its block's slot and the codec reads
  each row as it writes that chunk (`EnvelopesByRoot`,
  `Store::execution_payload_envelopes_of`), the way the by-range answer
  already does. `EnvelopeAnswer<I>` is the budgeted answer both share.
- A root named more than once is answered once, at its first place.
@pablodeymo

Copy link
Copy Markdown
Collaborator Author

/review

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

AI security review: 0 finding(s) to check

Reviewed by Opus, GPT, Kimi, GLM, DeepSeek. A judge model checked every one of the findings against the code.

No findings to check.

Pre-existing (real, but already on the base branch): 1
# Finding Category Severity (reported → judged) Found by Where Judge's reason
M1 Inbound envelope responses have no total byte budget DoS/resource high DeepSeek, GLM (2/5) crates/net/p2p/src/beacon/encoding.rs:484 Both reports identify a real inbound memory-exhaustion risk: either envelope protocol accepts 128 chunks of up to 10 MiB each and retains decoded envelopes until EOF, without an aggregate byte cap. Even a one-root request uses this limit; root filtering occurs afterward and does not reject duplicate matching envelopes. Compressible transaction bytes can therefore drive roughly 1.25 GiB of retained

Cost of this review: ~$1.90 of a $30 budget (estimated from token counts).

…by-root-byte-budget

# Conflicts:
#	CLAUDE.md
#	crates/storage/src/store.rs
#	docs/beacon_wire.md
A peer answering either envelope protocol may send MAX_REQUEST_PAYLOADS
chunks of MAX_PAYLOAD_SIZE, whatever was asked for, and the reader decoded
and held every one until the stream ended: about 1.25 GiB for one request.
The serving budgets bound what this node sends, not what it reads.

ChunkLimits gains max_bytes: once that many payload bytes have been read no
further chunk is, and what was read is the answer. The chunk that crosses
the limit is kept, so the first always is. Envelope answers are read under
ENVELOPE_RESPONSE_BYTE_BUDGET, the servers' 16 MiB, so one answer holds
under 26 MiB and an answer from a node with this serving budget is read
whole. A longer one is cut rather than refused and reads as a short answer:
by range the requester already asks again for the slots after its last
envelope, and by root this node names one root a request. The block and
column readers are unchanged.
@pablodeymo

Copy link
Copy Markdown
Collaborator Author

/review

…by-root-byte-budget

# Conflicts:
#	crates/net/p2p/src/req_resp/messages.rs
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

AI security review: 0 finding(s) to check

Reviewed by Opus, GPT, Kimi, GLM, DeepSeek. A judge model checked every one of the findings against the code.

No findings to check.

Pre-existing (real, but already on the base branch): 4
# Finding Category Severity (reported → judged) Found by Where Judge's reason
M1 Beacon block responses can still accumulate over a gigabyte in memory DoS/resource high Kimi (1/5) crates/net/p2p/src/beacon/encoding.rs:211 Confirmed: block responses can retain 128 near-10-MiB decoded blocks before request-specific filtering. The sidecar and lean readers also lack cumulative byte limits. Base already used the same accumulating decoder and chunk ceilings; adding max_bytes: None preserves that behavior.
M2 Envelope byte budget does not bound decoded heap size DoS/resource high GLM (1/5) crates/net/p2p/src/req_resp/encoding.rs:281 Confirmed: the budget measures serialized payload bytes, not decoded allocations, and retains the crossing chunk. Unbounded nested progressive transaction lists permit millions of small transactions with substantial per-transaction heap overhead before root or signature validation. Base used the same types and decoding path without any cumulative byte budget, so this PR reduces an existing exposur
M3 By-root planning amplifies pre-Gloas lookups through full block rows DoS/resource high GLM (1/5) crates/storage/src/store.rs:4607 Confirmed: planning synchronously resolves every distinct requested root through a full BlockHeaders value before the serving budget applies, including pre-Gloas blocks. RocksDB pinning avoids a Rust-side copy but still retrieves the value; actual physical disk traffic depends on caching. Base already called get_execution_payload_envelope for each root, which performed the same block-slot lookup,
M4 By-root root resolution panics on storage read failure memory safety/panic low GLM (1/5) crates/storage/src/store.rs:1464 Confirmed: a backend read error during eager root resolution reaches expect("read") and panics before the fallible envelope iterator exists. The later graceful error handling cannot catch it. Base's by-root handler reached the identical helper through get_execution_payload_envelope and beacon_block_slot, so the peer-triggered panic path already existed.

Cost of this review: ~$2.73 of a $30 budget (estimated from token counts).

@pablodeymo
pablodeymo merged commit 40f8098 into fix/envelopes-by-range-byte-budget Dec 2, 2026
14 checks passed
@pablodeymo
pablodeymo deleted the fix/envelopes-by-root-byte-budget branch December 2, 2026 03:58
pablodeymo added a commit that referenced this pull request Dec 2, 2026
…they are sent (#968)

## 🗒️ Description / Motivation

Backport of #956 to `release/v2.0.0`, for `v2.0.0-rc.2`.

#956 is approved and not merged on `main` yet, so this is taken from its
head (`36232dcf`) and not from a squash commit on `main`. If #956
changes before it merges, this branch has to be refreshed from the new
head.

## What Changed

The ten commits of #956 as one commit on top of `release/v2.0.0`
(`3ed99c86`): the diff from its branch point on `main` (`e48246f8`) to
its head (`36232dcf`), applied with `git apply --3way`. It applied
without conflicts and nothing was edited by hand. That includes #958,
the by-root counterpart, which was merged into #956's branch.

What it does, from #956:

- An `execution_payload_envelopes_by_range` answer stops once 16 MiB of
SSZ has gone out, always carrying the first envelope its window has.
Before, one request could make the node load and send about 1 GiB.
- Envelopes are read as they are sent: the codec reads each row just
before writing its chunk, and a row that fails to read or decode ends
the answer where it is.
- The requester asks the same peer again after an answer that stops
short, for up to 30 s.
- `execution_payload_envelopes_by_root` answers are bounded and read the
same way (#958).

## Tests Added / Run

On this branch:

- `cargo fmt --all -- --check` and `make lint`: clean.
- `cargo test -p ethlambda-p2p --profile release-fast --lib`: 587
passed, 0 failed, 1 ignored.
- `cargo test -p ethlambda-storage --profile release-fast --features
rocksdb`: 256 passed, 0 failed, 2 ignored.

Not run locally: the rest of `make test`.

## Related Issues / PRs

- Original: #956 (closes #895 on `main`), with #958.
- The other PRs open against the release branch: #954, #962 (backport of
#950) and #965 (backport of #952). This one merges with each of them
without conflicts (checked with `git merge-tree`), though it shares
`crates/net/p2p/src/lib.rs`, `CLAUDE.md` and `docs/beacon_wire.md` with
some of them.
- Release checklist: #943.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant