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

271 statements  

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

1from __future__ import annotations 

2 

3import logging 

4import subprocess 

5import sys 

6 

7import pytest 

8from monero import ( 

9 MoneroIncomingTransfer, 

10 MoneroKeyImage, 

11 MoneroOutgoingTransfer, 

12 MoneroOutputQuery, 

13 MoneroOutputWallet, 

14 MoneroTransfer, 

15 MoneroTransferQuery, 

16 MoneroTxSet, 

17 MoneroTxWallet, 

18) 

19 

20from utils import AssertUtils, BaseTestClass, TestUtils 

21 

22logger: logging.Logger = logging.getLogger("TestMoneroWalletModelTxs") 

23 

24 

25@pytest.mark.unit 

26class TestMoneroWalletModelTxs(BaseTestClass): 

27 """Wallet tx model unit tests.""" 

28 

29 def test_tx_wallet_deserialize(self) -> None: 

30 tx_wallet: MoneroTxWallet = MoneroTxWallet() 

31 tx_wallet.hash = "a" * 64 

32 tx_wallet.is_miner_tx = False 

33 tx_wallet.fee = 7500000 

34 tx_wallet.relay = True 

35 tx_wallet.is_relayed = True 

36 tx_wallet.is_confirmed = True 

37 tx_wallet.in_tx_pool = False 

38 tx_wallet.num_confirmations = 10 

39 tx_wallet.unlock_time = 0 

40 tx_wallet.is_incoming = True 

41 tx_wallet.is_outgoing = False 

42 tx_wallet.note = "thanks" 

43 tx_wallet.is_locked = False 

44 tx_wallet.input_sum = 2000000 

45 tx_wallet.output_sum = 1900000 

46 # tx_set, incoming_transfers, outgoing_transfer, change_address, 

47 # change_amount, num_dummy_outputs and extra_hex all raise "not 

48 # implemented" (see below), same as their monero_tx base counterparts 

49 # (version, inputs, outputs, ...) tested in test_monero_daemon_model.py 

50 AssertUtils.assert_serialization_integrity(tx_wallet) 

51 

52 @pytest.mark.parametrize("json_fragment", [ 

53 '{"txSet":{}}', 

54 '{"incomingTransfers":[]}', 

55 '{"outgoingTransfer":{}}', 

56 '{"changeAddress":"' + TestUtils.ADDRESS + '"}', 

57 '{"changeAmount":1}', 

58 '{"numDummyOutputs":1}', 

59 '{"extraHex":"deadbeef"}', 

60 ]) 

61 def test_tx_wallet_unimplemented_fields(self, json_fragment: str) -> None: 

62 with pytest.raises(Exception, match="not implemented"): 

63 MoneroTxWallet.deserialize(json_fragment) 

64 

65 def test_tx_set_deserialize(self) -> None: 

66 tx_set: MoneroTxSet = MoneroTxSet() 

67 tx_set.unsigned_tx_hex = "deadbeef" 

68 tx_set.multisig_tx_hex = "beefdead" 

69 AssertUtils.assert_serialization_integrity(tx_set) 

70 

71 def test_tx_set_signed_tx_hex_deserialize(self) -> None: 

72 tx_set: MoneroTxSet = MoneroTxSet() 

73 tx_set.signed_tx_hex = "deadbeef" 

74 AssertUtils.assert_serialization_integrity(tx_set) 

75 

76 def test_tx_set_txs_deserialize(self) -> None: 

77 json_str: str = '{"txs": [{"hash": "' + "a" * 64 + '"}, {"hash": "' + "b" * 64 + '"}]}' 

78 tx_set: MoneroTxSet = MoneroTxSet.deserialize(json_str) 

79 assert len(tx_set.txs) == 2 

80 assert all(isinstance(tx, MoneroTxWallet) for tx in tx_set.txs) 

81 assert tx_set.txs[0].hash == "a" * 64 

82 assert tx_set.txs[1].hash == "b" * 64 

83 

84 def test_tx_wallet_merge_incoming_transfers_with_unset_indices_are_kept_distinct(self) -> None: 

