def get_block_challenge(constants: ConsensusConstants, header_block: Union[(UnfinishedHeaderBlock, UnfinishedBlock, HeaderBlock, FullBlock)], blocks: BlockchainInterface, genesis_block: bool, overflow: bool, skip_overflow_last_ss_validation: bool):
    if (len(header_block.finished_sub_slots) > 0):
        if overflow:
            if skip_overflow_last_ss_validation:
                challenge: bytes32 = header_block.finished_sub_slots[(- 1)].challenge_chain.get_hash()
            else:
                challenge = header_block.finished_sub_slots[(- 1)].challenge_chain.challenge_chain_end_of_slot_vdf.challenge
        else:
            challenge = header_block.finished_sub_slots[(- 1)].challenge_chain.get_hash()
    elif genesis_block:
        challenge = constants.GENESIS_CHALLENGE
    else:
        if overflow:
            if skip_overflow_last_ss_validation:
                challenges_to_look_for = 1
            else:
                challenges_to_look_for = 2
        else:
            challenges_to_look_for = 1
        reversed_challenge_hashes: List[bytes32] = []
        curr: BlockRecord = blocks.block_record(header_block.prev_header_hash)
        while (len(reversed_challenge_hashes) < challenges_to_look_for):
            if curr.first_in_sub_slot:
                assert (curr.finished_challenge_slot_hashes is not None)
                reversed_challenge_hashes += reversed(curr.finished_challenge_slot_hashes)
            if (curr.height == 0):
                assert (curr.finished_challenge_slot_hashes is not None)
                assert (len(curr.finished_challenge_slot_hashes) > 0)
                break
            curr = blocks.block_record(curr.prev_hash)
        challenge = reversed_challenge_hashes[(challenges_to_look_for - 1)]
    return challenge
def get_block_challenge(constants: ConsensusConstants, header_block: Union[(UnfinishedHeaderBlock, UnfinishedBlock, HeaderBlock, FullBlock)], blocks: BlockchainInterface, genesis_block: bool, overflow: bool, skip_overflow_last_ss_validation: bool):
    if (len(header_block.finished_sub_slots) > 0):
        if overflow:
            if skip_overflow_last_ss_validation:
                challenge: bytes32 = header_block.finished_sub_slots[(- 1)].challenge_chain.get_hash()
            else:
                challenge = header_block.finished_sub_slots[(- 1)].challenge_chain.challenge_chain_end_of_slot_vdf.challenge
        else:
            challenge = header_block.finished_sub_slots[(- 1)].challenge_chain.get_hash()
    elif genesis_block:
        challenge = constants.GENESIS_CHALLENGE
    else:
        if overflow:
            if skip_overflow_last_ss_validation:
                challenges_to_look_for = 1
            else:
                challenges_to_look_for = 2
        else:
            challenges_to_look_for = 1
        reversed_challenge_hashes: List[bytes32] = []
        curr: BlockRecord = blocks.block_record(header_block.prev_header_hash)
        while (len(reversed_challenge_hashes) < challenges_to_look_for):
            if curr.first_in_sub_slot:
                assert (curr.finished_challenge_slot_hashes is not None)
                reversed_challenge_hashes += reversed(curr.finished_challenge_slot_hashes)
            if (curr.height == 0):
                assert (curr.finished_challenge_slot_hashes is not None)
                assert (len(curr.finished_challenge_slot_hashes) > 0)
                break
            curr = blocks.block_record(curr.prev_hash)
        challenge = reversed_challenge_hashes[(challenges_to_look_for - 1)]
    return challenge