Coverage for tests/utils/sync_seed_tester.py: 97%

106 statements  

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

1import logging 

2 

3from typing import Optional, Callable 

4 

5from time import sleep, monotonic 

6from monero import ( 

7 MoneroDaemonRpc, MoneroWalletFull, MoneroWalletConfig, 

8 MoneroSyncResult, MoneroTxWallet 

9) 

10from .sync_progress_tester import SyncProgressTester 

11from .wallet_sync_tester import WalletSyncTester 

12from .test_utils import TestUtils 

13from .mining_utils import MiningUtils 

14from .wallet_equality_utils import WalletEqualityUtils 

15 

16logger: logging.Logger = logging.getLogger("SyncSeedTester") 

17 

18 

19class SyncSeedTester: 

20 """Wallet sync from seed tester.""" 

21 

22 __test__ = False 

23 

24 daemon: MoneroDaemonRpc 

25 """Test daemon instance.""" 

26 wallet: MoneroWalletFull 

27 """Test wallet instance.""" 

28 start_height: Optional[int] 

29 """Wallet start height.""" 

30 restore_height: Optional[int] 

31 """Wallet restore height.""" 

32 skip_gt_comparison: bool 

33 """Skip wallet ground truth comparison.""" 

34 test_post_sync_notifications: bool 

35 """Test post-sync wallet notifications.""" 

36 

37 create_wallet: Callable[[MoneroWalletConfig, bool], MoneroWalletFull] 

38 """Create wallet function.""" 

39 

40 def __init__( 

41 self, 

42 daemon: MoneroDaemonRpc, 

43 wallet: MoneroWalletFull, 

44 create_wallet: Callable[[MoneroWalletConfig, bool], MoneroWalletFull], 

45 start_height: Optional[int], 

46 restore_height: Optional[int], 

47 skip_gt_comparison: bool = False, 

48 test_post_sync_notifications: bool = False 

49 ) -> None: 

50 """Initialize a new sync seed tester. 

51 

52 :param MoneroDaemonRpc daemon: daemon test instance. 

53 :param MoneroWalletFull wallet: wallet test instance. 

54 :param Callable[[MoneroWalletConfig, bool], MoneroWalletFull] create_wallet: wallet creation function. 

55 :param int | None start_height: blockchain start height. 

56 :param int | None restore_height: wallet restore height. 

57 :param bool skip_gt_comparison: Skip wallet ground thruth verification (default `False`). 

58 :param bool test_post_sync_notifications: Test wallet post-sync notifications (default `False`). 

59 """ 

60 self.daemon = daemon 

61 self.wallet = wallet 

62 self.create_wallet = create_wallet 

63 self.start_height = start_height 

64 self.restore_height = restore_height 

65 self.skip_gt_comparison = skip_gt_comparison 

66 self.test_post_sync_notifications = test_post_sync_notifications 

67 

68 def test_post_sync(self, wallet: MoneroWalletFull, wallet_sync_tester: WalletSyncTester) -> None: 

69 """Test that a registered wallet listener keeps receiving sync notifications after the 

70 wallet's initial sync has completed. 

71 

72 :param MoneroWalletFull wallet: wallet to start syncing and test. 

73 :param WalletSyncTester wallet_sync_tester: listener whose post-completion notifications 

74 are asserted. 

75 """ 

76 # start automatic syncing 

77 wallet.start_syncing(TestUtils.SYNC_PERIOD_IN_MS) 

78 

79 # attempt to start mining to push the network along 

80 MiningUtils.try_start_mining() 

81 

82 try: 

83 logger.info("Waiting for next block to test post sync notifications") 

84 self.daemon.wait_for_next_block_header() 

85 

86 # ensure wallet has time to detect new block 

87 deadline: float = monotonic() + max(6 * TestUtils.SYNC_PERIOD_IN_MS / 1000, 60) 

88 while monotonic() < deadline: 

89 if wallet_sync_tester.on_sync_progress_after_done and wallet_sync_tester.on_new_block_after_done: 

90 break 

91 sleep(1) 

92 

93 # test that wallet listener's on_sync_progress() and on_new_block() were invoked after previous completion 

94 assert wallet_sync_tester.on_sync_progress_after_done 

95 assert wallet_sync_tester.on_new_block_after_done 

96 finally: 

97 MiningUtils.try_stop_mining() 

98 

99 def test_notifications(self, wallet: MoneroWalletFull, start_height_expected: int, end_height_expected: int) -> None: 

100 """Test wallet notifications. 

101 

102 :param MoneroWalletFull wallet: wallet to test. 

103 :param int start_height_expected: expected start height. 

104 :param int end_height_expected: expected end height. 

105 """ 

106 # test wallet's height before syncing 

107 assert wallet.is_connected_to_daemon() 

