Coverage for tests/utils/send_and_update_txs_tester.py: 80%

111 statements  

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

1import logging 

2 

3from time import sleep 

4from monero import ( 

5 MoneroWallet, MoneroTxConfig, MoneroTxWallet, 

6 MoneroDaemon, MoneroBlockHeader, MoneroTxQuery 

7) 

8 

9from .test_utils import TestUtils 

10from .tx_wallet_utils import TxWalletUtils 

11from .wallet_txs_utils import WalletTxsUtils 

12from .mining_utils import MiningUtils 

13from .context import TxContext 

14 

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

16 

17 

18class SendAndUpdateTxsTester: 

19 """Tests sending a tx with an unlock time then tracking and updating it as 

20 blocks are added to the chain. 

21 

22 TODO: test wallet accounting throughout this; dedicated method? probably. 

23 

24 Allows sending to and from the same account which is an edge case where 

25 incoming txs are occluded by their outgoing counterpart (issue #4500) 

26 and also where it is impossible to discern which incoming output is 

27 the tx amount and which is the change amount without wallet metadata. 

28 """ 

29 

30 daemon: MoneroDaemon 

31 """Daemon test instance.""" 

32 wallet: MoneroWallet 

33 """The test wallet to send from.""" 

34 config: MoneroTxConfig 

35 """The send configuration to send and test.""" 

36 num_confirmations: int 

37 """Number of blockchain confirmations.""" 

38 

39 def __init__(self, daemon: MoneroDaemon, wallet: MoneroWallet, config: MoneroTxConfig) -> None: 

40 """Initialize a new send and update txs tester. 

41 

42 :param MoneroDaemon daemon: daemon test instance. 

43 :param MoneroWallet wallet: wallet test instance. 

44 :param MoneroWalletConfig config: txs send configuration to test. 

45 """ 

46 self.daemon = daemon 

47 self.wallet = wallet 

48 self.config = config 

49 self.num_confirmations = 0 

50 

51 def setup(self) -> None: 

52 """Setup test wallet.""" 

53 # wait for txs to confirm and for sufficient unlocked balance 

54 TestUtils.WALLET_TX_TRACKER.wait_for_txs_to_clear_pool(self.wallet) 

55 assert len(self.config.subaddress_indices) == 0 

56 assert self.config.account_index is not None 

57 fee: int = TxWalletUtils.MAX_FEE * 2 

58 TestUtils.WALLET_TX_TRACKER.wait_for_unlocked_balance(self.wallet, self.config.account_index, None, fee) 

59 

60 def test_unlock_tx(self, tx: MoneroTxWallet, is_send_response: bool) -> None: 

61 """Test tx unlock. 

62 

63 :param MoneroTxWallet tx: transaction to test. 

64 :param bool is_send_response: indicates if tx originated from send response. 

65 """ 

66 ctx: TxContext = TxContext() 

67 ctx.wallet = self.wallet 

68 ctx.config = self.config 

69 ctx.is_send_response = is_send_response 

70 try: 

71 TxWalletUtils.test_tx_wallet(tx, ctx) 

72 except Exception as e: 

73 logger.warning(e) 

74 raise 

75 

76 def test_out_in_pair(self, tx_out: MoneroTxWallet, tx_in: MoneroTxWallet) -> None: 

77 """Test transaction out / in pair. 

78 

79 :param MoneroTxWallet tx_out: outgoing transaction. 

80 :param MoneroTxWallet tx_in: incoming transaction. 

81 """ 

82 assert tx_out.is_confirmed == tx_in.is_confirmed 

83 assert tx_in.get_incoming_amount() == tx_out.get_outgoing_amount() 

84 

85 def test_out_in_pairs(self, txs: list[MoneroTxWallet], is_send_response: bool) -> None: 

86 """Test transactions out / in pairs. 

87 

88 :param list[MoneroTxWallet] txs: transactions to test out / in pairs. 

89 :param bool is_send_response: indicates if tx originated from send response. 

90 """ 

91 # for each out tx 

92 for tx in txs: 

93 self.test_unlock_tx(tx, is_send_response) 

94 if tx.outgoing_transfer is not None: 

95 continue 

96 

97 tx_out: MoneroTxWallet = tx 

98 

99 # find incoming counterpart 

100 tx_in: MoneroTxWallet | None = None 

101 for tx2 in txs: 

102 if tx.is_incoming and tx.hash == tx2.hash: 

103 tx_in = tx2 

104 break 

105 

106 # test out / in pair 

107 # TODO monero-wallet-rpc: incoming txs occluded by their outgoing counterpart #4500 

108 if tx_in is None: 

109 logger.warning(f"outgoing tx {tx.hash} missing incoming counterpart (issue #4500)") 

110 else: 

111 self.test_out_in_pair(tx_out, tx_in) 

112 

113 @classmethod 

114 def merge_fetched_into_updated_txs( 

115 cls, 

116 sent_txs: list[MoneroTxWallet], 

117 fetched_txs: list[MoneroTxWallet] 

118 ) -> list[MoneroTxWallet] | None: 

