Coverage for tests/daemon/rpc/test_txs.py: 100%

123 statements  

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

1from __future__ import annotations 

2 

3import logging 

4 

5import pytest 

6from monero import ( 

7 MoneroDaemonRpc, 

8 MoneroDestination, 

9 MoneroSubmitTxResult, 

10 MoneroTx, 

11 MoneroTxConfig, 

12 MoneroWalletRpc, 

13) 

14 

15from utils import DaemonUtils, TestContext, TestUtils as Utils, TxUtils, TxWalletUtils, WalletTxsUtils 

16from .base_test_monero_daemon_rpc import BaseTestMoneroDaemonRpc 

17 

18logger: logging.Logger = logging.getLogger("TestMoneroDaemonRpcTxs") 

19 

20 

21@pytest.mark.integration 

22class TestMoneroDaemonRpcTxs(BaseTestMoneroDaemonRpc): 

23 """Rpc daemon txs integration tests.""" 

24 

25 # Can get a transaction by hash and without pruning 

26 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") 

27 def test_get_tx_by_hash(self, daemon: MoneroDaemonRpc) -> None: 

28 # fetch tx hashes to test 

29 tx_hashes: list[str] = DaemonUtils.get_confirmed_tx_hashes(daemon) 

30 

31 # context for creating txs 

32 ctx: TestContext = TestContext() 

33 ctx.is_pruned = False 

34 ctx.is_confirmed = True 

35 ctx.from_get_tx_pool = False 

36 

37 # fetch each tx by hash without pruning 

38 for tx_hash in tx_hashes: 

39 tx: MoneroTx | None = daemon.get_tx(tx_hash) 

40 TxUtils.test_tx(tx, ctx) 

41 

42 # fetch each tx by hash with pruning 

43 for tx_hash in tx_hashes: 

44 tx: MoneroTx | None = daemon.get_tx(tx_hash, True) 

45 ctx.is_pruned = True 

46 TxUtils.test_tx(tx, ctx) 

47 

48 # fetch invalid hash 

49 with pytest.raises(Exception) as exc_info: 

50 daemon.get_tx("invalid tx hash") 

51 assert str(exc_info.value) == "Invalid transaction hash" 

52 

53 # Can get transactions by hashes with and without pruning 

54 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") 

55 @pytest.mark.flaky(reruns=5, reruns_delay=5, only_rerun=[]) 

56 def test_get_txs_by_hashes(self, daemon: MoneroDaemonRpc, wallet: MoneroWalletRpc) -> None: 

57 # fetch tx hashes to test 

58 tx_hashes: list[str] = DaemonUtils.get_confirmed_tx_hashes(daemon) 

59 assert len(tx_hashes) > 0, "No tx hashes found" 

60 

61 # context for creating txs 

62 ctx: TestContext = TestContext() 

63 ctx.is_pruned = False 

64 ctx.is_confirmed = True 

65 ctx.from_get_tx_pool = False 

66 

67 # fetch each tx by hash without pruning 

68 txs: list[MoneroTx] = daemon.get_txs(tx_hashes) 

69 assert len(txs) == len(tx_hashes), f"Expected len(txs) == len(tx_hashes), got {len(txs)} == {len(tx_hashes)}" 

70 for tx in txs: 

71 TxUtils.test_tx(tx, ctx) 

72 

73 # fetch each tx by hash with pruning 

74 txs = daemon.get_txs(tx_hashes, True) 

75 ctx.is_pruned = True 

76 assert len(txs) == len(tx_hashes), f"Expected len(txs) == len(tx_hashes), got {len(txs)} == {len(tx_hashes)}" 

77 for tx in txs: 

78 TxUtils.test_tx(tx, ctx) 

79 

80 # fetch missing hash 

81 dest: MoneroDestination = MoneroDestination() 

82 dest.address = wallet.get_primary_address() 

83 dest.amount = TxWalletUtils.MAX_FEE 

84 config: MoneroTxConfig = MoneroTxConfig() 

85 config.account_index = 0 

86 config.destinations.append(dest) 

87 tx = wallet.create_tx(config) 

88 assert tx.hash is not None 

89 daemon_tx: MoneroTx | None = daemon.get_tx(tx.hash) 

90 assert daemon_tx is None 

91 tx_hashes.append(tx.hash) 

92 num_txs: int = len(txs) 

93 txs = daemon.get_txs(tx_hashes) 

94 assert num_txs == len(txs) 

95 

96 # fetch invalid hash 

97 with pytest.raises(Exception) as exc_info: 

98 daemon.get_txs(["invalid tx hash"]) 

