@@ -882,6 +882,7 @@ def __init__(self, wallet: 'Abstract_Wallet', xprv):
882
882
self .active_forwardings = self .db .get_dict ('active_forwardings' ) # type: Dict[str, List[str]] # Dict: payment_key -> list of htlc_keys
883
883
self .forwarding_failures = self .db .get_dict ('forwarding_failures' ) # type: Dict[str, Tuple[str, str]] # Dict: payment_key -> (error_bytes, error_message)
884
884
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
885
886
886
887
# payment_hash -> callback:
887
888
self .hold_invoice_callbacks = {} # type: Dict[bytes, Callable[[bytes], Awaitable[None]]]
@@ -1246,6 +1247,9 @@ async def open_channel_just_in_time(
1246
1247
) -> str :
1247
1248
# if an exception is raised during negotiation, we raise an OnionRoutingFailure.
1248
1249
# 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
1249
1253
try :
1250
1254
funding_sat = 2 * (next_amount_msat_htlc // 1000 ) # try to fully spend htlcs
1251
1255
password = self .wallet .get_unlocked_password () if self .wallet .has_password () else None
@@ -1280,13 +1284,17 @@ async def wait_for_preimage():
1280
1284
while self .get_preimage (payment_hash ) is None :
1281
1285
await asyncio .sleep (1 )
1282
1286
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 )
1283
1291
except OnionRoutingFailure :
1284
1292
raise
1285
1293
except Exception :
1286
1294
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
+
1290
1298
htlc_key = serialize_htlc_key (next_chan .get_scid_or_local_alias (), htlc .htlc_id )
1291
1299
return htlc_key
1292
1300
0 commit comments