Browse Source

redeem htlcs:

- fix bug in lnsweep: lnwatcher transactions were indexed by prev_txid
 - add test for breach remedy with unsettled htlcs
 - add timeout option to lnpay, and replace DO_NOT_SETTLE with SETTLE_DELAY
   so that we can read intermediate commitment tx in regtest
regtest_lnd
ThomasV 6 years ago
parent
commit
a5e8f6ce6d
  1. 4
      electrum/commands.py
  2. 5
      electrum/lnchannel.py
  3. 3
      electrum/lnpeer.py
  4. 12
      electrum/lnsweep.py
  5. 25
      electrum/lnwatcher.py
  6. 4
      electrum/simple_config.py
  7. 49
      electrum/tests/regtest/regtest.sh
  8. 3
      electrum/tests/test_regtest.py

4
electrum/commands.py

@ -784,8 +784,8 @@ class Commands:
self.lnworker.reestablish_channel() self.lnworker.reestablish_channel()
@command('wn') @command('wn')
def lnpay(self, invoice): def lnpay(self, invoice, timeout=10):
return self.lnworker.pay(invoice, timeout=10) return self.lnworker.pay(invoice, timeout=timeout)
@command('wn') @command('wn')
def addinvoice(self, requested_amount, message): def addinvoice(self, requested_amount, message):

5
electrum/lnchannel.py

@ -457,9 +457,8 @@ class Channel(Logger):
outpoint = self.funding_outpoint.to_str() outpoint = self.funding_outpoint.to_str()
ctx = self.remote_commitment_to_be_revoked # FIXME can't we just reconstruct it? ctx = self.remote_commitment_to_be_revoked # FIXME can't we just reconstruct it?
sweeptxs = create_sweeptxs_for_their_revoked_ctx(self, ctx, per_commitment_secret, self.sweep_address) sweeptxs = create_sweeptxs_for_their_revoked_ctx(self, ctx, per_commitment_secret, self.sweep_address)
for prev_txid, tx in sweeptxs.items(): for tx in sweeptxs:
if tx is not None: self.lnwatcher.add_sweep_tx(outpoint, tx.prevout(0), str(tx))
self.lnwatcher.add_sweep_tx(outpoint, prev_txid, str(tx))
def receive_revocation(self, revocation: RevokeAndAck): def receive_revocation(self, revocation: RevokeAndAck):
self.logger.info("receive_revocation") self.logger.info("receive_revocation")

3
electrum/lnpeer.py

@ -1237,8 +1237,7 @@ class Peer(Logger):
await self.fail_htlc(chan, htlc.htlc_id, onion_packet, reason) await self.fail_htlc(chan, htlc.htlc_id, onion_packet, reason)
return return
self.network.trigger_callback('htlc_added', htlc, invoice, RECEIVED) self.network.trigger_callback('htlc_added', htlc, invoice, RECEIVED)
if self.network.config.debug_lightning_do_not_settle: await asyncio.sleep(self.network.config.lightning_settle_delay)
return
await self._fulfill_htlc(chan, htlc.htlc_id, preimage) await self._fulfill_htlc(chan, htlc.htlc_id, preimage)
async def _fulfill_htlc(self, chan: Channel, htlc_id: int, preimage: bytes): async def _fulfill_htlc(self, chan: Channel, htlc_id: int, preimage: bytes):

12
electrum/lnsweep.py

