Verify the validity of a renewal nameop.
    * the name must be well-formed
    * the namespace must be ready
    * the request must be sent by the owner.
    * the mining fee must be high enough.
    * the name must not be expired

    Return True if accepted.
    Return False if not.

def check_renewal( state_engine, nameop, block_id, checked_ops ):
    """
    Verify the validity of a renewal nameop.
    * the name must be well-formed
    * the namespace must be ready
    * the request must be sent by the owner.
    * the mining fee must be high enough.
    * the name must not be expired

    Return True if accepted.
    Return False if not.
    """

    name = nameop['name']
    sender = nameop['sender']
    address = nameop['address']

    epoch_features = get_epoch_features(block_id)

    # address mixed into the preorder
    recipient_addr = nameop.get('recipient_address', None)
    if recipient_addr is None:
        log.warning("No registration address given")
        return False

    recipient = nameop.get('recipient', None)
    if recipient is None:
        log.warning("No recipient given")
        return False

    # name must be well-formed
    if not is_name_valid( name ):
        log.warning("Malformed name '%s'" % name)
        return False

    # pre F-day 2017, on renewal, the sender and recipient must be the same 
    # post F-day 2017, the recipient and sender can differ 
    if sender != recipient:
        if EPOCH_FEATURE_OP_RENEW_TRANSFER_UPDATE not in epoch_features:
            log.warning("Sender '%s' is not the recipient '%s'" % (sender, recipient))
            return False 

        else:
            log.debug("Transferring '{}' to '{}'".format(sender, recipient))

    if recipient_addr != address:
        if EPOCH_FEATURE_OP_RENEW_TRANSFER_UPDATE not in epoch_features:
            log.warning("Sender address '%s' is not the recipient address '%s'" % (address, recipient_addr))
            return False

        else:
            log.debug("Transferring '{}' to '{}'".format(address, recipient_addr))
                
    name_fee = None
    namespace = None
    preorder_hash = None
    preorder_block_number = None 
    name_block_number = None
    opcode = nameop['opcode']

    # name must be well-formed
    if not is_b40( name ) or "+" in name or name.count(".") > 1:
        log.warning("Malformed name '%s': non-base-38 characters" % name)
        return False

    # name must not be revoked
    if state_engine.is_name_revoked( name ):
        log.warning("Name '%s' is revoked" % name)
        return False

    namespace_id = get_namespace_from_name( name )

    # namespace must exist and be ready
    if not state_engine.is_namespace_ready( namespace_id ):
        log.warning("Namespace '%s' is not ready" % namespace_id)
        return False

    # get namespace...
    namespace = state_engine.get_namespace( namespace_id )

    # cannot exceed quota
    num_names = get_num_names_owned( state_engine, checked_ops, recipient )
    if num_names >= MAX_NAMES_PER_SENDER:
        log.warning("Recipient '%s' has exceeded quota" % recipient)
        return False

    # name must be registered already 
    if not state_engine.is_name_registered( name ):
        log.warning("Name '%s' is not registered" % name)
        return False

    # pre F-day 2017: name must be owned by the recipient already
    # post F-day 2017: doesn't matter
    if not state_engine.is_name_owner( name, recipient ):
        if EPOCH_FEATURE_OP_RENEW_TRANSFER_UPDATE not in epoch_features:
            log.warning("Renew: Name '%s' not owned by recipient %s" % (name, recipient))
            return False

    # name must be owned by the sender
    if not state_engine.is_name_owner( name, sender ):
        log.warning("Renew: Name '%s' not owned by sender %s" % (name, sender))
        return False

    # fee borne by the renewal
    if not 'op_fee' in nameop or nameop['op_fee'] is None:
        log.warning("Name '%s' renewal did not pay the name registration fee" % (name))
        return False
   
    prev_name_rec = state_engine.get_name( name )
    
    first_registered = prev_name_rec['first_registered']
    preorder_block_number = prev_name_rec['preorder_block_number']
    name_block_number = prev_name_rec['block_number']
    name_fee = nameop['op_fee']
    preorder_hash = prev_name_rec['preorder_hash']
    value_hash = prev_name_rec['value_hash']        # use previous name record's value hash by default
    burn_address = nameop['burn_address']

    fee_block_id = block_id         # fee for this name is paid now
    fee_vtxindex = nameop['vtxindex']   # fee for this name is paid now
    token_address = address         # current owner pays tokens to renew

    # check name payment, but note that this does not query the account if we're paying with tokens (this just makes sure the nameop is well-formed)
    payment_res = check_payment(state_engine, "NAME_RENEWAL", nameop, fee_block_id, token_address, burn_address, name_fee, block_id)
    if not payment_res['status']:
        log.warning("Name '{}' did not receive the appropriate payment".format(name))
        return False
 
    # extract payment info
    token_fee = payment_res['tokens_paid']
    token_units = payment_res['token_units']

    if token_units == 'BTC':
        # paid in BTC
        assert token_fee == name_fee, 'Tokens paid in BTC does not match tokens paid in transaction ({} != {})'.format(token_fee, name_fee)

        # make sure we did NOT pay in stacks
        res = get_stacks_payment(state_engine, nameop, "NAME_RENEWAL")
        assert not res['status'], 'BUG: paid in both BTC and Stacks'

        # no Stacks will be spent
        state_transition_put_account_payment_info(nameop, None, None, None)
        token_fee = 0

    else:
        if EPOCH_FEATURE_NAMEOPS_COST_TOKENS not in epoch_features:
            # can't do this---tokens aren't active yet
            log.warning('Tried to pay for {} in Stacks before Stacks exist'.format(name))
            return False

        # paid in tokens.  need to debit if this was a renewal 
        # charge the price of this name when we commit this state-transition
        assert token_fee is not None
        assert token_units == TOKEN_TYPE_STACKS, 'BUG: token units must be BTC or STACKS'

        # make sure we did, in fact, pay in Stacks
        res = get_stacks_payment(state_engine, nameop, 'NAME_RENEWAL')
        assert res['status'], 'BUG: paid in Stacks but did not'
        assert res['token_units'] == token_units
        assert res['tokens_paid'] == token_fee

        # make sure the account in question has enough balance
        account_info = state_engine.get_account(token_address, token_units)
        if account_info is None:
            # no account!
            log.warning("Name buyer {} does not have an account for {}".format(token_address, token_units))
            return False

        # can this account afford it?
        account_balance = state_engine.get_account_balance(account_info)
        if account_balance < token_fee:
            # not enough balance
            log.warning("Address {} does not have enough {} tokens for {} (need at least {}, but only have {})".format(token_address, token_units, name, token_fee, account_balance))
            return False

        # can afford it! debit this account
        state_transition_put_account_payment_info(nameop, token_address, token_units, token_fee)

    # if we're in an epoch that allows us to include a value hash in the renewal, and one is given, then set it 
    # instead of the previous name record's value hash.
    if EPOCH_FEATURE_OP_RENEW_TRANSFER_UPDATE in epoch_features:
        if nameop.has_key('value_hash') and nameop['value_hash'] is not None:
            log.debug("Adding value hash {} for name '{}'".format(nameop['value_hash'], nameop['name']))
            value_hash = nameop['value_hash']

    nameop['op_fee'] = name_fee
    nameop['token_fee'] = '{}'.format(token_fee)      # NOTE: use a string to prevent integer overflow
    nameop['preorder_hash'] = preorder_hash
    nameop['namespace_block_number'] = namespace['block_number']
    nameop['first_registered'] = first_registered
    nameop['preorder_block_number'] = preorder_block_number
    nameop['block_number'] = name_block_number
    nameop['value_hash'] = value_hash

    # renewal
    nameop['last_renewed'] = block_id

    # propagate new sender information
    nameop['sender'] = nameop['recipient']
    nameop['address'] = nameop['recipient_address']
    nameop['sender_pubkey'] = prev_name_rec['sender_pubkey']

    del nameop['recipient']
    del nameop['recipient_address']
    del nameop['burn_address']

    # renewal!
    return True