• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

spesmilo / electrum / 5304010765238272

17 Aug 2023 02:17PM UTC coverage: 59.027% (+0.02%) from 59.008%
5304010765238272

Pull #8493

CirrusCI

ecdsa
storage.append: fail if the file length is not what we expect
Pull Request #8493: partial-writes using jsonpatch

165 of 165 new or added lines in 9 files covered. (100.0%)

18653 of 31601 relevant lines covered (59.03%)

2.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

25.25
/electrum/lnwatcher.py
1
# Copyright (C) 2018 The Electrum developers
2
# Distributed under the MIT software license, see the accompanying
3
# file LICENCE or http://www.opensource.org/licenses/mit-license.php
4

5
from typing import NamedTuple, Iterable, TYPE_CHECKING
5✔
6
import os
5✔
7
import asyncio
5✔
8
from enum import IntEnum, auto
5✔
9
from typing import NamedTuple, Dict
5✔
10

11
from . import util
5✔
12
from .sql_db import SqlDB, sql
5✔
13
from .wallet_db import WalletDB
5✔
14
from .util import bfh, log_exceptions, ignore_exceptions, TxMinedInfo, random_shuffled_copy
5✔
15
from .address_synchronizer import AddressSynchronizer, TX_HEIGHT_LOCAL, TX_HEIGHT_UNCONF_PARENT, TX_HEIGHT_UNCONFIRMED, TX_HEIGHT_FUTURE
5✔
16
from .transaction import Transaction, TxOutpoint
5✔
17
from .transaction import match_script_against_template
5✔
18
from .lnutil import WITNESS_TEMPLATE_RECEIVED_HTLC, WITNESS_TEMPLATE_OFFERED_HTLC
5✔
19
from .logging import Logger
5✔
20

21

22
if TYPE_CHECKING:
5✔
23
    from .network import Network
×
24
    from .lnsweep import SweepInfo
×
25
    from .lnworker import LNWallet
×
26

27
class ListenerItem(NamedTuple):
5✔
28
    # this is triggered when the lnwatcher is all done with the outpoint used as index in LNWatcher.tx_progress
29
    all_done : asyncio.Event
5✔
30
    # txs we broadcast are put on this queue so that the test can wait for them to get mined
31
    tx_queue : asyncio.Queue
5✔
32

33
class TxMinedDepth(IntEnum):
5✔
34
    """ IntEnum because we call min() in get_deepest_tx_mined_depth_for_txids """
35
    DEEP = auto()
5✔
36
    SHALLOW = auto()
5✔
37
    MEMPOOL = auto()
5✔
38
    FREE = auto()
5✔
39

40

41
create_sweep_txs="""
5✔
42
CREATE TABLE IF NOT EXISTS sweep_txs (
43
funding_outpoint VARCHAR(34) NOT NULL,
44
ctn INTEGER NOT NULL,
45
prevout VARCHAR(34),
46
tx VARCHAR
47
)"""
48

49
create_channel_info="""
5✔
50
CREATE TABLE IF NOT EXISTS channel_info (
51
outpoint VARCHAR(34) NOT NULL,
52
address VARCHAR(32),
53
PRIMARY KEY(outpoint)
54
)"""
55

56

57
class SweepStore(SqlDB):
5✔
58

59
    def __init__(self, path, network):
5✔
60
        super().__init__(network.asyncio_loop, path)
×
61

62
    def create_database(self):
5✔
63
        c = self.conn.cursor()
×
64
        c.execute(create_channel_info)
×
65
        c.execute(create_sweep_txs)
×
66
        self.conn.commit()
×
67

68
    @sql
5✔
69
    def get_sweep_tx(self, funding_outpoint, prevout):
5✔
70
        c = self.conn.cursor()
×
71
        c.execute("SELECT tx FROM sweep_txs WHERE funding_outpoint=? AND prevout=?", (funding_outpoint, prevout))
×
72
        return [Transaction(r[0].hex()) for r in c.fetchall()]
×
73

