Files
aevolution/migrations/0021_cellular_trace_and_recovery.sql
T
dingo 2133e5086e
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (checks) (push) Failing after 41s
Foundation / checks (postgres) (push) Canceled after 0s
Foundation / checks (tsan) (push) Canceled after 0s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (client) (push) Canceled after 1m46s
Foundation / checks (network) (push) Canceled after 1m9s
Foundation / checks (ecs) (push) Canceled after 1m38s
Add cellular replay trace and recovery lookup
2026-10-10 23:16:46 -04:00

129 lines
6.7 KiB
PL/PgSQL

-- M11.3d-6c, ADR-0113: bounded cellular replay trace and durable action-block read.
CREATE OR REPLACE FUNCTION evolution.parse_checkpoint_manifest(m bytea)
RETURNS evolution.checkpoint_manifest LANGUAGE plpgsql IMMUTABLE
SET search_path = pg_catalog, pg_temp AS $$
DECLARE
r evolution.checkpoint_manifest;
zero16 constant bytea := '\x00000000000000000000000000000000';
zero32 constant bytea := decode(repeat('00', 32), 'hex');
history numeric;
cap numeric;
ticks numeric;
BEGIN
IF m IS NULL OR octet_length(m) < 12 OR substring(m FROM 1 FOR 8) <> '\x4145564f52434d31' THEN
RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023';
END IF;
IF evolution.manifest_uint(m, 8, 4) <> 2 THEN
RAISE EXCEPTION 'unsupported checkpoint manifest' USING ERRCODE = 'EVL06';
END IF;
IF octet_length(m) <> 360 THEN
RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023';
END IF;
r.profile := evolution.manifest_uint(m, 12, 4);
history := evolution.manifest_uint(m, 232, 4);
IF NOT (
(((r.profile = 1 AND history = 0) OR (r.profile = 2 AND history = 1))
AND substring(m FROM 161 FOR 36) = '\x020000000200000001000000010000000100000001000000010000000100000001000000')
OR (r.profile = 3 AND history = 2
AND substring(m FROM 161 FOR 36) = '\x010000000100000001000000000000000000000001000000010000000100000001000000')
OR (r.profile = 4 AND history = 3
AND substring(m FROM 161 FOR 36) = '\x010000000100000001000000010000000100000002000000010000000100000000000000')
)
OR evolution.manifest_uint(m, 228, 4) <> 0 OR evolution.manifest_uint(m, 236, 4) <> 1 THEN
RAISE EXCEPTION 'unsupported checkpoint manifest' USING ERRCODE = 'EVL06';
END IF;
cap := CASE r.profile WHEN 1 THEN 1048576 WHEN 2 THEN 4268144
WHEN 4 THEN 16777216 ELSE 3266800 END;
r.world_id := evolution.manifest_uint(m, 16, 8);
r.region_id := evolution.manifest_uint(m, 24, 8);
r.frame_id := evolution.manifest_uint(m, 32, 8);
r.commit_id := encode(substring(m FROM 41 FOR 16), 'hex')::uuid;
r.commit_sequence := evolution.manifest_uint(m, 56, 8);
r.predecessor_commit := encode(substring(m FROM 65 FOR 16), 'hex')::uuid;
r.predecessor_digest := substring(m FROM 81 FOR 32);
r.begin_tick := evolution.manifest_uint(m, 112, 8);
r.end_tick := evolution.manifest_uint(m, 120, 8);
r.holder := encode(substring(m FROM 129 FOR 16), 'hex')::uuid;
r.epoch := evolution.manifest_uint(m, 144, 8);
r.tick_microseconds := evolution.manifest_uint(m, 152, 8);
r.versions := substring(m FROM 161 FOR 36);
r.build_identity := substring(m FROM 197 FOR 32);
r.payload_bytes := evolution.manifest_uint(m, 240, 8);
r.payload_sha256 := substring(m FROM 249 FOR 32);
r.trace_bytes := evolution.manifest_uint(m, 280, 8);
r.trace_sha256 := substring(m FROM 289 FOR 32);
r.decision_count := evolution.manifest_uint(m, 320, 4);
r.decisions_sha256 := substring(m FROM 329 FOR 32);
IF r.world_id = 0 OR r.region_id = 0 OR r.frame_id = 0
OR substring(m FROM 41 FOR 16) = zero16 OR r.tick_microseconds = 0
OR r.build_identity = zero32 OR r.payload_bytes < 1 OR r.payload_bytes > cap
OR r.payload_sha256 = zero32 OR evolution.manifest_uint(m, 324, 4) <> 0 THEN
RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023';
END IF;
IF r.profile IN (3, 4) AND (r.tick_microseconds <> 50000 OR
(r.profile = 3 AND (r.decision_count <> 0 OR r.decisions_sha256 <> zero32)) OR
(r.commit_sequence = 0 AND
(substring(m FROM 65 FOR 16) <> zero16 OR r.predecessor_digest <> zero32))) THEN
RAISE EXCEPTION 'invalid lifecycle checkpoint' USING ERRCODE = '22023';
END IF;
IF r.commit_sequence = 0 THEN
IF substring(m FROM 129 FOR 16) <> zero16 OR r.epoch <> 0 OR r.begin_tick <> r.end_tick
OR r.trace_bytes <> 0 OR r.trace_sha256 <> zero32 THEN
RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023';
END IF;
IF substring(m FROM 65 FOR 16) = zero16 AND r.predecessor_digest = zero32 THEN
IF r.end_tick <> 0 OR r.decision_count <> 0 OR r.decisions_sha256 <> zero32 THEN
RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023';
END IF;
r.predecessor_commit := NULL;
ELSIF substring(m FROM 65 FOR 16) = zero16 OR r.predecessor_digest = zero32
OR r.predecessor_commit = r.commit_id OR r.profile IN (3, 4)
OR r.decision_count > 8
OR (r.decision_count = 0) <> (r.decisions_sha256 = zero32) THEN
RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023';
END IF;
r.holder := NULL;
ELSE
ticks := r.end_tick - r.begin_tick;
IF substring(m FROM 65 FOR 16) = zero16 OR r.predecessor_commit = r.commit_id
OR r.predecessor_digest = zero32 OR substring(m FROM 129 FOR 16) = zero16
OR r.epoch = 0 OR r.end_tick <= r.begin_tick OR ticks > 64
OR (r.profile IN (1, 2) AND r.trace_bytes <> 96 + 304 * ticks)
OR (r.profile = 3 AND (r.trace_bytes < 112 OR r.trace_bytes > 12400
OR mod(r.trace_bytes - 112, 48) <> 0))
OR (r.profile = 4 AND (r.trace_bytes < 112 + 4 * ticks
OR r.trace_bytes > 112 + 6148 * ticks))
OR r.trace_sha256 = zero32 OR r.decision_count > 8
OR (r.decision_count = 0) <> (r.decisions_sha256 = zero32) THEN
RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023';
END IF;
END IF;
RETURN r;
END $$;
CREATE FUNCTION evolution.load_cellular_publication_actions(p_world evolution.u64,
p_region evolution.u64, p_holder uuid, p_epoch evolution.u64, p_commit uuid)
RETURNS TABLE(actions bytea)
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
BEGIN
IF p_commit IS NULL THEN
RAISE EXCEPTION 'invalid cellular recovery request' USING ERRCODE = '22023';
END IF;
PERFORM evolution.check_region_lease(p_world, p_region, p_holder, p_epoch);
IF EXISTS (SELECT 1 FROM evolution.region_commits c WHERE c.world_id = p_world
AND c.region_id = p_region AND c.commit_id = p_commit
AND c.commit_sequence > 0 AND evolution.manifest_uint(c.manifest, 12, 4) = 4) THEN
RETURN QUERY SELECT p.actions FROM evolution.cellular_publications p
WHERE p.commit_id = p_commit;
IF NOT FOUND THEN
RAISE EXCEPTION 'cellular publication evidence missing' USING ERRCODE = 'EVL06';
END IF;
END IF;
END $$;
REVOKE ALL ON FUNCTION evolution.load_cellular_publication_actions(
evolution.u64,evolution.u64,uuid,evolution.u64,uuid) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION evolution.load_cellular_publication_actions(
evolution.u64,evolution.u64,uuid,evolution.u64,uuid) TO :"runtime_role";
INSERT INTO evolution.schema_migrations(version, sha256) VALUES (21, :'migration_sha_21');