Browse Source

(formatting) lnsweep indentation

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

99
electrum/lnsweep.py

@ -70,7 +70,8 @@ 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(
chan=chan,
pcp=pcp, pcp=pcp,
subject=REMOTE, subject=REMOTE,
ctn=ctn, ctn=ctn,
@ -87,13 +88,15 @@ 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(
chan=chan,
ctx=ctx, ctx=ctx,
pcp=pcp, pcp=pcp,
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(htlc=htlc, secondstage_sweep_tx = create_sweeptx_for_htlc(
htlc=htlc,
htlc_direction=direction, htlc_direction=direction,
ctx_output_idx=ctx_output_idx) ctx_output_idx=ctx_output_idx)
if secondstage_sweep_tx: if secondstage_sweep_tx:
@ -101,7 +104,10 @@ def create_sweeptxs_for_watchtower(chan: 'Channel', ctx: Transaction, per_commit
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(
chan: 'Channel',
ctx: Transaction,
per_commitment_secret: bytes,
sweep_address: str) -> Optional[Callable[[], Optional[Transaction]]]: 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)
@ -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,
def create_sweeptx_for_their_revoked_htlc(
chan: 'Channel',
ctx: Transaction,
htlc_tx: Transaction,
sweep_address: str) -> Optional[SweepInfo]: sweep_address: str) -> Optional[SweepInfo]:
x = analyze_ctx(chan, ctx) x = analyze_ctx(chan, ctx)
if not x: if not x:
return return
@ -141,7 +152,8 @@ 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(
other_conf.revocation_basepoint.privkey,
per_commitment_secret) 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)
@ -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,15 +173,16 @@ 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(
return SweepInfo(name='redeem_htlc2', name='redeem_htlc2',
csv_delay=0, csv_delay=0,
cltv_expiry=0, cltv_expiry=0,
gen_tx=gen_tx) gen_tx=gen_tx)
def create_sweeptxs_for_our_ctx(
def create_sweeptxs_for_our_ctx(*, chan: 'AbstractChannel', ctx: Transaction, *, chan: 'AbstractChannel',
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).
@ -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(
name='our_ctx_to_local',
csv_delay=to_self_delay, csv_delay=to_self_delay,
cltv_expiry=0, cltv_expiry=0,
gen_tx=sweep_tx) 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,7 +245,8 @@ 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(
*, htlc: 'UpdateAddHtlc', htlc_direction: Direction,
ctx_output_idx: int, htlc_relative_idx: int): 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)
@ -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(
name='first-stage-htlc',
csv_delay=0, csv_delay=0,
cltv_expiry=htlc_tx.locktime, cltv_expiry=htlc_tx.locktime,
gen_tx=lambda: htlc_tx) gen_tx=lambda: htlc_tx)
txs[htlc_tx.txid() + ':0'] = SweepInfo(name='second-stage-htlc', txs[htlc_tx.txid() + ':0'] = SweepInfo(
name='second-stage-htlc',
csv_delay=to_self_delay, csv_delay=to_self_delay,
cltv_expiry=0, cltv_expiry=0,
gen_tx=sweep_tx) 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(
chan=chan,
ctx=ctx, ctx=ctx,
pcp=our_pcp, pcp=our_pcp,
subject=LOCAL, subject=LOCAL,
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():
create_txns_for_htlc(htlc=htlc, create_txns_for_htlc(
htlc=htlc,
htlc_direction=direction, htlc_direction=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)
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,7 +327,10 @@ 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,
def create_sweeptxs_for_their_ctx(
*, chan: 'Channel',
ctx: Transaction,
sweep_address: str) -> Optional[Dict[str,SweepInfo]]: 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).
@ -336,13 +357,13 @@ 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(
name='to_local_for_revoked_ctx',
csv_delay=0, csv_delay=0,
cltv_expiry=0, cltv_expiry=0,
gen_tx=gen_tx) gen_tx=gen_tx)
@ -367,12 +388,14 @@ 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(
name='their_ctx_to_remote',
csv_delay=0, csv_delay=0,
cltv_expiry=0, cltv_expiry=0,
gen_tx=sweep_tx) gen_tx=sweep_tx)
# HTLCs # HTLCs
def create_sweeptx_for_htlc(htlc: 'UpdateAddHtlc', is_received_htlc: bool, def create_sweeptx_for_htlc(
htlc: 'UpdateAddHtlc', is_received_htlc: bool,
ctx_output_idx: int) -> None: 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)
@ -398,26 +421,29 @@ 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(
name=f'their_ctx_htlc_{ctx_output_idx}',
csv_delay=0, csv_delay=0,
cltv_expiry=cltv_expiry, cltv_expiry=cltv_expiry,
gen_tx=sweep_tx) 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(
chan=chan,
ctx=ctx, ctx=ctx,
pcp=their_pcp, pcp=their_pcp,
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():
create_sweeptx_for_htlc(htlc=htlc, create_sweeptx_for_htlc(
htlc=htlc,
is_received_htlc=direction == RECEIVED, is_received_htlc=direction == RECEIVED,
ctx_output_idx=ctx_output_idx) 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(
chan: 'Channel', our_pcp: bytes,
ctx: Transaction, htlc: 'UpdateAddHtlc', ctx: Transaction, htlc: 'UpdateAddHtlc',
local_htlc_privkey: bytes, preimage: Optional[bytes], local_htlc_privkey: bytes, preimage: Optional[bytes],
htlc_direction: Direction, htlc_relative_idx: int, htlc_direction: Direction, htlc_relative_idx: int,
@ -425,7 +451,8 @@ def create_htlctx_that_spends_from_our_ctx(chan: 'Channel', our_pcp: bytes,
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(
chan=chan,
pcp=our_pcp, pcp=our_pcp,
subject=LOCAL, subject=LOCAL,
ctn=ctn, ctn=ctn,
@ -442,7 +469,8 @@ 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(
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,
cltv_expiry: int, config: SimpleConfig) -> Optional[PartialTransaction]: cltv_expiry: int, config: SimpleConfig) -> Optional[PartialTransaction]:
@ -472,7 +500,8 @@ 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(
sweep_address: str, ctx: Transaction, output_idx: int,
our_payment_privkey: ecc.ECPrivkey, our_payment_privkey: ecc.ECPrivkey,
config: SimpleConfig) -> Optional[PartialTransaction]: 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)
@ -497,7 +526,8 @@ 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(
*, sweep_address: str, ctx: Transaction, output_idx: int, witness_script: str,
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]:
"""Create a txn that sweeps the 'to_local' output of a commitment """Create a txn that sweeps the 'to_local' output of a commitment
@ -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