-- ADR-0120 amendment: a player may decline an outstanding mutation offer. A decline is an -- ordinary reserved choice whose operator id is 0: it reserves a successor identity like -- any choice (that identity simply stays unused), names the exact offer, and publishes -- with result 3 (declined). The offer is consumed and the genome is unchanged. -- Reserved inputs and trace selections may name operator 0. CREATE OR REPLACE FUNCTION evolution.valid_cellular_mutation_input(p_input bytea) RETURNS boolean LANGUAGE plpgsql IMMUTABLE STRICT SET search_path = pg_catalog, pg_temp AS $$ BEGIN IF octet_length(p_input) <> 34 THEN RETURN false; END IF; IF evolution.manifest_uint(p_input, 0, 8) = 0 OR evolution.manifest_uint(p_input, 8, 8) = 0 OR evolution.manifest_uint(p_input, 16, 8) = 0 OR evolution.manifest_uint(p_input, 24, 8) = 0 OR evolution.manifest_uint(p_input, 0, 8) <= evolution.manifest_uint(p_input, 24, 8) THEN RETURN false; END IF; RETURN true; END $$; CREATE OR REPLACE FUNCTION evolution.cellular_mutation_selections(p_trace bytea, p_manifest bytea) RETURNS TABLE(tick evolution.u64, life_id evolution.u64, event_index evolution.u64, offer_tick evolution.u64, successor_identity evolution.u64, operator_id integer) LANGUAGE plpgsql IMMUTABLE SECURITY INVOKER SET search_path = pg_catalog, pg_temp AS $$ DECLARE c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(p_manifest); ticks integer; off integer := 112; intent_count integer; birth_count integer; mutation_count integer; previous_life numeric; BEGIN PERFORM * FROM evolution.cellular_births(p_trace, p_manifest); -- Binding and structure. IF evolution.manifest_uint(p_trace, 8, 4) <> 3 THEN RETURN; -- Format 2 carries no selections. END IF; ticks := (c.end_tick - c.begin_tick)::integer; FOR tick_index IN 0 .. ticks - 1 LOOP intent_count := evolution.manifest_uint(p_trace, off, 4)::integer; birth_count := evolution.manifest_uint(p_trace, off + 4, 4)::integer; mutation_count := evolution.manifest_uint(p_trace, off + 8, 4)::integer; off := off + 16 + intent_count * 24 + birth_count * 40; previous_life := 0; FOR mutation_index IN 0 .. mutation_count - 1 LOOP tick := c.begin_tick + tick_index + 1; life_id := evolution.manifest_uint(p_trace, off, 8); event_index := evolution.manifest_uint(p_trace, off + 8, 8); offer_tick := evolution.manifest_uint(p_trace, off + 16, 8); successor_identity := evolution.manifest_uint(p_trace, off + 24, 8); operator_id := evolution.manifest_uint(p_trace, off + 32, 2)::integer; IF life_id = 0 OR life_id <= previous_life OR event_index = 0 OR offer_tick = 0 OR offer_tick >= tick OR successor_identity = 0 OR substring(p_trace FROM off + 35 FOR 6) <> '\x000000000000' THEN RAISE EXCEPTION 'invalid cellular mutation selection' USING ERRCODE = '22023'; END IF; previous_life := life_id; off := off + 40; RETURN NEXT; END LOOP; END LOOP; END $$; -- Results 0 applied, 1 life died, 2 insufficient funding, 3 declined. CREATE OR REPLACE FUNCTION evolution.check_cellular_mutation_block(p_manifest bytea, p_trace bytea, p_mutations bytea, p_results bytea) RETURNS integer LANGUAGE plpgsql STABLE SECURITY INVOKER SET search_path = pg_catalog, pg_temp AS $$ DECLARE c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(p_manifest); n integer; account uuid; account_bytes bytea; action numeric; tick numeric; input bytea; previous_tick numeric := -1; previous_account bytea := ''::bytea; previous_action numeric := -1; r evolution.cellular_mutation_reservations%ROWTYPE; BEGIN IF p_mutations IS NULL OR p_results IS NULL OR octet_length(p_mutations) = 0 OR octet_length(p_mutations) % 64 <> 0 OR octet_length(p_mutations) / 64 > 1024 OR octet_length(p_results) <> octet_length(p_mutations) / 64 THEN RAISE EXCEPTION 'invalid cellular mutation block' USING ERRCODE = '22023'; END IF; n := octet_length(p_mutations) / 64; IF (SELECT count(*) FROM evolution.cellular_mutation_selections(p_trace, p_manifest)) <> n THEN RAISE EXCEPTION 'cellular mutation block does not cover the trace' USING ERRCODE = '22023'; END IF; FOR i IN 0 .. n - 1 LOOP account_bytes := substring(p_mutations FROM 64 * i + 1 FOR 16); account := encode(account_bytes, 'hex')::uuid; action := evolution.manifest_uint(p_mutations, 64 * i + 16, 8); input := substring(p_mutations FROM 64 * i + 25 FOR 34); tick := evolution.manifest_uint(input, 0, 8); IF account = '00000000-0000-0000-0000-000000000000' OR action = 0 OR NOT evolution.valid_cellular_mutation_input(input) OR substring(p_mutations FROM 64 * i + 59 FOR 6) <> '\x000000000000' OR get_byte(p_results, i) > 3 OR tick <= c.begin_tick OR tick > c.end_tick OR (tick, account_bytes, action) <= (previous_tick, previous_account, previous_action) THEN RAISE EXCEPTION 'invalid cellular mutation record' USING ERRCODE = '22023'; END IF; previous_tick := tick; previous_account := account_bytes; previous_action := action; SELECT * INTO r FROM evolution.cellular_mutation_reservations x WHERE x.world_id = c.world_id AND x.account_id = account AND x.action_id = action; IF NOT FOUND OR r.region_id <> c.region_id OR r.input_bytes <> input OR r.target_tick <> evolution.manifest_uint(input, 0, 8) THEN RAISE EXCEPTION 'cellular mutation reservation conflicts' USING ERRCODE = 'EVL16'; END IF; IF NOT EXISTS (SELECT 1 FROM evolution.cellular_mutation_selections(p_trace, p_manifest) s WHERE s.tick = r.target_tick AND s.successor_identity = r.successor_identity AND s.life_id = evolution.manifest_uint(input, 8, 8) AND s.event_index = evolution.manifest_uint(input, 16, 8) AND s.offer_tick = evolution.manifest_uint(input, 24, 8) AND s.operator_id = evolution.manifest_uint(input, 32, 2)) THEN RAISE EXCEPTION 'cellular mutation differs from its trace' USING ERRCODE = '22023'; END IF; END LOOP; RETURN n; END $$; ALTER TABLE evolution.cellular_mutation_action_publications DROP CONSTRAINT cellular_mutation_action_publications_result_check, ADD CONSTRAINT cellular_mutation_action_publications_result_check CHECK (result BETWEEN 0 AND 3); INSERT INTO evolution.schema_migrations(version, sha256) VALUES (40, :'migration_sha_40');