-- M11.6, ADR-0127: permadeath and a complete restart. A life's reservation is marked -- ended, once and forever, by the region that holds its commit; an account may then -- reserve and publish a new founder. One ACTIVE life per account replaces one life -- per account. Corpses, archives, descendants and every earlier row stay unchanged. ALTER TABLE evolution.life_reservations ADD COLUMN ended_commit uuid; DROP INDEX evolution.one_reserved_life_per_account; CREATE UNIQUE INDEX one_active_life_per_account ON evolution.life_reservations(world_id, account_id) WHERE rejection = 0 AND ended_commit IS NULL; -- A published binding no longer implies the account's only life. ALTER TABLE evolution.primitive_life_bindings DROP CONSTRAINT primitive_life_bindings_world_id_account_id_key; -- An ending is set once, only on a published, unrefused life, and never undone. CREATE FUNCTION evolution.life_ending_is_final() RETURNS trigger LANGUAGE plpgsql SECURITY INVOKER SET search_path = pg_catalog, pg_temp AS $$ BEGIN IF OLD.ended_commit IS NOT NULL AND NEW.ended_commit IS DISTINCT FROM OLD.ended_commit THEN RAISE EXCEPTION 'life ending is final' USING ERRCODE = 'EVL16'; END IF; IF NEW.ended_commit IS NOT NULL AND OLD.ended_commit IS NULL AND (NEW.rejection <> 0 OR NOT EXISTS (SELECT 1 FROM evolution.primitive_life_bindings b WHERE b.world_id = NEW.world_id AND b.account_id = NEW.account_id AND b.request_id = NEW.request_id)) THEN RAISE EXCEPTION 'only a published life can end' USING ERRCODE = 'EVL16'; END IF; RETURN NEW; END $$; CREATE TRIGGER life_ending_is_final BEFORE UPDATE OF ended_commit ON evolution.life_reservations FOR EACH ROW EXECUTE FUNCTION evolution.life_ending_is_final(); -- Unchanged from schema v17 except that an ended life no longer blocks a new request. CREATE OR REPLACE FUNCTION evolution.reserve_primitive_life(p_world evolution.u64, p_region evolution.u64, p_holder uuid, p_epoch evolution.u64, p_account uuid, p_generation evolution.u64, p_request evolution.u64, p_rules integer) RETURNS TABLE (life_identity evolution.u64, life_region evolution.u64, rejection smallint) LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ DECLARE r evolution.life_reservations%ROWTYPE; allocated numeric; BEGIN IF p_request IS NULL OR p_request = 0 OR p_rules IS DISTINCT FROM 1 THEN RAISE EXCEPTION 'invalid life request' USING ERRCODE = '22023'; END IF; PERFORM evolution.check_life_control(p_world, p_region, p_holder, p_epoch, p_account, p_generation); SELECT * INTO r FROM evolution.life_reservations x WHERE x.world_id = p_world AND x.account_id = p_account AND x.request_id = p_request; IF FOUND THEN RETURN QUERY SELECT r.identity, r.region_id, r.rejection; RETURN; END IF; SELECT a.next_identity INTO allocated FROM evolution.life_allocators a WHERE a.world_id = p_world FOR UPDATE; IF NOT FOUND THEN RAISE EXCEPTION 'life allocator not provisioned' USING ERRCODE = 'EVL04'; END IF; IF allocated = 18446744073709551615 THEN RAISE EXCEPTION 'life identities exhausted' USING ERRCODE = 'EVL05'; END IF; PERFORM evolution.check_life_control(p_world, p_region, p_holder, p_epoch, p_account, p_generation); IF EXISTS (SELECT 1 FROM evolution.life_reservations x WHERE x.world_id = p_world AND x.account_id = p_account AND x.rejection = 0 AND x.ended_commit IS NULL) THEN UPDATE evolution.life_allocators SET next_identity = allocated + 1 WHERE world_id = p_world; INSERT INTO evolution.life_reservations(world_id, account_id, request_id, identity, region_id, rules, rejection) VALUES (p_world, p_account, p_request, allocated, p_region, p_rules, 4); RETURN QUERY SELECT allocated::evolution.u64, p_region, 4::smallint; RETURN; END IF; UPDATE evolution.life_allocators SET next_identity = allocated + 1 WHERE world_id = p_world; INSERT INTO evolution.life_reservations(world_id, account_id, request_id, identity, region_id, rules) VALUES (p_world, p_account, p_request, allocated, p_region, p_rules); RETURN QUERY SELECT allocated::evolution.u64, p_region, 0::smallint; END $$; -- Unchanged from schema v24 except that only the active life has an owner region. CREATE OR REPLACE FUNCTION evolution.primitive_life_owner(p_world evolution.u64, p_region evolution.u64, p_holder uuid, p_epoch evolution.u64, p_account uuid) RETURNS TABLE (owner_region evolution.u64) LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ BEGIN IF p_world IS NULL OR p_world = 0 OR p_region IS NULL OR p_region = 0 OR p_holder IS NULL OR p_epoch IS NULL OR p_epoch = 0 OR p_account IS NULL OR p_account = '00000000-0000-0000-0000-000000000000' THEN RAISE EXCEPTION 'invalid life owner lookup' USING ERRCODE = '22023'; END IF; PERFORM evolution.check_region_lease(p_world, p_region, p_holder, p_epoch); RETURN QUERY SELECT r.region_id FROM evolution.life_reservations r WHERE r.world_id = p_world AND r.account_id = p_account AND r.rejection = 0 AND r.ended_commit IS NULL; END $$; -- Unchanged from schema v31 (renamed in v35) except that only an active binding of the -- account conflicts with a new birth. CREATE OR REPLACE FUNCTION evolution.publish_cellular_birth_checkpoint(p_manifest bytea, p_decisions bytea, p_actions bytea, p_trace 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); birth_row record; r evolution.life_reservations%ROWTYPE; 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; PERFORM evolution.check_cellular_action_block(p_actions, c.begin_tick, c.end_tick); PERFORM * FROM evolution.cellular_births(p_trace, p_manifest); IF EXISTS (SELECT b.account_id FROM evolution.cellular_births(p_trace, p_manifest) b GROUP BY b.account_id HAVING count(*) > 1) OR EXISTS (SELECT b.identity FROM evolution.cellular_births(p_trace, p_manifest) b GROUP BY b.identity HAVING count(*) > 1) THEN RAISE EXCEPTION 'duplicate cellular birth identity' USING ERRCODE = '22023'; END IF; PERFORM evolution.check_region_lease(c.world_id, c.region_id, c.holder, c.epoch); -- The lease lock fences competing commits. Exact retries are accepted only -- when the immutable binding rows agree with every birth in the trace. IF EXISTS (SELECT 1 FROM evolution.region_commits x WHERE x.commit_id = c.commit_id) THEN IF NOT evolution.cellular_birth_bindings_match(c.world_id, c.commit_id, p_trace, p_manifest) THEN RAISE EXCEPTION 'cellular birth evidence differs on retry' USING ERRCODE = 'EVL07'; END IF; RETURN QUERY SELECT * FROM evolution.publish_cellular_checkpoint( p_manifest, p_decisions, p_actions); RETURN; END IF; -- Lock accounts in stable order and require the exact current control generation -- and previously allocated request identity before inserting any binding. FOR birth_row IN SELECT * FROM evolution.cellular_births(p_trace, p_manifest) ORDER BY account_id LOOP PERFORM evolution.check_life_control(c.world_id, c.region_id, c.holder, c.epoch, birth_row.account_id, birth_row.generation); SELECT * INTO r FROM evolution.life_reservations x WHERE x.world_id = c.world_id AND x.account_id = birth_row.account_id AND x.request_id = birth_row.request_id FOR UPDATE; IF NOT FOUND OR r.region_id <> c.region_id OR r.identity <> birth_row.identity OR r.rejection <> 0 OR r.rules <> 1 OR EXISTS (SELECT 1 FROM evolution.primitive_life_bindings x JOIN evolution.life_reservations y USING (world_id, account_id, request_id) WHERE x.world_id = c.world_id AND x.account_id = birth_row.account_id AND y.ended_commit IS NULL) THEN RAISE EXCEPTION 'cellular birth reservation conflicts' USING ERRCODE = 'EVL16'; END IF; END LOOP; FOR birth_row IN SELECT * FROM evolution.cellular_births(p_trace, p_manifest) ORDER BY account_id LOOP PERFORM evolution.check_life_control(c.world_id, c.region_id, c.holder, c.epoch, birth_row.account_id, birth_row.generation); END LOOP; INSERT INTO evolution.primitive_life_bindings(world_id, account_id, request_id, born, commit_id) SELECT c.world_id, b.account_id, b.request_id, b.born, c.commit_id FROM evolution.cellular_births(p_trace, p_manifest) b; RETURN QUERY SELECT * FROM evolution.publish_cellular_checkpoint( p_manifest, p_decisions, p_actions); END $$; -- The region holding the commit records which of its published lives had ended by then. -- Identities are u64 little-endian, at most 256 per call. Repeats and identities never -- reserved (opening founders) are no-ops; a life of -- another region, unpublished or refused, or an unknown commit is refused. CREATE FUNCTION evolution.end_cellular_lives(p_world evolution.u64, p_region evolution.u64, p_holder uuid, p_epoch evolution.u64, p_commit uuid, p_identities bytea) RETURNS integer LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ DECLARE n integer; ended integer := 0; ending_identity numeric; r evolution.life_reservations%ROWTYPE; BEGIN IF p_commit IS NULL OR p_identities IS NULL OR octet_length(p_identities) % 8 <> 0 OR octet_length(p_identities) / 8 > 256 THEN RAISE EXCEPTION 'invalid life endings' USING ERRCODE = '22023'; END IF; PERFORM evolution.check_region_lease(p_world, p_region, p_holder, p_epoch); IF NOT EXISTS (SELECT 1 FROM evolution.region_commits x WHERE x.world_id = p_world AND x.region_id = p_region AND x.commit_id = p_commit) THEN RAISE EXCEPTION 'life ending commit not in region' USING ERRCODE = '22023'; END IF; n := octet_length(p_identities) / 8; FOR i IN 0 .. n - 1 LOOP ending_identity := evolution.manifest_uint(p_identities, 8 * i, 8); SELECT * INTO r FROM evolution.life_reservations x WHERE x.world_id = p_world AND x.identity = ending_identity FOR UPDATE; IF NOT FOUND THEN CONTINUE; -- An opening founder was never reserved; nothing blocks its account. END IF; IF r.region_id <> p_region OR r.rejection <> 0 THEN RAISE EXCEPTION 'life ending not owned' USING ERRCODE = 'EVL04'; END IF; IF r.ended_commit IS NULL THEN UPDATE evolution.life_reservations SET ended_commit = p_commit WHERE world_id = p_world AND account_id = r.account_id AND request_id = r.request_id; ended := ended + 1; END IF; END LOOP; RETURN ended; END $$; REVOKE ALL ON FUNCTION evolution.life_ending_is_final(), evolution.end_cellular_lives(evolution.u64, evolution.u64, uuid, evolution.u64, uuid, bytea) FROM PUBLIC; GRANT EXECUTE ON FUNCTION evolution.end_cellular_lives(evolution.u64, evolution.u64, uuid, evolution.u64, uuid, bytea) TO :"runtime_role"; INSERT INTO evolution.schema_migrations(version, sha256) VALUES (37, :'migration_sha_37');