Skip to content

Pin confirmed-tx API reads to a chain view and stamp the tip - #200

Merged
reardencode merged 7 commits into
masterfrom
query/chain-view
Aug 23, 2026
Merged

Pin confirmed-tx API reads to a chain view and stamp the tip#200
reardencode merged 7 commits into
masterfrom
query/chain-view

Conversation

@rearden-grok

@rearden-grok rearden-grok Bot commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

Why

Yuval pointed out that Electrum and Esplora have an A-B-A hole: a
same-height reorg can leave txid:height (and a height-keyed join cache)
unchanged while merkle proofs and confirming block hashes moved.

We researched:

  • mempool/mempool#6584 — stamp the chain tip hash on every API response so sequential fetches detect tip movement, including A-B-A. A start/end tip GET is still racy unless the payload is consistent with that hash.
  • spesmilo/electrum-protocol#2 — 1.7 added chaintip, then reverted it in #17 because ElectrumX is bitcoind middleware and cannot pin. That argument does not apply here.

What

Confirmed-tx reads pin {height, hash, header_fk} of the published tip
(Query::pin_chain_view). Extension leaves a prefix pin live. Disconnect
or same-height replace dies the pin; run_at_chain_view retries (bound 8)
then StoreError::Stalenot a writer lock and not MVCC.

Surface Snapshot token
Esplora HTTP X-Bitcoin-Chain-Tip + X-Bitcoin-Chain-Tip-Height (CORS-exposed). Pin death → 503.
Electrum TCP JSON-RPC extra members chain_tip / chain_tip_height next to result (ping/version omit). server.features.chain_tip.
Electrum status Preimage is txid:height:blockhash: for confirmed rows so same-height replace changes status. Reorg restatuses every watch.
SH join slot Keyed on tip hash, not height.

We stamp tip, not only the last relevant history tx. We do not serve
“as of hash H” after H is disconnected.

Docs: COMPAT.md, docs/concurrency.md (reader pin+retry), docs/crash-recovery.md.

Test plan

  • Query: pin live across extension; dead after same-height replace; SH slot miss; retry helper; stale is not Corrupt.
  • Esplora: header matches /blocks/tip/hash; omitted on empty chain; changes after same-height replace.
  • Electrum: get_history stamps chain_tip; ping omits it; status includes blockhash; reorg notifies a dropped scripthash.

Do not merge unless asked.

rbitcoin-grok added 7 commits August 22, 2026 16:14
Confirmed-tx API reads need a snapshot token that is the tip hash, not
only height. Capture height+hash+header_fk; still_live is true while
confirmed[height] is that fk so extension keeps a prefix pin and
same-height replace does not.
Same-height reorg reused the connection join because the slot keyed on
height. Store the pin hash; is_confirmed_strong_at(view.height) hides
creates above the pin so a prefix view cannot leak the live tip.
A request that straddles a disconnect must not return a torn mix.
run_at_chain_view pins, runs the body, and retries up to 8 times if
confirmed[height] moved. Exhaustion is StoreError::Stale, not Corrupt.
Sequential fetches need a snapshot token that is the tip hash, not
height. Pin before the handler; if that prefix is still published,
stamp hash+height and CORS-expose them. A same-height replace kills
the pin and yields 503 so the client retries an honest body.
Electrum is TCP, not HTTP, so the snapshot token is an extra JSON-RPC
member next to result. get_history / get_balance / listunspent /
transaction.get / get_merkle retry while the pin stays live. ping and
version omit the fields. server.features.chain_tip advertises it.
Legacy status is SHA256 of txid:height, so a same-height replace is
silent. Include the confirming block hash in the preimage. TipNotify
carries reorg_from_height so a reorg restatuses every watch even when
the new block does not touch the script.
Document reader pin+retry (no pause-queries), Esplora tip headers, and
Electrum chain_tip + status blockhash. Credit Yuval for raising A-B-A;
cite mempool/mempool#6584 and spesmilo/electrum-protocol#2 (#17 revert).
@reardencode
reardencode merged commit 6e76f1f into master Aug 23, 2026
12 checks passed
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