Coverage for tests/daemon/model/test_blocks.py: 100%

203 statements  

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

1from __future__ import annotations 

2 

3import logging 

4import subprocess 

5import sys 

6from typing import LiteralString 

7 

8import pytest 

9from monero import ( 

10 MoneroBlock, 

11 MoneroBlockHeader, 

12 MoneroBlockTemplate, 

13 MoneroGetBlockHashesResult, 

14 MoneroGetBlocksByHashResult, 

15 MoneroTx, 

16) 

17 

18from utils import AssertUtils, BaseTestClass 

19 

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

21 

22 

23@pytest.mark.unit 

24class TestMoneroDaemonModelBlocks(BaseTestClass): 

25 """Daemon blocks model unit tests.""" 

26 

27 def test_block_template_deserialize(self) -> None: 

28 template: MoneroBlockTemplate = MoneroBlockTemplate() 

29 template.block_template_blob = "abcd" 

30 template.block_hashing_blob = "ef01" 

31 template.prev_hash = "a" * 64 

32 template.seed_hash = "b" * 64 

33 template.next_seed_hash = "c" * 64 

34 template.difficulty_low = 5000 

35 template.difficulty_high = 0 

36 template.expected_reward = 600000000000 

37 template.height = 12345 

38 template.reserved_offset = 130 

39 template.seed_height = 0 

40 AssertUtils.assert_serialization_integrity(template) 

41 

42 def test_block_header_deserialize(self) -> None: 

43 header: MoneroBlockHeader = MoneroBlockHeader() 

44 header.hash = "a" * 64 

45 header.height = 12345 

46 header.timestamp = 1600000000 

47 header.size = 2048 

48 header.weight = 2048 

49 header.long_term_weight = 2048 

50 header.depth = 10 

51 header.difficulty_low = 5000 

52 header.difficulty_high = 0 

53 header.cumulative_difficulty_low = 900000 

54 header.cumulative_difficulty_high = 0 

55 header.major_version = 16 

56 header.minor_version = 16 

57 header.nonce = 42 

58 header.miner_tx_hash = "b" * 64 

59 header.num_txs = 3 

60 header.orphan_status = False 

61 header.prev_hash = "c" * 64 

62 header.reward = 600000000000 

63 header.pow_hash = "d" * 64 

64 AssertUtils.assert_serialization_integrity(header) 

65 

66 def test_block_deserialize(self) -> None: 

67 block: MoneroBlock = MoneroBlock() 

68 block.hash = "a" * 64 

69 block.height = 12345 

70 block.hex = "deadbeef" 

71 block.tx_hashes = ["b" * 64, "c" * 64] 

72 

73 miner_tx: MoneroTx = MoneroTx() 

74 miner_tx.hash = "d" * 64 

75 miner_tx.is_miner_tx = True 

76 block.miner_tx = miner_tx 

77 

78 tx: MoneroTx = MoneroTx() 

79 tx.hash = "e" * 64 

80 block.txs = [tx] 

81 

82 restored: MoneroBlock = AssertUtils.assert_serialization_integrity(block) 

83 

84 # nested txs are parsed and back-linked to the block 

85 assert restored.miner_tx is not None 

86 assert restored.miner_tx.hash == "d" * 64 

87 assert restored.miner_tx.is_miner_tx is True 

88 assert restored.miner_tx.block is not None and restored.miner_tx.block.height == 12345 

89 assert len(restored.txs) == 1 

90 assert restored.txs[0].hash == "e" * 64 

91 assert restored.txs[0].block is not None and restored.txs[0].block.height == 12345 

92 

93 def test_get_block_hashes_result_deserialize(self) -> None: 

94 result: MoneroGetBlockHashesResult = MoneroGetBlockHashesResult() 

95 result.hashes = ["a" * 64, "b" * 64] 

96 result.start_height = 100 

97 result.current_height = 3000000 

98 AssertUtils.assert_serialization_integrity(result) 

99 

100 def test_get_blocks_by_hash_result_deserialize(self) -> None: 

