Coverage for tests/utils/sync_with_pool_submit_tester.py: 65%

114 statements  

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

1import logging 

2 

3from monero import ( 

4 MoneroWallet, MoneroTxConfig, MoneroTxWallet, 

5 MoneroSubmitTxResult, MoneroDaemon 

6) 

7 

8from .assert_utils import AssertUtils 

9from .test_utils import TestUtils 

10 

11logger: logging.Logger = logging.getLogger("SyncWithPoolSubmitTester") 

12 

13 

14class SyncWithPoolSubmitTester: 

15 """Test wallet capability to sync transactions in the pool.""" 

16 

17 BALANCE_ERR: str = "Wallet balance should revert to original after flushing tx from pool without relaying" 

18 UNLOCKED_BALANCE_ERR: str = "Wallet unlocked balance should revert to original after flushing tx from pool without relaying" 

19 

20 daemon: MoneroDaemon 

21 """Daemon instance.""" 

22 wallet: MoneroWallet 

23 """Test wallet instance.""" 

24 config: MoneroTxConfig 

25 """Transaction test configuration.""" 

26 balance_before: int 

27 """Wallet balance before test.""" 

28 unlocked_balance_before: int 

29 """Wallet unlocked balance before test.""" 

30 txs_before: list[MoneroTxWallet] 

31 """Wallet transactions before running test.""" 

32 run_failing_code: bool 

33 """Run failing monero core code.""" 

34 

35 def __init__(self, daemon: MoneroDaemon, wallet: MoneroWallet, config: MoneroTxConfig, run_failing_core_code: bool = False) -> None: 

36 """ 

37 Initialize a new sync with poll submit tester. 

38 

39 :param MoneroDaemon daemon: daemon test instance. 

40 :param MoneroWallet wallet: wallet test instance. 

41 :param MoneroTxConfig config: transaction config. 

42 :param bool run_failing_core_code: run also failing monero-project core code. 

43 """ 

44 self.daemon = daemon 

45 self.wallet = wallet 

46 self.config = config 

47 self.balance_before = 0 

48 self.unlocked_balance_before = 0 

49 self.txs_before = [] 

50 self.run_failing_code = run_failing_core_code 

51 

52 def run_failing_core_code(self, config_no_relay: MoneroTxConfig) -> None: 

53 """ 

54 TODO monero-project this code fails which indicates issues. 

55 TODO monero-project sync txs from pool. 

56 

57 :param MoneroTxConfig config_no_relay: non-relay tx config. 

58 """ 

59 if not self.run_failing_code: 

60 return 

61 

62 # wallet balances should change 

63 assert self.balance_before != self.wallet.get_balance(), self.BALANCE_ERR 

64 # TODO: test exact amounts, maybe in ux test 

65 assert self.unlocked_balance_before != self.wallet.get_unlocked_balance(), self.UNLOCKED_BALANCE_ERR 

66 

67 # create tx using same config which is no longer double spend 

68 tx2: MoneroTxWallet = self.wallet.create_tx(config_no_relay) 

69 assert tx2.hash is not None 

70 assert tx2.full_hex is not None 

71 result2: MoneroSubmitTxResult = self.daemon.submit_tx_hex(tx2.full_hex) 

72 

73 # test result and flush on finally 

74 try: 

75 if result2.is_double_spend: 

76 raise Exception(f"Wallet created double spend transaction after syncing with the pool: {result2}") 

77 

78 assert result2.is_good 

79 self.wallet.sync() 

80 # wallet is aware of tx2 

81 fetched: MoneroTxWallet | None = self.wallet.get_tx(tx2.hash) 

82 assert fetched is not None and fetched.is_failed is False, "Submitted tx should not be null or failed" 

83 finally: 

84 self.daemon.flush_tx_pool(tx2.hash) 

85 

86 def setup(self) -> None: 

87 """Setup test.""" 

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

89 # TODO monero-project: update from pool does not prevent creating double spend tx 

90 TestUtils.WALLET_TX_TRACKER.wait_for_txs_to_clear_wallets([self.wallet]) 

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

92 assert self.config.account_index is not None 

93 TestUtils.WALLET_TX_TRACKER.wait_for_unlocked_balance(self.wallet, self.config.account_index, None, self.config.amount) 

94 

95 # rescan spent outputs for reliable before/after state 

96 self.wallet.rescan_spent() 

97 

98 # record wallet balances before submitting tx to pool 

99 self.balance_before = self.wallet.get_balance() 

100 self.unlocked_balance_before = self.wallet.get_unlocked_balance() 

101 self.txs_before = self.wallet.get_txs() 

