Decorator for the check() method on state-creating operations.
    Makes sure that:
    * there is a __preorder__ field set, which contains the state-creating operation's associated preorder
    * there is a __table__ field set, which contains the table into which to insert this state into
    * there is a __history_id_key__ field set, which identifies the table's primary key name
    * there are no unexpired, duplicate instances of this state with this history id.
    (i.e. if we're preordering a name that had previously expired, we need to preserve its history)

def state_create(history_id_key, table_name, collision_checker, always_set=[]):
    """
    Decorator for the check() method on state-creating operations.
    Makes sure that:
    * there is a __preorder__ field set, which contains the state-creating operation's associated preorder
    * there is a __table__ field set, which contains the table into which to insert this state into
    * there is a __history_id_key__ field set, which identifies the table's primary key name
    * there are no unexpired, duplicate instances of this state with this history id.
    (i.e. if we're preordering a name that had previously expired, we need to preserve its history)
    """

    def wrap( check ):
        def wrapped_check( state_engine, nameop, block_id, checked_ops ):
            rc = check( state_engine, nameop, block_id, checked_ops )

            # pretty sure this isn't necessary any longer, but leave this is an assert just in case
            assert op_get_opcode_name(nameop['op']) in OPCODE_CREATION_OPS, 'BUG: opcode became {}'.format(nameop['op'])

            # succeeded?
            if rc:

                # ensure that there's now a __preorder__ 
                try:
                    assert '__preorder__' in nameop.keys(), "Missing __preorder__"
                except Exception, e:
                    log.exception(e)
                    log.error("FATAL: missing fields")
                    os.abort()

                # propagate __table__ and __history_id_key__
                nameop['__table__'] = table_name
                nameop['__history_id_key__'] = history_id_key
                nameop['__state_create__'] = True
                nameop['__always_set__'] = always_set

                # sanity check---we need to have the appropriate metadata for this operation
                invariant_tags = state_create_invariant_tags()
                for tag in invariant_tags:
                    assert tag in nameop, "BUG: missing invariant tag '%s'" % tag

                # sanity check---all required consensus fields must be present
                for required_field in CONSENSUS_FIELDS_REQUIRED:
                    assert required_field in nameop, 'BUG: missing required consensus field {}'.format(required_field)

                # verify no duplicates
                rc = state_check_collisions( state_engine, nameop, history_id_key, block_id, checked_ops, collision_checker )
                if rc:
                    # this is a duplicate!
                    log.debug("COLLISION on %s '%s'" % (history_id_key, nameop[history_id_key]))
                    rc = False
                else:
                    # no collision
                    rc = True

            return rc
        return wrapped_check
    return wrap