101 result: MoneroGetBlocksByHashResult = MoneroGetBlocksByHashResult() 

102 result.current_height = 3000000 

103 # blocks are serialized but from_property_tree() never reads them back 

104 # (monero_block has no from_property_tree of its own); see test below 

105 AssertUtils.assert_serialization_integrity(result) 

106 

107 def test_get_blocks_by_hash_result_blocks_deserialize(self) -> None: 

108 result: MoneroGetBlocksByHashResult = MoneroGetBlocksByHashResult() 

109 block: MoneroBlock = MoneroBlock() 

110 block.hash = "a" * 64 

111 block.height = 100 

112 result.blocks = [block] 

113 json_str: str = result.serialize() 

114 logger.debug(f"Serialized get blocks by hash result: {json_str}") 

115 assert '"blocks"' in json_str 

116 restored: MoneroGetBlocksByHashResult = MoneroGetBlocksByHashResult.deserialize(json_str) 

117 assert len(restored.blocks) == 0 

118 

119 def test_block_header_copy(self) -> None: 

120 header: MoneroBlockHeader = MoneroBlockHeader() 

121 header.hash = "a" * 64 

122 header.height = 100 

123 header.timestamp = 1700000000 

124 header.size = 1000 

125 header.weight = 1000 

126 header.major_version = 16 

127 header.minor_version = 16 

128 header.nonce = 12345 

129 header.reward = 600000000000 

130 

131 copy: MoneroBlockHeader = header.copy() 

132 assert copy is not header 

133 assert copy.serialize() == header.serialize() 

134 

135 # copy is independent of the original 

136 copy.height = 999 

137 assert header.height == 100 

138 

139 def test_block_header_merge(self) -> None: 

140 a: MoneroBlockHeader = MoneroBlockHeader() 

141 a.hash = "a" * 64 

142 a.height = 100 

143 a.timestamp = 1700000000 

144 

145 b: MoneroBlockHeader = a.copy() 

146 b.height = 200 # height can increase -> resolves to the higher value 

147 b.timestamp = 1800000000 # timestamp can increase -> resolves to the higher value 

148 b.size = 2000 # a.size is unset -> merge fills the gap 

149 

150 a.merge(b) 

151 assert a.height == 200 

152 assert a.timestamp == 1800000000 

153 assert a.size == 2000 

154 assert a.hash == "a" * 64 

155 

156 def test_block_header_merge_conflict_raises(self) -> None: 

157 # fields without special reconciliation (e.g. hash) must match on both 

158 # sides, or merge() raises rather than silently picking one 

159 a: MoneroBlockHeader = MoneroBlockHeader() 

160 a.hash = "a" * 64 

161 b: MoneroBlockHeader = MoneroBlockHeader() 

162 b.hash = "b" * 64 

163 with pytest.raises(Exception, match="[Cc]annot reconcile"): 

164 a.merge(b) 

165 

166 def test_block_copy(self) -> None: 

167 block: MoneroBlock = MoneroBlock() 

168 block.hash = "a" * 64 

169 block.height = 100 

170 block.hex = "deadbeef" 

171 block.tx_hashes = ["b" * 64, "c" * 64] 

172 

173 miner_tx: MoneroTx = MoneroTx() 

174 miner_tx.hash = "d" * 64 

175 miner_tx.is_miner_tx = True 

176 block.miner_tx = miner_tx 

177 

178 tx: MoneroTx = MoneroTx() 

179 tx.hash = "e" * 64 

180 block.txs = [tx] 

181 

182 copy: MoneroBlock = block.copy() 

183 assert copy is not block 

184 assert copy.serialize() == block.serialize() 

185 

186 # miner tx and non-wallet txs are deep copied and back-linked to the copy 

187 assert copy.miner_tx is not None and copy.miner_tx is not block.miner_tx 

188 assert copy.miner_tx.hash == "d" * 64 

189 assert copy.miner_tx.block is copy 

190 assert len(copy.txs) == 1 

191 assert copy.txs[0] is not block.txs[0] 

