Browse Source

lnworker: use NamedTuple for received_mpp_htlcs. add/fix type hints

try to avoid long plain tuples
master
SomberNight 2 years ago
parent
commit
8ef2495096
No known key found for this signature in database
GPG Key ID: B33B5F232C6271E9
  1. 108
      electrum/lnworker.py

108
electrum/lnworker.py

@ -10,7 +10,7 @@ import time
import operator import operator
from enum import IntEnum from enum import IntEnum
from typing import (Optional, Sequence, Tuple, List, Set, Dict, TYPE_CHECKING, from typing import (Optional, Sequence, Tuple, List, Set, Dict, TYPE_CHECKING,
NamedTuple, Union, Mapping, Any, Iterable, AsyncGenerator, DefaultDict) NamedTuple, Union, Mapping, Any, Iterable, AsyncGenerator, DefaultDict, Callable)
import threading import threading
import socket import socket
import aiohttp import aiohttp
@ -167,6 +167,13 @@ class PaymentInfo(NamedTuple):
status: int status: int
class ReceivedMPPStatus(NamedTuple):
is_expired: bool
is_accepted: bool
expected_msat: int
htlc_set: Set[Tuple[ShortChannelID, UpdateAddHtlc]]
class ErrorAddingPeer(Exception): pass class ErrorAddingPeer(Exception): pass
@ -665,7 +672,7 @@ class LNWallet(LNWorker):
self.sent_htlcs = defaultdict(asyncio.Queue) # type: Dict[bytes, asyncio.Queue[HtlcLog]] self.sent_htlcs = defaultdict(asyncio.Queue) # type: Dict[bytes, asyncio.Queue[HtlcLog]]
self.sent_htlcs_info = dict() # (RHASH, scid, htlc_id) -> route, payment_secret, amount_msat, bucket_msat, trampoline_fee_level self.sent_htlcs_info = dict() # (RHASH, scid, htlc_id) -> route, payment_secret, amount_msat, bucket_msat, trampoline_fee_level
self.sent_buckets = dict() # payment_secret -> (amount_sent, amount_failed) self.sent_buckets = dict() # payment_secret -> (amount_sent, amount_failed)
self.received_mpp_htlcs = dict() # RHASH -> mpp_status, htlc_set self.received_mpp_htlcs = dict() # type: Dict[bytes, ReceivedMPPStatus] # payment_secret -> ReceivedMPPStatus
self.swap_manager = SwapManager(wallet=self.wallet, lnworker=self) self.swap_manager = SwapManager(wallet=self.wallet, lnworker=self)
# detect inflight payments # detect inflight payments
@ -676,7 +683,8 @@ class LNWallet(LNWorker):
self.trampoline_forwarding_failures = {} # todo: should be persisted self.trampoline_forwarding_failures = {} # todo: should be persisted
# map forwarded htlcs (fw_info=(scid_hex, htlc_id)) to originating peer pubkeys # map forwarded htlcs (fw_info=(scid_hex, htlc_id)) to originating peer pubkeys
self.downstream_htlc_to_upstream_peer_map = {} # type: Dict[Tuple[str, int], bytes] self.downstream_htlc_to_upstream_peer_map = {} # type: Dict[Tuple[str, int], bytes]
self.hold_invoice_callbacks = {} # payment_hash -> callback, timeout # payment_hash -> callback, timeout:
self.hold_invoice_callbacks = {} # type: Dict[bytes, Tuple[Callable[[bytes], None], int]]
self.payment_bundles = [] # lists of hashes. todo:persist self.payment_bundles = [] # lists of hashes. todo:persist
@ -1891,11 +1899,13 @@ class LNWallet(LNWorker):
amount_msat, direction, status = self.payment_info[key] amount_msat, direction, status = self.payment_info[key]
return PaymentInfo(payment_hash, amount_msat, direction, status) return PaymentInfo(payment_hash, amount_msat, direction, status)
def add_payment_info_for_hold_invoice(self, payment_hash, lightning_amount_sat): def add_payment_info_for_hold_invoice(self, payment_hash: bytes, lightning_amount_sat: int):
info = PaymentInfo(payment_hash, lightning_amount_sat * 1000, RECEIVED, PR_UNPAID) info = PaymentInfo(payment_hash, lightning_amount_sat * 1000, RECEIVED, PR_UNPAID)
self.save_payment_info(info, write_to_disk=False) self.save_payment_info(info, write_to_disk=False)
def register_callback_for_hold_invoice(self, payment_hash, cb, timeout: Optional[int] = None): def register_callback_for_hold_invoice(
self, payment_hash: bytes, cb: Callable[[bytes], None], timeout: int,
):
expiry = int(time.time()) + timeout expiry = int(time.time()) + timeout
self.hold_invoice_callbacks[payment_hash] = cb, expiry self.hold_invoice_callbacks[payment_hash] = cb, expiry
@ -1907,7 +1917,12 @@ class LNWallet(LNWorker):
if write_to_disk: if write_to_disk:
self.wallet.save_db() self.wallet.save_db()
def check_received_htlc(self, payment_secret, short_channel_id, htlc: UpdateAddHtlc, expected_msat: int) -> Optional[bool]: def check_received_htlc(
self, payment_secret: bytes,
short_channel_id: ShortChannelID,
htlc: UpdateAddHtlc,
expected_msat: int,
) -> Optional[bool]:
""" return MPP status: True (accepted), False (expired) or None (waiting) """ return MPP status: True (accepted), False (expired) or None (waiting)
""" """
payment_hash = htlc.payment_hash payment_hash = htlc.payment_hash
@ -1952,47 +1967,64 @@ class LNWallet(LNWorker):
self.maybe_cleanup_mpp_status(payment_secret, short_channel_id, htlc) self.maybe_cleanup_mpp_status(payment_secret, short_channel_id, htlc)
return True if is_accepted else (False if is_expired else None) return True if is_accepted else (False if is_expired else None)
def update_mpp_with_received_htlc(self, payment_secret, short_channel_id, htlc, expected_msat): def update_mpp_with_received_htlc(
self,
payment_secret: bytes,
short_channel_id: ShortChannelID,
htlc: UpdateAddHtlc,
expected_msat: int,
):
# add new htlc to set # add new htlc to set
is_expired, is_accepted, _expected_msat, htlc_set = self.received_mpp_htlcs.get(payment_secret, (False, False, expected_msat, set())) mpp_status = self.received_mpp_htlcs.get(payment_secret)
assert expected_msat == _expected_msat if mpp_status is None:
mpp_status = ReceivedMPPStatus(
is_expired=False,
is_accepted=False,
expected_msat=expected_msat,
htlc_set=set(),
)
assert expected_msat == mpp_status.expected_msat
key = (short_channel_id, htlc) key = (short_channel_id, htlc)
if key not in htlc_set: if key not in mpp_status.htlc_set:
htlc_set.add(key) mpp_status.htlc_set.add(key) # side-effecting htlc_set
self.received_mpp_htlcs[payment_secret] = is_expired, is_accepted, _expected_msat, htlc_set self.received_mpp_htlcs[payment_secret] = mpp_status
def get_mpp_status(self, payment_secret): def get_mpp_status(self, payment_secret: bytes) -> Tuple[bool, bool]:
is_expired, is_accepted, _expected_msat, htlc_set = self.received_mpp_htlcs[payment_secret] mpp_status = self.received_mpp_htlcs[payment_secret]
return is_expired, is_accepted return mpp_status.is_expired, mpp_status.is_accepted
def set_mpp_status(self, payment_secret, is_expired, is_accepted): def set_mpp_status(self, payment_secret: bytes, is_expired: bool, is_accepted: bool):
_is_expired, _is_accepted, _expected_msat, htlc_set = self.received_mpp_htlcs[payment_secret] mpp_status = self.received_mpp_htlcs[payment_secret]
self.received_mpp_htlcs[payment_secret] = is_expired, is_accepted, _expected_msat, htlc_set self.received_mpp_htlcs[payment_secret] = mpp_status._replace(
is_expired=is_expired,
is_accepted=is_accepted,
)
def is_mpp_amount_reached(self, payment_secret): def is_mpp_amount_reached(self, payment_secret: bytes) -> bool:
mpp = self.received_mpp_htlcs.get(payment_secret) mpp_status = self.received_mpp_htlcs.get(payment_secret)
if not mpp: if not mpp_status:
return False return False
is_expired, is_accepted, _expected_msat, htlc_set = mpp total = sum([_htlc.amount_msat for scid, _htlc in mpp_status.htlc_set])
total = sum([_htlc.amount_msat for scid, _htlc in htlc_set]) return total >= mpp_status.expected_msat
return total >= _expected_msat
def get_first_timestamp_of_mpp(self, payment_secret): def get_first_timestamp_of_mpp(self, payment_secret: bytes) -> int:
mpp = self.received_mpp_htlcs.get(payment_secret) mpp_status = self.received_mpp_htlcs.get(payment_secret)
if not mpp: if not mpp_status:
return int(time.time()) return int(time.time())
is_expired, is_accepted, _expected_msat, htlc_set = mpp return min([_htlc.timestamp for scid, _htlc in mpp_status.htlc_set])
return min([_htlc.timestamp for scid, _htlc in htlc_set])
def maybe_cleanup_mpp_status(self, payment_secret, short_channel_id, htlc): def maybe_cleanup_mpp_status(
is_expired, is_accepted, _expected_msat, htlc_set = self.received_mpp_htlcs[payment_secret] self,
if not is_accepted and not is_expired: payment_secret: bytes,
short_channel_id: ShortChannelID,
htlc: UpdateAddHtlc,
) -> None:
mpp_status = self.received_mpp_htlcs[payment_secret]
if not mpp_status.is_accepted and not mpp_status.is_expired:
return return
key = (short_channel_id, htlc) key = (short_channel_id, htlc)
htlc_set.remove(key) mpp_status.htlc_set.remove(key) # side-effecting htlc_set
if len(htlc_set) > 0: if not mpp_status.htlc_set and payment_secret in self.received_mpp_htlcs:
self.received_mpp_htlcs[payment_secret] = is_expired, is_accepted, _expected_msat, htlc_set
elif payment_secret in self.received_mpp_htlcs:
self.received_mpp_htlcs.pop(payment_secret) self.received_mpp_htlcs.pop(payment_secret)
def get_payment_status(self, payment_hash: bytes) -> int: def get_payment_status(self, payment_hash: bytes) -> int:

Loading…
Cancel
Save