Repository navigation
switchrpc: carry the SendOnion outcome in the response - #11546
calvinrzachman wants to merge 3 commits into
Conversation
🔴 PR Severity: CRITICAL
🟠 High (5 files)
🟡 Medium (1 file)
🟢 Low (4 files)
AnalysisThe highest-severity files touched are under To override, add a |
7fda23a to
3c25264
Compare
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.
3c25264 to
248adcf
Compare
Change Description
After every
SendOnioncall, 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
SendOnionFailureDetailsattached to the error. Some codes, such asFailedPreconditionandInvalidArgument, 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,RejectedorAmbiguous, replacingSendOnionFailureDetailsand 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 sameattempt_id,Acceptedmeans track,Rejectedmeans 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 howrouterrpc.SendPaymentV2already reports payment outcomes in its response rather than in the status code.switchrpc is experimental and sits behind the non-default
switchrpcbuild 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
Acceptedalready_registeredis set when it already held itTrackOnionRejectedlnwire.DecodeFailureAmbiguousattempt_idAn unrecognised or unset outcome is treated as
Ambiguous, so outcomes added later fail safe.Ambiguous.reasoncan 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 newattempt_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.SendHTLCresultAcceptedErrDuplicateAddAccepted{already_registered}ClearTextErrorRejectedAmbiguousA non-duplicate error from
SendHTLCmeans nothing was queued on a link, and aClearTextErroris only raised before the circuit is committed. This is the same predicate lnd's router uses inhandleSwitchErr.Ambiguousis 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 thanAcceptedis 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 readsAmbiguousalthough it is a definite rejection. The dust fee exposure check inSendHTLCreturns an unexported sentinel, which is safe and left for a follow-up.Follow-ups
TrackOnionalready reports settle and failure in its response, so its contract is unchanged. The last commit only documents how to read aTrackOnionResponse, 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
SendOnionserver path, including that each non-Acceptedpath 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 theswitchrpctag,make rpcis clean, andmake lint-sourcereports no issues.