Given an operation and a new record (opcode, new_record), and point in time (block_id, vtxindex, txid)
    create the initial name as an import.  Does not work on namespaces.

    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_as_import( db, opcode, new_record, block_id, vtxindex, txid, history_id, record_table, constraints_ignored=[] ):
    """
    Given an operation and a new record (opcode, new_record), and point in time (block_id, vtxindex, txid)
    create the initial name as an import.  Does not work on namespaces.

    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 name, and must be an import
    if opcode not in OPCODE_NAME_STATE_IMPORTS:
        log.error("FATAL: Opcode '%s' is not a state-importing operation" % opcode)
        os.abort()

    cur = db.cursor()

    # does a previous version of this record exist?
    prior_import = namedb_get_last_name_import(cur, history_id, block_id, vtxindex)

    try:

        # sanity check to make sure we got valid state-import data
        rc = namedb_name_import_sanity_check( cur, opcode, new_record, history_id, block_id, vtxindex, prior_import, record_table )
        if not rc:
            raise Exception("state-import 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 in new_record'

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

    cur = db.cursor()
    creator_address = None
    if prior_import is None:
        # creating for the first time
        creator_address = new_record['address']

    history_data = {}
    history_data.update(new_record)
    history_data['opcode'] = opcode

    canonicalized_record = op_canonicalize_quirks(opcode, new_record, prior_import)
    canonicalized_record['opcode'] = opcode

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

    if prior_import is None:
        # creating for the first time
        cur = db.cursor()
        rc = namedb_name_insert(cur, new_record)

    else:
        # updating an existing import 
        rc = namedb_name_update(cur, opcode, new_record, constraints_ignored=constraints_ignored)

    if not rc:
        log.error("FATAL: failed to execute import operation")
        os.abort()

    # success!  canonicalize and preserve quirks
    return canonicalized_record