Coverage for tests/utils/txs_structure_tester.py: 98%

107 statements  

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

1from typing import Optional 

2 

3from monero import MoneroBlock, MoneroTxWallet, MoneroTxQuery, MoneroTx 

4 

5from .block_utils import BlockUtils 

6 

7 

8class TxsStructureTester: 

9 """Tests the integrity of the full structure in the given txs from the block down to transfers / destinations.""" 

10 

11 txs: list[MoneroTxWallet] 

12 """Txs to test structure.""" 

13 

14 query: MoneroTxQuery 

15 """Filter txs by query.""" 

16 

17 regtest: bool 

18 """Indicates if running test on regtest network.""" 

19 

20 seen_blocks: set[MoneroBlock] 

21 """Unique set of seen blocks in txs.""" 

22 

23 blocks: list[MoneroBlock] 

24 """All blocks seen by txs.""" 

25 

26 unconfirmed_txs: list[MoneroTxWallet] 

27 """Unconfirmed transactions to test.""" 

28 

29 @property 

30 def num_txs(self) -> int: 

31 """Number of transactions to test. 

32 

33 :returns int: number of transactions to test. 

34 """ 

35 return len(self.txs) 

36 

37 @property 

38 def num_unconfirmed_txs(self) -> int: 

39 """Number of unconfirmed txs to test. 

40 

41 :returns int: number of unconfirmed txs to test. 

42 """ 

43 return len(self.unconfirmed_txs) 

44 

45 @property 

46 def num_tx_hashes(self) -> int: 

47 """Number of tx hashes set in tx query. 

48 

49 :returns int: number of tx hashes set in tx query. 

50 """ 

51 return len(self.query.hashes) 

52 

53 def __init__(self, txs: list[MoneroTxWallet], query: Optional[MoneroTxQuery], regtest: bool) -> None: 

54 """Initialize a new txs structure tester. 

55 

56 :param list[MoneroTxWallet] txs: list of txs to get structure from. 

57 :param MoneroTxQuery | None query: filter txs by query, if set. 

58 :param bool regtest: indicates if running test on regtest network. 

59 """ 

60 self.txs = txs 

61 self.query = query if query is not None else MoneroTxQuery() 

62 self.seen_blocks = set() 

63 self.blocks = [] 

64 self.unconfirmed_txs = [] 

65 self.regtest = regtest 

66 

67 # initialize 

68 for tx in txs: 

69 if tx.block is None: 

70 self.unconfirmed_txs.append(tx) 

71 else: 

72 assert BlockUtils.is_tx_in_block(tx.hash, tx.block) 

73 if tx.block not in self.seen_blocks: 

74 self.seen_blocks.add(tx.block) 

75 self.blocks.append(tx.block) 

76 

77 def _test_block_txs_order(self, tx: MoneroTx, block: MoneroBlock, index: int) -> None: 

78 """Test that `tx` is at the expected position within `block`'s tx order. 

79 

80 :param MoneroTx tx: transaction to test. 

81 :param MoneroBlock block: block `tx` belongs to. 

82 :param int index: expected position of `tx` in `self.txs`. 

83 """ 

84 assert tx.block == block 

85 if self.num_tx_hashes == 0: 

86 other: MoneroTxWallet = self.txs[index] 

87 if not self.regtest: 

88 assert other.hash == tx.hash, "Txs in block are not in order" 

89 # verify tx order is self-consistent with blocks unless txs manually re-ordered by querying by hash 

90 assert other == tx 

91 else: 

92 # TODO regtest wallet2 has inconsinstent txs order between calls 

93 assert other in block.txs, "Tx not found in block" 

94 

95 def _test_txs_order(self) -> None: 

96 """Test that txs and blocks reference each other and blocks are in 

97 ascending order unless specific tx hashes queried. 

98 """ 

99 # tx hashes must be in order if requested 

100 if self.num_tx_hashes > 0: 

101 assert self.num_txs == self.num_tx_hashes 

102 for i, query_hash in enumerate(self.query.hashes): 

103 assert query_hash == self.txs[i].hash 

104 

105 # test that txs and blocks reference each other and blocks are in ascending order unless specific tx hashes queried 

106 index: int = 0 

107 prev_block_height: Optional[int] = None 

108 for block in self.blocks: 

109 if prev_block_height is None: 

110 prev_block_height = block.height 

111 elif self.num_tx_hashes == 0: 

112 assert block.height is not None 

113 msg: str = f"Blocks are not in order of heights: {prev_block_height} vs {block.height}" 

114 assert block.height > prev_block_height, msg 

115 

116 for tx in block.txs: 

117 self._test_block_txs_order(tx, block, index) 

118 index += 1 

119 

120 assert self.num_txs == index + self.num_unconfirmed_txs, f"txs: {self.num_txs}, unconfirmed txs: {self.num_unconfirmed_txs}, index: {index}" 

121 

122 def _test_incoming_transfers_order(self) -> None: 

123 """Test that incoming transfers are in order 

124 of ascending accounts and subaddresses. 

125 """ 

126 # test that incoming transfers are in order of ascending accounts and subaddresses 

127 for tx in self.txs: 

128 if len(tx.incoming_transfers) == 0: 

129 continue 

130 

131 prev_account_idx: Optional[int] = None 

132 prev_subaddress_idx: Optional[int] = None 

133 for transfer in tx.incoming_transfers: 

134 if prev_account_idx is None: 

135 prev_account_idx = transfer.account_index 

136 

137 else: 

138 assert prev_account_idx is not None 

139 assert transfer.account_index is not None 

140 assert prev_account_idx <= transfer.account_index 

141 if prev_account_idx < transfer.account_index: 

142 prev_subaddress_idx = None 

143 prev_account_idx = transfer.account_index 

144 if prev_subaddress_idx is None: 

145 prev_subaddress_idx = transfer.subaddress_index 

146 else: 

147 assert transfer.subaddress_index is not None 

148 assert prev_subaddress_idx < transfer.subaddress_index 

149 

150 def _test_outputs_order(self) -> None: 

151 """test that outputs are in order of ascending accounts and subaddresses.""" 

152 # test that outputs are in order of ascending accounts and subaddresses 

153 for tx in self.txs: 

154 if len(tx.outputs) == 0: 

155 continue 

156 

157 prev_account_idx: Optional[int] = None 

158 prev_subaddress_idx: Optional[int] = None 

159 for output in tx.get_outputs_wallet(): 

160 if prev_account_idx is None: 

161 prev_account_idx = output.account_index 

162 else: 

163 assert output.account_index is not None 

164 assert prev_account_idx <= output.account_index 

165 if prev_account_idx < output.account_index: 

166 prev_subaddress_idx = None 

167 prev_account_idx = output.account_index 

168 if prev_subaddress_idx is None: 

169 prev_subaddress_idx = output.subaddress_index 

170 else: 

171 assert prev_subaddress_idx is not None 

172 assert output.subaddress_index is not None 

173 # TODO: this does not test that index < other index if subaddresses are equal 

174 assert prev_subaddress_idx <= output.subaddress_index 

175 

176 def run(self) -> None: 

177 """Run test.""" 

178 self._test_txs_order() 

179 self._test_incoming_transfers_order() 

180 self._test_outputs_order()