74
    @sql
5✔
75
    def list_sweep_tx(self):
5✔
76
        c = self.conn.cursor()
×
77
        c.execute("SELECT funding_outpoint FROM sweep_txs")
×
78
        return set([r[0] for r in c.fetchall()])
×
79

80
    @sql
5✔
81
    def add_sweep_tx(self, funding_outpoint, ctn, prevout, raw_tx):
5✔
82
        c = self.conn.cursor()
×
83
        assert Transaction(raw_tx).is_complete()
×
84
        c.execute("""INSERT INTO sweep_txs (funding_outpoint, ctn, prevout, tx) VALUES (?,?,?,?)""", (funding_outpoint, ctn, prevout, bfh(raw_tx)))
×
85
        self.conn.commit()
×
86

87
    @sql
5✔
88
    def get_num_tx(self, funding_outpoint):
5✔
89
        c = self.conn.cursor()
×
90
        c.execute("SELECT count(*) FROM sweep_txs WHERE funding_outpoint=?", (funding_outpoint,))
×
91
        return int(c.fetchone()[0])
×
92

93
    @sql
5✔
94
    def get_ctn(self, outpoint, addr):
5✔
95
        if not self._has_channel(outpoint):
×
96
            self._add_channel(outpoint, addr)
×
97
        c = self.conn.cursor()
×
98
        c.execute("SELECT max(ctn) FROM sweep_txs WHERE funding_outpoint=?", (outpoint,))
×
99
        return int(c.fetchone()[0] or 0)
×
100

101
    @sql
5✔
102
    def remove_sweep_tx(self, funding_outpoint):
5✔
103
        c = self.conn.cursor()
×
104
        c.execute("DELETE FROM sweep_txs WHERE funding_outpoint=?", (funding_outpoint,))
×
105
        self.conn.commit()
×
106

107
    def _add_channel(self, outpoint, address):
5✔
108
        c = self.conn.cursor()
×
109
        c.execute("INSERT INTO channel_info (address, outpoint) VALUES (?,?)", (address, outpoint))
×
110
        self.conn.commit()
×
111

112
    @sql
5✔
113
    def remove_channel(self, outpoint):
5✔
114
        c = self.conn.cursor()
×
115
        c.execute("DELETE FROM channel_info WHERE outpoint=?", (outpoint,))
×
116
        self.conn.commit()
×
117

118
    def _has_channel(self, outpoint):
5✔
119
        c = self.conn.cursor()
×
120
        c.execute("SELECT * FROM channel_info WHERE outpoint=?", (outpoint,))
×
121
        r = c.fetchone()
×
122
        return r is not None
×
123

124
    @sql
5✔
125
    def get_address(self, outpoint):
5✔
126
        c = self.conn.cursor()
×
127
        c.execute("SELECT address FROM channel_info WHERE outpoint=?", (outpoint,))
×
128
        r = c.fetchone()
×
129
        return r[0] if r else None
×
130

131
    @sql
5✔
132
    def list_channels(self):
5✔
133
        c = self.conn.cursor()
×
134
        c.execute("SELECT outpoint, address FROM channel_info")
×
135
        return [(r[0], r[1]) for r in c.fetchall()]
×
136

137

138
from .util import EventListener, event_listener
5✔
139

140
class LNWatcher(Logger, EventListener):
5✔
141

142
    LOGGING_SHORTCUT = 'W'
5✔
143

144
    def __init__(self, adb: 'AddressSynchronizer', network: 'Network'):
5✔
145

146
        Logger.__init__(self)
×
147
        self.adb = adb
×
148
        self.config = network.config
×
149
        self.callbacks = {} # address -> lambda: coroutine
×
150
        self.network = network
×
151
        self.register_callbacks()
×
152
        # status gets populated when we run
153
        self.channel_status = {}
×
154

155
    async def stop(self):
5✔
156
        self.unregister_callbacks()
×
157

158
    def get_channel_status(self, outpoint):
5✔
159
        return self.channel_status.get(outpoint, 'unknown')
