Coverage for tests/utils/tx_wallet_tester.py: 95%

206 statements  

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

1import logging 

2 

3from typing import Optional 

4 

5from monero import ( 

6 MoneroTxWallet, MoneroTransfer, MoneroTxConfig, 

7 MoneroUtils, MoneroDestination 

8) 

9 

10from .gen_utils import GenUtils 

11from .output_utils import OutputUtils 

12from .transfer_utils import TransferUtils 

13from .context import TxContext 

14 

15logger: logging.Logger = logging.getLogger("TxWalletTester") 

16 

17 

18class TxWalletTester: 

19 

20 tx: MoneroTxWallet 

21 ctx: TxContext 

22 

23 def __init__(self, tx: MoneroTxWallet, context: Optional[TxContext]) -> None: 

24 """Initialize a new tx wallet tester. 

25 

26 :param MoneroTxWallet tx: transaction to test. 

27 :param TxContext | None context: test context. 

28 """ 

29 self.tx = tx 

30 # validate / sanitize inputs 

31 self.ctx = TxContext(context) 

32 self.ctx.wallet = None # TODO: re-enable 

33 assert tx is not None 

34 if self.ctx.is_send_response is None or self.ctx.config is None: 

35 assert self.ctx.is_send_response is None, "if either send_request or is_send_response is defined, they must both be defined" 

36 assert self.ctx.config is None, "if either send_request or is_send_response is defined, they must both be defined" 

37 

38 logger.debug(f"Initialized TxWalletTester with tx: {tx.serialize()}, and context: {self.ctx.serialize()}") 

39 

40 def _test_common(self) -> None: 

41 """Test common tx field types.""" 

42 # test common field types 

43 assert self.tx.hash is not None 

44 assert self.tx.is_confirmed is not None 

45 assert self.tx.is_miner_tx is not None 

46 assert self.tx.is_failed is not None 

47 assert self.tx.is_relayed is not None 

48 assert self.tx.in_tx_pool is not None 

49 assert self.tx.is_locked is not None 

50 GenUtils.test_unsigned_big_integer(self.tx.fee) 

51 if self.tx.payment_id is not None: 

52 # default payment id converted to None 

53 assert MoneroTxWallet.DEFAULT_PAYMENT_ID != self.tx.payment_id 

54 if self.tx.note is not None: 

55 # empty notes converted to undefined 

56 assert len(self.tx.note) > 0 

57 

58 assert self.tx.unlock_time is not None 

59 assert self.tx.unlock_time >= 0 

60 assert self.tx.size is None # TODO monero-wallet-rpc: add tx_size to get_transfers and get_transfer_by_txid 

61 assert self.tx.received_timestamp is None # TODO monero-wallet-rpc: return received timestamp (asked to file issue if wanted) 

62 

63 def _test_send(self) -> None: 

64 """Test send tx fields.""" 

65 # test send tx 

66 if self.ctx.is_send_response is True: 

67 assert self.tx.weight is not None 

68 assert self.tx.weight > 0 

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

70 for tx_input in self.tx.inputs: 

71 assert tx_input.tx == self.tx 

72 else: 

73 assert self.tx.weight is None 

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

75 

76 def _test_pool_status(self) -> None: 

77 """Test confirmed and tx pool status fields.""" 

78 # test confirmed 

79 if self.tx.is_confirmed: 

80 assert self.tx.block is not None 

81 assert self.tx in self.tx.block.txs 

82 assert self.tx.block.height is not None 

83 assert self.tx.block.height > 0 

84 assert self.tx.block.timestamp is not None 

85 assert self.tx.block.timestamp > 0 

86 assert self.tx.relay is True 

87 assert self.tx.is_relayed is True 

88 assert self.tx.is_failed is False 

89 assert self.tx.in_tx_pool is False 

90 assert self.tx.is_double_spend_seen is False 

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 

95 assert self.tx.num_confirmations is not None 

96 assert self.tx.num_confirmations == 0 

97 

98 # test in tx pool 

99 if self.tx.in_tx_pool: 

100 assert self.tx.is_confirmed is False 

101 assert self.tx.relay is True 

102 assert self.tx.is_relayed is True 

103 assert self.tx.is_double_spend_seen is False 

104 assert self.tx.is_locked is True 

105 

106 # these should be initialized unless a response from sending 

107 # TODO re-enable when received timestamp returned in wallet rpc 

