Skip to content

Commit 737a30c

Browse files
committed
lnpeer/lnworker: refactor htlc_switch
refactor `htlc_switch` to new architecture to make it more robust against partial settlement of htlc sets and increase maintainability. Htlcs are now processed in two steps, first the htlcs are collected into sets from the channels, and potentially failed on their own already. Then a second loop iterates over the htlc sets and finalizes only on whole sets.
1 parent ae4b770 commit 737a30c

File tree

4 files changed

+752
-467
lines changed

4 files changed

+752
-467
lines changed

electrum/lnchannel.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ def __init__(self, state: 'StoredDict', *, name=None, lnworker=None, initial_fee
783783
self.onion_keys = state['onion_keys'] # type: Dict[int, bytes]
784784
self.data_loss_protect_remote_pcp = state['data_loss_protect_remote_pcp']
785785
self.hm = HTLCManager(log=state['log'], initial_feerate=initial_feerate)
786-
self.unfulfilled_htlcs = state["unfulfilled_htlcs"] # type: Dict[int, Tuple[str, Optional[str]]]
786+
self.unfulfilled_htlcs = state["unfulfilled_htlcs"] # type: Dict[int, Optional[str]]
787787
# ^ htlc_id -> onion_packet_hex, forwarding_key
788788
self._state = ChannelState[state['state']]
789789
self.peer_state = PeerState.DISCONNECTED
@@ -1112,6 +1112,7 @@ def _assert_can_add_htlc(self, *, htlc_proposer: HTLCOwner, amount_msat: int,
11121112
if amount_msat <= 0:
11131113
raise PaymentFailure("HTLC value must be positive")
11141114
if amount_msat < chan_config.htlc_minimum_msat:
1115+
# todo: for incoming htlcs this could be handled more gracefully with `amount_below_minimum`
11151116
raise PaymentFailure(f'HTLC value too small: {amount_msat} msat')
11161117

11171118
if self.htlc_slots_left(htlc_proposer) == 0:
@@ -1226,7 +1227,7 @@ def receive_htlc(self, htlc: UpdateAddHtlc, onion_packet:bytes = None) -> Update
12261227
with self.db_lock:
12271228
self.hm.recv_htlc(htlc)
12281229
if onion_packet:
1229-
self.unfulfilled_htlcs[htlc.htlc_id] = onion_packet.hex(), None
1230+
self.unfulfilled_htlcs[htlc.htlc_id] = onion_packet.hex()
12301231

12311232
self.logger.info("receive_htlc")
12321233
return htlc

0 commit comments

Comments
 (0)