Coverage for tests/daemon/rpc/test_blocks.py: 99%

216 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 MoneroAltChain, 

8 MoneroBlock, 

9 MoneroBlockHeader, 

10 MoneroBlockTemplate, 

11 MoneroDaemonRpc, 

12 MoneroGetBlockHashesResult, 

13 MoneroGetBlocksByHashResult, 

14 MoneroMinerTxSum, 

15) 

16 

17from utils import AssertUtils, BlockUtils, DaemonUtils, MiningUtils, TestContext, TestUtils as Utils 

18from .base_test_monero_daemon_rpc import BaseTestMoneroDaemonRpc 

19 

20logger: logging.Logger = logging.getLogger("TestMoneroDaemonRpcBlocks") 

21 

22 

23@pytest.mark.integration 

24class TestMoneroDaemonRpcBlocks(BaseTestMoneroDaemonRpc): 

25 """Rpc daemon blocks integration tests.""" 

26 

27 # Can get the blockchain height 

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

29 def test_get_height(self, daemon: MoneroDaemonRpc) -> None: 

30 height: int = daemon.get_height() 

31 logger.debug(f"Daemon height: {height}") 

32 assert height > 0, "Height must be greater than 0" 

33 

34 # Can get a block hash by height 

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

36 def test_get_block_id_by_height(self, daemon: MoneroDaemonRpc) -> None: 

37 last_header: MoneroBlockHeader = daemon.get_last_block_header() 

38 logger.debug(f"Last block header height: {last_header.height}") 

39 assert last_header.height is not None 

40 hash_str: str = daemon.get_block_hash(last_header.height) 

41 logger.debug(f"Got block hash: {hash_str}") 

42 assert hash_str is not None 

43 assert 64 == len(hash_str), f"Invalid block hash '{hash_str}'" 

44 

45 # Can get a block template 

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

47 def test_get_block_template(self, daemon: MoneroDaemonRpc) -> None: 

48 template: MoneroBlockTemplate = daemon.get_block_template(Utils.ADDRESS, 2) 

49 BlockUtils.test_block_template(template) 

50 

51 # Can get the last block's header 

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

53 def test_get_last_block_header(self, daemon: MoneroDaemonRpc) -> None: 

54 last_header: MoneroBlockHeader = daemon.get_last_block_header() 

55 BlockUtils.test_block_header(last_header, True) 

56 

57 # Can get a block header by hash 

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

59 def test_get_block_header_by_hash(self, daemon: MoneroDaemonRpc) -> None: 

60 # retrieve last block by hash 

61 last_header: MoneroBlockHeader = daemon.get_last_block_header() 

62 assert last_header.height is not None 

63 hash_str: str = daemon.get_block_hash(last_header.height) 

64 header: MoneroBlockHeader = daemon.get_block_header_by_hash(hash_str) 

65 BlockUtils.test_block_header(header, True) 

66 AssertUtils.assert_equals(last_header, header) 

67 

68 # retrieve by hash of previous to last block 

69 hash_str = daemon.get_block_hash(last_header.height - 1) 

70 header = daemon.get_block_header_by_hash(hash_str) 

71 BlockUtils.test_block_header(header, True) 

72 assert last_header.height - 1 == header.height 

73 

74 # Can get a block header by height 

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

76 def test_get_block_header_by_height(self, daemon: MoneroDaemonRpc) -> None: 

77 # retrieve by height of last block 

78 last_header: MoneroBlockHeader = daemon.get_last_block_header() 

79 assert last_header.height is not None 

80 header: MoneroBlockHeader = daemon.get_block_header_by_height(last_header.height) 

81 BlockUtils.test_block_header(header, True) 

82 AssertUtils.assert_equals(last_header, header) 

83 

84 # retrieve by height of previous to last block 

85 header = daemon.get_block_header_by_height(last_header.height - 1) 

86 BlockUtils.test_block_header(header, True) 

87 assert last_header.height - 1 == header.height 

88 

89 # Can get block headers by range 