108 #if cself.self.tx.is_send_response: 

109 # assert self.tx.received_timestamp > 0 

110 else: 

111 assert self.tx.last_relayed_timestamp is None 

112 

113 def _test_status(self) -> None: 

114 """Test miner, failure, and relay status fields.""" 

115 # test miner tx 

116 if self.tx.is_miner_tx: 

117 assert self.tx.fee is not None 

118 assert self.tx.fee == 0 

119 

120 # test failed 

121 # TODO what else to test associated with failed 

122 if self.tx.is_failed: 

123 assert isinstance(self.tx.outgoing_transfer, MoneroTransfer) 

124 # TODO re-enable when received timestamp returned in wallet rpc 

125 #assert self.tx.received_timestamp > 0 

126 else: 

127 if self.tx.is_relayed: 

128 assert self.tx.is_double_spend_seen is False 

129 else: 

130 assert self.tx.relay is False 

131 assert self.tx.is_relayed is False 

132 assert self.tx.is_double_spend_seen is None 

133 

134 assert self.tx.last_failed_height is None 

135 assert self.tx.last_failed_hash is None 

136 

137 # received time only for tx pool or failed txs 

138 if self.tx.received_timestamp is not None: 

139 assert self.tx.in_tx_pool or self.tx.is_failed 

140 

141 # test relayed tx 

142 if self.tx.is_relayed: 

143 assert self.tx.relay is True 

144 if self.tx.relay is False: 

145 assert (not self.tx.is_relayed) is True 

146 

147 def _test_outgoing_transfer(self) -> None: 

148 """Test the tx's outgoing transfer, if any, against the test context.""" 

149 # test outgoing transfer per configuration 

150 if self.ctx.has_outgoing_transfer is False: 

151 assert self.tx.outgoing_transfer is None 

152 if self.ctx.has_destinations is True: 

153 assert self.tx.outgoing_transfer is not None 

154 assert len(self.tx.outgoing_transfer.destinations) > 0 

155 

156 # test outgoing transfer 

157 if self.tx.outgoing_transfer is not None: 

158 assert self.tx.is_outgoing is True 

159 TransferUtils.test_transfer(self.tx.outgoing_transfer, self.ctx) 

160 if self.ctx.is_sweep_response is True: 

161 assert len(self.tx.outgoing_transfer.destinations) == 1, f"Expected 1 tx, got {len(self.tx.outgoing_transfer.destinations)}" 

162 # TODO handle special cases 

163 else: 

164 assert len(self.tx.incoming_transfers) > 0 

165 assert self.tx.get_outgoing_amount() == 0 

166 assert self.tx.outgoing_transfer is None 

167 assert self.tx.ring_size is None 

168 assert self.tx.full_hex is None 

169 assert self.tx.metadata is None 

170 assert self.tx.key is None 

171 

172 def _test_incoming_transfers(self) -> None: 

173 """Test the tx's incoming transfers, if any.""" 

174 # test incoming transfers 

175 if len(self.tx.incoming_transfers) > 0: 

176 assert self.tx.is_incoming is True 

177 GenUtils.test_unsigned_big_integer(self.tx.get_incoming_amount()) 

178 assert self.tx.is_failed is False 

179 

180 # test each transfer and collect transfer sum 

181 transfer_sum: int = 0 

182 for transfer in self.tx.incoming_transfers: 

183 assert transfer.account_index is not None 

184 assert transfer.subaddress_index is not None 

185 TransferUtils.test_transfer(transfer, self.ctx) 

186 assert transfer.amount is not None 

187 transfer_sum += transfer.amount 

188 if self.ctx.wallet is not None: 

189 addr: str = self.ctx.wallet.get_address(transfer.account_index, transfer.subaddress_index) 

190 assert transfer.address == addr 

191 # TODO special case: transfer amount of 0 

192 

193 # incoming transfers add up to incoming tx amount 

194 assert self.tx.get_incoming_amount() == transfer_sum 

195 else: 

196 assert self.tx.outgoing_transfer is not None 

197 assert self.tx.get_incoming_amount() == 0 

198 assert len(self.tx.incoming_transfers) == 0 

199 

200 def _test_relay(self, config: MoneroTxConfig) -> None: 

201 """Test tx relay fields against the send configuration. 

202 

203 :param MoneroTxConfig config: send configuration the tx was created with. 

204 """ 

205 if config.relay is True: 

206 # test relayed txs 

