Browse Source

lnpeer: fix some type hints

related https://github.com/spesmilo/electrum/pull/8924

note: "forwarding_key" can be stored as False. I think this is ugly and
might be better to do a storage upgrade to change those values to None.
master
SomberNight 2 years ago
parent
commit
0faadc0469
No known key found for this signature in database
GPG Key ID: B33B5F232C6271E9
  1. 11
      electrum/lnpeer.py

11
electrum/lnpeer.py

@ -2021,7 +2021,7 @@ class Peer(Logger):
processed_onion: ProcessedOnionPacket, processed_onion: ProcessedOnionPacket,
onion_packet_bytes: bytes, onion_packet_bytes: bytes,
already_forwarded: bool = False, already_forwarded: bool = False,
) -> Tuple[Optional[str], Optional[Tuple[str, Callable[[], Awaitable[Optional[str]]]]]]: ) -> Tuple[Optional[bytes], Optional[Tuple[str, Callable[[], Awaitable[Optional[str]]]]]]:
""" """
Decide what to do with an HTLC: return preimage if it can be fulfilled, forwarding callback if it can be forwarded. Decide what to do with an HTLC: return preimage if it can be fulfilled, forwarding callback if it can be forwarded.
Return (preimage, (payment_key, callback)) with at most a single element not None. Return (preimage, (payment_key, callback)) with at most a single element not None.
@ -2613,6 +2613,7 @@ class Peer(Logger):
for htlc_id, (local_ctn, remote_ctn, onion_packet_hex, forwarding_key) in unfulfilled.items(): for htlc_id, (local_ctn, remote_ctn, onion_packet_hex, forwarding_key) in unfulfilled.items():
if not chan.hm.is_htlc_irrevocably_added_yet(htlc_proposer=REMOTE, htlc_id=htlc_id): if not chan.hm.is_htlc_irrevocably_added_yet(htlc_proposer=REMOTE, htlc_id=htlc_id):
continue continue
forwarding_key = forwarding_key or None # type: Optional[str]
htlc = chan.hm.get_htlc_by_id(REMOTE, htlc_id) htlc = chan.hm.get_htlc_by_id(REMOTE, htlc_id)
error_reason = None # type: Optional[OnionRoutingFailure] error_reason = None # type: Optional[OnionRoutingFailure]
error_bytes = None # type: Optional[bytes] error_bytes = None # type: Optional[bytes]
@ -2632,7 +2633,7 @@ class Peer(Logger):
onion_packet_bytes=onion_packet_bytes, onion_packet_bytes=onion_packet_bytes,
onion_packet=onion_packet) onion_packet=onion_packet)
if _forwarding_key: if _forwarding_key:
assert forwarding_key is False assert forwarding_key is None
unfulfilled[htlc_id] = local_ctn, remote_ctn, onion_packet_hex, _forwarding_key unfulfilled[htlc_id] = local_ctn, remote_ctn, onion_packet_hex, _forwarding_key
except OnionRoutingFailure as e: except OnionRoutingFailure as e:
error_bytes = construct_onion_error(e, onion_packet.public_key, our_onion_private_key=self.privkey) error_bytes = construct_onion_error(e, onion_packet.public_key, our_onion_private_key=self.privkey)
@ -2688,9 +2689,9 @@ class Peer(Logger):
self, *, self, *,
chan: Channel, chan: Channel,
htlc: UpdateAddHtlc, htlc: UpdateAddHtlc,
forwarding_key: str, forwarding_key: Optional[str],
onion_packet_bytes: bytes, onion_packet_bytes: bytes,
onion_packet: OnionPacket) -> Tuple[Optional[bytes], Union[bool, None, Tuple[str, int]], Optional[bytes]]: onion_packet: OnionPacket) -> Tuple[Optional[bytes], Optional[str], Optional[bytes]]:
""" """
return (preimage, payment_key, error_bytes) with at most a single element that is not None return (preimage, payment_key, error_bytes) with at most a single element that is not None
raise an OnionRoutingFailure if we need to fail the htlc raise an OnionRoutingFailure if we need to fail the htlc
@ -2739,7 +2740,7 @@ class Peer(Logger):
return None, None, None return None, None, None
else: else:
# HTLC we are supposed to forward, and have already forwarded # HTLC we are supposed to forward, and have already forwarded
# for trampoline onions, forwarding failures are stored with forwarding_key (which is the inner key) # for final trampoline onions, forwarding failures are stored with forwarding_key (which is the inner key)
payment_key = forwarding_key payment_key = forwarding_key
preimage = self.lnworker.get_preimage(payment_hash) preimage = self.lnworker.get_preimage(payment_hash)
error_bytes, error_reason = self.lnworker.get_forwarding_failure(payment_key) error_bytes, error_reason = self.lnworker.get_forwarding_failure(payment_key)

Loading…
Cancel
Save