Skip to content

switchrpc: carry the SendOnion outcome in the response - #11546

Open
calvinrzachman wants to merge 3 commits into
lightningnetwork:masterfrom
calvinrzachman:switchrpc-outcome-in-response
Open

calvinrzachman wants to merge 3 commits into
lightningnetwork:masterfrom
calvinrzachman:switchrpc-outcome-in-response

Conversation

@calvinrzachman

Copy link
Copy Markdown
Collaborator

Change Description

After every SendOnion call, the external client managing the payment lifecycle has to know one fund-critical fact. Was the HTLC dispatched, provably not dispatched, or is its state unknown? If the client wrongly concludes that a live HTLC was never dispatched, it may send a replacement attempt and pay twice.

Today that answer comes from the gRPC status code, with an optional SendOnionFailureDetails attached to the error. Some codes, such as FailedPrecondition and InvalidArgument, mean "not dispatched", so every client has to keep a table of which codes mean failed and which mean retry. A code the table does not list, from a newer lnd, a proxy or a gRPC library, falls into whichever branch the client happened to write, and the "not dispatched" branch is the one that can pay twice. Client runtimes and proxies also mint those same codes, so a client cannot be sure lnd wrote them.

This PR moves the outcome into the response body as one of Accepted, Rejected or Ambiguous, replacing SendOnionFailureDetails and its nested messages. The server is the only party that knows whether the HTLC was committed, so it now states the outcome itself instead of leaving each client to infer it from a status code. A client's fund-critical logic becomes four rules. A gRPC error means retry the same attempt_id, Accepted means track, Rejected means fail the attempt and try another route, and anything else, including an outcome added later, means retry. A gRPC status code must never be taken as a signal of whether the HTLC was dispatched. That answer comes only from the outcome in the response body, so there is no table of codes to maintain. This matches how routerrpc.SendPaymentV2 already reports payment outcomes in its response rather than in the status code.

switchrpc is experimental and sits behind the non-default switchrpc build tag. It has not shipped in any release yet, so we have the opportunity to change the contract now, before it is included in one.

Client

Outcome Meaning Client action
Accepted The switch owns the attempt. already_registered is set when it already held it Track with TrackOnion
Rejected The HTLC never left the node. Carries the wire failure, decodable with lnwire.DecodeFailure Fail the attempt, try another route
Ambiguous The server cannot tell whether it holds the attempt Retry the same attempt_id

An unrecognised or unset outcome is treated as Ambiguous, so outcomes added later fail safe. Ambiguous.reason can be used for logging, and clients must not branch on it.

A malformed request still returns InvalidArgument, and the attempt is recorded as failed, so a corrected request needs a new attempt_id. The idempotency check runs before validation on purpose, so a retry of an attempt the switch already holds is always acknowledged, even if a later build tightens a validation rule.

Server

Switch.SendHTLC result Outcome
nil Accepted
ErrDuplicateAdd Accepted{already_registered}
ClearTextError Rejected
any other error Ambiguous

A non-duplicate error from SendHTLC means nothing was queued on a link, and a ClearTextError is only raised before the circuit is committed. This is the same predicate lnd's router uses in handleSwitchErr. Ambiguous is therefore about the record, not the HTLC. It means the attempt store write failed, or nothing was sent but there is no wire reason to give. Either way, retrying the same id converges. Once the attempt is registered, every outcome other than Accepted is recorded as failed before the server answers, so tracking the attempt returns that failure.

When the first hop is given by public key, the server hands the switch a channel to that peer even if none can carry the amount, so the client gets the switch's precise failure. Only a peer with no channel is answered with Rejected{UnknownNextPeer} directly. One known case reads Ambiguous although it is a definite rejection. The dust fee exposure check in SendHTLC returns an unexported sentinel, which is safe and left for a follow-up.

Follow-ups

TrackOnion already reports settle and failure in its response, so its contract is unchanged. The last commit only documents how to read a TrackOnionResponse, where a response with no result set means the attempt may still be live. Tests pinning the switch boundary this contract relies on are offered separately.

Follows: #11282

