1
0

sweep: rename sweep creation functions

naming scheme: tx(s)_our/their_ctx/htlctx_output-description

function names are shortened to whether a single (tx) or several sweep
transactions (txs) are generated
This commit is contained in:
bitromortac
2021-11-12 10:34:50 +01:00
committed by ThomasV
parent de9fee706d
commit 9c277802e9
2 changed files with 56 additions and 47 deletions

View File

@@ -57,9 +57,9 @@ from .lnutil import (Outpoint, LocalConfig, RemoteConfig, Keypair, OnlyPubkeyKey
fee_for_htlc_output, offered_htlc_trim_threshold_sat, fee_for_htlc_output, offered_htlc_trim_threshold_sat,
received_htlc_trim_threshold_sat, make_commitment_output_to_remote_address, FIXED_ANCHOR_SAT, received_htlc_trim_threshold_sat, make_commitment_output_to_remote_address, FIXED_ANCHOR_SAT,
ChannelType, LNProtocolWarning, ctx_has_anchors) ChannelType, LNProtocolWarning, ctx_has_anchors)
from .lnsweep import create_sweeptxs_for_our_ctx, create_sweeptxs_for_their_ctx from .lnsweep import txs_our_ctx, txs_their_ctx
from .lnsweep import create_sweeptx_for_their_revoked_htlc, SweepInfo from .lnsweep import tx_their_htlctx_justice, SweepInfo
from .lnsweep import create_sweeptx_their_backup_ctx from .lnsweep import tx_their_ctx_to_remote_backup
from .lnhtlc import HTLCManager from .lnhtlc import HTLCManager
from .lnmsg import encode_msg, decode_msg from .lnmsg import encode_msg, decode_msg
from .address_synchronizer import TX_HEIGHT_LOCAL from .address_synchronizer import TX_HEIGHT_LOCAL
@@ -284,11 +284,11 @@ class AbstractChannel(Logger, ABC):
def delete_closing_height(self): def delete_closing_height(self):
self.storage.pop('closing_height', None) self.storage.pop('closing_height', None)
def create_sweeptxs_for_our_ctx(self, ctx: Transaction) -> Optional[Dict[str, SweepInfo]]: def create_sweeptxs_for_our_ctx(self, ctx):
return create_sweeptxs_for_our_ctx(chan=self, ctx=ctx, sweep_address=self.get_sweep_address()) return txs_our_ctx(chan=self, ctx=ctx, sweep_address=self.get_sweep_address())
def create_sweeptxs_for_their_ctx(self, ctx: Transaction) -> Optional[Dict[str, SweepInfo]]: def create_sweeptxs_for_their_ctx(self, ctx):
return create_sweeptxs_for_their_ctx(chan=self, ctx=ctx, sweep_address=self.get_sweep_address()) return txs_their_ctx(chan=self, ctx=ctx, sweep_address=self.get_sweep_address())
def is_backup(self) -> bool: def is_backup(self) -> bool:
return False return False
@@ -595,11 +595,11 @@ class ChannelBackup(AbstractChannel):
return True return True
def create_sweeptxs_for_their_ctx(self, ctx): def create_sweeptxs_for_their_ctx(self, ctx):
return create_sweeptx_their_backup_ctx(chan=self, ctx=ctx, sweep_address=self.get_sweep_address()) return tx_their_ctx_to_remote_backup(chan=self, ctx=ctx, sweep_address=self.get_sweep_address())
def create_sweeptxs_for_our_ctx(self, ctx): def create_sweeptxs_for_our_ctx(self, ctx):
if self.is_imported: if self.is_imported:
return create_sweeptxs_for_our_ctx(chan=self, ctx=ctx, sweep_address=self.get_sweep_address()) return txs_our_ctx(chan=self, ctx=ctx, sweep_address=self.get_sweep_address())
else: else:
return return
@@ -1491,9 +1491,9 @@ class Channel(AbstractChannel):
return self.get_commitment(subject, ctn=ctn) return self.get_commitment(subject, ctn=ctn)
def create_sweeptxs_for_watchtower(self, ctn: int) -> List[Transaction]: def create_sweeptxs_for_watchtower(self, ctn: int) -> List[Transaction]:
from .lnsweep import create_sweeptxs_for_watchtower from .lnsweep import txs_their_ctx_watchtower
secret, ctx = self.get_secret_and_commitment(REMOTE, ctn=ctn) secret, ctx = self.get_secret_and_commitment(REMOTE, ctn=ctn)
return create_sweeptxs_for_watchtower(self, ctx, secret, self.get_sweep_address()) return txs_their_ctx_watchtower(self, ctx, secret, self.get_sweep_address())
def get_oldest_unrevoked_ctn(self, subject: HTLCOwner) -> int: def get_oldest_unrevoked_ctn(self, subject: HTLCOwner) -> int:
return self.hm.ctn_oldest_unrevoked(subject) return self.hm.ctn_oldest_unrevoked(subject)
@@ -1728,7 +1728,7 @@ class Channel(AbstractChannel):
def maybe_sweep_revoked_htlc(self, ctx: Transaction, htlc_tx: Transaction) -> Optional[SweepInfo]: def maybe_sweep_revoked_htlc(self, ctx: Transaction, htlc_tx: Transaction) -> Optional[SweepInfo]:
# look at the output address, check if it matches # look at the output address, check if it matches
return create_sweeptx_for_their_revoked_htlc(self, ctx, htlc_tx, self.get_sweep_address()) return tx_their_htlctx_justice(self, ctx, htlc_tx, self.get_sweep_address())
def has_pending_changes(self, subject: HTLCOwner) -> bool: def has_pending_changes(self, subject: HTLCOwner) -> bool:
next_htlcs = self.hm.get_htlcs_in_next_ctx(subject) next_htlcs = self.hm.get_htlcs_in_next_ctx(subject)