85 """ 

86 merge_incoming_transfer() dedups incoming transfers by (account_index, subaddress_index) 

87 when reconciling two txs' transfer lists. Previously it dereferenced both indices 

88 unconditionally (boost::optional UB when unset), reachable via TxWallet.merge() with 

89 user-constructed MoneroIncomingTransfer objects that never had an index assigned. Since 

90 identity can't be verified without both indices, unset-index transfers must be kept as 

91 distinct entries rather than crashing or being silently coalesced. Run in an isolated 

92 subprocess: locally this UB just gives a wrong (deduped) result, but the same 

93 optional::get() assertion has been observed to abort the whole process in CI's build 

94 (NDEBUG/ODR ambiguity between monero-cpp and monero-python's own compiled units), which a 

95 plain in-process assertion can't survive. 

96 """ 

97 script: str = ( 

98 "import monero, sys\n" 

99 "tx_a = monero.MoneroTxWallet()\n" 

100 "tx_a.hash = 'a' * 64\n" 

101 "tx_a.is_confirmed = True\n" 

102 "transfer_a = monero.MoneroIncomingTransfer()\n" # account_index/subaddress_index intentionally unset 

103 "transfer_a.tx = tx_a\n" 

104 "tx_a.incoming_transfers = [transfer_a]\n" 

105 "tx_b = monero.MoneroTxWallet()\n" 

106 "tx_b.hash = 'a' * 64\n" 

107 "tx_b.is_confirmed = True\n" 

108 "transfer_b = monero.MoneroIncomingTransfer()\n" # account_index/subaddress_index intentionally unset 

109 "transfer_b.tx = tx_b\n" 

110 "tx_b.incoming_transfers = [transfer_b]\n" 

111 "tx_a.merge(tx_b)\n" 

112 "n = len(tx_a.incoming_transfers) if tx_a.incoming_transfers else 0\n" 

113 "sys.exit(0 if n == 2 else f'incoming_transfers not kept distinct: len={n}')\n" 

114 ) 

115 result: subprocess.CompletedProcess[str] = subprocess.run([sys.executable, "-c", script], capture_output=True, text=True, timeout=30) 

116 logger.debug(f"subprocess exit code: {result.returncode}, stderr: {result.stderr.strip()}") 

117 assert result.returncode == 0, ( 

118 f"TxWallet.merge() did not keep unset-index incoming transfers distinct " 

119 f"(exit code {result.returncode}): {result.stderr.strip()[-300:]}" 

120 ) 

121 

122 def test_tx_wallet_copy(self) -> None: 

123 tx: MoneroTxWallet = MoneroTxWallet() 

124 tx.hash = "a" * 64 

125 tx.is_confirmed = True 

126 tx.note = "hello" 

127 

128 copy: MoneroTxWallet = tx.copy() 

129 assert copy is not tx 

130 assert copy.serialize() == tx.serialize() 

131 

132 def test_tx_wallet_copy_re_copies_inputs_as_output_wallet(self) -> None: 

133 tx: MoneroTxWallet = MoneroTxWallet() 

134 tx.hash = "a" * 64 

135 

136 key_image: MoneroKeyImage = MoneroKeyImage() 

137 key_image.hex = "1" * 64 

138 tx_input: MoneroOutputWallet = MoneroOutputWallet() 

139 tx_input.tx = tx 

140 tx_input.key_image = key_image 

141 tx_input.amount = 1234 

142 tx_input.account_index = 2 

143 tx_input.subaddress_index = 3 

144 tx_input.is_spent = True 

145 tx_input.is_frozen = False 

146 tx.inputs = [tx_input] 

147 

148 copy: MoneroTxWallet = tx.copy() 

149 assert isinstance(copy.inputs[0], MoneroOutputWallet) 

150 assert copy.inputs[0] is not tx_input 

151 assert copy.inputs[0].amount == 1234 

152 assert copy.inputs[0].account_index == 2 

153 assert copy.inputs[0].subaddress_index == 3 

154 assert copy.inputs[0].is_spent is True 

155 assert copy.inputs[0].is_frozen is False 

