Coverage for tests/utils/wallet_sync_tester.py: 96%

105 statements  

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

1import logging 

2 

3from typing import Optional 

4from typing_extensions import override 

5 

6from monero import ( 

7 MoneroOutputWallet, MoneroWalletFull, MoneroTxWallet, 

8 MoneroTxQuery 

9) 

10from .sync_progress_tester import SyncProgressTester 

11 

12logger: logging.Logger = logging.getLogger("WalletSyncTester") 

13 

14 

15class WalletSyncTester(SyncProgressTester): 

16 """Wallet sync tester.""" 

17 

18 wallet_tester_prev_height: Optional[int] 

19 """Renamed from `prev_height` to not interfere with super's `prev_height`.""" 

20 sync_start_height: int 

21 """Requested sync start height; unlike super's `start_height` it is not rebased by progress notifications.""" 

22 prev_output_received: Optional[MoneroOutputWallet] 

23 """Previous notified output received.""" 

24 prev_output_spent: Optional[MoneroOutputWallet] 

25 """Previous notified output spent.""" 

26 incoming_total: int 

27 """Total incoming amount collected.""" 

28 outgoing_total: int 

29 """Total outgoing amount collected.""" 

30 on_new_block_after_done: Optional[bool] 

31 """Indicates that `on_new_block` has been called after `on_done`.""" 

32 prev_balance: Optional[int] 

33 """Previous notified wallet balance.""" 

34 prev_unlocked_balance: Optional[int] 

35 """Previous notified wallet unlocked balance.""" 

36 

37 def __init__(self, wallet: MoneroWalletFull, start_height: int, end_height: int) -> None: 

38 """Initialize a new wallet sync tester. 

39 

40 :param MoneroWalletFull wallet: wallet to test. 

41 :param int start_height: blockchain start height. 

42 :param int end_height: blockchain end height. 

43 """ 

44 super().__init__(wallet, start_height, end_height) 

45 assert start_height >= 0 

46 assert end_height >= 0 

47 self.sync_start_height = start_height 

48 self.incoming_total = 0 

49 self.outgoing_total = 0 

50 

51 # initialize empty fields 

52 self.wallet_tester_prev_height = None 

53 self.prev_output_received = None 

54 self.prev_output_spent = None 

55 self.on_new_block_after_done = None 

56 self.prev_balance = None 

57 self.prev_unlocked_balance = None 

58 

59 @override 

60 def on_new_block(self, height: int) -> None: 

61 """Invoked when the wallet detects a new block. 

62 

63 :param int height: height of the new block. 

64 """ 

65 if self.is_done: 

66 assert self in self.wallet.get_listeners(), "Listener has completed and is not registered so should not be called again" 

67 self.on_new_block_after_done = True 

68 if self.wallet_tester_prev_height is not None: 

69 assert self.wallet_tester_prev_height + 1 == height 

70 

71 # scanned blocks start at the requested height, not the rebased progress base 

72 assert height >= self.sync_start_height 

73 self.wallet_tester_prev_height = height 

74 

75 @override 

76 def on_balances_changed(self, new_balance: int, new_unlocked_balance: int) -> None: 

77 """Invoked when the wallet's balance changes. 

78 

79 :param int new_balance: the wallet's new balance. 

80 :param int new_unlocked_balance: the wallet's new unlocked balance. 

81 """ 

82 if self.prev_balance is not None: 

83 assert new_balance != self.prev_balance or new_unlocked_balance != self.prev_unlocked_balance 

84 self.prev_balance = new_balance 

85 self.prev_unlocked_balance = new_unlocked_balance 

86 

87 # test received/spent output 

88 def test_output(self, output: MoneroOutputWallet, received: bool) -> None: 

89 """Test received or spent output. 

90 

91 :param MoneroOutputWallet output: output to test. 

92 :param bool received: `True` for received output, `False` for spent. 

93 """ 

94 assert output is not None 

95 if received: 

