Coverage for tests/utils/wallet_notifications_tester.py: 93%

381 statements  

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

1from __future__ import annotations 

2 

3import logging 

4from time import monotonic 

5from typing import Callable, Optional 

6 

7from monero import ( 

8 MoneroDaemonRpc, MoneroDestination, MoneroOutputQuery, MoneroOutputWallet, 

9 MoneroTxConfig, MoneroTxQuery, MoneroTxWallet, MoneroWallet, MoneroWalletRpc 

10) 

11 

12from .context import TxContext 

13from .gen_utils import GenUtils 

14from .mining_utils import MiningUtils 

15from .test_utils import TestUtils 

16from .tx_wallet_utils import TxWalletUtils 

17from .wallet_notification_collector import WalletNotificationCollector 

18 

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

20 

21 

22class WalletNotificationsTester: 

23 """Sends funds from a wallet and tests the notifications of the sender and of the receiver, until the funds unlock. 

24 

25 The issues are collected as the test runs: the ones that start with `ERROR:` fail the test, the `WARNING:` ones are only logged. 

26 """ 

27 

28 NUM_BLOCKS_LOCKED: int = 10 

29 """Number of blocks for which received funds are locked.""" 

30 MAX_POLL_TIME_MS: int = 5000 

31 """Maximum time granted for a wallet to poll.""" 

32 

33 sender: MoneroWallet 

34 """Wallet that sends the funds.""" 

35 receiver: MoneroWallet 

36 """Wallet that receives the funds (the sender itself when sending to the same wallet).""" 

37 daemon: MoneroDaemonRpc 

38 """Daemon of the test network.""" 

39 same_account: bool 

40 """Send to the account the funds come from (account 0).""" 

41 sweep_output: bool 

42 """Sweep an output instead of sending to multiple accounts.""" 

43 create_then_relay: bool 

44 """Create the tx without relaying it, then relay it.""" 

45 unlock_delay: int 

46 """Blocks to wait, after the last height before sending, before testing the unlock notifications.""" 

47 issues: list[str] 

48 """Issues found while testing.""" 

49 

50 _sender_collector: WalletNotificationCollector 

51 _receiver_collector: WalletNotificationCollector 

52 _pending_checks: list[tuple[float, Callable[[], None]]] 

53 _started_mining: bool 

54 """`True` if the tester started mining, so it stops it at the end.""" 

55 """Checks to run when their time comes: the wallets get some sync periods to notify what happened.""" 

56 

57 def __init__( 

58 self, sender: MoneroWallet, receiver: MoneroWallet, daemon: MoneroDaemonRpc, 

59 same_account: bool, sweep_output: bool, create_then_relay: bool, unlock_delay: int 

60 ) -> None: 

61 """Initialize a new wallet notifications tester. 

62 

63 :param MoneroWallet sender: wallet that sends the funds. 

64 :param MoneroWallet receiver: wallet that receives the funds, `sender` to send to the same wallet. 

65 :param MoneroDaemonRpc daemon: daemon of the test network. 

66 :param bool same_account: send to the account the funds come from. 

67 :param bool sweep_output: sweep an output instead of sending to multiple accounts. 

68 :param bool create_then_relay: create the tx without relaying it, then relay it. 

69 :param int unlock_delay: blocks to wait before testing the unlock notifications. 

70 """ 

71 self.sender = sender 

72 self.receiver = receiver 

73 self.daemon = daemon 

74 self.same_account = same_account 

75 self.sweep_output = sweep_output 

76 self.create_then_relay = create_then_relay 

77 self.unlock_delay = unlock_delay 

78 self.issues = [] 

79 self._sender_collector = WalletNotificationCollector() 

80 self._receiver_collector = WalletNotificationCollector() 

81 self._pending_checks = [] 

82 self._started_mining = False 

83 

84 @property 

85 def same_wallet(self) -> bool: 

86 """`True` if the funds are sent to the same wallet.""" 

87 return self.sender is self.receiver 

88 

89 def test(self, test_name: str) -> None: 

90 """Run the test and fail if an issue is an error. 

91 

92 :param str test_name: name of the test, for the report of the issues. 

93 """ 

94 issues: list[str] = self.run() 

95 if len(issues) == 0: 

96 return 

