Skip to content

Commit 8011ae0

Browse files
authored
Merge pull request #9586 from f321x/jit-block-preimage
Add mechanism to block htlcs from settling back
2 parents 4641c91 + 109ad2b commit 8011ae0

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

electrum/lnpeer.py

+4
Original file line numberDiff line numberDiff line change
@@ -2146,6 +2146,7 @@ def log_fail_reason(reason: str):
21462146
except BaseException as e:
21472147
log_fail_reason(f"error sending message to next_peer={next_chan.node_id.hex()}")
21482148
raise OnionRoutingFailure(code=OnionFailureCode.TEMPORARY_CHANNEL_FAILURE, data=outgoing_chan_upd_message)
2149+
21492150
htlc_key = serialize_htlc_key(next_chan.get_scid_or_local_alias(), next_htlc.htlc_id)
21502151
return htlc_key
21512152

@@ -2500,6 +2501,9 @@ def log_fail_reason(reason: str):
25002501
else:
25012502
return None, None
25022503

2504+
if payment_hash.hex() in self.lnworker.dont_settle_htlcs:
2505+
return None, None
2506+
25032507
chan.opening_fee = None
25042508
self.logger.info(f"maybe_fulfill_htlc. will FULFILL HTLC: chan {chan.short_channel_id}. htlc={str(htlc)}")
25052509
return preimage, None

electrum/lnworker.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,7 @@ def __init__(self, wallet: 'Abstract_Wallet', xprv):
882882
self.active_forwardings = self.db.get_dict('active_forwardings') # type: Dict[str, List[str]] # Dict: payment_key -> list of htlc_keys
883883
self.forwarding_failures = self.db.get_dict('forwarding_failures') # type: Dict[str, Tuple[str, str]] # Dict: payment_key -> (error_bytes, error_message)
884884
self.downstream_to_upstream_htlc = {} # type: Dict[str, str] # Dict: htlc_key -> htlc_key (not persisted)
885+
self.dont_settle_htlcs = self.db.get_dict('dont_settle_htlcs') # type: Dict[str, None] # payment_hashes of htlcs that we should not settle back yet even if we have the preimage
885886

886887
# payment_hash -> callback:
887888
self.hold_invoice_callbacks = {} # type: Dict[bytes, Callable[[bytes], Awaitable[None]]]
@@ -1246,6 +1247,9 @@ async def open_channel_just_in_time(
12461247
) -> str:
12471248
# if an exception is raised during negotiation, we raise an OnionRoutingFailure.
12481249
# this will cancel the incoming HTLC
1250+
1251+
# prevent settling the htlc until the channel opening was successfull so we can fail it if needed
1252+
self.dont_settle_htlcs[payment_hash.hex()] = None
12491253
try:
12501254
funding_sat = 2 * (next_amount_msat_htlc // 1000) # try to fully spend htlcs
12511255
password = self.wallet.get_unlocked_password() if self.wallet.has_password() else None
@@ -1280,13 +1284,17 @@ async def wait_for_preimage():
12801284
while self.get_preimage(payment_hash) is None:
12811285
await asyncio.sleep(1)
12821286
await util.wait_for2(wait_for_preimage(), LN_P2P_NETWORK_TIMEOUT)
1287+
1288+
# We have been paid and can broadcast
1289+
# todo: if broadcasting raise an exception, we should try to rebroadcast
1290+
await self.network.broadcast_transaction(funding_tx)
12831291
except OnionRoutingFailure:
12841292
raise
12851293
except Exception:
12861294
raise OnionRoutingFailure(code=OnionFailureCode.TEMPORARY_NODE_FAILURE, data=b'')
1287-
# We have been paid and can broadcast
1288-
# todo: if broadcasting raise an exception, we should try to rebroadcast
1289-
await self.network.broadcast_transaction(funding_tx)
1295+
finally:
1296+
del self.dont_settle_htlcs[payment_hash.hex()]
1297+
12901298
htlc_key = serialize_htlc_key(next_chan.get_scid_or_local_alias(), htlc.htlc_id)
12911299
return htlc_key
12921300

tests/test_lnpeer.py

+1
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ def __init__(self, *, local_keypair: Keypair, chans: Iterable['Channel'], tx_que
198198
self.preimages = {}
199199
self.stopping_soon = False
200200
self.downstream_to_upstream_htlc = {}
201+
self.dont_settle_htlcs = {}
201202
self.hold_invoice_callbacks = {}
202203
self.payment_bundles = [] # lists of hashes. todo:persist
203204
self.config.INITIAL_TRAMPOLINE_FEE_LEVEL = 0

0 commit comments

Comments
 (0)