Repository: Chia-Network/chia-blockchain
File: chia/consensus/get_block_challenge.py (Python)

Code before:
            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 len(reversed_challenge_hashes) >= challenges_to_look_for:
                    break
            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 prev_tx_block(
    constants: ConsensusConstants,
    blocks: BlockRecordsProtocol,
    prev_b_hash: bytes32,
    sp_index: uint8,
    first_in_sub_slot: bool,
) -> uint32:
    if prev_b_hash == constants.GENESIS_CHALLENGE:
        return uint32(0)
    curr = blocks.block_record(prev_b_hash)
    before_slot = first_in_sub_slot
    while curr.height > 0:
        if curr.is_transaction_block and (before_slot or curr.signage_point_index < sp_index):
            break
        if curr.first_in_sub_slot:
            before_slot = True
        curr = blocks.block_record(curr.prev_hash)
    return curr.height


Review comment (suggestion):
```suggestion
    *,
    prev_b_hash: bytes32,
```
Does this make sense? It forces these parameters to be named. Is it overkill? I think the `bool` should be named at least.

Code after:
            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 len(reversed_challenge_hashes) >= challenges_to_look_for:
                    break
            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


# Returns the height of the previous transaction block up to the blocks signage point
# we use this for block validation since when the block is farmed we do not know the latest transaction block
# since a new one might be infused by the time the block is infused
def prev_tx_block(
    constants: ConsensusConstants,
    blocks: BlockRecordsProtocol,
    *,
    prev_b_hash: bytes32,
    sp_index: uint8,
    first_in_sub_slot: bool,
) -> uint32:
    if prev_b_hash == constants.GENESIS_CHALLENGE:
        return uint32(0)
    curr = blocks.block_record(prev_b_hash)
    before_slot = first_in_sub_slot
    while curr.height > 0:
        if curr.is_transaction_block and (before_slot or curr.signage_point_index < sp_index):
            break
        if curr.first_in_sub_slot:
            before_slot = True
        curr = blocks.block_record(curr.prev_hash)
    return curr.height