Browse Source

transaction: kill "name", "csv_delay", "cltv_expiry" fields

dependabot/pip/contrib/deterministic-build/ecdsa-0.13.3
SomberNight 6 years ago
parent
commit
befa8ea771
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 6
      electrum/lnchannel.py
  2. 70
      electrum/lnsweep.py
  3. 12
      electrum/lnutil.py
  4. 6
      electrum/lnwatcher.py
  5. 31
      electrum/lnworker.py
  6. 6
      electrum/network.py
  7. 7
      electrum/tests/test_lnutil.py
  8. 4
      electrum/tests/test_transaction.py
  9. 14
      electrum/transaction.py

6
electrum/lnchannel.py

@ -49,7 +49,7 @@ from .lnutil import (Outpoint, LocalConfig, RemoteConfig, Keypair, OnlyPubkeyKey
ShortChannelID, map_htlcs_to_ctx_output_idxs)
from .lnutil import FeeUpdate
from .lnsweep import create_sweeptxs_for_our_ctx, create_sweeptxs_for_their_ctx
from .lnsweep import create_sweeptx_for_their_revoked_htlc
from .lnsweep import create_sweeptx_for_their_revoked_htlc, SweepInfo
from .lnhtlc import HTLCManager
@ -146,7 +146,7 @@ class Channel(Logger):
self._is_funding_txo_spent = None # "don't know"
self._state = None
self.set_state('DISCONNECTED')
self.sweep_info = {}
self.sweep_info = {} # type: Dict[str, Dict[str, SweepInfo]]
self._outgoing_channel_update = None # type: Optional[bytes]
def get_feerate(self, subject, ctn):
@ -756,7 +756,7 @@ class Channel(Logger):
assert tx.is_complete()
return tx
def sweep_ctx(self, ctx: Transaction):
def sweep_ctx(self, ctx: Transaction) -> Dict[str, SweepInfo]:
txid = ctx.txid()
if self.sweep_info.get(txid) is None:
our_sweep_info = create_sweeptxs_for_our_ctx(chan=self, ctx=ctx, sweep_address=self.sweep_address)

70
electrum/lnsweep.py

