Coverage for tests/wallet/test_sweep.py: 91%
260 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 19:49 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 19:49 +0000
1from __future__ import annotations
3import logging
5import pytest
6from monero import (
7 MoneroAccount,
8 MoneroOutputQuery,
9 MoneroOutputWallet,
10 MoneroSubaddress,
11 MoneroTxConfig,
12 MoneroTxQuery,
13 MoneroTxWallet,
14 MoneroWallet,
15)
16from typing_extensions import override
18from utils import IntegrationTestUtils, OutputUtils, TestUtils, TxContext, TxWalletUtils, WalletSendUtils
19from .base_test_monero_wallet import BaseTestMoneroWallet
20from .wallet_support import FullWalletSupport, RpcWalletSupport, KeysWalletSupport
22logger: logging.Logger = logging.getLogger("TestMoneroWalletSweep")
25class BaseTestMoneroWalletSweep(BaseTestMoneroWallet):
26 """Common wallet sweep tests."""
28 # Can sweep individual outputs identified by their key images
29 @pytest.mark.skipif(TestUtils.TEST_RELAYS is False, reason="TEST_RELAYS disabled")
30 def test_sweep_outputs(self, wallet: MoneroWallet) -> None:
31 TestUtils.WALLET_TX_TRACKER.wait_for_txs_to_clear_pool(wallet)
33 # test config
34 num_outputs: int = 3
36 # get outputs to sweep (not spent, unlocked, and amount >= fee)
37 query: MoneroOutputQuery = MoneroOutputQuery()
38 query.is_spent = False
39 tx_query: MoneroTxQuery = MoneroTxQuery()
40 tx_query.is_locked = False
41 query.set_tx_query(tx_query, True)
42 spendable_unlocked_outputs: list[MoneroOutputWallet] = wallet.get_outputs(query)
43 outputs_to_sweep: list[MoneroOutputWallet] = []
44 for spendable_output in spendable_unlocked_outputs:
45 if len(outputs_to_sweep) >= num_outputs:
46 break
47 assert spendable_output.amount is not None
48 if spendable_output.amount > TxWalletUtils.MAX_FEE:
49 outputs_to_sweep.append(spendable_output)
51 assert len(outputs_to_sweep) >= num_outputs, "Wallet does not have enough sweepable outputs; run send tests"
53 # sweep each output by key image
54 for output in outputs_to_sweep:
55 OutputUtils.test_output_wallet(output)
56 assert output.is_spent is False
57 assert output.tx is not None
58 assert isinstance(output.tx, MoneroTxWallet)
59 assert output.tx.is_locked is False
60 assert output.amount is not None
62 if output.amount <= TxWalletUtils.MAX_FEE:
63 continue
65 # sweep output to address
66 assert output.account_index is not None
67 assert output.subaddress_index is not None
68 assert output.key_image is not None
69 assert output.key_image.hex is not None
70 address: str = wallet.get_address(output.account_index, output.subaddress_index)
71 config: MoneroTxConfig = MoneroTxConfig()
72 config.address = address
73 config.key_image = output.key_image.hex
74 config.relay = True
75 tx: MoneroTxWallet = wallet.sweep_output(config)
77 # test result tx
78 ctx: TxContext = TxContext()
79 ctx.wallet = wallet
80 ctx.config = config
81 ctx.config.can_split = False
82 ctx.is_send_response = True
83 ctx.is_sweep_response = True
84 ctx.is_sweep_output_response = True
85 TxWalletUtils.test_tx_wallet(tx, ctx)
87 # get outputs after sweeping
88 after_outputs: list[MoneroOutputWallet] = wallet.get_outputs()
90 # swept outputs are now spent
91 for after_output in after_outputs:
92 assert after_output.key_image is not None
94 for output in outputs_to_sweep:
95 assert output.key_image is not None
96 if output.key_image.hex == after_output.key_image.hex:
97 assert after_output.is_spent is True, "Output should be spent"
99 # Can sweep individual outputs identified by their key images
100 @pytest.mark.skipif(TestUtils.TEST_RELAYS is False, reason="TEST_RELAYS disabled")
101 def test_sweep_dust_no_relay(self, wallet: MoneroWallet) -> None:
102 TestUtils.WALLET_TX_TRACKER.wait_for_txs_to_clear_pool(wallet)
104 # sweep dust which returns empty list if no dust to sweep (dust does not exist after rct)
105 txs: list[MoneroTxWallet] = wallet.sweep_dust(False)
106 if len(txs) == 0:
107 return
109 # test txs
110 ctx: TxContext = TxContext()
111 ctx.is_send_response = True
112 ctx.config = MoneroTxConfig()
113 ctx.config.relay = False
114 ctx.is_sweep_response = True
115 for tx in txs:
116 TxWalletUtils.test_tx_wallet(tx, ctx)
118 # relay txs
119 metadatas: list[str] = []
120 for tx in txs:
121 assert tx.metadata is not None
122 metadatas.append(tx.metadata)
124 tx_hashes: list[str] = wallet.relay_txs(metadatas)
125 assert len(tx_hashes) == len(txs)
126 for tx_hash in tx_hashes:
127 assert len(tx_hash) == 64
129 # fetch and test txs
130 tx_query: MoneroTxQuery = MoneroTxQuery()
131 tx_query.hashes = tx_hashes
132 txs = wallet.get_txs(tx_query)
133 ctx.config.relay = True
134 for tx in txs:
135 TxWalletUtils.test_tx_wallet(tx, ctx)
137 # Can sweep dust
138 @pytest.mark.skipif(TestUtils.TEST_RELAYS is False, reason="TEST_RELAYS disabled")
139 def test_sweep_dust(self, wallet: MoneroWallet) -> None:
140 TestUtils.WALLET_TX_TRACKER.wait_for_txs_to_clear_pool(wallet)
142 # sweep dust which returns empty list if no dust to sweep (dust does not exist after rct)
143 txs: list[MoneroTxWallet] = wallet.sweep_dust(True)
145 # test any txs
146 ctx: TxContext = TxContext()
147 ctx.wallet = wallet
148 ctx.config = None
149 ctx.is_send_response = True
150 ctx.is_sweep_response = True
151 for tx in txs:
152 TxWalletUtils.test_tx_wallet(tx, ctx)
154 # Can sweep subaddresses
155 @pytest.mark.skipif(TestUtils.TEST_RESETS is False, reason="TEST_RESETS disabled")
156 def test_sweep_subaddresses(self, wallet: MoneroWallet) -> None:
157 TestUtils.WALLET_TX_TRACKER.wait_for_txs_to_clear_pool(wallet)
158 NUM_SUBADDRESSES_TO_SWEEP: int = 2
160 # collect subaddresses with balance and unlocked balance
161 subaddresses: list[MoneroSubaddress] = []
162 subaddresses_balance: list[MoneroSubaddress] = []
163 subaddresses_unlocked: list[MoneroSubaddress] = []
164 for account in wallet.get_accounts(True):
165 if account.index == 0:
166 # skip default account
167 continue
168 for subaddress in account.subaddresses:
169 subaddresses.append(subaddress)
170 assert subaddress.balance is not None
171 if subaddress.balance > TxWalletUtils.MAX_FEE:
172 subaddresses_balance.append(subaddress)
173 assert subaddress.unlocked_balance is not None
174 if subaddress.unlocked_balance > TxWalletUtils.MAX_FEE:
175 subaddresses_unlocked.append(subaddress)
177 # test requires at least one more subaddresses than the number being swept to verify it does not change
178 msg: str = f"Test requires balance in at least {NUM_SUBADDRESSES_TO_SWEEP + 1} subaddresses from non-default account; run send-to-multiple tests"
179 assert len(subaddresses_balance) >= NUM_SUBADDRESSES_TO_SWEEP + 1, msg
180 assert len(subaddresses_unlocked) >= NUM_SUBADDRESSES_TO_SWEEP + 1, "Wallet is waiting on unlocked funds"
182 # sweep from first unlocked subaddresses
183 for i in range(NUM_SUBADDRESSES_TO_SWEEP):
184 # sweep unlocked account
185 unlocked_subaddress: MoneroSubaddress = subaddresses_unlocked[i]
186 config: MoneroTxConfig = MoneroTxConfig()
187 config.address = wallet.get_primary_address()
188 assert unlocked_subaddress.account_index is not None
189 assert unlocked_subaddress.index is not None
190 config.account_index = unlocked_subaddress.account_index
191 config.subaddress_indices.append(unlocked_subaddress.index)
192 config.relay = True
193 txs: list[MoneroTxWallet] = wallet.sweep_unlocked(config)
195 # test transactions
196 assert len(txs) > 0
197 for tx in txs:
198 assert tx.tx_set is not None
199 assert tx in tx.tx_set.txs
200 ctx: TxContext = TxContext()
201 ctx.wallet = wallet
202 ctx.config = config
203 ctx.is_send_response = True
204 ctx.is_sweep_response = True
205 TxWalletUtils.test_tx_wallet(tx, ctx)
207 # assert unlocked balance is less than max fee
208 subaddress: MoneroSubaddress = wallet.get_subaddress(unlocked_subaddress.account_index, unlocked_subaddress.index)
209 assert subaddress.unlocked_balance is not None
210 assert subaddress.unlocked_balance < TxWalletUtils.MAX_FEE
212 # test subaddresses after sweeping
213 subaddresses_after: list[MoneroSubaddress] = []
214 for account in wallet.get_accounts(True):
215 if account.index == 0:
216 # skip default account
217 continue
218 for subaddress in account.subaddresses:
219 subaddresses_after.append(subaddress)
221 assert len(subaddresses) == len(subaddresses_after)
222 for i, subaddress_before in enumerate(subaddresses):
223 subaddress_after: MoneroSubaddress = subaddresses_after[i]
224 assert subaddress_after.unlocked_balance is not None
226 # determine if subaddress was swept
227 swept: bool = False
228 for j in range(NUM_SUBADDRESSES_TO_SWEEP):
229 subaddress_unlocked: MoneroSubaddress = subaddresses_unlocked[j]
230 account_eq: bool = subaddress_unlocked.account_index == subaddress_before.account_index
231 subaddr_eq: bool = subaddress_unlocked.index == subaddress_before.index
232 if account_eq and subaddr_eq:
233 swept = True
234 break
236 # assert unlocked balance is less than max fee if swept, unchanged otherwise
237 if swept:
238 assert subaddress_after.unlocked_balance < TxWalletUtils.MAX_FEE
239 else:
240 assert subaddress_before.unlocked_balance == subaddress_after.unlocked_balance
242 # Can sweep accounts
243 @pytest.mark.skipif(TestUtils.TEST_RESETS is False, reason="TEST_RESETS disabled")
244 def test_sweep_accounts(self, wallet: MoneroWallet) -> None:
245 TestUtils.WALLET_TX_TRACKER.wait_for_txs_to_clear_pool(wallet)
246 NUM_ACCOUNTS_TO_SWEEP: int = 1
248 # collect accounts with sufficient balance and unlocked balance to cover the fee
249 accounts: list[MoneroAccount] = wallet.get_accounts(True)
250 accounts_balance: list[MoneroAccount] = []
251 accounts_unlocked: list[MoneroAccount] = []
252 for account in accounts:
253 if account.index == 0:
254 # skip default account
255 continue
256 assert account.balance is not None
257 assert account.unlocked_balance is not None
258 if account.balance > TxWalletUtils.MAX_FEE:
259 accounts_balance.append(account)
260 if account.unlocked_balance > TxWalletUtils.MAX_FEE:
261 accounts_unlocked.append(account)
263 # test requires at least one more accounts than the number being swept to verify it does not change
265 msg: str = f"Test requires balance greater than the fee in at least {NUM_ACCOUNTS_TO_SWEEP + 1} non-default accounts; run send-to-multiple tests"
266 assert len(accounts_balance) >= NUM_ACCOUNTS_TO_SWEEP + 1, msg
267 assert len(accounts_unlocked) >= NUM_ACCOUNTS_TO_SWEEP + 1, "Wallet is waiting on unlocked funds"
269 # sweep from first unlocked accounts
270 for i in range(NUM_ACCOUNTS_TO_SWEEP):
271 # sweep unlocked account
272 unlocked_account: MoneroAccount = accounts_unlocked[i]
273 assert unlocked_account.index is not None
274 config: MoneroTxConfig = MoneroTxConfig()
275 config.address = wallet.get_primary_address()
276 config.account_index = unlocked_account.index
277 config.relay = True
278 txs: list[MoneroTxWallet] = wallet.sweep_unlocked(config)
280 # test transactions
281 assert len(txs) > 0
282 for tx in txs:
283 ctx: TxContext = TxContext()
284 ctx.wallet = wallet
285 ctx.config = config
286 ctx.is_send_response = True
287 ctx.is_sweep_response = True
288 TxWalletUtils.test_tx_wallet(tx, ctx)
289 assert tx.tx_set is not None
290 assert tx in tx.tx_set.txs
292 # assert unlocked account balance less than max fee
293 account: MoneroAccount = wallet.get_account(unlocked_account.index)
295 # test accounts after sweeping
296 accounts_after: list[MoneroAccount] = wallet.get_accounts(True)
297 assert len(accounts) == len(accounts_after)
298 for i, account_before in enumerate(accounts):
299 account_after: MoneroAccount = accounts_after[i]
300 assert account_after.unlocked_balance is not None
302 # determine if account was swept
303 swept: bool = False
304 for j in range(NUM_ACCOUNTS_TO_SWEEP):
305 account_unlocked: MoneroAccount = accounts_unlocked[j]
306 if account_unlocked.index == account_before.index:
307 swept = True
308 break
310 # assert unlocked balance is less than max fee if swept, unchanged otherwise
311 if swept:
312 assert account_after.unlocked_balance < TxWalletUtils.MAX_FEE
313 else:
314 assert account_after.unlocked_balance == account_after.unlocked_balance
316 # Can sweep the whole wallet by accounts
317 @pytest.mark.skipif(TestUtils.TEST_RESETS is False, reason="TEST_RESETS disabled")
318 def test_sweep_wallet_by_accounts(self, wallet: MoneroWallet) -> None:
319 IntegrationTestUtils.fund_wallet_and_wait_for_unlocked(wallet)
320 WalletSendUtils.test_sweep_wallet(wallet, None)
322 # Can sweep the whole wallet by subaddresses
323 @pytest.mark.skipif(TestUtils.TEST_RESETS is False, reason="TEST_RESETS disabled")
324 @pytest.mark.xfail(reason="TODO wallet2 error: No unlocked balance in the specified subaddress(es)")
325 def test_sweep_wallet_by_subaddresses(self, wallet: MoneroWallet) -> None:
326 IntegrationTestUtils.fund_wallet_and_wait_for_unlocked(wallet)
327 WalletSendUtils.test_sweep_wallet(wallet, True)
330@pytest.mark.integration
331class TestMoneroWalletFullSweep(FullWalletSupport, BaseTestMoneroWalletSweep):
332 """Full wallet sweep integration tests."""
335@pytest.mark.integration
336class TestMoneroWalletRpcSweep(RpcWalletSupport, BaseTestMoneroWalletSweep):
337 """Rpc wallet sweep integration tests."""
340@pytest.mark.unit
341class TestMoneroWalletKeysSweep(KeysWalletSupport, BaseTestMoneroWalletSweep):
342 """Keys-only wallet sweep unit tests."""
344 @pytest.mark.not_supported
345 @override
346 def test_sweep_outputs(self, wallet: MoneroWallet) -> None:
347 return super().test_sweep_outputs(wallet)
349 @pytest.mark.not_supported
350 @override
351 def test_sweep_dust_no_relay(self, wallet: MoneroWallet) -> None:
352 return super().test_sweep_dust_no_relay(wallet)
354 @pytest.mark.not_supported
355 @override
356 def test_sweep_dust(self, wallet: MoneroWallet) -> None:
357 return super().test_sweep_dust(wallet)
359 @pytest.mark.not_supported
360 @override
361 def test_sweep_subaddresses(self, wallet: MoneroWallet) -> None:
362 return super().test_sweep_subaddresses(wallet)
364 @pytest.mark.not_supported
365 @override
366 def test_sweep_accounts(self, wallet: MoneroWallet) -> None:
367 return super().test_sweep_accounts(wallet)
369 @pytest.mark.not_supported
370 @override
371 def test_sweep_wallet_by_accounts(self, wallet: MoneroWallet) -> None:
372 return super().test_sweep_wallet_by_accounts(wallet)
374 @pytest.mark.not_supported
375 @override
376 def test_sweep_wallet_by_subaddresses(self, wallet: MoneroWallet) -> None:
377 return super().test_sweep_wallet_by_subaddresses(wallet)