Coverage for tests/wallet/model/test_tx_query.py: 100%

297 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 MoneroBlock, 

8 MoneroIncomingTransfer, 

9 MoneroOutgoingTransfer, 

10 MoneroOutputQuery, 

11 MoneroOutputWallet, 

12 MoneroTransferQuery, 

13 MoneroTxQuery, 

14 MoneroTxWallet, 

15) 

16 

17from utils import AssertUtils, BaseTestClass 

18 

19logger: logging.Logger = logging.getLogger("TestMoneroWalletModelTxQuery") 

20 

21 

22@pytest.mark.unit 

23class TestMoneroWalletModelTxQuery(BaseTestClass): 

24 """Wallet tx query model unit tests.""" 

25 

26 # Test tx query expected behaviour 

27 def test_tx_query(self) -> None: 

28 tx_query: MoneroTxQuery = MoneroTxQuery() 

29 transfer_query: MoneroTransferQuery = MoneroTransferQuery() 

30 output_query: MoneroOutputQuery = MoneroOutputQuery() 

31 input_query: MoneroOutputQuery = MoneroOutputQuery() 

32 

33 # assign transfer query to tx query 

34 tx_query.transfer_query = transfer_query 

35 

36 assert tx_query.transfer_query == transfer_query 

37 assert transfer_query.tx_query is not None 

38 assert transfer_query.tx_query == tx_query 

39 

40 # remove transfer query from tx query 

41 tx_query.transfer_query = None 

42 

43 assert tx_query.transfer_query != transfer_query 

44 assert transfer_query.tx_query is None 

45 

46 # assign output query to tx query 

47 tx_query.output_query = output_query 

48 

49 assert tx_query.output_query == output_query 

50 assert output_query.tx_query is not None 

51 assert output_query.tx_query == tx_query 

52 

53 # remove output query from tx query 

54 tx_query.output_query = None 

55 

56 assert tx_query.output_query != output_query 

57 assert output_query.tx_query is None 

58 

59 # assign input query to tx query 

60 tx_query.input_query = input_query 

61 

62 assert tx_query.input_query == input_query 

63 assert input_query.tx_query is not None 

64 assert input_query.tx_query == tx_query 

65 

66 # remove output query from tx query 

67 tx_query.input_query = None 

68 

69 assert tx_query.input_query != input_query 

70 assert input_query.tx_query is None 

71 

72 def test_tx_query_meets_criteria_matches_unconstrained_tx(self) -> None: 

73 tx: MoneroTxWallet = MoneroTxWallet() 

74 tx.hash = "a" * 64 

75 assert MoneroTxQuery().meets_criteria(tx) is True 

76 

77 @pytest.mark.parametrize("field,value,other_value", [ 

78 ("hash", "a" * 64, "b" * 64), 

79 ("payment_id", "c" * 16, "d" * 16), 

80 ("is_confirmed", True, False), 

81 ("in_tx_pool", True, False), 

82 ("relay", True, False), 

83 ("is_failed", True, False), 

84 ("is_miner_tx", True, False), 

85 ("is_locked", True, False), 

86 ("is_incoming", True, False), 

87 ("is_outgoing", True, False), 

88 ]) 

89 def test_tx_query_meets_criteria_field_filters(self, field: str, value: object, other_value: object) -> None: 

90 tx: MoneroTxWallet = MoneroTxWallet() 

91 tx.hash = "a" * 64 

92 setattr(tx, field, value) 

93 

94 query: MoneroTxQuery = MoneroTxQuery() 

95 setattr(query, field, value) 

96 assert query.meets_criteria(tx) is True 

97 

98 setattr(query, field, other_value) 

99 assert query.meets_criteria(tx) is False 

100 

101 def test_tx_query_meets_criteria_has_payment_id(self) -> None: 

102 with_payment_id: MoneroTxWallet = MoneroTxWallet() 

103 with_payment_id.hash = "a" * 64 

