From 4bc8dfc05d045ae3b4f1a6aae826c250372379ff Mon Sep 17 00:00:00 2001 From: dingo Date: Sun, 11 Oct 2026 07:20:26 -0400 Subject: [PATCH] Add durable cellular mutation reservations --- docs/engineering/M11_4_DELIVERY_PLAN.md | 12 +- docs/engineering/VALIDATION.md | 9 ++ .../persistence/life_reservation_store.hpp | 26 ++- .../0029_cellular_mutation_reservations.sql | 149 ++++++++++++++++++ migrations/apply.sql | 18 ++- src/persistence/region_lease_store.cpp | 79 ++++++++++ tests/life_reservation_store_tests.cpp | 33 ++++ tools/ci/postgres-check.sh | 13 +- tools/migrate-postgres.sh | 3 + 9 files changed, 328 insertions(+), 14 deletions(-) create mode 100644 migrations/0029_cellular_mutation_reservations.sql diff --git a/docs/engineering/M11_4_DELIVERY_PLAN.md b/docs/engineering/M11_4_DELIVERY_PLAN.md index e3bdafc..5380e25 100644 --- a/docs/engineering/M11_4_DELIVERY_PLAN.md +++ b/docs/engineering/M11_4_DELIVERY_PLAN.md @@ -67,8 +67,10 @@ pending; no gameplay balance is treated as final by this plan. archive API/codec, cellular life owner/snapshot, checkpoint version bundle/migration, replay trace, SQL action publication, host/session and crash/retry tests. The archive API/codec, owner v3 snapshot and explicit - no-tick v2-to-v3 manifest/SQL migration are implemented locally with focused - tests. Durable selection, trace replay and atomic action publication remain - to be implemented; no 11.4c or live mutation path is claimed complete. - Windows build and PostgreSQL checks are pending the next available Linux CI - run (see [validation log](VALIDATION.md)). + no-tick v2-to-v3 manifest/SQL migration are implemented. The current 11.4c + slice adds schema v29's fenced, idempotent mutation reservation and + lease-fenced pending-action recovery API. Selection is still not admitted by + the region host: exact offers, replayed action records, atomic + biology/archive publication and crash recovery remain. No 11.4c or live + mutation path is claimed complete until those pieces pass CI (see + [validation log](VALIDATION.md)). diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index 361cc53..dac4947 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -6812,3 +6812,12 @@ CI run 268 compiled the v10 codec and passed all 68 CTests, including the checkpoint migration and protocol tests. Its format-check then rejected the new checkpoint test call layout at `tests/region_checkpoint_tests.cpp:478`; that wrapping is corrected for the next run. The run is not a green gate. + +The M11.4c schema-v29 reservation slice adds a world-allocated successor +identity reservation keyed by authenticated account and action ID, exact-retry +conflict handling, one pending choice per account, lease-fenced recovery reads, +and PostgreSQL integration coverage. It remains unverified until the pushed CI +run completes. The local PostgreSQL target reconfigured, then produced no build +output for 60 seconds and was stopped; `psql` is unavailable in this Windows +environment. `clang-format --dry-run --Werror` passed for the changed C++ files. +The user-directed validation path here is CI. diff --git a/include/evo/persistence/life_reservation_store.hpp b/include/evo/persistence/life_reservation_store.hpp index a5cc06e..b6a2e90 100644 --- a/include/evo/persistence/life_reservation_store.hpp +++ b/include/evo/persistence/life_reservation_store.hpp @@ -1,6 +1,7 @@ #pragma once #include "evo/persistence/postgres.hpp" #include "evo/persistence/region_lease.hpp" +#include #include #include #include @@ -16,7 +17,9 @@ enum class LifeReservationError : std::uint8_t { exhausted, invalid, store, - unknown // May have committed. Reconnect and retry the SAME request to reconcile. + unknown, // May have committed. Reconnect and retry the SAME request to reconcile. + pending_mutation, + identity_conflict }; struct LifeReservationFailure { LifeReservationError kind{}; @@ -46,6 +49,14 @@ struct PrimitiveLifeOutcome { std::optional published; friend bool operator==(const PrimitiveLifeOutcome&, const PrimitiveLifeOutcome&) = default; }; +inline constexpr std::size_t cellular_mutation_reservation_input_bytes = 34; +struct CellularMutationReservation { + Uuid account{}; + std::uint64_t action_id{}, region{}, generation{}, target_tick{}, successor_identity{}; + std::array input{}; + friend bool operator==(const CellularMutationReservation&, + const CellularMutationReservation&) = default; +}; // Exact retry returns the original identity/owning region/refusal, even after // account control moves. The result grants no authority in the returned region. // request_id is in the new-life namespace, separate from M10 probe action IDs. @@ -74,4 +85,17 @@ query_primitive_life(Postgres& db, RegionKey key, const LeaseGrant& lease, const [[nodiscard]] std::expected, LifeReservationFailure> primitive_life_owner(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account, std::chrono::milliseconds timeout); +// Schema v29, ADR-0121. The canonical bytes are target tick (u64 LE), life ID +// (u64 LE), offer event index (u64 LE), offer tick (u64 LE), and operator ID +// (u16 LE). Exact retries return the same world-allocated successor identity. +[[nodiscard]] std::expected +reserve_cellular_mutation( + Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account, + std::uint64_t generation, std::uint64_t action_id, + const std::array& input, + std::chrono::milliseconds timeout); +// Pending selections are reloaded under the recovered region lease after restart. +[[nodiscard]] std::expected, LifeReservationFailure> +load_pending_cellular_mutations(Postgres& db, RegionKey key, const LeaseGrant& lease, + std::chrono::milliseconds timeout); } // namespace evo::persistence diff --git a/migrations/0029_cellular_mutation_reservations.sql b/migrations/0029_cellular_mutation_reservations.sql new file mode 100644 index 0000000..af8762f --- /dev/null +++ b/migrations/0029_cellular_mutation_reservations.sql @@ -0,0 +1,149 @@ +-- M11.4c, ADR-0121: reserve a successor identity durably for an exact mutation choice. +-- Publication/result binding is added by the later mutation-publication migration. +CREATE TABLE evolution.cellular_mutation_reservations ( + world_id evolution.u64 NOT NULL REFERENCES evolution.life_allocators, + account_id uuid NOT NULL CHECK (account_id <> '00000000-0000-0000-0000-000000000000'), + action_id evolution.u64 NOT NULL CHECK (action_id > 0), + region_id evolution.u64 NOT NULL, + generation evolution.u64 NOT NULL CHECK (generation > 0), + target_tick evolution.u64 NOT NULL CHECK (target_tick > 0), + successor_identity evolution.u64 NOT NULL CHECK (successor_identity > 0), + input_bytes bytea NOT NULL CHECK (octet_length(input_bytes) = 34), + PRIMARY KEY (world_id, account_id, action_id), + UNIQUE (world_id, account_id, action_id, region_id), + UNIQUE (world_id, successor_identity), + FOREIGN KEY (world_id, region_id) REFERENCES evolution.region_leases +); +REVOKE ALL ON evolution.cellular_mutation_reservations FROM PUBLIC; +CREATE TRIGGER cellular_mutation_reservations_immutable BEFORE UPDATE OR DELETE + ON evolution.cellular_mutation_reservations FOR EACH ROW + EXECUTE FUNCTION evolution.refuse_history_rewrite(); +CREATE TRIGGER cellular_mutation_reservations_no_truncate BEFORE TRUNCATE + ON evolution.cellular_mutation_reservations FOR EACH STATEMENT + EXECUTE FUNCTION evolution.refuse_history_rewrite(); + +CREATE TABLE evolution.cellular_mutation_action_publications ( + world_id evolution.u64 NOT NULL, + region_id evolution.u64 NOT NULL, + account_id uuid NOT NULL, + action_id evolution.u64 NOT NULL, + commit_id uuid NOT NULL, + commit_digest bytea NOT NULL CHECK (octet_length(commit_digest) = 32), + commit_sequence evolution.u64 NOT NULL CHECK (commit_sequence > 0), + result smallint NOT NULL CHECK (result BETWEEN 0 AND 2), + PRIMARY KEY (world_id, account_id, action_id), + FOREIGN KEY (world_id, account_id, action_id, region_id) + REFERENCES evolution.cellular_mutation_reservations(world_id, account_id, action_id, region_id), + FOREIGN KEY (world_id, region_id, commit_sequence, commit_id, commit_digest) + REFERENCES evolution.region_commits(world_id, region_id, commit_sequence, commit_id, + commit_digest) + DEFERRABLE INITIALLY DEFERRED +); +REVOKE ALL ON evolution.cellular_mutation_action_publications FROM PUBLIC; +CREATE TRIGGER cellular_mutation_action_publications_immutable BEFORE UPDATE OR DELETE + ON evolution.cellular_mutation_action_publications FOR EACH ROW + EXECUTE FUNCTION evolution.refuse_history_rewrite(); +CREATE TRIGGER cellular_mutation_action_publications_no_truncate BEFORE TRUNCATE + ON evolution.cellular_mutation_action_publications FOR EACH STATEMENT + EXECUTE FUNCTION evolution.refuse_history_rewrite(); + +CREATE 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) + OR evolution.manifest_uint(p_input, 32, 2) = 0 THEN + RETURN false; + END IF; + RETURN true; +END $$; + +-- Lock order is lease -> account control -> mutation reservation -> shared world allocator. +-- A repeated action with different bytes is a conflict, and only one unbound +-- selection per account may exist until publication records its terminal outcome. +CREATE FUNCTION evolution.reserve_cellular_mutation(p_world evolution.u64, + p_region evolution.u64, p_holder uuid, p_epoch evolution.u64, p_account uuid, + p_generation evolution.u64, p_action evolution.u64, p_target_tick evolution.u64, + p_input bytea) +RETURNS TABLE(successor_identity evolution.u64, action_region evolution.u64) +LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ +DECLARE r evolution.cellular_mutation_reservations%ROWTYPE; allocated numeric; +BEGIN + IF p_action IS NULL OR p_action = 0 OR p_target_tick IS NULL OR p_target_tick = 0 + OR NOT evolution.valid_cellular_mutation_input(p_input) + OR evolution.manifest_uint(p_input, 0, 8) <> p_target_tick THEN + RAISE EXCEPTION 'invalid cellular mutation action' 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.cellular_mutation_reservations x + WHERE x.world_id = p_world AND x.account_id = p_account AND x.action_id = p_action; + IF FOUND THEN + IF r.region_id <> p_region THEN + RAISE EXCEPTION 'cellular mutation belongs to another region' USING ERRCODE = 'EVL04'; + END IF; + IF r.target_tick <> p_target_tick OR r.input_bytes <> p_input THEN + RAISE EXCEPTION 'cellular mutation action identity reused' USING ERRCODE = 'EVL07'; + END IF; + RETURN QUERY SELECT r.successor_identity, r.region_id; + RETURN; + END IF; + IF EXISTS (SELECT 1 FROM evolution.cellular_mutation_reservations x + LEFT JOIN evolution.cellular_mutation_action_publications p + USING (world_id, account_id, action_id) + WHERE x.world_id = p_world AND x.account_id = p_account AND p.action_id IS NULL) THEN + RAISE EXCEPTION 'cellular mutation is already pending' USING ERRCODE = 'EVL18'; + 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); + UPDATE evolution.life_allocators SET next_identity = allocated + 1 WHERE world_id = p_world; + INSERT INTO evolution.cellular_mutation_reservations(world_id, account_id, action_id, + region_id, generation, target_tick, successor_identity, input_bytes) + VALUES (p_world, p_account, p_action, p_region, p_generation, p_target_tick, + allocated, p_input); + RETURN QUERY SELECT allocated::evolution.u64, p_region; +END $$; + +CREATE FUNCTION evolution.load_pending_cellular_mutations(p_world evolution.u64, + p_region evolution.u64, p_holder uuid, p_epoch evolution.u64) +RETURNS TABLE(account_id uuid, action_id evolution.u64, generation evolution.u64, + target_tick evolution.u64, successor_identity evolution.u64, input_bytes bytea) +LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ +BEGIN + PERFORM evolution.check_region_lease(p_world, p_region, p_holder, p_epoch); + RETURN QUERY SELECT r.account_id, r.action_id, r.generation, r.target_tick, + r.successor_identity, r.input_bytes + FROM evolution.cellular_mutation_reservations r + WHERE r.world_id = p_world AND r.region_id = p_region + AND NOT EXISTS (SELECT 1 FROM evolution.cellular_mutation_action_publications p + WHERE p.world_id = r.world_id AND p.account_id = r.account_id AND p.action_id = r.action_id) + ORDER BY r.target_tick, r.account_id, r.action_id + LIMIT 257; +END $$; + +REVOKE ALL ON FUNCTION evolution.valid_cellular_mutation_input(bytea), + evolution.reserve_cellular_mutation(evolution.u64,evolution.u64,uuid,evolution.u64, + uuid,evolution.u64,evolution.u64,evolution.u64,bytea), + evolution.load_pending_cellular_mutations(evolution.u64,evolution.u64,uuid,evolution.u64) + FROM PUBLIC; +GRANT EXECUTE ON FUNCTION + evolution.reserve_cellular_mutation(evolution.u64,evolution.u64,uuid,evolution.u64, + uuid,evolution.u64,evolution.u64,evolution.u64,bytea), + evolution.load_pending_cellular_mutations(evolution.u64,evolution.u64,uuid,evolution.u64) + TO :"runtime_role"; +INSERT INTO evolution.schema_migrations(version, sha256) VALUES (29, :'migration_sha_29'); diff --git a/migrations/apply.sql b/migrations/apply.sql index 8edeb47..2a7cedd 100644 --- a/migrations/apply.sql +++ b/migrations/apply.sql @@ -102,7 +102,10 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') SELECT right(:'history', length(',26:' || :'migration_sha_26')) = ',26:' || :'migration_sha_26' AS at_v26 \gset SELECT right(:'history', length(',27:' || :'migration_sha_27')) = ',27:' || :'migration_sha_27' AS at_v27 \gset SELECT right(:'history', length(',28:' || :'migration_sha_28')) = ',28:' || :'migration_sha_28' AS at_v28 \gset - \if :at_v28 + SELECT right(:'history', length(',29:' || :'migration_sha_29')) = ',29:' || :'migration_sha_29' AS at_v29 \gset + \if :at_v29 + \elif :at_v28 + \ir 0029_cellular_mutation_reservations.sql \elif :at_v27 \ir 0028_cellular_v3_trace_bounds.sql \elif :at_v26 @@ -449,25 +452,32 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24, coalesce(max(version), 0) = 25 AS at_v25, coalesce(max(version), 0) = 26 AS at_v26, coalesce(max(version), 0) = 27 AS at_v27, - coalesce(max(version), 0) = 28 AS at_v28 + coalesce(max(version), 0) = 28 AS at_v28, + coalesce(max(version), 0) = 29 AS at_v29 FROM evolution.schema_migrations \gset \if :at_v24 \ir 0025_cellular_v2_bundle.sql \ir 0026_cellular_profile_migration.sql \ir 0027_cellular_v3_migration.sql \ir 0028_cellular_v3_trace_bounds.sql + \ir 0029_cellular_mutation_reservations.sql \elif :at_v25 \ir 0026_cellular_profile_migration.sql \ir 0027_cellular_v3_migration.sql \ir 0028_cellular_v3_trace_bounds.sql + \ir 0029_cellular_mutation_reservations.sql \elif :at_v26 \ir 0027_cellular_v3_migration.sql \ir 0028_cellular_v3_trace_bounds.sql + \ir 0029_cellular_mutation_reservations.sql \elif :at_v27 \ir 0028_cellular_v3_trace_bounds.sql + \ir 0029_cellular_mutation_reservations.sql \elif :at_v28 + \ir 0029_cellular_mutation_reservations.sql +\elif :at_v29 \else - \echo 'Unknown or changed migration history after v28' + \echo 'Unknown or changed migration history after v29' DO $$ BEGIN RAISE EXCEPTION 'unknown or changed migration history'; END $$; \endif -- Effective-privilege audit on every run, including no-ops (ADR-0060). Attributes @@ -494,6 +504,8 @@ BEGIN ('evolution.query_primitive_life(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64,evolution.u64)'::regprocedure::oid), ('evolution.primitive_life_owner(evolution.u64,evolution.u64,uuid,evolution.u64,uuid)'::regprocedure::oid), ('evolution.reserve_primitive_life(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64,evolution.u64,integer)'::regprocedure::oid), + ('evolution.reserve_cellular_mutation(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64,evolution.u64,evolution.u64,bytea)'::regprocedure::oid), + ('evolution.load_pending_cellular_mutations(evolution.u64,evolution.u64,uuid,evolution.u64)'::regprocedure::oid), ('evolution.reject_primitive_life(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64,evolution.u64,smallint)'::regprocedure::oid), ('evolution.acquire_region_lease(evolution.u64,evolution.u64,integer)'::regprocedure::oid), ('evolution.acquire_region_lease(evolution.u64,evolution.u64,integer,bytea)'::regprocedure::oid), diff --git a/src/persistence/region_lease_store.cpp b/src/persistence/region_lease_store.cpp index 6d16d37..29e3449 100644 --- a/src/persistence/region_lease_store.cpp +++ b/src/persistence/region_lease_store.cpp @@ -858,6 +858,10 @@ LifeReservationFailure life_failure(const PgFailure& f) { kind = LifeReservationError::not_available; else if (state == "EVL05") kind = LifeReservationError::exhausted; + else if (state == "EVL07") + kind = LifeReservationError::identity_conflict; + else if (state == "EVL18") + kind = LifeReservationError::pending_mutation; return {kind, f}; } } // namespace @@ -1252,4 +1256,79 @@ overdue_probe_transfers(Postgres& db, RegionKey key, const LeaseGrant& lease, } return out; } + +std::expected reserve_cellular_mutation( + Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account, + std::uint64_t generation, std::uint64_t action_id, + const std::array& input, + milliseconds timeout) { + const auto target_tick = little(input, 0, 8); + if (account == Uuid{} || action_id == 0 || generation == 0 || target_tick == 0 || + little(input, 8, 8) == 0 || little(input, 16, 8) == 0 || little(input, 24, 8) == 0 || + target_tick <= little(input, 24, 8) || little(input, 32, 2) == 0) + return std::unexpected{LifeReservationFailure{LifeReservationError::invalid, {}}}; + const auto base = control_parameters(key, lease, account, generation); + const std::array p{base[0], + base[1], + base[2], + base[3], + base[4], + base[5], + std::to_string(action_id), + std::to_string(target_tick), + "\\x" + hex(input)}; + auto rows = db.query("SELECT * FROM evolution.reserve_cellular_mutation($1::evolution.u64, " + "$2::evolution.u64, $3::uuid, $4::evolution.u64, $5::uuid, " + "$6::evolution.u64, $7::evolution.u64, $8::evolution.u64, $9::bytea)", + p, timeout); + if (!rows) + return std::unexpected{life_failure(rows.error())}; + if (rows->size() != 1 || (*rows)[0].size() != 2) + return std::unexpected{LifeReservationFailure{LifeReservationError::unknown, {}}}; + const auto identity = parse_integer((*rows)[0][0]); + const auto region = parse_integer((*rows)[0][1]); + if (!identity || *identity == 0 || !region || *region == 0) + return std::unexpected{LifeReservationFailure{LifeReservationError::unknown, {}}}; + return CellularMutationReservation{account, action_id, *region, generation, + target_tick, *identity, input}; +} + +std::expected, LifeReservationFailure> +load_pending_cellular_mutations(Postgres& db, RegionKey key, const LeaseGrant& lease, + milliseconds timeout) { + const std::array p{std::to_string(key.world), std::to_string(key.region), + uuid_text(lease.holder), std::to_string(lease.epoch)}; + auto rows = db.query("SELECT account_id::text, action_id::text, generation::text, " + "target_tick::text, successor_identity::text, " + "encode(input_bytes, 'hex') FROM " + "evolution.load_pending_cellular_mutations($1::evolution.u64, " + "$2::evolution.u64, $3::uuid, $4::evolution.u64)", + p, timeout); + if (!rows) + return std::unexpected{life_failure(rows.error())}; + if (rows->size() > 256) + return std::unexpected{LifeReservationFailure{LifeReservationError::store, {}}}; + std::vector out; + out.reserve(rows->size()); + for (const auto& row : *rows) { + if (row.size() != 6) + return std::unexpected{LifeReservationFailure{LifeReservationError::store, {}}}; + const auto account = parse_uuid(row[0]); + const auto action = parse_integer(row[1]); + const auto generation = parse_integer(row[2]); + const auto target_tick = parse_integer(row[3]); + const auto successor = parse_integer(row[4]); + const auto input = + row[5] ? unhex(*row[5]) : std::nullopt; + if (!account || *account == Uuid{} || !action || *action == 0 || !generation || + *generation == 0 || !target_tick || *target_tick == 0 || !successor || + *successor == 0 || !input || little(*input, 0, 8) != *target_tick || + little(*input, 8, 8) == 0 || little(*input, 16, 8) == 0 || little(*input, 24, 8) == 0 || + *target_tick <= little(*input, 24, 8) || little(*input, 32, 2) == 0) + return std::unexpected{LifeReservationFailure{LifeReservationError::store, {}}}; + out.push_back( + {*account, *action, key.region, *generation, *target_tick, *successor, *input}); + } + return out; +} } // namespace evo::persistence diff --git a/tests/life_reservation_store_tests.cpp b/tests/life_reservation_store_tests.cpp index 5d0d062..7959987 100644 --- a/tests/life_reservation_store_tests.cpp +++ b/tests/life_reservation_store_tests.cpp @@ -123,6 +123,39 @@ void run_life_reservation_store_tests() { const auto counter = admin->query( "SELECT next_identity FROM evolution.life_allocators WHERE world_id=9752", {}, 5s); CHECK(counter && (*counter)[0][0] == "9223372036854775811"); + std::array mutation_input{}; + auto put_u64 = [&](std::size_t offset, std::uint64_t value) { + for (std::size_t i = 0; i < 8; ++i) + mutation_input[offset + i] = static_cast(value >> (i * 8)); + }; + put_u64(0, 20); // target tick + put_u64(8, first->identity); + put_u64(16, 1); // exact offer event + put_u64(24, 10); // offer tick + mutation_input[32] = 1; // operator id + const auto mutation = + reserve_cellular_mutation(*db, a, *lease_a, alice, 1, 500, mutation_input, 5s); + CHECK(mutation && mutation->region == a.region && mutation->target_tick == 20 && + mutation->successor_identity == 9223372036854775811ULL && + mutation->input == mutation_input); + db = connect_as(env("EVO_PG_RUNTIME_USER")); + CHECK(reserve_cellular_mutation(*db, a, *lease_a, alice, 1, 500, mutation_input, 5s) == + *mutation); + auto changed_mutation_input = mutation_input; + changed_mutation_input[32] = 2; + CHECK(failed( + reserve_cellular_mutation(*db, a, *lease_a, alice, 1, 500, changed_mutation_input, 5s), + LifeReservationError::identity_conflict)); + CHECK(failed(reserve_cellular_mutation(*db, a, *lease_a, alice, 1, 501, mutation_input, 5s), + LifeReservationError::pending_mutation)); + const auto pending_mutations = load_pending_cellular_mutations(*db, a, *lease_a, 5s); + CHECK(pending_mutations && pending_mutations->size() == 1 && + pending_mutations->front() == *mutation); + auto invalid_mutation_input = mutation_input; + invalid_mutation_input[32] = 0; // operator ID zero is invalid + CHECK(failed( + reserve_cellular_mutation(*db, a, *lease_a, alice, 1, 502, invalid_mutation_input, 5s), + LifeReservationError::invalid)); CHECK(reject_primitive_life(*db, a, *lease_a, alice, 1, 3, LifeRefusal::no_placement, 5s) == LifeRefusal::no_placement); const auto pending = reserve(4); diff --git a/tools/ci/postgres-check.sh b/tools/ci/postgres-check.sh index 01b3b11..d1dd18c 100644 --- a/tools/ci/postgres-check.sh +++ b/tools/ci/postgres-check.sh @@ -69,7 +69,7 @@ PGDATABASE=upgrade_path psql -X -v ON_ERROR_STOP=1 -1 -v "migration_sha=$v1_sha" -f "$root/migrations/0001_region_bootstrap.sql" > /dev/null test "$(PGDATABASE=upgrade_path psql -XAtc "SELECT string_agg(version::text, ',') FROM evolution.schema_migrations")" = 1 PGDATABASE=upgrade_path bash "$root/tools/migrate-postgres.sh" -test "$(PGDATABASE=upgrade_path psql -XAtc "SELECT string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28 +test "$(PGDATABASE=upgrade_path psql -XAtc "SELECT string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29 # v2 heads have no payload binding: v2->v3 fails closed and leaves history at v2. v2_sha="$(sha256sum "$root/migrations/0002_region_fencing.sql")" v2_sha="${v2_sha%% *}" @@ -90,7 +90,7 @@ PGDATABASE=v3_commits psql -X -v ON_ERROR_STOP=1 \ -c "INSERT INTO evolution.retained_builds VALUES (decode(repeat('b1', 32), 'hex'))" \ -c "SELECT evolution.provision_region_checkpoint(decode('4145564f52434d31' || '01000000' || '01000000' || '0100000000000000' || '0100000000000000' || '0900000000000000' || repeat('11', 16) || repeat('00', 8) || repeat('00', 16) || repeat('00', 32) || repeat('00', 16) || repeat('00', 16) || repeat('00', 8) || '50c3000000000000' || '020000000200000001000000010000000100000001000000010000000100000001000000' || repeat('b1', 32) || '00000000' || '00000000' || '01000000' || '6400000000000000' || repeat('a0', 32), 'hex'))" > /dev/null PGDATABASE=v3_commits bash "$root/tools/migrate-postgres.sh" -test "$(PGDATABASE=v3_commits psql -XAtc "SELECT count(*) || ':' || string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 28:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28 +test "$(PGDATABASE=v3_commits psql -XAtc "SELECT count(*) || ':' || string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 29:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29 test "$(PGDATABASE=v3_commits psql -XAtc "SELECT count(*) FROM evolution.region_commits")" = 1 # Review P1: a v4 database holding a published (possibly terminal) checkpoint has # no durable decisions for it. The v5 upgrade must refuse before changing anything. @@ -144,7 +144,7 @@ PGDATABASE=v6_populated psql -X -v ON_ERROR_STOP=1 \ -c "INSERT INTO evolution.retained_builds VALUES (decode(repeat('b1', 32), 'hex'))" \ -c "SELECT evolution.provision_region_checkpoint(decode('4145564f52434d31' || '02000000' || '01000000' || '0100000000000000' || '0100000000000000' || '0900000000000000' || repeat('11', 16) || repeat('00', 8) || repeat('00', 16) || repeat('00', 32) || repeat('00', 16) || repeat('00', 16) || repeat('00', 8) || '50c3000000000000' || '020000000200000001000000010000000100000001000000010000000100000001000000' || repeat('b1', 32) || '00000000' || '00000000' || '01000000' || '6400000000000000' || repeat('a0', 32) || repeat('00', 80), 'hex'))" > /dev/null PGDATABASE=v6_populated bash "$root/tools/migrate-postgres.sh" -test "$(PGDATABASE=v6_populated psql -XAtc "SELECT string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28 +test "$(PGDATABASE=v6_populated psql -XAtc "SELECT string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29 test "$(PGDATABASE=v6_populated psql -XAtc "SELECT count(*) FROM evolution.region_commits c, LATERAL evolution.parse_checkpoint_manifest(c.manifest) m WHERE m.commit_sequence = 0 AND m.predecessor_commit IS NULL")" = 1 test "$(PGDATABASE=v6_populated psql -XAtc "SELECT string_agg(conname, ',') FROM pg_constraint WHERE conrelid = 'evolution.region_heads'::regclass AND contype = 'c' AND pg_get_constraintdef(oid) LIKE '%end_tick%'")" = region_heads_published_tick # Inherited privileges bypass fencing, so every migrate run audits effective rights. @@ -185,14 +185,17 @@ expect_audit_failure 'GRANT SELECT ON evolution.region_actions TO evo_runtime_te expect_audit_failure 'GRANT SELECT ON evolution.region_commit_actions TO evo_runtime_test' 'REVOKE SELECT ON evolution.region_commit_actions FROM evo_runtime_test' expect_audit_failure 'GRANT EXECUTE ON FUNCTION evolution.check_action_block(bytea, numeric, numeric) TO evo_runtime_test' 'REVOKE EXECUTE ON FUNCTION evolution.check_action_block(bytea, numeric, numeric) FROM evo_runtime_test' expect_audit_failure 'GRANT EXECUTE ON FUNCTION evolution.find_region_action_record(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64) TO evo_runtime_test' 'REVOKE EXECUTE ON FUNCTION evolution.find_region_action_record(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64) FROM evo_runtime_test' +# Schema v29 mutation reservations and their publication history are fenced. +expect_audit_failure 'GRANT SELECT ON evolution.cellular_mutation_reservations TO evo_runtime_test' 'REVOKE SELECT ON evolution.cellular_mutation_reservations FROM evo_runtime_test' +expect_audit_failure 'GRANT SELECT ON evolution.cellular_mutation_action_publications TO evo_runtime_test' 'REVOKE SELECT ON evolution.cellular_mutation_action_publications FROM evo_runtime_test' # Schema v10: account controls are reachable only through the three fenced functions. expect_audit_failure 'GRANT SELECT ON evolution.account_controls TO evo_runtime_test' 'REVOKE SELECT ON evolution.account_controls FROM evo_runtime_test' expect_audit_failure 'REVOKE EXECUTE ON FUNCTION evolution.claim_account_control(evolution.u64, evolution.u64, uuid, evolution.u64, uuid, evolution.u64, evolution.u64, integer) FROM evo_runtime_test' 'GRANT EXECUTE ON FUNCTION evolution.claim_account_control(evolution.u64, evolution.u64, uuid, evolution.u64, uuid, evolution.u64, evolution.u64, integer) TO evo_runtime_test' bash "$root/tools/migrate-postgres.sh" # Restored configuration passes the audit again. # Unknown future versions and checksum drift must not be repaired silently. -psql -X -v ON_ERROR_STOP=1 -c 'UPDATE evolution.schema_migrations SET version=29 WHERE version=28' +psql -X -v ON_ERROR_STOP=1 -c 'UPDATE evolution.schema_migrations SET version=30 WHERE version=29' if bash "$root/tools/migrate-postgres.sh"; then exit 1; fi -psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET version=28 WHERE version=29" +psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET version=29 WHERE version=30" psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET sha256=repeat('0',64) WHERE version=14" if bash "$root/tools/migrate-postgres.sh"; then exit 1; fi psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET sha256=repeat('0',64) WHERE version=13" diff --git a/tools/migrate-postgres.sh b/tools/migrate-postgres.sh index 3714d88..b55d73c 100644 --- a/tools/migrate-postgres.sh +++ b/tools/migrate-postgres.sh @@ -68,6 +68,8 @@ digest_27="$(sha256sum "$root/migrations/0027_cellular_v3_migration.sql")" digest_27="${digest_27%% *}" digest_28="$(sha256sum "$root/migrations/0028_cellular_v3_trace_bounds.sql")" digest_28="${digest_28%% *}" +digest_29="$(sha256sum "$root/migrations/0029_cellular_mutation_reservations.sql")" +digest_29="${digest_29%% *}" psql -X --no-password --set=ON_ERROR_STOP=1 --set="migration_sha=$digest" \ --set="migration_sha_2=$digest_2" --set="migration_sha_3=$digest_3" \ --set="migration_sha_4=$digest_4" --set="migration_sha_5=$digest_5" \ @@ -89,5 +91,6 @@ psql -X --no-password --set=ON_ERROR_STOP=1 --set="migration_sha=$digest" \ --set="migration_sha_26=$digest_26" \ --set="migration_sha_27=$digest_27" \ --set="migration_sha_28=$digest_28" \ + --set="migration_sha_29=$digest_29" \ --set="runtime_role=$runtime_role" \ --file="$root/migrations/apply.sql"