Coverage for tests/daemon/model/test_info.py: 100%
138 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 19:49 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 19:49 +0000
1from __future__ import annotations
3import logging
5import pytest
6from monero import (
7 MoneroAltChain,
8 MoneroConnectionSpan,
9 MoneroDaemonInfo,
10 MoneroDaemonNetworkStats,
11 MoneroDaemonSyncInfo,
12 MoneroDaemonUpdateCheckResult,
13 MoneroDaemonUpdateDownloadResult,
14 MoneroFeeEstimate,
15 MoneroHardForkInfo,
16 MoneroNetworkType,
17 MoneroPeer,
18 MoneroPruneResult,
19)
21from utils import AssertUtils, BaseTestClass
23logger: logging.Logger = logging.getLogger("TestMoneroDaemonModelInfo")
26@pytest.mark.unit
27class TestMoneroDaemonModelInfo(BaseTestClass):
28 """Daemon info model unit tests."""
30 def test_alt_chain_deserialize(self) -> None:
31 alt_chain: MoneroAltChain = MoneroAltChain()
32 alt_chain.block_hashes = ["a" * 64, "b" * 64]
33 alt_chain.difficulty_low = 100
34 alt_chain.difficulty_high = 0
35 alt_chain.height = 12345
36 alt_chain.length = 3
37 alt_chain.main_chain_parent_block_hash = "c" * 64
38 AssertUtils.assert_serialization_integrity(alt_chain)
40 def test_prune_result_deserialize(self) -> None:
41 result: MoneroPruneResult = MoneroPruneResult()
42 result.pruning_seed = 387
43 # is_pruned is deliberately not set here: to_rapidjson_val() serializes it
44 # under "isPruned" but from_property_tree() looks for "pruned" instead, so
45 # it never round trips (see test below)
46 AssertUtils.assert_serialization_integrity(result)
48 def test_prune_result_is_pruned_deserialize(self) -> None:
49 result: MoneroPruneResult = MoneroPruneResult()
50 result.is_pruned = True
51 json_str: str = result.serialize()
52 logger.debug(f"Serialized prune result: {json_str}")
53 assert '"isPruned"' in json_str
54 restored: MoneroPruneResult = MoneroPruneResult.deserialize(json_str)
55 logger.debug(f"Deserialized prune result re-serialized: {restored.serialize()}")
56 assert restored.is_pruned == result.is_pruned
58 def test_fee_estimate_deserialize(self) -> None:
59 estimate: MoneroFeeEstimate = MoneroFeeEstimate()
60 estimate.fee = 20000
61 estimate.quantization_mask = 10000
62 estimate.fees = [10000, 20000, 30000, 40000]
63 AssertUtils.assert_serialization_integrity(estimate)
65 def test_daemon_info_deserialize(self) -> None:
66 info: MoneroDaemonInfo = MoneroDaemonInfo()
67 info.credits = 0
68 info.top_block_hash = "a" * 64
69 info.version = "0.18.5.1"
70 info.num_alt_blocks = 1
71 info.block_size_limit = 600000
72 info.block_size_median = 300000
73 info.block_weight_limit = 600000
74 info.block_weight_median = 300000
75 info.bootstrap_daemon_address = "node.example.com"
76 info.difficulty_low = 123456
77 info.difficulty_high = 0
78 info.cumulative_difficulty_low = 987654
79 info.cumulative_difficulty_high = 0
80 info.free_space = 1000000000
81 info.num_offline_peers = 1
82 info.num_online_peers = 10
83 info.height = 3000000
84 info.height_without_bootstrap = 3000000
85 info.network_type = MoneroNetworkType.TESTNET
86 info.is_offline = False
87 info.num_incoming_connections = 5
88 info.num_outgoing_connections = 8
89 info.num_rpc_connections = 2
90 info.start_timestamp = 1600000000
91 info.adjusted_timestamp = 1700000000
92 info.target = 120
93 info.target_height = 0
94 info.num_txs = 100
95 info.num_txs_pool = 3
96 info.was_bootstrap_ever_used = False
97 info.database_size = 100000000
98 info.update_available = False
99 info.is_busy_syncing = False
100 info.is_synchronized = True
101 info.is_restricted = False
102 info.is_regtest = True
103 AssertUtils.assert_serialization_integrity(info)
105 def test_daemon_info_invalid_network_type(self) -> None:
106 with pytest.raises(RuntimeError, match="invalid network type"):
107 MoneroDaemonInfo.deserialize('{"networkType":9}')
109 def test_daemon_sync_info_deserialize(self) -> None:
110 info: MoneroDaemonSyncInfo = MoneroDaemonSyncInfo()
111 info.credits = 0
112 info.top_block_hash = "a" * 64
113 info.height = 3000000
114 info.target_height = 3000010
115 info.next_needed_pruning_seed = 0
116 info.overview = "syncing"
117 # peers/spans are serialized as sub-arrays but from_property_tree() never
118 # reads them back (see test below)
119 AssertUtils.assert_serialization_integrity(info)
121 def test_daemon_sync_info_peers_and_spans_deserialize(self) -> None:
122 info: MoneroDaemonSyncInfo = MoneroDaemonSyncInfo()
123 info.peers = [MoneroPeer()]
124 info.spans = [MoneroConnectionSpan()]
125 json_str: str = info.serialize()
126 logger.debug(f"Serialized daemon sync info: {json_str}")
127 assert "peers" in json_str and "spans" in json_str
128 restored: MoneroDaemonSyncInfo = MoneroDaemonSyncInfo.deserialize(json_str)
129 logger.debug(f"Deserialized daemon sync info re-serialized: {restored.serialize()}")
130 assert len(restored.peers) == len(info.peers)
131 assert len(restored.spans) == len(info.spans)
133 def test_hard_fork_info_deserialize(self) -> None:
134 info: MoneroHardForkInfo = MoneroHardForkInfo()
135 info.credits = 0
136 info.top_block_hash = "a" * 64
137 info.earliest_height = 100000
138 info.is_enabled = True
139 info.state = 0
140 info.threshold = 0
141 info.version = 16
142 info.num_votes = 5000
143 info.window = 10080
144 info.voting = 16
145 AssertUtils.assert_serialization_integrity(info)
147 def test_daemon_network_stats_deserialize(self) -> None:
148 stats: MoneroDaemonNetworkStats = MoneroDaemonNetworkStats()
149 stats.credits = 0
150 stats.top_block_hash = "a" * 64
151 stats.start_time = 1700000000
152 stats.total_packets_in = 1234
153 stats.total_bytes_in = 567890
154 stats.total_packets_out = 4321
155 stats.total_bytes_out = 98765
156 AssertUtils.assert_serialization_integrity(stats)
158 def test_daemon_update_check_result_deserialize(self) -> None:
159 result: MoneroDaemonUpdateCheckResult = MoneroDaemonUpdateCheckResult()
160 result.is_update_available = True
161 result.version = "0.18.5.1"
162 result.hash = "a" * 64
163 result.auto_uri = "https://example.com/auto"
164 result.user_uri = "https://example.com/user"
165 AssertUtils.assert_serialization_integrity(result)
167 def test_daemon_update_download_result_deserialize(self) -> None:
168 result: MoneroDaemonUpdateDownloadResult = MoneroDaemonUpdateDownloadResult()
169 result.is_update_available = True
170 result.version = "0.18.5.1"
171 result.hash = "a" * 64
172 result.auto_uri = "https://example.com/auto"
173 result.user_uri = "https://example.com/user"
174 result.download_path = "/tmp/update.bin"
175 AssertUtils.assert_serialization_integrity(result)