192 assert copy.txs[0].hash == "e" * 64 

193 assert copy.txs[0].block is copy 

194 

195 def test_block_merge(self) -> None: 

196 a: MoneroBlock = MoneroBlock() 

197 a.hash = "a" * 64 

198 a.height = 100 

199 b: MoneroBlock = a.copy() 

200 b.hex = "deadbeef" # a.hex is unset -> merge fills the gap 

201 a.merge(b) 

202 assert a.hex == "deadbeef" 

203 

204 def test_block_header_merge_dispatches_to_block_merge(self) -> None: 

205 a: MoneroBlock = MoneroBlock() 

206 a.height = 100 

207 b: MoneroBlock = MoneroBlock() 

208 b.height = 100 

209 b.hex = "deadbeef" # a.hex is unset -> merge fills the gap 

210 

211 MoneroBlockHeader.merge(a, b) 

212 assert a.hex == "deadbeef" 

213 

214 def test_block_merge_adopts_other_miner_tx(self) -> None: 

215 a: MoneroBlock = MoneroBlock() 

216 a.height = 100 

217 # a.miner_tx is unset -> merge adopts other's miner tx and repoints its block at a 

218 

219 b: MoneroBlock = MoneroBlock() 

220 b.height = 100 

221 miner_tx: MoneroTx = MoneroTx() 

222 miner_tx.hash = "a" * 64 

223 miner_tx.is_miner_tx = True 

224 b.miner_tx = miner_tx 

225 

226 a.merge(b) 

227 assert a.miner_tx is not None 

228 assert a.miner_tx is b.miner_tx 

229 assert a.miner_tx.hash == "a" * 64 

230 assert a.miner_tx.block is a 

231 

232 def test_block_merge_merges_distinct_miner_txs(self) -> None: 

233 a: MoneroBlock = MoneroBlock() 

234 a.height = 100 

235 miner_a: MoneroTx = MoneroTx() 

236 miner_a.hash = "a" * 64 

237 miner_a.is_confirmed = True # required: tx merge() dereferences is_confirmed directly 

238 a.miner_tx = miner_a 

239 

240 b: MoneroBlock = MoneroBlock() 

241 b.height = 100 

242 miner_b: MoneroTx = MoneroTx() 

243 miner_b.hash = "a" * 64 

244 miner_b.is_confirmed = True 

245 miner_b.num_confirmations = 5 # miner_a's num_confirmations is unset -> merge fills the gap 

246 b.miner_tx = miner_b 

247 

248 a.merge(b) 

249 assert a.miner_tx is miner_a # kept in place, fields merged into it 

250 assert a.miner_tx.num_confirmations == 5 

251 assert a.miner_tx.block is a 

252 

253 def test_block_merge_txs_with_unset_hash_are_kept_distinct(self) -> None: 

254 script: LiteralString = ( 

255 "import monero, sys\n" 

256 "a = monero.MoneroBlock()\n" 

257 "a.height = 100\n" 

258 "tx_a = monero.MoneroTx()\n" # hash intentionally left unset 

259 "a.txs = [tx_a]\n" 

260 "b = monero.MoneroBlock()\n" 

261 "b.height = 100\n" 

262 "tx_b = monero.MoneroTx()\n" # hash intentionally left unset 

263 "b.txs = [tx_b]\n" 

264 "a.merge(b)\n" 

265 "n = len(a.txs) if a.txs else 0\n" 

266 "sys.exit(0 if n == 2 else f'txs not kept distinct: len={n}')\n" 

267 ) 

268 result: subprocess.CompletedProcess[str] = subprocess.run([sys.executable, "-c", script], capture_output=True, text=True, timeout=30) 

269 logger.debug(f"subprocess exit code: {result.returncode}, stderr: {result.stderr.strip()}") 

270 assert result.returncode == 0, ( 

271 f"Block.merge() did not keep unset-hash txs distinct (exit code {result.returncode}): " 

272 f"{result.stderr.strip()[-300:]}" 

273 )