Skip to content

fix(discovery): start the ENR's seq at the clock so a restart replaces the record peers hold - #954

Merged
pablodeymo merged 2 commits into
release/v2.0.0from
fix/enr-seq-starts-at-the-clock
Dec 2, 2026
Merged

pablodeymo merged 2 commits into
release/v2.0.0from
fix/enr-seq-starts-at-the-clock

Conversation

@pablodeymo

@pablodeymo pablodeymo commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

🗒️ Description / Motivation

Targets release/v2.0.0 directly: it is the part of the "Decide on #735" item on #943's checklist that can ship in v2.0.0. main gets the same fix through #735.

The ENR this node serves over discv5 keeps the fork digest it started with across a fork boundary (#932): ethrex's DiscoveryServer cannot replace its record at runtime. #735 fixes that, but it depends on ethrex work that is not in a release (lambdaclass/ethrex#7488), so it cannot be in v2.0.0, which pins ethrex to a release tag. The plan for v2.0.0 is to state the gap in the release notes and tell operators to restart after Hoodi's Glamsterdam boundary (epoch 143616, 2026-12-15 19:12:24 UTC).

That restart does not reliably work today. The record is signed at ethrex's INITIAL_ENR_SEQ (1) on every boot, and a discv5 peer takes a replacement record only at a strictly higher seq. So after a restart, peers that already hold the node's record keep their copy, previous fork digest included, and hand it on in their NODES answers. docs/discovery.md listed this as a known limitation for upgrades ("An upgrade's new ENR entries are invisible to peers that stayed up").

This PR takes two parts of #735: the starting seq, and the record kept across restarts. Neither needs an ethrex change or a lockfile change.

The second part is there because of the first. A record that starts at the clock is newer than the one peers hold, and on a node started without --discovery.advertise-ip it is at the bind address, 0.0.0.0. Peers would swap a record they can dial for one they cannot, until IP voting settled again. The AI security review raised this on #735 (its M1), and the fix here is the one made there.

What Changed

  • crates/net/p2p/src/discovery/enr.rs:
    • build_local_enr_after signs the record at starting_seq(previous_seq): the current time in milliseconds, or one past the seq of the record this node last served where the clock has not got that far.
    • carried_over reads what a start may take from that last record, its address and its seq, and takes nothing from a record under another node key or with a bad signature.
  • crates/net/p2p/src/discovery/mod.rs:
    • spawn_discovery takes the last record. A start that names no address takes the last record's, within the address family the discovery socket is bound on (resumes_address), and logs that it did. IP voting stays on in that case.
    • ServedEnr reads the served record through the mirror ethrex writes each re-signed record into, which this branch used to hand over and drop.
    • The warning logged at a boundary (update_served_enr) ends with "Restart the node to publish the new fork digest".
  • crates/net/p2p/src/discovery/dial.rs: save_served_enr, called on every dial tick, saves the served record when its seq has moved. IP voting re-signs the record inside ethrex, so there is no event to save it on.
  • crates/net/p2p/src/lib.rs: P2P::spawn reads the saved record and hands it to spawn_discovery.
  • crates/storage/src/store.rs: Store::local_enr / set_local_enr, a new Metadata row (local_enr). An older directory has no row; DB_VERSION does not change.
  • Docs: docs/discovery.md, docs/beacon_wire.md, docs/data_storage.md, docs/cli.md (the --node-key row said the ENR stays the same across restarts) and CLAUDE.md.

Correctness / Behavior Guarantees

  • The served record still cannot change at a boundary. A node running across one keeps following the chain, keeps its peers and still dials out; peers that discover it afterwards reject its record until it restarts.
  • A restart serves a record above any seq the previous run reached: above the clock in every ordinary case, and above the saved record's seq when the clock reads earlier than it did.
  • A restart without --discovery.advertise-ip starts at the address its last record carried. If the address has changed, peers dial a stale one until IP voting replaces it, which is what they did with their cached record before this PR.
  • An address the operator names is never replaced by the saved one.
  • A first start, a start under another node key, and a start whose saved record does not decode all behave as before: the bind address, at the clock.
  • The first start on this build also replaces the seq = 1 record peers hold from an earlier version.
  • Applies to ethlambda node with --discovery.enable too: same code.

Tests Added / Run

  • New, enr: the_record_starts_at_the_clock_not_at_a_fixed_seq, a_start_takes_over_the_address_and_seq_of_this_nodes_last_record, a_last_record_that_never_found_its_address_carries_only_its_seq, a_record_under_another_key_carries_nothing_over, a_record_starts_above_the_last_one_whatever_the_clock_says.
  • New, discovery: a_restart_serves_the_address_its_last_record_carried, an_address_the_operator_names_is_not_replaced_by_the_last_records, the_last_address_is_taken_only_within_the_bound_family.
  • New, dial: the_served_record_is_saved_once_and_again_when_it_moves.
  • New, store: the_served_enr_survives_a_reopen.
  • On this branch: cargo test -p ethlambda-p2p --profile release-fast --lib (575 passed, 1 ignored), cargo test -p ethlambda-storage --profile release-fast --lib (247 passed, 2 ignored), make lint and cargo fmt --all -- --check clean.
  • Not run locally: the rest of make test.
  • Not tested: a restart of a real node against live peers, and a live peer of another client fetching the restarted node's record. The strictly-higher rule was read in ethrex v33.0.0's discv5 handlers (pong_message.enr_seq > cached_seq; the handshake sends the record only when seq differs from the requester's); for other clients it is the ENR spec's rule.

Related Issues / PRs

✅ Verification Checklist

  • Ran make fmt — clean
  • Ran make lint (clippy with -D warnings) — clean
  • Ran make test (test-consensus, test-node and test-ethrex, at release-fast) — all passing (only ethlambda-p2p --lib and ethlambda-storage --lib)

@pablodeymo

Copy link
Copy Markdown
Collaborator Author

/review

…s the record peers hold

The record was signed at ethrex's INITIAL_ENR_SEQ (1) on every boot. A discv5
peer takes a replacement record only at a strictly higher seq, so after a
restart peers kept the copy they had cached from the previous run. That copy
carries the previous run's `eth2` entry, and the served record cannot be
edited at runtime (#932), so a restart is the only way a node publishes its
fork digest after a boundary: at a fixed seq even that did not reach peers
that already knew the node.

The record now starts at the current time in milliseconds, as geth's does.
This is the seq part of #735, which needs no ethrex change.

The boundary warning now tells the operator to restart.

Related to #932
…erved

The clock-based seq makes a restart's first record newer than the one
peers hold. On a node started without --discovery.advertise-ip that
record is at the bind address, 0.0.0.0, so peers would replace a record
they can dial with one they cannot, until IP voting settled again. With
the constant seq the old record stayed in place.

The node now saves the record it serves (Metadata "local_enr", on the
dial tick, whenever its seq moves), and a start reads it back:

- A start that names no address takes the last record's, within the
  family the discovery socket is bound on. Voting stays on and replaces
  it if it has changed.
- Every start signs above the last record's seq, so a clock that reads
  earlier than it did no longer puts the new record behind.

A record under another node key carries nothing over.

Also: starting_seq reads the clock through unix_now_ms, its test checks
what a restart needs in place of a bracket between two clock reads, the
doc comment no longer says geth starts its record at the clock (it does
so only for a node it has no stored counter for), and docs/cli.md no
longer says the ENR stays the same across restarts.

The same fix is on #735, which the AI security review flagged for this
(its M1). The code here differs only where #735 has an editable record:
this branch, cut from release/v2.0.0, has neither that nor #732's
LocalEnr, and reads the served seq through a small ServedEnr over the
mirror ethrex writes.
@pablodeymo
pablodeymo force-pushed the fix/enr-seq-starts-at-the-clock branch from 3843558 to 19f9116 Compare December 2, 2026 17:38
@pablodeymo
pablodeymo changed the base branch from main to release/v2.0.0 December 2, 2026 17:38
@pablodeymo

Copy link
Copy Markdown
Collaborator Author

/review

@pablodeymo

Copy link
Copy Markdown
Collaborator Author

Verified with Antigravity:

PR #954 (19f9116) is an accurate, self-contained port of the exact subset of PR #735 that release/v2.0.0 needs, with no ethrex
pin or Cargo.lock change.
──────

  1. Side-by-Side Check Against PR feat(discovery): republish the ENR's eth2 entry at fork boundaries #735 (bde6732, bfb7659, 874cb23)

• Clock + persisted seq floor (enr::starting_seq, enr::build_local_enr_after):
• Identical to #735: unix_now_ms().max(previous_seq.map_or(INITIAL_ENR_SEQ, |seq| seq.saturating_add(1))).
• Guarantees a first upgrade from v1.0.0 / v2.0.0-rc.1 (seq = 1, or 1 + k after IP voting) jumps to millisecond wall-clock seq so
peers replace their cached ENR after Hoodi's Glamsterdam restart, and guarantees a restart under backward clock skew still signs
strictly above previous_seq.
• Safe address & seq carryover (enr::carried_over, discovery::resumes_address):
• Identical to #735 (874cb23, fixing #735 security review M1): verifies compressed secp256k1 key ownership + previous.
verify_signature() before trusting the stored record, ignores unspecified IPs (0.0.0.0 / :: / ::ffff:0.0.0.0), and only reuses
the last voted IP when the operator didn't set --discovery.advertise-ip and the address family matches bind_ip.to_canonical().
is_ipv4().
• Verified against ethrex v33.0.0 (crates/networking/p2p/discovery/server.rs):
• ServedEnr(SharedLocalNode) replaces #735's LocalEnrWriter cleanly: in ethrex v33.0.0, DiscoveryServer::apply_predicted_ip
writes every PONG-voted re-signed NodeRecord into shared_local_node: Arc<RwLock> (guard.record = self.
local_node_record.clone()).
• Because DiscoveryConfig.nat_extip_set is still computed from config.advertise_ip (not resumed_ip), ip_override_locked stays
false when resuming a voted IP: if the external IP is unchanged, apply_predicted_ip short-circuits (winning_ip == self.
local_node.ip) without bumping seq; if it changed, IP voting updates and re-signs it, and dial::save_served_enr persists the new
record on the next dial tick.
• Storage compatibility (crates/storage/src/store.rs):
• Identical to #735: optional Metadata["local_enr"] RLP row with no DB_VERSION bump, so existing data directories resume cleanly
and corrupt bytes log a warning and fall back to None.
• Correctly omitted from #735 & documented:
• Leaves out runtime eth2 editing (ethrex#7488) and #732's live /eth/v1/node/identity plumbing; updates update_served_enr's
warning ("Restart the node to publish the new fork digest"), CLAUDE.md, docs/beacon_wire.md, docs/cli.md, docs/data_storage.md,
and docs/discovery.md to match v2.0.0's exact behavior (and notes that when release/v2.0.0 merges back into main, main's
discovery module wins).
• Conflict-free with all sibling v2.0.0 PRs:
• Verified a sequential 4-way git merge-tree --write-tree onto origin/release/v2.0.0 (ded3fcc): #954 + #962 + #965 + #968 all
merge together cleanly with zero conflicts.

──────
2. Heads-up: Re-trigger CI on #954 Before Merging

Just like #962, gh pr checks 954 reports no checks on the current head (19f9116) (the last CI run on that branch was on 8919274
before it was rebuilt on release/v2.0.0 and retargeted; note that the earlier docs failure on 8919274 was just a transient HTTP 503
from github.com in lychee, while mdbook build + mdbook-linkcheck2 passed).

@pablodeymo
pablodeymo merged commit b90e7a3 into release/v2.0.0 Dec 2, 2026
@pablodeymo
pablodeymo deleted the fix/enr-seq-starts-at-the-clock branch December 2, 2026 19:13
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.

2 participants