Coverage for tests/daemon/rpc/test_mining.py: 100%
80 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 MoneroAddAuxiliaryPowResult,
8 MoneroAuxiliaryPow,
9 MoneroBlockTemplate,
10 MoneroDaemonRpc,
11 MoneroMinerData,
12 MoneroMiningStatus,
13 MoneroWalletRpc,
14)
16from utils import DaemonUtils, MiningUtils, TestUtils as Utils
17from .base_test_monero_daemon_rpc import BaseTestMoneroDaemonRpc
19logger: logging.Logger = logging.getLogger("TestMoneroDaemonRpcMining")
22@pytest.mark.integration
23class TestMoneroDaemonRpcMining(BaseTestMoneroDaemonRpc):
24 """Rpc daemon mining integration tests."""
26 # Can start and stop mining
27 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
28 def test_mining(self, daemon: MoneroDaemonRpc, wallet: MoneroWalletRpc) -> None:
29 # stop mining at beginning of test
30 MiningUtils.try_stop_mining(daemon)
32 # generate address to mine to
33 address: str = wallet.get_primary_address()
35 # start mining
36 daemon.start_mining(address, 1, False, True)
38 # stop mining
39 daemon.stop_mining()
41 # Can get mining status
42 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
43 def test_get_mining_status(self, daemon: MoneroDaemonRpc, wallet: MoneroWalletRpc) -> None:
44 try:
45 # stop mining at beginning of test
46 MiningUtils.try_stop_mining(daemon)
48 # test status without mining
49 status: MoneroMiningStatus = daemon.get_mining_status()
50 logger.debug(f"Testing mining status: {status.serialize()}")
51 assert status.is_active is False
52 assert status.address is None, f"Mining address is not None: {status.address}"
53 assert 0 == status.speed
54 assert 0 == status.num_threads
55 assert status.is_background is None
57 # test status with mining
58 address: str = wallet.get_primary_address()
59 thread_count: int = 1
60 is_background: bool = False
61 daemon.start_mining(address, thread_count, is_background, True)
62 status = daemon.get_mining_status()
63 logger.debug(f"Testing mining status: {status.serialize()}")
64 assert status.speed is not None
65 assert status.is_active is True
66 assert address == status.address
67 assert status.speed >= 0
68 assert thread_count == status.num_threads
69 assert is_background == status.is_background
70 finally:
71 # stop mining at end of test
72 MiningUtils.try_stop_mining(daemon)
74 # Can submit a mined block to the network
75 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
76 @pytest.mark.flaky(reruns=5, reruns_delay=5, only_rerun=[])
77 def test_submit_mined_block(self, daemon: MoneroDaemonRpc) -> None:
78 # get template to mine on
79 template: MoneroBlockTemplate = daemon.get_block_template(Utils.ADDRESS)
80 assert template.block_template_blob is not None
82 # TODO monero rpc: way to get mining nonce when found in order to submit?
84 # try to submit block hashing blob without nonce
85 with pytest.raises(Exception) as exc_info:
86 daemon.submit_block(template.block_template_blob)
87 assert str(exc_info.value) == "Block not accepted"
89 # Can get the data needed to mine a block
90 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
91 def test_get_miner_data(self, daemon: MoneroDaemonRpc) -> None:
92 data: MoneroMinerData = daemon.get_miner_data()
93 DaemonUtils.test_miner_data(data)
95 # Can calculate a block's proof-of-work hash
96 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
97 def test_calculate_pow(self, daemon: MoneroDaemonRpc) -> None:
98 data: MoneroMinerData = daemon.get_miner_data()
99 template: MoneroBlockTemplate = daemon.get_block_template(Utils.ADDRESS)
100 assert data.major_version is not None
101 assert data.height is not None
102 assert data.seed_hash is not None
103 assert template.block_template_blob is not None
105 pow_hash: str = daemon.calculate_pow(data.major_version, data.height, template.block_template_blob, data.seed_hash)
106 assert len(pow_hash) == 64
108 with pytest.raises(Exception) as exc_info:
109 daemon.calculate_pow(data.major_version, data.height, "", data.seed_hash)
110 assert str(exc_info.value) == "Must provide a block blob to hash"
112 # Can add auxiliary proof-of-work to a block template
113 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
114 def test_add_auxiliary_pow(self, daemon: MoneroDaemonRpc) -> None:
115 template: MoneroBlockTemplate = daemon.get_block_template(Utils.ADDRESS)
116 assert template.block_template_blob is not None
118 aux_pow: MoneroAuxiliaryPow = MoneroAuxiliaryPow("11" * 32, "22" * 32)
119 result: MoneroAddAuxiliaryPowResult = daemon.add_auxiliary_pow(template.block_template_blob, [aux_pow])
120 assert result.block_template_blob is not None and len(result.block_template_blob) > 0
121 assert result.block_hashing_blob is not None and len(result.block_hashing_blob) > 0
122 assert result.merkle_root is not None and len(result.merkle_root) == 64
123 assert result.merkle_tree_depth is not None and result.merkle_tree_depth >= 0
124 assert len(result.aux_pow) == 1
125 assert result.aux_pow[0].id is not None and len(result.aux_pow[0].id) == 64
126 assert result.aux_pow[0].hash is not None and len(result.aux_pow[0].hash) == 64
128 with pytest.raises(Exception) as exc_info:
129 daemon.add_auxiliary_pow(template.block_template_blob, [])
130 assert str(exc_info.value) == "Must provide auxiliary proof of work to add"