156 assert copy.inputs[0].key_image.hex == "1" * 64 # type: ignore 

157 assert copy.inputs[0].tx is copy 

158 

159 def test_tx_wallet_merge(self) -> None: 

160 a: MoneroTxWallet = MoneroTxWallet() 

161 a.hash = "a" * 64 

162 a.is_confirmed = True # required: base monero_tx::merge() dereferences is_confirmed directly 

163 

164 b: MoneroTxWallet = a.copy() 

165 b.note = "hello" # a.note is unset -> merge fills the gap 

166 a.merge(b) 

167 assert a.note == "hello" 

168 

169 def test_tx_wallet_merge_is_locked_can_become_false(self) -> None: 

170 a: MoneroTxWallet = MoneroTxWallet() 

171 a.hash = "a" * 64 

172 a.is_confirmed = True 

173 a.is_locked = False # self: already unlocked 

174 b: MoneroTxWallet = a.copy() 

175 b.is_locked = True # other: still locked 

176 a.merge(b) 

177 assert a.is_locked is False 

178 

179 def test_tx_wallet_inputs_deserialize_as_output_wallet(self) -> None: 

180 tx: MoneroTxWallet = MoneroTxWallet() 

181 tx.hash = "a" * 64 

182 tx_input: MoneroOutputWallet = MoneroOutputWallet() 

183 tx_input.amount = 500000 

184 tx_input.index = 7 

185 tx_input.account_index = 1 

186 tx_input.subaddress_index = 2 

187 tx_input.is_spent = True 

188 tx_input.is_frozen = False 

189 tx_input.key_image = MoneroKeyImage() 

190 tx_input.key_image.hex = "k" * 64 

191 tx.inputs = [tx_input] 

192 

193 json_str: str = tx.serialize() 

194 assert "accountIndex" in json_str 

195 assert "isSpent" in json_str 

196 

197 # monero_tx::from_property_tree parses inputs as MoneroOutput; the wallet 

198 # deserializer re-parses them as MoneroOutputWallet in place 

199 restored: MoneroTxWallet = MoneroTxWallet.deserialize(json_str) 

200 assert len(restored.inputs) == 1 

201 assert isinstance(restored.inputs[0], MoneroOutputWallet) 

202 assert restored.inputs[0].account_index == 1 

203 assert restored.inputs[0].subaddress_index == 2 

204 assert restored.inputs[0].is_spent is True 

205 assert restored.inputs[0].is_frozen is False 

206 assert restored.inputs[0].key_image is not None 

207 assert restored.inputs[0].key_image.hex == "k" * 64 

208 assert restored.inputs[0].tx is restored 

209 

210 def test_tx_wallet_outputs_deserialize_as_output_wallet(self) -> None: 

211 tx: MoneroTxWallet = MoneroTxWallet() 

212 tx.hash = "a" * 64 

213 output: MoneroOutputWallet = MoneroOutputWallet() 

214 output.amount = 500000 

215 output.index = 3 

216 output.account_index = 2 

217 output.subaddress_index = 1 

218 output.is_spent = True 

219 output.is_frozen = False 

220 tx.outputs = [output] 

221 

222 json_str: str = tx.serialize() 

223 assert "accountIndex" in json_str 

224 assert "isSpent" in json_str 

225 

226 restored: MoneroTxWallet = MoneroTxWallet.deserialize(json_str) 

227 assert len(restored.outputs) == 1 

228 assert isinstance(restored.outputs[0], MoneroOutputWallet) 

229 assert restored.outputs[0].account_index == 2 

230 assert restored.outputs[0].subaddress_index == 1 

231 assert restored.outputs[0].is_spent is True 

232 assert restored.outputs[0].is_frozen is False 

233 

234 def test_tx_wallet_get_outputs_wallet_after_deserialize(self) -> None: 

235 tx: MoneroTxWallet = MoneroTxWallet() 

236 tx.hash = "a" * 64 

237 output: MoneroOutputWallet = MoneroOutputWallet() 

238 output.amount = 500000 

239 output.index = 3 