104 with_payment_id.payment_id = "b" * 16 

105 without_payment_id: MoneroTxWallet = MoneroTxWallet() 

106 without_payment_id.hash = "c" * 64 

107 

108 query_true: MoneroTxQuery = MoneroTxQuery() 

109 query_true.has_payment_id = True 

110 assert query_true.meets_criteria(with_payment_id) is True 

111 assert query_true.meets_criteria(without_payment_id) is False 

112 

113 query_false: MoneroTxQuery = MoneroTxQuery() 

114 query_false.has_payment_id = False 

115 assert query_false.meets_criteria(without_payment_id) is True 

116 assert query_false.meets_criteria(with_payment_id) is False 

117 

118 def test_tx_query_meets_criteria_hashes(self) -> None: 

119 tx: MoneroTxWallet = MoneroTxWallet() 

120 tx.hash = "a" * 64 

121 

122 query: MoneroTxQuery = MoneroTxQuery() 

123 query.hashes = ["a" * 64, "b" * 64] 

124 assert query.meets_criteria(tx) is True 

125 

126 query.hashes = ["c" * 64] 

127 assert query.meets_criteria(tx) is False 

128 

129 def test_tx_query_meets_criteria_payment_ids(self) -> None: 

130 with_payment_id: MoneroTxWallet = MoneroTxWallet() 

131 with_payment_id.hash = "a" * 64 

132 with_payment_id.payment_id = "b" * 16 

133 without_payment_id: MoneroTxWallet = MoneroTxWallet() 

134 without_payment_id.hash = "c" * 64 

135 

136 query: MoneroTxQuery = MoneroTxQuery() 

137 query.payment_ids = ["b" * 16, "d" * 16] 

138 assert query.meets_criteria(with_payment_id) is True 

139 assert query.meets_criteria(without_payment_id) is False # tx has no payment id at all 

140 

141 query.payment_ids = ["d" * 16] 

142 assert query.meets_criteria(with_payment_id) is False # tx's payment id isn't in the list 

143 

144 def test_tx_query_meets_criteria_height_filters(self) -> None: 

145 def confirmed_tx(height: int) -> MoneroTxWallet: 

146 tx: MoneroTxWallet = MoneroTxWallet() 

147 tx.hash = "a" * 64 

148 block: MoneroBlock = MoneroBlock() 

149 block.height = height 

150 tx.block = block 

151 return tx 

152 

153 tx: MoneroTxWallet = confirmed_tx(100) 

154 

155 height_query: MoneroTxQuery = MoneroTxQuery() 

156 height_query.height = 100 

157 assert height_query.meets_criteria(tx) is True 

158 height_query.height = 200 

159 assert height_query.meets_criteria(tx) is False 

160 

161 min_query: MoneroTxQuery = MoneroTxQuery() 

162 min_query.min_height = 50 

163 assert min_query.meets_criteria(tx) is True 

164 min_query.min_height = 150 

165 assert min_query.meets_criteria(tx) is False 

166 

167 max_query: MoneroTxQuery = MoneroTxQuery() 

168 max_query.max_height = 150 

169 assert max_query.meets_criteria(tx) is True 

170 max_query.max_height = 50 

171 assert max_query.meets_criteria(tx) is False 

172 

173 # an unconfirmed tx (no height) never satisfies a height-bound query 

174 unconfirmed: MoneroTxWallet = MoneroTxWallet() 

175 unconfirmed.hash = "b" * 64 

176 assert min_query.meets_criteria(unconfirmed) is False 

177 

178 def test_tx_query_meets_criteria_query_children_default_and_false(self) -> None: 

179 tx: MoneroTxWallet = MoneroTxWallet() 

180 tx.hash = "a" * 64 # no inputs -> fails an enforced input_query 

181 

182 query: MoneroTxQuery = MoneroTxQuery() 

183 query.input_query = MoneroOutputQuery() 

184 

