Skip to content

photon: keep a block retryable when its header is refused for lack of room - #417

Open
kiwidream wants to merge 11 commits into
1.x.xfrom
photon/capacity-refusal-retry
Open

kiwidream wants to merge 11 commits into
1.x.xfrom
photon/capacity-refusal-retry

Conversation

@kiwidream

@kiwidream kiwidream commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

Not for v1.0.1. This change is deferred to a later release. The relay never re-sends a header, so the approach pushed so far rarely recovers a block in practice, and a reworked design is still being written. It also depends on #410.

Summary

Related #381, #410.

Depends on #410. This branch is based on #410's head and contains #410's commits until #410 merges; after that it is rebased onto 1.x.x and only this PR's own commits remain. The later commits rework the first, so review their combined diff (ca1cf91970..HEAD).

Since #381, a refused block header ends that block for the sending peer for 10 seconds: the rest of its header copies and chunks from that peer are ignored. That is intended for headers that no honest sender produces, but it also applied when a header was refused only because there was no room for its decoder at that moment. Senders send their header copies only before the block's chunks and never repeat them, so such a block could not be started from that peer even once room was free again. This is a robustness improvement to the inbound resource accounting:

  • Waiting for room. A header refused only because there is no room for its decoder now is kept with the block's incomplete entry: the entry made by chunks sent before the header, or a new one only if the peer and the relay each have an entry free without dropping anything. Otherwise the header is refused as before.
  • Chunks while waiting. They must fit the kept header (a chunk outside it counts against the peer, as after the decoder is built) and are buffered within the existing pre-header caps (64 chunks per block, and the total pre-header byte cap). The block is counted in inbound_rejected once for its header, and again only if it is later dropped for a chunk outside the header or a decoder that cannot be built; chunks that cannot be buffered are not counted.
  • Ending a block that cannot complete. A sender sends each chunk once, so a waiting block that has lost more chunks than all its coding groups have parity chunks can no longer complete. It then ends as if its header had been refused, instead of holding its entry, its buffered chunks and later a decoder.
  • Starting. After an incomplete block has been released, the next chunk or header copy of the waiting block builds its decoder if it fits the per-peer and total decoder caps without displacing any other block, then gets the buffered chunks. Starting it does not count as progress. Nothing is ever evicted for a waiting block, and room is looked for again only after a release, with a constant-time check.
  • Unchanged or restored. Headers that no honest sender produces still end the block and count against the peer. A decoder that exceeds the per-peer or total cap on its own, or that cannot be built, ends the block without counting against the peer, as before. A waiting entry is an ordinary pre-header entry otherwise: it ages out, can be displaced when idle, and is dropped when the block is accepted, settled or held, or its peer is paused.

Only contrib/photon/src/relay_engine.{h,cpp}, contrib/photon/test/relay_engine_tests.cpp and contrib/photon/RUNBOOK.md change. No wire-format, protocol or qbitd change; senders are unchanged, so mixed versions behave as before.

Testing

  • Built locally (contrib/photon, RelWithDebInfo, Ninja). No compiler warnings.
  • Ran focused unit or functional tests for the changed area.
    • New relay_engine_tests:
      • capacity_refused_block_completes_in_sender_order: another peer's block holds all decoder memory; the three header copies are refused, then each chunk is sent once in the sender's (ChunkScheduler) order, more than 64 of them before room frees; the block waits without a decoder, is refused once and not counted against the peer, and completes from the chunks sent after the other block completes.
      • waiting_block_ends_once_it_cannot_complete (own and shared pre-header buffer): the block still waits after losing as many chunks as it has parity chunks, ends at one more without a fault or a further refusal, and nothing is built for it once room frees.
      • waiting_block_takes_nothing_from_other_blocks: with no free entry the header is refused as before and its chunks displace nothing; a waiting block does not displace another peer's idle block after an unrelated release, and starts at the first chunk after the release that frees room.
      • never_fitting_decoder_finishes_block (per-peer and total cap), waiting_block_chunk_must_fit_its_header, fault_refused_header_stays_handled.
      • Engine-level orders that a sender following the protocol does not produce (the peer layer drops packets older than the last one accepted): capacity_refused_header_can_be_retried (a further header copy starts a waiting block from its kept chunks), header_after_chunks_waits_with_them, starting_waiting_block_is_not_progress.
    • All new tests except never_fitting_decoder_finishes_block and fault_refused_header_stays_handled (guards for paths that end the block as before) fail on photon: announce blocks under the low-order bits of their hash #410's head; the sender-order test fails there and on this PR's first commit even with only its completion checks enabled. Reverting each new rule in a scratch copy (displacing blocks when a waiting block starts, keeping never-fitting headers, ignoring the free-entry condition, counting waiting chunks again, not checking chunks against the kept header, not finishing a block started by a header copy, never ending a block that cannot complete, ending it one chunk early, counting a start as progress) fails at least one test.
    • photon_relay_engine_tests: 78/78 pass.
    • ctest for contrib/photon: all suites pass except photon_scheduler_tests encode_schedule_regression, a p95 timing ratio that fails the same way on the unmodified base on the build machine.
    • Functional tests with a ZMQ-enabled qbitd and this relay: feature_photon_relay.py and feature_integration_photon_topology.py pass.
  • Reviewed locally in three rounds (/code-review xhigh); the first two rounds' findings led to the later commits, and the third found no correctness issues.
  • Ran lint or formatting checks relevant to this change: git diff --check, test/lint/lint-files.py, lint-include-guards.py, lint-includes.py, lint-locale-dependence.py, lint-qbit-public-docs.py, lint-qbit-node-defaults.py (lint-spelling.py skipped: codespell not installed).
  • Not run. Reason:

