Coverage for tests/common/monero_utils/test_blocks.py: 100%

176 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 MoneroBlock, MoneroIncomingTransfer, MoneroOutputWallet, MoneroTxWallet, MoneroUtils 

7 

8from utils import BaseTestClass 

9 

10logger: logging.Logger = logging.getLogger("TestMoneroUtilsBlocks") 

11 

12 

13@pytest.mark.unit 

14class TestMoneroUtilsBlocks(BaseTestClass): 

15 """Monero utilities blocks unit tests.""" 

16 

17 def test_get_blocks_from_txs_dedup_and_order(self) -> None: 

18 block1: MoneroBlock = MoneroBlock() 

19 block1.height = 100 

20 block2: MoneroBlock = MoneroBlock() 

21 block2.height = 200 

22 

23 tx1: MoneroTxWallet = MoneroTxWallet() 

24 tx1.hash = "a" * 64 

25 tx1.block = block1 

26 

27 tx2: MoneroTxWallet = MoneroTxWallet() 

28 tx2.hash = "b" * 64 

29 tx2.block = block2 

30 

31 tx3: MoneroTxWallet = MoneroTxWallet() # shares block1 with tx1 

32 tx3.hash = "c" * 64 

33 tx3.block = block1 

34 

35 blocks: list[MoneroBlock] = MoneroUtils.get_blocks_from_txs([tx1, tx2, tx3]) 

36 assert len(blocks) == 2 # block1 deduplicated despite appearing twice 

37 assert blocks[0] is block1 # blocks are returned in first-seen order 

38 assert blocks[1] is block2 

39 

40 def test_get_blocks_from_txs_unconfirmed_placeholder(self) -> None: 

41 tx1: MoneroTxWallet = MoneroTxWallet() 

42 tx1.hash = "a" * 64 

43 tx2: MoneroTxWallet = MoneroTxWallet() 

44 tx2.hash = "b" * 64 

45 assert tx1.block is None and tx2.block is None 

46 

47 blocks: list[MoneroBlock] = MoneroUtils.get_blocks_from_txs([tx1, tx2]) 

48 

49 # unconfirmed (blockless) txs are grouped under one shared placeholder block 

50 assert len(blocks) == 1 

51 placeholder: MoneroBlock = blocks[0] 

52 assert placeholder.height is None 

53 assert len(placeholder.txs) == 2 

54 

55 # side effect: get_blocks_from_txs() mutates its inputs, attaching each 

56 # unconfirmed tx to the placeholder block it creates 

57 assert tx1.block is placeholder 

58 assert tx2.block is placeholder 

59 

60 def test_get_blocks_from_txs_mixed_confirmed_and_unconfirmed(self) -> None: 

61 block: MoneroBlock = MoneroBlock() 

62 block.height = 100 

63 confirmed: MoneroTxWallet = MoneroTxWallet() 

64 confirmed.hash = "a" * 64 

65 confirmed.block = block 

66 

67 unconfirmed1: MoneroTxWallet = MoneroTxWallet() 

68 unconfirmed1.hash = "b" * 64 

69 unconfirmed2: MoneroTxWallet = MoneroTxWallet() 

70 unconfirmed2.hash = "c" * 64 

71 

72 blocks: list[MoneroBlock] = MoneroUtils.get_blocks_from_txs([confirmed, unconfirmed1, unconfirmed2]) 

73 assert len(blocks) == 2 # the real block, plus one shared unconfirmed placeholder 

74 assert blocks[0] is block 

75 assert blocks[1].height is None 

76 assert unconfirmed1.block is unconfirmed2.block is blocks[1] 

77 

78 def test_get_blocks_from_txs_empty(self) -> None: 

79 assert MoneroUtils.get_blocks_from_txs([]) == [] 

80 

81 def test_get_blocks_from_transfers_dedup_and_order(self) -> None: 

82 block1: MoneroBlock = MoneroBlock() 

83 block1.height = 100 

84 block2: MoneroBlock = MoneroBlock() 

85 block2.height = 200 

86 

87 t1: MoneroIncomingTransfer = MoneroIncomingTransfer() 