185 # query_children defaults to True, matching monero-cpp's default 

186 assert query.meets_criteria(tx) is False 

187 assert query.meets_criteria(tx, True) is False 

188 assert query.meets_criteria(tx, False) is True 

189 

190 def test_tx_query_meets_criteria_transfer_query(self) -> None: 

191 def tx_with_outgoing(amount: int) -> MoneroTxWallet: 

192 tx: MoneroTxWallet = MoneroTxWallet() 

193 tx.hash = "a" * 64 

194 tx.outgoing_transfer = MoneroOutgoingTransfer() 

195 tx.outgoing_transfer.amount = amount 

196 tx.outgoing_transfer.tx = tx 

197 return tx 

198 

199 def tx_with_incoming(amount: int) -> MoneroTxWallet: 

200 tx: MoneroTxWallet = MoneroTxWallet() 

201 tx.hash = "a" * 64 

202 transfer: MoneroIncomingTransfer = MoneroIncomingTransfer() 

203 transfer.amount = amount 

204 transfer.tx = tx 

205 tx.incoming_transfers = [transfer] 

206 return tx 

207 

208 query: MoneroTxQuery = MoneroTxQuery() 

209 query.transfer_query = MoneroTransferQuery() 

210 query.transfer_query.amount = 500 

211 

212 assert query.meets_criteria(tx_with_outgoing(500), True) is True # matches via outgoing 

213 assert query.meets_criteria(tx_with_incoming(500), True) is True # matches via incoming 

214 assert query.meets_criteria(tx_with_outgoing(1), True) is False # no transfer matches 

215 

216 # outgoing fails the query but an incoming transfer still matches as a fallback 

217 tx: MoneroTxWallet = tx_with_outgoing(1) 

218 incoming: MoneroIncomingTransfer = MoneroIncomingTransfer() 

219 incoming.amount = 500 

220 incoming.tx = tx 

221 tx.incoming_transfers = [incoming] 

222 assert query.meets_criteria(tx, True) is True 

223 

224 def test_tx_query_meets_criteria_input_query(self) -> None: 

225 tx_no_inputs: MoneroTxWallet = MoneroTxWallet() 

226 tx_no_inputs.hash = "a" * 64 

227 

228 tx_with_input: MoneroTxWallet = MoneroTxWallet() 

229 tx_with_input.hash = "b" * 64 

230 tx_input: MoneroOutputWallet = MoneroOutputWallet() 

231 tx_input.amount = 100 

232 tx_with_input.inputs = [tx_input] 

233 

234 query: MoneroTxQuery = MoneroTxQuery() 

235 query.input_query = MoneroOutputQuery() 

236 assert query.meets_criteria(tx_no_inputs, True) is False # no inputs at all 

237 

238 query.input_query.amount = 999 

239 assert query.meets_criteria(tx_with_input, True) is False # no input matches 

240 

241 query.input_query.amount = 100 

242 assert query.meets_criteria(tx_with_input, True) is True # one input matches 

243 

244 def test_tx_query_meets_criteria_output_query(self) -> None: 

245 tx_no_outputs: MoneroTxWallet = MoneroTxWallet() 

246 tx_no_outputs.hash = "a" * 64 

247 

248 tx_with_output: MoneroTxWallet = MoneroTxWallet() 

249 tx_with_output.hash = "b" * 64 

250 output: MoneroOutputWallet = MoneroOutputWallet() 

251 output.amount = 100 

252 tx_with_output.outputs = [output] 

253 

254 query: MoneroTxQuery = MoneroTxQuery() 

255 query.output_query = MoneroOutputQuery() 

256 assert query.meets_criteria(tx_no_outputs, True) is False # no outputs at all 

257 

258 query.output_query.amount = 999 

259 assert query.meets_criteria(tx_with_output, True) is False # no output matches 

260 

261 query.output_query.amount = 100 

262 assert query.meets_criteria(tx_with_output, True) is True # one output matches 

