Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,9 @@ actual_slot = finalized_slot + 1 + relative_index
one's left `UNSUBSCRIBE_LAG_EPOCHS` after, blob-parameter (BPO) boundaries included.
**Known gap**: ethrex's `DiscoveryServer` cannot replace the served ENR at runtime, so its
`eth2` entry keeps the startup digest until a restart; peers discovering the node after a
boundary reject the record
boundary reject the record. A restart publishes the new one because the record's `seq`
starts at the clock in milliseconds (`discovery::enr::starting_seq`), above the copy peers
hold; at a fixed starting `seq` they would keep that copy
- Validator custody (fulu `validator.md`; `p2p/src/beacon/validator_custody.rs`): the Beacon
API records every validator index named to `beacon_committee_subscriptions` and
`prepare_beacon_proposer` in `AttachedValidators` (`net/api`; expires after two silent
Expand Down
51 changes: 49 additions & 2 deletions crates/net/p2p/src/discovery/enr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

use std::collections::HashSet;
use std::net::IpAddr;
use std::time::{SystemTime, UNIX_EPOCH};

use ethrex_p2p::types::{INITIAL_ENR_SEQ, Node, NodeRecord, NodeRecordPairs};
use ethrex_p2p::utils::{node_id, public_key_from_signing_key};
Expand Down Expand Up @@ -233,12 +234,38 @@ pub(crate) fn dialable_port(port: u16) -> Option<u16> {
Some(port).filter(|port| *port != 0)
}

/// Build and sign this node's ENR.
/// Build and sign this node's ENR, at [`starting_seq`].
pub(crate) fn build_local_enr(params: &LocalEnrParams) -> Result<NodeRecord, DiscoveryError> {
NodeRecord::from_pairs(INITIAL_ENR_SEQ, &params.signer, params.local_pairs())
NodeRecord::from_pairs(starting_seq(), &params.signer, params.local_pairs())
.map_err(DiscoveryError::BuildEnr)
}

/// The `seq` this node's record starts at: the current time in milliseconds.
///
/// A peer fetches a record again only once its `seq` grows. Starting every boot
/// from [`INITIAL_ENR_SEQ`] would serve a record at or behind the copy peers
/// cached from the previous run, and they would keep that one. That is what
/// makes a restart publish what the running node cannot: the served `eth2`
/// entry keeps its startup fork digest across a boundary
/// ([`update_served_enr`](super::update_served_enr)), and only a restart whose
/// record peers take replaces it. An upgrade that changes which entries the
/// record carries depends on it the same way.
///
/// Nothing is persisted across restarts, so the clock is the floor: the only
/// edits to a running record are discv5 IP voting's, one `seq` each, so the
/// previous run would have had to edit its record more than once per
/// millisecond of uptime to pass it. geth starts its record the same way. A
/// clock stepped back past the previous run's last edit is the one case this
/// does not cover.
fn starting_seq() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.ok()
.and_then(|elapsed| u64::try_from(elapsed.as_millis()).ok())
.unwrap_or_default()
.max(INITIAL_ENR_SEQ)
}

/// The address a record advertises, preferring IPv4 when it carries both.
///
/// `None` for a record with neither `ip` nor `ip6`, which names no host to
Expand Down Expand Up @@ -309,6 +336,26 @@ mod tests {
.expect("ENR builds")
}

/// A restart must not serve a `seq` at or below what the previous run
/// reached: peers would keep their cached copy, stale `eth2` included. A
/// record that started at a fixed `seq` went back there on every boot.
#[test]
fn the_record_starts_at_the_clock_not_at_a_fixed_seq() {
fn now_ms() -> u64 {
let elapsed = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
u64::try_from(elapsed.as_millis()).unwrap()
}
let before = now_ms();
let record = build();
let after = now_ms();
assert!(
(before..=after).contains(&record.seq),
"seq {} outside [{before}, {after}]",
record.seq
);
assert!(record.verify_signature());
}