88 t1.tx = MoneroTxWallet() 

89 t1.tx.hash = "a" * 64 

90 t1.tx.block = block1 

91 

92 t2: MoneroIncomingTransfer = MoneroIncomingTransfer() 

93 t2.tx = MoneroTxWallet() 

94 t2.tx.hash = "b" * 64 

95 t2.tx.block = block2 

96 

97 t3: MoneroIncomingTransfer = MoneroIncomingTransfer() # tx shares block1 with t1 

98 t3.tx = MoneroTxWallet() 

99 t3.tx.hash = "c" * 64 

100 t3.tx.block = block1 

101 

102 blocks: list[MoneroBlock] = MoneroUtils.get_blocks_from_transfers([t1, t2, t3]) 

103 assert len(blocks) == 2 

104 assert blocks[0] is block1 

105 assert blocks[1] is block2 

106 

107 def test_get_blocks_from_transfers_unconfirmed_placeholder(self) -> None: 

108 t1: MoneroIncomingTransfer = MoneroIncomingTransfer() 

109 t1.tx = MoneroTxWallet() 

110 t1.tx.hash = "a" * 64 

111 t2: MoneroIncomingTransfer = MoneroIncomingTransfer() 

112 t2.tx = MoneroTxWallet() 

113 t2.tx.hash = "b" * 64 

114 assert t1.tx.block is None and t2.tx.block is None 

115 

116 blocks: list[MoneroBlock] = MoneroUtils.get_blocks_from_transfers([t1, t2]) 

117 assert len(blocks) == 1 

118 placeholder: MoneroBlock = blocks[0] 

119 assert placeholder.height is None 

120 

121 # side effect: mutates transfer.tx.block, same as get_blocks_from_txs() 

122 assert t1.tx.block is placeholder 

123 assert t2.tx.block is placeholder 

124 

125 def test_get_blocks_from_transfers_mixed_confirmed_and_unconfirmed(self) -> None: 

126 block: MoneroBlock = MoneroBlock() 

127 block.height = 100 

128 confirmed: MoneroIncomingTransfer = MoneroIncomingTransfer() 

129 confirmed.tx = MoneroTxWallet() 

130 confirmed.tx.hash = "a" * 64 

131 confirmed.tx.block = block 

132 

133 unconfirmed1: MoneroIncomingTransfer = MoneroIncomingTransfer() 

134 unconfirmed1.tx = MoneroTxWallet() 

135 unconfirmed1.tx.hash = "b" * 64 

136 unconfirmed2: MoneroIncomingTransfer = MoneroIncomingTransfer() 

137 unconfirmed2.tx = MoneroTxWallet() 

138 unconfirmed2.tx.hash = "c" * 64 

139 

140 blocks: list[MoneroBlock] = MoneroUtils.get_blocks_from_transfers([confirmed, unconfirmed1, unconfirmed2]) 

141 assert len(blocks) == 2 # the real block, plus one shared unconfirmed placeholder 

142 assert blocks[0] is block 

143 assert blocks[1].height is None 

144 assert unconfirmed1.tx.block is unconfirmed2.tx.block is blocks[1] 

145 

146 def test_get_blocks_from_transfers_missing_tx_raises(self) -> None: 

147 # a transfer with no tx set is a legitimate, reachable state (it's just 

148 # never assigned); get_blocks_from_transfers() must raise a catchable 

149 # exception, not dereference transfer.tx and segfault 

150 transfer: MoneroIncomingTransfer = MoneroIncomingTransfer() 

151 transfer.amount = 500000 

152 with pytest.raises(RuntimeError) as exc_info: 

153 MoneroUtils.get_blocks_from_transfers([transfer]) 

154 assert str(exc_info.value) == "Transfer has no tx" 

155 

156 def test_get_blocks_from_transfers_missing_tx_raises_after_partial_mutation(self) -> None: 

157 # get_blocks_from_transfers() is not transactional: a transfer earlier 

158 # in the list is already mutated (attached to a placeholder block) 

159 # before a later, invalid transfer aborts the whole call 