207 assert self.tx.in_tx_pool is True 

208 assert self.tx.relay is True 

209 assert self.tx.is_relayed is True 

210 assert self.tx.last_relayed_timestamp is not None 

211 assert self.tx.last_relayed_timestamp > 0 

212 assert self.tx.is_double_spend_seen is False 

213 else: 

214 # test non-relayed txs 

215 assert self.tx.in_tx_pool is False 

216 assert self.tx.relay is False 

217 assert self.tx.is_relayed is False 

218 assert self.tx.last_relayed_timestamp is None 

219 assert self.tx.is_double_spend_seen is None 

220 

221 def _test_send_response(self) -> None: 

222 """Test tx fields specific to a send response.""" 

223 # test tx set 

224 assert self.tx.tx_set is not None 

225 found: bool = False 

226 for a_tx in self.tx.tx_set.txs: 

227 if a_tx == self.tx: 

228 found = True 

229 break 

230 

231 if self.ctx.is_copy is True: 

232 assert found is False 

233 else: 

234 assert found 

235 

236 # test common attributes 

237 assert self.ctx.config is not None 

238 config: MoneroTxConfig = self.ctx.config 

239 assert self.tx.is_confirmed is False 

240 TransferUtils.test_transfer(self.tx.outgoing_transfer, self.ctx) 

241 assert self.tx.ring_size == MoneroUtils.get_ring_size() 

242 assert self.tx.unlock_time == 0 

243 assert self.tx.block is None 

244 assert self.tx.key is not None 

245 assert len(self.tx.key) > 0 

246 assert self.tx.full_hex is not None 

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

248 assert self.tx.metadata is not None 

249 assert self.tx.received_timestamp is None 

250 assert self.tx.is_locked is True 

251 

252 # get locked state 

253 if self.tx.unlock_time == 0: 

254 assert self.tx.is_confirmed == (not self.tx.is_locked) 

255 else: 

256 assert self.tx.is_locked is True 

257 

258 # TODO implement is_locked 

259 #for output in self.tx.get_outputs_wallet(): 

260 # assert self.tx.is_locked == output.is_locked 

261 

262 # test destinations of sent tx 

263 assert self.tx.outgoing_transfer is not None 

264 if len(self.tx.outgoing_transfer.destinations) == 0: 

265 assert config.can_split is True 

266 # TODO: remove this after >18.3.1 when amounts_by_dest_list official 

267 logger.warning("Destinations not returned from split transactions") 

268 else: 

269 subtract_fee_from_dests: bool = len(config.subtract_fee_from) > 0 

270 if self.ctx.is_sweep_response is True: 

271 dests: list[MoneroDestination] = config.get_normalized_destinations() 

272 assert len(dests) == 1 

273 assert dests[0].amount is None 

274 if not subtract_fee_from_dests: 

275 assert self.tx.outgoing_transfer.amount == self.tx.outgoing_transfer.destinations[0].amount 

276 

277 self._test_relay(config) 

278 

279 def _test_inputs_and_outputs(self) -> None: 

280 """Test the tx's wallet inputs and outputs.""" 

281 # test inputs 

282 if self.tx.is_outgoing is True and self.ctx.is_send_response is True: 

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

284 

285 for self.wallet_input in self.tx.get_inputs_wallet(): 

286 OutputUtils.test_input_wallet(self.wallet_input) 

287 

288 # test outputs 

289 if self.tx.is_incoming is True and self.ctx.include_outputs is True: 

290 if self.tx.is_confirmed is True: 

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

292 else: 

293 assert len(self.tx.outputs) == 0 

294 

295 for output in self.tx.get_outputs_wallet(): 

296 OutputUtils.test_output_wallet(output) 

297 

298 def run(self) -> None: 

299 """Run test.""" 

300 self._test_common() 

301 self._test_send() 

302 self._test_pool_status() 

303 self._test_status() 

304 self._test_outgoing_transfer() 

305 self._test_incoming_transfers() 

306 

307 if self.ctx.is_send_response: 

308 self._test_send_response() 

309 else: 

310 # test tx result query 

311 # tx set only initialized on send responses 

312 assert self.tx.tx_set is None 

313 assert self.tx.ring_size is None 

314 assert self.tx.key is None 

315 assert self.tx.full_hex is None 

316 assert self.tx.metadata is None 

317 assert self.tx.last_relayed_timestamp is None 

318 

319 self._test_inputs_and_outputs()