mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-31 09:21:39 +00:00
encapsulate detect_who_closed in channel
This commit is contained in:
parent
24cc3599c7
commit
d9b041e64d
3 changed files with 20 additions and 30 deletions
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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…
Add table
Reference in a new issue