electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 783cac1f2357810cff780a90ee5f841e7d0c6823
parent 5b7c801ca44a1c2323953abcc19c120058f127fc
Author: Janus <ysangkok@gmail.com>
Date:   Mon,  5 Nov 2018 20:30:56 +0100

function that returns map from commitment number to list of HTLCs

Diffstat:
Melectrum/lnchan.py | 19+++++++++++++++++--
Melectrum/tests/test_lnchan.py | 11+++++++++++
2 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/electrum/lnchan.py b/electrum/lnchan.py @@ -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) diff --git a/electrum/tests/test_lnchan.py b/electrum/tests/test_lnchan.py @@ -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()