160 good: MoneroIncomingTransfer = MoneroIncomingTransfer() 

161 good.tx = MoneroTxWallet() 

162 good.tx.hash = "a" * 64 

163 assert good.tx.block is None 

164 

165 bad: MoneroIncomingTransfer = MoneroIncomingTransfer() # no tx 

166 with pytest.raises(RuntimeError): 

167 MoneroUtils.get_blocks_from_transfers([good, bad]) 

168 

169 assert good.tx.block is not None 

170 assert good.tx.block.height is None 

171 

172 def test_get_blocks_from_transfers_empty(self) -> None: 

173 assert MoneroUtils.get_blocks_from_transfers([]) == [] 

174 

175 def test_get_blocks_from_outputs_dedup_and_order(self) -> None: 

176 block1: MoneroBlock = MoneroBlock() 

177 block1.height = 100 

178 block2: MoneroBlock = MoneroBlock() 

179 block2.height = 200 

180 

181 tx1: MoneroTxWallet = MoneroTxWallet() 

182 tx1.hash = "a" * 64 

183 tx1.block = block1 

184 tx2: MoneroTxWallet = MoneroTxWallet() 

185 tx2.hash = "b" * 64 

186 tx2.block = block2 

187 

188 o1: MoneroOutputWallet = MoneroOutputWallet() 

189 o1.tx = tx1 

190 o2: MoneroOutputWallet = MoneroOutputWallet() 

191 o2.tx = tx2 

192 o3: MoneroOutputWallet = MoneroOutputWallet() # tx shares block1 with o1 

193 o3.tx = tx1 

194 

195 blocks: list[MoneroBlock] = MoneroUtils.get_blocks_from_outputs([o1, o2, o3]) 

196 assert len(blocks) == 2 

197 assert blocks[0] is block1 

198 assert blocks[1] is block2 

199 

200 def test_get_blocks_from_outputs_unconfirmed_raises(self) -> None: 

201 # unlike get_blocks_from_txs()/get_blocks_from_transfers(), an 

202 # unconfirmed (blockless) output's tx does not get a placeholder 

203 # block -- it raises instead 

204 output: MoneroOutputWallet = MoneroOutputWallet() 

205 output.tx = MoneroTxWallet() 

206 output.tx.hash = "a" * 64 

207 assert output.tx.block is None 

208 

209 with pytest.raises(RuntimeError, match="Need to handle unconfirmed output"): 

210 MoneroUtils.get_blocks_from_outputs([output]) 

211 

212 def test_get_blocks_from_outputs_unconfirmed_raises_after_earlier_match(self) -> None: 

213 # unlike get_blocks_from_transfers(), get_blocks_from_outputs() never mutates its inputs 

214 block: MoneroBlock = MoneroBlock() 

215 block.height = 100 

216 confirmed_tx: MoneroTxWallet = MoneroTxWallet() 

217 confirmed_tx.hash = "a" * 64 

218 confirmed_tx.block = block 

219 good: MoneroOutputWallet = MoneroOutputWallet() 

220 good.tx = confirmed_tx 

221 

222 bad: MoneroOutputWallet = MoneroOutputWallet() 

223 bad.tx = MoneroTxWallet() 

224 bad.tx.hash = "b" * 64 # unconfirmed 

225 

226 with pytest.raises(RuntimeError, match="Need to handle unconfirmed output"): 

227 MoneroUtils.get_blocks_from_outputs([good, bad]) 

228 

229 def test_get_blocks_from_outputs_missing_tx_raises(self) -> None: 

230 # same as get_blocks_from_transfers(): output.tx is a legitimate but 

231 # unchecked null; must raise rather than dereference and segfault 

232 output: MoneroOutputWallet = MoneroOutputWallet() 

233 output.amount = 1000000 

234 with pytest.raises(RuntimeError) as exc_info: 

235 MoneroUtils.get_blocks_from_outputs([output]) 

236 assert str(exc_info.value) == "Output has no tx" 

237 

238 def test_get_blocks_from_outputs_empty(self) -> None: 

239 assert MoneroUtils.get_blocks_from_outputs([]) == []