Target Branch

  • This PR targets main or a maintainer-requested release branch such as 0.1.x. (Targets 1.x.x, for a release after v1.0.1.)

Risk / Review Notes

  • Consensus, script, crypto, wallet, P2P, release, CI, or security-sensitive behavior changed.
  • No consensus, script, crypto, wallet, P2P, release, CI, or security-sensitive behavior changed.

Notes: Relay-side block intake in contrib/photon only. The caps from #381 are unchanged; a waiting block uses only an entry and pre-header buffer space that are free, and its decoder only when it fits without displacing anything.

Docs / Process Impact

Choose exactly one:

  • I updated public docs because this PR changes user-visible behavior, integration guidance, release/process guidance, or expected validation. (contrib/photon/RUNBOOK.md: when a refused block waits for room, how it is counted, and the new test anchors.)
  • No public docs update needed. Reason:

libbitcoinpqc Subtree Checklist (if src/libbitcoinpqc changed)

Not applicable.

🤖 Generated with Claude Code


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

kiwidream and others added 7 commits November 11, 2026 13:33
qbit-photon announced a block under the leading 64 bits of its hash. A
native-lane block's proof of work is on its own hash, so those bits take
only a few values, and most native-lane blocks were taken for a block
already relayed and were not sent.

Announce blocks under the low-order 64 bits of the hash instead (the
last 16 hex digits as qbitd prints it). The packet format and protocol
version are unchanged:
- A received block is accepted under either form: its reconstructed
  header must hash to the announced identifier in one of them.
- Blocks are tracked as seen or settled by the low-order bits of the
  reconstructed or announced hash, so a block that arrives under both
  forms is submitted and relayed once.
- Earlier versions use the identifier only as a key, so they receive
  blocks announced under the new form. After their node accepts such a
  block they may send it on once under the earlier form; this version
  recognises that copy as a block it already has.

Also adjust the pre-submission parent check:
- A block whose parent qbitd has as a header on a branch with more work
  than its best block, at most max_parent_depth blocks above it, is
  submitted, and qbitd connects it after the parent. qbitd storing it
  without connecting it yet does not count against the sender. A parent
  further ahead still means qbitd is catching up, and the block is
  dropped.
- A block whose parent qbitd does not know is dropped without counting
  against the sender.
- When qbitd does not answer the parent check or submitblock, the block
  is kept (one at a time, for up to two minutes) and tried once more
  after qbitd next answers a call or announces a block.

Tests cover the identifier forms, blocks that share their leading hash
bits, one block announced under both forms, a receiver keyed only by the
identifier, a replay of 64 mainnet block headers, and the parent check
and retry cases.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ndling

- A block whose parent qbitd does not know is let off once per peer per
  fault decay interval (60 s), since its parent may still be on its way
  to qbitd; further ones within the interval count against the peer, so
  a peer cannot keep qbitd busy with such blocks. They are counted in a
  new block_parent_unknown field instead of block_parent_rejected.
- A held block's retry no longer counts outcomes that may only reflect
  the delay (too far from the tip, or stored without being connected)
  against the sender; rejections still count.
- While a block is held, other copies of it are ignored through the held
  block itself rather than the 10 s settled set, so an outcome that
  concerns only the retried copy leaves other peers' copies to be tried.
- node_retries counts only retries that call qbitd; a new held_dropped
  field counts held blocks dropped without a retry, which are also
  logged. The held block keeps its parsed header.
- The runbook names v1.0.0 as the version that keys blocks only by the
  announced identifier, and notes that a copy announced under the
  earlier form is recognised after it is decoded.
- Tests: the unknown-parent allowance and an honest race, retry
  outcomes, held-block replacement and drop counts, copies of settled or
  held blocks skipped before decoding, and the main loop retrying a held
  block once qbitd announces a block.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- A held block dropped without a retry (replaced, expired, or its sender
  paused) is settled like any block qbitd did not answer for, so late
  copies of it are not sent to qbitd again for 10 s.
- Copies in progress when a block is held are marked handled, so the
  rest of their chunks do not start new incomplete blocks after the
  hold ends.
