Skip to content

Commit f8fc2b6

Browse files
authored
Merge pull request #10271 from f321x/fix_save_payment_info
lightning: fix self payments (e.g. rebalance)
2 parents 07b5963 + df612fa commit f8fc2b6

File tree

12 files changed

+134
-91
lines changed

12 files changed

+134
-91
lines changed

electrum/commands.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
)
7171
from .address_synchronizer import TX_HEIGHT_LOCAL
7272
from .mnemonic import Mnemonic
73-
from .lnutil import (channel_id_from_funding_tx, LnFeatures, SENT, MIN_FINAL_CLTV_DELTA_ACCEPTED,
73+
from .lnutil import (channel_id_from_funding_tx, LnFeatures, SENT, RECEIVED, MIN_FINAL_CLTV_DELTA_ACCEPTED,
7474
PaymentFeeBudget, NBLOCK_CLTV_DELTA_TOO_FAR_INTO_FUTURE)
7575
from .plugin import run_hook, DeviceMgr, Plugins
7676
from .version import ELECTRUM_VERSION
@@ -1398,7 +1398,7 @@ async def add_hold_invoice(
13981398
arg:int:min_final_cltv_expiry_delta:Optional min final cltv expiry delta (default: 294 blocks)
13991399
"""
14001400
assert len(payment_hash) == 64, f"Invalid payment hash length: {len(payment_hash)} != 64"
1401-
assert payment_hash not in wallet.lnworker.payment_info, "Payment hash already used!"
1401+
assert not wallet.lnworker.get_payment_info(bfh(payment_hash), direction=RECEIVED), "Payment hash already used!"
14021402
assert payment_hash not in wallet.lnworker.dont_expire_htlcs, "Payment hash already used!"
14031403
assert wallet.lnworker.get_preimage(bfh(payment_hash)) is None, "Already got a preimage for this payment hash!"
14041404
assert MIN_FINAL_CLTV_DELTA_ACCEPTED < min_final_cltv_expiry_delta < 576, "Use a sane min_final_cltv_expiry_delta value"
@@ -1413,7 +1413,7 @@ async def add_hold_invoice(
14131413
min_final_cltv_delta=min_final_cltv_expiry_delta,
14141414
exp_delay=expiry,
14151415
)
1416-
info = wallet.lnworker.get_payment_info(bfh(payment_hash))
1416+
info = wallet.lnworker.get_payment_info(bfh(payment_hash), direction=RECEIVED)
14171417
lnaddr, invoice = wallet.lnworker.get_bolt11_invoice(
14181418
payment_info=info,
14191419
message=memo,
@@ -1439,12 +1439,11 @@ async def settle_hold_invoice(self, preimage: str, wallet: Abstract_Wallet = Non
14391439
assert len(preimage) == 64, f"Invalid payment_hash length: {len(preimage)} != 64"
14401440
payment_hash: str = crypto.sha256(bfh(preimage)).hex()
14411441
assert payment_hash not in wallet.lnworker._preimages, f"Invoice {payment_hash=} already settled"
1442-
assert payment_hash in wallet.lnworker.payment_info, \
1443-
f"Couldn't find lightning invoice for {payment_hash=}"
1442+
info = wallet.lnworker.get_payment_info(bfh(payment_hash), direction=RECEIVED)
1443+
assert info, f"Couldn't find lightning invoice for {payment_hash=}"
14441444
assert payment_hash in wallet.lnworker.dont_expire_htlcs, f"Invoice {payment_hash=} not a hold invoice?"
14451445
assert wallet.lnworker.is_complete_mpp(bfh(payment_hash)), \
14461446
f"MPP incomplete, cannot settle hold invoice {payment_hash} yet"
1447-
info: Optional['PaymentInfo'] = wallet.lnworker.get_payment_info(bfh(payment_hash))
14481447
assert (wallet.lnworker.get_payment_mpp_amount_msat(bfh(payment_hash)) or 0) >= (info.amount_msat or 0)
14491448
wallet.lnworker.save_preimage(bfh(payment_hash), bfh(preimage))
14501449
util.trigger_callback('wallet_updated', wallet)
@@ -1460,13 +1459,13 @@ async def cancel_hold_invoice(self, payment_hash: str, wallet: Abstract_Wallet =
14601459
14611460
arg:str:payment_hash:Payment hash in hex of the hold invoice
14621461
"""
1463-
assert payment_hash in wallet.lnworker.payment_info, \
1462+
assert wallet.lnworker.get_payment_info(bfh(payment_hash), direction=RECEIVED), \
14641463
f"Couldn't find lightning invoice for payment hash {payment_hash}"
14651464
assert payment_hash not in wallet.lnworker._preimages, "Cannot cancel anymore, preimage already given."
14661465
assert payment_hash in wallet.lnworker.dont_expire_htlcs, f"{payment_hash=} not a hold invoice?"
14671466
# set to PR_UNPAID so it can get deleted
1468-
wallet.lnworker.set_payment_status(bfh(payment_hash), PR_UNPAID)
1469-
wallet.lnworker.delete_payment_info(payment_hash)
1467+
wallet.lnworker.set_payment_status(bfh(payment_hash), PR_UNPAID, direction=RECEIVED)
1468+
wallet.lnworker.delete_payment_info(payment_hash, direction=RECEIVED)
14701469
wallet.set_label(payment_hash, None)
14711470
del wallet.lnworker.dont_expire_htlcs[payment_hash]
14721471
while wallet.lnworker.is_complete_mpp(bfh(payment_hash)):
@@ -1492,7 +1491,7 @@ async def check_hold_invoice(self, payment_hash: str, wallet: Abstract_Wallet =
14921491
arg:str:payment_hash:Payment hash in hex of the hold invoice
14931492
"""
14941493
assert len(payment_hash) == 64, f"Invalid payment_hash length: {len(payment_hash)} != 64"
1495-
info: Optional['PaymentInfo'] = wallet.lnworker.get_payment_info(bfh(payment_hash))
1494+
info: Optional['PaymentInfo'] = wallet.lnworker.get_payment_info(bfh(payment_hash), direction=RECEIVED)
14961495
is_complete_mpp: bool = wallet.lnworker.is_complete_mpp(bfh(payment_hash))
14971496
amount_sat = (wallet.lnworker.get_payment_mpp_amount_msat(bfh(payment_hash)) or 0) // 1000
14981497
result = {
@@ -1514,7 +1513,7 @@ async def check_hold_invoice(self, payment_hash: str, wallet: Abstract_Wallet =
15141513
elif wallet.lnworker.get_preimage_hex(payment_hash) is not None:
15151514
result["status"] = "settled"
15161515
plist = wallet.lnworker.get_payments(status='settled')[bfh(payment_hash)]
1517-
_dir, amount_msat, _fee, _ts = wallet.lnworker.get_payment_value(info, plist)
1516+
_dir, amount_msat, _fee, _ts = wallet.lnworker.get_payment_value(None, plist)
15181517
result["received_amount_sat"] = amount_msat // 1000
15191518
result['preimage'] = wallet.lnworker.get_preimage_hex(payment_hash)
15201519
if info is not None:

electrum/gui/qml/qerequestdetails.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from electrum.invoices import (
99
PR_UNPAID, PR_EXPIRED, PR_UNKNOWN, PR_PAID, PR_INFLIGHT, PR_FAILED, PR_ROUTING, PR_UNCONFIRMED, LN_EXPIRY_NEVER
1010
)
11-
from electrum.lnutil import MIN_FUNDING_SAT
11+
from electrum.lnutil import MIN_FUNDING_SAT, RECEIVED
1212
from electrum.lnurl import LNURL3Data, request_lnurl_withdraw_callback, LNURLError
1313
from electrum.payment_identifier import PaymentIdentifier, PaymentIdentifierType
1414
from electrum.i18n import _
@@ -237,7 +237,7 @@ def lnurlRequestWithdrawal(self, amount_sat: int) -> None:
237237
address=None,
238238
)
239239
req = self._wallet.wallet.get_request(key)
240-
info = self._wallet.wallet.lnworker.get_payment_info(req.payment_hash)
240+
info = self._wallet.wallet.lnworker.get_payment_info(req.payment_hash, direction=RECEIVED)
241241
_lnaddr, b11_invoice = self._wallet.wallet.lnworker.get_bolt11_invoice(
242242
payment_info=info,
243243
message=req.get_message(),

electrum/gui/qt/send_tab.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
NotEnoughFunds, NoDynamicFeeEstimates, parse_max_spend, UserCancelled, ChoiceItem,
1919
UserFacingException,
2020
)
21+
from electrum.lnutil import RECEIVED
2122
from electrum.invoices import PR_PAID, Invoice, PR_BROADCASTING, PR_BROADCAST
2223
from electrum.transaction import Transaction, PartialTxInput, PartialTxOutput
2324
from electrum.network import TxBroadcastError, BestEffortRequestFailed
@@ -979,7 +980,7 @@ def request_lnurl_withdraw_dialog(self, lnurl_data: LNURL3Data):
979980
address=None,
980981
)
981982
req = self.wallet.get_request(key)
982-
info = self.wallet.lnworker.get_payment_info(req.payment_hash)
983+
info = self.wallet.lnworker.get_payment_info(req.payment_hash, direction=RECEIVED)
983984
_lnaddr, b11_invoice = self.wallet.lnworker.get_bolt11_invoice(
984985
payment_info=info,
985986
message=req.get_message(),

electrum/lnpeer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2221,7 +2221,7 @@ def _check_unfulfilled_htlc(
22212221
outer_onion_payment_secret=payment_secret_from_onion,
22222222
)
22232223

2224-
info = self.lnworker.get_payment_info(payment_hash)
2224+
info = self.lnworker.get_payment_info(payment_hash, direction=RECEIVED)
22252225
if info is None:
22262226
_log_fail_reason(f"no payment_info found for RHASH {payment_hash.hex()}")
22272227
raise exc_incorrect_or_unknown_pd
@@ -3115,7 +3115,7 @@ def _check_unfulfilled_htlc_set(
31153115
return None, None, fwd_cb
31163116

31173117
# -- from here on it's assumed this set is a payment for us (not something to forward) --
3118-
payment_info = self.lnworker.get_payment_info(payment_hash)
3118+
payment_info = self.lnworker.get_payment_info(payment_hash, direction=RECEIVED)
31193119
if payment_info is None:
31203120
_log_fail_reason(f"payment info has been deleted")
31213121
return OnionFailureCode.INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS, None, None

electrum/lnutil.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,7 @@ def __neg__(self) -> 'HTLCOwner':
10891089
return HTLCOwner(super().__neg__())
10901090

10911091

1092+
# part of lightning_payments db keys
10921093
class Direction(IntEnum):
10931094
SENT = -1 # in the context of HTLCs: "offered" HTLCs
10941095
RECEIVED = 1 # in the context of HTLCs: "received" HTLCs

0 commit comments

Comments
 (0)