Skip to content

pyln-proto: keep every route hint when decoding an invoice - #9780

Open
takinanton wants to merge 1 commit into
ElementsProject:masterfrom
takinanton:pyln-proto-route-hints
Open

takinanton wants to merge 1 commit into
ElementsProject:masterfrom
takinanton:pyln-proto-route-hints

Conversation

@takinanton

@takinanton takinanton commented Nov 28, 2026 •

Copy link
Copy Markdown

Invoice.decode() in pyln-proto assigned route_hints again on every r field so a BOLT11 invoice with more than one came back with only the last one. Decode now keeps every r field as its own RouteHintSet so route_hints is a list with one set per r field. encode() writes one r field per list item and still takes a bare RouteHintSet like before.

This changes the type a reader sees. inv.route_hints.route_hints becomes inv.route_hints[0].route_hints.

Tests cover two r fields in the encoded string and one 2-hop field against two 1-hop fields. Ran the pyln-proto tests only which gives 47 passed and 1 skipped. I did not build lightningd or run the integration tests.

Checklist

Before submitting the PR, ensure the following tasks are completed. If an item is not applicable to your PR, please mark it as checked:

  • The changelog has been updated in the relevant commit(s) according to the guidelines.
  • Tests have been added or modified to reflect the changes.
  • Documentation has been reviewed and updated as needed.
  • Related issues have been listed and linked, including any that this PR closes.
  • Important All PRs must consider how to reverse any persistent changes for tools/lightning-downgrade

🤖 Generated with Claude Code

if inv.route_hints is None:
inv.route_hints = hints
else:
inv.route_hints.route_hints.extend(hints.route_hints)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flattening multiple r fields into a single RouteHintSet loses the BOLT #11 distinction between hops along one route (entries inside a single r field) and alternative routes (separate r fields). With this change, a single 2-hop r field (like the invoice in test_decode) and two 1-hop r fields decode to the exact same flat list, and multiple multi-hop r fields lose their route boundaries altogether.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, merging them was wrong. In a049c26 inv.route_hints is a list with one RouteHintSet per r field and encode() writes one r field per list item. That breaks a reader of inv.route_hints.route_hints which now needs inv.route_hints[0].route_hints. A bare RouteHintSet still encodes like before with one r field per hint.

inv.route_hints.add(rh)

privkey = 'c28a9f80738f770d527803a566cf6fc3edf6cea586c4fc4a5223a5ad797e1ac3'
decoded = Invoice.decode(inv.encode(privkey))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should assert that .encode() emits two r tags first.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added. The test now parses the encoded string and checks it has two r fields before it decodes. There's also a new test that one 2-hop r field and two 1-hop r fields decode differently and both encode back to the same string.

@daywalker90

Copy link
Copy Markdown
Collaborator

Does this fix an issue for you? Because i don't think we even use this in-repo?

@takinanton

Copy link
Copy Markdown
Author

No production issue. I decoded an invoice with two r fields with pyln-proto and only got the last one back. I grepped the repo and you're right, Invoice.route_hints is only touched in pyln-proto itself and its test so this is just the published package API. The rework changes the type of that attribute so if you'd rather not carry it I'm fine with closing this.

@daywalker90

Copy link
Copy Markdown
Collaborator

lgtm, but please squash the commits first

Invoice.decode() replaced route_hints on each `r` field so an invoice
with several of them came back with only the last one. encode() writes
one `r` field per hint so even its own output did not round trip.

Entries inside one `r` field are hops along one route and separate `r`
fields are alternative routes. Invoice.route_hints is now a list with
one RouteHintSet per `r` field and encode() writes one `r` field per
list item. A bare RouteHintSet is still encoded like before, one `r`
field per hint.

Changelog-Changed: pyln-proto: `Invoice.route_hints` is now a list with one `RouteHintSet` per `r` field instead of a single `RouteHintSet`, so `Invoice.decode()` no longer drops all but the last `r` field.
@takinanton
takinanton force-pushed the pyln-proto-route-hints branch from a049c26 to 9c20863 Compare December 2, 2026 22:53
@takinanton

Copy link
Copy Markdown
Author

Squashed into one commit 9c20863. Same tree as before.

This branch has not been deployed

No deployments
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