Coverage for tests/utils/mining_utils.py: 91%
54 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
1import logging
3from monero import MoneroDaemonRpc, MoneroGenerateBlocksResult, MoneroMiningStatus
5from .test_utils import TestUtils as Utils
7logger: logging.Logger = logging.getLogger("MiningUtils")
10class MiningUtils:
11 """Mining test utilities."""
13 _DAEMON: MoneroDaemonRpc | None = None
14 """Internal mining daemon."""
16 @classmethod
17 def get_daemon(cls) -> MoneroDaemonRpc:
18 """Get internal mining daemon.
20 :returns MoneroDaemonRpc: daemon rpc used for internal mining.
21 """
22 return Utils.get_mining_daemon()
24 @classmethod
25 def generate_blocks(cls, address: str, num_blocks: int, d: MoneroDaemonRpc | None = None) -> MoneroGenerateBlocksResult:
26 """Generate blocks to a wallet address (regtest only).
28 :param str address: is the address of the wallet to receive miner transactions if block is successfully mined.
29 :param int num_blocks: is the number of blocks to generate.
30 :param MoneroDaemonRpc | None d: daemon to generate blocks with (default internal daemon).
31 :returns MoneroGenerateBlocksResult: the result of generating blocks; height is the height of the last block generated.
32 """
33 assert Utils.REGTEST, "Generating blocks is supported only on regtest."
34 daemon: MoneroDaemonRpc = cls.get_daemon() if d is None else d
35 return daemon.generate_blocks(address, num_blocks)
37 @classmethod
38 def is_mining(cls, d: MoneroDaemonRpc | None = None) -> bool:
39 """Check if mining is enabled.
41 :param MoneroDaemonRpc | None d: rpc daemon to check (default internal daemon).
42 :returns bool: `True` if mining is enabled, `False` otherwise.
43 """
44 # max tries 3
45 daemon: MoneroDaemonRpc = cls.get_daemon() if d is None else d
46 for i in range(3):
47 try:
48 status: MoneroMiningStatus = daemon.get_mining_status()
49 return status.is_active is True
51 except Exception:
52 if i == 2:
53 raise
55 return False
57 @classmethod
58 def start_mining(cls, d: MoneroDaemonRpc | None = None) -> None:
59 """Start mining.
61 :param MoneroDaemonRpc | None d: daemon to start mining with (default internal daemon).
62 """
63 if cls.is_mining():
64 raise Exception("Mining already started")
66 daemon: MoneroDaemonRpc = cls.get_daemon() if d is None else d
67 daemon.start_mining(Utils.MINING_ADDRESS, 1, False, False)
69 @classmethod
70 def stop_mining(cls, d: MoneroDaemonRpc | None = None) -> None:
71 """
72 Stop mining.
74 :param MoneroDaemonRpc | None d: daemon to stop mining with (default internal daemon).
75 """
76 if not cls.is_mining():
77 raise Exception("Mining already stopped")
79 daemon: MoneroDaemonRpc = cls.get_daemon() if d is None else d
80 daemon.stop_mining()
82 @classmethod
83 def try_stop_mining(cls, d: MoneroDaemonRpc | None = None) -> bool:
84 """
85 Try stop mining.
87 :param MoneroDaemonRpc | None d: daemon to stop mining with (default internal daemon).
88 :returns bool: `True` if mining stopped, `False` otherwise.
89 """
90 try:
91 cls.stop_mining(d)
92 return True
93 except Exception as e:
94 logger.warning(f"MiningUtils.stop_mining(): {e}")
95 return False
97 @classmethod
98 def try_start_mining(cls, d: MoneroDaemonRpc | None = None) -> bool:
99 """
100 Try start mining.
102 :param MoneroDaemonRpc | None d: daemon to start mining with (default internal daemon).
103 :returns bool: `True` if mining started, `False` otherwise.
104 """
105 try:
106 cls.start_mining(d)
107 return True
108 except Exception as e:
109 logger.warning(f"MiningUtils.start_mining(): {e}")
110 return False