102 logger.debug(f"balance {self.balance_before}, unlocked balance {self.unlocked_balance_before}") 

103 

104 def flush_tx(self, tx_hash: str) -> None: 

105 """Flush created test tx. 

106 

107 :param str tx_hash: hash of the transaction to flush. 

108 """ 

109 if self.config.relay is True: 

110 return 

111 

112 # flush the tx from the pool 

113 self.daemon.flush_tx_pool(tx_hash) 

114 logger.debug(f"flushed tx from pool: {tx_hash}") 

115 

116 # clear tx from wallet 

117 TestUtils.WALLET_TX_TRACKER.wait_for_txs_to_clear_wallets([self.wallet]) 

118 

119 # wallet should see failed state 

120 fetched: MoneroTxWallet | None = self.wallet.get_tx(tx_hash) 

121 if fetched is not None: 

122 assert fetched.is_failed, "Flushed tx should be failed" 

123 assert not fetched.in_tx_pool, "Flushed tx should not be in pool" 

124 assert not fetched.is_relayed, "Flushed tx should not be relayed" 

125 

126 # wallet txs are restored 

127 txs_after: list[MoneroTxWallet] = self.wallet.get_txs() 

128 for tx_before in self.txs_before: 

129 if tx_before.hash != tx_hash: 

130 continue 

131 found: bool = False 

132 for tx_after in txs_after: 

133 if tx_before.hash != tx_after.hash: 

134 continue 

135 # transfer fields which can update with confirmations 

136 # TODO: merge instead? 

137 tx_before.num_confirmations = tx_after.num_confirmations 

138 tx_before.is_locked = tx_after.is_locked 

139 

140 AssertUtils.assert_equals(tx_before, tx_after) 

141 found = True 

142 

143 assert found, f"Tx {tx_before.hash} not found after flushing tx from pool without relaying" 

144 

145 # wallet balance should be restored 

146 wallet_balance: int = self.wallet.get_balance() 

147 logger.debug(f"New balance {wallet_balance}") 

148 assert self.balance_before == wallet_balance, "Wallet balance should be same as original since tx was flushed and not relayed" 

149 

150 def test(self) -> None: 

151 """Run test.""" 

152 self.setup() 

153 # create tx but do not relay 

154 config_no_relay: MoneroTxConfig = self.config.copy() 

155 config_no_relay.relay = False 

156 config_no_relay_copy: MoneroTxConfig = config_no_relay.copy() 

157 tx: MoneroTxWallet = self.wallet.create_tx(config_no_relay) 

158 assert tx.hash is not None 

159 

160 # create tx using same config which is double spend 

161 tx_double_spend: MoneroTxWallet = self.wallet.create_tx(config_no_relay) 

162 assert tx_double_spend.hash is not None 

163 assert tx_double_spend.full_hex is not None 

164 

165 # test that config is unchanged 

166 assert config_no_relay_copy != config_no_relay 

167 AssertUtils.assert_equals(config_no_relay_copy, config_no_relay) 

168 

169 # submit tx directly to the pool but do not relay 

170 assert tx.full_hex is not None 

171 result: MoneroSubmitTxResult = self.daemon.submit_tx_hex(tx.full_hex, True) 

172 assert result.is_good, f"Transaction could not be submitted to the pool: {result}" 

173 

174 # sync wallet which checks pool 

175 self.wallet.sync() 

176 

177 # test result and flush on finally 

178 try: 

179 # test tx state 

180 fetched: MoneroTxWallet | None = self.wallet.get_tx(tx.hash) 

181 assert fetched is not None 

182 assert fetched.is_failed is False, "Submitted tx should not be failed" 

183 # TODO: relay flag is not correctly set, insufficient info comes from monero-wallet-rpc 

184 #assert fetched.is_relayed is False, "Submitted tx should be same relayed as configuration" 

185 assert fetched.in_tx_pool is True, "Submitted tx should be in the pool" 

186 

187 # submit double spend tx 

188 result_double_spend: MoneroSubmitTxResult = self.daemon.submit_tx_hex(tx_double_spend.full_hex, True) 

189 if result_double_spend.is_good is True: 

190 self.daemon.flush_tx_pool(tx_double_spend.hash) 

191 raise Exception("Tx submit result should have been double spend") 

192 

193 # relay if configured 

194 if self.config.relay is True: 

195 self.daemon.relay_tx_by_hash(tx.hash) 

196 

197 # sync wallet which updates from pool 

198 self.wallet.sync() 

199 

200 self.run_failing_core_code(config_no_relay) 

201 finally: 

202 self.flush_tx(tx.hash)