URI: 
       tencapsulate detect_who_closed in channel - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit d9b041e64d0c1451f789c037304fed7445fcb3e8
   DIR parent 24cc3599c700aa2e6082247141a000ac216d4821
  HTML Author: ThomasV <thomasv@electrum.org>
       Date:   Tue,  4 Jun 2019 11:37:29 +0200
       
       encapsulate detect_who_closed in channel
       
       Diffstat:
         M electrum/lnchannel.py               |      19 ++++++++++++++++++-
         M electrum/lnsweep.py                 |      18 ------------------
         M electrum/lnworker.py                |      13 ++-----------
       
       3 files changed, 20 insertions(+), 30 deletions(-)
       ---
   DIR diff --git a/electrum/lnchannel.py b/electrum/lnchannel.py
       t@@ -46,7 +46,7 @@ from .lnutil import (Outpoint, LocalConfig, RemoteConfig, Keypair, OnlyPubkeyKey
                            HTLC_TIMEOUT_WEIGHT, HTLC_SUCCESS_WEIGHT, extract_ctn_from_tx_and_chan, UpdateAddHtlc,
                            funding_output_script, SENT, RECEIVED, LOCAL, REMOTE, HTLCOwner, make_commitment_outputs,
                            ScriptHtlc, PaymentFailure, calc_onchain_fees, RemoteMisbehaving, make_htlc_output_witness_script)
       -from .lnsweep import create_sweeptxs_for_their_revoked_ctx
       +from .lnsweep import create_sweeptxs_for_their_revoked_ctx, create_sweeptxs_for_our_ctx, create_sweeptxs_for_their_ctx
        from .lnhtlc import HTLCManager
        
        
       t@@ -807,3 +807,20 @@ class Channel(Logger):
                tx.add_signature_to_txin(0, none_idx, bh2u(remote_sig))
                assert tx.is_complete()
                return tx
       +
       +    def get_sweep_info(self, ctx: Transaction):
       +        if self.sweep_info is None:
       +            ctn = extract_ctn_from_tx_and_chan(ctx, self)
       +            our_sweep_info = create_sweeptxs_for_our_ctx(self, ctx, ctn, self.sweep_address)
       +            their_sweep_info = create_sweeptxs_for_their_ctx(self, ctx, ctn, self.sweep_address)
       +            if our_sweep_info:
       +                self.sweep_info = our_sweep_info
       +                self.logger.info(f'we force closed.')
       +            elif their_sweep_info:
       +                self.sweep_info = their_sweep_info
       +                self.logger.info(f'they force closed.')
       +            else:
       +                self.sweep_info = {}
       +                self.logger.info(f'not sure who closed {ctx}.')
       +            self.logger.info(f'{repr(self.sweep_info)}')
       +        return self.sweep_info
   DIR diff --git a/electrum/lnsweep.py b/electrum/lnsweep.py
       t@@ -91,24 +91,6 @@ def create_sweeptxs_for_their_revoked_ctx(chan: 'Channel', ctx: Transaction, per
            return txs
        
        
       -class ChannelClosedBy(Enum):
       -    US = auto()
       -    THEM = auto()
       -    UNKNOWN = auto()
       -
       -
       -
       -def detect_who_closed(chan: 'Channel', ctx: Transaction) -> ChannelClosedBy:
       -    ctn = extract_ctn_from_tx_and_chan(ctx, chan)
       -    sweep_info = create_sweeptxs_for_our_ctx(chan, ctx, ctn, chan.sweep_address)
       -    if sweep_info:
       -        return ChannelClosedBy.US, sweep_info
       -    sweep_info = create_sweeptxs_for_their_ctx(chan, ctx, ctn, chan.sweep_address)
       -    if sweep_info:
       -        return ChannelClosedBy.THEM, sweep_info
       -    return ChannelClosedBy.UNKNOWN, {}
       -
       -
        def create_sweeptxs_for_our_ctx(chan: 'Channel', ctx: Transaction, ctn: int,
                                        sweep_address: str) -> Dict[str,Transaction]:
            """Handle the case where we force close unilaterally with our latest ctx.
   DIR diff --git a/electrum/lnworker.py b/electrum/lnworker.py
       t@@ -46,7 +46,6 @@ from .i18n import _
        from .lnrouter import RouteEdge, is_route_sane_to_use
        from .address_synchronizer import TX_HEIGHT_LOCAL
        from . import lnsweep
       -from .lnsweep import ChannelClosedBy
        from .lnsweep import create_sweeptxs_for_their_ctx, create_sweeptxs_for_our_ctx
        
        if TYPE_CHECKING:
       t@@ -515,18 +514,10 @@ class LNWallet(LNWorker):
                    self.channel_db.remove_channel(chan.short_channel_id)
        
                # detect who closed and set sweep_info
       -        if chan.sweep_info is None:
       -            closed_by, chan.sweep_info = lnsweep.detect_who_closed(chan, closing_tx)
       -            if closed_by == ChannelClosedBy.US:
       -                self.logger.info(f'we force closed {funding_outpoint}.')
       -            elif closed_by == ChannelClosedBy.THEM:
       -                self.logger.info(f'they force closed {funding_outpoint}.')
       -            else:
       -                self.logger.info(f'not sure who closed {funding_outpoint} {closing_txid}.')
       -            self.logger.info(f'{repr(chan.sweep_info)}')
       +        sweep_info = chan.get_sweep_info(closing_tx)
        
                # create and broadcast transaction
       -        for prevout, e_tx in chan.sweep_info.items():
       +        for prevout, e_tx in sweep_info.items():
                    name, csv_delay, cltv_expiry, gen_tx = e_tx
                    if spenders.get(prevout) is not None:
                        self.logger.info(f'outpoint already spent {prevout}')