×
160

161
    def add_channel(self, outpoint: str, address: str) -> None:
5✔
162
        assert isinstance(outpoint, str)
×
163
        assert isinstance(address, str)
×
164
        cb = lambda: self.check_onchain_situation(address, outpoint)
×
165
        self.add_callback(address, cb)
×
166

167
    async def unwatch_channel(self, address, funding_outpoint):
5✔
168
        self.logger.info(f'unwatching {funding_outpoint}')
×
169
        self.remove_callback(address)
×
170

171
    def remove_callback(self, address):
5✔
172
        self.callbacks.pop(address, None)
×
173

174
    def add_callback(self, address, callback):
5✔
175
        self.adb.add_address(address)
×
176
        self.callbacks[address] = callback
×
177

178
    @event_listener
5✔
179
    async def on_event_fee(self, *args):
5✔
180
        await self.trigger_callbacks()
×
181

182
    @event_listener
5✔
183
    async def on_event_network_updated(self, *args):
5✔
184
        await self.trigger_callbacks()
×
185

186
    @event_listener
5✔
187
    async def on_event_blockchain_updated(self, *args):
5✔
188
        await self.trigger_callbacks()
×
189

190
    @event_listener
5✔
191
    async def on_event_adb_added_verified_tx(self, adb, tx_hash):
5✔
192
        if adb != self.adb:
×
193
            return
×
194
        await self.trigger_callbacks()
×
195

196
    @event_listener
5✔
197
    async def on_event_adb_set_up_to_date(self, adb):
5✔
198
        if adb != self.adb:
×
199
            return
×
200
        await self.trigger_callbacks()
×
201

202
    @log_exceptions
5✔
203
    async def trigger_callbacks(self):
5✔
204
        if not self.adb.synchronizer:
×
205
            self.logger.info("synchronizer not set yet")
×
206
            return
×
207
        for address, callback in list(self.callbacks.items()):
×
208
            await callback()
×
209

210
    async def check_onchain_situation(self, address, funding_outpoint):
5✔
211
        # early return if address has not been added yet
212
        if not self.adb.is_mine(address):
×
213
            return
×
214
        spenders = self.inspect_tx_candidate(funding_outpoint, 0)
×
215
        # inspect_tx_candidate might have added new addresses, in which case we return early
216
        if not self.adb.is_up_to_date():
×
217
            return
×
218
        funding_txid = funding_outpoint.split(':')[0]
×
219
        funding_height = self.adb.get_tx_height(funding_txid)
×
220
        closing_txid = spenders.get(funding_outpoint)
×
221
        closing_height = self.adb.get_tx_height(closing_txid)
×
222
        if closing_txid:
×
223
            closing_tx = self.adb.get_transaction(closing_txid)
×
224
            if closing_tx:
×
225
                keep_watching = await self.do_breach_remedy(funding_outpoint, closing_tx, spenders)
×
226
            else:
227
                self.logger.info(f"channel {funding_outpoint} closed by {closing_txid}. still waiting for tx itself...")
×
228
                keep_watching = True
×
229
        else:
230
            keep_watching = True
×
231
        await self.update_channel_state(
×
232
            funding_outpoint=funding_outpoint,
233
            funding_txid=funding_txid,
234
            funding_height=funding_height,
235
            closing_txid=closing_txid,
236
            closing_height=closing_height,
237
            keep_watching=keep_watching)
238
        if not keep_watching:
×
239
            await self.unwatch_channel(address, funding_outpoint)
×
240

241
    async def do_breach_remedy(self, funding_outpoint, closing_tx, spenders) -> bool:
5✔
242
        raise NotImplementedError()  # implemented by subclasses
×
243

244
    async def update_channel_state(self, *, funding_outpoint: str, funding_txid: str,
5✔
245
                                   funding_height: TxMinedInfo, closing_txid: str,
246
                                   closing_height: TxMinedInfo, keep_watching: bool) -> None:
247
        raise NotImplementedError()  # implemented by subclasses
