Coverage for tests/wallet/test_pool.py: 91%

117 statements  

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

1from __future__ import annotations 

2 

3import logging 

4 

5import pytest 

6from monero import MoneroDaemonRpc, MoneroSubmitTxResult, MoneroTxConfig, MoneroTxWallet, MoneroWallet 

7from typing_extensions import override 

8 

9from utils import TestUtils, TxWalletUtils, WalletSendUtils 

10from .base_test_monero_wallet import BaseTestMoneroWallet 

11from .wallet_support import FullWalletSupport, RpcWalletSupport, KeysWalletSupport 

12 

13logger: logging.Logger = logging.getLogger("TestMoneroWalletPool") 

14 

15 

16class BaseTestMoneroWalletPool(BaseTestMoneroWallet): 

17 """Common wallet pool tests.""" 

18 

19 # Can sync with txs in the pool sent from/to the same account 

20 # TODO this test fails because wallet does not recognize pool tx sent from/to same account 

21 @pytest.mark.skipif(TestUtils.TEST_RELAYS is False, reason="TEST_RELAYS disabled") 

22 def test_sync_with_pool_same_accounts(self, daemon: MoneroDaemonRpc, wallet: MoneroWallet) -> None: 

23 config: MoneroTxConfig = MoneroTxConfig() 

24 config.account_index = 0 

25 config.address = wallet.get_primary_address() 

26 config.amount = TxWalletUtils.MAX_FEE * 5 

27 config.relay = True 

28 WalletSendUtils.test_sync_with_pool_submit(daemon, wallet, config) 

29 

30 # Can sync with txs submitted and flushed from the pool 

31 @pytest.mark.skipif(TestUtils.TEST_NON_RELAYS is False, reason="TEST_RELAYS disabled") 

32 @pytest.mark.skipif(TestUtils.LITE_MODE, reason="LITE_MODE enabled") 

33 @pytest.mark.skipif(TestUtils.CI, reason="CI is too slow to catchup failed txs (see wallet2::process_unconfirmed_transfer)") 

34 def test_sync_with_pool_submit_and_flush(self, daemon: MoneroDaemonRpc, wallet: MoneroWallet) -> None: 

35 logger.warning("This test takes at least 500 seconds (~8 minutes) to catchup failed txs (see wallet2::process_unconfirmed_transfer).") 

36 config: MoneroTxConfig = MoneroTxConfig() 

37 config.account_index = 2 

38 config.address = wallet.get_primary_address() 

39 config.amount = TxWalletUtils.MAX_FEE * 5 

40 config.relay = False 

41 WalletSendUtils.test_sync_with_pool_submit(daemon, wallet, config) 

42 

43 # Can sync with txs submitted and relayed from the pool 

44 @pytest.mark.skipif(TestUtils.TEST_RELAYS is False, reason="TEST_RELAYS disabled") 

45 @pytest.mark.skipif(TestUtils.LITE_MODE, reason="LITE_MODE enabled") 

46 def test_sync_with_pool_submit_and_relay(self, daemon: MoneroDaemonRpc, wallet: MoneroWallet) -> None: 

47 config: MoneroTxConfig = MoneroTxConfig() 

48 config.account_index = 2 

49 config.address = wallet.get_primary_address() 

50 config.amount = TxWalletUtils.MAX_FEE * 5 

51 config.relay = True 

52 WalletSendUtils.test_sync_with_pool_submit(daemon, wallet, config) 

53 

54 # can sync with txs relayed to the pool 

55 @pytest.mark.skipif(TestUtils.TEST_RELAYS is False, reason="TEST_RELAYS disabled") 

56 @pytest.mark.skipif(TestUtils.LITE_MODE, reason="LITE_MODE enabled") 

57 def test_sync_with_pool_relay(self, daemon: MoneroDaemonRpc, wallet: MoneroWallet) -> None: 

58 # wait one time for wallet txs in the pool to clear 

59 TestUtils.WALLET_TX_TRACKER.wait_for_txs_to_clear_pool(wallet) 

60 

61 # record wallet balances before submitting tx to pool 

62 balance_before: int = wallet.get_balance() 

63 unlocked_balance_before: int = wallet.get_unlocked_balance() 

64 

65 # create config 

66 config: MoneroTxConfig = MoneroTxConfig() 

67 config.account_index = 2 

68 config.address = wallet.get_primary_address() 

69 config.amount = TxWalletUtils.MAX_FEE * 5 

70 

71 # create tx to relay 

72 tx: MoneroTxWallet = wallet.create_tx(config) 

73 assert tx.hash is not None 

74 assert tx.full_hex is not None 

75 assert tx.fee is not None 

76 

77 # create another tx using same config which would be double spend 

78 tx_double_spend: MoneroTxWallet = wallet.create_tx(config) 

79 assert tx_double_spend.hash is not None 