90 # TODO: test start with no end, vice versa, inclusivity 

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

92 def test_get_block_headers_by_range(self, daemon: MoneroDaemonRpc) -> None: 

93 # determine start and end height based on number of blocks and how many blocks ago 

94 num_blocks: int = 100 

95 num_blocks_ago: int = 100 

96 current_height: int = daemon.get_height() 

97 start_height: int = current_height - num_blocks_ago 

98 end_height: int = current_height - (num_blocks_ago - num_blocks) - 1 

99 

100 # fetch headers 

101 headers: list[MoneroBlockHeader] = daemon.get_block_headers_by_range(start_height, end_height) 

102 

103 # test headers 

104 assert num_blocks == len(headers) 

105 

106 for i in range(num_blocks): 

107 header: MoneroBlockHeader = headers[i] 

108 assert start_height + i == header.height 

109 BlockUtils.test_block_header(header, True) 

110 

111 # Can get a block by hash 

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

113 def test_get_block_by_hash(self, daemon: MoneroDaemonRpc) -> None: 

114 # test config 

115 ctx: TestContext = TestContext() 

116 ctx.has_hex = True 

117 ctx.has_txs = False 

118 ctx.header_is_full = True 

119 

120 # retrieve by hash of last block 

121 last_header: MoneroBlockHeader = daemon.get_last_block_header() 

122 assert last_header.height is not None 

123 hash_str: str = daemon.get_block_hash(last_header.height) 

124 block: MoneroBlock = daemon.get_block_by_hash(hash_str) 

125 assert block.height is not None 

126 BlockUtils.test_block(block, ctx) 

127 AssertUtils.assert_equals(daemon.get_block_by_height(block.height), block) 

128 assert len(block.txs) == 0, f"No block tx expected, found: {len(block.txs)}" 

129 

130 # retrieve by hash of previous to last block 

131 hash_str = daemon.get_block_hash(last_header.height - 1) 

132 block = daemon.get_block_by_hash(hash_str) 

133 BlockUtils.test_block(block, ctx) 

134 AssertUtils.assert_equals(daemon.get_block_by_height(last_header.height - 1), block) 

135 assert len(block.txs) == 0, f"No block tx expected, found: {len(block.txs)}" 

136 

137 # Can get blocks by hash which includes transactions (binary) 

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

139 def test_get_blocks_by_hash_binary(self, daemon: MoneroDaemonRpc) -> None: 

140 chain_height: int = daemon.get_height() 

141 genesis_hash: str = daemon.get_block_hash(0) 

142 

143 # a non-zero start height resumes from there and ignores the hash list 

144 resume_height: int = chain_height - 5 

145 result: MoneroGetBlocksByHashResult = daemon.get_blocks_by_hash([genesis_hash], resume_height, False, 0) 

146 assert result.current_height is not None and result.current_height >= chain_height 

147 assert len(result.blocks) > 0 

148 for i, block in enumerate(result.blocks): 

149 assert block.height == resume_height + i 

150 BlockUtils.test_block(block, self.BINARY_BLOCK_CTX) 

151 

152 # a genesis-terminated history resumes from height 0, capped by max_block_count 

153 result = daemon.get_blocks_by_hash([genesis_hash], 0, False, 10) 

154 assert 0 < len(result.blocks) <= 10 

155 assert result.blocks[0].height == 0 

156 

157 # noop when the first hash is already the tip 

158 tip: MoneroBlockHeader = daemon.get_last_block_header() 

159 assert tip.hash is not None 

160 result = daemon.get_blocks_by_hash([tip.hash, genesis_hash], 0, False, 0) 

161 assert len(result.blocks) == 0 

162 

163 # Can get a block by height 

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

165 def test_get_block_by_height(self, daemon: MoneroDaemonRpc) -> None: 

166 # config for testing blocks 

167 ctx: TestContext = TestContext() 

168 ctx.has_hex = True 

169 ctx.header_is_full = True 

170 ctx.has_txs = False 

171 

172 # retrieve by height of last block 

