Files
aevolution/migrations/0046_boundary_accounting_upgrade.sql
dingo 1b5c0cdf7c
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m23s
Foundation / checks (network) (push) Successful in 21m4s
Foundation / checks (client) (push) Successful in 22m15s
Foundation / checks (tsan) (push) Successful in 23m3s
Foundation / checks (postgres) (push) Successful in 51m33s
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 55m18s
M12.4: admit and replay durable boundary-accounting upgrades
2026-10-18 08:13:36 -04:00

245 lines
15 KiB
PL/PgSQL

-- ADR-0146: explicit same-profile bundle 9 -> 10 upgrades; retained formats keep their meanings.
ALTER FUNCTION evolution.parse_checkpoint_manifest(bytea) RENAME TO parse_checkpoint_manifest_v45;
CREATE FUNCTION evolution.parse_checkpoint_manifest(m bytea)
RETURNS evolution.checkpoint_manifest LANGUAGE plpgsql IMMUTABLE
SET search_path = pg_catalog, pg_temp AS $$
DECLARE
c evolution.checkpoint_manifest;
bundle9 constant bytea := '\x080000000600000002000000010000000100000004000000030000000200000001000000';
bundle10 constant bytea := '\x080000000700000002000000010000000100000004000000030000000200000001000000';
BEGIN
IF m IS NOT NULL AND octet_length(m) = 360 AND evolution.manifest_uint(m,12,4) IN (4,5)
AND substring(m FROM 161 FOR 36) = bundle10 THEN
IF evolution.manifest_uint(m,56,8) = 0 THEN
RAISE EXCEPTION 'accounting bundle requires retained upgrade' USING ERRCODE = '22023';
END IF;
c := evolution.parse_checkpoint_manifest_v45(overlay(m PLACING bundle9 FROM 161 FOR 36));
c.versions := bundle10;
RETURN c;
END IF;
RETURN evolution.parse_checkpoint_manifest_v45(m);
END $$;
REVOKE ALL ON FUNCTION evolution.parse_checkpoint_manifest(bytea) FROM PUBLIC;
ALTER FUNCTION evolution.cellular_births(bytea,bytea) RENAME TO cellular_births_v45;
REVOKE ALL ON FUNCTION evolution.cellular_births_v45(bytea,bytea) FROM PUBLIC, :"runtime_role";
CREATE FUNCTION evolution.cellular_births(t bytea,m bytea)
RETURNS TABLE(account_id uuid,request_id evolution.u64,generation evolution.u64,identity evolution.u64,born evolution.u64)
LANGUAGE plpgsql IMMUTABLE SET search_path = pg_catalog, pg_temp AS $$
DECLARE c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(m);
BEGIN
IF c.versions = '\x080000000700000002000000010000000100000004000000030000000200000001000000'::bytea THEN
m := overlay(m PLACING '\x080000000600000002000000010000000100000004000000030000000200000001000000'::bytea FROM 161 FOR 36);
END IF;
RETURN QUERY SELECT * FROM evolution.cellular_births_v45(t,m);
END $$;
REVOKE ALL ON FUNCTION evolution.cellular_births(bytea,bytea) FROM PUBLIC;
ALTER FUNCTION evolution.check_population_trace(bytea,bytea) RENAME TO check_population_trace_v45;
REVOKE ALL ON FUNCTION evolution.check_population_trace_v45(bytea,bytea) FROM PUBLIC, :"runtime_role";
CREATE FUNCTION evolution.check_population_trace(m bytea,t bytea) RETURNS void
LANGUAGE plpgsql IMMUTABLE SET search_path = pg_catalog, pg_temp AS $$
DECLARE c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(m); n bytea; local_trace bytea;
BEGIN
IF c.versions = '\x080000000700000002000000010000000100000004000000030000000200000001000000'::bytea THEN
IF t IS NULL OR octet_length(t) < 152 OR octet_length(t) <> c.trace_bytes OR sha256(t) <> c.trace_sha256
OR substring(t FROM 1 FOR 8) <> '\x4145564f50425432'::bytea OR evolution.manifest_uint(t,8,4) <> 2 THEN
RAISE EXCEPTION 'invalid accounting population trace binding' USING ERRCODE = '22023';
END IF;
-- Normalize ONLY parser-local copies after checking the actual strong binding.
-- Committed manifest/trace, object digests and retry identity remain exact.
local_trace := overlay(t PLACING '\x4145564f50425431'::bytea FROM 1 FOR 8);
local_trace := overlay(local_trace PLACING '\x01000000'::bytea FROM 9 FOR 4);
n := overlay(m PLACING '\x080000000600000002000000010000000100000004000000030000000200000001000000'::bytea FROM 161 FOR 36);
n := overlay(n PLACING sha256(local_trace) FROM 289 FOR 32);
PERFORM evolution.check_population_trace_v45(n,local_trace);
ELSE
PERFORM evolution.check_population_trace_v45(m,t);
END IF;
END $$;
REVOKE ALL ON FUNCTION evolution.check_population_trace(bytea,bytea) FROM PUBLIC;
ALTER FUNCTION evolution.publish_cellular_profile_migration(bytea) RENAME TO publish_cellular_profile_migration_v45;
REVOKE ALL ON FUNCTION evolution.publish_cellular_profile_migration_v45(bytea) FROM PUBLIC, :"runtime_role";
CREATE OR REPLACE FUNCTION evolution.publish_cellular_profile_migration(p_manifest bytea)
RETURNS TABLE(region_frame evolution.u64, head_sequence evolution.u64, head_commit uuid,
head_digest text, head_tick evolution.u64)
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
DECLARE
c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(p_manifest);
s evolution.checkpoint_manifest;
h evolution.region_heads%ROWTYPE;
p evolution.region_commits%ROWTYPE;
old evolution.region_commits%ROWTYPE;
frame evolution.u64;
d bytea := sha256(p_manifest);
v9_bundle constant bytea := '\x080000000600000002000000010000000100000004000000030000000200000001000000';
v10_bundle constant bytea := '\x080000000700000002000000010000000100000004000000030000000200000001000000';
zero32 constant bytea := decode(repeat('00', 32), 'hex');
BEGIN
IF c.versions <> v10_bundle THEN
RETURN QUERY SELECT * FROM evolution.publish_cellular_profile_migration_v45(p_manifest);
RETURN;
END IF;
IF c.profile NOT IN (4,5) OR c.commit_sequence = 0 OR c.begin_tick <> c.end_tick
OR c.end_tick = 0 OR c.versions <> v10_bundle
OR evolution.manifest_uint(p_manifest, 228, 4) <> 0
OR evolution.manifest_uint(p_manifest, 232, 4) <> (CASE c.profile WHEN 4 THEN 4 ELSE 5 END)
OR evolution.manifest_uint(p_manifest, 236, 4) <> 1
OR c.trace_bytes <> 0 OR c.trace_sha256 <> zero32
OR c.decision_count <> 0 OR c.decisions_sha256 <> zero32 THEN
RAISE EXCEPTION 'invalid boundary-accounting profile migration' USING ERRCODE = '22023';
END IF;
PERFORM evolution.check_region_lease(c.world_id, c.region_id, c.holder, c.epoch);
SELECT * INTO old FROM evolution.region_commits r WHERE r.commit_id = c.commit_id;
IF FOUND THEN
IF old.manifest <> p_manifest THEN
RAISE EXCEPTION 'cellular profile migration identity reused' USING ERRCODE = 'EVL07';
END IF;
SELECT * INTO p FROM evolution.region_commits r
WHERE r.world_id = old.world_id AND r.region_id = old.region_id
AND r.commit_sequence = old.commit_sequence - 1;
IF NOT FOUND OR NOT EXISTS (
SELECT 1 FROM evolution.cellular_profile_migrations m
WHERE m.target_commit = c.commit_id AND m.world_id = c.world_id
AND m.region_id = c.region_id AND m.source_sequence = p.commit_sequence
AND m.source_commit = p.commit_id AND m.source_manifest_sha256 = p.commit_digest
AND m.source_payload_sha256 = p.payload_sha256 AND m.source_versions = p.versions
AND m.target_sequence = old.commit_sequence AND m.target_manifest_sha256 = d
AND m.target_payload_sha256 = old.payload_sha256 AND m.target_versions = old.versions
AND m.source_commit = c.predecessor_commit
AND m.source_manifest_sha256 = c.predecessor_digest) THEN
RAISE EXCEPTION 'cellular profile migration identity reused' USING ERRCODE = 'EVL07';
END IF;
SELECT l.frame_id INTO frame FROM evolution.region_leases l
WHERE l.world_id = c.world_id AND l.region_id = c.region_id;
RETURN QUERY SELECT frame, old.commit_sequence, old.commit_id,
encode(old.commit_digest, 'hex'), old.end_tick;
RETURN;
END IF;
IF 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 'accounting upgrade waits for interval' USING ERRCODE = 'EVL18';
END IF;
SELECT * INTO h FROM evolution.region_heads r
WHERE r.world_id = c.world_id AND r.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 THEN
RAISE EXCEPTION 'region head moved' USING ERRCODE = 'EVL03';
END IF;
SELECT * INTO p FROM evolution.region_commits r
WHERE r.world_id = c.world_id AND r.region_id = c.region_id
AND r.commit_sequence = h.commit_sequence;
IF NOT FOUND THEN
RAISE EXCEPTION 'missing migration predecessor' USING ERRCODE = 'XX000';
END IF;
s := evolution.parse_checkpoint_manifest(p.manifest);
IF s.profile <> c.profile OR s.versions <> v9_bundle OR p.versions <> v9_bundle
OR c.frame_id <> s.frame_id OR c.tick_microseconds <> s.tick_microseconds
OR s.commit_id <> c.predecessor_commit OR s.end_tick <> c.end_tick
OR evolution.manifest_uint(p.manifest, 232, 4) <> (CASE c.profile WHEN 4 THEN 4 ELSE 5 END)
OR NOT EXISTS (SELECT 1 FROM evolution.retained_builds b
WHERE b.build_identity = c.build_identity) THEN
RAISE EXCEPTION 'unsupported boundary-accounting profile migration' USING ERRCODE = 'EVL06';
END IF;
SELECT l.frame_id INTO frame FROM evolution.region_leases l
WHERE l.world_id = c.world_id AND l.region_id = c.region_id;
IF c.frame_id <> frame THEN
RAISE EXCEPTION 'foreign frame' USING ERRCODE = '22023';
END IF;
INSERT INTO evolution.cellular_profile_migrations(target_commit, world_id, region_id,
source_sequence, source_commit, source_manifest_sha256, source_payload_sha256,
source_versions, target_sequence, target_manifest_sha256, target_payload_sha256,
target_versions)
VALUES (c.commit_id, c.world_id, c.region_id, p.commit_sequence, p.commit_id,
p.commit_digest, p.payload_sha256, p.versions, c.commit_sequence, d,
c.payload_sha256, c.versions);
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, d, p_manifest,
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 r
SET commit_sequence = c.commit_sequence, commit_id = c.commit_id, digest = d,
end_tick = c.end_tick, published_epoch = c.epoch
WHERE r.world_id = c.world_id AND r.region_id = c.region_id;
RETURN QUERY SELECT frame, c.commit_sequence, c.commit_id, encode(d, 'hex'), c.end_tick;
END $$;
REVOKE ALL ON FUNCTION evolution.publish_cellular_profile_migration(bytea) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION evolution.publish_cellular_profile_migration(bytea) TO :"runtime_role";
CREATE OR REPLACE FUNCTION evolution.publish_population_profile_transition_v44(m bytea)
RETURNS TABLE(region_frame evolution.u64,head_sequence evolution.u64,head_commit uuid,
head_digest text,head_tick evolution.u64)
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
DECLARE
c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(m);
s evolution.checkpoint_manifest; p evolution.region_commits%ROWTYPE;
h evolution.region_heads%ROWTYPE; old evolution.region_commits%ROWTYPE;
d bytea := sha256(m); frame evolution.u64;
bundle9 constant bytea := '\x080000000600000002000000010000000100000004000000030000000200000001000000';
BEGIN
IF c.profile NOT IN (4,5) OR c.versions NOT IN (bundle9,'\x080000000700000002000000010000000100000004000000030000000200000001000000'::bytea) OR c.commit_sequence = 0
OR c.begin_tick <> c.end_tick OR c.end_tick = 0 OR c.trace_bytes <> 0 THEN
RAISE EXCEPTION 'invalid population profile transition' USING ERRCODE = '22023';
END IF;
PERFORM evolution.check_region_lease(c.world_id,c.region_id,c.holder,c.epoch);
SELECT * INTO old FROM evolution.region_commits r WHERE r.commit_id = c.commit_id;
IF FOUND THEN
IF old.manifest <> m OR NOT EXISTS (SELECT 1 FROM evolution.population_profile_transitions x
WHERE x.target_commit = c.commit_id AND x.source_commit = c.predecessor_commit
AND x.source_manifest_sha256 = c.predecessor_digest AND x.target_profile = c.profile) THEN
RAISE EXCEPTION 'population transition identity reused' USING ERRCODE = 'EVL07';
END IF;
SELECT l.frame_id INTO frame FROM evolution.region_leases l WHERE l.world_id = c.world_id AND l.region_id = c.region_id;
RETURN QUERY SELECT frame,old.commit_sequence,old.commit_id,encode(old.commit_digest,'hex'),old.end_tick;
RETURN;
END IF;
SELECT * INTO h FROM evolution.region_heads r WHERE r.world_id = c.world_id AND r.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 THEN
RAISE EXCEPTION 'population transition head moved' USING ERRCODE = 'EVL03';
END IF;
SELECT * INTO p FROM evolution.region_commits r WHERE r.commit_id = h.commit_id;
IF NOT FOUND THEN RAISE EXCEPTION 'missing population source' USING ERRCODE = 'XX000'; END IF;
s := evolution.parse_checkpoint_manifest(p.manifest);
IF c.profile = 5 AND EXISTS (SELECT 1 FROM evolution.cellular_mutation_reservations x
WHERE x.world_id = c.world_id AND x.region_id = c.region_id 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)) THEN
RAISE EXCEPTION 'pending player mutation blocks population transition' USING ERRCODE = 'EVL18';
END IF;
IF s.profile NOT IN (4,5) OR s.profile = c.profile OR s.versions <> c.versions
OR s.frame_id <> c.frame_id OR s.tick_microseconds <> c.tick_microseconds
OR s.end_tick <> c.end_tick OR NOT EXISTS (SELECT 1 FROM evolution.retained_builds b WHERE b.build_identity = c.build_identity) THEN
RAISE EXCEPTION 'unsupported population source' USING ERRCODE = 'EVL06';
END IF;
INSERT INTO evolution.population_profile_transitions VALUES(c.commit_id,p.commit_id,p.commit_digest,p.payload_sha256,s.profile,c.profile);
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,d,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 r SET commit_sequence = c.commit_sequence,commit_id = c.commit_id,
digest = d,end_tick = c.end_tick,published_epoch = c.epoch WHERE r.world_id = c.world_id AND r.region_id = c.region_id;
RETURN QUERY SELECT c.frame_id,c.commit_sequence,c.commit_id,encode(d,'hex'),c.end_tick;
END $$;
CREATE OR REPLACE FUNCTION evolution.refuse_unpublished_population() RETURNS trigger
LANGUAGE plpgsql SECURITY INVOKER SET search_path = pg_catalog, pg_temp AS $$
BEGIN
IF evolution.manifest_uint(NEW.manifest,12,4) = 5 AND
(NEW.commit_sequence = 0 OR
(NOT EXISTS (SELECT 1 FROM evolution.population_publications p WHERE p.commit_id = NEW.commit_id)
AND NOT EXISTS (SELECT 1 FROM evolution.population_profile_transitions p
WHERE p.target_commit = NEW.commit_id AND p.target_profile = 5)
AND NOT EXISTS (SELECT 1 FROM evolution.cellular_profile_migrations p WHERE p.target_commit = NEW.commit_id))) THEN
RAISE EXCEPTION 'population commit requires fenced publisher' USING ERRCODE = 'EVL06';
END IF;
RETURN NEW;
END $$;
REVOKE ALL ON FUNCTION evolution.refuse_unpublished_population() FROM PUBLIC;
INSERT INTO evolution.schema_migrations(version,sha256) VALUES(46,:'migration_sha_46');