|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from unittest.mock import MagicMock, patch |
| 5 | + |
| 6 | +import pytest |
| 7 | +from chia_rs import ProofOfSpace |
| 8 | +from chia_rs.sized_bytes import bytes32 |
| 9 | +from chia_rs.sized_ints import uint64 |
| 10 | + |
| 11 | +from chia._tests.conftest import HarvesterFarmerEnvironment |
| 12 | +from chia.harvester.harvester_api import HarvesterAPI |
| 13 | +from chia.plotting.util import PlotInfo |
| 14 | +from chia.protocols import harvester_protocol |
| 15 | +from chia.protocols.harvester_protocol import PoolDifficulty |
| 16 | +from chia.server.ws_connection import WSChiaConnection |
| 17 | +from chia.simulator.block_tools import BlockTools |
| 18 | + |
| 19 | + |
| 20 | +def create_signage_point_harvester_from_constants(bt: BlockTools) -> harvester_protocol.NewSignagePointHarvester: |
| 21 | + """create a NewSignagePointHarvester using real constants from block tools""" |
| 22 | + # use the pre-generated signage point data from network_protocol_data.py |
| 23 | + # but with real constants from block_tools |
| 24 | + from chia._tests.util.network_protocol_data import new_signage_point_harvester |
| 25 | + |
| 26 | + # create a version with real constants values |
| 27 | + return harvester_protocol.NewSignagePointHarvester( |
| 28 | + challenge_hash=new_signage_point_harvester.challenge_hash, |
| 29 | + difficulty=uint64(bt.constants.DIFFICULTY_STARTING), |
| 30 | + sub_slot_iters=uint64(bt.constants.SUB_SLOT_ITERS_STARTING), |
| 31 | + signage_point_index=new_signage_point_harvester.signage_point_index, |
| 32 | + sp_hash=new_signage_point_harvester.sp_hash, |
| 33 | + pool_difficulties=[], # empty for simplicity, unless testing pool functionality |
| 34 | + peak_height=new_signage_point_harvester.peak_height, |
| 35 | + last_tx_height=new_signage_point_harvester.last_tx_height, |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.anyio |
| 40 | +async def test_new_signage_point_harvester_no_keys( |
| 41 | + harvester_farmer_environment: HarvesterFarmerEnvironment, |
| 42 | +) -> None: |
| 43 | + """test that new_signage_point_harvester returns early when no keys available""" |
| 44 | + _farmer_service, _farmer_rpc_client, harvester_service, _harvester_rpc_client, bt = harvester_farmer_environment |
| 45 | + harvester_api = harvester_service._server.api |
| 46 | + assert isinstance(harvester_api, HarvesterAPI) |
| 47 | + |
| 48 | + # create real signage point data from block tools |
| 49 | + new_challenge = create_signage_point_harvester_from_constants(bt) |
| 50 | + |
| 51 | + # mock plot manager to return false for public_keys_available |
| 52 | + with patch.object(harvester_api.harvester.plot_manager, "public_keys_available", return_value=False): |
| 53 | + mock_peer = MagicMock(spec=WSChiaConnection) |
| 54 | + |
| 55 | + result = harvester_api.new_signage_point_harvester(new_challenge, mock_peer) |
| 56 | + assert result is None |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.anyio |
| 60 | +async def test_new_signage_point_harvester_happy_path( |
| 61 | + harvester_farmer_environment: HarvesterFarmerEnvironment, |
| 62 | +) -> None: |
| 63 | + """test successful signage point processing with valid plots""" |
| 64 | + _farmer_service, _farmer_rpc_client, harvester_service, _harvester_rpc_client, bt = harvester_farmer_environment |
| 65 | + harvester_api = harvester_service._server.api |
| 66 | + assert isinstance(harvester_api, HarvesterAPI) |
| 67 | + |
| 68 | + # create real signage point data from block tools |
| 69 | + new_challenge = create_signage_point_harvester_from_constants(bt) |
| 70 | + |
| 71 | + mock_peer = MagicMock(spec=WSChiaConnection) |
| 72 | + |
| 73 | + # create mock plot info |
| 74 | + mock_prover = MagicMock() |
| 75 | + mock_prover.get_id.return_value = bytes32(b"2" * 32) |
| 76 | + mock_prover.get_size.return_value = 32 |
| 77 | + mock_prover.get_qualities_for_challenge.return_value = [bytes32(b"quality" + b"0" * 25)] |
| 78 | + |
| 79 | + mock_plot_info = MagicMock(spec=PlotInfo) |
| 80 | + mock_plot_info.prover = mock_prover |
| 81 | + mock_plot_info.pool_contract_puzzle_hash = None |
| 82 | + |
| 83 | + plot_path = Path("/fake/plot.plot") |
| 84 | + |
| 85 | + with patch.object(harvester_api.harvester.plot_manager, "public_keys_available", return_value=True): |
| 86 | + with patch.object(harvester_api.harvester.plot_manager, "plots", {plot_path: mock_plot_info}): |
| 87 | + with patch("chia.harvester.harvester_api.passes_plot_filter", return_value=True): |
| 88 | + with patch("chia.harvester.harvester_api.calculate_pos_challenge") as mock_calc_pos: |
| 89 | + mock_calc_pos.return_value = bytes32(b"sp_challenge" + b"0" * 20) |
| 90 | + |
| 91 | + with patch("chia.harvester.harvester_api.calculate_iterations_quality") as mock_calc_iter: |
| 92 | + # set required_iters low enough to pass the sp_interval_iters check |
| 93 | + mock_calc_iter.return_value = uint64(1000) |
| 94 | + |
| 95 | + with patch("chia.harvester.harvester_api.calculate_sp_interval_iters") as mock_sp_interval: |
| 96 | + mock_sp_interval.return_value = uint64(10000) |
| 97 | + |
| 98 | + with patch.object(mock_prover, "get_full_proof") as mock_get_proof: |
| 99 | + mock_proof = MagicMock(spec=ProofOfSpace) |
| 100 | + mock_get_proof.return_value = mock_proof, None |
| 101 | + |
| 102 | + result = harvester_api.new_signage_point_harvester(new_challenge, mock_peer) |
| 103 | + # function returns None but should have processed the plot |
| 104 | + assert result is None |
| 105 | + |
| 106 | + |
| 107 | +@pytest.mark.anyio |
| 108 | +async def test_new_signage_point_harvester_pool_difficulty_override( |
| 109 | + harvester_farmer_environment: HarvesterFarmerEnvironment, |
| 110 | +) -> None: |
| 111 | + """test that pool difficulty overrides are applied correctly""" |
| 112 | + _farmer_service, _farmer_rpc_client, harvester_service, _harvester_rpc_client, bt = harvester_farmer_environment |
| 113 | + harvester_api = harvester_service._server.api |
| 114 | + assert isinstance(harvester_api, HarvesterAPI) |
| 115 | + |
| 116 | + mock_peer = MagicMock(spec=WSChiaConnection) |
| 117 | + |
| 118 | + pool_puzzle_hash = bytes32(b"pool" + b"0" * 28) |
| 119 | + |
| 120 | + mock_prover = MagicMock() |
| 121 | + mock_prover.get_id.return_value = bytes32(b"2" * 32) |
| 122 | + mock_prover.get_size.return_value = 32 |
| 123 | + mock_prover.get_qualities_for_challenge.return_value = [bytes32(b"quality" + b"0" * 25)] |
| 124 | + |
| 125 | + mock_plot_info = MagicMock(spec=PlotInfo) |
| 126 | + mock_plot_info.prover = mock_prover |
| 127 | + mock_plot_info.pool_contract_puzzle_hash = pool_puzzle_hash |
| 128 | + |
| 129 | + plot_path = Path("/fake/plot.plot") |
| 130 | + |
| 131 | + pool_difficulty = PoolDifficulty( |
| 132 | + pool_contract_puzzle_hash=pool_puzzle_hash, |
| 133 | + difficulty=uint64(500), # lower than main difficulty |
| 134 | + sub_slot_iters=uint64(67108864), # different from main |
| 135 | + ) |
| 136 | + |
| 137 | + # create real signage point data from constants with pool difficulty |
| 138 | + new_challenge = create_signage_point_harvester_from_constants(bt) |
| 139 | + # override with pool difficulty for this test |
| 140 | + new_challenge = harvester_protocol.NewSignagePointHarvester( |
| 141 | + challenge_hash=new_challenge.challenge_hash, |
| 142 | + difficulty=new_challenge.difficulty, |
| 143 | + sub_slot_iters=new_challenge.sub_slot_iters, |
| 144 | + signage_point_index=new_challenge.signage_point_index, |
| 145 | + sp_hash=new_challenge.sp_hash, |
| 146 | + pool_difficulties=[pool_difficulty], # add pool difficulty |
| 147 | + peak_height=new_challenge.peak_height, |
| 148 | + last_tx_height=new_challenge.last_tx_height, |
| 149 | + ) |
| 150 | + |
| 151 | + with patch.object(harvester_api.harvester.plot_manager, "public_keys_available", return_value=True): |
| 152 | + with patch.object(harvester_api.harvester.plot_manager, "plots", {plot_path: mock_plot_info}): |
| 153 | + with patch("chia.harvester.harvester_api.passes_plot_filter", return_value=True): |
| 154 | + with patch("chia.harvester.harvester_api.calculate_pos_challenge") as mock_calc_pos: |
| 155 | + mock_calc_pos.return_value = bytes32(b"sp_challenge" + b"0" * 20) |
| 156 | + |
| 157 | + with patch("chia.harvester.harvester_api.calculate_iterations_quality") as mock_calc_iter: |
| 158 | + mock_calc_iter.return_value = uint64(1000) |
| 159 | + |
| 160 | + with patch("chia.harvester.harvester_api.calculate_sp_interval_iters") as mock_sp_interval: |
| 161 | + mock_sp_interval.return_value = uint64(10000) |
| 162 | + |
| 163 | + with patch.object(mock_prover, "get_full_proof") as mock_get_proof: |
| 164 | + mock_proof = MagicMock(spec=ProofOfSpace) |
| 165 | + mock_get_proof.return_value = mock_proof, None |
| 166 | + |
| 167 | + result = harvester_api.new_signage_point_harvester(new_challenge, mock_peer) |
| 168 | + |
| 169 | + # verify that calculate_iterations_quality was called with pool difficulty |
| 170 | + mock_calc_iter.assert_called() |
| 171 | + call_args = mock_calc_iter.call_args[0] |
| 172 | + assert call_args[3] == uint64(500) # pool difficulty was used |
| 173 | + |
| 174 | + assert result is None |
| 175 | + |
| 176 | + |
| 177 | +@pytest.mark.anyio |
| 178 | +async def test_new_signage_point_harvester_prover_error( |
| 179 | + harvester_farmer_environment: HarvesterFarmerEnvironment, |
| 180 | +) -> None: |
| 181 | + """test error handling when prover fails""" |
| 182 | + _farmer_service, _farmer_rpc_client, harvester_service, _harvester_rpc_client, bt = harvester_farmer_environment |
| 183 | + harvester_api = harvester_service._server.api |
| 184 | + assert isinstance(harvester_api, HarvesterAPI) |
| 185 | + |
| 186 | + # create real signage point data from block tools |
| 187 | + new_challenge = create_signage_point_harvester_from_constants(bt) |
| 188 | + |
| 189 | + mock_peer = MagicMock(spec=WSChiaConnection) |
| 190 | + |
| 191 | + mock_prover = MagicMock() |
| 192 | + mock_prover.get_id.return_value = bytes32(b"2" * 32) |
| 193 | + mock_prover.get_qualities_for_challenge.side_effect = RuntimeError("test error") |
| 194 | + |
| 195 | + mock_plot_info = MagicMock(spec=PlotInfo) |
| 196 | + mock_plot_info.prover = mock_prover |
| 197 | + mock_plot_info.pool_contract_puzzle_hash = None |
| 198 | + |
| 199 | + plot_path = Path("/fake/plot.plot") |
| 200 | + |
| 201 | + with patch.object(harvester_api.harvester.plot_manager, "public_keys_available", return_value=True): |
| 202 | + with patch.object(harvester_api.harvester.plot_manager, "plots", {plot_path: mock_plot_info}): |
| 203 | + with patch("chia.harvester.harvester_api.passes_plot_filter", return_value=True): |
| 204 | + with patch("chia.harvester.harvester_api.calculate_pos_challenge") as mock_calc_pos: |
| 205 | + mock_calc_pos.return_value = bytes32(b"sp_challenge" + b"0" * 20) |
| 206 | + |
| 207 | + # should not raise exception, should handle error gracefully |
| 208 | + result = harvester_api.new_signage_point_harvester(new_challenge, mock_peer) |
| 209 | + assert result is None |
0 commit comments