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
9 changes: 7 additions & 2 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,16 @@ actual_slot = finalized_slot + 1 + relative_index
mailbox) and the store's head (which gossip imports move and the watermark never sees).
Fetched blocks reach the actor as `BlockSource::Sync`.
- Gloas adds `execution_payload_envelopes_by_{range,root}/1`. Server: canonical FULL
envelopes only, and the head's envelope only when the head node is FULL. Client: by root
envelopes only, and the head's envelope only when the head node is FULL. A by-range
answer stops once `ENVELOPES_BY_RANGE_BYTE_BUDGET` (16 MiB of SSZ) has gone out, always
carrying the first envelope, and is never in memory whole: the handler plans it and the
codec reads each row as it writes that chunk (`EnvelopesByRange`). Client: by root
on `needs_envelope` (same dedup and retry ladder as block fetches) and on a gossip vote
for a payload not verified here (at most once a slot per root), by range after a
gloas span's blocks were handed to the actor, with range sync holding the next batch
until the envelope answer is forwarded (the actor keeps unmatched envelopes only briefly)
until the envelope answer is forwarded (the actor keeps unmatched envelopes only briefly);
an answer that stops short of the span is followed by a request for the slots after its
last envelope, the batch still held, for up to `ENVELOPE_FOLLOW_UP_WINDOW` (30 s).
Their chunks carry four `<context-bytes>` (the block's own epoch's fork digest), which is why
`BeaconWire` and the codec carry `genesis_validators_root`. See [`docs/beacon_wire.md`](docs/beacon_wire.md)

Expand Down
81 changes: 72 additions & 9 deletions crates/net/p2p/src/beacon/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use super::protocols::{self, MAX_REQUEST_BLOCKS_DENEB};
use super::size_bounds;
use crate::req_resp::codec::write_success_chunk;
use crate::req_resp::encoding::{ChunkLimits, MAX_PAYLOAD_SIZE, invalid, read_chunked_response};
use crate::req_resp::envelopes::EnvelopesByRange;
use crate::req_resp::messages::BlocksByRangeRequest;

/// This chain's wire body for a slot window. Every field is shared, since this
Expand Down Expand Up @@ -351,13 +352,40 @@ where
.await
}

/// Whether an SSZ payload of `size` bytes is one this node sends as a response
/// chunk: `MAX_PAYLOAD_SIZE` less a margin, the cut the block and sidecar
/// writers use too.
pub(crate) fn fits_one_chunk(size: usize) -> bool {
size <= MAX_PAYLOAD_SIZE - 1024
}

/// Write one envelope chunk: the result code, the `ForkDigest` of the epoch
/// `slot` is in, and the envelope's SSZ.
///
/// The spec keys the context epoch off the block the envelope fulfills, so
/// `slot` is that block's.
async fn write_execution_payload_envelope_chunk<T>(
io: &mut T,
label: &'static str,
config: &Config,
genesis_validators_root: Root,
slot: u64,
encoded: Vec<u8>,
) -> io::Result<()>
where
T: AsyncWrite + Unpin + Send,
{
let epoch = slot / preset::slots_per_epoch();
let digest = compute_fork_digest(config, genesis_validators_root, epoch);
write_success_chunk(io, label, &digest, encoded).await
}