80 assert tx_double_spend.full_hex is not None 

81 

82 # relay first tx directly to daemon 

83 result: MoneroSubmitTxResult = daemon.submit_tx_hex(tx.full_hex) 

84 assert result.is_good, "Transaction could not be submitted to the pool" 

85 

86 # sync wallet which updates from pool 

87 wallet.sync() 

88 

89 # collect issues to report at end of test 

90 issues: list[str] = [] 

91 

92 # wallet should be aware of tx 

93 fetched: MoneroTxWallet | None = wallet.get_tx(tx.hash) 

94 assert fetched is not None, "Wallet should be aware of its tx in pool after syncing" 

95 

96 # test wallet balances 

97 if unlocked_balance_before == wallet.get_unlocked_balance(): 

98 issues.append("unlocked balance should have decreased after relaying tx directly to the daemon") 

99 

100 if balance_before == wallet.get_balance(): 

101 issues.append("balance should have decreased after relaying tx directly to the daemon") 

102 

103 expected_balance: int = balance_before - tx.fee 

104 if expected_balance != wallet.get_balance(): 

105 issues.append(f"expected balance after relaying tx directly to the daemon to be {expected_balance} but was {wallet.get_balance()}") 

106 

107 # submit double spend tx 

108 result_double_spend: MoneroSubmitTxResult = daemon.submit_tx_hex(tx_double_spend.full_hex) 

109 if result_double_spend.is_good: 

110 daemon.flush_tx_pool(tx_double_spend.hash) 

111 issues.append("Tx submit result should have been double spend") 

112 

113 # sync wallet which updates from pool 

114 wallet.sync() 

115 

116 # create tx using same config which is no longer double spend 

117 tx2: MoneroTxWallet | None = None 

118 try: 

119 config_copy: MoneroTxConfig = config.copy() 

120 config_copy.relay = True 

121 tx2 = wallet.create_tx(config_copy) 

122 except Exception as e: 

123 logger.warning(e) 

124 # TODO monero-project: this fails meaning wallet did not recognize tx relayed directly to pool 

125 issues.append("creating and sending tx through wallet should succeed after syncing wallet with pool but creates a double spend") 

126 

127 # submit the transaction to the pool and test 

128 if tx2 is not None: 

129 assert tx2.hash is not None 

130 assert tx2.full_hex is not None 

131 result2: MoneroSubmitTxResult = daemon.submit_tx_hex(tx2.full_hex, True) 

132 if result2.is_double_spend: 

133 issues.append("creating and submitting tx to daemon should succeed after syncing wallet with pool but was a double spend") 

134 else: 

135 assert result2.is_good 

136 

137 wallet.sync() 

138 if wallet.get_tx(tx2.hash) is None: 

139 issues.append("wallet should be aware of tx created and relayed through it after syncing with pool") 

140 

141 daemon.flush_tx_pool(tx2.hash) 

142 

143 # should be no issues 

144 for issue in issues: 

145 logger.warning(issue) 

146 

147 assert len(issues) == 0, f"test_sync_with_pool_relay has {len(issues)} issues" 

148 

149 

150@pytest.mark.integration 

151class TestMoneroWalletFullPool(FullWalletSupport, BaseTestMoneroWalletPool): 

152 """Full wallet pool integration tests.""" 

153 

154 

155@pytest.mark.integration 

156class TestMoneroWalletRpcPool(RpcWalletSupport, BaseTestMoneroWalletPool): 

157 """Rpc wallet pool integration tests.""" 

158 

159 

160@pytest.mark.unit 

161class TestMoneroWalletKeysPool(KeysWalletSupport, BaseTestMoneroWalletPool): 

162 """Keys-only wallet pool unit tests.""" 

163 

164 @pytest.mark.not_supported 

165 @override 

166 def test_sync_with_pool_same_accounts(self, daemon: MoneroDaemonRpc, wallet: MoneroWallet) -> None: 

167 return super().test_sync_with_pool_same_accounts(daemon, wallet) 

168 

169 @pytest.mark.not_supported 

170 @override 

171 def test_sync_with_pool_submit_and_flush(self, daemon: MoneroDaemonRpc, wallet: MoneroWallet) -> None: 

172 return super().test_sync_with_pool_submit_and_flush(daemon, wallet) 

173 

174 @pytest.mark.not_supported 

175 @override 

176 def test_sync_with_pool_submit_and_relay(self, daemon: MoneroDaemonRpc, wallet: MoneroWallet) -> None: 

177 return super().test_sync_with_pool_submit_and_relay(daemon, wallet) 

178 

179 @pytest.mark.not_supported 

180 @override 

181 def test_sync_with_pool_relay(self, daemon: MoneroDaemonRpc, wallet: MoneroWallet) -> None: 

182 return super().test_sync_with_pool_relay(daemon, wallet)