×
248

249
    def inspect_tx_candidate(self, outpoint, n):
5✔
250
        """
251
        returns a dict of spenders for a transaction of interest.
252
        subscribes to addresses as a side effect.
253
        n==0 => outpoint is a channel funding.
254
        n==1 => outpoint is a commitment or close output: to_local, to_remote or first-stage htlc
255
        n==2 => outpoint is a second-stage htlc
256
        """
257
        prev_txid, index = outpoint.split(':')
×
258
        spender_txid = self.adb.db.get_spent_outpoint(prev_txid, int(index))
×
259
        result = {outpoint:spender_txid}
×
260
        if n == 0:
×
261
            if spender_txid is None:
×
262
                self.channel_status[outpoint] = 'open'
×
263
            elif not self.is_deeply_mined(spender_txid):
×
264
                self.channel_status[outpoint] = 'closed (%d)' % self.adb.get_tx_height(spender_txid).conf
×
265
            else:
266
                self.channel_status[outpoint] = 'closed (deep)'
×
267
        if spender_txid is None:
×
268
            return result
×
269
        spender_tx = self.adb.get_transaction(spender_txid)
×
270
        if n == 1:
×
271
            # if tx input is not a first-stage HTLC, we can stop recursion
272
            if len(spender_tx.inputs()) != 1:
×
273
                return result
×
274
            o = spender_tx.inputs()[0]
×
275
            witness = o.witness_elements()
×
276
            if not witness:
×
277
                # This can happen if spender_tx is a local unsigned tx in the wallet history, e.g.:
278
                # channel is coop-closed, outpoint is for our coop-close output, and spender_tx is an
279
                # arbitrary wallet-spend.
280
                return result
×
281
            redeem_script = witness[-1]
×
282
            if match_script_against_template(redeem_script, WITNESS_TEMPLATE_OFFERED_HTLC):
×
283
                #self.logger.info(f"input script matches offered htlc {redeem_script.hex()}")
284
                pass
×
285
            elif match_script_against_template(redeem_script, WITNESS_TEMPLATE_RECEIVED_HTLC):
×
286
                #self.logger.info(f"input script matches received htlc {redeem_script.hex()}")
287
                pass
×
288
            else:
289
                return result
×
290
        for i, o in enumerate(spender_tx.outputs()):
×
291
            if o.address is None:
×
292
                continue
×
293
            if not self.adb.is_mine(o.address):
×
294
                self.adb.add_address(o.address)
×
295
            elif n < 2:
×
296
                r = self.inspect_tx_candidate(spender_txid+':%d'%i, n+1)
×
297
                result.update(r)
×
298
        return result
×
299

300
    def get_tx_mined_depth(self, txid: str):
5✔
301
        if not txid:
×
302
            return TxMinedDepth.FREE
×
303
        tx_mined_depth = self.adb.get_tx_height(txid)
×
304
        height, conf = tx_mined_depth.height, tx_mined_depth.conf
×
305
        if conf > 100:
×
306
            return TxMinedDepth.DEEP
×
307
        elif conf > 0:
×
308
            return TxMinedDepth.SHALLOW
×
309
        elif height in (TX_HEIGHT_UNCONFIRMED, TX_HEIGHT_UNCONF_PARENT):
×
310
            return TxMinedDepth.MEMPOOL
×
311
        elif height in (TX_HEIGHT_LOCAL, TX_HEIGHT_FUTURE):
×
312
            return TxMinedDepth.FREE
×
313
        elif height > 0 and conf == 0:
×
314
            # unverified but claimed to be mined
315
            return TxMinedDepth.MEMPOOL
×
316
        else:
317
            raise NotImplementedError()
×
318

319
    def is_deeply_mined(self, txid):
5✔
320
        return self.get_tx_mined_depth(txid) == TxMinedDepth.DEEP
×
321

322

323
class WatchTower(LNWatcher):
5✔
324

325
    LOGGING_SHORTCUT = 'W'