- A child of the held block whose parent qbitd does not know yet does
  not count against its sender.
- Tests: settle timing after a slow check now uses an answer that
  settles; a retried unknown-parent block leaves the peer's allowance;
  the main loop also handles the earlier identifier form; and the cases
  above. The runbook lists the new counters where operators are told to
  look.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… copies free

- A block that builds on the held block now has the held block tried
  once more first, so qbitd can connect both, instead of finding the
  new block's parent unknown. If qbitd still does not answer, the
  parent is given up and the new block is held instead. The exemption
  for such a block from the unknown-parent count is no longer needed.
- A held block dropped because its sender was paused is not settled:
  qbitd never saw it, so other peers' copies are still tried.
- Copies ignored while a block is held are marked handled, so their
  remaining chunks do not start incomplete blocks once the hold ends.
- Tests for these cases, for late copies of expired and replaced held
  blocks, and runbook wording.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A block whose parent is the held block is now checked first, as any
other block. Only when qbitd answers that its parent is unknown is the
held block tried again, and the block then checked once more. This
keeps the rule that a held block is retried only after qbitd answers,
so a child arriving while qbitd is still not answering costs no extra
call. A child whose held parent was just tried again does not count
against its sender if its parent is still unknown, and a child is
dropped if its parent's retry paused their common sender.

Also fold the handling of ignored copies into one helper, and update
the tests and runbook.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…answered

- When the held block's retry for a new block gets no answer from qbitd,
  the new block is held instead of being checked again at once.
- A block that builds on the held block no longer replaces it when
  qbitd does not answer for it, since qbitd cannot connect it without
  the parent; it is dropped and its copies are not checked for 10 s.
- A block dropped because its sender was paused during its parent's
  retry is logged.
- Tests for these paths, for a parent whose retry is answered but still
  cannot be used, and for a held parent's retry pausing only its own
  sender; the runbook describes the extra calls such a block can take.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… room

A header refused only because the inbound entry or decoder caps are full
no longer ends the block for the sending peer. Headers that no honest
sender produces (malformed, or above the per-block limits) still end it:
the rest of that block's packets from the peer are ignored for 10 seconds
and the refusal counts against the peer, as before.

After a refusal for lack of room, the chunks that follow are handled like
those of a block whose header has not arrived, within the same per-peer
and total caps, and a later copy of the header starts the block once
there is room.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kiwidream
kiwidream marked this pull request as ready for review November 11, 2026 21:52
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T20:34:44.763456Z 29dfcb2 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

The previous commit left such a block open, but senders send their header
copies only before the chunks, so the block could still not start once
room was free, and its chunks built entries of their own instead.

A header refused only because there is no room for its decoder now is
kept with the block's incomplete entry, when that entry already exists
or when the peer and the relay each have an entry free without dropping
anything. The block's chunks must fit the kept header and are buffered
within the pre-header caps. After an incomplete block has been released,
the next chunk or header copy of the waiting block starts its decoder if
it fits without displacing any other block, so the block can complete
from the chunks that follow.

Otherwise the header is refused as before, and the rest of the block from
that peer is ignored. A decoder that exceeds the per-peer or total cap on
its own, or that cannot be built, also ends the block, without counting
against the peer. A waiting block is counted once in inbound_rejected.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kiwidream kiwidream changed the title photon: keep a block retryable when its header is refused for lack of room photon: keep a block retryable when its header is refused for lack of room (not for v1.0.1) Nov 11, 2026
kiwidream and others added 2 commits November 11, 2026 17:36
A sender sends each chunk once, so a block waiting for room that has
lost more chunks than all its coding groups have parity chunks can no
longer be completed. It now ends then, as if its header had been
refused, instead of holding an entry and its buffered chunks, and later
a decoder, for nothing.

Starting the decoder of a waiting block no longer counts as progress, so
a repeated header copy or chunk does not keep an idle block from being
displaced. The tests now send chunks in the sender's order and also
cover a header that arrives after some of its chunks. The runbook
states how a waiting block is counted in inbound_rejected.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…duce

A sender sends its header copies before its chunks, and the peer layer
drops packets older than the last one it accepted, so a header copy
after chunks, or chunks before their header, reach the relay engine only
from a sender that does not follow the protocol. Say so in the tests
that use those orders to check the engine on its own.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kiwidream kiwidream changed the title photon: keep a block retryable when its header is refused for lack of room (not for v1.0.1) photon: keep a block retryable when its header is refused for lack of room Nov 16, 2026
Conflict in contrib/photon/RUNBOOK.md: #410 landed this branch's first six commits as one commit, and the later commits here edit the same two paragraphs. Resolved to this branch's text. Nothing else on 1.x.x touched contrib/photon, so that directory is unchanged by the merge.

Co-Authored-By: Claude Fable 5.2 <[email protected]>

This branch has not been deployed

No deployments
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