Repository: Chia-Network/chia-blockchain
File: chia/_tests/core/custom_types/test_proof_of_space.py (Python)

Code before:
)
def test_verify_and_get_quality_string_v2(caplog: pytest.LogCaptureFixture, case: ProofOfSpaceCase) -> None:
    pos = ProofOfSpace(
        challenge=case.pos_challenge,
        pool_public_key=case.pool_public_key,
        pool_contract_puzzle_hash=case.pool_contract_puzzle_hash,
        plot_public_key=case.plot_public_key,
        version_and_size=case.plot_size,
        proof=b"1",
    )
    size = pos.size()
    assert size.size_v2 is not None
    assert size.size_v1 is None
    try:
        quality_string = verify_and_get_quality_string(
            pos=pos,
            constants=DEFAULT_CONSTANTS,
            original_challenge_hash=bytes32.from_hexstr(
                "0x73490e166d0b88347c37d921660b216c27316aae9a3450933d3ff3b854e5831a"
            ),
            signage_point=bytes32.from_hexstr("0x7b3e23dbd438f9aceefa9827e2c5538898189987f49b06eceb7a43067e77b531"),
            height=case.height,
        )
        assert quality_string is None
        assert len(caplog.text) == 0 if case.expected_error is None else case.expected_error in caplog.text
    except Exception as e:
        assert case.expected_error is not None
        assert case.expected_error in repr(e)


class TestProofOfSpace:
    @pytest.mark.parametrize("prefix_bits", [DEFAULT_CONSTANTS.NUMBER_ZERO_BITS_PLOT_FILTER_V1, 8, 7, 6, 5, 1, 0])
    def test_can_create_proof(self, prefix_bits: int, seeded_random: random.Random) -> None:
        """
        Tests that the change of getting a correct proof is exactly 1/target_filter.
        """
        num_trials = 100000
        success_count = 0
        target_filter = 2**prefix_bits
        for _ in range(num_trials):
            challenge_hash = bytes32.random(seeded_random)
            plot_id = bytes32.random(seeded_random)
            sp_output = bytes32.random(seeded_random)

            if passes_plot_filter(prefix_bits, plot_id, challenge_hash, sp_output):
                success_count += 1

        assert abs((success_count * target_filter / num_trials) - 1) < 0.35


Review comment (suggestion):
```suggestion
    except Exception as e:
        assert case.expected_error is not None
        assert case.expected_error in repr(e)
    else:
        assert case.expected_error is None
```

Code after:
)
def test_verify_and_get_quality_string_v2(caplog: pytest.LogCaptureFixture, case: ProofOfSpaceCase) -> None:
    pos = ProofOfSpace(
        challenge=case.pos_challenge,
        pool_public_key=case.pool_public_key,
        pool_contract_puzzle_hash=case.pool_contract_puzzle_hash,
        plot_public_key=case.plot_public_key,
        version_and_size=case.plot_size,
        proof=b"1",
    )
    size = pos.size()
    assert size.size_v2 is not None
    assert size.size_v1 is None
    try:
        quality_string = verify_and_get_quality_string(
            pos=pos,
            constants=DEFAULT_CONSTANTS,
            original_challenge_hash=bytes32.from_hexstr(
                "0x73490e166d0b88347c37d921660b216c27316aae9a3450933d3ff3b854e5831a"
            ),
            signage_point=bytes32.from_hexstr("0x7b3e23dbd438f9aceefa9827e2c5538898189987f49b06eceb7a43067e77b531"),
            height=case.height,
        )
    except Exception as e:
        assert case.expected_error is not None
        assert case.expected_error in repr(e)
    else:
        assert quality_string is None
        assert len(caplog.text) == 0 if case.expected_error is None else case.expected_error in caplog.text


class TestProofOfSpace:
    @pytest.mark.parametrize("prefix_bits", [DEFAULT_CONSTANTS.NUMBER_ZERO_BITS_PLOT_FILTER_V1, 8, 7, 6, 5, 1, 0])
    def test_can_create_proof(self, prefix_bits: int, seeded_random: random.Random) -> None:
        """
        Tests that the change of getting a correct proof is exactly 1/target_filter.
        """
        num_trials = 100000
        success_count = 0
        target_filter = 2**prefix_bits
        for _ in range(num_trials):
            challenge_hash = bytes32.random(seeded_random)
            plot_id = bytes32.random(seeded_random)
            sp_output = bytes32.random(seeded_random)

            if passes_plot_filter(prefix_bits, plot_id, challenge_hash, sp_output):
                success_count += 1

        assert abs((success_count * target_filter / num_trials) - 1) < 0.35