119 """Merge fetched txs into updated txs and original sent txs. 

120 

121 :param list[MoneroTxWallet] sent_txs: original sent txs. 

122 :param list[MoneroTxWallet] fetched_txs: fetched txs to merge into updated txs. 

123 :returns list[MoneroTxWallet] | None: updated txs to merged with fetched txs. 

124 """ 

125 updated_txs: list[MoneroTxWallet] | None = None 

126 

127 for fetched_tx in fetched_txs: 

128 # merge with updated txs 

129 if updated_txs is None: 

130 updated_txs = fetched_txs 

131 else: 

132 for updated_tx in updated_txs: 

133 if fetched_tx.hash != updated_tx.hash or fetched_tx.is_outgoing != updated_tx.is_outgoing: 

134 continue 

135 updated_tx.merge(fetched_tx.copy()) 

136 if updated_tx.block is None and fetched_tx.block is not None: 

137 # copy block for testing 

138 updated_tx.block = fetched_tx.block.copy() 

139 updated_tx.block.txs = [updated_tx] 

140 

141 # merge with original sent txs 

142 for sent_tx in sent_txs: 

143 if fetched_tx.hash != sent_tx.hash or fetched_tx.is_outgoing != sent_tx.is_outgoing: 

144 continue 

145 # TODO: it's mergeable but tests don't account for extra info 

146 # from send (e.g. hex) so not tested; could specify in test config 

147 sent_tx.merge(fetched_tx.copy()) 

148 

149 return updated_txs 

150 

151 def wait_for_confirmations(self, sent_txs: list[MoneroTxWallet], num_confirmations_total: int) -> None: 

152 """Wait for txs to confirm. 

153 

154 :param list[MoneroTxWallet] sent_txs: list of sent transactions. 

155 :param int num_confirmations_total: number of confirmed txs required. 

156 """ 

157 # track resulting outgoing and incoming txs as blocks are added to the chain 

158 logger.debug(f"{self.num_confirmations} < {num_confirmations_total} needed confirmations") 

159 header: MoneroBlockHeader = self.daemon.wait_for_next_block_header() 

160 logger.debug(f"*** Block {header.height} added to chain ***") 

161 

162 # give wallet time to catch up, otherwise incoming tx may not appear 

163 # TODO: this lets new block slip, okay? 

164 sleep(TestUtils.SYNC_PERIOD_IN_MS / 1000) 

165 

166 # get incoming/outgoing txs with sent hashes 

167 tx_hashes: list[str] = [] 

168 for sent_tx in sent_txs: 

169 assert sent_tx.hash is not None 

170 tx_hashes.append(sent_tx.hash) 

171 

172 query: MoneroTxQuery = MoneroTxQuery() 

173 query.hashes = tx_hashes 

174 fetched_txs: list[MoneroTxWallet] = WalletTxsUtils.get_and_test_txs(self.wallet, query, None, True, TestUtils.REGTEST) 

175 assert len(fetched_txs) > 0 

176 

177 # test fetched txs 

178 self.test_out_in_pairs(fetched_txs, False) 

179 

180 # merge fetched txs into updated txs and original sent txs 

181 updated_txs: list[MoneroTxWallet] | None = self.merge_fetched_into_updated_txs(sent_txs, fetched_txs) 

182 

183 # test updated txs 

184 assert updated_txs is not None 

185 self.test_out_in_pairs(updated_txs, False) 

186 

187 # update confirmations in order to exit loop 

188 fetched_tx: MoneroTxWallet = fetched_txs[0] 

189 assert fetched_tx.num_confirmations is not None 

190 self.num_confirmations = fetched_tx.num_confirmations 

191 

192 def test(self) -> None: 

193 """Run send and update txs test.""" 

194 self.setup() 

195 # this test starts and stops mining, so it's wrapped in order to stop mining if anything fails 

196 try: 

197 # send transactions 

198 sent_txs: list[MoneroTxWallet] = self.wallet.create_txs(self.config) 

199 

200 # build test context 

201 ctx: TxContext = TxContext() 

202 ctx.wallet = self.wallet 

203 ctx.config = self.config 

204 ctx.is_send_response = True 

205 

206 # test sent transactions 

207 for tx in sent_txs: 

208 TxWalletUtils.test_tx_wallet(tx, ctx) 

209 assert tx.is_confirmed is False 

210 assert tx.in_tx_pool is True 

211 

212 # attempt to start mining to push the network along 

213 MiningUtils.try_start_mining() 

214 

215 # number of confirmations to test 

216 num_confirmations_total: int = 2 

217 # loop to update txs through confirmations 

218 

219 while self.num_confirmations < num_confirmations_total: 

220 self.wait_for_confirmations(sent_txs, num_confirmations_total) 

221 

222 finally: 

223 # stop mining at end of test 

224 MiningUtils.try_stop_mining()