Foundation / checks (client) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (checks) (push) Failing after 38s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 43s
Foundation / checks (ecs) (push) Canceled after 54s
Foundation / checks (tsan) (push) Canceled after 24s
195 lines
9.5 KiB
PL/PgSQL
195 lines
9.5 KiB
PL/PgSQL
-- M11.3d-6a, ADR-0112: isolated profile-4 checkpoint and action publication.
|
|
-- The private action block is inserted in the same transaction as the checkpoint;
|
|
-- direct calls to the generic writer cannot pass the profile-4 commit trigger.
|
|
CREATE TABLE evolution.cellular_publications (
|
|
commit_id uuid PRIMARY KEY REFERENCES evolution.region_commits(commit_id)
|
|
DEFERRABLE INITIALLY DEFERRED,
|
|
actions bytea NOT NULL CHECK (octet_length(actions) <= 49152)
|
|
);
|
|
REVOKE ALL ON evolution.cellular_publications FROM PUBLIC;
|
|
CREATE TRIGGER cellular_publications_immutable BEFORE UPDATE OR DELETE
|
|
ON evolution.cellular_publications FOR EACH ROW
|
|
EXECUTE FUNCTION evolution.refuse_history_rewrite();
|
|
CREATE TRIGGER cellular_publications_no_truncate BEFORE TRUNCATE
|
|
ON evolution.cellular_publications FOR EACH STATEMENT
|
|
EXECUTE FUNCTION evolution.refuse_history_rewrite();
|
|
|
|
CREATE FUNCTION evolution.check_cellular_action_block(p_actions bytea,
|
|
p_begin numeric, p_end numeric)
|
|
RETURNS integer LANGUAGE plpgsql IMMUTABLE SECURITY INVOKER
|
|
SET search_path = pg_catalog, pg_temp AS $$
|
|
DECLARE
|
|
n integer := coalesce(octet_length(p_actions), -1);
|
|
account bytea;
|
|
action_id numeric;
|
|
input_bytes bytea;
|
|
tick numeric;
|
|
prev_tick numeric := -1;
|
|
prev_account bytea := ''::bytea;
|
|
prev_id numeric := -1;
|
|
seen text[] := '{}';
|
|
BEGIN
|
|
-- Each record: account UUID (16), action id (u64 LE), canonical input (24).
|
|
IF n < 0 OR n % 48 <> 0 OR n / 48 > 1024 THEN
|
|
RAISE EXCEPTION 'invalid cellular action block' USING ERRCODE = '22023';
|
|
END IF;
|
|
FOR i IN 0 .. n / 48 - 1 LOOP
|
|
account := substring(p_actions FROM 48 * i + 1 FOR 16);
|
|
action_id := evolution.manifest_uint(p_actions, 48 * i + 16, 8);
|
|
input_bytes := substring(p_actions FROM 48 * i + 25 FOR 24);
|
|
tick := evolution.manifest_uint(input_bytes, 0, 8);
|
|
IF account = decode(repeat('00', 16), 'hex') OR action_id = 0
|
|
OR tick <= p_begin OR tick > p_end
|
|
OR NOT evolution.valid_cellular_action_input(input_bytes, tick)
|
|
OR (tick, account, action_id) <= (prev_tick, prev_account, prev_id) THEN
|
|
RAISE EXCEPTION 'invalid cellular action record' USING ERRCODE = '22023';
|
|
END IF;
|
|
IF encode(account, 'hex') || ':' || action_id::text = ANY (seen) THEN
|
|
RAISE EXCEPTION 'duplicate cellular action id' USING ERRCODE = '22023';
|
|
END IF;
|
|
seen := seen || (encode(account, 'hex') || ':' || action_id::text);
|
|
prev_tick := tick;
|
|
prev_account := account;
|
|
prev_id := action_id;
|
|
END LOOP;
|
|
RETURN n / 48;
|
|
END $$;
|
|
|
|
-- Replace the temporary M11.3 profile-4 refusal gate. Evidence can only be
|
|
-- inserted by publish_cellular_checkpoint; the runtime role has no table grant.
|
|
CREATE OR REPLACE FUNCTION evolution.refuse_unpublished_cellular() RETURNS trigger
|
|
LANGUAGE plpgsql SECURITY INVOKER SET search_path = pg_catalog, pg_temp AS $$
|
|
BEGIN
|
|
IF evolution.manifest_uint(NEW.manifest, 12, 4) = 4 AND NEW.commit_sequence > 0
|
|
AND NOT EXISTS (SELECT 1 FROM evolution.cellular_publications p
|
|
WHERE p.commit_id = NEW.commit_id) THEN
|
|
RAISE EXCEPTION 'cellular publication evidence required' USING ERRCODE = 'EVL06';
|
|
END IF;
|
|
RETURN NEW;
|
|
END $$;
|
|
|
|
-- The former two-argument function becomes a private transaction core. Its
|
|
-- public name remains a frozen profile-1/2/3 wrapper, while profile 4 can enter
|
|
-- the core only through publish_cellular_checkpoint below.
|
|
ALTER FUNCTION evolution.publish_region_checkpoint(bytea,bytea)
|
|
RENAME TO publish_region_checkpoint_core;
|
|
REVOKE ALL ON FUNCTION evolution.publish_region_checkpoint_core(bytea,bytea)
|
|
FROM PUBLIC, :"runtime_role";
|
|
CREATE FUNCTION evolution.publish_region_checkpoint(p_manifest bytea, p_decisions 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);
|
|
BEGIN
|
|
IF c.profile = 4 THEN
|
|
RAISE EXCEPTION 'profile-4 requires cellular publisher' USING ERRCODE = 'EVL06';
|
|
END IF;
|
|
RETURN QUERY SELECT * FROM evolution.publish_region_checkpoint_core(p_manifest, p_decisions);
|
|
END $$;
|
|
REVOKE ALL ON FUNCTION evolution.publish_region_checkpoint(bytea,bytea) FROM PUBLIC;
|
|
GRANT EXECUTE ON FUNCTION evolution.publish_region_checkpoint(bytea,bytea) TO :"runtime_role";
|
|
|
|
CREATE FUNCTION evolution.publish_cellular_checkpoint(p_manifest bytea,
|
|
p_decisions bytea, p_actions 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);
|
|
n integer;
|
|
old_actions bytea;
|
|
r record;
|
|
BEGIN
|
|
IF c.profile <> 4 OR c.commit_sequence = 0 OR p_actions IS NULL THEN
|
|
RAISE EXCEPTION 'invalid cellular publication' USING ERRCODE = '22023';
|
|
END IF;
|
|
n := evolution.check_cellular_action_block(p_actions, c.begin_tick, c.end_tick);
|
|
SELECT p.actions INTO old_actions FROM evolution.cellular_publications p
|
|
WHERE p.commit_id = c.commit_id;
|
|
IF FOUND THEN
|
|
IF old_actions <> p_actions THEN
|
|
RAISE EXCEPTION 'cellular commit identity reused' USING ERRCODE = 'EVL07';
|
|
END IF;
|
|
RETURN QUERY SELECT * FROM evolution.publish_region_checkpoint_core(p_manifest, p_decisions);
|
|
RETURN;
|
|
END IF;
|
|
|
|
PERFORM evolution.check_region_lease(c.world_id, c.region_id, c.holder, c.epoch);
|
|
-- Recheck after the lease lock: an uncertain first attempt may have completed.
|
|
SELECT p.actions INTO old_actions FROM evolution.cellular_publications p
|
|
WHERE p.commit_id = c.commit_id;
|
|
IF FOUND THEN
|
|
IF old_actions <> p_actions THEN
|
|
RAISE EXCEPTION 'cellular commit identity reused' USING ERRCODE = 'EVL07';
|
|
END IF;
|
|
RETURN QUERY SELECT * FROM evolution.publish_region_checkpoint_core(p_manifest, p_decisions);
|
|
RETURN;
|
|
END IF;
|
|
|
|
INSERT INTO evolution.cellular_publications(commit_id, actions)
|
|
VALUES (c.commit_id, p_actions);
|
|
FOR i IN 0 .. n - 1 LOOP
|
|
INSERT INTO evolution.region_actions(world_id, account_id, action_id, region_id, tick,
|
|
kind, commit_sequence, input_bytes, result)
|
|
VALUES (c.world_id,
|
|
encode(substring(p_actions FROM 48 * i + 1 FOR 16), 'hex')::uuid,
|
|
evolution.manifest_uint(p_actions, 48 * i + 16, 8), c.region_id,
|
|
evolution.manifest_uint(p_actions, 48 * i + 25, 8), 2, c.commit_sequence,
|
|
substring(p_actions FROM 48 * i + 25 FOR 24), 5)
|
|
ON CONFLICT (world_id, account_id, action_id) DO NOTHING;
|
|
IF NOT EXISTS (SELECT 1 FROM evolution.region_actions a
|
|
WHERE a.world_id = c.world_id
|
|
AND a.account_id = encode(substring(p_actions FROM 48 * i + 1 FOR 16), 'hex')::uuid
|
|
AND a.action_id = evolution.manifest_uint(p_actions, 48 * i + 16, 8)
|
|
AND a.kind = 2 AND a.result = 5
|
|
AND a.input_bytes = substring(p_actions FROM 48 * i + 25 FOR 24)) THEN
|
|
RAISE EXCEPTION 'cellular action identity reused' USING ERRCODE = 'EVL07';
|
|
END IF;
|
|
END LOOP;
|
|
-- The trigger on region_commits requires the private action-block row above.
|
|
-- A failure here rolls back the block and all outcome rows with the head advance.
|
|
SELECT * INTO r FROM evolution.publish_region_checkpoint_core(p_manifest, p_decisions);
|
|
IF NOT FOUND THEN
|
|
RAISE EXCEPTION 'publication returned no head' USING ERRCODE = 'XX000';
|
|
END IF;
|
|
RETURN QUERY SELECT r.region_frame, r.head_sequence, r.head_commit, r.head_digest,
|
|
r.head_tick;
|
|
END $$;
|
|
|
|
CREATE FUNCTION evolution.resolve_cellular_region_commit(p_world evolution.u64,
|
|
p_region evolution.u64, p_commit uuid, p_manifest bytea, p_decisions bytea,
|
|
p_actions bytea)
|
|
RETURNS TABLE(status text, region_frame evolution.u64, head_sequence evolution.u64,
|
|
head_commit uuid, head_digest text, head_tick evolution.u64)
|
|
LANGUAGE plpgsql STABLE SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
|
|
DECLARE
|
|
c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(p_manifest);
|
|
old_actions bytea;
|
|
BEGIN
|
|
IF c.profile <> 4 OR c.commit_sequence = 0 OR c.world_id <> p_world
|
|
OR c.region_id <> p_region OR c.commit_id <> p_commit OR p_actions IS NULL THEN
|
|
RAISE EXCEPTION 'invalid cellular resolution request' USING ERRCODE = '22023';
|
|
END IF;
|
|
PERFORM evolution.check_cellular_action_block(p_actions, c.begin_tick, c.end_tick);
|
|
SELECT p.actions INTO old_actions FROM evolution.cellular_publications p
|
|
WHERE p.commit_id = p_commit;
|
|
IF EXISTS (SELECT 1 FROM evolution.region_commits x WHERE x.commit_id = p_commit)
|
|
AND (old_actions IS NULL OR old_actions <> p_actions) THEN
|
|
RETURN QUERY SELECT 'conflict'::text, NULL::evolution.u64, NULL::evolution.u64,
|
|
NULL::uuid, NULL::text, NULL::evolution.u64;
|
|
RETURN;
|
|
END IF;
|
|
RETURN QUERY SELECT * FROM evolution.resolve_region_commit(p_world, p_region, p_commit,
|
|
p_manifest, p_decisions);
|
|
END $$;
|
|
|
|
REVOKE ALL ON FUNCTION evolution.check_cellular_action_block(bytea,numeric,numeric),
|
|
evolution.publish_cellular_checkpoint(bytea,bytea,bytea),
|
|
evolution.resolve_cellular_region_commit(evolution.u64,evolution.u64,uuid,bytea,bytea,bytea)
|
|
FROM PUBLIC;
|
|
GRANT EXECUTE ON FUNCTION evolution.publish_cellular_checkpoint(bytea,bytea,bytea),
|
|
evolution.resolve_cellular_region_commit(evolution.u64,evolution.u64,uuid,bytea,bytea,bytea)
|
|
TO :"runtime_role";
|
|
INSERT INTO evolution.schema_migrations(version, sha256) VALUES (20, :'migration_sha_20');
|