@ -40,7 +40,7 @@ def create_sweeptxs_for_their_revoked_ctx(chan: 'Channel', ctx: Transaction, per
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)
txs = {} txs = []
# to_local # to_local
revocation_pubkey = ecc.ECPrivkey(other_revocation_privkey).get_public_key_bytes(compressed=True) revocation_pubkey = ecc.ECPrivkey(other_revocation_privkey).get_public_key_bytes(compressed=True)
witness_script = bh2u(make_commitment_output_to_local_witness_script( witness_script = bh2u(make_commitment_output_to_local_witness_script(
@ -55,7 +55,7 @@ def create_sweeptxs_for_their_revoked_ctx(chan: 'Channel', ctx: Transaction, per
witness_script=witness_script, witness_script=witness_script,
privkey=other_revocation_privkey, privkey=other_revocation_privkey,
is_revocation=True) is_revocation=True)
txs[ctx.txid()] = sweep_tx txs.append(sweep_tx)
# HTLCs # HTLCs
def create_sweeptx_for_htlc(htlc: 'UpdateAddHtlc', is_received_htlc: bool) -> Tuple[Optional[Transaction], def create_sweeptx_for_htlc(htlc: 'UpdateAddHtlc', is_received_htlc: bool) -> Tuple[Optional[Transaction],
Optional[Transaction], Optional[Transaction],
@ -93,17 +93,17 @@ def create_sweeptxs_for_their_revoked_ctx(chan: 'Channel', ctx: Transaction, per
for htlc in received_htlcs: for htlc in received_htlcs:
direct_sweep_tx, secondstage_sweep_tx, htlc_tx = create_sweeptx_for_htlc(htlc, is_received_htlc=True) direct_sweep_tx, secondstage_sweep_tx, htlc_tx = create_sweeptx_for_htlc(htlc, is_received_htlc=True)
if direct_sweep_tx: if direct_sweep_tx:
txs[ctx.txid()] = direct_sweep_tx txs.append(direct_sweep_tx)
if secondstage_sweep_tx: if secondstage_sweep_tx:
txs[htlc_tx.txid()] = secondstage_sweep_tx txs.append(secondstage_sweep_tx)
# offered HTLCs, in their ctx # offered HTLCs, in their ctx
offered_htlcs = chan.included_htlcs(REMOTE, SENT, ctn) offered_htlcs = chan.included_htlcs(REMOTE, SENT, ctn)
for htlc in offered_htlcs: for htlc in offered_htlcs:
direct_sweep_tx, secondstage_sweep_tx, htlc_tx = create_sweeptx_for_htlc(htlc, is_received_htlc=False) direct_sweep_tx, secondstage_sweep_tx, htlc_tx = create_sweeptx_for_htlc(htlc, is_received_htlc=False)
if direct_sweep_tx: if direct_sweep_tx:
txs[ctx.txid()] = direct_sweep_tx txs.append(direct_sweep_tx)
if secondstage_sweep_tx: if secondstage_sweep_tx:
txs[htlc_tx.txid()] = secondstage_sweep_tx txs.append(secondstage_sweep_tx)
return txs return txs

25
electrum/lnwatcher.py

@ -48,7 +48,7 @@ class SweepTx(Base):
__tablename__ = 'sweep_txs' __tablename__ = 'sweep_txs'
funding_outpoint = Column(String(34), primary_key=True) funding_outpoint = Column(String(34), primary_key=True)
index = Column(Integer(), primary_key=True) index = Column(Integer(), primary_key=True)
prev_txid = Column(String(32)) prevout = Column(String(34))
tx = Column(String()) tx = Column(String())
class ChannelInfo(Base): class ChannelInfo(Base):
@ -64,22 +64,22 @@ class SweepStore(SqlDB):
super().__init__(network, path, Base) super().__init__(network, path, Base)
@sql @sql
def get_sweep_tx(self, funding_outpoint, prev_txid): def get_sweep_tx(self, funding_outpoint, prevout):
return [Transaction(bh2u(r.tx)) for r in self.DBSession.query(SweepTx).filter(SweepTx.funding_outpoint==funding_outpoint, SweepTx.prev_txid==prev_txid).all()] return [Transaction(bh2u(r.tx)) for r in self.DBSession.query(SweepTx).filter(SweepTx.funding_outpoint==funding_outpoint, SweepTx.prevout==prevout).all()]
@sql @sql
def get_tx_by_index(self, funding_outpoint, index): def get_tx_by_index(self, funding_outpoint, index):
r = self.DBSession.query(SweepTx).filter(SweepTx.funding_outpoint==funding_outpoint, SweepTx.index==index).one_or_none() r = self.DBSession.query(SweepTx).filter(SweepTx.funding_outpoint==funding_outpoint, SweepTx.index==index).one_or_none()
return r.prev_txid, bh2u(r.tx) return r.prevout, bh2u(r.tx)
@sql @sql
def list_sweep_tx(self): def list_sweep_tx(self):
return set(r.funding_outpoint for r in self.DBSession.query(SweepTx).all()) return set(r.funding_outpoint for r in self.DBSession.query(SweepTx).all())
@sql @sql
def add_sweep_tx(self, funding_outpoint, prev_txid, tx): def add_sweep_tx(self, funding_outpoint, prevout, tx):
n = self.DBSession.query(SweepTx).filter(funding_outpoint==funding_outpoint).count() n = self.DBSession.query(SweepTx).filter(funding_outpoint==funding_outpoint).count()
self.DBSession.add(SweepTx(funding_outpoint=funding_outpoint, index=n, prev_txid=prev_txid, tx=bfh(tx))) self.DBSession.add(SweepTx(funding_outpoint=funding_outpoint, index=n, prevout=prevout, tx=bfh(tx)))
self.DBSession.commit() self.DBSession.commit()
@sql @sql
@ -172,8 +172,8 @@ class LNWatcher(AddressSynchronizer):
self.watchtower.add_channel(outpoint, address) self.watchtower.add_channel(outpoint, address)
self.logger.info("sending %d transactions to watchtower"%(local_n - n)) self.logger.info("sending %d transactions to watchtower"%(local_n - n))
for index in range(n, local_n): for index in range(n, local_n):
prev_txid, tx = self.sweepstore.get_tx_by_index(outpoint, index) prevout, tx = self.sweepstore.get_tx_by_index(outpoint, index)
self.watchtower.add_sweep_tx(outpoint, prev_txid, tx) self.watchtower.add_sweep_tx(outpoint, prevout, tx)
except ConnectionRefusedError: except ConnectionRefusedError:
self.logger.info('could not reach watchtower, will retry in 5s') self.logger.info('could not reach watchtower, will retry in 5s')
await asyncio.sleep(5) await asyncio.sleep(5)
@ -260,11 +260,10 @@ class LNWatcher(AddressSynchronizer):
for prevout, spender in spenders.items(): for prevout, spender in spenders.items():
if spender is not None: if spender is not None:
continue continue
prev_txid, prev_n = prevout.split(':') sweep_txns = self.sweepstore.get_sweep_tx(funding_outpoint, prevout)
sweep_txns = self.sweepstore.get_sweep_tx(funding_outpoint, prev_txid)
for tx in sweep_txns: for tx in sweep_txns:
if not await self.broadcast_or_log(funding_outpoint, tx): if not await self.broadcast_or_log(funding_outpoint, tx):
self.logger.info(f'{tx.name} could not publish tx: {str(tx)}, prev_txid: {prev_txid}') self.logger.info(f'{tx.name} could not publish tx: {str(tx)}, prevout: {prevout}')
async def broadcast_or_log(self, funding_outpoint, tx): async def broadcast_or_log(self, funding_outpoint, tx):
height = self.get_tx_height(tx.txid()).height height = self.get_tx_height(tx.txid()).height
@ -280,8 +279,8 @@ class LNWatcher(AddressSynchronizer):
await self.tx_progress[funding_outpoint].tx_queue.put(tx) await self.tx_progress[funding_outpoint].tx_queue.put(tx)
return txid return txid
def add_sweep_tx(self, funding_outpoint: str, prev_txid: str, tx: str): def add_sweep_tx(self, funding_outpoint: str, prevout: str, tx: str):
self.sweepstore.add_sweep_tx(funding_outpoint, prev_txid, tx) self.sweepstore.add_sweep_tx(funding_outpoint, prevout, tx)
if self.watchtower: if self.watchtower:
self.watchtower_queue.put_nowait(funding_outpoint) self.watchtower_queue.put_nowait(funding_outpoint)

4
electrum/simple_config.py

@ -66,9 +66,7 @@ class SimpleConfig(Logger):
options = {} options = {}
Logger.__init__(self) Logger.__init__(self)
self.lightning_settle_delay = int(os.environ.get('ELECTRUM_DEBUG_LIGHTNING_SETTLE_DELAY', 0))
self.debug_lightning = 'ELECTRUM_DEBUG_LIGHTNING_DANGEROUS' in os.environ
self.debug_lightning_do_not_settle = 'ELECTRUM_DEBUG_LIGHTNING_DO_NOT_SETTLE' in os.environ
# This lock needs to be acquired for updating and reading the config in # This lock needs to be acquired for updating and reading the config in
# a thread-safe way. # a thread-safe way.

49
electrum/tests/regtest/regtest.sh

@ -107,7 +107,7 @@ fi
if [[ $1 == "redeem_htlcs" ]]; then if [[ $1 == "redeem_htlcs" ]]; then
$bob daemon stop $bob daemon stop
ELECTRUM_DEBUG_LIGHTNING_DO_NOT_SETTLE=1 $bob daemon -s 127.0.0.1:51001:t start ELECTRUM_DEBUG_LIGHTNING_SETTLE_DELAY=10 $bob daemon -s 127.0.0.1:51001:t start
$bob daemon load_wallet $bob daemon load_wallet
sleep 1 sleep 1
# alice opens channel # alice opens channel
@ -117,11 +117,11 @@ if [[ $1 == "redeem_htlcs" ]]; then
sleep 10 sleep 10
# alice pays bob # alice pays bob
invoice=$($bob addinvoice 0.05 "test") invoice=$($bob addinvoice 0.05 "test")
$alice lnpay $invoice || true $alice lnpay $invoice --timeout=1 || true
sleep 1 sleep 1
settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length') settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
if [[ "$settled" != "0" ]]; then if [[ "$settled" != "0" ]]; then
echo 'DO_NOT_SETTLE did not work' echo 'SETTLE_DELAY did not work'
exit 1 exit 1
fi fi
# bob goes away # bob goes away
@ -145,3 +145,46 @@ if [[ $1 == "redeem_htlcs" ]]; then
exit 1 exit 1
fi fi
fi fi
if [[ $1 == "breach_with_htlc" ]]; then
$bob daemon stop
ELECTRUM_DEBUG_LIGHTNING_SETTLE_DELAY=3 $bob daemon -s 127.0.0.1:51001:t start
$bob daemon load_wallet
sleep 1
# alice opens channel
bob_node=$($bob nodeid)
channel=$($alice open_channel $bob_node 0.15)
new_blocks 6
sleep 5
# alice pays bob
invoice=$($bob addinvoice 0.05 "test")
echo "alice pays bob"
$alice lnpay $invoice --timeout=1 || true
settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
if [[ "$settled" != "0" ]]; then
echo 'SETTLE_DELAY did not work'
exit 1
fi
ctx=$($alice get_channel_ctx $channel | jq '.hex' | tr -d '"')
sleep 3
settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
if [[ "$settled" != "1" ]]; then
echo 'SETTLE_DELAY did not work'
exit 1
fi
echo $($bob getbalance)
echo "alice breaches with old ctx"
echo $ctx
$bitcoin_cli sendrawtransaction $ctx
new_blocks 1
sleep 10
new_blocks 1
sleep 1
echo $($bob getbalance)
balance_after=$($bob getbalance | jq '[.confirmed, .unconfirmed] | to_entries | map(select(.value != null).value) | map(tonumber) | add ')
if (( $(echo "$balance_after < 0.14" | bc -l) )); then
echo "htlc not redeemed."
exit 1
fi
fi

3
electrum/tests/test_regtest.py

@ -32,3 +32,6 @@ class TestLightning(unittest.TestCase):
def test_redeem_htlcs(self): def test_redeem_htlcs(self):
self.run_shell(['redeem_htlcs']) self.run_shell(['redeem_htlcs'])
def test_breach_with_htlc(self):
self.run_shell(['breach_with_htlc'])

Loading…
Cancel
Save