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

69 statements  

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

1 

2from typing import Optional 

3from typing_extensions import override 

4 

5from monero import MoneroWalletFull 

6 

7from .wallet_sync_printer import WalletSyncPrinter 

8 

9 

10class SyncProgressTester(WalletSyncPrinter): 

11 """Wallet sync progress tester.""" 

12 

13 wallet: MoneroWalletFull 

14 """Test wallet instance.""" 

15 start_height: int 

16 """Blockchain start height.""" 

17 prev_end_height: int 

18 """Previous blockchain end height.""" 

19 prev_height: Optional[int] 

20 """Previous notified blockchain height.""" 

21 prev_complete_height: Optional[int] 

22 """End height of the last completed sync session.""" 

23 session_start: bool 

24 """`True` while the next notification is the first of a sync session.""" 

25 is_done: bool 

26 """Indicates if wallet sync is completed.""" 

27 on_sync_progress_after_done: Optional[bool] 

28 """Indicates that `on_sync_progress` has been called after `on_done`.""" 

29 

30 @property 

31 def is_notified(self) -> bool: 

32 """Check if listener was notified. 

33 

34 :returns bool: `True` if listener got notified by sync progress. 

35 """ 

36 return self.prev_height is not None 

37 

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

39 """Initialize a new wallet sync progress tester. 

40 

41 :param MoneroWalletFull wallet: wallet to test. 

42 :param int start_height: wallet start height. 

43 :param int end_height: wallet end height. 

44 """ 

45 super(SyncProgressTester, self).__init__() 

46 self.wallet = wallet 

47 assert start_height >= 0, f"Invalid start height provided: {start_height}" 

48 assert end_height >= 0, f"Invalid end height provided: {end_height}" 

49 self.start_height = start_height 

50 self.prev_end_height = end_height 

51 self.session_start = True 

52 self.is_done = False 

53 

54 self.prev_height = None 

55 self.prev_complete_height = None 

56 self.on_sync_progress_after_done = None 

57 

58 @override 

59 def on_sync_progress(self, height: int, start_height: int, end_height: int, percent_done: float, message: str) -> None: 

60 """Invoked on wallet sync progress. 

61 

62 :param int height: current blockchain height. 

63 :param int start_height: sync start height. 

64 :param int end_height: sync end height. 

65 :param float percent_done: sync percentage progress. 

66 :param str message: sync progress message. 

67 """ 

68 super().on_sync_progress(height, start_height, end_height, percent_done, message) 

69 

70 # registered wallet listeners will continue to get sync notifications after the wallet's initial sync 

71 if self.is_done: 

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

73 self.on_sync_progress_after_done = True 

74 

75 # update tester's start height if new sync session 

76 if self.session_start and self.prev_complete_height is not None and start_height >= self.prev_complete_height: 

77 self.start_height = start_height 

78 

79 # progress notifications are throttled, so heights may skip, and the start height may rebase 

80 # down to report progress while the wallet skips hashes below the sync start 

81 assert end_height > start_height, "end height > start height" 

82 assert start_height <= self.start_height, "start height only rebases down" 

83 self.start_height = start_height 

84 assert end_height >= self.prev_end_height, "chain can only grow while syncing" 

85 self.prev_end_height = end_height 

86 if self.prev_height is not None: 

87 assert height >= self.prev_height, "heights advance monotonically" 

88 self.prev_height = height 

89 

90 if height < start_height: 

91 assert self.session_start, "hash-skip notification only at the start of a sync session" 

92 assert percent_done == 0.0 # initial notification while the wallet skips ahead to the sync start 

93 else: 

94 assert height < end_height 

95 expected_percent_done: float = (height - start_height + 1) / (end_height - start_height) 

96 assert expected_percent_done == percent_done 

97 if percent_done == 1.0: 

98 self.prev_complete_height = end_height # record completion height for subsequent sync sessions 

99 

100 # completion starts a new session 

101 self.session_start = percent_done == 1.0 

102 

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

104 """Called once on sync progress done. 

105 

106 :param int chain_height: blockchain height reached. 

107 """ 

108 assert self.is_done is False 

109 self.is_done = True 

110 if self.prev_height is None: 

111 assert self.prev_complete_height is None 

112 assert chain_height == self.start_height 

113 else: 

114 # otherwise the last progress notification reports the final block 

115 assert chain_height - 1 == self.prev_height 

116 assert chain_height == self.prev_complete_height