Skip to content

3.x.x: offer again a won block whose submitblock never connected, instead of losing it as unknown (#522) - #527

Merged
djh58 merged 2 commits into
3.x.xfrom
djh58/prism-b522-connect-refused-reoffer
Sep 28, 2026
Merged

djh58 merged 2 commits into
3.x.xfrom
djh58/prism-b522-connect-refused-reoffer

Conversation

@djh58

@djh58 djh58 commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

Closes #522 (Refs #521)

Problem

If submitblock fails because qbitd refuses the connection (a node or RPC restart, a network blip), the offer is recorded as an unknown outcome. Under the never-offer-twice rule the block is then never offered again. The restarted node would still accept it on the same tip, so a full block reward is lost. #521 scenario 4 pinned this as a known defect in node_killed_before_submitblock_never_reoffers_the_unsent_block_known_defect.

Fix

A submitblock whose connection was never established did not reach the node. That attempt now returns its own reservation to pending, and the block is offered again after the ordinary pending backoff. Every other failure stays unknown, exactly as today.

Where the boundary is (rpc.rs)

Rpc::call_timeout now returns a typed RpcNotSentError exactly when reqwest's is_connect() holds. Every other transport failure keeps today's untyped error. is_connect() is true when hyper-util's legacy client reports its Connect error kind somewhere in the chain. I checked this in the locked versions (reqwest 0.12.28, hyper-util 0.1.20, hyper 1.11.1):

  • hyper raises Connect only inside connection_for: the connector (DNS, TCP connect, reqwest's 5 s connect timeout, TLS or proxy handshake) or the pool checkout. A request is handed to a connection (try_send_request) only after connection_for has returned one.
  • hyper retries a request on another connection only when its dispatcher hands the request back unwritten (take_message() is Some only if the message was never serialized). So if an attempt after such a retry fails to connect, still no attempt wrote the request.
  • reqwest's default retry policy retries only HTTP/2 and HTTP/3 protocol NACKs, and both features are off here.

Not treated as "not sent", because the request may have reached the node:

  • anything on an established connection: a reset or close after the write, a close right after accept, a lost reply;
  • the request timeout, even when it expires while connecting (reqwest reports it as a timeout, not a connect error);
  • any HTTP status or JSON-RPC error.

The classifier checks the error kind, not the OS error code. The error message keeps the qbit RPC <method> transport failed wording that callers and tests match on, and never includes the URL. No pre-flight probe is needed: the client distinguishes the case reliably, and tests/rpc_not_sent.rs pins that against real sockets, so a dependency upgrade that changed these kinds would fail CI.

State machine (coordinator.rs, ledger/candidates.rs)

offer_candidate checks the submitblock result for RpcNotSentError before it classifies an outcome. When it finds one, Ledger::release_unsent_offer does the following in one transaction:

  • It moves the row offer_reserved → pending, fenced on the live claim token and on a reservation with no recorded call (offered_at_ms IS NULL AND offer_outcome IS NULL).
  • It clears the reservation columns. The existing CHECK requires that for pending, which still means "never offered".
  • It records the attempt in last_error under the new OFFER_NOT_SENT_REASON_PREFIX (offer not sent: … connection refused …; the reservation taken by <instance> was returned to pending).
  • It releases the claim with the ordinary min(60, attempt_count) s pending backoff.

After that:

  • Validity: the next claim runs the pre-offer phase again. An ordinary candidate whose parent was superseded meanwhile is abandoned there, the one proven pre-offer rejection, and a superseded payout revision goes through the 3.x.x: An own block found 9 s after the previous landing was refused unknown-job and never reached the node #478 ceiling again. A 3.x.x: Reference the window from candidates and move the found-block write off the ordering lock #350 leased candidate is offered without that screen, as every leased candidate is. The node decides everything else.
  • Crash window: a crash or lost claim between the failed connect and this commit leaves offer_reserved with no recorded call. Nothing durable proves the call was not made, so recovery keeps treating that row as delivery unknown and never offers it. No recovery path was changed.
  • Metrics and alerts: an unsent attempt records no outcome, no offered_at_ms and no first-offer sample. It never counts as an unknown outcome. While the node refuses, the row is pending and counts toward block_candidate_oldest_unacknowledged_seconds, like any block the node has not accepted, so a long outage still pages. When the offer finally goes through, its first-offer sample covers the whole outage.

No migration, no new metric, and no change to any post-write path.

Known, accepted: a block whose payout revision was superseded (#478) is re-reserved on each attempt, so capture_offer_decisions_total{decision="offered"} counts once per reservation.

Tests

Test Kind What it pins Per-PR CI time
rpc_not_sent (6 cases) ungated unit, real loopback sockets refused → not sent; refused after the pooled connection closed → not sent; reset after the request was read, close right after accept, timeout after the read, HTTP 500, JSON-RPC error → not "not sent" ~0.5 s + one small test binary
candidate_lease_tests::offer_not_sent_tests::a_refused_submitblock_returns_the_row_to_pending_and_lands_the_block_once_when_the_node_is_back gated PG refused → pending with empty offer columns, reason recorded, backoff, no unknown outcome, no sample; a second refused attempt is also not sent; once the node is reachable the block lands with exactly one submitblock ~1.0 s
…::a_reset_after_the_submitblock_was_written_stays_unknown_and_is_never_offered_again gated PG the node accepts, and the reply is replaced by an RST → outcome unknown, never pending, later claims never offer again (one submitblock) ~1.0 s
…::a_crash_between_the_refused_connect_and_the_row_update_recovers_as_delivery_unknown gated PG the release fails after the refusal → row stays offer_reserved; recovery settles it unknown and never offers it, even with the node back ~1.1 s
live_regtest::node_outage_tests::node_killed_before_submitblock_lands_the_block_once_after_restart gated live (flipped #521 case) block admitted → qbitd killed before its submitblock → refused, pending, retried (≥2 attempts, none reaches the node) → qbitd restarts → the block lands; exactly one submitblock per block ~14 s, replacing the old case (its crash-and-orphan sibling takes ~21 s), so no net increase

The live case. The #521 case held the request inside the test's proxy after the server had written it, then killed the node. For the server that is a request that may have reached the node, so it correctly stays unknown after this fix; flipping that assertion as written would break never-offer-twice. The flipped case therefore faults where "before submit" really is before the write:

  1. A database trigger holds the held block's offer reservation (after Stratum admission, before the call).
  2. qbitd is killed and the proxy closes its port, so the server's connect is refused, exactly as from a dead node's port.
  3. The reservation is released and the offer is not sent.
  4. The port stays closed until the restarted qbitd is out of warmup.

The other scenario-4 cases are unchanged and pass.

Mutants (each run on the relevant tests, then reverted):

  • The classifier treats every non-timeout transport failure as "not sent", so a reset after the write counts as unsent. It fails a_reset_after_the_request_was_read_may_have_been_sent, a_connection_closed_right_after_it_was_accepted_may_have_been_sent, and the gated reset test with state pending (the double-offer path).
  • The classifier treats connect-refused as unknown, i.e. today's behaviour. It fails both refused rpc_not_sent cases, the gated refused test and the gated crash test, and the flipped live case with recorded with outcome Some("unknown") (reconciliation) (the lost-block path).

Follow-up (not in this PR)

A retry that reaches qbitd during its warmup gets a JSON-RPC -28 (RPC_IN_WARMUP) reply. Today that reply is still recorded as unknown, which loses the block. It is an answered call, so this PR keeps it unknown as scoped. In Bitcoin Core, from which qbitd derives, the warmup check runs before any method is dispatched. This deserves its own issue.

🤖 Generated with Claude Code

…tead of losing it as unknown (#522)

A submitblock that fails before its connection is established (the node
refuses the connection, a name or route failure, the connect timeout)
provably never reached the node. Rpc::call_timeout now returns a typed
RpcNotSentError for exactly reqwest's is_connect() errors. hyper raises
that kind only while obtaining a connection, before any request is
handed to one. The offer path returns such an attempt's own reservation
to pending in one claim-fenced transaction: the offer columns are
cleared, the attempt is recorded in last_error, and the row is offered
again after the ordinary pending backoff.

Every failure after the connection existed (a reset or close after the
write, a lost reply, the request timeout, any HTTP or JSON-RPC error)
stays unknown and is never offered again. A crash between the refused
connect and the row update leaves offer_reserved, which recovery still
treats as delivery unknown.

Tests: rpc_not_sent (classification against real sockets), three gated
PostgreSQL cases (refused lands once, reset after write stays unknown,
crash window recovers unknown), and #521 scenario 4's kill-before-submit
live case flipped to land the block exactly once after qbitd restarts.

Closes #522
Refs #521

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@djh58
djh58 requested a review from kiwidream September 28, 2026 16:21
@djh58

djh58 commented Sep 28, 2026

Copy link
Copy Markdown
Member Author

@cursor review

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 15595b8. Configure here.

…candidate wording, outage fault helpers

- Classify release_unsent_offer's ledger transaction in the pool-acquire
  census like its sibling offer transitions (the guard failed CI).
- Say what the next claim's pre-offer phase does for a leased candidate:
  it is offered without the supersession screen, as every leased
  candidate is (#350), in the ledger doc comment and the operations doc.
- Move the two held-block fault paths of the scenario-4 live test into
  held_offer and unsent_offer, so outage_steps no longer branches on the
  hold in four places.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@djh58

djh58 commented Sep 28, 2026

Copy link
Copy Markdown
Member Author

Deep-review ledger (head 9a7ddcf, CI green: 20 passed, 1 skipped)

Stakes: high, because this is the block-submission path (reward capture, chain state). Bugbot was summoned.

# Source Finding Disposition
1 code-review (inline) The release_unsent_offer doc and the ops doc said the next claim's pre-offer screen abandons a block whose parent was superseded. A #350 leased candidate skips that screen by design and is offered again directly. fixed in 9a7ddcf (wording in the doc comment, docs/prism-ledger-ops.md and the PR body)
2 thermo (inline) outage_steps branched on hold == Reservation in four places fixed in 9a7ddcf (held_offer / unsent_offer / inject helpers; all scenario-4 cases re-run green)
3 CI (Python shard 1) pool-acquire census: release_unsent_offer#1 self.begin() unclassified fixed in 9a7ddcf (classified ledger, like record_offer and reconcile_candidate)
— Cursor Bugbot reviewed 15595b8: no issues —

Adversarial pass: no refutation found. The three riskiest areas I attacked, and why they held:

  1. Whether is_connect() can be true after request bytes were written. hyper-util raises Connect only inside connection_for (connector or pool checkout). The request is handed over only afterwards, and hyper retries only requests its dispatcher returned unwritten. An HTTP/1 handshake error is SendRequest, not Connect, so it stays unknown.
  2. Racing the release against a lost claim, the heartbeat or a crash. The release is one transaction fenced on the live token and on a reservation with no recorded call. Any failure leaves offer_reserved, which recovers as unknown and is never offered. This is pinned by the gated crash-window test.
  3. Tests that still pass on subtly wrong code. Both classifier mutants (reset-after-write treated as not sent, and connect-refused treated as unknown) fail the unit tests, the gated tests and the flipped live case. See the PR body.

Review-lane gaps: the review battery ran inline in the authoring session. The worker contract forbade nested workers and non-Claude providers, so there was no independent cross-agent adversarial lane (Fable worker or Codex).

Follow-up (the coordinator is filing it): a retry that reaches qbitd during its warmup gets a JSON-RPC -28 reply, which is still recorded unknown.

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