263 

264 def test_tx_query_meets_criteria_none_tx_raises(self) -> None: 

265 with pytest.raises(RuntimeError) as exc_info: 

266 MoneroTxQuery().meets_criteria(None) # type: ignore 

267 assert str(exc_info.value) == "nullptr given to monero_tx_query::meets_criteria()" 

268 

269 def test_tx_query_deserialize(self) -> None: 

270 tx_query: MoneroTxQuery = MoneroTxQuery() 

271 tx_query.hash = "a" * 64 

272 tx_query.is_confirmed = True 

273 tx_query.hashes = ["a" * 64, "b" * 64] 

274 tx_query.has_payment_id = False 

275 tx_query.payment_ids = ["c" * 16] 

276 tx_query.height = 3000000 

277 tx_query.min_height = 2999990 

278 tx_query.max_height = 3000010 

279 tx_query.include_outputs = False 

280 # is_outgoing/is_incoming are deliberately not set here: monero_tx_query 

281 # redeclares them as its own fields shadowing monero_tx_wallet's, and 

282 # deserializing populates both copies at once (see test below) 

283 AssertUtils.assert_serialization_integrity(tx_query) 

284 

285 def test_tx_query_is_incoming_deserialize_not_duplicated(self) -> None: 

286 tx_query: MoneroTxQuery = MoneroTxQuery() 

287 tx_query.is_incoming = True 

288 tx_query.is_outgoing = False 

289 json_str: str = tx_query.serialize() 

290 logger.debug(f"Serialized tx query: {json_str}") 

291 assert json_str.count("isIncoming") == 1 

292 assert json_str.count("isOutgoing") == 1 

293 

294 restored: MoneroTxQuery = MoneroTxQuery.deserialize(json_str) 

295 assert restored.is_incoming == tx_query.is_incoming 

296 assert restored.is_outgoing == tx_query.is_outgoing 

297 

298 restored_json: str = restored.serialize() 

299 incoming_count: int = restored_json.count("isIncoming") 

300 outgoing_count: int = restored_json.count("isOutgoing") 

301 logger.debug(f"Deserialized tx query re-serialized: {restored_json}") 

302 logger.debug(f"'isIncoming' occurs {incoming_count} time(s), 'isOutgoing' occurs {outgoing_count} time(s) (expected 1 each -- >1 means duplicated, i.e. malformed)") 

303 assert incoming_count == 1 

304 assert outgoing_count == 1 

305 

306 def test_tx_query_nested_transfer_query_deserialize(self) -> None: 

307 # transfer_query is a nested sub-object that to_rapidjson_val() and 

308 # from_property_tree() both handle recursively 

309 tx_query: MoneroTxQuery = MoneroTxQuery() 

310 tx_query.height = 3000000 

311 transfer_query: MoneroTransferQuery = MoneroTransferQuery() 

312 transfer_query.incoming = True 

313 transfer_query.amount = 500000 

314 transfer_query.account_index = 0 

315 tx_query.transfer_query = transfer_query 

316 

317 json_str: str = tx_query.serialize() 

318 logger.debug(f"Serialized nested tx query: {json_str}") 

319 assert "transferQuery" in json_str 

320 

321 restored: MoneroTxQuery = MoneroTxQuery.deserialize(json_str) 

322 assert restored.height == tx_query.height 

323 assert restored.transfer_query is not None 

324 assert restored.transfer_query.incoming == transfer_query.incoming 

325 assert restored.transfer_query.amount == transfer_query.amount 

326 assert restored.transfer_query.account_index == transfer_query.account_index 

327 

328 def test_tx_query_input_and_output_query_deserialize(self) -> None: 

329 # TODO input_query/output_query are read by from_property_tree() but never written by to_rapidjson_val() 

330 tx_query: MoneroTxQuery = MoneroTxQuery.deserialize( 

331 '{"inputQuery":{"amount":5,"index":1},"outputQuery":{"amount":7,"index":2}}' 

332 ) 

