mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-31 09:21:39 +00:00
function that returns map from commitment number to list of HTLCs
This commit is contained in:
parent
5b7c801ca4
commit
783cac1f23
2 changed files with 28 additions and 2 deletions
|
@ -579,14 +579,14 @@ class Channel(PrintError):
|
|||
unsettled_local + remote_settled - local_settled
|
||||
return remote_msat, local_msat
|
||||
|
||||
def included_htlcs(self, subject, htlc_initiator):
|
||||
def included_htlcs(self, subject, htlc_initiator, only_pending=True):
|
||||
"""
|
||||
return filter of non-dust htlcs for subjects commitment transaction, initiated by given party
|
||||
"""
|
||||
feerate = self.pending_feerate(subject)
|
||||
conf = self.config[subject]
|
||||
weight = HTLC_SUCCESS_WEIGHT if subject != htlc_initiator else HTLC_TIMEOUT_WEIGHT
|
||||
htlcs = self.htlcs(htlc_initiator, only_pending=True)
|
||||
htlcs = self.htlcs(htlc_initiator, only_pending=only_pending)
|
||||
fee_for_htlc = lambda htlc: htlc.amount_msat // 1000 - (weight * feerate // 1000)
|
||||
return filter(lambda htlc: fee_for_htlc(htlc) >= conf.dust_limit_sat, htlcs)
|
||||
|
||||
|
@ -853,6 +853,21 @@ class Channel(PrintError):
|
|||
assert tx.is_complete()
|
||||
return tx
|
||||
|
||||
def included_htlcs_in_latest_ctxs(self):
|
||||
""" A map from commitment number to list of HTLCs in
|
||||
their latest two commitment transactions.
|
||||
The oldest might have been revoked. """
|
||||
old_htlcs = list(self.included_htlcs(REMOTE, REMOTE, only_pending=False)) \
|
||||
+ list(self.included_htlcs(REMOTE, LOCAL, only_pending=False))
|
||||
|
||||
old_logs = dict(self.lock_in_htlc_changes(LOCAL))
|
||||
new_htlcs = list(self.included_htlcs(REMOTE, REMOTE)) \
|
||||
+ list(self.included_htlcs(REMOTE, LOCAL))
|
||||
self.log = old_logs
|
||||
|
||||
return {self.config[REMOTE].ctn: old_htlcs,
|
||||
self.config[REMOTE].ctn+1: new_htlcs, }
|
||||
|
||||
def maybe_create_sweeptx_for_their_ctx_to_remote(chan, ctx, their_pcp: bytes,
|
||||
sweep_address) -> Optional[EncumberedTransaction]:
|
||||
assert isinstance(their_pcp, bytes)
|
||||
|
|
|
@ -199,6 +199,11 @@ class TestChannel(unittest.TestCase):
|
|||
alice_channel, bob_channel = self.alice_channel, self.bob_channel
|
||||
htlc = self.htlc
|
||||
|
||||
ctn_to_htlcs = alice_channel.included_htlcs_in_latest_ctxs()
|
||||
self.assertEqual(list(ctn_to_htlcs.keys()), [0,1])
|
||||
self.assertEqual(ctn_to_htlcs[0], [])
|
||||
self.assertEqual(ctn_to_htlcs[1], [htlc])
|
||||
|
||||
# Next alice commits this change by sending a signature message. Since
|
||||
# we expect the messages to be ordered, Bob will receive the HTLC we
|
||||
# just sent before he receives this signature, so the signature will
|
||||
|
@ -273,6 +278,12 @@ class TestChannel(unittest.TestCase):
|
|||
alice_channel.receive_htlc_settle(preimage, self.aliceHtlcIndex)
|
||||
|
||||
bobSig2, bobHtlcSigs2 = bob_channel.sign_next_commitment()
|
||||
|
||||
ctn_to_htlcs = bob_channel.included_htlcs_in_latest_ctxs()
|
||||
self.assertEqual(list(ctn_to_htlcs.keys()), [1,2])
|
||||
self.assertEqual(len(ctn_to_htlcs[1]), 1)
|
||||
self.assertEqual(len(ctn_to_htlcs[2]), 0)
|
||||
|
||||
alice_channel.receive_new_commitment(bobSig2, bobHtlcSigs2)
|
||||
|
||||
aliceRevocation2, _ = alice_channel.revoke_current_commitment()
|
||||
|
|
Loading…
Add table
Reference in a new issue