173 last_header: MoneroBlockHeader = daemon.get_last_block_header() 

174 assert last_header.height is not None 

175 block: MoneroBlock = daemon.get_block_by_height(last_header.height) 

176 assert block.height is not None 

177 BlockUtils.test_block(block, ctx) 

178 AssertUtils.assert_equals(daemon.get_block_by_height(block.height), block) 

179 

180 # retrieve by height of previous to last block 

181 block = daemon.get_block_by_height(last_header.height - 1) 

182 BlockUtils.test_block(block, ctx) 

183 assert last_header.height - 1 == block.height 

184 

185 # Can get blocks by height which includes transactions (binary) 

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

187 def test_get_blocks_by_height_binary(self, daemon: MoneroDaemonRpc) -> None: 

188 # set number of blocks to test 

189 num_blocks: int = 100 

190 

191 # select random heights # TODO: this is horribly inefficient way of computing last 100 blocks if not shuffling 

192 current_height: int = daemon.get_height() 

193 all_heights: list[int] = list(range(current_height - 1)) 

194 start_height: int = len(all_heights) - num_blocks 

195 heights: list[int] = list(range(start_height, len(all_heights))) 

196 

197 # fetch blocks 

198 blocks: list[MoneroBlock] = daemon.get_blocks_by_height(heights) 

199 

200 # test blocks 

201 tx_found: bool = False 

202 assert num_blocks == len(blocks) 

203 

204 for i, height in enumerate(heights): 

205 block: MoneroBlock = blocks[i] 

206 if len(block.txs) > 0: 

207 tx_found = True 

208 

209 BlockUtils.test_block(block, self.BINARY_BLOCK_CTX) 

210 assert block.height == height 

211 

212 assert tx_found, "No transactions found to test" 

213 

214 # Can get blocks by range in a single request 

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

216 def test_get_blocks_by_range(self, daemon: MoneroDaemonRpc) -> None: 

217 # get height range 

218 num_blocks: int = 100 

219 num_blocks_ago: int = 102 

220 assert num_blocks > 0 

221 assert num_blocks_ago >= num_blocks 

222 height: int = daemon.get_height() 

223 assert height - num_blocks_ago + num_blocks - 1 < height 

224 start_height: int = height - num_blocks_ago 

225 end_height: int = height - num_blocks_ago + num_blocks - 1 

226 

227 # test known start and end heights 

228 BlockUtils.test_get_blocks_range(daemon, start_height, end_height, height, False, self.BINARY_BLOCK_CTX) 

229 

230 # test unspecified start 

231 BlockUtils.test_get_blocks_range(daemon, None, num_blocks - 1, height, False, self.BINARY_BLOCK_CTX) 

232 

233 # test unspecified end 

234 BlockUtils.test_get_blocks_range(daemon, height - num_blocks - 1, None, height, False, self.BINARY_BLOCK_CTX) 

235 

236 # Test invalid blocks range 

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

238 def test_get_blocks_by_range_invalid(self, daemon: MoneroDaemonRpc) -> None: 

239 height: int = daemon.get_height() 

240 blocks: list[MoneroBlock] = daemon.get_blocks_by_range_chunked(height, height - 10) 

241 num_blocks: int = len(blocks) 

242 assert num_blocks == 0, f"Expected empty blocks for invalid range request, got {num_blocks}" 

243 

244 # Can get blocks by range using chunked requests 

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

246 def test_get_blocks_by_range_chunked(self, daemon: MoneroDaemonRpc) -> None: 

247 # get long height range 

248 num_blocks: int = min(daemon.get_height() - 2, 1440) # test up to ~2 days of blocks 

249 assert num_blocks > 0 

250 height: int = daemon.get_height() 

251 assert height - num_blocks - 1 < height 

252 start_height: int = height - num_blocks 

253 end_height: int = height - 1 

254 

255 # test known start and end heights 

256 BlockUtils.test_get_blocks_range(daemon, start_height, end_height, height, True, self.BINARY_BLOCK_CTX) 

257 