333 assert tx_query.input_query is not None 

334 assert tx_query.input_query.amount == 5 

335 assert tx_query.input_query.index == 1 

336 assert tx_query.output_query is not None 

337 assert tx_query.output_query.amount == 7 

338 assert tx_query.output_query.index == 2 

339 

340 def test_tx_query_input_and_output_query_serialize_round_trip(self) -> None: 

341 tx_query: MoneroTxQuery = MoneroTxQuery() 

342 tx_query.input_query = MoneroOutputQuery() 

343 tx_query.input_query.amount = 5 

344 tx_query.input_query.index = 1 

345 tx_query.output_query = MoneroOutputQuery() 

346 tx_query.output_query.amount = 7 

347 tx_query.output_query.index = 2 

348 

349 json_str: str = tx_query.serialize() 

350 logger.debug(f"Serialized tx query with input/output query: {json_str}") 

351 assert "inputQuery" in json_str 

352 assert "outputQuery" in json_str 

353 

354 restored: MoneroTxQuery = MoneroTxQuery.deserialize(json_str) 

355 assert restored.input_query is not None 

356 assert restored.input_query.amount == 5 

357 assert restored.input_query.index == 1 

358 assert restored.output_query is not None 

359 assert restored.output_query.amount == 7 

360 assert restored.output_query.index == 2 

361 

362 def test_tx_query_deserialize_from_block(self) -> None: 

363 tx_query: MoneroTxQuery = MoneroTxQuery() 

364 tx_query.hash = "a" * 64 

365 tx_query.is_confirmed = True 

366 tx_query.hashes = ["a" * 64, "b" * 64] 

367 tx_query.min_height = 100 

368 tx_query.max_height = 200 

369 

370 # deserialize_from_block expects a block node wrapping the query under "txs" 

371 block_json: str = '{"txs":[' + tx_query.serialize() + ']}' 

372 restored: MoneroTxQuery = MoneroTxQuery.deserialize_from_block(block_json) 

373 assert restored.hash == "a" * 64 

374 assert restored.is_confirmed is True 

375 assert list(restored.hashes) == ["a" * 64, "b" * 64] 

376 assert restored.min_height == 100 

377 assert restored.max_height == 200 

378 

379 # the query is linked back to its parent block 

380 assert restored.block is not None 

381 assert restored.block.txs[0] is restored 

382 

383 def test_tx_query_deserialize_from_block_no_txs(self) -> None: 

384 restored: MoneroTxQuery = MoneroTxQuery.deserialize_from_block('{"txs": []}') 

385 assert restored.hash is None 

386 assert restored.block is None 

387 

388 def test_tx_query_copy(self) -> None: 

389 query: MoneroTxQuery = MoneroTxQuery() 

390 query.hash = "a" * 64 

391 query.height = 3000000 

392 query.is_confirmed = True 

393 

394 copy: MoneroTxQuery = query.copy() 

395 assert copy is not query 

396 assert copy.serialize() == query.serialize() 

397 

398 def test_tx_query_decontextualize(self) -> None: 

399 query: MoneroTxQuery = MoneroTxQuery() 

400 query.hash = "a" * 64 

401 query.is_incoming = True 

402 query.is_outgoing = False 

403 query.transfer_query = MoneroTransferQuery() 

404 query.input_query = MoneroOutputQuery() 

405 query.output_query = MoneroOutputQuery() 

406 

407 # mutates and returns the same query, for convenience 

408 decontextualized: MoneroTxQuery = MoneroTxQuery.decontextualize(query) 

409 assert decontextualized is query 

410 assert query.is_incoming is None 

411 assert query.is_outgoing is None 

412 assert query.transfer_query is None 

413 assert query.input_query is None 

414 assert query.output_query is None 

415 

416 # criteria that don't require looking up other transfers/outputs are untouched 

417 assert query.hash == "a" * 64