View File

@@ -45,8 +45,8 @@ class SweepInfo(NamedTuple):
gen_tx: Callable[[], Optional[Transaction]] gen_tx: Callable[[], Optional[Transaction]]
def create_sweeptxs_for_watchtower(chan: 'Channel', ctx: Transaction, per_commitment_secret: bytes, def txs_their_ctx_watchtower(chan: 'Channel', ctx: Transaction, per_commitment_secret: bytes,
sweep_address: str) -> List[Transaction]: sweep_address: str) -> List[Transaction]:
"""Presign sweeping transactions using the just received revoked pcs. """Presign sweeping transactions using the just received revoked pcs.
These will only be utilised if the remote breaches. These will only be utilised if the remote breaches.
Sweep 'to_local', and all the HTLCs (two cases: directly from ctx, or from HTLC tx). Sweep 'to_local', and all the HTLCs (two cases: directly from ctx, or from HTLC tx).
@@ -68,7 +68,7 @@ def create_sweeptxs_for_watchtower(chan: 'Channel', ctx: Transaction, per_commit
output_idxs = ctx.get_output_idxs_from_address(to_local_address) output_idxs = ctx.get_output_idxs_from_address(to_local_address)
if output_idxs: if output_idxs:
output_idx = output_idxs.pop() output_idx = output_idxs.pop()
sweep_tx = create_sweeptx_ctx_to_local( sweep_tx = tx_ctx_to_local(
sweep_address=sweep_address, sweep_address=sweep_address,
ctx=ctx, ctx=ctx,
output_idx=output_idx, output_idx=output_idx,
@@ -79,7 +79,7 @@ def create_sweeptxs_for_watchtower(chan: 'Channel', ctx: Transaction, per_commit
if sweep_tx: if sweep_tx:
txs.append(sweep_tx) txs.append(sweep_tx)
# HTLCs # HTLCs
def create_sweeptx_for_htlc(*, htlc: 'UpdateAddHtlc', htlc_direction: Direction, def txs_their_htlctx_justice(*, htlc: 'UpdateAddHtlc', htlc_direction: Direction,
ctx_output_idx: int) -> Optional[Transaction]: ctx_output_idx: int) -> Optional[Transaction]:
htlc_tx_witness_script, htlc_tx = make_htlc_tx_with_open_channel( htlc_tx_witness_script, htlc_tx = make_htlc_tx_with_open_channel(
chan=chan, chan=chan,
@@ -90,7 +90,7 @@ def create_sweeptxs_for_watchtower(chan: 'Channel', ctx: Transaction, per_commit
commit=ctx, commit=ctx,
htlc=htlc, htlc=htlc,
ctx_output_idx=ctx_output_idx) ctx_output_idx=ctx_output_idx)
return create_sweeptx_that_spends_htlctx_that_spends_htlc_in_ctx( return tx_sweep_our_htlctx(
htlc_tx=htlc_tx, htlc_tx=htlc_tx,
htlctx_witness_script=htlc_tx_witness_script, htlctx_witness_script=htlc_tx_witness_script,
sweep_address=sweep_address, sweep_address=sweep_address,
@@ -105,7 +105,7 @@ def create_sweeptxs_for_watchtower(chan: 'Channel', ctx: Transaction, per_commit
subject=REMOTE, subject=REMOTE,
ctn=ctn) ctn=ctn)
for (direction, htlc), (ctx_output_idx, htlc_relative_idx) in htlc_to_ctx_output_idx_map.items(): for (direction, htlc), (ctx_output_idx, htlc_relative_idx) in htlc_to_ctx_output_idx_map.items():
secondstage_sweep_tx = create_sweeptx_for_htlc( secondstage_sweep_tx = txs_their_htlctx_justice(
htlc=htlc, htlc=htlc,
htlc_direction=direction, htlc_direction=direction,
ctx_output_idx=ctx_output_idx) ctx_output_idx=ctx_output_idx)
@@ -114,7 +114,7 @@ def create_sweeptxs_for_watchtower(chan: 'Channel', ctx: Transaction, per_commit
return txs return txs
def create_sweeptx_for_their_revoked_ctx( def tx_their_ctx_justice(
chan: 'Channel', chan: 'Channel',
ctx: Transaction, ctx: Transaction,
per_commitment_secret: bytes, per_commitment_secret: bytes,
@@ -135,7 +135,7 @@ def create_sweeptx_for_their_revoked_ctx(
output_idxs = ctx.get_output_idxs_from_address(to_local_address) output_idxs = ctx.get_output_idxs_from_address(to_local_address)
if output_idxs: if output_idxs:
output_idx = output_idxs.pop() output_idx = output_idxs.pop()
sweep_tx = lambda: create_sweeptx_ctx_to_local( sweep_tx = lambda: tx_ctx_to_local(
sweep_address=sweep_address, sweep_address=sweep_address,
ctx=ctx, ctx=ctx,
output_idx=output_idx, output_idx=output_idx,
@@ -147,7 +147,7 @@ def create_sweeptx_for_their_revoked_ctx(
return None return None
def create_sweeptx_for_their_revoked_htlc( def tx_their_htlctx_justice(
chan: 'Channel', chan: 'Channel',
ctx: Transaction, ctx: Transaction,
htlc_tx: Transaction, htlc_tx: Transaction,
@@ -175,7 +175,7 @@ def create_sweeptx_for_their_revoked_htlc(
# check that htlc_tx is a htlc # check that htlc_tx is a htlc
if htlc_tx.outputs()[0].address != htlc_address: if htlc_tx.outputs()[0].address != htlc_address:
return return
gen_tx = lambda: create_sweeptx_that_spends_htlctx_that_spends_htlc_in_ctx( gen_tx = lambda: tx_sweep_our_htlctx(
sweep_address=sweep_address, sweep_address=sweep_address,
htlc_tx=htlc_tx, htlc_tx=htlc_tx,
htlctx_witness_script=witness_script, htlctx_witness_script=witness_script,
@@ -189,7 +189,7 @@ def create_sweeptx_for_their_revoked_htlc(
gen_tx=gen_tx) gen_tx=gen_tx)
def create_sweeptxs_for_our_ctx( def txs_our_ctx(
*, chan: 'AbstractChannel', *, chan: 'AbstractChannel',
ctx: Transaction, ctx: Transaction,
sweep_address: str) -> Optional[Dict[str, SweepInfo]]: sweep_address: str) -> Optional[Dict[str, SweepInfo]]:
@@ -243,7 +243,7 @@ def create_sweeptxs_for_our_ctx(
output_idxs = ctx.get_output_idxs_from_address(to_local_address) output_idxs = ctx.get_output_idxs_from_address(to_local_address)
if output_idxs: if output_idxs:
output_idx = output_idxs.pop() output_idx = output_idxs.pop()
sweep_tx = lambda: create_sweeptx_ctx_to_local( sweep_tx = lambda: tx_ctx_to_local(
sweep_address=sweep_address, sweep_address=sweep_address,
ctx=ctx, ctx=ctx,
output_idx=output_idx, output_idx=output_idx,
@@ -265,13 +265,13 @@ def create_sweeptxs_for_our_ctx(
return txs return txs
# HTLCs # HTLCs
def create_txns_for_htlc( def txs_htlc(
*, htlc: 'UpdateAddHtlc', *, htlc: 'UpdateAddHtlc',
htlc_direction: Direction, htlc_direction: Direction,
ctx_output_idx: int, ctx_output_idx: int,
htlc_relative_idx: int, htlc_relative_idx,
preimage: Optional[bytes]): preimage: Optional[bytes]):
htlctx_witness_script, htlc_tx = create_htlctx_that_spends_from_our_ctx( htlctx_witness_script, htlc_tx = tx_our_ctx_htlctx(
chan=chan, chan=chan,
our_pcp=our_pcp, our_pcp=our_pcp,
ctx=ctx, ctx=ctx,
@@ -281,7 +281,7 @@ def create_sweeptxs_for_our_ctx(
htlc_direction=htlc_direction, htlc_direction=htlc_direction,
ctx_output_idx=ctx_output_idx, ctx_output_idx=ctx_output_idx,
htlc_relative_idx=htlc_relative_idx) htlc_relative_idx=htlc_relative_idx)
sweep_tx = lambda: create_sweeptx_that_spends_htlctx_that_spends_htlc_in_ctx( sweep_tx = lambda: tx_sweep_our_htlctx(
to_self_delay=to_self_delay, to_self_delay=to_self_delay,
htlc_tx=htlc_tx, htlc_tx=htlc_tx,
htlctx_witness_script=htlctx_witness_script, htlctx_witness_script=htlctx_witness_script,
@@ -321,7 +321,7 @@ def create_sweeptxs_for_our_ctx(
else: else:
preimage = None preimage = None
try: try:
create_txns_for_htlc( txs_htlc(
htlc=htlc, htlc=htlc,
htlc_direction=direction, htlc_direction=direction,
ctx_output_idx=ctx_output_idx, ctx_output_idx=ctx_output_idx,
@@ -374,10 +374,10 @@ def extract_funding_pubkeys_from_ctx(txin: TxInput) -> Tuple[bytes, bytes]:
return (pubkey1, pubkey2) return (pubkey1, pubkey2)
def create_sweeptx_their_backup_ctx( def tx_their_ctx_to_remote_backup(
*, chan: 'ChannelBackup', *, chan: 'ChannelBackup',
ctx: Transaction, ctx: Transaction,
sweep_address: str) -> Optional[Dict[str, SweepInfo]]: sweep_address: str) -> Optional[Dict[str, SweepInfo]]:
txs = {} # type: Dict[str, SweepInfo] txs = {} # type: Dict[str, SweepInfo]
"""If we only have a backup, and the remote force-closed with their ctx, """If we only have a backup, and the remote force-closed with their ctx,
and anchors are enabled, we need to sweep to_remote.""" and anchors are enabled, we need to sweep to_remote."""
@@ -408,7 +408,7 @@ def create_sweeptx_their_backup_ctx(
if output_idxs: if output_idxs:
output_idx = output_idxs.pop() output_idx = output_idxs.pop()
prevout = ctx.txid() + ':%d' % output_idx prevout = ctx.txid() + ':%d' % output_idx
sweep_tx = lambda: create_sweeptx_their_ctx_to_remote( sweep_tx = lambda: tx_their_ctx_to_remote(
sweep_address=sweep_address, sweep_address=sweep_address,
ctx=ctx, ctx=ctx,
output_idx=output_idx, output_idx=output_idx,
@@ -424,7 +424,9 @@ def create_sweeptx_their_backup_ctx(
return txs return txs
def create_sweeptxs_for_their_ctx(
def txs_their_ctx(
*, chan: 'Channel', *, chan: 'Channel',
ctx: Transaction, ctx: Transaction,
sweep_address: str) -> Optional[Dict[str, SweepInfo]]: sweep_address: str) -> Optional[Dict[str, SweepInfo]]:
@@ -466,10 +468,11 @@ def create_sweeptxs_for_their_ctx(
if not found_to_local and not found_to_remote: if not found_to_local and not found_to_remote:
return return
chan.logger.debug(f'(lnsweep) found their ctx: {to_local_address} {to_remote_address}') chan.logger.debug(f'(lnsweep) found their ctx: {to_local_address} {to_remote_address}')
# to_local is handled by lnwatcher # to_local is handled by lnwatcher
if is_revocation: if is_revocation:
our_revocation_privkey = derive_blinded_privkey(our_conf.revocation_basepoint.privkey, per_commitment_secret) our_revocation_privkey = derive_blinded_privkey(our_conf.revocation_basepoint.privkey, per_commitment_secret)
gen_tx = create_sweeptx_for_their_revoked_ctx(chan, ctx, per_commitment_secret, sweep_address) gen_tx = tx_their_ctx_justice(chan, ctx, per_commitment_secret, sweep_address)
if gen_tx: if gen_tx:
tx = gen_tx() tx = gen_tx()
txs[tx.inputs()[0].prevout.to_str()] = SweepInfo( txs[tx.inputs()[0].prevout.to_str()] = SweepInfo(
@@ -478,6 +481,7 @@ def create_sweeptxs_for_their_ctx(
cltv_abs=0, cltv_abs=0,
gen_tx=gen_tx) gen_tx=gen_tx)
# to_remote # to_remote
if chan.has_anchors(): if chan.has_anchors():
csv_delay = 1 csv_delay = 1
@@ -495,7 +499,7 @@ def create_sweeptxs_for_their_ctx(
if output_idxs: if output_idxs:
output_idx = output_idxs.pop() output_idx = output_idxs.pop()
prevout = ctx.txid() + ':%d' % output_idx prevout = ctx.txid() + ':%d' % output_idx
sweep_tx = lambda: create_sweeptx_their_ctx_to_remote( sweep_tx = lambda: tx_their_ctx_to_remote(
sweep_address=sweep_address, sweep_address=sweep_address,
ctx=ctx, ctx=ctx,
output_idx=output_idx, output_idx=output_idx,
@@ -513,7 +517,7 @@ def create_sweeptxs_for_their_ctx(
our_htlc_privkey = derive_privkey(secret=int.from_bytes(our_conf.htlc_basepoint.privkey, 'big'), per_commitment_point=their_pcp) our_htlc_privkey = derive_privkey(secret=int.from_bytes(our_conf.htlc_basepoint.privkey, 'big'), per_commitment_point=their_pcp)
our_htlc_privkey = ecc.ECPrivkey.from_secret_scalar(our_htlc_privkey) our_htlc_privkey = ecc.ECPrivkey.from_secret_scalar(our_htlc_privkey)
their_htlc_pubkey = derive_pubkey(their_conf.htlc_basepoint.pubkey, their_pcp) their_htlc_pubkey = derive_pubkey(their_conf.htlc_basepoint.pubkey, their_pcp)
def create_sweeptx_for_htlc( def tx_htlc(
*, htlc: 'UpdateAddHtlc', *, htlc: 'UpdateAddHtlc',
is_received_htlc: bool, is_received_htlc: bool,
ctx_output_idx: int, ctx_output_idx: int,
@@ -530,7 +534,7 @@ def create_sweeptxs_for_their_ctx(
cltv_abs = htlc.cltv_abs if is_received_htlc and not is_revocation else 0 cltv_abs = htlc.cltv_abs if is_received_htlc and not is_revocation else 0
csv_delay = 1 if chan.has_anchors() else 0 csv_delay = 1 if chan.has_anchors() else 0
prevout = ctx.txid() + ':%d'%ctx_output_idx prevout = ctx.txid() + ':%d'%ctx_output_idx
sweep_tx = lambda: create_sweeptx_their_ctx_htlc( sweep_tx = lambda: tx_their_ctx_htlc(
ctx=ctx, ctx=ctx,
witness_script=htlc_output_witness_script, witness_script=htlc_output_witness_script,
sweep_address=sweep_address, sweep_address=sweep_address,
@@ -567,7 +571,7 @@ def create_sweeptxs_for_their_ctx(
continue continue
else: else:
preimage = None preimage = None
create_sweeptx_for_htlc( tx_htlc(
htlc=htlc, htlc=htlc,
is_received_htlc=is_received_htlc, is_received_htlc=is_received_htlc,
ctx_output_idx=ctx_output_idx, ctx_output_idx=ctx_output_idx,
@@ -575,7 +579,7 @@ def create_sweeptxs_for_their_ctx(
return txs return txs
def create_htlctx_that_spends_from_our_ctx( def tx_our_ctx_htlctx(
chan: 'Channel', chan: 'Channel',
our_pcp: bytes, our_pcp: bytes,
ctx: Transaction, ctx: Transaction,
@@ -681,7 +685,7 @@ def create_htlctx_that_spends_from_our_ctx(
return witness_script_out, final_htlc_tx return witness_script_out, final_htlc_tx
def create_sweeptx_their_ctx_htlc( def tx_their_ctx_htlc(
ctx: Transaction, witness_script: bytes, sweep_address: str, ctx: Transaction, witness_script: bytes, sweep_address: str,
preimage: Optional[bytes], output_idx: int, preimage: Optional[bytes], output_idx: int,
privkey: bytes, is_revocation: bool, privkey: bytes, is_revocation: bool,
@@ -717,7 +721,8 @@ def create_sweeptx_their_ctx_htlc(
return tx return tx
def create_sweeptx_their_ctx_to_remote(
def tx_their_ctx_to_remote(
sweep_address: str, ctx: Transaction, output_idx: int, sweep_address: str, ctx: Transaction, output_idx: int,
our_payment_privkey: ecc.ECPrivkey, our_payment_privkey: ecc.ECPrivkey,
config: SimpleConfig, config: SimpleConfig,
@@ -759,7 +764,10 @@ def create_sweeptx_their_ctx_to_remote(
return sweep_tx return sweep_tx
def create_sweeptx_ctx_to_local(
def tx_ctx_to_local(
*, sweep_address: str, ctx: Transaction, output_idx: int, witness_script: bytes, *, sweep_address: str, ctx: Transaction, output_idx: int, witness_script: bytes,
privkey: bytes, is_revocation: bool, config: SimpleConfig, privkey: bytes, is_revocation: bool, config: SimpleConfig,
to_self_delay: int = None) -> Optional[PartialTransaction]: to_self_delay: int = None) -> Optional[PartialTransaction]:
@@ -792,16 +800,17 @@ def create_sweeptx_ctx_to_local(
return sweep_tx return sweep_tx
def create_sweeptx_that_spends_htlctx_that_spends_htlc_in_ctx(
def tx_sweep_our_htlctx(
*, htlc_tx: Transaction, htlctx_witness_script: bytes, sweep_address: str, *, htlc_tx: Transaction, htlctx_witness_script: bytes, sweep_address: str,
privkey: bytes, is_revocation: bool, to_self_delay: int = None, privkey: bytes, is_revocation: bool, to_self_delay: int = None,
config: SimpleConfig) -> Optional[PartialTransaction]: config: SimpleConfig) -> Optional[PartialTransaction]:
"""Create a txn that sweeps the output of a second stage htlc tx """Create a txn that sweeps the output of a first stage htlc tx
(i.e. sweeps from an HTLC-Timeout or an HTLC-Success tx). (i.e. sweeps from an HTLC-Timeout or an HTLC-Success tx).
""" """
# note: this is the same as sweeping the to_local output of the ctx, # note: this is the same as sweeping the to_local output of the ctx,
# as these are the same script (address-reuse). # as these are the same script (address-reuse).
return create_sweeptx_ctx_to_local( return tx_ctx_to_local(
sweep_address=sweep_address, sweep_address=sweep_address,
ctx=htlc_tx, ctx=htlc_tx,
output_idx=0, output_idx=0,