Repository navigation
bolt12handler+devrpc: dispatch invoice requests and add CreateOffer - #11374
Conversation
🔴 PR Severity: CRITICAL
🔴 Critical (3 files)
🟠 High (9 files)
🟡 Medium (13 files)
🟢 Low (24 files)
AnalysisThis PR adds BOLT12 offer support end-to-end: a new To override, add a |
|
@ViktorT-11: review reminder |
ViktorT-11
left a comment
There was a problem hiding this comment.
Since the PR this PR is based on hasn't been merged yet, I've mainly focused this review on spec compliance/interoperability issues introduced by this PR, as well as potential deadlocks introduces by the new invoice request handler server. Those areas seems unlikely to change due to any of the dependent PR's changes.
Once that initial PR is merged & this has been update, I'll focus my next review on testing the actual cmd & focus on the UX there.
Great work 🔥!!
| // Include the active chain hash so other implementations can | ||
| // validate the offer on non-mainnet networks. The spec defaults | ||
| // to Bitcoin mainnet when offer_chains is absent. | ||
| genesisHash := *s.cfg.ActiveNetParams.GenesisHash | ||
| chains := [][32]byte{genesisHash} |
There was a problem hiding this comment.
I think that potentially always setting this value actually violates the spec, and it should be omitted when the offer is for bitcoin mainnet?
There was a problem hiding this comment.
Right, I think let's follow the spec's should 👍
| case SciddirIntro: | ||
| return nil, fmt.Errorf("sciddir intro is not supported") |
There was a problem hiding this comment.
Just pointing out here a interoperability issue my LLM review found here:
LDK’s DefaultMessageRouter compacts the introduction node to a sciddir specifically for the invoice_request reply path. As we'll drop the entire request due to this error, this will lead to interoperability issues with LDK when LDK's DefaultMessageRouter does that.
Both CLN plugins/offers.c:276-285 & Eclair Postman.scala:154-172 resolves this, so I think we should do the same in lnd.
There was a problem hiding this comment.
Yes, it was kind of meant as something we'd do later, but this PR adds a commit to resolve the scid, though it comes at the cost of a db lookup.
| if s.bolt12Handler != nil { | ||
| s.bolt12Handler.Stop() | ||
| } |
There was a problem hiding this comment.
This should be moved below the s.invoices.Stop() clause below.
Posting an LLM deadlock finding here, which is basically due to that the runInvoiceRequest in dispatcher.go can block on waiting for s.invoices to stop, which isn't possible as the shutdown is triggered after the s.bolt12Handler, and hence needs to wait for s.bolt12Handler to finish before it is stopped.
Posting the full finding below:
Stop()→h.cg.WgWait()(dispatcher.go:49) waits for every in-flightrunInvoiceRequest.runInvoiceRequest→HandleInvoiceRequest→notifyInvoice→NotifyNewBolt12Invoice→notifyClients(invoices/invoiceregistry.go:1573-1576):No context.select { case i.invoiceEvents <- event: case <-i.quit: }
h.cg.Quit()cannot interrupt it. Onlyi.quitcan.i.quitis closed bys.invoices.Stop()— which runs after, at line 2950.- And the registry's own event loop can be blocked meanwhile:
dispatchToSingleClients(invoiceregistry.go:374-380) parks on<-client.backlogDelivered, again breakable only byi.quit.
So: any SubscribeSingleInvoice client with an undelivered backlog at shutdown + one in-flight invoice request past SendInvoiceReply ⇒ WgWait never returns ⇒ lnd never shuts down. There is no watchdog on server.Stop().
The cleanup path gets this right by accident: cleaner.run() is LIFO (server.go:2277-2283) and s.invoices.Stop is registered later (:2608) than the handler's (:2407), so it runs first. Only the explicit Stop() has the order inverted.
There was a problem hiding this comment.
Thanks for the detailed trace and good catch, have updated that.
| // Stop cancels the running requests and waits until they return. | ||
| func (h *Handler) Stop() { | ||
| h.cg.Quit() | ||
| h.cg.WgWait() |
There was a problem hiding this comment.
Just adding here that this h.cg.WgWait can be executed prior to the h.cg.WgAdd(1) call in the Start function, which will then cause a panic. This is due to that server.Stop (which calls this function) can be executed in a race with the server.Start function.
There was a problem hiding this comment.
Nice, I have switched to use fn.GoroutineManager. Its Go refuses to start a goroutine once Stop has begun, so no add can race the wait. Because GoroutineManager needs each request to exit when its context ends, #11340 now passes the context to NotifyNewBolt12Invoice.
969f60b to
395ce73
Compare
| alicePath := onionmessage.BuildBlindedPath(ht.T, aliceHops) | ||
|
|
||
| // Reply path to Bob (single hop: Bob is the final dest). | ||
| bobHops := []*sphinx.HopInfo{ |
There was a problem hiding this comment.
non-blocking: the reply path starts at Bob, so the new self-processing in server.go never runs in a test. An unannounced LDK or CLN payer connected only to Alice sends a reply path that starts at Alice. I ran this test with a two hop reply path, Alice with next_node_id Bob and then Bob, and it passes through the self actor. Worth adding as a case here, or switching this one to that shape.
There was a problem hiding this comment.
Done, thanks for checking. I added a second case where we originate at the source node.
|
|
||
| // SendOnionMessage sends a custom message to the peer with the specified | ||
| // pubkey. | ||
| // pubkey. If the pubkey is our own, the node processes the message itself. |
There was a problem hiding this comment.
nit: this also changes the lnrpc SendOnionMessage RPC. A call with our own key used to fail on the peer lookup and now runs the onion through the self actor, without the ingress limiter. Fine for a local caller, but it is not in the PR text or the proto doc. Either note it there or keep the self path on the replier.
There was a problem hiding this comment.
Yes, I think that the change wasn't too great putting more methods onto the server and changing an RPC. The self actor is gone now. The new commit onionmessage: peel the own hops off a blinded path adds HopPeeler, which removes the hops of this node at the start of a blinded path. The handler uses it on the reply path before it answers, so the reply goes to the next node, which is a peer. Let me know what you think about those changes. I think it's cleaner to encapsulate this in the onionmessage package.
| // node answers. Each answer costs a signature and puts an event into | ||
| // the queue of every invoice subscriber, and any peer can send a | ||
| // request. A payer needs one invoice per payment. | ||
| requestRate = 10 |
There was a problem hiding this comment.
nit: this limiter and the slots are global, not per peer. The default per peer ingress limit of 512 kbps lets one peer push well over 10 small invoice requests a second, so one flooding peer makes the node drop everyone else's requests. The update carries the peer key, so a per peer limiter in front of this one would be cheap. Can be a follow-up.
There was a problem hiding this comment.
Thanks, agreed. I keep this as a follow-up, as you suggest. The global limit protects the node, and a fair split between peers needs a bucket per peer in a bounded cache, but there may be other interactions with the ingress side to consider, which also has rate limits.
395ce73 to
39046ec
Compare
There was a problem hiding this comment.
Rebased on master now that #11340 is merged.
Edit: the branch was pushed, but github didn't update the PR here yet, as there are problems with PRs right now (https://www.githubstatus.com/).
The linter flags a missing blank line after a wrapped function signature and a blank line before a closing brace in invoiceregistry_test.go. Fix both in a separate commit, so that the lint check of this PR passes.
The linter flags a return without a blank line before it in the stream loop of SubscribeChannelEvents. The PR changes this file, so the lint diff covers the line. Fix it in a separate commit before the change.
ToSphinx converts a blinded path to the sphinx form that the onion message layer uses to send a reply. It accepts only a pubkey-form introduction node, because the sciddir form needs a channel-graph lookup.
Add the Delivered field to OnionMessageUpdate. The actor sets it when it delivers the message to this node, and leaves it false when it forwards the message. For a forwarded message, the custom records and the reply path come from the payload of this hop, and BOLT 4 tells a forwarding node to ignore them. The flag lets a subscriber skip these updates.
The final hop of a blinded path carries a path_id that the creator of the path set. The actor decrypts the recipient data of each hop, but it dropped the path_id. A subscriber could not tell whether a message arrived on a path it made. A delivered message now carries the path_id in OnionMessageUpdate. Only the creator of the path can check it, so the actor passes it on and does not interpret it.
A payer can name the introduction node of its reply path by a channel and a direction, the sciddir form, to keep the path short. To send the reply, the node needs the key of that introduction node. GraphIntroResolver finds the key in the channel graph. Direction 0 names node_1, the node with the lesser key, and direction 1 names node_2. resolveIntroNode replaces a sciddir introduction node with that key and returns a path with a pubkey introduction node as it is. The next commit uses it for the reply path of an invoice request.
Add Start and Stop to the Handler. Start subscribes to the incoming onion messages and gives each invoice request to HandleInvoiceRequest, which replies over the reply path of the request. The handler answers only messages that are delivered to this node, because a forwarding node must ignore the payload of its hop. Each request runs on its own goroutine with a fixed number of slots, so that a slow peer on one reply path does not stall the other requests. The dispatch ends with Stop, with the context that Start gets, or when the subscribe server ends the subscription. The subscribe server signals this with Quit and does not close the updates, so the dispatch waits on Quit. The end of the dispatch cancels the running requests.
Create the BOLT 12 handler when the offer store is set, which needs native SQL. Start the handler after the onion message server, so that the handler answers the invoice requests in incoming onion messages.
A blinded path can start at this node. A payer whose only peer is this node names it as the introduction node of its reply path, to hide its own node id. An onion message on such a path has this node as its first hop, and no peer connection leads there. HopPeeler removes the hops of this node at the start of a path. It decrypts the own hop with the node key, finds the next node by its id or by a channel, and derives the next blinding point, or takes the override from the hop data. The result starts at the next node and keeps the remaining hops, so the sender can build the onion for a real peer. The actor and the peeler share the lookup of the next node, which moves into its own function.
A payer whose only peer is this node names this node as the introduction node of its reply path, so that its own node id stays hidden. The replier sends the invoice to the introduction node, and no peer connection leads to the own node, so such a payer got no invoice. Start takes a HopPeeler, and the handler removes the hops of this node at the start of the reply path before it answers. The reply then goes to the next node, which is a peer. A reply path that ends at this node gets no answer. The server passes an onionmessage.HopPeeler with the onion message router and the graph. The SendOnionMessage RPC does not change.
Add the CreateOffer method and its messages to the Dev service, with the POST /v2/dev/createoffer REST route. The Dev service exists only in a dev build, so a release build has no BOLT 12 API while the feature is experimental. The server does not implement the method yet.
CreateOffer checks the request and passes it to the OfferCreator of the config, which is nil when the node runs without BOLT 12 offers. It maps the offer errors to gRPC codes, so that a client can tell a bad request from a failure of the node.
addDeps is close to the funlen limit of 200 lines. A later commit adds the offer creator of the dev RPC to it. Move the code that adds the macaroon permissions to the interceptor chain into addPermissions. The behaviour does not change.
Add CreateOffer to the server. It builds the offer from the node's identity, includes the active chain hash so other implementations can validate it off mainnet, persists the offer, and returns the lno1 string with its content-addressed offer hash. The server gives itself to devrpc as the offer creator only when the offer store is set.
Add createoffer, which calls the CreateOffer RPC of devrpc and prints the encoded offer and its offer hash. Like the other devrpc commands, it exists only in a dev build. The expiry can be a duration from now, such as --expiry 1h, which lncli converts to the absolute expiry of the offer. --absolute_expiry still takes a timestamp, and the two flags cannot be used together.
Cover offer creation and decoding, and the receiver-side negotiation loop: an invoice request arrives over an onion message and the node replies with a signed BOLT 12 invoice carrying a blinded payment path.
bitromortac
left a comment
There was a problem hiding this comment.
Rebased on upstream/master. The only conflict was in server_test.go, where both sides added tests. The PR diff is unchanged. (M2) A new prep commit, invoices: fix whitespace in the registry tests, fixes the two lint issues that master has in invoiceregistry_test.go. (M3)
b0acbd9 to
f658650
Compare
ViktorT-11
left a comment
There was a problem hiding this comment.
This looks really good to me. I just have one feedback/question that may be worth considering. Other than that, this looks ready to go from my end.
| activeChain: activeChain, | ||
| finalCLTVDelta: finalCLTVDelta, | ||
| maxInflight: maxInflightRequests, | ||
| requestLimiter: rate.NewLimiter(requestRate, requestBurst), |
There was a problem hiding this comment.
Should we make this rate limit per peer instead of being global? Else we potentially open up for a griefing vector for single peers, which will then be able to block other payers from creating invoice requests.
There was a problem hiding this comment.
Was discussed in #11374 (comment), I'd defer that as it needs more wholistic approach/analysis.
There was a problem hiding this comment.
Ah sorry my bad! Makes sense.
|
Note: This can be rebased on master to remove the initial commit |
This is part 3 of the BOLT 12 receiver work for #10736. It is stacked on #11340, so review only the commits after
multi: gate BOLT 12 offers behind a protocol flag.#11340 adds a handler that answers an invoice request, but the node does not give it any requests. This PR connects the handler to the onion message layer and adds the API to create offers, so a node can now receive an invoice request for its offer and reply with a signed invoice. All of it stays behind the dev-only
protocol.bolt12-offersflag and needs native SQL.The handler answers only the onion messages that are delivered to this node, because a forwarding node must ignore the payload of its hop. Each request runs on its own goroutine with a fixed number of slots, so a slow reply path does not stall the other requests. A failed request gets no
invoice_errorreply yet.CreateOfferis on thedevrpcsub-server withlncli createoffer, so a release build has no BOLT 12 API while the feature is experimental. The offer includes the active chain hash, so that other implementations can validate it off mainnet.The new itests (
create offerandbolt12 invoice request) skip unless they run with thetest_native_sqltag.