Files
aevolution/migrations/0047_cellular_migration_freeze.sql
dingo c625a4cb68
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m24s
Foundation / checks (network) (push) Successful in 20m44s
Foundation / checks (client) (push) Successful in 22m3s
Foundation / checks (tsan) (push) Successful in 23m1s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 24m7s
Foundation / checks (postgres) (push) Canceled after 24m3s
M12.4: retain migration source freezes and explicit aborts
2026-10-18 09:08:50 -04:00

208 lines
14 KiB
PL/PgSQL

-- ADR-0147: retain exact biological preparation; source owns all stock until paired commit.
CREATE TABLE evolution.cellular_migration_preparations (
request_id uuid PRIMARY KEY,
world_id evolution.u64 NOT NULL,
source_region evolution.u64 NOT NULL,
destination_region evolution.u64 NOT NULL CHECK (destination_region <> source_region),
source_commit uuid NOT NULL REFERENCES evolution.region_commits(commit_id),
destination_commit uuid NOT NULL REFERENCES evolution.region_commits(commit_id),
trace bytea NOT NULL CHECK (octet_length(trace) BETWEEN 272 AND 2312),
prepared_holder uuid NOT NULL,
prepared_epoch evolution.u64 NOT NULL CHECK (prepared_epoch > 0),
FOREIGN KEY(world_id,source_region) REFERENCES evolution.region_leases,
FOREIGN KEY(world_id,destination_region) REFERENCES evolution.region_leases
);
CREATE TABLE evolution.cellular_migration_aborts (
request_id uuid PRIMARY KEY REFERENCES evolution.cellular_migration_preparations,
holder uuid NOT NULL, epoch evolution.u64 NOT NULL CHECK (epoch > 0)
);
CREATE TABLE evolution.cellular_migration_pending (
world_id evolution.u64 NOT NULL, region_id evolution.u64 NOT NULL,
request_id uuid UNIQUE NOT NULL REFERENCES evolution.cellular_migration_preparations,
PRIMARY KEY(world_id,region_id)
);
REVOKE ALL ON evolution.cellular_migration_preparations,evolution.cellular_migration_aborts,
evolution.cellular_migration_pending FROM PUBLIC;
CREATE TRIGGER cellular_migration_preparations_immutable BEFORE UPDATE OR DELETE ON evolution.cellular_migration_preparations
FOR EACH ROW EXECUTE FUNCTION evolution.refuse_history_rewrite();
CREATE TRIGGER cellular_migration_preparations_no_truncate BEFORE TRUNCATE ON evolution.cellular_migration_preparations
FOR EACH STATEMENT EXECUTE FUNCTION evolution.refuse_history_rewrite();
CREATE TRIGGER cellular_migration_aborts_immutable BEFORE UPDATE OR DELETE ON evolution.cellular_migration_aborts
FOR EACH ROW EXECUTE FUNCTION evolution.refuse_history_rewrite();
CREATE TRIGGER cellular_migration_aborts_no_truncate BEFORE TRUNCATE ON evolution.cellular_migration_aborts
FOR EACH STATEMENT EXECUTE FUNCTION evolution.refuse_history_rewrite();
CREATE FUNCTION evolution.check_cellular_migration_trace(t bytea) RETURNS void
LANGUAGE plpgsql IMMUTABLE SET search_path = pg_catalog, pg_temp AS $$
DECLARE n integer; members numeric; previous numeric := 0; life numeric;
hash numeric := 14695981039346656037; ids bytea[];
BEGIN
IF t IS NULL OR octet_length(t) < 272 OR octet_length(t) > 2312 THEN
RAISE EXCEPTION 'invalid migration trace length' USING ERRCODE = '22023';
END IF;
n := octet_length(t); members := evolution.manifest_uint(t,248,4);
ids := ARRAY[substring(t FROM 73 FOR 16),substring(t FROM 121 FOR 16),
substring(t FROM 145 FOR 16),substring(t FROM 193 FOR 16)];
IF substring(t FROM 1 FOR 8) <> '\x4145564d49473031'::bytea OR evolution.manifest_uint(t,8,4) <> 1
OR evolution.manifest_uint(t,12,4) NOT IN (1,2) OR evolution.manifest_uint(t,16,8) = 0
OR evolution.manifest_uint(t,24,8) = 0 OR evolution.manifest_uint(t,32,8) = 0
OR evolution.manifest_uint(t,40,8) = 0 OR substring(t FROM 49 FOR 16) = decode(repeat('00',16),'hex')
OR evolution.manifest_uint(t,64,8) NOT BETWEEN 1 AND 2147483647
OR evolution.manifest_uint(t,136,8) NOT BETWEEN 1 AND 2147483647
OR evolution.manifest_uint(t,64,8) = evolution.manifest_uint(t,136,8)
OR substring(t FROM 89 FOR 32) = decode(repeat('00',32),'hex')
OR substring(t FROM 161 FOR 32) = decode(repeat('00',32),'hex')
OR substring(t FROM 209 FOR 40) = decode(repeat('00',40),'hex')
OR members NOT BETWEEN 1 AND 256 OR n <> 264 + 8 * members
OR evolution.manifest_uint(t,252,4) <> 0
OR (SELECT count(DISTINCT x) FROM unnest(ids) x) <> 4
OR decode(repeat('00',16),'hex') = ANY(ids) THEN
RAISE EXCEPTION 'invalid migration trace shape' USING ERRCODE = '22023';
END IF;
FOR i IN 0..members::integer-1 LOOP
life := evolution.manifest_uint(t,256+8*i,8);
IF life <= previous OR life < 9223372036854775808
OR trunc((life-9223372036854775808)/4294967296) = 0 OR mod(life,4294967296) = 0 THEN
RAISE EXCEPTION 'invalid migration member' USING ERRCODE = '22023';
END IF;
previous := life;
END LOOP;
-- Exact bounded FNV-1a64, corruption detection only; manifests/payloads use SHA-256.
FOR i IN 0..n-9 LOOP
hash := mod((hash-mod(hash,256)+(mod(hash,256)::integer # get_byte(t,i)))*1099511628211,
18446744073709551616);
END LOOP;
IF hash <> evolution.manifest_uint(t,n-8,8) THEN
RAISE EXCEPTION 'migration trace checksum differs' USING ERRCODE = '22023';
END IF;
END $$;
REVOKE ALL ON FUNCTION evolution.check_cellular_migration_trace(bytea) FROM PUBLIC;
CREATE FUNCTION evolution.cellular_migration_record(w evolution.u64,r evolution.u64,id uuid)
RETURNS TABLE(trace bytea,source_manifest bytea,destination_manifest bytea,aborted boolean)
LANGUAGE sql STABLE SET search_path = pg_catalog, pg_temp AS $$
SELECT p.trace,s.manifest,d.manifest,EXISTS(SELECT 1 FROM evolution.cellular_migration_aborts a WHERE a.request_id=p.request_id)
FROM evolution.cellular_migration_preparations p
JOIN evolution.region_commits s ON s.commit_id=p.source_commit
JOIN evolution.region_commits d ON d.commit_id=p.destination_commit
WHERE p.world_id=w AND p.source_region=r AND p.request_id=id
$$;
REVOKE ALL ON FUNCTION evolution.cellular_migration_record(evolution.u64,evolution.u64,uuid) FROM PUBLIC;
CREATE FUNCTION evolution.prepare_cellular_migration_freeze(w evolution.u64,r evolution.u64,holder uuid,epoch evolution.u64,t bytea)
RETURNS TABLE(trace bytea,source_manifest bytea,destination_manifest bytea,aborted boolean)
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
DECLARE id uuid; src uuid; dst uuid; destination evolution.u64; h evolution.region_heads%ROWTYPE;
sm bytea; dm bytea; s evolution.checkpoint_manifest; d evolution.checkpoint_manifest;
old evolution.cellular_migration_preparations%ROWTYPE;
bundle10 constant bytea := '\x080000000700000002000000010000000100000004000000030000000200000001000000';
BEGIN
PERFORM evolution.check_cellular_migration_trace(t);
IF evolution.manifest_uint(t,16,8) <> w OR evolution.manifest_uint(t,64,8) <> r THEN
RAISE EXCEPTION 'foreign migration source' USING ERRCODE = '22023';
END IF;
id := encode(substring(t FROM 49 FOR 16),'hex')::uuid;
src := encode(substring(t FROM 73 FOR 16),'hex')::uuid;
dst := encode(substring(t FROM 145 FOR 16),'hex')::uuid;
destination := evolution.manifest_uint(t,136,8);
PERFORM evolution.check_region_lease(w,r,holder,epoch);
SELECT * INTO old FROM evolution.cellular_migration_preparations p WHERE p.request_id=id;
IF FOUND THEN
IF old.world_id<>w OR old.source_region<>r OR old.trace<>t THEN
RAISE EXCEPTION 'migration request reused' USING ERRCODE = 'EVL07';
END IF;
RETURN QUERY SELECT * FROM evolution.cellular_migration_record(w,r,id);
RETURN;
END IF;
SELECT * INTO h FROM evolution.region_heads x WHERE x.world_id=w AND x.region_id=r FOR UPDATE;
IF NOT FOUND OR h.commit_id<>src OR h.digest<>substring(t FROM 89 FOR 32)
OR h.end_tick<>evolution.manifest_uint(t,32,8) THEN
RAISE EXCEPTION 'migration source moved' USING ERRCODE = 'EVL03';
END IF;
SELECT c.manifest INTO sm FROM evolution.region_commits c WHERE c.commit_id=src;
-- Destination remains unowned by this workflow. A later stage must recheck its head.
SELECT c.manifest INTO dm FROM evolution.region_heads x JOIN evolution.region_commits c ON c.commit_id=x.commit_id
WHERE x.world_id=w AND x.region_id=destination AND x.commit_id=dst AND x.digest=substring(t FROM 161 FOR 32);
IF dm IS NULL THEN RAISE EXCEPTION 'migration destination moved' USING ERRCODE = 'EVL03'; END IF;
s := evolution.parse_checkpoint_manifest(sm); d := evolution.parse_checkpoint_manifest(dm);
IF s.profile NOT IN (4,5) OR d.profile<>s.profile OR s.versions<>bundle10 OR d.versions<>bundle10
OR s.commit_sequence=0 OR d.commit_sequence=0 OR s.frame_id<>d.frame_id
OR s.frame_id<>evolution.manifest_uint(t,24,8) OR s.end_tick<>d.end_tick
OR s.tick_microseconds<>d.tick_microseconds
OR s.profile<>(CASE evolution.manifest_uint(t,12,4) WHEN 1 THEN 4 ELSE 5 END) THEN
RAISE EXCEPTION 'incompatible migration cuts' USING ERRCODE = 'EVL06';
END IF;
IF EXISTS(SELECT 1 FROM evolution.cellular_migration_pending p WHERE p.world_id=w AND p.region_id=r)
OR EXISTS(SELECT 1 FROM evolution.population_interval_pending p WHERE p.world_id=w AND p.region_id=r)
OR EXISTS(SELECT 1 FROM evolution.cellular_mutation_reservations x WHERE x.world_id=w AND x.region_id=r
AND NOT EXISTS(SELECT 1 FROM evolution.cellular_mutation_action_publications a
WHERE a.world_id=x.world_id AND a.account_id=x.account_id AND a.action_id=x.action_id))
OR EXISTS(SELECT 1 FROM evolution.life_reservations x WHERE x.world_id=w AND x.region_id=r AND x.rejection=0 AND x.ended_commit IS NULL
AND NOT EXISTS(SELECT 1 FROM evolution.primitive_life_bindings b
WHERE b.world_id=x.world_id AND b.account_id=x.account_id AND b.request_id=x.request_id)) THEN
RAISE EXCEPTION 'migration source has pending work' USING ERRCODE = 'EVL18';
END IF;
INSERT INTO evolution.cellular_migration_preparations VALUES(id,w,r,destination,src,dst,t,holder,epoch);
INSERT INTO evolution.cellular_migration_pending VALUES(w,r,id);
RETURN QUERY SELECT * FROM evolution.cellular_migration_record(w,r,id);
END $$;
REVOKE ALL ON FUNCTION evolution.prepare_cellular_migration_freeze(evolution.u64,evolution.u64,uuid,evolution.u64,bytea) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION evolution.prepare_cellular_migration_freeze(evolution.u64,evolution.u64,uuid,evolution.u64,bytea) TO :"runtime_role";
CREATE FUNCTION evolution.abort_cellular_migration_freeze(w evolution.u64,r evolution.u64,holder uuid,epoch evolution.u64,id uuid)
RETURNS TABLE(trace bytea,source_manifest bytea,destination_manifest bytea,aborted boolean)
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
DECLARE p evolution.cellular_migration_preparations%ROWTYPE;
BEGIN
PERFORM evolution.check_region_lease(w,r,holder,epoch);
SELECT * INTO p FROM evolution.cellular_migration_preparations x WHERE x.request_id=id AND x.world_id=w AND x.source_region=r;
IF NOT FOUND THEN RAISE EXCEPTION 'unknown source migration' USING ERRCODE = 'EVL04'; END IF;
IF NOT EXISTS(SELECT 1 FROM evolution.cellular_migration_aborts a WHERE a.request_id=id) THEN
IF NOT EXISTS(SELECT 1 FROM evolution.cellular_migration_pending x WHERE x.world_id=w AND x.region_id=r AND x.request_id=id)
OR NOT EXISTS(SELECT 1 FROM evolution.region_heads h WHERE h.world_id=w AND h.region_id=r AND h.commit_id=p.source_commit
AND h.digest=substring(p.trace FROM 89 FOR 32) AND h.end_tick=evolution.manifest_uint(p.trace,32,8)) THEN
RAISE EXCEPTION 'migration freeze inconsistent' USING ERRCODE = 'EVL03';
END IF;
INSERT INTO evolution.cellular_migration_aborts VALUES(id,holder,epoch);
DELETE FROM evolution.cellular_migration_pending x WHERE x.world_id=w AND x.region_id=r AND x.request_id=id;
END IF;
RETURN QUERY SELECT * FROM evolution.cellular_migration_record(w,r,id);
END $$;
REVOKE ALL ON FUNCTION evolution.abort_cellular_migration_freeze(evolution.u64,evolution.u64,uuid,evolution.u64,uuid) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION evolution.abort_cellular_migration_freeze(evolution.u64,evolution.u64,uuid,evolution.u64,uuid) TO :"runtime_role";
CREATE FUNCTION evolution.load_cellular_migration_freeze(w evolution.u64,r evolution.u64,holder uuid,epoch evolution.u64)
RETURNS TABLE(trace bytea,source_manifest bytea,destination_manifest bytea,aborted boolean)
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
DECLARE p evolution.cellular_migration_preparations%ROWTYPE;
BEGIN
PERFORM evolution.check_region_lease(w,r,holder,epoch);
SELECT a.* INTO p FROM evolution.cellular_migration_pending x JOIN evolution.cellular_migration_preparations a ON a.request_id=x.request_id
WHERE x.world_id=w AND x.region_id=r;
IF NOT FOUND THEN RETURN; END IF;
IF p.world_id<>w OR p.source_region<>r
OR EXISTS(SELECT 1 FROM evolution.cellular_migration_aborts a WHERE a.request_id=p.request_id)
OR NOT EXISTS(SELECT 1 FROM evolution.region_heads h WHERE h.world_id=w AND h.region_id=r AND h.commit_id=p.source_commit
AND h.digest=substring(p.trace FROM 89 FOR 32) AND h.end_tick=evolution.manifest_uint(p.trace,32,8)) THEN
RAISE EXCEPTION 'migration freeze inconsistent' USING ERRCODE = 'XX000';
END IF;
RETURN QUERY SELECT * FROM evolution.cellular_migration_record(w,r,p.request_id);
END $$;
REVOKE ALL ON FUNCTION evolution.load_cellular_migration_freeze(evolution.u64,evolution.u64,uuid,evolution.u64) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION evolution.load_cellular_migration_freeze(evolution.u64,evolution.u64,uuid,evolution.u64) TO :"runtime_role";
CREATE FUNCTION evolution.refuse_frozen_cellular_work() RETURNS trigger
LANGUAGE plpgsql SET search_path = pg_catalog, pg_temp AS $$
BEGIN
IF EXISTS(SELECT 1 FROM evolution.cellular_migration_pending p WHERE p.world_id=NEW.world_id AND p.region_id=NEW.region_id) THEN
RAISE EXCEPTION 'cellular source frozen for migration' USING ERRCODE = 'EVL18';
END IF;
RETURN NEW;
END $$;
REVOKE ALL ON FUNCTION evolution.refuse_frozen_cellular_work() FROM PUBLIC;
CREATE TRIGGER region_cellular_migration_freeze BEFORE INSERT ON evolution.region_commits FOR EACH ROW EXECUTE FUNCTION evolution.refuse_frozen_cellular_work();
CREATE TRIGGER interval_cellular_migration_freeze BEFORE INSERT ON evolution.population_interval_admissions FOR EACH ROW EXECUTE FUNCTION evolution.refuse_frozen_cellular_work();
CREATE TRIGGER mutation_cellular_migration_freeze BEFORE INSERT ON evolution.cellular_mutation_reservations FOR EACH ROW EXECUTE FUNCTION evolution.refuse_frozen_cellular_work();
CREATE TRIGGER birth_cellular_migration_freeze BEFORE INSERT ON evolution.life_reservations FOR EACH ROW EXECUTE FUNCTION evolution.refuse_frozen_cellular_work();
INSERT INTO evolution.schema_migrations(version,sha256) VALUES(47,:'migration_sha_47');