-- ADR-0161: exact bundle 10/trace 1 and bundle 11/trace 2 migration pairs. -- No new table or normalized retained bytes. Existing fenced transactions remain. CREATE OR REPLACE 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 NOT ((substring(t FROM 1 FOR 8) = '\x4145564d49473031'::bytea AND evolution.manifest_uint(t,8,4) = 1) OR (substring(t FROM 1 FOR 8) = '\x4145564d49473032'::bytea AND evolution.manifest_uint(t,8,4) = 2)) 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, :"runtime_role"; CREATE OR REPLACE 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; bundle bytea; bundle10 constant bytea := '\x080000000700000002000000010000000100000004000000030000000200000001000000'; BEGIN PERFORM evolution.check_cellular_migration_trace(t); bundle := CASE evolution.manifest_uint(t,8,4) WHEN 1 THEN bundle10 ELSE '\x090000000700000002000000010000000100000004000000030000000200000001000000'::bytea END; 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<>bundle OR d.versions<>bundle 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 OR REPLACE FUNCTION evolution.publish_cellular_migration(id uuid,sm bytea,dm bytea,sh uuid,se evolution.u64,dh uuid,de evolution.u64) RETURNS TABLE(trace bytea,source_predecessor bytea,destination_predecessor bytea,source_result bytea,destination_result bytea,ghost_sha256 bytea,ghost_bytes evolution.u64) LANGUAGE plpgsql SECURITY DEFINER SET search_path=pg_catalog,pg_temp AS $$ DECLARE p evolution.cellular_migration_preparations%ROWTYPE; g evolution.cellular_migration_stages%ROWTYPE; old evolution.cellular_migration_commits%ROWTYPE; s evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(sm); d evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(dm); predecessor evolution.checkpoint_manifest; h evolution.region_heads%ROWTYPE; m bytea; c evolution.checkpoint_manifest; result_digest bytea; bundle bytea := '\x080000000700000002000000010000000100000004000000030000000200000001000000'; zero32 constant bytea := decode(repeat('00',32),'hex'); BEGIN SELECT * INTO p FROM evolution.cellular_migration_preparations x WHERE x.request_id=id; IF NOT FOUND THEN RAISE EXCEPTION 'unknown migration preparation' USING ERRCODE='EVL04'; END IF; PERFORM evolution.check_cellular_migration_trace(p.trace); IF evolution.manifest_uint(p.trace,8,4)=2 THEN bundle := '\x090000000700000002000000010000000100000004000000030000000200000001000000'::bytea; END IF; IF s.world_id<>p.world_id OR d.world_id<>p.world_id OR s.region_id<>p.source_region OR d.region_id<>p.destination_region OR s.commit_id<>encode(substring(p.trace FROM 121 FOR 16),'hex')::uuid OR d.commit_id<>encode(substring(p.trace FROM 193 FOR 16),'hex')::uuid THEN RAISE EXCEPTION 'foreign migration results' USING ERRCODE='22023'; END IF; PERFORM 1 FROM evolution.region_leases x WHERE x.world_id=p.world_id AND x.region_id IN (p.source_region,p.destination_region) ORDER BY x.region_id FOR UPDATE; PERFORM evolution.check_region_lease(p.world_id,p.source_region,sh,se); PERFORM evolution.check_region_lease(p.world_id,p.destination_region,dh,de); SELECT * INTO old FROM evolution.cellular_migration_commits x WHERE x.request_id=id; IF FOUND THEN IF NOT EXISTS(SELECT 1 FROM evolution.region_commits x WHERE x.commit_id=old.source_result AND x.manifest=sm) OR NOT EXISTS(SELECT 1 FROM evolution.region_commits x WHERE x.commit_id=old.destination_result AND x.manifest=dm) THEN RAISE EXCEPTION 'migration commit request reused' USING ERRCODE='EVL07'; END IF; RETURN QUERY SELECT * FROM evolution.cellular_migration_commit_record(id); RETURN; END IF; SELECT * INTO g FROM evolution.cellular_migration_stages x WHERE x.request_id=id; IF NOT FOUND OR EXISTS(SELECT 1 FROM evolution.cellular_migration_aborts x WHERE x.request_id=id) OR NOT EXISTS(SELECT 1 FROM evolution.cellular_migration_pending x WHERE x.world_id=p.world_id AND x.region_id=p.source_region AND x.request_id=id) OR NOT EXISTS(SELECT 1 FROM evolution.cellular_migration_destination_pending x WHERE x.world_id=p.world_id AND x.region_id=p.destination_region AND x.request_id=id) THEN RAISE EXCEPTION 'migration not staged or was aborted' USING ERRCODE='EVL18'; END IF; IF s.holder<>sh OR s.epoch<>se OR d.holder<>dh OR d.epoch<>de OR d.payload_sha256<>g.ghost_sha256 OR d.payload_bytes<>g.ghost_bytes THEN RAISE EXCEPTION 'migration writer or staged ghost differs' USING ERRCODE='EVL07'; END IF; FOREACH m IN ARRAY ARRAY[sm,dm] LOOP c := evolution.parse_checkpoint_manifest(m); SELECT * INTO h FROM evolution.region_heads x WHERE x.world_id=c.world_id AND x.region_id=c.region_id FOR UPDATE; IF NOT FOUND OR h.commit_sequence=18446744073709551615 OR c.commit_sequence<>h.commit_sequence+1 OR c.predecessor_commit<>h.commit_id OR c.predecessor_digest<>h.digest OR c.begin_tick<>h.end_tick OR c.end_tick<>h.end_tick OR c.end_tick<>evolution.manifest_uint(p.trace,32,8) OR h.commit_id<>(CASE WHEN c.region_id=p.source_region THEN p.source_commit ELSE p.destination_commit END) THEN RAISE EXCEPTION 'migration endpoint head moved' USING ERRCODE='EVL03'; END IF; predecessor := evolution.parse_checkpoint_manifest((SELECT x.manifest FROM evolution.region_commits x WHERE x.commit_id=h.commit_id)); IF c.profile NOT IN (4,5) OR c.profile<>predecessor.profile OR c.profile<>s.profile OR c.versions<>bundle OR c.versions<>predecessor.versions OR c.frame_id<>predecessor.frame_id OR c.tick_microseconds<>predecessor.tick_microseconds OR c.frame_id<>evolution.manifest_uint(p.trace,24,8) OR c.frame_id<>(SELECT x.frame_id FROM evolution.region_leases x WHERE x.world_id=c.world_id AND x.region_id=c.region_id) OR evolution.manifest_uint(m,228,4)<>0 OR evolution.manifest_uint(m,232,4)<>(CASE c.profile WHEN 4 THEN 4 ELSE 5 END) OR evolution.manifest_uint(m,236,4)<>1 OR c.trace_bytes<>0 OR c.trace_sha256<>zero32 OR c.decision_count<>0 OR c.decisions_sha256<>zero32 OR NOT EXISTS(SELECT 1 FROM evolution.retained_builds x WHERE x.build_identity=c.build_identity) OR EXISTS(SELECT 1 FROM evolution.population_interval_pending x WHERE x.world_id=c.world_id AND x.region_id=c.region_id) THEN RAISE EXCEPTION 'unsupported paired migration result' USING ERRCODE='EVL06'; END IF; END LOOP; INSERT INTO evolution.cellular_migration_commits VALUES(id,s.commit_id,d.commit_id); DELETE FROM evolution.cellular_migration_pending x WHERE x.request_id=id; DELETE FROM evolution.cellular_migration_destination_pending x WHERE x.request_id=id; FOREACH m IN ARRAY ARRAY[sm,dm] LOOP c := evolution.parse_checkpoint_manifest(m); result_digest := sha256(m); INSERT INTO evolution.region_commits(world_id,region_id,commit_sequence,commit_id,commit_digest,manifest, begin_tick,end_tick,published_epoch,tick_microseconds,versions,build_identity,payload_bytes,payload_sha256) VALUES(c.world_id,c.region_id,c.commit_sequence,c.commit_id,result_digest,m,c.begin_tick,c.end_tick,c.epoch, c.tick_microseconds,c.versions,c.build_identity,c.payload_bytes,c.payload_sha256); UPDATE evolution.region_heads x SET commit_sequence=c.commit_sequence,commit_id=c.commit_id,digest=result_digest, end_tick=c.end_tick,published_epoch=c.epoch WHERE x.world_id=c.world_id AND x.region_id=c.region_id; END LOOP; RETURN QUERY SELECT * FROM evolution.cellular_migration_commit_record(id); END $$; REVOKE ALL ON FUNCTION evolution.publish_cellular_migration(uuid,bytea,bytea,uuid,evolution.u64,uuid,evolution.u64) FROM PUBLIC; GRANT EXECUTE ON FUNCTION evolution.publish_cellular_migration(uuid,bytea,bytea,uuid,evolution.u64,uuid,evolution.u64) TO :"runtime_role"; INSERT INTO evolution.schema_migrations(version,sha256) VALUES(54,:'migration_sha_54');