Coverage for tests/utils/wallet_notification_collector.py: 75%
81 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 19:49 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 19:49 +0000
1import logging
3from typing_extensions import override
5from monero import (
6 MoneroWalletListener, MoneroOutputWallet,
7 MoneroOutputQuery
8)
10logger: logging.Logger = logging.getLogger("WalletNotificationCollector")
13class WalletNotificationCollector(MoneroWalletListener):
14 """Collects blocks, outputs and balances changes from wallet."""
16 listening: bool
17 """Indicates if listener is expected to be active."""
18 block_notifications: list[int]
19 """Collection of blocks."""
20 balance_notifications: list[tuple[int, int]]
21 """Collection of balance notifications."""
22 outputs_received: list[MoneroOutputWallet]
23 """Collection of outputs received by the wallet."""
24 outputs_spent: list[MoneroOutputWallet]
25 """Collection of outputs spend by the wallet."""
26 errors: list[Exception]
27 """Errors while collecting notifications."""
29 def __init__(self) -> None:
30 """Initialize a new wallet notification collector."""
31 super().__init__()
32 self.listening = True
33 self.block_notifications = []
34 self.balance_notifications = []
35 self.outputs_received = []
36 self.outputs_spent = []
37 self.errors = []
39 @override
40 def on_new_block(self, height: int) -> None:
41 """Invoked when the wallet detects a new block.
43 :param int height: height of the new block.
44 """
45 try:
46 assert self.listening
47 num_block_notifications: int = len(self.block_notifications)
49 if num_block_notifications > 0:
50 # check block notifications order
51 expected_height: int = self.block_notifications[num_block_notifications - 1] + 1
52 assert height == expected_height, f"Expected height {expected_height}, got {height}"
54 # collect height
55 self.block_notifications.append(height)
56 logger.debug(f"Collected height: {height}")
57 except Exception as e:
58 logger.error(f"{e}")
59 self.errors.append(e)
61 @override
62 def on_balances_changed(self, new_balance: int, new_unlocked_balance: int) -> None:
63 """Invoked when the wallet's balance changes.
65 :param int new_balance: the wallet's new balance.
66 :param int new_unlocked_balance: the wallet's new unlocked balance.
67 """
68 try:
69 assert self.listening
70 num_balance_notifications: int = len(self.balance_notifications)
72 if num_balance_notifications > 0:
73 last_notification: tuple[int, int] = self.balance_notifications[num_balance_notifications - 1]
74 # test that balances change
75 assert new_balance != last_notification[0] or new_unlocked_balance != last_notification[1]
77 # collect balance notification
78 self.balance_notifications.append((new_balance, new_unlocked_balance))
79 logger.debug(f"Collected balance: {new_balance}, unlocked balance: {new_unlocked_balance}")
80 except Exception as e:
81 logger.error(f"{e}")
82 self.errors.append(e)
84 @override
85 def on_output_received(self, output: MoneroOutputWallet) -> None:
86 """Invoked when the wallet receives a new output.
88 :param MoneroOutputWallet output: the output received.
89 """
90 try:
91 assert self.listening
92 # collect received output
93 self.outputs_received.append(output)
94 logger.debug(f"Received output: {output.serialize()}")
95 except Exception as e:
96 logger.error(f"{e}")
97 self.errors.append(e)
99 @override
100 def on_output_spent(self, output: MoneroOutputWallet) -> None:
101 """Invoked when one of the wallet's outputs is spent.
103 :param MoneroOutputWallet output: the output spent.
104 """
105 try:
106 assert self.listening
107 # collect spent output
108 self.outputs_spent.append(output)
109 logger.debug(f"Spent output: {output.serialize()}")
110 except Exception as e:
111 logger.error(f"{e}")
112 self.errors.append(e)
114 def get_outputs_received(self, query: MoneroOutputQuery) -> list[MoneroOutputWallet]:
115 """Get outputs received by query.
117 :param MoneroOutputQuery query: filter outputs received.
118 :returns list[MoneroOutputWallet]: outputs received by the wallet filtered by query.
119 """
120 result: list[MoneroOutputWallet] = []
121 # filter received outputs
122 for output in self.outputs_received:
123 if query.meets_criteria(output):
124 result.append(output)
125 return result
127 def get_outputs_spent(self, query: MoneroOutputQuery) -> list[MoneroOutputWallet]:
128 """Get outputs spent by query.
130 :param MoneroOutputQuery query: filter outputs spent.
131 :returns list[MoneroOutputWallet]: outputs spent by the wallet filtered by query.
132 """
133 result: list[MoneroOutputWallet] = []
134 # filter spent outputs
135 for output in self.outputs_spent:
136 if query.meets_criteria(output):
137 result.append(output)
138 return result