Skip to content

fix(payments): send the NWC subscription before the request it asks about - #1104

Merged
cameri merged 2 commits into
rewrite/mainfrom
factory/026-nwc-req-before-event
Nov 10, 2026
Merged

cameri merged 2 commits into
rewrite/mainfrom
factory/026-nwc-req-before-event

Conversation

@chappie-daemon

Copy link
Copy Markdown
Collaborator

Closes nothing by keyword: base is rewrite/main, not the repository's default branch. #1099 is closed by hand after the merge.

An NWC (NIP-47) call could lose its answer. The processor sent its EVENT before its REQ, so a wallet that replies the moment it decrypts the request published into a socket with no subscription: kind-23195 replies are ephemeral (the strategy broadcasts and stores nothing), and live dispatch writes only to subscriptions already registered. The reply was not delayed for us — it was gone, and the payment call blocked until it timed out. One hop away, a self-hosted wallet on the same relay is exactly the shape that loses.

The change

-  for frame in [event_frame, request_frame] {
+  for frame in [request_frame, event_frame] {

plus the four-line comment at that site stating the obligation, and the test infrastructure that can see the defect.

No reader change was needed, and that was measured rather than assumed: a dependency-free socket probe shows a reply written while the client is not yet reading survives to the later read; tokio-tungstenite reads on poll; and the existing in-tree test already sends and closes before the client's read loop. The read loop, filter, subscription id, encryption and error arms are byte-identical to base — the production diff is one hunk, one line removed and one added.

The oracle, and why it is the assertion

A plain ordering assertion is not enough, and this PR is careful about the difference. tests/nwc_relay.rs already had a relay() helper that waits for both frames before replying — which is precisely why its four tests were green at base. The new strict_relay double answers inside its EVENT arm and only when a REQ registered a subscription; otherwise it drops the reply, records that it dropped, and closes.

Red at 4adaa42, with the base production file restored over the fix:

running 1 test
test a_reply_published_the_moment_the_wallet_sees_the_event_is_received ... FAILED
panicked at crates/nostream-services/tests/nwc_relay.rs:229:5:
assertion `left == right` failed
  left: ["EVENT"]
 right: ["REQ", "EVENT"]
test result: FAILED. 0 passed; 1 failed; 4 filtered out        (finished in 0.00s, exit 101)

Green at head: 1 passed; the whole target 5 passed; 0 failed.

The trap, measured both ways: a tolerant double (one that waits for the REQ) also fails at base — but only on that same order assertion, and with the assertion deleted it goes green at base. So the assert_eq!(seen.verbs(), ["REQ", "EVENT"]) is the whole oracle; the dropped flag is an invariant inside the double, kept so a future tolerance fails loudly.

What would make this test stop being red at base: making the double tolerant, or dropping the order assertion. Both are recorded in spec.md's Assumptions so a future author does not "simplify" the harness back into the shape that hid the defect.

Validation

cargo fmt --all --check                                               exit 0
cargo clippy --workspace --all-targets --all-features -D warnings      exit 0
cargo clippy --workspace --lib --bins --all-features (strict five)     exit 0
cargo test --workspace --all-features      1245 passed / 0 failed / 4 ignored   (80 result lines)
cargo build --workspace --all-features --release                       exit 0
cargo test -p nostream-services --test nwc_relay                       5 passed / 0 failed

The four ignored are the #[ignore]d Postgres-backed storage targets, unchanged by this branch.

What the review did not look at

Follow-ups the lenses named, not fixed here

  • The double is filter-blind and has no OK/EOSE, so a future fix that waits for an ack would hang rather than fail — worth closing when someone next touches this transport.
  • The wallet-reply construction is duplicated between relay() and strict_relay; a wallet_reply(..) beside Published is the cheap repair when a third double appears.
  • nostream_relay::testing is public and database-free, so a services test could drive the real Relay on loopback instead of a hand model; that is a bigger test-infrastructure change than this job.

@coderabbitai

coderabbitai Bot commented Nov 10, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 74d645b3-8951-4bba-9baa-70c759129916

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@greptile-apps

greptile-apps Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

Greptile CLI A Greptile CLI review exists for this PR, but a new review ran because the PR's changes differ from what the CLI reviewed.

[Medium impact] This focused NWC fix appears safe to merge.

Summary

Sends the NWC subscription before publishing the request, so an immediate wallet reply has a subscription to reach.

  • NWC calls register for replies before sending wallet requests.

Reviews (1) · Last reviewed commit: "fix(payments): send the NWC subscription..." · Reviewed by Greptile

@cameri
cameri merged commit 2c151f2 into rewrite/main Nov 10, 2026
16 checks passed
@cameri
cameri deleted the factory/026-nwc-req-before-event branch November 10, 2026 13:49
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.

2 participants