@ -26,6 +26,11 @@ if TYPE_CHECKING:
_logger = get_logger(__name__)
class SweepInfo(NamedTuple):
name: str
csv_delay: int
cltv_expiry: int
gen_tx: Callable[[], Optional[Transaction]]
def create_sweeptxs_for_watchtower(chan: 'Channel', ctx: Transaction, per_commitment_secret: bytes,
@ -70,7 +75,6 @@ def create_sweeptxs_for_watchtower(chan: 'Channel', ctx: Transaction, per_commit
htlc=htlc,
ctx_output_idx=ctx_output_idx)
return create_sweeptx_that_spends_htlctx_that_spends_htlc_in_ctx(
'sweep_from_their_ctx_htlc_',
to_self_delay=0,
htlc_tx=htlc_tx,
htlctx_witness_script=htlc_tx_witness_script,
@ -122,7 +126,7 @@ def create_sweeptx_for_their_revoked_ctx(chan: 'Channel', ctx: Transaction, per_
return None
def create_sweeptx_for_their_revoked_htlc(chan: 'Channel', ctx: Transaction, htlc_tx: Transaction,
sweep_address: str) -> Optional[Tuple[str, int, int, Callable]]:
sweep_address: str) -> Optional[SweepInfo]:
x = analyze_ctx(chan, ctx)
if not x:
return
@ -153,12 +157,15 @@ def create_sweeptx_for_their_revoked_htlc(chan: 'Channel', ctx: Transaction, htl
privkey=other_revocation_privkey,
is_revocation=True)
return 'redeem_htlc2', 0, 0, gen_tx
return SweepInfo(name='redeem_htlc2',
csv_delay=0,
cltv_expiry=0,
gen_tx=gen_tx)
def create_sweeptxs_for_our_ctx(*, chan: 'Channel', ctx: Transaction,
sweep_address: str) -> Optional[Dict[str, Tuple]]:
sweep_address: str) -> Optional[Dict[str, SweepInfo]]:
"""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).
'to_local' can be swept even if this is a breach (by us),
@ -194,7 +201,7 @@ def create_sweeptxs_for_our_ctx(*, chan: 'Channel', ctx: Transaction,
if ctn < chan.get_oldest_unrevoked_ctn(LOCAL):
_logger.info("we breached.")
return {}
txs = {}
txs = {} # type: Dict[str, SweepInfo]
# to_local
output_idxs = ctx.get_output_idxs_from_address(to_local_address)
if output_idxs:
@ -208,7 +215,10 @@ def create_sweeptxs_for_our_ctx(*, chan: 'Channel', ctx: Transaction,
is_revocation=False,
to_self_delay=to_self_delay)
prevout = ctx.txid() + ':%d'%output_idx
txs[prevout] = ('our_ctx_to_local', to_self_delay, 0, sweep_tx)
txs[prevout] = SweepInfo(name='our_ctx_to_local',
csv_delay=to_self_delay,
cltv_expiry=0,
gen_tx=sweep_tx)
# HTLCs
def create_txns_for_htlc(*, htlc: 'UpdateAddHtlc', htlc_direction: Direction,
ctx_output_idx: int, htlc_relative_idx: int):
@ -231,7 +241,6 @@ def create_sweeptxs_for_our_ctx(*, chan: 'Channel', ctx: Transaction,
ctx_output_idx=ctx_output_idx,
htlc_relative_idx=htlc_relative_idx)
sweep_tx = lambda: create_sweeptx_that_spends_htlctx_that_spends_htlc_in_ctx(
'our_ctx_htlc_',
to_self_delay=to_self_delay,
htlc_tx=htlc_tx,
htlctx_witness_script=htlctx_witness_script,
@ -239,8 +248,14 @@ def create_sweeptxs_for_our_ctx(*, chan: 'Channel', ctx: Transaction,
privkey=our_localdelayed_privkey.get_secret_bytes(),
is_revocation=False)
# side effect
txs[htlc_tx.prevout(0)] = ('first-stage-htlc', 0, htlc_tx.cltv_expiry, lambda: htlc_tx)
txs[htlc_tx.txid() + ':0'] = ('second-stage-htlc', to_self_delay, 0, sweep_tx)
txs[htlc_tx.prevout(0)] = SweepInfo(name='first-stage-htlc',
csv_delay=0,
cltv_expiry=htlc_tx.locktime,
gen_tx=lambda: htlc_tx)
txs[htlc_tx.txid() + ':0'] = SweepInfo(name='second-stage-htlc',
csv_delay=to_self_delay,
cltv_expiry=0,
gen_tx=sweep_tx)
# offered HTLCs, in our ctx --> "timeout"
# received HTLCs, in our ctx --> "success"
@ -285,12 +300,12 @@ def analyze_ctx(chan: 'Channel', ctx: Transaction):
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,Tuple]]:
sweep_address: str) -> Optional[Dict[str,SweepInfo]]:
"""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).
Outputs with CSV delay ('to_local' and second-stage HTLCs) are redeemed by LNWatcher.
"""
txs = {}
txs = {} # type: Dict[str, SweepInfo]
our_conf, their_conf = get_ordered_channel_configs(chan=chan, for_us=True)
x = analyze_ctx(chan, ctx)
if not x:
@ -315,7 +330,10 @@ def create_sweeptxs_for_their_ctx(*, chan: 'Channel', ctx: Transaction,
gen_tx = create_sweeptx_for_their_revoked_ctx(chan, ctx, per_commitment_secret, chan.sweep_address)
if gen_tx:
tx = gen_tx()
txs[tx.prevout(0)] = ('to_local_for_revoked_ctx', 0, 0, gen_tx)
txs[tx.prevout(0)] = SweepInfo(name='to_local_for_revoked_ctx',
csv_delay=0,
cltv_expiry=0,
gen_tx=gen_tx)
# prep
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)
@ -335,7 +353,10 @@ def create_sweeptxs_for_their_ctx(*, chan: 'Channel', ctx: Transaction,
ctx=ctx,
output_idx=output_idx,
our_payment_privkey=our_payment_privkey)
txs[prevout] = ('their_ctx_to_remote', 0, 0, sweep_tx)
txs[prevout] = SweepInfo(name='their_ctx_to_remote',
csv_delay=0,
cltv_expiry=0,
gen_tx=sweep_tx)
# HTLCs
def create_sweeptx_for_htlc(htlc: 'UpdateAddHtlc', is_received_htlc: bool,
ctx_output_idx: int) -> None:
@ -366,8 +387,10 @@ def create_sweeptxs_for_their_ctx(*, chan: 'Channel', ctx: Transaction,
privkey=our_revocation_privkey if is_revocation else our_htlc_privkey.get_secret_bytes(),
is_revocation=is_revocation,
cltv_expiry=cltv_expiry)
name = f'their_ctx_htlc_{ctx_output_idx}'
txs[prevout] = (name, 0, cltv_expiry, sweep_tx)
txs[prevout] = SweepInfo(name=f'their_ctx_htlc_{ctx_output_idx}',
csv_delay=0,
cltv_expiry=cltv_expiry,
gen_tx=sweep_tx)
# received HTLCs, in their ctx --> "timeout"
# offered HTLCs, in their ctx --> "success"
@ -429,10 +452,7 @@ def create_sweeptx_their_ctx_htlc(ctx: Transaction, witness_script: bytes, sweep
outvalue = val - fee
if outvalue <= dust_threshold(): return None
sweep_outputs = [TxOutput(TYPE_ADDRESS, sweep_address, outvalue)]
tx = Transaction.from_io(sweep_inputs, sweep_outputs, version=2
, name=f'their_ctx_htlc_{output_idx}'
# note that cltv_expiry, and therefore also locktime will be zero when breach!
, cltv_expiry=cltv_expiry, locktime=cltv_expiry)
tx = Transaction.from_io(sweep_inputs, sweep_outputs, version=2, locktime=cltv_expiry)
sig = bfh(tx.sign_txin(0, privkey))
if not is_revocation:
witness = construct_witness([sig, preimage, witness_script])
@ -463,7 +483,7 @@ def create_sweeptx_their_ctx_to_remote(sweep_address: str, ctx: Transaction, out
outvalue = val - fee
if outvalue <= dust_threshold(): return None
sweep_outputs = [TxOutput(TYPE_ADDRESS, sweep_address, outvalue)]
sweep_tx = Transaction.from_io(sweep_inputs, sweep_outputs, name='their_ctx_to_remote')
sweep_tx = Transaction.from_io(sweep_inputs, sweep_outputs)
sweep_tx.set_rbf(True)
sweep_tx.sign({our_payment_pubkey: (our_payment_privkey.get_secret_bytes(), True)})
if not sweep_tx.is_complete():
@ -501,17 +521,14 @@ def create_sweeptx_ctx_to_local(sweep_address: str, ctx: Transaction, output_idx
if outvalue <= dust_threshold():
return None
sweep_outputs = [TxOutput(TYPE_ADDRESS, sweep_address, outvalue)]
name = 'their_ctx_to_local' if is_revocation else 'our_ctx_to_local'
csv_delay = 0 if is_revocation else to_self_delay
sweep_tx = Transaction.from_io(sweep_inputs, sweep_outputs, version=2, name=name, csv_delay=csv_delay)
sweep_tx = Transaction.from_io(sweep_inputs, sweep_outputs, version=2)
sig = sweep_tx.sign_txin(0, privkey)
witness = construct_witness([sig, int(is_revocation), witness_script])
sweep_tx.inputs()[0]['witness'] = witness
return sweep_tx
def create_sweeptx_that_spends_htlctx_that_spends_htlc_in_ctx(
name_prefix: str,
def create_sweeptx_that_spends_htlctx_that_spends_htlc_in_ctx(*,
htlc_tx: Transaction, htlctx_witness_script: bytes, sweep_address: str,
privkey: bytes, is_revocation: bool, to_self_delay: int) -> Optional[Transaction]:
val = htlc_tx.outputs()[0].value
@ -534,8 +551,7 @@ def create_sweeptx_that_spends_htlctx_that_spends_htlc_in_ctx(
outvalue = val - fee
if outvalue <= dust_threshold(): return None
sweep_outputs = [TxOutput(TYPE_ADDRESS, sweep_address, outvalue)]
name = name_prefix + htlc_tx.txid()[0:4]
tx = Transaction.from_io(sweep_inputs, sweep_outputs, version=2, name=name, csv_delay=to_self_delay)
tx = Transaction.from_io(sweep_inputs, sweep_outputs, version=2)
sig = bfh(tx.sign_txin(0, privkey))
witness = construct_witness([sig, int(is_revocation), htlctx_witness_script])

12
electrum/lnutil.py

@ -318,10 +318,10 @@ def make_htlc_tx_inputs(htlc_output_txid: str, htlc_output_index: int,
}]
return c_inputs
def make_htlc_tx(cltv_timeout, inputs, output, name, cltv_expiry):
assert type(cltv_timeout) is int
def make_htlc_tx(*, cltv_expiry: int, inputs, output) -> Transaction:
assert type(cltv_expiry) is int
c_outputs = [output]
tx = Transaction.from_io(inputs, c_outputs, locktime=cltv_timeout, version=2, name=name, cltv_expiry=cltv_expiry)
tx = Transaction.from_io(inputs, c_outputs, locktime=cltv_expiry, version=2)
return tx
def make_offered_htlc(revocation_pubkey: bytes, remote_htlcpubkey: bytes,
@ -468,8 +468,7 @@ def make_htlc_tx_with_open_channel(*, chan: 'Channel', pcp: bytes, subject: 'HTL
witness_script=bh2u(preimage_script))
if is_htlc_success:
cltv_expiry = 0
htlc_tx = make_htlc_tx(cltv_expiry, inputs=htlc_tx_inputs, output=htlc_tx_output,
name=name, cltv_expiry=cltv_expiry)
htlc_tx = make_htlc_tx(cltv_expiry=cltv_expiry, inputs=htlc_tx_inputs, output=htlc_tx_output)
return witness_script_of_htlc_tx_output, htlc_tx
def make_funding_input(local_funding_pubkey: bytes, remote_funding_pubkey: bytes,
@ -672,7 +671,8 @@ def get_compressed_pubkey_from_bech32(bech32_pubkey: str) -> bytes:
def make_closing_tx(local_funding_pubkey: bytes, remote_funding_pubkey: bytes,
funding_txid: bytes, funding_pos: int, funding_sat: int, outputs: List[TxOutput]):
funding_txid: bytes, funding_pos: int, funding_sat: int,
outputs: List[TxOutput]) -> Transaction:
c_input = make_funding_input(local_funding_pubkey, remote_funding_pubkey,
funding_pos, funding_txid, funding_sat)
c_input['sequence'] = 0xFFFF_FFFF

6
electrum/lnwatcher.py

@ -268,16 +268,16 @@ class WatchTower(LNWatcher):
for tx in sweep_txns:
await self.broadcast_or_log(funding_outpoint, tx)
async def broadcast_or_log(self, funding_outpoint, tx):
async def broadcast_or_log(self, funding_outpoint: str, tx: Transaction):
height = self.get_tx_height(tx.txid()).height
if height != TX_HEIGHT_LOCAL:
return
try:
txid = await self.network.broadcast_transaction(tx)
except Exception as e:
self.logger.info(f'broadcast failure: {tx.name}: {repr(e)}')
self.logger.info(f'broadcast failure: txid={tx.txid()}, funding_outpoint={funding_outpoint}: {repr(e)}')
else:
self.logger.info(f'broadcast success: {tx.name}')
self.logger.info(f'broadcast success: txid={tx.txid()}, funding_outpoint={funding_outpoint}')
if funding_outpoint in self.tx_progress:
await self.tx_progress[funding_outpoint].tx_queue.put(tx)
return txid

31
electrum/lnworker.py

@ -56,6 +56,7 @@ from .lnwatcher import LNWatcher
if TYPE_CHECKING:
from .network import Network
from .wallet import Abstract_Wallet
from .lnsweep import SweepInfo
NUM_PEERS_TARGET = 4
@ -601,11 +602,11 @@ class LNWallet(LNWorker):
if chan.short_channel_id is not None:
self.channel_db.remove_channel(chan.short_channel_id)
# detect who closed and set sweep_info
sweep_info = chan.sweep_ctx(closing_tx)
self.logger.info(f'sweep_info length: {len(sweep_info)}')
sweep_info_dict = chan.sweep_ctx(closing_tx)
self.logger.info(f'sweep_info_dict length: {len(sweep_info_dict)}')
# create and broadcast transaction
for prevout, e_tx in sweep_info.items():
name, csv_delay, cltv_expiry, gen_tx = e_tx
for prevout, sweep_info in sweep_info_dict.items():
name = sweep_info.name
spender = spenders.get(prevout)
if spender is not None:
spender_tx = await self.network.get_transaction(spender)
@ -622,31 +623,31 @@ class LNWallet(LNWorker):
self.logger.info(f'outpoint already spent {name}: {prevout}')
else:
self.logger.info(f'trying to redeem {name}: {prevout}')
await self.try_redeem(prevout, e_tx)
await self.try_redeem(prevout, sweep_info)
@log_exceptions
async def try_redeem(self, prevout, e_tx):
name, csv_delay, cltv_expiry, gen_tx = e_tx
async def try_redeem(self, prevout: str, sweep_info: 'SweepInfo') -> None:
name = sweep_info.name
prev_txid, prev_index = prevout.split(':')
broadcast = True
if cltv_expiry:
if sweep_info.cltv_expiry:
local_height = self.network.get_local_height()
remaining = cltv_expiry - local_height
remaining = sweep_info.cltv_expiry - local_height
if remaining > 0:
self.logger.info('waiting for {}: CLTV ({} > {}), prevout {}'
.format(name, local_height, cltv_expiry, prevout))
.format(name, local_height, sweep_info.cltv_expiry, prevout))
broadcast = False
if csv_delay:
if sweep_info.csv_delay:
prev_height = self.lnwatcher.get_tx_height(prev_txid)
remaining = csv_delay - prev_height.conf
remaining = sweep_info.csv_delay - prev_height.conf
if remaining > 0:
self.logger.info('waiting for {}: CSV ({} >= {}), prevout: {}'
.format(name, prev_height.conf, csv_delay, prevout))
.format(name, prev_height.conf, sweep_info.csv_delay, prevout))
broadcast = False
tx = gen_tx()
self.wallet.set_label(tx.txid(), name)
tx = sweep_info.gen_tx()
if tx is None:
self.logger.info(f'{name} could not claim output: {prevout}, dust')
self.wallet.set_label(tx.txid(), name)
if broadcast:
try:
await self.network.broadcast_transaction(tx)

6
electrum/network.py

@ -62,6 +62,8 @@ from .logging import get_logger, Logger
if TYPE_CHECKING:
from .channel_db import ChannelDB
from .lnworker import LNGossip
from .lnwatcher import WatchTower
_logger = get_logger(__name__)
@ -311,8 +313,8 @@ class Network(Logger):
self.local_watchtower = lnwatcher.WatchTower(self) if self.config.get('local_watchtower', False) else None
else:
self.channel_db = None # type: Optional[ChannelDB]
self.lngossip = None
self.local_watchtower = None
self.lngossip = None # type: Optional[LNGossip]
self.local_watchtower = None # type: Optional[WatchTower]
def run_from_another_thread(self, coro, *, timeout=None):
assert self._loop_thread != threading.current_thread(), 'must not be called from network thread'

7
electrum/tests/test_lnutil.py

@ -553,11 +553,10 @@ class TestLNUtil(unittest.TestCase):
htlc_output_index=htlc_output_index,
amount_msat=amount_msat,
witness_script=bh2u(htlc))
our_htlc_tx = make_htlc_tx(cltv_timeout,
our_htlc_tx = make_htlc_tx(
cltv_expiry=cltv_timeout,
inputs=our_htlc_tx_inputs,
output=our_htlc_tx_output,
name='test',
cltv_expiry=0)
output=our_htlc_tx_output)
local_sig = our_htlc_tx.sign_txin(0, local_privkey[:-1])

4
electrum/tests/test_transaction.py

@ -86,7 +86,7 @@ class TestTransaction(SequentialTestCase):
self.assertEqual(tx.deserialize(), expected)
self.assertEqual(tx.deserialize(), None)
self.assertEqual(tx.as_dict(), {'hex': unsigned_blob, 'complete': False, 'final': True, 'csv_delay': 0, 'cltv_expiry': 0, 'name': None})
self.assertEqual(tx.as_dict(), {'hex': unsigned_blob, 'complete': False, 'final': True})
self.assertEqual(tx.get_outputs_for_UI(), [TxOutputForUI('14CHYaaByjJZpx4oHBpfDMdqhTyXnZ3kVs', 1000000)])
self.assertTrue(tx.has_address('14CHYaaByjJZpx4oHBpfDMdqhTyXnZ3kVs'))
@ -127,7 +127,7 @@ class TestTransaction(SequentialTestCase):
tx = transaction.Transaction(signed_blob)
self.assertEqual(tx.deserialize(), expected)
self.assertEqual(tx.deserialize(), None)
self.assertEqual(tx.as_dict(), {'hex': signed_blob, 'complete': True, 'final': True, 'csv_delay': 0, 'cltv_expiry': 0, 'name': None})
self.assertEqual(tx.as_dict(), {'hex': signed_blob, 'complete': True, 'final': True})
self.assertEqual(tx.serialize(), signed_blob)

14
electrum/transaction.py

@ -607,9 +607,6 @@ class Transaction:
self._outputs = None # type: List[TxOutput]
self.locktime = 0
self.version = 2
self.name = None
self.csv_delay = 0
self.cltv_expiry = 0
# by default we assume this is a partial txn;
# this value will get properly set when deserializing
self.is_partial_originally = True
@ -720,16 +717,13 @@ class Transaction:
return d
@classmethod
def from_io(klass, inputs, outputs, locktime=0, version=None, name=None, csv_delay=0, cltv_expiry=0):
def from_io(klass, inputs, outputs, *, locktime=0, version=None):
self = klass(None)
self._inputs = inputs
self._outputs = outputs
self.locktime = locktime
if version is not None:
self.version = version
self.name = name
self.csv_delay = csv_delay
self.cltv_expiry = cltv_expiry
self.BIP69_sort()
return self
@ -1229,9 +1223,6 @@ class Transaction:
'hex': self.raw,
'complete': self.is_complete(),
'final': self.is_final(),
'name': self.name,
'csv_delay': self.csv_delay,
'cltv_expiry': self.cltv_expiry,
}
return out
@ -1239,9 +1230,6 @@ class Transaction:
def from_dict(cls, d):
tx = cls(d['hex'])
tx.deserialize(True)
tx.name = d.get('name')
tx.csv_delay = d.get('csv_delay', 0)
tx.cltv_expiry = d.get('cltv_expiry', 0)
return tx

Loading…
Cancel
Save