Browse Source

lnchannel: introduce HTLCWithStatus NamedTuple

patch-4
SomberNight 4 years ago
parent
commit
8481afb286
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 6
      electrum/gui/qt/channel_details.py
  2. 15
      electrum/lnchannel.py
  3. 21
      electrum/lnworker.py

6
electrum/gui/qt/channel_details.py

@ -200,9 +200,9 @@ class ChannelDetailsDialog(QtWidgets.QDialog, MessageBoxMixin):
w = QtWidgets.QTreeView(self) w = QtWidgets.QTreeView(self)
htlc_dict = chan.get_payments() htlc_dict = chan.get_payments()
htlc_list = [] htlc_list = []
for rhash, _list in htlc_dict.items(): for rhash, plist in htlc_dict.items():
for _tuple in _list: for htlc_with_status in plist:
htlc_list.append((rhash.hex(),) + _tuple) htlc_list.append((rhash.hex(),) + htlc_with_status)
w.setModel(self.make_model(htlc_list)) w.setModel(self.make_model(htlc_list))
w.header().setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents) w.header().setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents)
vbox.addWidget(w) vbox.addWidget(w)

15
electrum/lnchannel.py

@ -24,7 +24,7 @@ import binascii
import json import json
from enum import IntEnum from enum import IntEnum
from typing import (Optional, Dict, List, Tuple, NamedTuple, Set, Callable, from typing import (Optional, Dict, List, Tuple, NamedTuple, Set, Callable,
Iterable, Sequence, TYPE_CHECKING, Iterator, Union) Iterable, Sequence, TYPE_CHECKING, Iterator, Union, Mapping)
import time import time
import threading import threading
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
@ -140,6 +140,13 @@ def htlcsum(htlcs: Iterable[UpdateAddHtlc]):
return sum([x.amount_msat for x in htlcs]) return sum([x.amount_msat for x in htlcs])
class HTLCWithStatus(NamedTuple):
channel_id: bytes
htlc: UpdateAddHtlc
direction: Direction
status: str
class AbstractChannel(Logger, ABC): class AbstractChannel(Logger, ABC):
storage: Union['StoredDict', dict] storage: Union['StoredDict', dict]
config: Dict[HTLCOwner, Union[LocalConfig, RemoteConfig]] config: Dict[HTLCOwner, Union[LocalConfig, RemoteConfig]]
@ -722,7 +729,7 @@ class Channel(AbstractChannel):
def get_next_feerate(self, subject: HTLCOwner) -> int: def get_next_feerate(self, subject: HTLCOwner) -> int:
return self.hm.get_feerate_in_next_ctx(subject) return self.hm.get_feerate_in_next_ctx(subject)
def get_payments(self, status=None): def get_payments(self, status=None) -> Mapping[bytes, List[HTLCWithStatus]]:
out = defaultdict(list) out = defaultdict(list)
for direction, htlc in self.hm.all_htlcs_ever(): for direction, htlc in self.hm.all_htlcs_ever():
htlc_proposer = LOCAL if direction is SENT else REMOTE htlc_proposer = LOCAL if direction is SENT else REMOTE
@ -734,7 +741,9 @@ class Channel(AbstractChannel):
_status = 'inflight' _status = 'inflight'
if status and status != _status: if status and status != _status:
continue continue
out[htlc.payment_hash].append((self.channel_id, htlc, direction, _status)) htlc_with_status = HTLCWithStatus(
channel_id=self.channel_id, htlc=htlc, direction=direction, status=_status)
out[htlc.payment_hash].append(htlc_with_status)
return out return out
def open_with_first_pcp(self, remote_pcp: bytes, remote_sig: bytes) -> None: def open_with_first_pcp(self, remote_pcp: bytes, remote_sig: bytes) -> None:

21
electrum/lnworker.py

@ -48,7 +48,7 @@ from .lnpeer import Peer, LN_P2P_NETWORK_TIMEOUT
from .lnaddr import lnencode, LnAddr, lndecode from .lnaddr import lnencode, LnAddr, lndecode
from .ecc import der_sig_from_sig_string from .ecc import der_sig_from_sig_string
from .lnchannel import Channel, AbstractChannel from .lnchannel import Channel, AbstractChannel
from .lnchannel import ChannelState, PeerState from .lnchannel import ChannelState, PeerState, HTLCWithStatus
from .lnrater import LNRater from .lnrater import LNRater
from . import lnutil from . import lnutil
from .lnutil import funding_output_script from .lnutil import funding_output_script
@ -777,24 +777,27 @@ class LNWallet(LNWorker):
util.trigger_callback('channel', self.wallet, chan) util.trigger_callback('channel', self.wallet, chan)
super().peer_closed(peer) super().peer_closed(peer)
def get_payments(self, *, status=None): def get_payments(self, *, status=None) -> Mapping[bytes, List[HTLCWithStatus]]:
# return one item per payment_hash
# note: with AMP we will have several channels per payment
out = defaultdict(list) out = defaultdict(list)
for chan in self.channels.values(): for chan in self.channels.values():
d = chan.get_payments(status=status) d = chan.get_payments(status=status)
for k, v in d.items(): for payment_hash, plist in d.items():
out[k] += v out[payment_hash] += plist
return out return out
def get_payment_value(self, info: Optional['PaymentInfo'], plist): def get_payment_value(
self, info: Optional['PaymentInfo'], plist: List[HTLCWithStatus],
) -> Tuple[int, int, int]:
assert plist
amount_msat = 0 amount_msat = 0
fee_msat = None fee_msat = None
for chan_id, htlc, _direction, _status in plist: for htlc_with_status in plist:
htlc = htlc_with_status.htlc
_direction = htlc_with_status.direction
amount_msat += int(_direction) * htlc.amount_msat amount_msat += int(_direction) * htlc.amount_msat
if _direction == SENT and info and info.amount_msat: if _direction == SENT and info and info.amount_msat:
fee_msat = (fee_msat or 0) - info.amount_msat - amount_msat fee_msat = (fee_msat or 0) - info.amount_msat - amount_msat
timestamp = min([htlc.timestamp for chan_id, htlc, _direction, _status in plist]) timestamp = min([htlc_with_status.htlc.timestamp for htlc_with_status in plist])
return amount_msat, fee_msat, timestamp return amount_msat, fee_msat, timestamp
def get_lightning_history(self): def get_lightning_history(self):

Loading…
Cancel
Save