240 tx.outputs = [output] 

241 

242 restored: MoneroTxWallet = MoneroTxWallet.deserialize(tx.serialize()) 

243 # once deserialize works: raises "nullptr given to monero_output_query::meets_criteria()" 

244 outputs_wallet: list[MoneroOutputWallet] = restored.get_outputs_wallet() 

245 assert len(outputs_wallet) == 1 

246 assert outputs_wallet[0].amount == 500000 

247 

248 def test_tx_wallet_filter_inputs_wallet(self) -> None: 

249 def make_input(account_index: int, amount: int) -> MoneroOutputWallet: 

250 tx_input: MoneroOutputWallet = MoneroOutputWallet() 

251 tx_input.account_index = account_index 

252 tx_input.amount = amount 

253 return tx_input 

254 

255 tx: MoneroTxWallet = MoneroTxWallet() 

256 tx.hash = "a" * 64 

257 tx.inputs = [make_input(0, 100), make_input(1, 200), make_input(0, 300)] 

258 

259 query: MoneroOutputQuery = MoneroOutputQuery() 

260 query.account_index = 0 

261 

262 # filter returns the matches and drops the rest from tx.inputs in place 

263 matched: list[MoneroOutputWallet] = tx.filter_inputs_wallet(query) 

264 assert [i.amount for i in matched] == [100, 300] 

265 assert all(isinstance(i, MoneroOutputWallet) for i in matched) 

266 assert [i.amount for i in tx.inputs] == [100, 300] 

267 

268 # an unconstrained query keeps everything 

269 assert len(tx.filter_inputs_wallet(MoneroOutputQuery())) == 2 

270 

271 def test_tx_wallet_get_inputs_wallet(self) -> None: 

272 def make_input(account_index: int, amount: int) -> MoneroOutputWallet: 

273 tx_input: MoneroOutputWallet = MoneroOutputWallet() 

274 tx_input.account_index = account_index 

275 tx_input.amount = amount 

276 return tx_input 

277 

278 tx: MoneroTxWallet = MoneroTxWallet() 

279 tx.hash = "a" * 64 

280 tx.inputs = [make_input(0, 100), make_input(1, 200), make_input(0, 300)] 

281 

282 query: MoneroOutputQuery = MoneroOutputQuery() 

283 query.account_index = 0 

284 

285 # get_inputs_wallet() does not mutate tx.inputs 

286 matched: list[MoneroOutputWallet] = tx.get_inputs_wallet(query) 

287 assert [i.amount for i in matched] == [100, 300] 

288 assert len(tx.inputs) == 3 

289 

290 # unconstrained default query (no args) returns all inputs 

291 assert len(tx.get_inputs_wallet()) == 3 

292 

293 def test_tx_wallet_filter_outputs_wallet(self) -> None: 

294 def make_output(account_index: int, amount: int) -> MoneroOutputWallet: 

295 output: MoneroOutputWallet = MoneroOutputWallet() 

296 output.account_index = account_index 

297 output.amount = amount 

298 return output 

299 

300 tx: MoneroTxWallet = MoneroTxWallet() 

301 tx.hash = "a" * 64 

302 tx.outputs = [make_output(0, 100), make_output(1, 200), make_output(0, 300)] 

303 

304 query: MoneroOutputQuery = MoneroOutputQuery() 

305 query.account_index = 0 

306 

307 # filter returns the matches and drops the rest from tx.outputs in place 

308 matched: list[MoneroOutputWallet] = tx.filter_outputs_wallet(query) 

309 assert [o.amount for o in matched] == [100, 300] 

310 assert all(isinstance(o, MoneroOutputWallet) for o in matched) 

311 assert [o.amount for o in tx.outputs] == [100, 300] 

312 

313 # an unconstrained query keeps everything 

314 assert len(tx.filter_outputs_wallet(MoneroOutputQuery())) == 2 

315 

316 def test_tx_wallet_filter_transfers(self) -> None: 

317 tx: MoneroTxWallet = MoneroTxWallet() 

318 tx.hash = "a" * 64 

319 