99 assert str(exc_info.value) == "Invalid transaction hash" 

100 

101 # Can get transactions by hashes that are in the transaction pool 

102 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") 

103 def test_get_txs_by_hashes_in_pool(self, daemon: MoneroDaemonRpc, wallet: MoneroWalletRpc) -> None: 

104 # wait for wallet's txs in the pool to clear to ensure reliable sync 

105 Utils.WALLET_TX_TRACKER.wait_for_txs_to_clear_pool(wallet) 

106 

107 # submit txs to the pool but don't relay 

108 tx_hashes: list[str] = [] 

109 for i in range(1, 3): 

110 tx: MoneroTx = WalletTxsUtils.get_unrelayed_tx(wallet, i) 

111 assert tx.hash is not None 

112 assert tx.full_hex is not None 

113 assert len(tx.full_hex) > 0 

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

115 DaemonUtils.test_submit_tx_result_good(result) 

116 assert result.is_relayed is False 

117 tx_hashes.append(tx.hash) 

118 

119 # fetch txs by hash 

120 logger.debug("Fetching txs...") 

121 txs: list[MoneroTx] = daemon.get_txs(tx_hashes) 

122 num_txs: int = len(txs) 

123 logger.debug(f"Fetched {num_txs} tx(s)") 

124 

125 # context for testing tx 

126 ctx: TestContext = TestContext() 

127 ctx.is_pruned = False 

128 ctx.is_confirmed = False 

129 ctx.from_get_tx_pool = False 

130 

131 # test fetched txs 

132 assert len(tx_hashes) == num_txs 

133 for tx in txs: 

134 TxUtils.test_tx(tx, ctx) 

135 

136 # clear txs from pool 

137 daemon.flush_tx_pool(tx_hashes) 

138 wallet.sync() 

139 

140 # Can get a transaction hex by hash with and without pruning 

141 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") 

142 def test_get_tx_hex_by_hash(self, daemon: MoneroDaemonRpc) -> None: 

143 # fetch transaction hashes to test 

144 tx_hashes: list[str] = DaemonUtils.get_confirmed_tx_hashes(daemon) 

145 

146 # fetch each tx hex by hash with and without pruning 

147 hexes: list[str | None] = [] 

148 hexes_pruned: list[str | None] = [] 

149 for tx_hash in tx_hashes: 

150 hexes.append(daemon.get_tx_hex(tx_hash)) 

151 hexes_pruned.append(daemon.get_tx_hex(tx_hash, True)) 

152 

153 # test results 

154 assert len(hexes) == len(tx_hashes) 

155 assert len(hexes_pruned) == len(tx_hashes) 

156 

157 for i, tx_hex in enumerate(hexes): 

158 pruned_tx_hex: str | None = hexes_pruned[i] 

159 assert tx_hex is not None 

160 assert pruned_tx_hex is not None 

161 assert len(pruned_tx_hex) > 0 

162 # pruned hex is shorter 

163 assert len(tx_hex) >= len(pruned_tx_hex) 

164 

165 # fetch invalid hash 

166 with pytest.raises(Exception) as exc_info: 

167 daemon.get_tx_hex("invalid tx hash") 

168 assert str(exc_info.value) == "Invalid transaction hash" 

169 

170 # Can get transaction hexes by hashes with and without pruning 

171 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") 

172 def test_get_tx_hexes_by_hashes(self, daemon: MoneroDaemonRpc) -> None: 

173 # fetch transaction hashes to test 

174 tx_hashes: list[str] = DaemonUtils.get_confirmed_tx_hashes(daemon) 

175 

176 # fetch tx hexes by hash with and without pruning 

177 hexes: list[str] = daemon.get_tx_hexes(tx_hashes) 

178 hexes_pruned: list[str] = daemon.get_tx_hexes(tx_hashes, True) 

179 

180 # test results 

181 assert len(hexes) == len(tx_hashes) 

182 assert len(hexes_pruned) == len(tx_hashes) 

183 for i, tx_hex in enumerate(hexes): 

184 pruned_tx_hex: str = hexes_pruned[i] 

185 assert tx_hex is not None 

186 assert pruned_tx_hex is not None 

187 assert len(pruned_tx_hex) > 0 

188 # pruned hex is shorter 

189 assert len(tx_hex) >= len(pruned_tx_hex) 

190 

191 # fetch invalid hash 

192 tx_hashes.append("invalid tx hash") 

193 with pytest.raises(Exception) as exc_info: 

194 daemon.get_tx_hexes(tx_hashes) 

195 assert str(exc_info.value) == "Invalid transaction hash"