5✔
326

327
    def __init__(self, network: 'Network'):
5✔
328
        adb = AddressSynchronizer(WalletDB({}, storage=None, manual_upgrades=False), network.config, name=self.diagnostic_name())
×
329
        adb.start_network(network)
×
330
        LNWatcher.__init__(self, adb, network)
×
331
        self.network = network
×
332
        self.sweepstore = SweepStore(os.path.join(self.network.config.path, "watchtower_db"), network)
×
333
        # this maps funding_outpoints to ListenerItems, which have an event for when the watcher is done,
334
        # and a queue for seeing which txs are being published
335
        self.tx_progress = {} # type: Dict[str, ListenerItem]
×
336

337
    async def stop(self):
5✔
338
        await super().stop()
×
339
        await self.adb.stop()
×
340

341
    def diagnostic_name(self):
5✔
342
        return "local_tower"
×
343

344
    async def start_watching(self):
5✔
345
        # I need to watch the addresses from sweepstore
346
        lst = await self.sweepstore.list_channels()
×
347
        for outpoint, address in random_shuffled_copy(lst):
×
348
            self.add_channel(outpoint, address)
×
349

350
    async def do_breach_remedy(self, funding_outpoint, closing_tx, spenders):
5✔
351
        keep_watching = False
×
352
        for prevout, spender in spenders.items():
×
353
            if spender is not None:
×
354
                keep_watching |= not self.is_deeply_mined(spender)
×
355
                continue
×
356
            sweep_txns = await self.sweepstore.get_sweep_tx(funding_outpoint, prevout)
×
357
            for tx in sweep_txns:
×
358
                await self.broadcast_or_log(funding_outpoint, tx)
×
359
                keep_watching = True
×
360
        return keep_watching
×
361

362
    async def broadcast_or_log(self, funding_outpoint: str, tx: Transaction):
5✔
363
        height = self.adb.get_tx_height(tx.txid()).height
×
364
        if height != TX_HEIGHT_LOCAL:
×
365
            return
×
366
        try:
×
367
            txid = await self.network.broadcast_transaction(tx)
×
368
        except Exception as e:
×
369
            self.logger.info(f'broadcast failure: txid={tx.txid()}, funding_outpoint={funding_outpoint}: {repr(e)}')
×
370
        else:
371
            self.logger.info(f'broadcast success: txid={tx.txid()}, funding_outpoint={funding_outpoint}')
×
372
            if funding_outpoint in self.tx_progress:
×
373
                await self.tx_progress[funding_outpoint].tx_queue.put(tx)
×
374
            return txid
×
375

376
    async def get_ctn(self, outpoint, addr):
5✔
377
        if addr not in self.callbacks.keys():
×
378
            self.logger.info(f'watching new channel: {outpoint} {addr}')
×
379
            self.add_channel(outpoint, addr)
×
380
        return await self.sweepstore.get_ctn(outpoint, addr)
×
381

382
    def get_num_tx(self, outpoint):
5✔
383
        async def f():
×
384
            return await self.sweepstore.get_num_tx(outpoint)
×
385
        return self.network.run_from_another_thread(f())
×
386

387
    def list_sweep_tx(self):
5✔
388
        async def f():
×
389
            return await self.sweepstore.list_sweep_tx()
×
390
        return self.network.run_from_another_thread(f())
×
391

392
    def list_channels(self):
5✔
393
        async def f():
×
394
            return await self.sweepstore.list_channels()
×
395
        return self.network.run_from_another_thread(f())
×
396

397
    async def unwatch_channel(self, address, funding_outpoint):
5✔
398
        await super().unwatch_channel(address, funding_outpoint)
×
399
        await self.sweepstore.remove_sweep_tx(funding_outpoint)
×
400
        await self.sweepstore.remove_channel(funding_outpoint)
×
401
        if funding_outpoint in self.tx_progress:
×
402
            self.tx_progress[funding_outpoint].all_done.set()
×
403

404
    async def update_channel_state(self, *args, **kwargs):