/// Write an envelope response: one result code, one `ForkDigest` and one
/// payload per envelope.
///
/// The counterpart of [`write_blocks_response`]. The spec keys the context
/// epoch off the block the envelope fulfills; an envelope carries no slot of
/// its own, but `verify_execution_payload_envelope` pins
/// `payload.slot_number` to that block's slot, so a stored envelope's
/// The counterpart of [`write_blocks_response`]. An envelope carries no slot
/// of its own for the context epoch, but `verify_execution_payload_envelope`
/// pins `payload.slot_number` to its block's slot, so a stored envelope's
/// `slot_number` is the block's slot.
pub async fn write_execution_payload_envelopes_response<T>(
io: &mut T,
Expand All @@ -372,20 +400,55 @@ where
for envelope in envelopes {
let encoded = envelope.to_ssz();
let slot = envelope.message.payload.slot_number;
if encoded.len() > MAX_PAYLOAD_SIZE - 1024 {
if !fits_one_chunk(encoded.len()) {
warn!(
slot,
size = encoded.len(),
"Skipping oversized execution payload envelope in response"
);
continue;
}
let digest = compute_fork_digest(
write_execution_payload_envelope_chunk(
io,
label,
config,
genesis_validators_root,
slot / preset::slots_per_epoch(),
);
write_success_chunk(io, label, &digest, encoded).await?;
slot,
encoded,
)
.await?;
}
Ok(())
}

/// Write this node's own `ExecutionPayloadEnvelopesByRange` answer, reading
/// each envelope out of the store just before its chunk goes out.
///
/// Same chunks as [`write_execution_payload_envelopes_response`], without the
/// answer ever being in memory whole: one envelope is held at a time, and the
/// next is read only once the peer has taken this one. Which envelopes there
/// are, and where the answer stops, is [`EnvelopesByRange`]'s business. The
/// rows go out as stored, with no decode and re-encode in between.
pub async fn write_envelopes_by_range_response<T>(
io: &mut T,
label: &'static str,
config: &Config,
genesis_validators_root: Root,
answer: &mut EnvelopesByRange,
) -> io::Result<()>
where
T: AsyncWrite + Unpin + Send,
{
while let Some(envelope) = answer.next_envelope() {
write_execution_payload_envelope_chunk(
io,
label,
config,
genesis_validators_root,
envelope.slot,
envelope.ssz,
)
.await?;
}
Ok(())
}
Expand Down
32 changes: 23 additions & 9 deletions crates/net/p2p/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,14 +525,22 @@ pub(crate) enum PendingRequestKind {
/// asked for the slots a `BeaconBlocksByRange` answer just delivered.
///
/// Like [`Self::ColumnRange`] it carries no per-root bookkeeping: a short
/// or empty answer is not a failure to retry, since the chain actor asks
/// again by root for every parent still missing its envelope.
/// or empty answer is not a failure to retry. An answer that stops short
/// of `end_slot` is followed by another of these for the slots after its
/// last envelope, and the chain actor asks by root for every parent
/// still missing its envelope after that.
EnvelopeRange {
start_slot: u64,
end_slot: u64,
/// The range session that asked, whose gate the answer (or its
/// failure) releases; see [`RangeSyncState::id`].
/// failure) releases; see [`RangeSyncState::id`]. `None` when no
/// session waits on the answer: there was none, or it ran out of
/// range with this request still out
/// (`release_envelope_requests_of`).
session: Option<u64>,
/// Until when a short answer is still followed by a request for the
/// rest of the span; see `ENVELOPE_FOLLOW_UP_WINDOW`.
follow_ups_until: Instant,
},
}

Expand Down Expand Up @@ -2138,7 +2146,11 @@ pub(crate) trait P2PProtocol: Send + Sync {
#[allow(dead_code)] // invoked via send_after, not called directly
fn retry_envelope_fetch(&self, block_root: H256) -> Result<(), ActorError>;
#[allow(dead_code)] // invoked via send_after, not called directly
fn resume_range_after_envelopes(&self, session: u64) -> Result<(), ActorError>;
fn resume_range_after_envelopes(
&self,
session: Option<u64>,
rest: Option<req_resp::UnansweredEnvelopes>,
) -> Result<(), ActorError>;
#[allow(dead_code)] // invoked via send_after, not called directly
fn retry_peer_redial(&self, peer_id: PeerId) -> Result<(), ActorError>;
#[allow(dead_code)] // invoked via send_after, not called directly
Expand Down Expand Up @@ -2199,17 +2211,19 @@ impl P2PServer {
}
}

/// A range answer's envelopes are checked and forwarded. The check runs
/// on its own task, so the session that asked for them may have ended
/// meanwhile (`end_range_session`), and a later one may be waiting on
/// envelopes of its own, which this must not release.
/// A range answer's envelopes are checked and forwarded: ask for the
/// slots the answer stopped short of, or let range sync go on. The check
/// runs on its own task, so the session that asked for them may have
/// ended meanwhile (`end_range_session`), and a later one may be waiting
/// on envelopes of its own, which this must neither release nor ask on
/// behalf of.
#[send_handler]
async fn handle_resume_range_after_envelopes(
&mut self,
msg: p2p_protocol::ResumeRangeAfterEnvelopes,
ctx: &Context<Self>,
) {
req_resp::handlers::release_envelope_gate(self, ctx, Some(msg.session)).await;
req_resp::resume_range_after_envelopes(self, ctx, msg.session, msg.rest).await;
}

#[send_handler]
Expand Down
15 changes: 14 additions & 1 deletion crates/net/p2p/src/req_resp/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,9 @@ impl libp2p::request_response::Codec for Codec {
{
let label = protocol_label(protocol.as_ref());
match resp {
Response::Success { payload } => match &payload {
// By `&mut`: the by-range envelope answer is consumed as it is
// written. Every other payload is only read.
Response::Success { mut payload } => match &mut payload {
ResponsePayload::LeanStatus(status) => {
write_success_chunk(io, label, NO_CONTEXT, lean_encoding::encode_status(status))
.await
Expand Down Expand Up @@ -440,6 +442,17 @@ impl libp2p::request_response::Codec for Codec {
)
.await
}
ResponsePayload::ExecutionPayloadEnvelopesByRange(answer) => {
let context = self.beacon_context(protocol.as_ref())?;
beacon_encoding::write_envelopes_by_range_response(
io,
label,
&context.config,
context.genesis_validators_root,
answer,
)
.await
}
},
Response::Error { code, message } => {
// Send error code
Expand Down
Loading
Loading