258 # test unspecified start 

259 BlockUtils.test_get_blocks_range(daemon, None, num_blocks - 1, height, True, self.BINARY_BLOCK_CTX) 

260 

261 # test unspecified end 

262 BlockUtils.test_get_blocks_range(daemon, end_height - num_blocks - 1, None, height, True, self.BINARY_BLOCK_CTX) 

263 

264 # Can get genesis block by range using chunked requests 

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

266 def test_get_genesis_block_by_range_chunked(self, daemon: MoneroDaemonRpc) -> None: 

267 # get current chain height 

268 chain_height: int = daemon.get_height() 

269 

270 # test genesis 

271 BlockUtils.test_get_blocks_range(daemon, 0, 0, chain_height, True, self.BINARY_BLOCK_CTX) 

272 

273 # Can get block hashes (binary) 

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

275 def test_get_block_hashes_binary(self, daemon: MoneroDaemonRpc) -> None: 

276 chain_height: int = daemon.get_height() 

277 genesis_hash: str = daemon.get_block_hash(0) 

278 

279 # a genesis-terminated history returns the whole chain from height 0 

280 result: MoneroGetBlockHashesResult = daemon.get_block_hashes([genesis_hash]) 

281 assert result.start_height == 0 

282 assert result.current_height is not None and result.current_height >= chain_height 

283 assert len(result.hashes) > 0 

284 assert result.hashes[0] == genesis_hash 

285 for hash_str in result.hashes: 

286 assert len(hash_str) == 64 

287 

288 # only the tip comes back when it is already the first hash 

289 tip: MoneroBlockHeader = daemon.get_last_block_header() 

290 assert tip.hash is not None 

291 result = daemon.get_block_hashes([tip.hash, genesis_hash]) 

292 assert result.hashes == [tip.hash] 

293 

294 # Can get the miner tx sum 

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

296 def test_get_miner_tx_sum(self, daemon: MoneroDaemonRpc) -> None: 

297 tx_sum: MoneroMinerTxSum = daemon.get_miner_tx_sum(0, min(5000, daemon.get_height())) 

298 DaemonUtils.test_miner_tx_sum(tx_sum) 

299 

300 # Can get alternative chains 

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

302 def test_get_alternative_chains(self, daemon: MoneroDaemonRpc) -> None: 

303 alt_chains: list[MoneroAltChain] = daemon.get_alt_chains() 

304 if len(alt_chains) == 0: 

305 logger.warning("No alternative chain found!") 

306 

307 for alt_chain in alt_chains: 

308 BlockUtils.test_alt_chain(alt_chain) 

309 

310 # Can get alternative block hashes 

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

312 def test_get_alternative_block_ids(self, daemon: MoneroDaemonRpc) -> None: 

313 alt_block_ids: list[str] = daemon.get_alt_block_hashes() 

314 if len(alt_block_ids) == 0: 

315 logger.warning("No alternative block ids found!") 

316 

317 for alt_block_id in alt_block_ids: 

318 logger.debug(f"Testing alternative block id: {alt_block_id}") 

319 assert alt_block_id is not None 

320 # TODO: common validation 

321 assert 64, len(alt_block_id) 

322 

323 # Can pop blocks from the top of the chain 

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

325 @pytest.mark.skipif(Utils.REGTEST is False, reason="REGTEST disabled") 

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

327 def test_pop_blocks(self, daemon: MoneroDaemonRpc) -> None: 

328 # stop mining so it doesn't race the pop, then restore the shared chain afterwards 

329 MiningUtils.try_stop_mining(daemon) 

330 height: int = daemon.get_height() 

331 

332 assert daemon.pop_blocks(1) == height - 1 

333 MiningUtils.generate_blocks(Utils.MINING_ADDRESS, 1, daemon) 

334 assert daemon.get_height() == height 

335 

336 with pytest.raises(Exception) as exc_info: 

337 daemon.pop_blocks(0) 

338 assert str(exc_info.value) == "Must provide a number of blocks to pop greater than 0"