-- M11.3d: the action tick is the first eight bytes of canonical input. The -- manifest_uint helper uses zero-based offsets, unlike bytea substring below. -- Correct migration 20's off-by-one read without changing its recorded checksum. CREATE OR REPLACE 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 + 24, 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 $$; -- The three-argument publisher is the internal action-only core. Runtime callers -- must use the four-argument publication API, which supplies birth evidence. REVOKE ALL ON FUNCTION evolution.publish_cellular_checkpoint(bytea,bytea,bytea) FROM PUBLIC, :"runtime_role"; INSERT INTO evolution.schema_migrations(version, sha256) VALUES (33, :'migration_sha_33');