97 msg: str = ( 

98 f"{test_name}(same_wallet={self.same_wallet}, same_account={self.same_account}, sweep_output={self.sweep_output}, " 

99 f"create_then_relay={self.create_then_relay}) generated {len(issues)} issues:\n{self.issues_to_str(issues)}" 

100 ) 

101 logger.warning(msg) 

102 assert "ERROR:" not in msg 

103 

104 def run(self) -> list[str]: 

105 """Send the funds and test the notifications until they unlock. 

106 

107 :returns list[str]: issues found. 

108 """ 

109 try: 

110 self._run() 

111 finally: 

112 # run the checks that are left, also when the test fails, then stop everything 

113 try: 

114 self._run_pending_checks(True) 

115 finally: 

116 # like monero-java's StartMining.stopMining(), it does not ask the daemon when the tester did not start mining: a test 

117 # that fails before, or that is not supported (the keys-only wallet), may have no daemon (the unit tests of the CI) 

118 if self._started_mining: 

119 self._stop_mining() 

120 self._stop_listening(self.sender, self._sender_collector) 

121 self._stop_listening(self.receiver, self._receiver_collector) 

122 

123 # errors raised inside the listeners 

124 for error in self._sender_collector.errors: 

125 self.issues.append(f"ERROR: sender listener raised: {error}") 

126 for error in self._receiver_collector.errors: 

127 self.issues.append(f"ERROR: receiver listener raised: {error}") 

128 

129 return self.issues 

130 

131 def _run(self) -> None: 

132 sender: MoneroWallet = self.sender 

133 receiver: MoneroWallet = self.receiver 

134 sender_collector: WalletNotificationCollector = self._sender_collector 

135 receiver_collector: WalletNotificationCollector = self._receiver_collector 

136 issues: list[str] = self.issues 

137 

138 # create receiver accounts if necessary 

139 num_accounts: int = len(receiver.get_accounts()) 

140 for _ in range(4 - num_accounts): 

141 receiver.create_account() 

142 

143 # wait for unlocked funds in source account 

144 TestUtils.WALLET_TX_TRACKER.wait_for_txs_to_clear_pool(sender) 

145 TestUtils.WALLET_TX_TRACKER.wait_for_unlocked_balance(sender, 0, None, TxWalletUtils.MAX_FEE * 10) 

146 

147 # include the last mined blocks before measuring balance changes 

148 self._stop_mining() 

149 sender.sync() 

150 if not self.same_wallet: 

151 receiver.sync() 

152 

153 # get balances to compare after sending 

154 sender_balance_before: int = sender.get_balance() 

155 sender_unlocked_balance_before: int = sender.get_unlocked_balance() 

156 receiver_balance_before: int = receiver.get_balance() 

157 receiver_unlocked_balance_before: int = receiver.get_unlocked_balance() 

158 last_height: int = self.daemon.get_height() 

159 

160 # start collecting notifications from sender and receiver 

161 sender.add_listener(sender_collector) 

