Coverage for tests/common/test_wallet_tx_tracker.py: 98%

48 statements  

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

1from __future__ import annotations 

2 

3from typing import Any 

4 

5import pytest 

6from monero import MoneroSyncResult, MoneroTx, MoneroTxQuery, MoneroTxWallet, MoneroWalletRpc 

7 

8from utils import BaseTestClass, FakeWalletRpcServer, WalletTxTracker 

9 

10 

11class _Daemon: 

12 """The part of a daemon that the tracker uses when there is nothing to wait for.""" 

13 

14 def get_height(self) -> int: 

15 return 0 

16 

17 def get_tx_pool(self) -> list[MoneroTx]: 

18 return [] 

19 

20 def stop_mining(self) -> None: 

21 pass 

22 

23 

24class _Wallet: 

25 """A wallet that is not a wallet rpc and has no pending txs, that counts the times that it was asked which outputs are spent.""" 

26 

27 def __init__(self) -> None: 

28 self.num_rescans: int = 0 

29 

30 def sync(self) -> MoneroSyncResult: 

31 result: MoneroSyncResult = MoneroSyncResult() 

32 result.num_blocks_fetched = 0 

33 return result 

34 

35 def get_txs(self, query: MoneroTxQuery) -> list[MoneroTxWallet]: 

36 return [] 

37 

38 def get_height(self) -> int: 

39 return 0 

40 

41 def rescan_spent(self) -> None: 

42 self.num_rescans += 1 

43 

44 

45@pytest.mark.unit 

46class TestWalletTxTracker(BaseTestClass): 

47 """The tracker waits for the txs of a wallet to leave the pool, and then the wallet is fit for the next tx.""" 

48 

49 #region Tests 

50 

51 # A wallet rpc is asked which of its outputs are spent, when its txs left the pool 

52 def test_rpc_wallet_rescans_spent_outputs_after_the_pool_is_clear(self) -> None: 

53 # the server of a wallet rpc sets back as unspent the inputs of a tx that it saw in the pool and that left it, 500 s after, and 

54 # the next tx used them: the daemon rejected it as a double spend (test_subtract_fee_from, in CI) 

55 tracker: WalletTxTracker = WalletTxTracker(_Daemon(), 10, "") # type: ignore[arg-type] 

56 with FakeWalletRpcServer() as server: 

57 wallet: MoneroWalletRpc = MoneroWalletRpc(server.uri) 

58 tracker.wait_for_txs_to_clear_pool([wallet]) 

59 methods: list[str] = server.methods 

60 assert "refresh" in methods, f"The wallet was not synced: {methods}" 

61 assert "rescan_spent" in methods, f"The wallet was not asked which outputs are spent: {methods}" 

62 assert methods.index("rescan_spent") > methods.index("refresh"), f"It was asked before it synced: {methods}" 

63 

64 # Every wallet rpc of a list is asked, not only the first one 

65 def test_each_rpc_wallet_rescans_spent_outputs(self) -> None: 

66 tracker: WalletTxTracker = WalletTxTracker(_Daemon(), 10, "") # type: ignore[arg-type] 

67 with FakeWalletRpcServer() as server1, FakeWalletRpcServer() as server2: 

68 wallets: list[Any] = [MoneroWalletRpc(server1.uri), MoneroWalletRpc(server2.uri)] 

69 tracker.wait_for_txs_to_clear_pool(wallets) 

70 assert "rescan_spent" in server1.methods 

71 assert "rescan_spent" in server2.methods 

72 

73 # The other wallets are not asked: the full wallet follows its own outputs, and a wallet that is not a wallet rpc may not have it 

74 def test_other_wallets_do_not_rescan_spent_outputs(self) -> None: 

75 tracker: WalletTxTracker = WalletTxTracker(_Daemon(), 10, "") # type: ignore[arg-type] 

76 wallet: _Wallet = _Wallet() 

77 tracker.wait_for_txs_to_clear_pool([wallet]) # type: ignore[list-item] 

78 assert wallet.num_rescans == 0 

79 

80 #endregion