Given an operation and a new record (opcode, new_record), a point in time (block_id, vtxindex, txid), and a preorder
    record for a known record (history_id, preorder_record), create the initial name or namespace using
    the preorder and operation's data.  Record the preorder as history.

    This operation will allow the caller to update an existing name or namespace if it is being re-registered.
    It is up to the caller to verify that the name or namespace does not exist at the time of this call.

    Returns the data to snapshot on success (with all compatibility quirks preserved)
    Raise an exception on failure.

    DO NOT CALL THIS METHOD DIRECTLY.

def namedb_state_create( cur, opcode, new_record, block_id, vtxindex, txid, history_id, preorder_record, record_table, constraints_ignored=[] ):
    """
    Given an operation and a new record (opcode, new_record), a point in time (block_id, vtxindex, txid), and a preorder
    record for a known record (history_id, preorder_record), create the initial name or namespace using
    the preorder and operation's data.  Record the preorder as history.

    This operation will allow the caller to update an existing name or namespace if it is being re-registered.
    It is up to the caller to verify that the name or namespace does not exist at the time of this call.

    Returns the data to snapshot on success (with all compatibility quirks preserved)
    Raise an exception on failure.

    DO NOT CALL THIS METHOD DIRECTLY.
    """

    # sanity check: must be a state-creation operation 
    if opcode not in OPCODE_NAME_STATE_CREATIONS + OPCODE_NAMESPACE_STATE_CREATIONS or opcode in OPCODE_NAME_STATE_IMPORTS:
        log.error("FATAL: Opcode '%s' is not a state-creating operation" % opcode)
        os.abort()
    
    # did this name or namespace previously exist?
    exists = False
    prev_rec = None
    if opcode in OPCODE_NAMESPACE_STATE_CREATIONS:
        prev_rec = namedb_get_namespace(cur, history_id, block_id, include_expired=True, include_history=False)
        if prev_rec is not None:
            exists = True

    elif opcode in OPCODE_NAME_STATE_CREATIONS or opcode in OPCODE_NAME_STATE_IMPORTS:
        prev_rec = namedb_get_name(cur, history_id, block_id, include_expired=True, include_history=False)
        if prev_rec is not None:
            exists = True
    
    # the record we insert into the history table
    preorder_record_history = {}
    preorder_record_history.update(preorder_record)

    try:
        assert 'op' in preorder_record_history.keys(), 'BUG: no preorder op'
        assert 'preorder_hash' in preorder_record_history.keys(), "BUG: no preorder hash"
        assert 'block_number' in preorder_record_history.keys(), "BUG: preorder has no block number"
        assert 'vtxindex' in preorder_record_history.keys(), "BUG: preorder has no vtxindex"
        assert 'txid' in preorder_record_history.keys(), "BUG: preorder has no txid"
        assert 'burn_address' in preorder_record_history.keys(), 'BUG: preorder has no burn address'
        assert 'token_fee' in preorder_record_history.keys(), 'BUG: preorder has no token fee'
        assert 'op_fee' in preorder_record_history.keys(), 'BUG: preorder has no op fee'

        if prev_rec is not None:
            # block_number cannot change
            assert prev_rec['block_number'] == new_record['block_number'], 'BUG: trying to change block number from {} to {} for "{}"'.format(prev_rec['block_number'], new_record['block_number'], history_id)

        # convert token fee to string, always
        if isinstance(preorder_record_history['token_fee'], (int,long)):
            preorder_record_history['token_fee'] = '{}'.format(preorder_record_history['token_fee'])

    except Exception, e:
        log.exception(e)
        log.error("FATAL: no preorder hash")
        os.abort()
        
    try:
        if not exists:
            # sanity check to make sure we got valid state-creation data
            rc = namedb_state_create_sanity_check( opcode, new_record, history_id, preorder_record, record_table )
            if not rc:
                raise Exception("state-creation sanity check on '%s' failed" % opcode )

        rc = namedb_state_mutation_sanity_check( opcode, new_record )
        if not rc:
            raise Exception("State mutation sanity checks failed")

        assert 'opcode' not in new_record, 'BUG: opcode not allowed in op_data'

    except Exception, e:
        log.exception(e)
        log.error("FATAL: state-creation sanity check failed")
        os.abort()

    # save the preorder as history.
    rc = namedb_history_save(cur, preorder_record['opcode'], history_id, None, None, preorder_record['block_number'], preorder_record['vtxindex'], preorder_record['txid'], preorder_record_history)
    if not rc:
        log.error("FATAL: failed to save preorder for {} at ({}, {})".format(history_id, preorder_record['block_number'], preorder_record['vtxindex']))
        os.abort()

    # save new record
    history_data = {}
    history_data.update(new_record)
    history_data['opcode'] = opcode
    
    canonicalized_record = op_canonicalize_quirks(opcode, history_data, prev_rec)
    canonicalized_record['opcode'] = opcode

    rc = namedb_history_save(cur, opcode, history_id, history_data['address'], history_data.get('value_hash', None), block_id, vtxindex, txid, canonicalized_record)
    if not rc:
        log.error("FATAL: failed to save history for '%s' at (%s, %s)" % (history_id, block_id, vtxindex))
        os.abort()

    rc = False
    if opcode in OPCODE_NAME_STATE_CREATIONS:
        # name state transition 
        if exists:
            # update existing name entry (i.e. re-registering it)
            rc = namedb_name_update(cur, opcode, new_record, constraints_ignored=constraints_ignored)
        else:
            # insert new entry
            rc = namedb_name_insert(cur, new_record)

    elif opcode in OPCODE_NAMESPACE_STATE_CREATIONS:
        # namespace state transition 
        if exists:
            # update existing namespace entry (i.e. re-revealing it) 
            rc = namedb_namespace_update(cur, opcode, new_record, constraints_ignored=constraints_ignored)
        else:
            # insert new entry
            rc = namedb_namespace_insert(cur, new_record)
    
    if not rc:
        log.error("FATAL: opcode is not a state-creation operation")
        os.abort()

    # clear the associated preorder 
    rc = namedb_preorder_remove( cur, preorder_record['preorder_hash'] )
    if not rc:
        log.error("FATAL: failed to remove preorder")
        os.abort()

    # success!  canonicalize and preserve quirks
    return canonicalized_record