Browse Source

encapsulate detect_who_closed in channel

dependabot/pip/contrib/deterministic-build/ecdsa-0.13.3
ThomasV 6 years ago
parent
commit
d9b041e64d
  1. 19
      electrum/lnchannel.py
  2. 18
      electrum/lnsweep.py
  3. 13
      electrum/lnworker.py

19
electrum/lnchannel.py

@ -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
@ -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

18
electrum/lnsweep.py

@ -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.

13
electrum/lnworker.py

@ -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:
@ -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}')

Loading…
Cancel
Save