Skip to content

Commit 27603e9

Browse files
committed
tests: add unittest for TxBatch._to_sweep_after()
Adds unittest for `TxBatch._to_sweep_after()` anchor claiming part. # Conflicts: # tests/test_txbatcher.py
1 parent 5bff1b5 commit 27603e9

File tree

1 file changed

+58
-4
lines changed

1 file changed

+58
-4
lines changed

tests/test_txbatcher.py

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import unittest
21
import logging
32
from unittest import mock
43
import asyncio
54
from aiorpcx import timeout_after
65

7-
from electrum import storage, bitcoin, keystore, wallet
6+
import electrum.fee_policy
7+
from electrum import keystore, wallet, lnutil
88
from electrum import SimpleConfig
99
from electrum import util
10-
from electrum.address_synchronizer import TX_HEIGHT_UNCONFIRMED, TX_HEIGHT_UNCONF_PARENT, TX_HEIGHT_LOCAL
10+
from electrum.address_synchronizer import TX_HEIGHT_UNCONFIRMED
1111
from electrum.transaction import Transaction, PartialTxInput, PartialTxOutput, TxOutpoint
1212
from electrum.logging import console_stderr_handler, Logger
1313
from electrum.submarine_swaps import SwapManager, SwapData
14-
from electrum.lnsweep import SweepInfo
14+
from electrum.lnsweep import SweepInfo, sweep_ctx_anchor
1515
from electrum.fee_policy import FeeTimeEstimates
1616

1717
from . import ElectrumTestCase
@@ -85,6 +85,28 @@ def is_connected(self):
8585
name='swap claim',
8686
can_be_batched=True,
8787
)
88+
anchor_chan_ctx = Transaction(
89+
"02000000000101d24af3b7adefff5a068f736d64842c18da7087b41ba43ab5b999c545c5f1606501000000008d0d5d8"
90+
"0024a010000000000002200207b95cb2555b3f8fc246d26ac38023ec8edf423d70b41dfe17efc89baa6e0cc72740003"
91+
"000000000022002075f8af76a5b5c4b25e4aee3a4f96a190a168bde5d9761de8d45d3e49cd6f1d82040047304402200"
92+
"6524eb2f467bf1eacd2116ea79a80c182eb95e18d0fa24fc5c600581ec4aa5f02206f50bdfc3577ca3bc9892eade7d8"
93+
"1a9b06b9a8803296fad689af9fee9375191d0147304402202112d08ffa79010b1d698ca9fb0790119a42f7b70e183a2"
94+
"a05dbf79c32806d3b022001cd3e3aec8c1142c8689e9e679c684ea82dcde8c86cf8e55466c743b3647b1f0147522103"
95+
"0551e6017a0e9dbffd468c2a08ecf8446b532f0a6d5db291eb77026f2ef3deb421034f986fd43561d52a96b19fbdd0c"
96+
"296f0442034d3f3f63fc394320a95750d42a852ae08572d20"
97+
)
98+
chan_multisig_key = lnutil.Keypair(
99+
privkey="4c8b2c19d6528f54a4c900d87b3d8dbf746314925d126e0abf8e2ed965a9302f",
100+
pubkey="034f986fd43561d52a96b19fbdd0c296f0442034d3f3f63fc394320a95750d42a8",
101+
)
102+
anchor_txin = sweep_ctx_anchor(ctx=anchor_chan_ctx, multisig_key=chan_multisig_key)
103+
ANCHOR_SWEEP_INFO = SweepInfo(
104+
name='local_anchor',
105+
cltv_abs=None,
106+
txin=anchor_txin,
107+
txout=None,
108+
can_be_batched=True,
109+
)
88110

89111

90112
class TestTxBatcher(ElectrumTestCase):
@@ -222,6 +244,38 @@ async def test_sweep_from_submarine_swap(self, mock_save_db):
222244
assert new_tx.inputs()[0].prevout == tx.inputs()[0].prevout == txin.prevout
223245
assert output1 in new_tx.outputs()
224246

247+
async def test_to_sweep_after_anchor_sweep_conditions(self):
248+
# create wallet
249+
wallet = self._create_wallet()
250+
wallet.txbatcher.add_sweep_input('lnwatcher', ANCHOR_SWEEP_INFO)
251+
anchor_batch = wallet.txbatcher.tx_batches['lnwatcher']
252+
253+
# does not return sweep info if prev_tx is not in db
254+
to_sweep_no_tx = anchor_batch._to_sweep_after(tx=None)
255+
assert not to_sweep_no_tx
256+
257+
# returns sweep input if anchor_chan_ctx conf < 1
258+
wallet.adb.db.transactions[anchor_chan_ctx.txid()] = anchor_chan_ctx
259+
to_sweep_no_conf = anchor_batch._to_sweep_after(tx=None)
260+
assert to_sweep_no_conf[anchor_txin.prevout] == ANCHOR_SWEEP_INFO
261+
assert len(to_sweep_no_conf) == 1
262+
263+
# does not return sweep input if ctx fee is already higher than target fee
264+
with mock.patch.object(wallet.adb, 'get_tx_fee', return_value=2000), \
265+
mock.patch.object(electrum.fee_policy.FeePolicy, 'estimate_fee', return_value=1000):
266+
to_sweep_high_fee = anchor_batch._to_sweep_after(tx=None)
267+
assert not to_sweep_high_fee
268+
269+
# after the ctx is confirmed the anchor claim shouldn't be broadcast anymore
270+
wallet.adb.receive_tx_callback(anchor_chan_ctx, tx_height=1)
271+
tx_mined_status = wallet.adb.get_tx_height(anchor_chan_ctx.txid())
272+
wallet.adb.add_verified_tx(anchor_chan_ctx.txid(), tx_mined_status._replace(conf=1))
273+
assert anchor_txin.prevout in anchor_batch.batch_inputs
274+
to_sweep_ctx_conf = anchor_batch._to_sweep_after(tx=None)
275+
assert not to_sweep_ctx_conf
276+
assert not anchor_batch.batch_inputs
277+
assert wallet.txbatcher.tx_batches['lnwatcher'] == anchor_batch
278+
225279
async def _wait_for_base_tx(self, txbatch, should_be_none=False):
226280
async with timeout_after(10):
227281
while True:

0 commit comments

Comments
 (0)