Steps to Test

Unit tests cover every SendOnion server path, including that each non-Accepted path after registration writes exactly one terminal record and still answers correctly when that write fails. The itests assert the new outcomes. Each commit builds with and without the switchrpc tag, make rpc is clean, and make lint-source reports no issues.

go test -v -tags switchrpc github.com/lightningnetwork/lnd/lnrpc/switchrpc
make itest icase='send onion'
make itest icase='send onion twice'
make itest icase='send onion concurrency'
make itest icase='track onion'

@github-actions github-actions Bot added ci-hold CI is held; remove this label to run CI severity-critical Requires expert review - security/consensus critical labels Nov 10, 2026
@github-actions

Copy link
Copy Markdown

🔴 PR Severity: CRITICAL

Bumped from HIGH | 10 files | 1960 lines changed (710 lines / 5 files non-test, non-generated)

🟠 High (5 files)
  • lnrpc/switchrpc/switch_server.go - RPC/API server implementation under lnrpc/*
  • lnrpc/switchrpc/mock.go - supporting mock for lnrpc/* RPC package
  • lnrpc/switchrpc/switch.pb.go - generated API types under lnrpc/*
  • lnrpc/switchrpc/switch_grpc.pb.go - generated gRPC service code under lnrpc/*
  • lnrpc/switchrpc/switch.swagger.json - generated API spec under lnrpc/*
🟡 Medium (1 file)
  • lnrpc/switchrpc/switch.proto - API definition change (*.proto)
🟢 Low (4 files)
  • docs/release-notes/unreleased/11282.md - release notes
  • itest/lnd_sendonion_test.go - integration test
  • lnrpc/switchrpc/switch_server_test.go - unit test
  • lntest/rpc/switch.go - test harness RPC helper

Analysis

The highest-severity files touched are under lnrpc/switchrpc/* (HIGH — RPC/API definitions and server implementation). Excluding test files and generated *.pb.go files, this PR still changes 710 lines across the hand-written/semi-generated API surface (switch_server.go, mock.go, switch.proto, switch.swagger.json), which exceeds the 500-line bump threshold. Per the bump rule, severity is escalated one level from HIGH to CRITICAL. This looks like a substantial rework of the switchrpc subservice's RPC surface and server logic, warranting expert review of the API contract and server implementation.


To override, add a severity-override-{critical,high,medium,low} label.

@calvinrzachman
calvinrzachman force-pushed the switchrpc-outcome-in-response branch from 7fda23a to 3c25264 Compare November 11, 2026 18:21
Report each SendOnion outcome in the response body as Accepted, Rejected
or Ambiguous, and keep gRPC errors for calls that did not complete. The
server knows whether the HTLC was committed, so it states the outcome
itself. A status code can also be produced by a transport or a proxy,
so it says nothing reliable about dispatch.

Every non-duplicate dispatch error still records the attempt as failed,
so a retry is accepted as already registered and tracking returns the
recorded failure. The itests now assert outcomes instead of status
codes.
marshallDispatchOutcome and the router's handleSwitchErr both split a
SendHTLC error on whether it is a ClearTextError, and both rely on the
switch raising one only before it commits a circuit. Say so where the
server applies the test, so that a change to that guarantee is checked
against both.
SendOnionResponse states how a caller must read an unset or unknown
outcome, but TrackOnionResponse said nothing, leaving a client free to
read an empty result as a failure and retry under a new attempt_id.
Spell out the rule: preimage means settled, failure_details means failed
and out of flight, and anything else means the attempt may still be
live.

Also note that a ForwardingFailure with an empty wire_message still
identifies the failing hop. This is a comment-only change; the wire
contract is unchanged.
@calvinrzachman
calvinrzachman force-pushed the switchrpc-outcome-in-response branch from 3c25264 to 248adcf Compare November 12, 2026 15:43
@calvinrzachman
calvinrzachman marked this pull request as ready for review November 12, 2026 18:22
@calvinrzachman calvinrzachman removed the ci-hold CI is held; remove this label to run CI label Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

severity-critical Requires expert review - security/consensus critical

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant