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

153 statements  

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

1 

2from monero import MoneroTx, MoneroBlock 

3 

4from .context import TestContext 

5from .gen_utils import GenUtils 

6from .output_utils import OutputUtils 

7 

8 

9class TxTester: 

10 """Test a `MoneroTx`.""" 

11 

12 tx: MoneroTx 

13 """Transaction to test.""" 

14 

15 ctx: TestContext 

16 """Transaction test context.""" 

17 

18 def __init__(self, tx: MoneroTx, ctx: TestContext) -> None: 

19 """Initialize a new transaction tester. 

20 

21 :param MoneroTx tx: transaction to test. 

22 :param TestContext ctx: test context. 

23 """ 

24 # check inputs 

25 assert ctx.is_pruned is not None 

26 assert ctx.is_confirmed is not None 

27 assert ctx.from_get_tx_pool is not None 

28 self.tx = tx 

29 self.ctx = ctx 

30 

31 def _test_standard(self) -> None: 

32 """Test standard tx data model across all txs.""" 

33 

34 # standard across all txs 

35 assert self.tx.hash is not None 

36 assert len(self.tx.hash) == 64 

37 if self.tx.is_relayed is None: 

38 assert self.tx.in_tx_pool is True 

39 else: 

40 assert self.tx.is_relayed is not None 

41 assert self.tx.is_confirmed is not None 

42 assert self.tx.in_tx_pool is not None 

43 assert self.tx.is_miner_tx is not None 

44 assert self.tx.is_double_spend_seen is not None 

45 assert self.tx.version is not None 

46 assert self.tx.version >= 0 

47 assert self.tx.unlock_time is not None 

48 assert self.tx.unlock_time >= 0 

49 assert self.tx.extra is not None 

50 assert len(self.tx.extra) > 0 

51 GenUtils.test_unsigned_big_integer(self.tx.fee, True) 

52 

53 # test presence of output indices 

54 # TODO change this over to outputs only 

55 if self.tx.is_miner_tx is True: 

56 # TODO how to get output indices for miner transactions? 

57 assert len(self.tx.output_indices) == 0 

58 assert self.tx.fee == 0 

59 assert len(self.tx.inputs) == 0 

60 assert len(self.tx.signatures) == 0 

61 if self.tx.in_tx_pool or self.ctx.from_get_tx_pool or self.ctx.has_output_indices is False: 

62 assert len(self.tx.output_indices) == 0 

63 else: 

64 assert len(self.tx.output_indices) > 0 

65 

66 def _test_confirmed(self) -> None: 

67 """Test transaction confirmation.""" 

68 # test confirmed ctx 

69 if self.ctx.is_confirmed is True: 

70 assert self.tx.is_confirmed is True 

71 elif self.ctx.is_confirmed is False: 

72 assert self.tx.is_confirmed is False 

73 

74 # test confirmed 

75 if self.tx.is_confirmed is True: 

76 block: MoneroBlock | None = self.tx.block 

77 assert block is not None 

78 assert self.tx in block.txs 

79 assert block.height is not None 

80 assert block.height > 0 

81 assert block.timestamp is not None 

82 assert block.timestamp > 0 

83 assert self.tx.relay is True 

84 assert self.tx.is_relayed is True 

85 assert self.tx.is_failed is False 

86 assert self.tx.in_tx_pool is False 

87 assert self.tx.is_double_spend_seen is False 

88 if self.ctx.from_binary_block is True: 

89 assert self.tx.num_confirmations is None 

90 else: 

91 assert self.tx.num_confirmations is not None 

92 assert self.tx.num_confirmations > 0 

93 else: 

94 assert self.tx.block is None, f"Expected block tx to be null: {self.tx.block.serialize()}" 

95 assert self.tx.num_confirmations == 0 

96 

97 def _test_in_tx_pool(self) -> None: 

98 """Test transaction pool details.""" 

99 # test in tx pool 

100 if self.tx.in_tx_pool: 

101 assert self.tx.is_confirmed is False 

102 assert self.tx.is_double_spend_seen is False 

103 assert self.tx.last_failed_height is None 

104 assert self.tx.last_failed_hash is None 

105 assert self.tx.received_timestamp is not None 

106 assert self.tx.received_timestamp > 0 

107 if self.ctx.from_get_tx_pool: 

108 assert self.tx.size is not None 

109 assert self.tx.size > 0 

110 assert self.tx.weight is not None 

111 assert self.tx.weight > 0 

112 assert self.tx.is_kept_by_block is not None 

113 assert self.tx.max_used_block_height is not None 

114 assert self.tx.max_used_block_height >= 0 

115 assert self.tx.max_used_block_hash is not None 

116 

117 assert self.tx.last_failed_height is None 

118 assert self.tx.last_failed_hash is None 

119 else: 

120 assert self.tx.last_relayed_timestamp is None 

121 

122 def _test_failed(self) -> None: 

123 """Test transaction failure details.""" 

124 # test failed 

125 # TODO what else to test associated with failed 

126 if self.tx.is_failed: 

127 assert self.tx.received_timestamp is not None 

128 assert self.tx.received_timestamp > 0 

129 else: 

130 if self.tx.is_relayed is None: 

131 assert self.tx.relay is None 

132 elif self.tx.is_relayed: 

133 assert self.tx.is_double_spend_seen is False 

134 else: 

135 assert self.tx.is_relayed is False 

136 if self.ctx.from_get_tx_pool: 

137 assert self.tx.relay is False 

138 assert self.tx.is_double_spend_seen is not None 

139 

140 assert self.tx.last_failed_height is None 

141 assert self.tx.last_failed_hash is None 

142 

143 # received time only for tx pool or failed txs 

144 if self.tx.received_timestamp is not None: 

145 assert self.tx.in_tx_pool or self.tx.is_failed 

146 

147 def _test_inputs_and_outputs(self) -> None: 

148 """Test transaction's inputs and outputs.""" 

149 

150 # test inputs and outputs 

151 if not self.tx.is_miner_tx: 

152 assert len(self.tx.inputs) > 0 

153 

154 for tx_input in self.tx.inputs: 

155 assert self.tx == tx_input.tx 

156 OutputUtils.test_input(tx_input, self.ctx) 

157 

158 assert len(self.tx.outputs) > 0 

159 for output in self.tx.outputs: 

160 assert self.tx == output.tx 

161 OutputUtils.test_output(output, self.ctx) 

162 

163 def _test_full_tx(self) -> None: 

164 """Test non-pruned transaction""" 

165 assert self.tx.version is not None 

166 assert self.tx.version >= 0 

167 assert self.tx.unlock_time is not None 

168 assert self.tx.unlock_time >= 0 

169 assert self.tx.extra is not None 

170 assert len(self.tx.extra) > 0 

171 

172 if self.ctx.from_binary_block is True: 

173 # TODO: get_blocks_by_height() has inconsistent client-side pruning 

174 assert self.tx.full_hex is None 

175 assert self.tx.rct_sig_prunable is None 

176 else: 

177 assert self.tx.full_hex is not None 

178 assert len(self.tx.full_hex) > 0 

179 # TODO define and test this 

180 #assert self.tx.rct_sig_prunable is not None 

181 

182 assert self.tx.is_double_spend_seen is False 

183 if self.tx.is_confirmed: 

184 assert self.tx.last_relayed_timestamp is None 

185 assert self.tx.received_timestamp is None 

186 else: 

187 if self.tx.is_relayed: 

188 assert self.tx.last_relayed_timestamp is not None 

189 assert self.tx.last_relayed_timestamp > 0 

190 else: 

191 assert self.tx.last_relayed_timestamp is None 

192 

193 assert self.tx.received_timestamp is not None 

194 assert self.tx.received_timestamp > 0 

195 

196 def _test_pruning(self) -> None: 

197 """Test transaction pruning.""" 

198 # test pruned vs not pruned 

199 # tx might be pruned regardless of configuration 

200 is_pruned: bool = self.tx.pruned_hex is not None 

201 if self.ctx.is_pruned: 

202 assert is_pruned 

203 if self.ctx.from_get_tx_pool or self.ctx.from_binary_block: 

204 assert self.tx.prunable_hash is None 

205 else: 

206 assert self.tx.prunable_hash is not None 

207 

208 if is_pruned: 

209 assert self.tx.rct_sig_prunable is None 

210 assert self.tx.size is None 

211 assert self.tx.last_relayed_timestamp is None 

212 assert self.tx.received_timestamp is None 

213 # TODO getting full hex in regtest regardless configuration 

214 # assert self.tx.full_hex is None, f"Expected None got: {tx.full_hex}" 

215 assert self.tx.pruned_hex is not None 

216 else: 

217 self._test_full_tx() 

218 

219 def run(self) -> None: 

220 """Run MoneroTx test.""" 

221 ... 

222 

223 self._test_standard() 

224 self._test_confirmed() 

225 self._test_in_tx_pool() 

226 self._test_failed() 

227 self._test_inputs_and_outputs() 

228 self._test_pruning() 

229 

230 # TODO test failed tx 

231 

232 # TODO implement extra copy 

233 # test deep copy 

234 #if ctx.do_not_test_copy is not True: 

235 # cls.test_tx_copy(tx, ctx)