320 outgoing: MoneroOutgoingTransfer = MoneroOutgoingTransfer() 

321 outgoing.amount = 1000 

322 

323 incoming_1: MoneroIncomingTransfer = MoneroIncomingTransfer() 

324 incoming_1.account_index = 0 

325 incoming_1.amount = 100 

326 

327 incoming_2: MoneroIncomingTransfer = MoneroIncomingTransfer() 

328 incoming_2.account_index = 1 

329 incoming_2.amount = 200 

330 

331 tx.outgoing_transfer = outgoing 

332 tx.incoming_transfers = [incoming_1, incoming_2] 

333 

334 query: MoneroTransferQuery = MoneroTransferQuery() 

335 query.incoming = True 

336 query.account_index = 0 

337 

338 # filter returns the matches and drops the rest (outgoing_transfer cleared, incoming_2 removed) 

339 matched: list[MoneroTransfer] = tx.filter_transfers(query) 

340 assert [t.amount for t in matched] == [100] 

341 assert tx.outgoing_transfer is None 

342 assert [t.amount for t in tx.incoming_transfers] == [100] 

343 

344 def test_tx_wallet_get_transfers(self) -> None: 

345 tx: MoneroTxWallet = MoneroTxWallet() 

346 tx.hash = "a" * 64 

347 tx.outgoing_transfer = MoneroOutgoingTransfer() 

348 tx.outgoing_transfer.amount = 1000 

349 tx.incoming_transfers = [MoneroIncomingTransfer(), MoneroIncomingTransfer()] 

350 tx.incoming_transfers[0].amount = 100 

351 tx.incoming_transfers[1].amount = 200 

352 

353 # no-arg overload delegates to an empty query -> outgoing then incoming, in order 

354 all_transfers: list[MoneroTransfer] = tx.get_transfers() 

355 assert [t.amount for t in all_transfers] == [1000, 100, 200] 

356 assert isinstance(all_transfers[0], MoneroOutgoingTransfer) 

357 assert all(isinstance(t, MoneroIncomingTransfer) for t in all_transfers[1:]) 

358 

359 # get_transfers() does not mutate the tx 

360 assert tx.outgoing_transfer is not None 

361 assert len(tx.incoming_transfers) == 2 

362 

363 def test_tx_wallet_get_transfers_with_query(self) -> None: 

364 tx: MoneroTxWallet = MoneroTxWallet() 

365 tx.hash = "a" * 64 

366 tx.outgoing_transfer = MoneroOutgoingTransfer() 

367 tx.outgoing_transfer.amount = 1000 

368 tx.incoming_transfers = [MoneroIncomingTransfer(), MoneroIncomingTransfer()] 

369 tx.incoming_transfers[0].amount = 100 

370 tx.incoming_transfers[1].amount = 200 

371 

372 incoming_query: MoneroTransferQuery = MoneroTransferQuery() 

373 incoming_query.incoming = True 

374 assert [t.amount for t in tx.get_transfers(incoming_query)] == [100, 200] 

375 

376 outgoing_query: MoneroTransferQuery = MoneroTransferQuery() 

377 outgoing_query.outgoing = True 

378 assert [t.amount for t in tx.get_transfers(outgoing_query)] == [1000] 

379 

380 no_match_query: MoneroTransferQuery = MoneroTransferQuery() 

381 no_match_query.amount = 999999 

382 assert tx.get_transfers(no_match_query) == [] 

383 

384 def test_tx_wallet_copy_preserves_output_wallet_type(self) -> None: 

385 tx: MoneroTxWallet = MoneroTxWallet() 

386 tx.hash = "a" * 64 

387 output: MoneroOutputWallet = MoneroOutputWallet() 

388 output.amount = 500000 

389 output.account_index = 2 

390 output.is_spent = True 

391 tx.outputs = [output] 

392 

393 copy: MoneroTxWallet = tx.copy() 

394 assert len(copy.outputs) == 1 

395 assert isinstance(copy.outputs[0], MoneroOutputWallet) 

396 assert copy.outputs[0].account_index == 2 

397 assert copy.outputs[0].is_spent is True