108 assert wallet.is_synced() is False 

109 assert wallet.get_height() == 1 

110 assert wallet.get_restore_height() == self.restore_height 

111 

112 # register a wallet listener which tests notifications throughout the sync 

113 wallet_sync_tester: WalletSyncTester = WalletSyncTester(wallet, start_height_expected, end_height_expected) 

114 wallet.add_listener(wallet_sync_tester) 

115 

116 # sync the wallet with a listener which tests sync notifications 

117 progress_tester: SyncProgressTester = SyncProgressTester(wallet, start_height_expected, end_height_expected) 

118 result: MoneroSyncResult = wallet.sync(self.start_height, progress_tester) if self.start_height is not None else wallet.sync(progress_tester) 

119 

120 # test completion of the wallet and sync listeners 

121 progress_tester.on_done(wallet.get_daemon_height()) 

122 wallet_sync_tester.on_done(wallet.get_daemon_height()) 

123 

124 # test result after syncing 

125 assert wallet.is_synced() 

126 assert wallet.get_daemon_height() - start_height_expected == result.num_blocks_fetched 

127 assert result.received_money 

128 

129 if wallet.get_height() != self.daemon.get_height(): 

130 # TODO height may not be same after long sync 

131 logger.warning(f"wallet height {wallet.get_height()} is not synced with daemon height {self.daemon.get_height()}") 

132 

133 assert wallet.get_daemon_height() == self.daemon.get_height() 

134 

135 wallet_txs: list[MoneroTxWallet] = wallet.get_txs() 

136 assert len(wallet_txs) > 0 

137 wallet_tx: MoneroTxWallet = wallet_txs[0] 

138 tx_height: int | None = wallet_tx.get_height() 

139 assert tx_height is not None 

140 

141 if start_height_expected > TestUtils.FIRST_RECEIVE_HEIGHT: 

142 assert tx_height > TestUtils.FIRST_RECEIVE_HEIGHT 

143 else: 

144 assert tx_height == TestUtils.FIRST_RECEIVE_HEIGHT 

145 

146 # sync the wallet with default params 

147 result = wallet.sync() 

148 assert wallet.is_synced() 

149 assert self.daemon.get_height() == wallet.get_height() 

150 # block might be added to chain 

151 assert result.num_blocks_fetched == 0 or result.num_blocks_fetched == 1 

152 assert result.received_money is False 

153 if wallet.get_restore_height() > wallet.get_height(): 

154 logger.warning( 

155 f"restore height {wallet.get_restore_height()} > wallet height {wallet.get_height()} " 

156 "after sync: wallet will re-scan on every sync" 

157 ) 

158 

159 # compare with ground truth 

160 if not self.skip_gt_comparison: 

161 wallet_gt: MoneroWalletFull = TestUtils.create_wallet_ground_truth(TestUtils.NETWORK_TYPE, wallet.get_seed(), self.start_height, self.restore_height) 

162 WalletEqualityUtils.test_wallet_full_equality_on_chain(wallet_gt, wallet) 

163 

164 # if testing post-sync notifications, wait for a block to be added to the chain 

165 # then test that sync arg listener was not invoked and registered wallet listener was invoked 

166 if self.test_post_sync_notifications: 

167 self.test_post_sync(wallet, wallet_sync_tester) 

168 

169 def test(self) -> None: 

170 """Run sync seed test.""" 

171 # check test parameters 

172 assert self.daemon.is_connected(), "Not connected to daemon" 

173 if self.start_height is not None and self.restore_height is not None: 

174 assert self.start_height <= TestUtils.FIRST_RECEIVE_HEIGHT or self.restore_height <= TestUtils.FIRST_RECEIVE_HEIGHT 

175 

176 # wait for txs to clear pool 

177 TestUtils.WALLET_TX_TRACKER.wait_for_txs_to_clear_pool(self.wallet) 

178 

179 # create wallet from seed 

180 config: MoneroWalletConfig = MoneroWalletConfig() 

181 config.seed = TestUtils.SEED 

182 config.restore_height = self.restore_height 

183 wallet: MoneroWalletFull = self.create_wallet(config, False) 

184 

185 # sanitize expected sync bounds 

186 if self.restore_height is None: 

187 self.restore_height = 0 

188 start_height_expected: int = self.start_height if self.start_height is not None else self.restore_height 

189 if start_height_expected == 0: 

190 start_height_expected = 1 

191 end_height_expected: int = wallet.get_daemon_max_peer_height() 

192 

193 # test wallet and close as final step 

194 wallet_gt: Optional[MoneroWalletFull] = None 

195 try: 

196 self.test_notifications(wallet, start_height_expected, end_height_expected) 

197 finally: 

198 if wallet_gt is not None: 

199 wallet_gt.close(True) 

200 wallet.close()