mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-30 08:51:32 +00:00
lnhtlc: small clean-up / docstrings
This commit is contained in:
parent
268f05c60c
commit
8632f027da
4 changed files with 36 additions and 22 deletions
|
@ -114,8 +114,10 @@ class ChannelDetailsDialog(QtWidgets.QDialog):
|
|||
self.update_sent_received()
|
||||
|
||||
def update_sent_received(self):
|
||||
self.sent_label.setText(str(htlcsum(self.chan.hm.settled_htlcs_by(LOCAL))))
|
||||
self.received_label.setText(str(htlcsum(self.chan.hm.settled_htlcs_by(REMOTE))))
|
||||
self.sent_label.setText(str(htlcsum(
|
||||
self.chan.total_msat(Direction.SENT))))
|
||||
self.received_label.setText(str(htlcsum(
|
||||
self.chan.total_msat(Direction.RECEIVED))))
|
||||
|
||||
@QtCore.pyqtSlot(str)
|
||||
def show_tx(self, link_text: str):
|
||||
|
|
|
@ -516,7 +516,7 @@ class Channel(PrintError):
|
|||
assert type(subject) is HTLCOwner
|
||||
initial = self.config[subject].initial_msat
|
||||
|
||||
for direction, htlc in self.hm.settled_htlcs(subject, ctn):
|
||||
for direction, htlc in self.hm.all_settled_htlcs_ever(subject, ctn):
|
||||
if direction == SENT:
|
||||
initial -= htlc.amount_msat
|
||||
else:
|
||||
|
@ -594,9 +594,9 @@ class Channel(PrintError):
|
|||
return self.config[subject].ctn
|
||||
|
||||
def total_msat(self, direction):
|
||||
"""Return the cumulative total msat amount received/sent so far."""
|
||||
assert type(direction) is Direction
|
||||
sub = LOCAL if direction == SENT else REMOTE
|
||||
return htlcsum(self.hm.settled_htlcs_by(sub, self.config[sub].ctn))
|
||||
return htlcsum(self.hm.all_settled_htlcs_ever_by_direction(LOCAL, direction))
|
||||
|
||||
def get_unfulfilled_htlcs(self):
|
||||
log = self.hm.log[REMOTE]
|
||||
|
|
|
@ -8,6 +8,7 @@ from .util import bh2u
|
|||
class HTLCManager:
|
||||
|
||||
def __init__(self, local_ctn=0, remote_ctn=0, log=None):
|
||||
# self.ctn[sub] is the ctn for the oldest unrevoked ctx of sub
|
||||
self.ctn = {LOCAL:local_ctn, REMOTE: remote_ctn}
|
||||
self.expect_sig = {SENT: False, RECEIVED: False}
|
||||
if log is None:
|
||||
|
@ -16,7 +17,6 @@ class HTLCManager:
|
|||
else:
|
||||
assert type(log) is dict
|
||||
log = {HTLCOwner(int(x)): y for x, y in deepcopy(log).items()}
|
||||
# self.ctn[sub] is the ctn for the oldest unrevoked ctx of sub
|
||||
for sub in (LOCAL, REMOTE):
|
||||
log[sub]['adds'] = {int(x): UpdateAddHtlc(*y) for x, y in log[sub]['adds'].items()}
|
||||
coerceHtlcOwner2IntMap = lambda x: {HTLCOwner(int(y)): z for y, z in x.items()}
|
||||
|
@ -83,7 +83,9 @@ class HTLCManager:
|
|||
|
||||
def htlcs_by_direction(self, subject: HTLCOwner, direction: Direction,
|
||||
ctn: int = None) -> Sequence[UpdateAddHtlc]:
|
||||
"""
|
||||
"""Return the list of received or sent (depending on direction) HTLCs
|
||||
in subject's ctx at ctn.
|
||||
|
||||
direction is relative to subject!
|
||||
"""
|
||||
assert type(subject) is HTLCOwner
|
||||
|
@ -91,12 +93,9 @@ class HTLCManager:
|
|||
if ctn is None:
|
||||
ctn = self.ctn[subject]
|
||||
l = []
|
||||
if direction == SENT and subject == LOCAL:
|
||||
party = LOCAL
|
||||
elif direction == RECEIVED and subject == REMOTE:
|
||||
party = LOCAL
|
||||
else:
|
||||
party = REMOTE
|
||||
# subject's ctx
|
||||
# party is the proposer of the HTLCs
|
||||
party = subject if direction == SENT else subject.inverted()
|
||||
for htlc_id, ctns in self.log[party]['locked_in'].items():
|
||||
htlc_height = ctns[subject]
|
||||
if htlc_height is None:
|
||||
|
@ -113,6 +112,7 @@ class HTLCManager:
|
|||
return l
|
||||
|
||||
def htlcs(self, subject: HTLCOwner, ctn: int = None) -> Sequence[Tuple[Direction, UpdateAddHtlc]]:
|
||||
"""Return the list of HTLCs in subject's ctx at ctn."""
|
||||
assert type(subject) is HTLCOwner
|
||||
if ctn is None:
|
||||
ctn = self.ctn[subject]
|
||||
|
@ -122,11 +122,13 @@ class HTLCManager:
|
|||
return l
|
||||
|
||||
def current_htlcs(self, subject: HTLCOwner) -> Sequence[Tuple[Direction, UpdateAddHtlc]]:
|
||||
"""Return the list of HTLCs in subject's oldest unrevoked ctx."""
|
||||
assert type(subject) is HTLCOwner
|
||||
ctn = self.ctn[subject]
|
||||
return self.htlcs(subject, ctn)
|
||||
|
||||
def pending_htlcs(self, subject: HTLCOwner) -> Sequence[Tuple[Direction, UpdateAddHtlc]]:
|
||||
"""Return the list of HTLCs in subject's next ctx (one after oldest unrevoked)."""
|
||||
assert type(subject) is HTLCOwner
|
||||
ctn = self.ctn[subject] + 1
|
||||
return self.htlcs(subject, ctn)
|
||||
|
@ -137,23 +139,33 @@ class HTLCManager:
|
|||
def recv_settle(self, htlc_id: int) -> None:
|
||||
self.log[LOCAL]['settles'][htlc_id] = {LOCAL: self.ctn[LOCAL] + 1, REMOTE: None}
|
||||
|
||||
def settled_htlcs_by(self, subject: HTLCOwner, ctn: int = None) -> Sequence[UpdateAddHtlc]:
|
||||
def all_settled_htlcs_ever_by_direction(self, subject: HTLCOwner, direction: Direction,
|
||||
ctn: int = None) -> Sequence[UpdateAddHtlc]:
|
||||
"""Return the list of all HTLCs that have been ever settled in subject's
|
||||
ctx up to ctn, filtered to only "direction".
|
||||
"""
|
||||
assert type(subject) is HTLCOwner
|
||||
if ctn is None:
|
||||
ctn = self.ctn[subject]
|
||||
# subject's ctx
|
||||
# party is the proposer of the HTLCs
|
||||
party = subject if direction == SENT else subject.inverted()
|
||||
d = []
|
||||
for htlc_id, ctns in self.log[subject]['settles'].items():
|
||||
if ctns[subject] <= ctn:
|
||||
d.append(self.log[subject]['adds'][htlc_id])
|
||||
for htlc_id, ctns in self.log[party]['settles'].items():
|
||||
if ctns[subject] is not None and ctns[subject] <= ctn:
|
||||
d.append(self.log[party]['adds'][htlc_id])
|
||||
return d
|
||||
|
||||
def settled_htlcs(self, subject: HTLCOwner, ctn: int = None) -> Sequence[Tuple[Direction, UpdateAddHtlc]]:
|
||||
def all_settled_htlcs_ever(self, subject: HTLCOwner, ctn: int = None) \
|
||||
-> Sequence[Tuple[Direction, UpdateAddHtlc]]:
|
||||
"""Return the list of all HTLCs that have been ever settled in subject's
|
||||
ctx up to ctn.
|
||||
"""
|
||||
assert type(subject) is HTLCOwner
|
||||
if ctn is None:
|
||||
ctn = self.ctn[subject]
|
||||
sent = [(SENT, x) for x in self.settled_htlcs_by(subject, ctn)]
|
||||
other = subject.inverted()
|
||||
received = [(RECEIVED, x) for x in self.settled_htlcs_by(other, ctn)]
|
||||
sent = [(SENT, x) for x in self.all_settled_htlcs_ever_by_direction(subject, SENT, ctn)]
|
||||
received = [(RECEIVED, x) for x in self.all_settled_htlcs_ever_by_direction(subject, RECEIVED, ctn)]
|
||||
return sent + received
|
||||
|
||||
def received_in_ctn(self, ctn: int) -> Sequence[UpdateAddHtlc]:
|
||||
|
|
|
@ -84,7 +84,7 @@ class TestHTLCManager(unittest.TestCase):
|
|||
self.assertEqual(A.current_htlcs(LOCAL), [])
|
||||
self.assertEqual(A.current_htlcs(REMOTE), [])
|
||||
self.assertEqual(B.current_htlcs(REMOTE), [])
|
||||
self.assertEqual(len(A.settled_htlcs(LOCAL)), 1)
|
||||
self.assertEqual(len(A.all_settled_htlcs_ever(LOCAL)), 1)
|
||||
self.assertEqual(len(A.sent_in_ctn(2)), 1)
|
||||
self.assertEqual(len(B.received_in_ctn(2)), 1)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue