Repository: Chia-Network/chia-blockchain
File: chia/types/blockchain_format/proof_of_space.py (Python)

Code before:
    return ProofOfSpace(
        challenge,
        pool_public_key,
        pool_contract_puzzle_hash,
        plot_public_key,
        uint8(k),
        proof,
    )


def get_plot_id(pos: ProofOfSpace) -> bytes32:
    plot_param = pos.param()
    if plot_param.strength_v2 is not None:
        assert pos.pool_contract_puzzle_hash is not None
        return calculate_plot_id_v2(pos.pool_contract_puzzle_hash, pos.plot_public_key, uint8(plot_param.strength_v2))

    assert pos.pool_public_key is None or pos.pool_contract_puzzle_hash is None
    if pos.pool_public_key is None:
        assert pos.pool_contract_puzzle_hash is not None
        return calculate_plot_id_ph(pos.pool_contract_puzzle_hash, pos.plot_public_key)
    return calculate_plot_id_pk(pos.pool_public_key, pos.plot_public_key)


def check_plot_param(constants: ConsensusConstants, ps: PlotParam) -> bool:
    size_v1 = ps.size_v1
    strength_v2 = ps.strength_v2
    if strength_v2 is not None:
        if strength_v2 < constants.MIN_PLOT_STRENGTH:
            log.error(f"Plot strength ({strength_v2}) is lower than the minimum ({constants.MIN_PLOT_STRENGTH})")
            return False
        if strength_v2 > constants.MAX_PLOT_STRENGTH:
            log.error(f"Plot strength ({strength_v2}) is too high (max is {constants.MAX_PLOT_STRENGTH})")
            return False
        return True

    assert size_v1 is not None
    if size_v1 < constants.MIN_PLOT_SIZE_V1:
        log.error(f"Plot size ({size_v1}) is lower than the minimum ({constants.MIN_PLOT_SIZE_V1})")
        return False
    if size_v1 > constants.MAX_PLOT_SIZE_V1:
        log.error(f"Plot size ({size_v1}) is higher than the maximum ({constants.MAX_PLOT_SIZE_V1})")
        return False
    return True


def num_phase_out_epochs(constants: ConsensusConstants) -> int:
    """
    The number of phase-out epochs is always a power-of-two minus 1. i.e. it
    will also be a mask for the hash of the proof we're checking for phase-out.
    Since the hash of the proof are random bits, the simplest check is just to
    mask and compare the resulting value against the phase-out count down.

Review comment (suggestion):
I think it would be simpler to not rely on `isinstance`. something like this:
```
assert pos.pool_public_key is None or pos.pool_contract_puzzle_hash is None

# v2
if plot_param.strength_v2 is not None:
    if pos.pool_public_key is None:
        return calculate_plot_v2(pos.pool_contract_puzzle_hash, pos.plot_public_key, uint8(plot_param.strength_v2))
    return calculate_plot_v2(pos.pool_public_key, pos.plot_public_key, uint8(plot_param.strength_v2))

# v1
if pos.pool_public_key is None:
    return calculate_plot_id_ph(pos.pool_contract_puzzle_hash, pos.plot_public_key)
return calculate_plot_id_pk(pos.pool_public_key, pos.plot_public_key)
 ```

Code after:
    return ProofOfSpace(
        challenge,
        pool_public_key,
        pool_contract_puzzle_hash,
        plot_public_key,
        uint8(k),
        proof,
    )


def get_plot_id(pos: ProofOfSpace) -> bytes32:
    plot_param = pos.param()
    if pos.pool_public_key is None:
        assert pos.pool_contract_puzzle_hash is not None
        pool_pk_or_contract_ph: G1Element | bytes32 = pos.pool_contract_puzzle_hash
    else:
        assert pos.pool_contract_puzzle_hash is None
        pool_pk_or_contract_ph = pos.pool_public_key
    # V2 plots
    if plot_param.strength_v2 is not None:
        return calculate_plot_id_v2(pool_pk_or_contract_ph, pos.plot_public_key, uint8(plot_param.strength_v2))
    # V1 plots
    if isinstance(pool_pk_or_contract_ph, G1Element):
        return calculate_plot_id_pk(pool_pk_or_contract_ph, pos.plot_public_key)
    else:
        return calculate_plot_id_ph(pool_pk_or_contract_ph, pos.plot_public_key)


def check_plot_param(constants: ConsensusConstants, ps: PlotParam) -> bool:
    size_v1 = ps.size_v1
    strength_v2 = ps.strength_v2
    if strength_v2 is not None:
        if strength_v2 < constants.MIN_PLOT_STRENGTH:
            log.error(f"Plot strength ({strength_v2}) is lower than the minimum ({constants.MIN_PLOT_STRENGTH})")
            return False
        if strength_v2 > constants.MAX_PLOT_STRENGTH:
            log.error(f"Plot strength ({strength_v2}) is too high (max is {constants.MAX_PLOT_STRENGTH})")
            return False
        return True

    assert size_v1 is not None
    if size_v1 < constants.MIN_PLOT_SIZE_V1:
        log.error(f"Plot size ({size_v1}) is lower than the minimum ({constants.MIN_PLOT_SIZE_V1})")
        return False
    if size_v1 > constants.MAX_PLOT_SIZE_V1:
        log.error(f"Plot size ({size_v1}) is higher than the maximum ({constants.MAX_PLOT_SIZE_V1})")
        return False
    return True


def num_phase_out_epochs(constants: ConsensusConstants) -> int: