Coverage for tests/daemon/rpc/test_tx_pool.py: 97%

162 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 ( 

7 MoneroDaemonRpc, 

8 MoneroFeeEstimate, 

9 MoneroSubmitTxResult, 

10 MoneroTx, 

11 MoneroTxPoolStats, 

12 MoneroWalletRpc, 

13) 

14 

15from utils import ( 

16 DaemonUtils, 

17 GenUtils, 

18 SubmitThenRelayTxTester, 

19 TestContext, 

20 TestUtils as Utils, 

21 TxUtils, 

22 WalletTxsUtils, 

23) 

24from .base_test_monero_daemon_rpc import BaseTestMoneroDaemonRpc 

25 

26logger: logging.Logger = logging.getLogger("TestMoneroDaemonRpcTxPool") 

27 

28 

29@pytest.mark.integration 

30class TestMoneroDaemonRpcTxPool(BaseTestMoneroDaemonRpc): 

31 """Rpc daemon tx pool integration tests.""" 

32 

33 # Can get fee estimate 

34 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") 

35 def test_get_fee_estimate(self, daemon: MoneroDaemonRpc) -> None: 

36 fee_estimate: MoneroFeeEstimate = daemon.get_fee_estimate() 

37 logger.debug(f"Testing fee estimate: {fee_estimate.serialize()}") 

38 GenUtils.test_unsigned_big_integer(fee_estimate.fee, True) 

39 assert len(fee_estimate.fees) == 4, "Expected 4 fees" 

40 for fee in fee_estimate.fees: 

41 GenUtils.test_unsigned_big_integer(fee, True) 

42 GenUtils.test_unsigned_big_integer(fee_estimate.quantization_mask, True) 

43 

44 # Can get all transactions in the transaction pool 

45 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") 

46 def test_get_txs_in_pool(self, daemon: MoneroDaemonRpc, wallet: MoneroWalletRpc) -> None: 

47 Utils.WALLET_TX_TRACKER.wait_for_txs_to_clear_pool(wallet) 

48 

49 # submit tx to pool but don't relay 

50 tx: MoneroTx = WalletTxsUtils.get_unrelayed_tx(wallet, 1) 

51 assert tx.hash is not None 

52 assert tx.full_hex is not None 

53 result: MoneroSubmitTxResult = daemon.submit_tx_hex(tx.full_hex, True) 

54 DaemonUtils.test_submit_tx_result_good(result) 

55 assert result.is_relayed is False 

56 

57 # fetch txs in pool 

58 txs: list[MoneroTx] = daemon.get_tx_pool() 

59 num_txs: int = len(txs) 

60 

61 # fetch tx hashes in pool 

62 tx_hashes: list[str] = daemon.get_tx_pool_hashes() 

63 num_tx_hashes: int = len(tx_hashes) 

64 

65 # context for testing tx 

66 ctx: TestContext = TestContext() 

67 ctx.is_pruned = False 

68 ctx.is_confirmed = False 

69 ctx.from_get_tx_pool = True 

70 

71 # test txs 

72 assert num_txs > 0, "Test requires an unconfirmed tx in the tx pool" 

73 assert num_txs == num_tx_hashes, f"Txs in pool {num_txs} != Txs hashes in pool {num_tx_hashes}" 

74 for a_tx in txs: 

75 TxUtils.test_tx(a_tx, ctx) 

76 assert a_tx.hash in tx_hashes 

77 

78 # flush the tx from the pool, gg 

79 daemon.flush_tx_pool(tx.hash) 

80 wallet.sync() 

81 

82 # Can get transaction pool statistics 

83 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") 

84 def test_get_tx_pool_statistics(self, daemon: MoneroDaemonRpc, wallet: MoneroWalletRpc) -> None: 

85 Utils.WALLET_TX_TRACKER.wait_for_txs_to_clear_pool([wallet]) 

86 tx_ids: list[str] = [] 

87 try: 

88 # submit txs to the pool but don't relay 

89 for i in range(1, 3): 

90 # submit tx hex 

91 tx: MoneroTx = WalletTxsUtils.get_unrelayed_tx(wallet, i) 

92 assert tx.full_hex is not None 

93 result: MoneroSubmitTxResult = daemon.submit_tx_hex(tx.full_hex, True) 

94 assert result.is_good, f"Expected True, got {result.is_good}" 

95 assert tx.hash is not None 

96 tx_ids.append(tx.hash) 

97 

98 # get tx pool stats 

99 stats: MoneroTxPoolStats = daemon.get_tx_pool_stats() 