#[test]
fn node_id_from_secret_key_matches_the_discovery_servers_own_computation() {
// If this function ever computed a different id than `local_node`'s
Expand Down
6 changes: 4 additions & 2 deletions crates/net/p2p/src/discovery/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,15 @@ pub async fn spawn_discovery(
/// `Status`, which does follow the switch) and the admission filter does move,
/// but a peer that discovers this node after a boundary reads the stale digest
/// and rejects the record, so a long-running node becomes harder to find until
/// it is restarted.
/// it is restarted. A restart signs a new record at a higher `seq`
/// ([`enr::build_local_enr`]), which is what makes peers holding the old one
/// fetch it.
pub(crate) fn update_served_enr(fork_id: &EnrForkId) {
warn!(
fork_digest = %hex::encode(fork_id.fork_digest),
next_fork_epoch = fork_id.next_fork_epoch,
"The served ENR keeps its startup eth2 entry: the discovery server offers no way to \
replace its record at runtime"
replace its record at runtime. Restart the node to publish the new fork digest"
);
}

Expand Down
5 changes: 4 additions & 1 deletion docs/beacon_wire.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ digest (or the next one, within clock skew of the boundary).
One gap: the ENR this node serves over discv5 keeps the `eth2` entry it started
with, because ethrex's `DiscoveryServer` offers no way to replace its record at
runtime. Peers already connected are unaffected; peers discovering the node
after a boundary read the stale digest and reject the record.
after a boundary read the stale digest and reject the record. A restart after
the boundary publishes the new digest: the record is signed at a `seq` taken
from the clock, above the one peers hold, so they fetch it
(see [discovery.md](./discovery.md#the-enr)).

## Gossip

Expand Down
34 changes: 15 additions & 19 deletions docs/discovery.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ voting later changes our external address, ethrex edits and re-signs that record
rather than rebuilding one, so the consensus entries survive the bump; only the
sequence number and `ip` move, which the reported ENR then lags.

The record starts each boot at `seq` = the current time in milliseconds, not at
a constant. A peer identifies a record by (node id, seq) and takes a replacement
only at a strictly higher `seq`, and ethrex's WHOAREYOU responder does not even
send the record when the requester's `enr_seq` already matches. A restart that
began again from a constant would therefore serve a record at or behind the one
peers cached from the previous run (which IP voting may have bumped), and they
would keep the cached copy, stale `eth2`, address and all. The same goes for an
upgrade that changes which entries the record carries. Nothing is persisted, so
the clock is the floor: the previous run would have had to edit its record more
than once per millisecond of uptime to pass it. A clock stepped back past the
previous run's last edit is the one case it does not cover.

## Which peers get dialed

A discovered peer is admitted only if:
Expand All @@ -98,7 +110,9 @@ hardcoded dummy and `beacon` passes the digest it derived at startup and moves
it at each fork-digest boundary, so one admission policy serves both chains. The
record this node serves is not refreshed at a boundary (ethrex's
`DiscoveryServer` cannot replace it at runtime), so its `eth2` entry keeps the
startup digest until a restart; see [beacon_wire.md](./beacon_wire.md#the-fork-digest).
startup digest until a restart, whose record peers take because it starts at a
higher `seq` (see [The ENR](#the-enr)); see
[beacon_wire.md](./beacon_wire.md#the-fork-digest).

A differing `next_fork_version` or `next_fork_epoch` is *not* grounds for
rejection: the spec permits connecting to a peer that is incompatible with an
Expand Down Expand Up @@ -269,24 +283,6 @@ redial is what keeps a devnet's own bootnode reachable across its restarts.
Splitting the flag into initial peers, dialed once, and bootnodes, seeded into
discv5, is the real fix and is left to a follow-up.

### An upgrade's new ENR entries are invisible to peers that stayed up

The record is signed at ethrex's `INITIAL_ENR_SEQ`, a constant. A peer
identifies a record by (node id, seq) and accepts a replacement only at a
strictly higher seq, and ethrex's WHOAREYOU responder does not even send the
record when the requester's `enr_seq` already matches. So when an ethlambda
release changes which entries it publishes, as adding `tcp` did, a peer holding
the previous record under the same seq keeps it: the new entries reach only
peers that meet this node for the first time.

A fixed local floor above `INITIAL_ENR_SEQ` does not close this. ethrex re-signs
the record at `seq + 1` whenever discv5's IP voting moves the advertised
address, so a node behind NAT may already be serving a seq above any constant
the code could pick, which is exactly the case the floor was meant to cover.
Closing it needs a seq that grows without bound across restarts: a persisted
counter bumped on every content change, or one derived from the wall clock at
startup, which is what several beacon clients do.

### One lean devnet is not separated from another

The spec's `fork_digest` is derived from genesis, so it separates one chain from
Expand Down
Loading