<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def find_transfer_consensus_hash( name_rec, block_id, vtxindex, nameop_consensus_hash ):
 """ Given a name record, find the last consensus hash set by a non-NAME_TRANSFER operation. @name_rec is the current name record, before this NAME_TRANSFER. @block_id is the current block height. @vtxindex is the relative index of this transaction in this block. @nameop_consensus_hash is the consensus hash given in the NAME_TRANSFER. This preserves compatibility from a bug prior to 0.14.x where the consensus hash from a NAME_TRANSFER is ignored in favor of the last consensus hash (if any) supplied by an operation to the affected name. This method finds that consensus hash (if present). The behavior emulated comes from the fact that in the original release of this software, the fields from a name operation fed into the block's consensus hash included the consensus hashes given in each of the a name operations' transactions. However, a quirk in the behavior of the NAME_TRANSFER-handling code prevented this from happening consistently for NAME_TRANSFERs. Specifically, the only time a NAME_TRANSFER's consensus hash was used to calculate the block's new consensus hash was if the name it affected had never been affected by a prior state transition other than a NAME_TRANSFER. If the name was affected by a prior state transition that set a consensus hash, then that prior state transition's consensus hash (not the NAME_TRANSFER's) would be used in the block consensus hash calculation. If the name was NOT affected by a prior state transition that set a consensus hash (back to the point of its last NAME_REGISTRATION), then the consensus hash fed into the block would be that from the NAME_TRANSFER itself. In practice, the only name operation that consistently sets a consensus hash is NAME_UPDATE. As for the others: * NAME_REGISTRATION sets it to None * NAME_IMPORT sets it to None * NAME_RENEWAL doesn't set it at all; it just takes what was already there * NAME_TRANSFER only sets it if there were no prior NAME_UPDATEs between now and the last NAME_REGISTRATION or NAME_IMPORT. Here are some example name histories, and the consensus hash that should be used to calculate this block's consensus hash: NAME_PREORDER, NAME_REGISTRATION, NAME_TRANSFER: nameop_consensus_hash NAME_PREORDER, NAME_REGISTRATION, NAME_TRANSFER, NAME_TRANSFER: nameop_consensus_hash NAME_PREORDER, NAME_REGISTRATION, NAME_UPDATE, NAME_TRANSFER: whatever it was from the last NAME_UPDATE NAME_PREORDER, NAME_REGISTRATION, NAME_UPDATE, NAME_TRANSFER, NAME_UPDATE, NAME_TRANSFER: whatever it was from the last NAME_UPDATE NAME_PREORDER, NAME_REGISTRATION, NAME_UPDATE, NAME_RENEWAL, NAME_TRANSFER: whatever it was from the last NAME_UPDATE NAME_PREORDER, NAME_REGISTRATION, NAME_RENEWAL, NAME_TRANSFER: nameop_consensus_hash NAME_PREORDER, NAME_REGISTRATION, NAME_TRANSFER, NAME_RENEWAL, NAME_TRANSFER: nameop_consensus_hash NAME_IMPORT, NAME_TRANSFER: nameop_consensus_hash NAME_IMPORT, NAME_UPDATE, NAME_TRANSFER whatever it was from the last NAME_UPDATE NAME_IMPORT, NAME_PREORDER, NAME_REGISTRATION, NAME_TRANSFER: nameop_consensus_hash NAME_IMPORT, NAME_TRANSFER, NAME_PREORDER, NAME_REGISTRATION, NAME_TRANSFER: nameop_consensus_hash """

    # work backwards from the last block
    for historic_block_number in reversed(sorted(name_rec['history'].keys())):
        for historic_state in reversed(name_rec['history'][historic_block_number]):
            if historic_state['block_number'] > block_id or (historic_state['block_number'] == block_id and historic_state['vtxindex'] > vtxindex):
                # from the future
                continue
            
            if historic_state['op'] in [NAME_REGISTRATION, NAME_IMPORT]:
                # out of history without finding a NAME_UPDATE
                return nameop_consensus_hash

            if historic_state['op'] == NAME_UPDATE:
                # reuse this consensus hash 
                assert historic_state['consensus_hash'] is not None, 'BUG: NAME_UPDATE did not set "consensus_hash": {}'.format(historic_state)
                return historic_state['consensus_hash']

    return nameop_consensus_hash