162 GenUtils.wait_for(TestUtils.SYNC_PERIOD_IN_MS // 2) 

163 receiver.add_listener(receiver_collector) 

164 

165 # send funds 

166 ctx: TxContext = TxContext() 

167 ctx.wallet = sender 

168 ctx.is_send_response = True 

169 destination_accounts: list[int] 

170 if self.same_account: 

171 destination_accounts = [0] if self.sweep_output else [0, 1, 2] 

172 else: 

173 destination_accounts = [1] if self.sweep_output else [1, 2, 3] 

174 expected_outputs: list[MoneroOutputWallet] = [] 

175 sender_tx: MoneroTxWallet 

176 config: MoneroTxConfig = MoneroTxConfig() 

177 config.relay = not self.create_then_relay 

178 if self.sweep_output: 

179 ctx.is_sweep_response = True 

180 ctx.is_sweep_output_response = True 

181 output_query: MoneroOutputQuery = MoneroOutputQuery() 

182 output_query.is_spent = False 

183 output_query.account_index = 0 

184 output_query.min_amount = TxWalletUtils.MAX_FEE * 5 

185 unlocked_query: MoneroTxQuery = MoneroTxQuery() 

186 unlocked_query.is_locked = False 

187 output_query.set_tx_query(unlocked_query, True) 

188 outputs: list[MoneroOutputWallet] = sender.get_outputs(output_query) 

189 if len(outputs) == 0: 

190 issues.append("ERROR: No outputs available to sweep from account 0") 

191 return 

192 assert outputs[0].key_image is not None 

193 config.address = receiver.get_address(destination_accounts[0], 0) 

194 config.key_image = outputs[0].key_image.hex 

195 sender_tx = sender.sweep_output(config) 

196 assert sender_tx.outgoing_transfer is not None 

197 swept_amount: int | None = sender_tx.outgoing_transfer.destinations[0].amount 

198 assert swept_amount is not None 

199 expected_outputs.append(self._output(swept_amount, destination_accounts[0], 0)) 

200 else: 

201 config.account_index = 0 

202 destinations: list[MoneroDestination] = [] 

203 for destination_account in destination_accounts: 

204 # TODO: send and check random amounts? 

205 destinations.append(MoneroDestination(receiver.get_address(destination_account, 0), TxWalletUtils.MAX_FEE)) 

206 expected_outputs.append(self._output(TxWalletUtils.MAX_FEE, destination_account, 0)) 

207 config.destinations = destinations 

208 sender_tx = sender.create_tx(config) 

209 ctx.config = config 

210 if self.create_then_relay: 

211 sender.relay_tx(sender_tx) 

212 

213 # start timer to measure end of sync period 

214 start_time: float = monotonic() 

215 

216 # test send tx 

217 TxWalletUtils.test_tx_wallet(sender_tx, ctx) 

218 tx_hash: str | None = sender_tx.hash 

219 assert tx_hash is not None 

220 

221 # test sender after sending 

222 tx_outputs_query: MoneroOutputQuery = self._outputs_query(tx_hash) # query for outputs from sender tx 

223 if self.same_wallet: 

224 incoming_amount: int = sender_tx.get_incoming_amount() 

225 if incoming_amount == 0: 

226 issues.append("WARNING: sender tx incoming amount is 0 when sent to same wallet") 

227 else: 

228 assert sender_tx.fee is not None 

229 if incoming_amount != sender_tx.get_outgoing_amount() - sender_tx.fee: 

230 issues.append("WARNING: sender tx incoming amount != outgoing amount - fee when sent to same wallet") 

231 elif sender_tx.get_incoming_amount() != 0: 

232 issues.append("ERROR: tx incoming amount should be 0") 

233 tx_query: MoneroTxQuery = MoneroTxQuery() 

234 tx_query.hash = tx_hash 

235 tx_query.include_outputs = True 

236 sender_tx = sender.get_txs(tx_query)[0] 

237 assert sender_tx.fee is not None 

238 sender_balance: int = sender.get_balance() 

239 expected_sender_balance: int = ( 

240 sender_balance_before - sender_tx.fee - sender_tx.get_outgoing_amount() + sender_tx.get_incoming_amount() 

241 ) 

242 if sender_balance != expected_sender_balance: 

243 issues.append( 

244 f"ERROR: sender balance after send != balance before - tx fee - outgoing amount + incoming amount ({sender_balance} != " 

245 f"{sender_balance_before} - {sender_tx.fee} - {sender_tx.get_outgoing_amount()} + {sender_tx.get_incoming_amount()})" 

246 ) 

247 sender_unlocked_balance_after: int = sender.get_unlocked_balance() 

248 if sender_unlocked_balance_after >= sender_unlocked_balance_before: 

249 issues.append( 

250 f"ERROR: sender unlocked balance should have decreased after sending (before: {sender_unlocked_balance_before}, " 

251 f"after: {sender_unlocked_balance_after})" 

252 ) 

253 self._test_last_balance_notification(sender, sender_collector, "sender", "after sending", "did not notify balance change after sending") 

254 # TODO monero-cpp: the rpc wallet does not notify the outputs of the txs in the pool (see the xfail marks in test_listeners.py) 

255 if len(sender_collector.get_outputs_spent(tx_outputs_query)) == 0: 

256 issues.append("ERROR: sender did not announce unconfirmed spent output") 

257 

258 # test receiver after a sync period 

259 GenUtils.wait_for(self._remaining_sync_period_ms(start_time)) 

260 start_time = monotonic() # reset timer 

261 receiver_tx: MoneroTxWallet | None = receiver.get_tx(tx_hash) 

262 assert receiver_tx is not None 

263 if sender_tx.get_outgoing_amount() != receiver_tx.get_incoming_amount() and self.same_account: 

264 issues.append( 

265 "WARNING: sender tx outgoing amount != receiver tx incoming amount when sent to same account " 

266 f"({sender_tx.get_outgoing_amount()} != {receiver_tx.get_incoming_amount()})" 

267 ) 

268 receiver_fee: int = 0 

269 if self.same_wallet: 

270 assert receiver_tx.fee is not None 

271 receiver_fee = receiver_tx.fee 

272 receiver_balance: int = receiver.get_balance() 

273 expected_receiver_balance: int = ( 

274 receiver_balance_before + receiver_tx.get_incoming_amount() - receiver_tx.get_outgoing_amount() - receiver_fee 

275 ) 

276 if receiver_balance != expected_receiver_balance: 

277 level: str = "WARNING" if self.same_account else "ERROR" 

278 suffix: str = " when sent to same account" if self.same_account else "" 

279 issues.append( 

280 f"{level}: after sending, receiver balance != balance before + incoming amount - outgoing amount - tx fee{suffix} " 

281 f"({receiver_balance} != {receiver_balance_before} + {receiver_tx.get_incoming_amount()} - " 

282 f"{receiver_tx.get_outgoing_amount()} - {receiver_fee})" 

283 ) 

284 if not self.same_wallet and receiver.get_unlocked_balance() != receiver_unlocked_balance_before: 

285 issues.append("ERROR: receiver unlocked balance should not have changed after sending") 

286 self._test_last_balance_notification( 

287 receiver, receiver_collector, "receiver", "after funds received", "did not notify balance change when funds received" 

288 ) 

289 if len(receiver_collector.get_outputs_received(tx_outputs_query)) == 0: 

290 issues.append("ERROR: receiver did not announce unconfirmed received output") 

291 else: 

292 for output in self.get_missing_outputs(expected_outputs, receiver_collector.get_outputs_received(tx_outputs_query), True): 

293 issues.append(f"ERROR: receiver did not announce received output for {self._output_str(output)}") 

294 

295 # mine until the funds unlock 

296 if not MiningUtils.is_mining(): 

297 MiningUtils.start_mining() 

298 self._started_mining = True 

299 

300 # loop every sync period until unlock tested 

301 expected_unlock_height: int = last_height + self.unlock_delay 

302 confirm_height: Optional[int] = None 

303 while True: 

304 self._run_pending_checks(False) 

305 

306 # test height notifications 

307 height: int = self.daemon.get_height() 

308 if height > last_height: 

309 self._schedule(self._block_notifications_check(last_height, height)) 

310 last_height = height 

311 

312 # check if tx confirmed 

313 if confirm_height is None: 

314 

315 # get updated tx 

316 tx: MoneroTxWallet | None = receiver.get_tx(tx_hash) 

317 assert tx is not None 

318 

319 # break if tx fails 

320 if tx.is_failed: 

321 issues.append("ERROR: tx failed in tx pool") 

322 break 

323 

324 # test confirm notifications 

325 if tx.is_confirmed: 

326 confirm_height = tx.get_height() 

327 assert confirm_height is not None 

328 expected_unlock_height = max(confirm_height + self.NUM_BLOCKS_LOCKED, expected_unlock_height) # exact unlock time known 

329 self._schedule(self._confirmed_notifications_check(tx, tx_hash, expected_outputs)) 

330 

331 # otherwise test unlock notifications 

332 elif height >= expected_unlock_height: 

333 # freeze the chain so unrelated unlocks cannot change balances before the final checks 

334 self._stop_mining() 

335 self._schedule(self._unlocked_notifications_check(tx_hash, expected_outputs)) 

336 break 

337 

338 # wait for end of sync period 

339 GenUtils.wait_for(self._remaining_sync_period_ms(start_time)) 

340 start_time = monotonic() # reset timer 

341 

342 # wait for the pending checks 

343 self._run_pending_checks(True) 

344 

345 # test notified outputs 

346 for output in sender_collector.get_outputs_spent(tx_outputs_query): 

347 self.test_notified_output(output, True, issues) 

348 for output in sender_collector.get_outputs_received(tx_outputs_query): 

349 self.test_notified_output(output, False, issues) 

350 for output in receiver_collector.get_outputs_spent(tx_outputs_query): 

351 self.test_notified_output(output, True, issues) 

352 for output in receiver_collector.get_outputs_received(tx_outputs_query): 

353 self.test_notified_output(output, False, issues) 

354 

355 #region Checks 

356 

357 def _block_notifications_check(self, start_height: int, end_height: int) -> Callable[[], None]: 

358 """The check that the wallets announced the blocks from `start_height` to `end_height` (excluded).""" 

359 def check() -> None: 

360 for i in range(start_height, end_height): 

361 if i not in self._sender_collector.block_notifications: 

362 self.issues.append(f"ERROR: sender did not announce block {i}") 

363 if i not in self._receiver_collector.block_notifications: 

364 self.issues.append(f"ERROR: receiver did not announce block {i}") 

365 return check 

366 

367 def _confirmed_notifications_check( 

368 self, tx: MoneroTxWallet, tx_hash: str, expected_outputs: list[MoneroOutputWallet] 

369 ) -> Callable[[], None]: 

370 """The check of the notifications of the outputs of the tx when it is confirmed.""" 

371 def check() -> None: 

372 confirmed_query: MoneroOutputQuery = self._outputs_query(tx_hash, is_confirmed=True, is_locked=True) 

373 sender_collector: WalletNotificationCollector = self._sender_collector 

374 receiver_collector: WalletNotificationCollector = self._receiver_collector 

375 if len(sender_collector.get_outputs_spent(confirmed_query)) == 0: 

376 self.issues.append("ERROR: sender did not announce confirmed spent output") # TODO: test amount 

377 received: list[MoneroOutputWallet] = receiver_collector.get_outputs_received(confirmed_query) 

378 if len(received) == 0: 

379 self.issues.append("ERROR: receiver did not announce confirmed received output") 

380 else: 

381 for output in self.get_missing_outputs(expected_outputs, received, True): 

382 self.issues.append(f"ERROR: receiver did not announce confirmed received output for {self._output_str(output)}") 

383 

384 # if same wallet, net amount spent = tx fee = outputs spent - outputs received 

385 if self.same_wallet: 

386 net_amount: int = 0 

387 for output_spent in sender_collector.get_outputs_spent(confirmed_query): 

388 assert output_spent.amount is not None 

389 net_amount += output_spent.amount 

390 for output_received in sender_collector.get_outputs_received(confirmed_query): 

391 assert output_received.amount is not None 

392 net_amount -= output_received.amount 

393 if tx.fee != net_amount: 

394 if self.same_account: 

395 self.issues.append(f"WARNING: net output amount != tx fee when funds sent to same account: {net_amount} vs {tx.fee}") 

396 elif isinstance(self.sender, MoneroWalletRpc): 

397 # TODO (monero-project): open issue to provide tx inputs 

398 self.issues.append( 

399 "WARNING: net output amount != tx fee when funds sent to same wallet because monero-wallet-rpc does not " 

400 f"provide tx inputs: {net_amount} vs {tx.fee}" 

401 ) 

402 else: 

403 self.issues.append(f"ERROR: net output amount must equal tx fee when funds sent to same wallet: {net_amount} vs {tx.fee}") 

404 return check 

405 

406 def _unlocked_notifications_check(self, tx_hash: str, expected_outputs: list[MoneroOutputWallet]) -> Callable[[], None]: 

407 """The check of the notifications of the outputs and of the balances when the funds unlock.""" 

408 def check() -> None: 

409 unlocked_query: MoneroOutputQuery = self._outputs_query(tx_hash, is_locked=False) 

410 if len(self._sender_collector.get_outputs_spent(unlocked_query)) == 0: 

411 self.issues.append("ERROR: sender did not announce unlocked spent output") # TODO: test amount? 

412 received: list[MoneroOutputWallet] = self._receiver_collector.get_outputs_received(unlocked_query) 

413 for output in self.get_missing_outputs(expected_outputs, received, True): 

414 self.issues.append(f"ERROR: receiver did not announce unlocked received output for {self._output_str(output)}") 

415 if not self.same_wallet and self.receiver.get_balance() != self.receiver.get_unlocked_balance(): 

416 self.issues.append("ERROR: receiver balance != unlocked balance after funds unlocked") 

417 self._test_last_balance_notification( 

418 self.sender, self._sender_collector, "sender", "after funds unlocked", "did not announce any balance notifications" 

419 ) 

420 self._test_last_balance_notification( 

421 self.receiver, self._receiver_collector, "receiver", "after funds unlocked", "did not announce any balance notifications" 

422 ) 

423 return check 

424 

425 def _test_last_balance_notification( 

426 self, wallet: MoneroWallet, collector: WalletNotificationCollector, role: str, when: str, missing_msg: str 

427 ) -> None: 

428 """Test that the last notified balances are the ones of the wallet.""" 

429 if len(collector.balance_notifications) == 0: 

430 self.issues.append(f"ERROR: {role} {missing_msg}") 

431 return 

432 last_balance, last_unlocked_balance = collector.balance_notifications[-1] 

433 balance: int = wallet.get_balance() 

434 unlocked_balance: int = wallet.get_unlocked_balance() 

435 if balance != last_balance: 

436 self.issues.append(f"ERROR: {role} balance != last notified balance {when} ({balance} != {last_balance})") 

437 if unlocked_balance != last_unlocked_balance: 

438 self.issues.append( 

439 f"ERROR: {role} unlocked balance != last notified unlocked balance {when} ({unlocked_balance} != {last_unlocked_balance})" 

440 ) 

441 

442 @classmethod 

443 def test_notified_output(cls, output: MoneroOutputWallet, is_tx_input: bool, issues: list[str]) -> None: 

444 """Test an output notified by a wallet. 

445 

446 :param MoneroOutputWallet output: notified output. 

447 :param bool is_tx_input: `True` if the output was notified as spent. 

448 :param list[str] issues: collects the issues found. 

449 """ 

450 # test tx link 

451 assert output.tx is not None 

452 if is_tx_input: 

453 assert output in output.tx.inputs 

454 else: 

455 assert output in output.tx.outputs 

456 

457 # test output values 

458 GenUtils.test_unsigned_big_integer(output.amount) 

459 state: str = cls.get_output_state(output) 

460 if output.account_index is not None: 

461 assert output.account_index >= 0 

462 elif is_tx_input: 

463 # TODO (monero-project): account index not provided when output swept by key image. could retrieve it but slows tx creation significantly 

464 issues.append(f"WARNING: notification of {state} spent output missing account index") 

465 else: 

466 issues.append(f"ERROR: notification of {state} received output missing account index") 

467 if output.subaddress_index is not None: 

468 assert output.subaddress_index >= 0 

469 elif is_tx_input: 

470 # TODO (monero-project): because inputs are not provided, creating fake input from outgoing transfer, which can be sourced from 

471 # multiple subaddress indices, whereas an output can only come from one subaddress index; need to provide tx inputs to resolve this 

472 issues.append(f"WARNING: notification of {state} spent output missing subaddress index") 

473 else: 

474 issues.append(f"ERROR: notification of {state} received output missing subaddress index") 

475 

476 #endregion 

477 

478 #region Utils 

479 

480 @staticmethod 

481 def get_output_state(output: MoneroOutputWallet) -> str: 

482 """Get the state of a notified output. 

483 

484 :param MoneroOutputWallet output: notified output. 

485 :returns str: `unlocked`, `confirmed` or `unconfirmed`. 

486 """ 

487 assert output.tx is not None 

488 if output.tx.is_locked is False: 

489 return "unlocked" 

490 if output.tx.is_confirmed is True: 

491 return "confirmed" 

492 if output.tx.is_confirmed is False: 

493 return "unconfirmed" 

494 raise Exception(f"Unknown output state: {output.serialize()}") 

495 

496 @staticmethod 

497 def get_missing_outputs( 

498 expected_outputs: list[MoneroOutputWallet], actual_outputs: list[MoneroOutputWallet], match_subaddress: bool 

499 ) -> list[MoneroOutputWallet]: 

500 """Get the expected outputs that are not in the actual ones, matching each actual output once. 

501 

502 :param list[MoneroOutputWallet] expected_outputs: expected outputs. 

503 :param list[MoneroOutputWallet] actual_outputs: actual outputs. 

504 :param bool match_subaddress: match also the account and subaddress indices. 

505 :returns list[MoneroOutputWallet]: expected outputs not found. 

506 """ 

507 missing: list[MoneroOutputWallet] = [] 

508 used: list[int] = [] 

509 for expected_output in expected_outputs: 

510 found: bool = False 

511 for i, actual_output in enumerate(actual_outputs): 

512 if i in used: 

513 continue 

514 if actual_output.amount == expected_output.amount and ( 

515 not match_subaddress or ( 

516 actual_output.account_index == expected_output.account_index 

517 and actual_output.subaddress_index == expected_output.subaddress_index 

518 ) 

519 ): 

520 used.append(i) 

521 found = True 

522 break 

523 if not found: 

524 missing.append(expected_output) 

525 return missing 

526 

527 @staticmethod 

528 def issues_to_str(issues: list[str]) -> str: 

529 """Numbered list of issues, one per line. 

530 

531 :param list[str] issues: issues. 

532 :returns str: the issues, one per line. 

533 """ 

534 return "\n".join(f"{i + 1}: {issue}" for i, issue in enumerate(issues)) 

535 

536 @staticmethod 

537 def _output(amount: int, account_index: int, subaddress_index: int) -> MoneroOutputWallet: 

538 output: MoneroOutputWallet = MoneroOutputWallet() 

539 output.amount = amount 

540 output.account_index = account_index 

541 output.subaddress_index = subaddress_index 

542 return output 

543 

544 @staticmethod 

545 def _output_str(output: MoneroOutputWallet) -> str: 

546 return f"amount {output.amount} to subaddress [{output.account_index}, {output.subaddress_index}]" 

547 

548 @staticmethod 

549 def _outputs_query(tx_hash: str, is_confirmed: Optional[bool] = None, is_locked: Optional[bool] = None) -> MoneroOutputQuery: 

550 """Query for the outputs of the tx with `tx_hash`.""" 

551 tx_query: MoneroTxQuery = MoneroTxQuery() 

552 tx_query.hash = tx_hash 

553 tx_query.is_confirmed = is_confirmed 

554 tx_query.is_locked = is_locked 

555 query: MoneroOutputQuery = MoneroOutputQuery() 

556 query.set_tx_query(tx_query, True) 

557 return query 

558 

559 @staticmethod 

560 def _remaining_sync_period_ms(start_time: float) -> int: 

561 """Milliseconds left of the sync period started at `start_time`.""" 

562 return max(0, int(TestUtils.SYNC_PERIOD_IN_MS - (monotonic() - start_time) * 1000)) 

563 

564 def _schedule(self, check: Callable[[], None]) -> None: 

565 """Run `check` after 2 sync periods and the poll time, the time the wallets have to notify what happened.""" 

566 delay_s: float = (TestUtils.SYNC_PERIOD_IN_MS * 2 + self.MAX_POLL_TIME_MS) / 1000 

567 self._pending_checks.append((monotonic() + delay_s, check)) 

568 

569 def _run_pending_checks(self, wait: bool) -> None: 

570 """Run the checks whose time has come, or all of them, waiting for each one, if `wait`.""" 

571 while len(self._pending_checks) > 0: 

572 due_time, check = self._pending_checks[0] 

573 remaining_ms: int = int((due_time - monotonic()) * 1000) 

574 if remaining_ms > 0: 

575 if not wait: 

576 return 

577 GenUtils.wait_for(remaining_ms) 

578 self._pending_checks.pop(0) 

579 check() 

580 

581 @staticmethod 

582 def _stop_mining() -> None: 

583 """Stop mining, if the daemon is mining.""" 

584 if MiningUtils.is_mining(): 

585 MiningUtils.stop_mining() 

586 

587 @staticmethod 

588 def _stop_listening(wallet: MoneroWallet, collector: WalletNotificationCollector) -> None: 

589 """Remove the collector from the wallet, if it was added, and stop it.""" 

590 try: 

591 wallet.remove_listener(collector) 

592 except Exception as e: 

593 logger.debug(f"Listener not removed: {e}") 

594 collector.listening = False 

595 

596 #endregion