100 assert stats.num_txs is not None 

101 assert stats.num_txs > i - 1 

102 DaemonUtils.test_tx_pool_stats(stats) 

103 finally: 

104 # flush txs 

105 daemon.flush_tx_pool(tx_ids) 

106 

107 # Can flush all transactions from the pool 

108 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") 

109 def test_flush_txs_from_pool(self, daemon: MoneroDaemonRpc, wallet: MoneroWalletRpc) -> None: 

110 Utils.WALLET_TX_TRACKER.wait_for_txs_to_clear_pool(wallet) 

111 

112 # preserve original transactions in the pool 

113 tx_pool_before: list[MoneroTx] = daemon.get_tx_pool() 

114 

115 # submit txs to the pool but don't relay 

116 for i in range(1, 3): 

117 tx: MoneroTx = WalletTxsUtils.get_unrelayed_tx(wallet, i) 

118 assert tx.full_hex is not None 

119 result: MoneroSubmitTxResult = daemon.submit_tx_hex(tx.full_hex, True) 

120 DaemonUtils.test_submit_tx_result_good(result) 

121 

122 assert len(tx_pool_before) + 2 == len(daemon.get_tx_pool()) 

123 

124 # flush tx pool 

125 daemon.flush_tx_pool() 

126 assert len(daemon.get_tx_pool()) == 0 

127 

128 # re-submit original transactions 

129 for tx in tx_pool_before: 

130 assert tx.full_hex is not None 

131 assert tx.is_relayed is not None 

132 result: MoneroSubmitTxResult = daemon.submit_tx_hex(tx.full_hex, tx.is_relayed) 

133 

134 # pool is back to original state 

135 assert len(tx_pool_before) == len(daemon.get_tx_pool()) 

136 

137 # sync wallet for next test 

138 wallet.sync() 

139 

140 # Can flush a transaction from the pool by hash 

141 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") 

142 def test_flush_tx_from_pool_by_hash(self, daemon: MoneroDaemonRpc, wallet: MoneroWalletRpc) -> None: 

143 Utils.WALLET_TX_TRACKER.wait_for_txs_to_clear_pool(wallet) 

144 

145 # preserve original transactions in the pool 

146 tx_pool_before: list[MoneroTx] = daemon.get_tx_pool() 

147 

148 # submit txs to the pool but don't relay 

149 txs: list[MoneroTx] = [] 

150 for i in range(1, 3): 

151 tx: MoneroTx = WalletTxsUtils.get_unrelayed_tx(wallet, i) 

152 assert tx.full_hex is not None 

153 result: MoneroSubmitTxResult = daemon.submit_tx_hex(tx.full_hex, True) 

154 DaemonUtils.test_submit_tx_result_good(result) 

155 txs.append(tx) 

156 

157 # remove each tx from the pool by hash and test 

158 num_txs: int = len(txs) 

159 for i, tx in enumerate(txs): 

160 assert tx.hash is not None 

161 # flush tx from pool 

162 daemon.flush_tx_pool(tx.hash) 

163 

164 # test tx pool 

165 pool_txs: list[MoneroTx] = daemon.get_tx_pool() 

166 assert num_txs - i - 1 == len(pool_txs) 

167 

168 # pool is back to original state 

169 assert len(tx_pool_before) == len(daemon.get_tx_pool()) 

170 

171 # sync wallet for next test 

172 wallet.sync() 

173 

174 # Can flush transactions from the pool by hashes 

175 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") 

176 def test_flush_txs_from_pool_by_hashes(self, daemon: MoneroDaemonRpc, wallet: MoneroWalletRpc) -> None: 

177 Utils.WALLET_TX_TRACKER.wait_for_txs_to_clear_pool(wallet) 

178 

179 # preserve original transactions in the pool 

180 tx_pool_before: list[MoneroTx] = daemon.get_tx_pool() 

181 

182 # submit txs to the pool but don't relay 

183 tx_hashes: list[str] = [] 

184 for i in range(1, 3): 

185 tx: MoneroTx = WalletTxsUtils.get_unrelayed_tx(wallet, i) 

186 assert tx.hash is not None 

187 assert tx.full_hex is not None 

188 result: MoneroSubmitTxResult = daemon.submit_tx_hex(tx.full_hex, True) 

189 DaemonUtils.test_submit_tx_result_good(result) 

190 tx_hashes.append(tx.hash) 

191 

192 assert len(tx_pool_before) + len(tx_hashes) == len(daemon.get_tx_pool()) 

193 

194 # remove all txs by hashes 

195 daemon.flush_tx_pool(tx_hashes) 

