Coverage for tests/utils/wallet_sweeper.py: 100%

72 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-22 19:49 +0000

1from monero import ( 

2 MoneroWallet, MoneroSubaddress, 

3 MoneroTxConfig, MoneroTxWallet, 

4 MoneroOutputQuery, MoneroOutputWallet, 

5 MoneroTxQuery 

6) 

7 

8from .context import TxContext 

9from .assert_utils import AssertUtils 

10from .tx_wallet_utils import TxWalletUtils 

11from .test_utils import TestUtils 

12 

13 

14class WalletSweeper: 

15 """Wallet funds sweeper.""" 

16 

17 _wallet: MoneroWallet 

18 """Test wallet instance to sweep funds from.""" 

19 _sweep_each_subaddress: bool | None 

20 """Indicates if each subaddress must be sweeped out.""" 

21 

22 def __init__(self, wallet: MoneroWallet, sweep_each_subaddress: bool | None) -> None: 

23 """Initialize a new wallet sweeper. 

24 

25 :param MoneroWallet wallet: wallet to sweep funds from. 

26 :param bool sweep_each_subaddress: sweep each wallet subaddress. 

27 """ 

28 self._wallet = wallet 

29 self._sweep_each_subaddress = sweep_each_subaddress 

30 

31 def _check_for_balance(self) -> None: 

32 """Checks for subaddresses with enough unlocked balance for sweep test.""" 

33 # verify 2 subaddresses with enough unlocked balance to cover the fee 

34 subaddresses_balance: list[MoneroSubaddress] = [] 

35 subaddresses_unlocked: list[MoneroSubaddress] = [] 

36 

37 for account in self._wallet.get_accounts(True): 

38 for subaddress in account.subaddresses: 

39 assert subaddress.balance is not None 

40 assert subaddress.unlocked_balance is not None 

41 if subaddress.balance > TxWalletUtils.MAX_FEE: 

42 subaddresses_balance.append(subaddress) 

43 if subaddress.unlocked_balance > TxWalletUtils.MAX_FEE: 

44 subaddresses_unlocked.append(subaddress) 

45 

46 assert len(subaddresses_balance) >= 2, "Test requires multiple accounts with a balance greater than the fee; run send to multiple first" 

47 assert len(subaddresses_unlocked) >= 2, "Wallet is waiting on unlocked funds" 

48 

49 def _check_outputs(self) -> None: 

50 """Check for outputs amount after sweep.""" 

51 # all unspent, unlocked outputs must be less than fee 

52 query: MoneroOutputQuery = MoneroOutputQuery() 

53 query.is_spent = False 

54 tx_query: MoneroTxQuery = MoneroTxQuery() 

55 tx_query.is_locked = False 

56 query.set_tx_query(tx_query, True) 

57 spendable_outputs: list[MoneroOutputWallet] = self._wallet.get_outputs(query) 

58 for spendable_output in spendable_outputs: 

59 assert spendable_output.amount is not None 

60 assert spendable_output.amount < TxWalletUtils.MAX_FEE, f"Unspent output should have been swept\n{spendable_output.serialize()}" 

61 

62 # all subaddress unlocked balances must be less than fee 

63 for account in self._wallet.get_accounts(True): 

64 for subaddress in account.subaddresses: 

65 assert subaddress.unlocked_balance is not None 

66 assert subaddress.unlocked_balance < TxWalletUtils.MAX_FEE, "No subaddress should have more unlocked than the fee" 

67 

68 def sweep(self) -> None: 

69 """Sweep outputs from wallet.""" 

70 # cleanup and check balance 

71 TestUtils.WALLET_TX_TRACKER.wait_for_txs_to_clear_pool(self._wallet) 

72 self._check_for_balance() 

73 

74 # sweep funds 

75 destination: str = self._wallet.get_primary_address() 

76 config: MoneroTxConfig = MoneroTxConfig() 

77 config.address = destination 

78 config.sweep_each_subaddress = self._sweep_each_subaddress 

79 config.relay = True 

80 copy: MoneroTxConfig = config.copy() 

81 txs: list[MoneroTxWallet] = self._wallet.sweep_unlocked(config) 

82 # config is unchanged 

83 AssertUtils.assert_equals(config, copy) 

84 for tx in txs: 

85 assert tx.tx_set is not None 

86 assert tx in tx.tx_set.txs 

87 assert tx.tx_set.multisig_tx_hex is None 

88 assert tx.tx_set.signed_tx_hex is None 

89 assert tx.tx_set.unsigned_tx_hex is None 

90 

91 assert len(txs) > 0 

92 for tx in txs: 

93 assert tx.outgoing_transfer is not None 

94 config = MoneroTxConfig() 

95 config.address = destination 

96 config.account_index = tx.outgoing_transfer.account_index 

97 config.sweep_each_subaddress = self._sweep_each_subaddress 

98 config.relay = True 

99 

100 ctx: TxContext = TxContext() 

101 ctx.wallet = self._wallet 

102 ctx.config = config 

103 ctx.is_send_response = True 

104 ctx.is_sweep_response = True 

105 TxWalletUtils.test_tx_wallet(tx, ctx) 

106 

107 self._check_outputs()