Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NUT-15] LND: Try Multiple Routes #692

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions cashu/lightning/lnd_grpc/lnd_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,21 +245,26 @@ async def pay_partial_invoice(
)
"""
# modify the mpp_record in the last hop
route_nr = 0
r.routes[route_nr].hops[-1].mpp_record.payment_addr = bytes.fromhex( # type: ignore
payer_addr
)
r.routes[route_nr].hops[ # type: ignore
-1
].mpp_record.total_amt_msat = total_amount_msat

# Send to route request
r = await router_stub.SendToRouteV2(
routerrpc.SendToRouteRequest(
payment_hash=bytes.fromhex(invoice.payment_hash),
route=r.routes[route_nr], # type: ignore
for route_nr in range(len(r.routes)):
r.routes[route_nr].hops[-1].mpp_record.payment_addr = bytes.fromhex( # type: ignore
payer_addr
)
)
r.routes[route_nr].hops[ # type: ignore
-1
].mpp_record.total_amt_msat = total_amount_msat

# Send to route request
r = await router_stub.SendToRouteV2(
routerrpc.SendToRouteRequest(
payment_hash=bytes.fromhex(invoice.payment_hash),
route=r.routes[route_nr], # type: ignore
)
)
if r.status == lnrpc.HTLCAttempt.HTLCStatus.FAILED:
if r.failure.code == lnrpc.Failure.FailureCode.TEMPORARY_CHANNEL_FAILURE:
# Try a different route
continue
break
except AioRpcError as e:
logger.error(f"QueryRoute or SendToRouteV2 failed: {e}")
return PaymentResponse(
Expand Down
37 changes: 23 additions & 14 deletions cashu/lightning/lndrest.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,22 +259,31 @@ async def pay_partial_invoice(
}

# add the mpp_record to the last hop
rout_nr = 0
data["routes"][rout_nr]["hops"][-1].update(mpp_record)
r = None # type: ignore
for route_nr in range(len(data["routes"])):
logger.debug(f"Trying to pay partial amount with route number {route_nr+1}")
data["routes"][route_nr]["hops"][-1].update(mpp_record)

# send to route
r = await self.client.post(
url="/v2/router/route/send",
json={
"payment_hash": base64.b64encode(
bytes.fromhex(invoice.payment_hash)
).decode(),
"route": data["routes"][route_nr],
},
timeout=None,
)

# send to route
r = await self.client.post(
url="/v2/router/route/send",
json={
"payment_hash": base64.b64encode(
bytes.fromhex(invoice.payment_hash)
).decode(),
"route": data["routes"][rout_nr],
},
timeout=None,
)
data = r.json()
failure = data.get("failure")
if failure:
if failure["code"] == 15:
# Try with a different route
continue
break

data = r.json()
if r.is_error or data.get("message"):
error_message = data.get("message") or r.text
return PaymentResponse(
Expand Down
Loading