Browse Source

(formatting) lnsweep indentation

patch-4
ThomasV 4 years ago
parent
commit
e186214f6f
  1. 245
      electrum/lnsweep.py

245
electrum/lnsweep.py

@ -70,14 +70,15 @@ def create_sweeptxs_for_watchtower(chan: 'Channel', ctx: Transaction, per_commit
# HTLCs # HTLCs
def create_sweeptx_for_htlc(*, htlc: 'UpdateAddHtlc', htlc_direction: Direction, def create_sweeptx_for_htlc(*, 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(chan=chan, htlc_tx_witness_script, htlc_tx = make_htlc_tx_with_open_channel(
pcp=pcp, chan=chan,
subject=REMOTE, pcp=pcp,
ctn=ctn, subject=REMOTE,
htlc_direction=htlc_direction, ctn=ctn,
commit=ctx, htlc_direction=htlc_direction,
htlc=htlc, commit=ctx,
ctx_output_idx=ctx_output_idx) htlc=htlc,
ctx_output_idx=ctx_output_idx)
return create_sweeptx_that_spends_htlctx_that_spends_htlc_in_ctx( return create_sweeptx_that_spends_htlctx_that_spends_htlc_in_ctx(
to_self_delay=0, to_self_delay=0,
htlc_tx=htlc_tx, htlc_tx=htlc_tx,
@ -87,22 +88,27 @@ def create_sweeptxs_for_watchtower(chan: 'Channel', ctx: Transaction, per_commit
is_revocation=True, is_revocation=True,
config=chan.lnworker.config) config=chan.lnworker.config)
htlc_to_ctx_output_idx_map = map_htlcs_to_ctx_output_idxs(chan=chan, htlc_to_ctx_output_idx_map = map_htlcs_to_ctx_output_idxs(
ctx=ctx, chan=chan,
pcp=pcp, ctx=ctx,
subject=REMOTE, pcp=pcp,
ctn=ctn) subject=REMOTE,
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(htlc=htlc, secondstage_sweep_tx = create_sweeptx_for_htlc(
htlc_direction=direction, htlc=htlc,
ctx_output_idx=ctx_output_idx) htlc_direction=direction,
ctx_output_idx=ctx_output_idx)
if secondstage_sweep_tx: if secondstage_sweep_tx:
txs.append(secondstage_sweep_tx) txs.append(secondstage_sweep_tx)
return txs return txs
def create_sweeptx_for_their_revoked_ctx(chan: 'Channel', ctx: Transaction, per_commitment_secret: bytes, def create_sweeptx_for_their_revoked_ctx(
sweep_address: str) -> Optional[Callable[[], Optional[Transaction]]]: chan: 'Channel',
ctx: Transaction,
per_commitment_secret: bytes,
sweep_address: str) -> Optional[Callable[[], Optional[Transaction]]]:
# prep # prep
pcp = ecc.ECPrivkey(per_commitment_secret).get_public_key_bytes(compressed=True) pcp = ecc.ECPrivkey(per_commitment_secret).get_public_key_bytes(compressed=True)
this_conf, other_conf = get_ordered_channel_configs(chan=chan, for_us=False) this_conf, other_conf = get_ordered_channel_configs(chan=chan, for_us=False)
@ -130,8 +136,13 @@ def create_sweeptx_for_their_revoked_ctx(chan: 'Channel', ctx: Transaction, per_
return sweep_tx return sweep_tx
return None return None
def create_sweeptx_for_their_revoked_htlc(chan: 'Channel', ctx: Transaction, htlc_tx: Transaction,
sweep_address: str) -> Optional[SweepInfo]: def create_sweeptx_for_their_revoked_htlc(
chan: 'Channel',
ctx: Transaction,
htlc_tx: Transaction,
sweep_address: str) -> Optional[SweepInfo]:
x = analyze_ctx(chan, ctx) x = analyze_ctx(chan, ctx)
if not x: if not x:
return return
@ -141,8 +152,9 @@ def create_sweeptx_for_their_revoked_htlc(chan: 'Channel', ctx: Transaction, htl
# prep # prep
pcp = ecc.ECPrivkey(per_commitment_secret).get_public_key_bytes(compressed=True) pcp = ecc.ECPrivkey(per_commitment_secret).get_public_key_bytes(compressed=True)
this_conf, other_conf = get_ordered_channel_configs(chan=chan, for_us=False) this_conf, other_conf = get_ordered_channel_configs(chan=chan, for_us=False)
other_revocation_privkey = derive_blinded_privkey(other_conf.revocation_basepoint.privkey, other_revocation_privkey = derive_blinded_privkey(
per_commitment_secret) other_conf.revocation_basepoint.privkey,
per_commitment_secret)
to_self_delay = other_conf.to_self_delay to_self_delay = other_conf.to_self_delay
this_delayed_pubkey = derive_pubkey(this_conf.delayed_basepoint.pubkey, pcp) this_delayed_pubkey = derive_pubkey(this_conf.delayed_basepoint.pubkey, pcp)
# same witness script as to_local # same witness script as to_local
@ -153,7 +165,6 @@ def create_sweeptx_for_their_revoked_htlc(chan: 'Channel', ctx: Transaction, htl
# 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_ctx_to_local( gen_tx = lambda: create_sweeptx_ctx_to_local(
sweep_address=sweep_address, sweep_address=sweep_address,
ctx=htlc_tx, ctx=htlc_tx,
@ -162,16 +173,17 @@ def create_sweeptx_for_their_revoked_htlc(chan: 'Channel', ctx: Transaction, htl
privkey=other_revocation_privkey, privkey=other_revocation_privkey,
is_revocation=True, is_revocation=True,
config=chan.lnworker.config) config=chan.lnworker.config)
return SweepInfo(
name='redeem_htlc2',
csv_delay=0,
cltv_expiry=0,
gen_tx=gen_tx)
return SweepInfo(name='redeem_htlc2',
csv_delay=0,
cltv_expiry=0,
gen_tx=gen_tx)
def create_sweeptxs_for_our_ctx(
*, chan: 'AbstractChannel',
def create_sweeptxs_for_our_ctx(*, chan: 'AbstractChannel', ctx: Transaction, ctx: Transaction,
sweep_address: str) -> Optional[Dict[str, SweepInfo]]: sweep_address: str) -> Optional[Dict[str, SweepInfo]]:
"""Handle the case where we force close unilaterally with our latest ctx. """Handle the case where we force close unilaterally with our latest ctx.
Construct sweep txns for 'to_local', and for all HTLCs (2 txns each). Construct sweep txns for 'to_local', and for all HTLCs (2 txns each).
'to_local' can be swept even if this is a breach (by us), 'to_local' can be swept even if this is a breach (by us),
@ -221,11 +233,11 @@ def create_sweeptxs_for_our_ctx(*, chan: 'AbstractChannel', ctx: Transaction,
to_self_delay=to_self_delay, to_self_delay=to_self_delay,
config=chan.lnworker.config) config=chan.lnworker.config)
prevout = ctx.txid() + ':%d'%output_idx prevout = ctx.txid() + ':%d'%output_idx
txs[prevout] = SweepInfo(name='our_ctx_to_local', txs[prevout] = SweepInfo(
csv_delay=to_self_delay, name='our_ctx_to_local',
cltv_expiry=0, csv_delay=to_self_delay,
gen_tx=sweep_tx) cltv_expiry=0,
gen_tx=sweep_tx)
we_breached = ctn < chan.get_oldest_unrevoked_ctn(LOCAL) we_breached = ctn < chan.get_oldest_unrevoked_ctn(LOCAL)
if we_breached: if we_breached:
_logger.info("we breached.") _logger.info("we breached.")
@ -233,8 +245,9 @@ def create_sweeptxs_for_our_ctx(*, chan: 'AbstractChannel', ctx: Transaction,
return txs return txs
# HTLCs # HTLCs
def create_txns_for_htlc(*, htlc: 'UpdateAddHtlc', htlc_direction: Direction, def create_txns_for_htlc(
ctx_output_idx: int, htlc_relative_idx: int): *, htlc: 'UpdateAddHtlc', htlc_direction: Direction,
ctx_output_idx: int, htlc_relative_idx: int):
if htlc_direction == RECEIVED: if htlc_direction == RECEIVED:
preimage = chan.lnworker.get_preimage(htlc.payment_hash) preimage = chan.lnworker.get_preimage(htlc.payment_hash)
else: else:
@ -258,29 +271,34 @@ def create_sweeptxs_for_our_ctx(*, chan: 'AbstractChannel', ctx: Transaction,
is_revocation=False, is_revocation=False,
config=chan.lnworker.config) config=chan.lnworker.config)
# side effect # side effect
txs[htlc_tx.inputs()[0].prevout.to_str()] = SweepInfo(name='first-stage-htlc', txs[htlc_tx.inputs()[0].prevout.to_str()] = SweepInfo(
csv_delay=0, name='first-stage-htlc',
cltv_expiry=htlc_tx.locktime, csv_delay=0,
gen_tx=lambda: htlc_tx) cltv_expiry=htlc_tx.locktime,
txs[htlc_tx.txid() + ':0'] = SweepInfo(name='second-stage-htlc', gen_tx=lambda: htlc_tx)
csv_delay=to_self_delay, txs[htlc_tx.txid() + ':0'] = SweepInfo(
cltv_expiry=0, name='second-stage-htlc',
gen_tx=sweep_tx) csv_delay=to_self_delay,
cltv_expiry=0,
gen_tx=sweep_tx)
# offered HTLCs, in our ctx --> "timeout" # offered HTLCs, in our ctx --> "timeout"
# received HTLCs, in our ctx --> "success" # received HTLCs, in our ctx --> "success"
htlc_to_ctx_output_idx_map = map_htlcs_to_ctx_output_idxs(chan=chan, htlc_to_ctx_output_idx_map = map_htlcs_to_ctx_output_idxs(
ctx=ctx, chan=chan,
pcp=our_pcp, ctx=ctx,
subject=LOCAL, pcp=our_pcp,
ctn=ctn) subject=LOCAL,
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():
create_txns_for_htlc(htlc=htlc, create_txns_for_htlc(
htlc_direction=direction, htlc=htlc,
ctx_output_idx=ctx_output_idx, htlc_direction=direction,
htlc_relative_idx=htlc_relative_idx) ctx_output_idx=ctx_output_idx,
htlc_relative_idx=htlc_relative_idx)
return txs return txs
def analyze_ctx(chan: 'Channel', ctx: Transaction): def analyze_ctx(chan: 'Channel', ctx: Transaction):
# note: the remote sometimes has two valid non-revoked commitment transactions, # note: the remote sometimes has two valid non-revoked commitment transactions,
# either of which could be broadcast # either of which could be broadcast
@ -309,8 +327,11 @@ def analyze_ctx(chan: 'Channel', ctx: Transaction):
return return
return ctn, their_pcp, is_revocation, per_commitment_secret return ctn, their_pcp, is_revocation, per_commitment_secret
def create_sweeptxs_for_their_ctx(*, chan: 'Channel', ctx: Transaction,
sweep_address: str) -> Optional[Dict[str,SweepInfo]]: def create_sweeptxs_for_their_ctx(
*, chan: 'Channel',
ctx: Transaction,
sweep_address: str) -> Optional[Dict[str,SweepInfo]]:
"""Handle the case when the remote force-closes with their ctx. """Handle the case when the remote force-closes with their ctx.
Sweep outputs that do not have a CSV delay ('to_remote' and first-stage HTLCs). Sweep outputs that do not have a CSV delay ('to_remote' and first-stage HTLCs).
Outputs with CSV delay ('to_local' and second-stage HTLCs) are redeemed by LNWatcher. Outputs with CSV delay ('to_local' and second-stage HTLCs) are redeemed by LNWatcher.
@ -336,16 +357,16 @@ def create_sweeptxs_for_their_ctx(*, chan: 'Channel', ctx: Transaction,
if not ctx.get_output_idxs_from_address(to_local_address) \ if not ctx.get_output_idxs_from_address(to_local_address) \
and not ctx.get_output_idxs_from_address(to_remote_address): and not ctx.get_output_idxs_from_address(to_remote_address):
return return
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, chan.sweep_address) gen_tx = create_sweeptx_for_their_revoked_ctx(chan, ctx, per_commitment_secret, chan.sweep_address)
if gen_tx: if gen_tx:
tx = gen_tx() tx = gen_tx()
txs[tx.inputs()[0].prevout.to_str()] = SweepInfo(name='to_local_for_revoked_ctx', txs[tx.inputs()[0].prevout.to_str()] = SweepInfo(
csv_delay=0, name='to_local_for_revoked_ctx',
cltv_expiry=0, csv_delay=0,
gen_tx=gen_tx) cltv_expiry=0,
gen_tx=gen_tx)
# prep # prep
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)
@ -367,13 +388,15 @@ def create_sweeptxs_for_their_ctx(*, chan: 'Channel', ctx: Transaction,
output_idx=output_idx, output_idx=output_idx,
our_payment_privkey=our_payment_privkey, our_payment_privkey=our_payment_privkey,
config=chan.lnworker.config) config=chan.lnworker.config)
txs[prevout] = SweepInfo(name='their_ctx_to_remote', txs[prevout] = SweepInfo(
csv_delay=0, name='their_ctx_to_remote',
cltv_expiry=0, csv_delay=0,
gen_tx=sweep_tx) cltv_expiry=0,
gen_tx=sweep_tx)
# HTLCs # HTLCs
def create_sweeptx_for_htlc(htlc: 'UpdateAddHtlc', is_received_htlc: bool, def create_sweeptx_for_htlc(
ctx_output_idx: int) -> None: htlc: 'UpdateAddHtlc', is_received_htlc: bool,
ctx_output_idx: int) -> None:
if not is_received_htlc and not is_revocation: if not is_received_htlc and not is_revocation:
preimage = chan.lnworker.get_preimage(htlc.payment_hash) preimage = chan.lnworker.get_preimage(htlc.payment_hash)
else: else:
@ -398,42 +421,46 @@ def create_sweeptxs_for_their_ctx(*, chan: 'Channel', ctx: Transaction,
is_revocation=is_revocation, is_revocation=is_revocation,
cltv_expiry=cltv_expiry, cltv_expiry=cltv_expiry,
config=chan.lnworker.config) config=chan.lnworker.config)
txs[prevout] = SweepInfo(name=f'their_ctx_htlc_{ctx_output_idx}', txs[prevout] = SweepInfo(
csv_delay=0, name=f'their_ctx_htlc_{ctx_output_idx}',
cltv_expiry=cltv_expiry, csv_delay=0,
gen_tx=sweep_tx) cltv_expiry=cltv_expiry,
gen_tx=sweep_tx)
# received HTLCs, in their ctx --> "timeout" # received HTLCs, in their ctx --> "timeout"
# offered HTLCs, in their ctx --> "success" # offered HTLCs, in their ctx --> "success"
htlc_to_ctx_output_idx_map = map_htlcs_to_ctx_output_idxs(chan=chan, htlc_to_ctx_output_idx_map = map_htlcs_to_ctx_output_idxs(
ctx=ctx, chan=chan,
pcp=their_pcp, ctx=ctx,
subject=REMOTE, pcp=their_pcp,
ctn=ctn) subject=REMOTE,
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():
create_sweeptx_for_htlc(htlc=htlc, create_sweeptx_for_htlc(
is_received_htlc=direction == RECEIVED, htlc=htlc,
ctx_output_idx=ctx_output_idx) is_received_htlc=direction == RECEIVED,
ctx_output_idx=ctx_output_idx)
return txs return txs
def create_htlctx_that_spends_from_our_ctx(chan: 'Channel', our_pcp: bytes, def create_htlctx_that_spends_from_our_ctx(
ctx: Transaction, htlc: 'UpdateAddHtlc', chan: 'Channel', our_pcp: bytes,
local_htlc_privkey: bytes, preimage: Optional[bytes], ctx: Transaction, htlc: 'UpdateAddHtlc',
htlc_direction: Direction, htlc_relative_idx: int, local_htlc_privkey: bytes, preimage: Optional[bytes],
ctx_output_idx: int) -> Tuple[bytes, Transaction]: htlc_direction: Direction, htlc_relative_idx: int,
ctx_output_idx: int) -> Tuple[bytes, Transaction]:
assert (htlc_direction == RECEIVED) == bool(preimage), 'preimage is required iff htlc is received' assert (htlc_direction == RECEIVED) == bool(preimage), 'preimage is required iff htlc is received'
preimage = preimage or b'' preimage = preimage or b''
ctn = extract_ctn_from_tx_and_chan(ctx, chan) ctn = extract_ctn_from_tx_and_chan(ctx, chan)
witness_script, htlc_tx = make_htlc_tx_with_open_channel(chan=chan, witness_script, htlc_tx = make_htlc_tx_with_open_channel(
pcp=our_pcp, chan=chan,
subject=LOCAL, pcp=our_pcp,
ctn=ctn, subject=LOCAL,
htlc_direction=htlc_direction, ctn=ctn,
commit=ctx, htlc_direction=htlc_direction,
htlc=htlc, commit=ctx,
ctx_output_idx=ctx_output_idx, htlc=htlc,
name=f'our_ctx_{ctx_output_idx}_htlc_tx_{bh2u(htlc.payment_hash)}') ctx_output_idx=ctx_output_idx,
name=f'our_ctx_{ctx_output_idx}_htlc_tx_{bh2u(htlc.payment_hash)}')
remote_htlc_sig = chan.get_remote_htlc_sig_for_htlc(htlc_relative_idx=htlc_relative_idx) remote_htlc_sig = chan.get_remote_htlc_sig_for_htlc(htlc_relative_idx=htlc_relative_idx)
local_htlc_sig = bfh(htlc_tx.sign_txin(0, local_htlc_privkey)) local_htlc_sig = bfh(htlc_tx.sign_txin(0, local_htlc_privkey))
txin = htlc_tx.inputs()[0] txin = htlc_tx.inputs()[0]
@ -442,10 +469,11 @@ def create_htlctx_that_spends_from_our_ctx(chan: 'Channel', our_pcp: bytes,
return witness_script, htlc_tx return witness_script, htlc_tx
def create_sweeptx_their_ctx_htlc(ctx: Transaction, witness_script: bytes, sweep_address: str, def create_sweeptx_their_ctx_htlc(
preimage: Optional[bytes], output_idx: int, ctx: Transaction, witness_script: bytes, sweep_address: str,
privkey: bytes, is_revocation: bool, preimage: Optional[bytes], output_idx: int,
cltv_expiry: int, config: SimpleConfig) -> Optional[PartialTransaction]: privkey: bytes, is_revocation: bool,
cltv_expiry: int, config: SimpleConfig) -> Optional[PartialTransaction]:
assert type(cltv_expiry) is int assert type(cltv_expiry) is int
preimage = preimage or b'' # preimage is required iff (not is_revocation and htlc is offered) preimage = preimage or b'' # preimage is required iff (not is_revocation and htlc is offered)
val = ctx.outputs()[output_idx].value val = ctx.outputs()[output_idx].value
@ -472,9 +500,10 @@ def create_sweeptx_their_ctx_htlc(ctx: Transaction, witness_script: bytes, sweep
return tx return tx
def create_sweeptx_their_ctx_to_remote(sweep_address: str, ctx: Transaction, output_idx: int, def create_sweeptx_their_ctx_to_remote(
our_payment_privkey: ecc.ECPrivkey, sweep_address: str, ctx: Transaction, output_idx: int,
config: SimpleConfig) -> Optional[PartialTransaction]: our_payment_privkey: ecc.ECPrivkey,
config: SimpleConfig) -> Optional[PartialTransaction]:
our_payment_pubkey = our_payment_privkey.get_public_key_hex(compressed=True) our_payment_pubkey = our_payment_privkey.get_public_key_hex(compressed=True)
val = ctx.outputs()[output_idx].value val = ctx.outputs()[output_idx].value
prevout = TxOutpoint(txid=bfh(ctx.txid()), out_idx=output_idx) prevout = TxOutpoint(txid=bfh(ctx.txid()), out_idx=output_idx)
@ -497,9 +526,10 @@ def create_sweeptx_their_ctx_to_remote(sweep_address: str, ctx: Transaction, out
return sweep_tx return sweep_tx
def create_sweeptx_ctx_to_local(*, sweep_address: str, ctx: Transaction, output_idx: int, witness_script: str, def create_sweeptx_ctx_to_local(
privkey: bytes, is_revocation: bool, config: SimpleConfig, *, sweep_address: str, ctx: Transaction, output_idx: int, witness_script: str,
to_self_delay: int=None) -> Optional[PartialTransaction]: privkey: bytes, is_revocation: bool, config: SimpleConfig,
to_self_delay: int=None) -> Optional[PartialTransaction]:
"""Create a txn that sweeps the 'to_local' output of a commitment """Create a txn that sweeps the 'to_local' output of a commitment
transaction into our wallet. transaction into our wallet.
@ -529,8 +559,8 @@ def create_sweeptx_ctx_to_local(*, sweep_address: str, ctx: Transaction, output_
return sweep_tx return sweep_tx
def create_sweeptx_that_spends_htlctx_that_spends_htlc_in_ctx(*, def create_sweeptx_that_spends_htlctx_that_spends_htlc_in_ctx(
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, privkey: bytes, is_revocation: bool, to_self_delay: int,
config: SimpleConfig) -> Optional[PartialTransaction]: config: SimpleConfig) -> Optional[PartialTransaction]:
val = htlc_tx.outputs()[0].value val = htlc_tx.outputs()[0].value
@ -549,7 +579,6 @@ def create_sweeptx_that_spends_htlctx_that_spends_htlc_in_ctx(*,
if outvalue <= dust_threshold(): return None if outvalue <= dust_threshold(): return None
sweep_outputs = [PartialTxOutput.from_address_and_value(sweep_address, outvalue)] sweep_outputs = [PartialTxOutput.from_address_and_value(sweep_address, outvalue)]
tx = PartialTransaction.from_io(sweep_inputs, sweep_outputs, version=2) tx = PartialTransaction.from_io(sweep_inputs, sweep_outputs, version=2)
sig = bfh(tx.sign_txin(0, privkey)) sig = bfh(tx.sign_txin(0, privkey))
witness = construct_witness([sig, int(is_revocation), htlctx_witness_script]) witness = construct_witness([sig, int(is_revocation), htlctx_witness_script])
tx.inputs()[0].witness = bfh(witness) tx.inputs()[0].witness = bfh(witness)

Loading…
Cancel
Save