196 

197 # pool is back to original state 

198 assert len(tx_pool_before) == len(daemon.get_tx_pool()) 

199 wallet.sync() 

200 

201 # Can submit a tx in hex format to the pool and relay in one call 

202 @pytest.mark.skipif(Utils.TEST_RELAYS is False, reason="TEST_RELAYS disabled") 

203 @pytest.mark.skipif(Utils.LITE_MODE, reason="LITE_MODE enabled") 

204 def test_submit_and_relay_tx_hex(self, daemon: MoneroDaemonRpc, wallet: MoneroWalletRpc) -> None: 

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

206 # TODO monero-project: update from pool does not prevent creating double spend tx 

207 Utils.WALLET_TX_TRACKER.wait_for_txs_to_clear_pool(wallet) 

208 

209 # create 2 txs, the second will double spend outputs of first 

210 # TODO: this test requires tx to be from/to different accounts else 

211 # the occlusion issue (#4500) causes the tx to not be recognized by the wallet at all 

212 tx1: MoneroTx = WalletTxsUtils.get_unrelayed_tx(wallet, 2) 

213 tx2: MoneroTx = WalletTxsUtils.get_unrelayed_tx(wallet, 2) 

214 

215 # submit and relay tx1 

216 assert tx1.hash is not None 

217 assert tx1.full_hex is not None 

218 result: MoneroSubmitTxResult = daemon.submit_tx_hex(tx1.full_hex) 

219 assert result.is_relayed is True 

220 DaemonUtils.test_submit_tx_result_good(result) 

221 

222 # tx1 is in the pool 

223 txs: list[MoneroTx] = daemon.get_tx_pool() 

224 found: bool = False 

225 for a_tx in txs: 

226 assert a_tx.hash is not None 

227 if a_tx.hash == tx1.hash: 

228 assert a_tx.is_relayed is True 

229 found = True 

230 break 

231 

232 assert found, "Tx1 was not found after being submitted to the daemon's tx pool" 

233 

234 # tx1 is recognized by the wallet 

235 wallet.sync() 

236 wallet.get_tx(tx1.hash) 

237 

238 # submit and relay tx2 hex which double spends tx1 

239 assert tx2.full_hex is not None 

240 result = daemon.submit_tx_hex(tx2.full_hex) 

241 assert result.is_relayed is True 

242 DaemonUtils.test_submit_tx_result_double_spend(result) 

243 

244 # tx2 is not in the pool 

245 txs = daemon.get_tx_pool() 

246 found = False 

247 for a_tx in txs: 

248 assert a_tx.hash is not None 

249 if a_tx.hash == tx2.hash: 

250 found = True 

251 break 

252 

253 assert not found, "Tx2 should not be in the pool because it double spends tx1 which is in the pool" 

254 

255 # Can submit a tx in hex format to the pool then relay 

256 @pytest.mark.skipif(Utils.TEST_RELAYS is False, reason="TEST_RELAYS disabled") 

257 @pytest.mark.skipif(Utils.LITE_MODE, reason="LITE_MODE enabled") 

258 def test_submit_then_relay_tx_hex(self, daemon: MoneroDaemonRpc, wallet: MoneroWalletRpc) -> None: 

259 Utils.WALLET_TX_TRACKER.wait_for_txs_to_clear_pool(wallet) 

260 tx: MoneroTx = WalletTxsUtils.get_unrelayed_tx(wallet, 1) 

261 tester: SubmitThenRelayTxTester = SubmitThenRelayTxTester(daemon, [tx]) 

262 tester.test() 

263 

264 # Can submit txs in hex format to the pool then relay 

265 @pytest.mark.skipif(Utils.TEST_RELAYS is False, reason="TEST_RELAYS disabled") 

266 @pytest.mark.skipif(Utils.LITE_MODE, reason="LITE_MODE enabled") 

267 def test_submit_then_relay_tx_hexes(self, daemon: MoneroDaemonRpc, wallet: MoneroWalletRpc) -> None: 

268 Utils.WALLET_TX_TRACKER.wait_for_txs_to_clear_pool(wallet) 

269 txs: list[MoneroTx] = [] 

270 txs.append(WalletTxsUtils.get_unrelayed_tx(wallet, 1)) 

271 # TODO: accounts cannot be reused across send tests else isRelayed is true; wallet needs to update? 

272 txs.append(WalletTxsUtils.get_unrelayed_tx(wallet, 2)) 

273 tester: SubmitThenRelayTxTester = SubmitThenRelayTxTester(daemon, txs) 

274 tester.test()