Skip to content

friglet: remove duplicated Electrum request handling (SNB-665) - #46

Closed
setavenger wants to merge 1 commit into
integrationfrom
snb-665-electrum-dedup
Closed

setavenger wants to merge 1 commit into
integrationfrom
snb-665-electrum-dedup

Conversation

@setavenger

@setavenger setavenger commented Oct 3, 2026 •

Copy link
Copy Markdown
Owner

Part of the friglet cleanup (SNB-661). Implements SNB-665. Net −68 lines, no behaviour change.

What was merged and why it was duplicated

  • Notification lines: the JSON-RPC notification was written out four times (scripthash push, SP push, tip push, subscribe handler) → notification(). The SP subscription object + history notification was built twice (push task and blockchain.silentpayments.subscribe) → sp_subscription().
  • Param extraction: the same "missing / wrong type → -32602" block four times (block.header, scripthash.subscribe, scripthash.get_history, transaction.get) → invalid_param(), same messages.
  • Header sidecar write: after a backfill and after a reorg → save_header_sidecar(). The two failure messages are kept; the reorg path's "write task failed" log line now also carries the path field.
  • Header hash checked three times: Peer::headers_by_hash(&[hash]) was only ever called with one hash. It is now header_by_hash(hash) and returns only a header whose hash is hash, so the re-checks in ChainSource::header and backfill_header (both unreachable) are gone.
  • Relay: connect-then-settle with a 2 s literal (×3, plus the literal in ChainSource::fee_filter) → connect_settled() and p2p::SETTLE_WAIT; wire-tx re-encode ×2 → Candidate::wire_tx(); announce-then-wait ×2 → Signals::announce(). In broadcast() the settle moved from the top of drive() to just before it: a settle error still returns Err (it happened before any hand-over, where Closed was never a rejection).
  • index_unconfirmed_tx: two match arms that both did nothing → one any() check.
  • Two tests that built JsonRpcRequest by hand now use call().

Why behaviour is unchanged

Scripted Electrum session (verification only, not committed): 52 requests through handle_request plus 8 batches of push lines, written raw to a file, on integration a9ff4d6 and on this branch. It covers server.*, fees, unknown method, headers.subscribe (tip with stored header, no tip, header unavailable), block.header (stored, P2P backfill by checkpoint with a FakePeer, unknown block, missing/mistyped/negative/float param), scripthash.subscribe / get_history (known, unknown, missing, mistyped), transaction.get, silentpayments.subscribe / unsubscribe at progress 0.37 and 1.0, broadcast (missing, mistyped, bad hex, short tx, no inputs, no peer, already confirmed, accepted, resent, fee-bump replacement, peer disconnect, missing inputs) and the push lines after each step.

sha256 before: e429a7df9a07d7713cbd40b8178a1941fa3aee671b20c677ec0b32397c42ec7a
sha256 after:  e429a7df9a07d7713cbd40b8178a1941fa3aee671b20c677ec0b32397c42ec7a   (cmp: identical)

(Push lines from a HashMap are sorted within each batch before comparing; two runs on the base were identical too.)

Verification

  • cargo check --workspace --all-targets --all-features: 0 warnings
  • cargo clippy --no-deps -p friglet -p friglet-ipc -p friglet-tray --all-targets -- -D warnings: clean
  • cargo fmt -p friglet -p friglet-ipc -p friglet-tray -- --check: clean
  • cargo test --workspace: 254 passed, 1 ignored, 0 failed (same as base; 58.8 s)

Left out on purpose

  • jsonrpc request field (kept, with its allow(dead_code)): removing it changes how a malformed request is answered. Today {"jsonrpc":2,...} fails to parse (-32700 Parse error); without the field it would be answered normally.
  • Input-scripthash fallback in index_unconfirmed_tx (kept): I could not prove it unreachable. It runs when the history holds a txid whose raw tx is not in idx.txs. One candidate path: a resend after an inconclusive wait finds the tx in the peer's mempool after the scanner already indexed it as confirmed (the oracle can be ahead of the peer). accept_pending then adds height-0 entries that are never promoted. A later reorg drops the raw txs of the disconnected blocks but keeps height-0 entries.

The Electrum server built the same things in several places. One copy
of each now:

- notification(): the JSON-RPC notification line (was written out four
  times); sp_subscription(): the SP subscription object and its
  notification, shared by the push task and the subscribe handler.
- invalid_param(): the -32602 answer for a missing or mistyped first
  parameter (block.header, scripthash.subscribe/get_history,
  transaction.get); the messages are unchanged.
- save_header_sidecar(): the header-sidecar write after a backfill and
  after a reorg.
- Peer::header_by_hash() takes one hash (its only caller passed a
  one-element slice) and returns only a header with that hash, so the
  two re-checks of the same hash in ChainSource::header and
  backfill_header go.
- relay: connect_settled() (connect + settle, the 2 s literal is now
  p2p::SETTLE_WAIT, also used by ChainSource::fee_filter),
  Candidate::wire_tx() and Signals::announce() replace two or three
  copies each.
- index_unconfirmed_tx: two match arms that both did nothing are one
  condition.
- Two tests build their request with call() like the others.

The bytes on the wire are unchanged: a scripted session of 52 requests
and 8 push batches gives byte-identical output before and after.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@setavenger

Copy link
Copy Markdown
Owner Author

Merged into integration through #70 on 2026-10-11 (the user live-tested the combined stack first). GitHub's stacked-PR merge rebased the stack, so this PR's commits landed with new IDs and GitHub can't mark it merged itself. The code is identical: integration now has the same tree as the tested branch friglet-deslop-live-test (08bf11d), which contains this PR's head. Closing as merged via #70.

@setavenger setavenger closed this Oct 11, 2026
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