@@ -812,6 +812,7 @@ def __init__(self, wallet: 'Abstract_Wallet', xprv):
812
812
self .active_forwardings = self .db .get_dict ('active_forwardings' ) # type: Dict[str, List[str]] # Dict: payment_key -> list of htlc_keys
813
813
self .forwarding_failures = self .db .get_dict ('forwarding_failures' ) # type: Dict[str, Tuple[str, str]] # Dict: payment_key -> (error_bytes, error_message)
814
814
self .downstream_to_upstream_htlc = {} # type: Dict[str, str] # Dict: htlc_key -> htlc_key (not persisted)
815
+ 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
815
816
816
817
# payment_hash -> callback:
817
818
self .hold_invoice_callbacks = {} # type: Dict[bytes, Callable[[bytes], Awaitable[None]]]
@@ -1176,6 +1177,9 @@ async def open_channel_just_in_time(
1176
1177
) -> str :
1177
1178
# if an exception is raised during negotiation, we raise an OnionRoutingFailure.
1178
1179
# this will cancel the incoming HTLC
1180
+
1181
+ # prevent settling the htlc until the channel opening was successfull so we can fail it if needed
1182
+ self .dont_settle_htlcs [payment_hash .hex ()] = None
1179
1183
try :
1180
1184
funding_sat = 2 * (next_amount_msat_htlc // 1000 ) # try to fully spend htlcs
1181
1185
password = self .wallet .get_unlocked_password () if self .wallet .has_password () else None
@@ -1210,13 +1214,17 @@ async def wait_for_preimage():
1210
1214
while self .get_preimage (payment_hash ) is None :
1211
1215
await asyncio .sleep (1 )
1212
1216
await util .wait_for2 (wait_for_preimage (), LN_P2P_NETWORK_TIMEOUT )
1217
+
1218
+ # We have been paid and can broadcast
1219
+ # todo: if broadcasting raise an exception, we should try to rebroadcast
1220
+ await self .network .broadcast_transaction (funding_tx )
1213
1221
except OnionRoutingFailure :
1214
1222
raise
1215
1223
except Exception :
1216
1224
raise OnionRoutingFailure (code = OnionFailureCode .TEMPORARY_NODE_FAILURE , data = b'' )
1217
- # We have been paid and can broadcast
1218
- # todo: if broadcasting raise an exception, we should try to rebroadcast
1219
- await self . network . broadcast_transaction ( funding_tx )
1225
+ finally :
1226
+ del self . dont_settle_htlcs [ payment_hash . hex ()]
1227
+
1220
1228
htlc_key = serialize_htlc_key (next_chan .get_scid_or_local_alias (), htlc .htlc_id )
1221
1229
return htlc_key
1222
1230
0 commit comments