URI: 
       tln: share more code with control path for failed htlc, verify ctx'es - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit 9247da5203f2e6f6c9dff85db1a6acaf83f4249a
   DIR parent 96a16adf30f90ace13023135ff4b16441785b653
  HTML Author: Janus <ysangkok@gmail.com>
       Date:   Wed, 25 Jul 2018 17:43:52 +0200
       
       ln: share more code with control path for failed htlc, verify ctx'es
       
       Diffstat:
         M electrum/lnbase.py                  |      54 +++++++++++--------------------
         M electrum/tests/test_lnhtlc.py       |       2 --
       
       2 files changed, 19 insertions(+), 37 deletions(-)
       ---
   DIR diff --git a/electrum/lnbase.py b/electrum/lnbase.py
       t@@ -879,12 +879,9 @@ class Peer(PrintError):
                self.attempted_route[(chan.channel_id, htlc.htlc_id)] = route
        
                sig_64, htlc_sigs = chan.sign_next_commitment()
       -
                self.send_message(gen_msg("commitment_signed", channel_id=chan.channel_id, signature=sig_64, num_htlcs=len(htlc_sigs), htlc_signature=b"".join(htlc_sigs)))
                await self.receive_revoke(chan)
       -
                await self.receive_commitment(chan)
       -
                self.revoke(chan)
        
                fulfill_coro = asyncio.ensure_future(self.update_fulfill_htlc[chan.channel_id].get())
       t@@ -892,42 +889,31 @@ class Peer(PrintError):
        
                done, pending = await asyncio.wait([fulfill_coro, failure_coro], return_when=FIRST_COMPLETED)
                if failure_coro.done():
       +            fulfill_coro.cancel()
                    sig_64, htlc_sigs = chan.sign_next_commitment()
       -            # TODO this is wrong when the htlc is dust
       -            self.send_message(gen_msg("commitment_signed", channel_id=chan.channel_id, signature=sig_64, num_htlcs=1, htlc_signature=htlc_sigs[0]))
       -            self.revoke(chan)
       -            while (await self.commitment_signed[chan.channel_id].get())["htlc_signature"] != b"":
       -                pass
       -            # TODO process above commitment transactions
       +            self.send_message(gen_msg("commitment_signed", channel_id=chan.channel_id, signature=sig_64, num_htlcs=len(htlc_sigs), htlc_signature=b"".join(htlc_sigs)))
                    await self.receive_revoke(chan)
                    chan.fail_htlc(htlc)
       +            await self.receive_commitment(chan)
       +            self.revoke(chan)
                    sig_64, htlc_sigs = chan.sign_next_commitment()
       -            self.send_message(gen_msg("commitment_signed", channel_id=chan.channel_id, signature=sig_64, num_htlcs=0))
       -            await self.receive_revoke(chan)
       -            fulfill_coro.cancel()
       -            self.lnworker.save_channel(chan)
       -            return failure_coro.result()
       -
       -        failure_coro.cancel()
       -        update_fulfill_htlc_msg = fulfill_coro.result()
       -
       -        preimage = update_fulfill_htlc_msg["payment_preimage"]
       -        chan.receive_htlc_settle(preimage, int.from_bytes(update_fulfill_htlc_msg["id"], "big"))
       -
       -        await self.receive_commitment(chan)
       -
       -        self.revoke(chan)
       -
       -        bare_ctx = chan.make_commitment(chan.remote_state.ctn + 1, False, chan.remote_state.next_per_commitment_point,
       -            msat_remote, msat_local)
       +            res = failure_coro.result()
       +        else:
       +            failure_coro.cancel()
       +            update_fulfill_htlc_msg = fulfill_coro.result()
       +            preimage = update_fulfill_htlc_msg["payment_preimage"]
       +            chan.receive_htlc_settle(preimage, int.from_bytes(update_fulfill_htlc_msg["id"], "big"))
       +            await self.receive_commitment(chan)
       +            self.revoke(chan)
       +            bare_ctx = chan.make_commitment(chan.remote_state.ctn + 1, False, chan.remote_state.next_per_commitment_point,
       +                msat_remote, msat_local)
       +            sig_64 = sign_and_get_sig_string(bare_ctx, chan.local_config, chan.remote_config)
       +            res = bh2u(preimage)
        
       -        sig_64 = sign_and_get_sig_string(bare_ctx, chan.local_config, chan.remote_config)
                self.send_message(gen_msg("commitment_signed", channel_id=chan.channel_id, signature=sig_64, num_htlcs=0))
       -
                await self.receive_revoke(chan)
       -
                self.lnworker.save_channel(chan)
       -        return bh2u(preimage)
       +        return res
        
            async def receive_revoke(self, m):
                revoke_and_ack_msg = await self.revoke_and_ack[m.channel_id].get()
       t@@ -996,9 +982,7 @@ class Peer(PrintError):
        
                if commit_coro.done():
                    # this branch is taken with lnd after a fee update (initiated by us, of course)
       -
       -            # TODO await process_commit(chan, commit_coro.result())
       -            await asyncio.wait([revoke_coro])
       +            await revoke_coro
                    process_revoke(revoke_coro.result())
                    self.revoke(chan)
                    await self.receive_commitment(chan)
       t@@ -1010,7 +994,7 @@ class Peer(PrintError):
                elif revoke_coro.done():
                    process_revoke(revoke_coro.result())
        
       -            await asyncio.wait([commit_coro])
       +            await commit_coro
                    await self.receive_commitment(chan, commit_coro.result())
                    self.revoke(chan)
        
   DIR diff --git a/electrum/tests/test_lnhtlc.py b/electrum/tests/test_lnhtlc.py
       t@@ -133,7 +133,6 @@ class TestLNBaseHTLCStateMachine(unittest.TestCase):
                    payment_hash = paymentHash,
                    amount_msat =  one_bitcoin_in_msat,
                    cltv_expiry =  5,
       -            total_fee = 0
                )
        
                # First Alice adds the outgoing HTLC to her local channel's state
       t@@ -326,7 +325,6 @@ class TestLNHTLCDust(unittest.TestCase):
                    payment_hash = paymentHash,
                    amount_msat =  1000 * htlcAmt,
                    cltv_expiry =  5, # also in create_test_channels
       -            total_fee = 0
                )
        
                aliceHtlcIndex = alice_channel.add_htlc(htlc)