-- M11.4c, ADR-0121: reserve a successor identity durably for an exact mutation choice. -- Publication/result binding is added by the later mutation-publication migration. CREATE TABLE evolution.cellular_mutation_reservations ( world_id evolution.u64 NOT NULL REFERENCES evolution.life_allocators, account_id uuid NOT NULL CHECK (account_id <> '00000000-0000-0000-0000-000000000000'), action_id evolution.u64 NOT NULL CHECK (action_id > 0), region_id evolution.u64 NOT NULL, generation evolution.u64 NOT NULL CHECK (generation > 0), target_tick evolution.u64 NOT NULL CHECK (target_tick > 0), successor_identity evolution.u64 NOT NULL CHECK (successor_identity > 0), input_bytes bytea NOT NULL CHECK (octet_length(input_bytes) = 34), PRIMARY KEY (world_id, account_id, action_id), UNIQUE (world_id, account_id, action_id, region_id), UNIQUE (world_id, successor_identity), FOREIGN KEY (world_id, region_id) REFERENCES evolution.region_leases ); REVOKE ALL ON evolution.cellular_mutation_reservations FROM PUBLIC; CREATE TRIGGER cellular_mutation_reservations_immutable BEFORE UPDATE OR DELETE ON evolution.cellular_mutation_reservations FOR EACH ROW EXECUTE FUNCTION evolution.refuse_history_rewrite(); CREATE TRIGGER cellular_mutation_reservations_no_truncate BEFORE TRUNCATE ON evolution.cellular_mutation_reservations FOR EACH STATEMENT EXECUTE FUNCTION evolution.refuse_history_rewrite(); CREATE TABLE evolution.cellular_mutation_action_publications ( world_id evolution.u64 NOT NULL, region_id evolution.u64 NOT NULL, account_id uuid NOT NULL, action_id evolution.u64 NOT NULL, commit_id uuid NOT NULL, commit_digest bytea NOT NULL CHECK (octet_length(commit_digest) = 32), commit_sequence evolution.u64 NOT NULL CHECK (commit_sequence > 0), result smallint NOT NULL CHECK (result BETWEEN 0 AND 2), PRIMARY KEY (world_id, account_id, action_id), FOREIGN KEY (world_id, account_id, action_id, region_id) REFERENCES evolution.cellular_mutation_reservations(world_id, account_id, action_id, region_id), FOREIGN KEY (world_id, region_id, commit_sequence, commit_id, commit_digest) REFERENCES evolution.region_commits(world_id, region_id, commit_sequence, commit_id, commit_digest) DEFERRABLE INITIALLY DEFERRED ); REVOKE ALL ON evolution.cellular_mutation_action_publications FROM PUBLIC; CREATE TRIGGER cellular_mutation_action_publications_immutable BEFORE UPDATE OR DELETE ON evolution.cellular_mutation_action_publications FOR EACH ROW EXECUTE FUNCTION evolution.refuse_history_rewrite(); CREATE TRIGGER cellular_mutation_action_publications_no_truncate BEFORE TRUNCATE ON evolution.cellular_mutation_action_publications FOR EACH STATEMENT EXECUTE FUNCTION evolution.refuse_history_rewrite(); CREATE FUNCTION evolution.valid_cellular_mutation_input(p_input bytea) RETURNS boolean LANGUAGE plpgsql IMMUTABLE STRICT SET search_path = pg_catalog, pg_temp AS $$ BEGIN IF octet_length(p_input) <> 34 THEN RETURN false; END IF; IF evolution.manifest_uint(p_input, 0, 8) = 0 OR evolution.manifest_uint(p_input, 8, 8) = 0 OR evolution.manifest_uint(p_input, 16, 8) = 0 OR evolution.manifest_uint(p_input, 24, 8) = 0 OR evolution.manifest_uint(p_input, 0, 8) <= evolution.manifest_uint(p_input, 24, 8) OR evolution.manifest_uint(p_input, 32, 2) = 0 THEN RETURN false; END IF; RETURN true; END $$; -- Lock order is lease -> account control -> mutation reservation -> shared world allocator. -- A repeated action with different bytes is a conflict, and only one unbound -- selection per account may exist until publication records its terminal outcome. CREATE FUNCTION evolution.reserve_cellular_mutation(p_world evolution.u64, p_region evolution.u64, p_holder uuid, p_epoch evolution.u64, p_account uuid, p_generation evolution.u64, p_action evolution.u64, p_target_tick evolution.u64, p_input bytea) RETURNS TABLE(successor_identity evolution.u64, action_region evolution.u64) LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ DECLARE r evolution.cellular_mutation_reservations%ROWTYPE; allocated numeric; BEGIN IF p_action IS NULL OR p_action = 0 OR p_target_tick IS NULL OR p_target_tick = 0 OR NOT evolution.valid_cellular_mutation_input(p_input) OR evolution.manifest_uint(p_input, 0, 8) <> p_target_tick THEN RAISE EXCEPTION 'invalid cellular mutation action' USING ERRCODE = '22023'; END IF; PERFORM evolution.check_life_control(p_world, p_region, p_holder, p_epoch, p_account, p_generation); SELECT * INTO r FROM evolution.cellular_mutation_reservations x WHERE x.world_id = p_world AND x.account_id = p_account AND x.action_id = p_action; IF FOUND THEN IF r.region_id <> p_region THEN RAISE EXCEPTION 'cellular mutation belongs to another region' USING ERRCODE = 'EVL04'; END IF; IF r.target_tick <> p_target_tick OR r.input_bytes <> p_input THEN RAISE EXCEPTION 'cellular mutation action identity reused' USING ERRCODE = 'EVL07'; END IF; RETURN QUERY SELECT r.successor_identity, r.region_id; RETURN; END IF; IF EXISTS (SELECT 1 FROM evolution.cellular_mutation_reservations x LEFT JOIN evolution.cellular_mutation_action_publications p USING (world_id, account_id, action_id) WHERE x.world_id = p_world AND x.account_id = p_account AND p.action_id IS NULL) THEN RAISE EXCEPTION 'cellular mutation is already pending' USING ERRCODE = 'EVL18'; END IF; SELECT a.next_identity INTO allocated FROM evolution.life_allocators a WHERE a.world_id = p_world FOR UPDATE; IF NOT FOUND THEN RAISE EXCEPTION 'life allocator not provisioned' USING ERRCODE = 'EVL04'; END IF; IF allocated = 18446744073709551615 THEN RAISE EXCEPTION 'life identities exhausted' USING ERRCODE = 'EVL05'; END IF; PERFORM evolution.check_life_control(p_world, p_region, p_holder, p_epoch, p_account, p_generation); UPDATE evolution.life_allocators SET next_identity = allocated + 1 WHERE world_id = p_world; INSERT INTO evolution.cellular_mutation_reservations(world_id, account_id, action_id, region_id, generation, target_tick, successor_identity, input_bytes) VALUES (p_world, p_account, p_action, p_region, p_generation, p_target_tick, allocated, p_input); RETURN QUERY SELECT allocated::evolution.u64, p_region; END $$; CREATE FUNCTION evolution.load_pending_cellular_mutations(p_world evolution.u64, p_region evolution.u64, p_holder uuid, p_epoch evolution.u64) RETURNS TABLE(account_id uuid, action_id evolution.u64, generation evolution.u64, target_tick evolution.u64, successor_identity evolution.u64, input_bytes bytea) LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ BEGIN PERFORM evolution.check_region_lease(p_world, p_region, p_holder, p_epoch); RETURN QUERY SELECT r.account_id, r.action_id, r.generation, r.target_tick, r.successor_identity, r.input_bytes FROM evolution.cellular_mutation_reservations r WHERE r.world_id = p_world AND r.region_id = p_region AND NOT EXISTS (SELECT 1 FROM evolution.cellular_mutation_action_publications p WHERE p.world_id = r.world_id AND p.account_id = r.account_id AND p.action_id = r.action_id) ORDER BY r.target_tick, r.account_id, r.action_id LIMIT 257; END $$; REVOKE ALL ON FUNCTION evolution.valid_cellular_mutation_input(bytea), evolution.reserve_cellular_mutation(evolution.u64,evolution.u64,uuid,evolution.u64, uuid,evolution.u64,evolution.u64,evolution.u64,bytea), evolution.load_pending_cellular_mutations(evolution.u64,evolution.u64,uuid,evolution.u64) FROM PUBLIC; GRANT EXECUTE ON FUNCTION evolution.reserve_cellular_mutation(evolution.u64,evolution.u64,uuid,evolution.u64, uuid,evolution.u64,evolution.u64,evolution.u64,bytea), evolution.load_pending_cellular_mutations(evolution.u64,evolution.u64,uuid,evolution.u64) TO :"runtime_role"; INSERT INTO evolution.schema_migrations(version, sha256) VALUES (29, :'migration_sha_29');