96 self.prev_output_received = output 

97 else: 

98 self.prev_output_spent = output 

99 

100 # test output 

101 assert output.amount is not None 

102 assert output.account_index is not None 

103 assert output.account_index >= 0 

104 

105 # TODO (monero-project): if spent, can be undefined because inputs not 

106 # provided so one created from outgoing transfer 

107 if received: 

108 assert output.subaddress_index is not None 

109 assert output.subaddress_index >= 0 

110 

111 # test output's tx 

112 assert output.tx is not None 

113 assert isinstance(output.tx, MoneroTxWallet) 

114 assert output.tx.hash is not None 

115 assert len(output.tx.hash) == 64 

116 assert output.tx.version is not None 

117 assert output.tx.version >= 0 

118 assert output.tx.unlock_time is not None 

119 assert output.tx.unlock_time >= 0 

120 # TODO this part is failing, maybe for little differences in java data model 

121 #if received: 

122 # assert len(output.tx.inputs) == 0 

123 # assert len(output.tx.outputs) == 1 

124 # assert output.tx.outputs[0] == output 

125 #else: 

126 # assert len(output.tx.inputs) == 1 

127 # assert output.tx.inputs[0] == output 

128 # assert len(output.tx.outputs) == 0 

129 assert len(output.tx.extra) > 0 

130 

131 if output.tx.is_locked: 

132 if received: 

133 # add incoming amount to running total 

134 # TODO: only add if not unlocked, test unlocked received 

135 self.incoming_total += output.amount 

136 else: 

137 # add outgoing amount to running total 

138 self.outgoing_total += output.amount 

139 

140 @override 

141 def on_output_received(self, output: MoneroOutputWallet) -> None: 

142 """Invoked when the wallet receives a new output. 

143 

144 :param MoneroOutputWallet output: the output received. 

145 """ 

146 self.test_output(output, True) 

147 

148 @override 

149 def on_output_spent(self, output: MoneroOutputWallet) -> None: 

150 """Invoked when one of the wallet's outputs is spent. 

151 

152 :param MoneroOutputWallet output: the output spent. 

153 """ 

154 self.test_output(output, False) 

155 

156 @override 

157 def on_done(self, chain_height: int) -> None: 

158 """Invoked when the wallet's initial sync completes. 

159 

160 :param int chain_height: blockchain height the wallet finished syncing to. 

161 """ 

162 super().on_done(chain_height) 

163 

164 assert self.wallet_tester_prev_height is not None 

165 assert self.prev_output_received is not None 

166 assert self.prev_output_spent is not None 

167 logger.info(f"incoming amount {self.incoming_total}, outgoing total {self.outgoing_total}") 

168 expected_balance: int = self.incoming_total - self.outgoing_total 

169 

170 # output notifications do not include pool fees or outgoing amount 

171 pool_spend_amount: int = 0 

172 query: MoneroTxQuery = MoneroTxQuery() 

173 query.in_tx_pool = True 

174 for pool_tx in self.wallet.get_txs(query): 

175 assert pool_tx.fee is not None 

176 pool_spend_amount += pool_tx.get_outgoing_amount() + pool_tx.fee 

177 

178 logger.debug(f"pool spend amount: {pool_spend_amount}, expected balance: {expected_balance}") 

179 expected_balance -= pool_spend_amount 

180 logger.debug(f"new expected balance = {expected_balance}") 

181 

182 wallet_balance: int = self.wallet.get_balance() 

183 wallet_unlocked_balance: int = self.wallet.get_unlocked_balance() 

184 assert expected_balance == wallet_balance, f"expected balance {expected_balance} != balance {wallet_balance}" 

185 assert self.prev_balance == wallet_balance, f"previous balance {expected_balance} != balance {wallet_balance}" 

186 assert self.prev_unlocked_balance == wallet_unlocked_balance, f"previous unlocked balance {expected_balance} != unlocked balance {wallet_balance}"