Coverage for tests/common/test_block_generation.py: 100%

30 statements  

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

1from __future__ import annotations 

2 

3from typing import Any 

4 

5import pytest 

6from monero import MoneroDaemonInfo 

7 

8from utils import BaseTestClass 

9from utils.block_generation import MAX_GENERATE_DIFFICULTY, is_generation_fast 

10 

11 

12class _Daemon: 

13 """Daemon that answers get_info with a given difficulty.""" 

14 

15 def __init__(self, difficulty: int, is_regtest: bool | None = True, fails: bool = False) -> None: 

16 self._difficulty: int = difficulty 

17 self._is_regtest: bool | None = is_regtest 

18 self._fails: bool = fails 

19 

20 def get_info(self) -> MoneroDaemonInfo: 

21 if self._fails: 

22 raise RuntimeError("no node") 

23 info: MoneroDaemonInfo = MoneroDaemonInfo() 

24 info.difficulty_low = self._difficulty & ((1 << 64) - 1) 

25 info.difficulty_high = self._difficulty >> 64 

26 info.is_regtest = self._is_regtest 

27 return info 

28 

29 

30@pytest.mark.unit 

31class TestBlockGeneration(BaseTestClass): 

32 """Choice between generating and mining blocks, unit tests.""" 

33 

34 # Blocks are generated at a low difficulty, where the miner makes too many 

35 @pytest.mark.parametrize("difficulty", [1, 5, MAX_GENERATE_DIFFICULTY]) 

36 def test_generates_at_low_difficulty(self, difficulty: int) -> None: 

37 assert is_generation_fast(_Daemon(difficulty)) is True # type: ignore[arg-type] 

38 

39 # Blocks are mined at a high difficulty, where the miner is faster than generating 

40 @pytest.mark.parametrize("difficulty", [MAX_GENERATE_DIFFICULTY + 1, 500, 1 << 70]) 

41 def test_mines_at_high_difficulty(self, difficulty: int) -> None: 

42 assert is_generation_fast(_Daemon(difficulty)) is False # type: ignore[arg-type] 

43 

44 # Blocks are not generated outside regtest, or when the daemon does not answer 

45 @pytest.mark.parametrize("daemon", [_Daemon(5, is_regtest=False), _Daemon(5, fails=True), _Daemon(0)]) 

46 def test_does_not_generate_otherwise(self, daemon: Any) -> None: 

47 assert is_generation_fast(daemon) is False