5✔
405
        pass
×
406

407

408

409

410
class LNWalletWatcher(LNWatcher):
5✔
411

412
    def __init__(self, lnworker: 'LNWallet', network: 'Network'):
5✔
413
        self.network = network
×
414
        self.lnworker = lnworker
×
415
        LNWatcher.__init__(self, lnworker.wallet.adb, network)
×
416

417
    def diagnostic_name(self):
5✔
418
        return f"{self.lnworker.wallet.diagnostic_name()}-LNW"
×
419

420
    @ignore_exceptions
5✔
421
    @log_exceptions
5✔
422
    async def update_channel_state(self, *, funding_outpoint: str, funding_txid: str,
5✔
423
                                   funding_height: TxMinedInfo, closing_txid: str,
424
                                   closing_height: TxMinedInfo, keep_watching: bool) -> None:
425
        chan = self.lnworker.channel_by_txo(funding_outpoint)
×
426
        if not chan:
×
427
            return
×
428
        chan.update_onchain_state(
×
429
            funding_txid=funding_txid,
430
            funding_height=funding_height,
431
            closing_txid=closing_txid,
432
            closing_height=closing_height,
433
            keep_watching=keep_watching)
434
        await self.lnworker.handle_onchain_state(chan)
×
435

436
    @log_exceptions
5✔
437
    async def do_breach_remedy(self, funding_outpoint, closing_tx, spenders):
5✔
438
        chan = self.lnworker.channel_by_txo(funding_outpoint)
×
439
        if not chan:
×
440
            return False
×
441
        chan_id_for_log = chan.get_id_for_log()
×
442
        # detect who closed and set sweep_info
443
        sweep_info_dict = chan.sweep_ctx(closing_tx)
×
444
        keep_watching = False if sweep_info_dict else not self.is_deeply_mined(closing_tx.txid())
×
445
        # create and broadcast transaction
446
        for prevout, sweep_info in sweep_info_dict.items():
×
447
            name = sweep_info.name + ' ' + chan.get_id_for_log()
×
448
            spender_txid = spenders.get(prevout)
×
449
            spender_tx = self.adb.get_transaction(spender_txid) if spender_txid else None
×
450
            if spender_tx:
×
451
                # the spender might be the remote, revoked or not
452
                e_htlc_tx = chan.maybe_sweep_revoked_htlc(closing_tx, spender_tx)
×
453
                if e_htlc_tx:
×
454
                    spender2 = spenders.get(spender_txid+':0')
×
455
                    if spender2:
×
456
                        keep_watching |= not self.is_deeply_mined(spender2)
×
457
                    else:
458
                        keep_watching = True
×
459
                    await self.maybe_redeem(spenders, spender_txid+':0', e_htlc_tx, name)
×
460
                else:
461
                    keep_watching |= not self.is_deeply_mined(spender_tx.txid())
×
462
                    txin_idx = spender_tx.get_input_idx_that_spent_prevout(TxOutpoint.from_str(prevout))
×
463
                    assert txin_idx is not None
×
464
                    spender_txin = spender_tx.inputs()[txin_idx]
×
465
                    chan.extract_preimage_from_htlc_txin(spender_txin)
×
466
            else:
467
                keep_watching = True
×
468
            # broadcast or maybe update our own tx
469
            await self.maybe_redeem(spenders, prevout, sweep_info, name)
×
470

471
        return keep_watching
×
472

473
    def get_redeem_tx(self, spenders, prevout: str, sweep_info: 'SweepInfo', name: str):
5✔
474
        # check if redeem tx needs to be updated
475
        # if it is in the mempool, we need to check fee rise
476
        txid = spenders.get(prevout)
×
477
        old_tx = self.adb.get_transaction(txid)
×
478
        assert old_tx is not None or txid is None
×
479
        tx_depth = self.get_tx_mined_depth(txid) if txid else None
×
480
        if txid and tx_depth not in [TxMinedDepth.FREE, TxMinedDepth.MEMPOOL]:
×
481
            assert old_tx is not None
×
482
            return old_tx, None
×
483
        new_tx = sweep_info.gen_tx()
×
484
        if new_tx is None:
×
485
            self.logger.info(f'{name} could not claim output: {prevout}, dust')
×
486
            assert old_tx is not None
×
487
            return old_tx, None
×
488
        if txid is None:
×
489
            return None, new_tx
×
490
        elif tx_depth == TxMinedDepth.MEMPOOL:
×
491
            delta = new_tx.get_fee() - self.adb.get_tx_fee(txid)
×
492
            if delta > 1:
×
493
                self.logger.info(f'increasing fee of mempool tx {name}: {prevout}')
×
494
                return old_tx, new_tx
×
495
            else:
496
                assert old_tx is not None
×
497
                return old_tx, None
×
498
        elif tx_depth == TxMinedDepth.FREE:
×
499
            # return new tx, even if it is equal to old_tx,
500
            # because we need to test if it can be broadcast
501
            return old_tx, new_tx
×
502
        else:
503
            assert old_tx is not None
×
504
            return old_tx, None
×
505

506
    async def maybe_redeem(self, spenders, prevout, sweep_info: 'SweepInfo', name: str) -> None:
5✔
507
        old_tx, new_tx = self.get_redeem_tx(spenders, prevout, sweep_info, name)
×
508
        if new_tx is None:
×
509
            return
×
510
        prev_txid, prev_index = prevout.split(':')
×
511
        can_broadcast = True
×
512
        local_height = self.network.get_local_height()
×
513
        if sweep_info.cltv_expiry:
×
514
            wanted_height = sweep_info.cltv_expiry
×
515
            if wanted_height - local_height > 0:
×
516
                can_broadcast = False
×
517
                # self.logger.debug(f"pending redeem for {prevout}. waiting for {name}: CLTV ({local_height=}, {wanted_height=})")
518
        if sweep_info.csv_delay:
×
519
            prev_height = self.adb.get_tx_height(prev_txid)
×
520
            if prev_height.height > 0:
×
521
                wanted_height = prev_height.height + sweep_info.csv_delay - 1
×
522
            else:
523
                wanted_height = local_height + sweep_info.csv_delay
×
524
            if wanted_height - local_height > 0:
×
525
                can_broadcast = False
×
526
                # self.logger.debug(
527
                #     f"pending redeem for {prevout}. waiting for {name}: CSV "
528
                #     f"({local_height=}, {wanted_height=}, {prev_height.height=}, {sweep_info.csv_delay=})")
529
        if can_broadcast:
×
530
            self.logger.info(f'we can broadcast: {name}')
×
531
            tx_was_added = await self.network.try_broadcasting(new_tx, name)
×
532
        else:
533
            # we may have a tx with a different fee, in which case it will be replaced
534
            if not old_tx or (old_tx and old_tx.txid() != new_tx.txid()):
×
535
                try:
×
536
                    tx_was_added = self.adb.add_transaction(new_tx, is_new=(old_tx is None))
×
537
                except Exception as e:
×
538
                    self.logger.info(f'could not add future tx: {name}. prevout: {prevout} {str(e)}')
×
539
                    tx_was_added = False
×
540
                if tx_was_added:
×
541
                    self.logger.info(f'added redeem tx: {name}. prevout: {prevout}')
×
542
            else:
543
                tx_was_added = False
×
544
            # set future tx regardless of tx_was_added, because it is not persisted
545
            # (and wanted_height can change if input of CSV was not mined before)
546
            self.adb.set_future_tx(new_tx.txid(), wanted_height=wanted_height)
×
547
        if tx_was_added:
×
548
            self.lnworker.wallet.set_label(new_tx.txid(), name)
×
549
            if old_tx and old_tx.txid() != new_tx.txid():
×
550
                self.lnworker.wallet.set_label(old_tx.txid(), None)
×
551
            util.trigger_callback('wallet_updated', self.lnworker.wallet)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc