Publish mutation choices atomically with their checkpoint
ADR-0121, schema v35. A checkpoint whose trace carries mutation selections publishes with its canonical mutation block and one result byte per choice. SQL checks the block's order, matches each record to one reserved choice of this region and to its trace selection (tick, life, offer, operator, successor identity), binds it to the commit with its result, refuses a choice already bound elsewhere and checks exact retries. The four-argument publisher becomes an internal helper behind a wrapper that refuses traces with selections. C++ publication takes the results from its own replay rather than a caller, prepared commits carry block and results through retry and resolution, and recovery rebuilds the bound choices and requires the replay's results to match. A PostgreSQL test feeds a v4 cell to its first offer, publishes a reserved choice, recovers it and restarts a host. Host and session admission follow in 11.4c-2. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -74,3 +74,13 @@ pending; no gameplay balance is treated as final by this plan.
|
||||
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)).
|
||||
- 11.4c-1 (ADR-0121, schema v35): a checkpoint whose trace carries mutation
|
||||
selections publishes through `publish_cellular_mutation_checkpoint` with its
|
||||
canonical 64-byte mutation block and one result byte per choice. SQL checks
|
||||
block order, one reserved choice of this region per trace selection (same
|
||||
tick, life, offer, operator and successor identity), binds each to the commit
|
||||
with its result, refuses another commit's binding and checks exact retries;
|
||||
the four-argument publisher now refuses traces with selections. C++
|
||||
publication takes its results from its own replay, and recovery reloads the
|
||||
bound choices and requires the replay's results to equal them. Host and
|
||||
session admission (11.4c-2) remain.
|
||||
@@ -7275,4 +7275,22 @@ contracts for the v4 genesis (round trip, restore, wrong history, v3 bundle
|
||||
refusal, successor); g++-14 `-Werror` and clang-tidy-19 on the persistence,
|
||||
provisioner and PostgreSQL test sources; `bash -n` on both scripts. The SQL
|
||||
migration, `region_migration_tests` (v34 history, kind-2 input validation) and
|
||||
`region_store_tests` (cellular-v2 provisioning under v4) run only in CI.
|
||||
`region_store_tests` (cellular-v2 provisioning under v4) run only in CI.
|
||||
## 2026-10-13 — M11.4c-1 durable mutation publication (CI pending)
|
||||
|
||||
Migration 35 lets birth binding read format-3 traces that carry mutation
|
||||
records, adds `cellular_mutation_selections`, `check_cellular_mutation_block`
|
||||
and `publish_cellular_mutation_checkpoint` (binding each reserved choice to
|
||||
the commit with its result), a matching resolver and a lease-fenced recovery
|
||||
loader, and turns the old four-argument publisher into an internal helper
|
||||
behind a wrapper that refuses traces with selections. The runtime privilege
|
||||
audit lists the three new functions. `publish_cellular_batch` accepts mutation
|
||||
actions, matches them to the trace, and takes their results from its replay;
|
||||
prepared commits carry the block and results through retry and resolution;
|
||||
recovery rebuilds the choices from the publication rows and compares results.
|
||||
`cellular_publication_tests` adds a v4 region that feeds a cell to its first
|
||||
offer, reserves and publishes a choice, recovers it, and restarts a host.
|
||||
|
||||
Executed locally: MSVC Debug `ctest` 68/68; g++-14 `-Werror`, clang-tidy-19
|
||||
and clang-format 19.1.5 on the persistence, batch, host and test sources. The
|
||||
migration and PostgreSQL tests run only in CI.
|
||||
@@ -105,6 +105,9 @@ struct CellularPreparedCommit {
|
||||
RegionKey key{};
|
||||
Uuid commit{};
|
||||
std::vector<std::uint8_t> manifest, trace, actions;
|
||||
// ADR-0121: the canonical mutation block and one result byte per record; both empty
|
||||
// when the trace carries no selections.
|
||||
std::vector<std::uint8_t> mutations, results;
|
||||
};
|
||||
struct CellularPublicationFailure {
|
||||
PublicationError kind{};
|
||||
@@ -125,12 +128,16 @@ struct CellularProfileMigrationFailure {
|
||||
};
|
||||
struct PublishedCellularBatch {
|
||||
CommittedCheckpoint committed;
|
||||
// Each published mutation action's final result, in action-block order.
|
||||
std::vector<server::CellularMutationResult> mutation_results;
|
||||
};
|
||||
struct RecoveredCellularCheckpoint {
|
||||
CommittedCheckpoint committed;
|
||||
CheckpointManifest manifest;
|
||||
std::unique_ptr<server::CellularLives> state;
|
||||
std::vector<server::CellularActionRecord> actions;
|
||||
std::vector<server::CellularMutationActionRecord> mutation_actions;
|
||||
std::vector<server::CellularMutationResult> mutation_results;
|
||||
};
|
||||
|
||||
// Owner/operator connection only: uploads a tick-0 state and records genesis.
|
||||
@@ -220,7 +227,8 @@ publish_cellular_batch(Postgres& db, const FilesystemObjectStore& objects, const
|
||||
const server::CellularLives& candidate, Uuid commit,
|
||||
const Sha256Digest& build, std::chrono::milliseconds timeout,
|
||||
const PublishHooks& hooks = {},
|
||||
std::span<const server::CellularActionRecord> actions = {});
|
||||
std::span<const server::CellularActionRecord> actions = {},
|
||||
std::span<const server::CellularMutationActionRecord> mutation_actions = {});
|
||||
[[nodiscard]] std::expected<CommittedCheckpoint, CellularPublicationFailure>
|
||||
retry_cellular_prepared(Postgres& db, const CellularPreparedCommit& prepared,
|
||||
std::chrono::milliseconds timeout);
|
||||
|
||||
@@ -143,6 +143,31 @@ struct CellularBirthBinding {
|
||||
[[nodiscard]] std::expected<std::vector<CellularBirthBinding>, LeaseStoreFailure>
|
||||
load_cellular_publication_births(Postgres& db, RegionKey key, const LeaseGrant& lease,
|
||||
const Uuid& commit, std::chrono::milliseconds timeout);
|
||||
// Schema v35 (ADR-0121): a checkpoint whose trace carries mutation selections publishes
|
||||
// with its canonical mutation block (64-byte records) and one result byte per record.
|
||||
[[nodiscard]] std::expected<RegionHead, LeaseStoreFailure> publish_cellular_mutation_checkpoint(
|
||||
Postgres& db, std::span<const std::uint8_t> manifest, std::span<const std::uint8_t> decisions,
|
||||
std::span<const std::uint8_t> actions, std::span<const std::uint8_t> trace,
|
||||
std::span<const std::uint8_t> mutations, std::span<const std::uint8_t> results,
|
||||
std::chrono::milliseconds timeout);
|
||||
[[nodiscard]] std::expected<ResolvedCommit, LeaseStoreFailure> resolve_cellular_mutation_commit(
|
||||
Postgres& db, RegionKey key, const Uuid& commit, std::span<const std::uint8_t> manifest,
|
||||
std::span<const std::uint8_t> decisions, std::span<const std::uint8_t> actions,
|
||||
std::span<const std::uint8_t> trace, std::span<const std::uint8_t> mutations,
|
||||
std::span<const std::uint8_t> results, std::chrono::milliseconds timeout);
|
||||
// The mutation choices bound to one commit, ordered (tick, account, action): the 16-byte
|
||||
// account, action id, 34-byte reserved input and final result.
|
||||
struct CellularMutationPublication {
|
||||
std::array<std::uint8_t, 16> account{};
|
||||
std::uint64_t action_id{};
|
||||
std::array<std::uint8_t, 34> input{};
|
||||
std::uint8_t result{};
|
||||
friend bool operator==(const CellularMutationPublication&,
|
||||
const CellularMutationPublication&) = default;
|
||||
};
|
||||
[[nodiscard]] std::expected<std::vector<CellularMutationPublication>, LeaseStoreFailure>
|
||||
load_cellular_mutation_publications(Postgres& db, RegionKey key, const LeaseGrant& lease,
|
||||
const Uuid& commit, std::chrono::milliseconds timeout);
|
||||
// The committed record of an account's action in the lease's world (schema v11), under the
|
||||
// caller's exact lease. Nothing when the action was never committed.
|
||||
struct CommittedAction {
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <vector>
|
||||
|
||||
@@ -93,6 +94,25 @@ decode_cellular_batch(std::span<const std::uint8_t> bytes);
|
||||
// Every tick must cover its living set; the caller compares the returned snapshot.
|
||||
[[nodiscard]] std::expected<std::unique_ptr<CellularLives>, CellularBatchError>
|
||||
replay_cellular_batch(const CellularLives& base, const CellularBatch& batch);
|
||||
// The same replay, also reporting every mutation selection's final result in trace order
|
||||
// (ADR-0121): the authority for the results a publication binds and recovery compares.
|
||||
struct CellularMutationOutcome {
|
||||
std::uint64_t tick{}, life_id{}, successor_id{};
|
||||
CellularMutationResult result{};
|
||||
friend bool operator==(const CellularMutationOutcome&,
|
||||
const CellularMutationOutcome&) = default;
|
||||
};
|
||||
[[nodiscard]] std::expected<std::unique_ptr<CellularLives>, CellularBatchError>
|
||||
replay_cellular_batch(const CellularLives& base, const CellularBatch& batch,
|
||||
std::vector<CellularMutationOutcome>& mutation_outcomes);
|
||||
// The canonical 34-byte reserved choice (schema v29): tick, life, event and offer tick as
|
||||
// u64 and the operator as u16, little-endian.
|
||||
inline constexpr std::size_t cellular_mutation_input_bytes = 34;
|
||||
[[nodiscard]] std::array<std::uint8_t, cellular_mutation_input_bytes>
|
||||
encode_cellular_mutation_input(const CellularMutationActionRecord& action);
|
||||
[[nodiscard]] std::optional<CellularMutationActionRecord>
|
||||
decode_cellular_mutation_input(const std::array<std::uint8_t, 16>& account, std::uint64_t action_id,
|
||||
std::span<const std::uint8_t> input);
|
||||
[[nodiscard]] std::expected<std::vector<std::uint8_t>, CellularActionBlockError>
|
||||
encode_cellular_action_block(std::span<const CellularActionRecord> actions,
|
||||
std::uint64_t begin_tick, std::uint64_t end_tick);
|
||||
|
||||
@@ -0,0 +1,362 @@
|
||||
-- M11.4c, ADR-0121: publish mutation selections atomically with their checkpoint.
|
||||
-- Each published selection binds its v29 reservation to the commit with a final
|
||||
-- result (0 applied, 1 life died, 2 insufficient funding). The trace records the
|
||||
-- complete selection; the separate block names the durable account/action keys.
|
||||
|
||||
-- Birth binding now admits mutation records in format-3 traces; their content is
|
||||
-- validated by cellular_mutation_selections. Otherwise unchanged from schema v34.
|
||||
CREATE OR REPLACE FUNCTION evolution.cellular_births(p_trace bytea, p_manifest bytea)
|
||||
RETURNS TABLE(account_id uuid, request_id evolution.u64, generation evolution.u64,
|
||||
identity evolution.u64, born evolution.u64)
|
||||
LANGUAGE plpgsql IMMUTABLE SECURITY INVOKER SET search_path = pg_catalog, pg_temp AS $$
|
||||
DECLARE
|
||||
c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(p_manifest);
|
||||
revision boolean;
|
||||
ticks numeric;
|
||||
tick_index integer;
|
||||
trace_format numeric;
|
||||
tick_header_size integer;
|
||||
intent_count numeric;
|
||||
birth_count numeric;
|
||||
mutation_count numeric;
|
||||
off integer := 112;
|
||||
account bytea;
|
||||
previous_account bytea;
|
||||
previous_request numeric;
|
||||
BEGIN
|
||||
revision := c.versions IN (
|
||||
'\x020000000300000001000000010000000100000002000000010000000200000001000000'::bytea,
|
||||
'\x030000000400000001000000010000000100000002000000010000000200000001000000'::bytea);
|
||||
trace_format := evolution.manifest_uint(p_trace, 8, 4);
|
||||
tick_header_size := CASE trace_format WHEN 2 THEN 8 WHEN 3 THEN 16 ELSE 0 END;
|
||||
IF c.profile <> 4 OR c.commit_sequence = 0 OR p_trace IS NULL
|
||||
OR octet_length(p_trace) <> c.trace_bytes OR sha256(p_trace) <> c.trace_sha256
|
||||
OR substring(p_trace FROM 1 FOR 8) <> '\x45564c4342543031'
|
||||
OR tick_header_size = 0
|
||||
OR (revision AND trace_format <> 3)
|
||||
OR (NOT revision AND trace_format <> 2)
|
||||
OR evolution.manifest_uint(p_trace, 12, 4) <> 4
|
||||
OR evolution.manifest_uint(p_trace, 16, 8) <> c.world_id
|
||||
OR evolution.manifest_uint(p_trace, 24, 8) <> c.region_id
|
||||
OR evolution.manifest_uint(p_trace, 32, 8) <> c.frame_id
|
||||
OR encode(substring(p_trace FROM 41 FOR 16), 'hex')::uuid <> c.predecessor_commit
|
||||
OR substring(p_trace FROM 57 FOR 32) <> c.predecessor_digest
|
||||
OR evolution.manifest_uint(p_trace, 88, 8) <> c.begin_tick
|
||||
OR evolution.manifest_uint(p_trace, 96, 8) <> c.end_tick THEN
|
||||
RAISE EXCEPTION 'invalid cellular birth trace binding' USING ERRCODE = '22023';
|
||||
END IF;
|
||||
ticks := c.end_tick - c.begin_tick;
|
||||
IF ticks < 1 OR ticks > 64 OR evolution.manifest_uint(p_trace, 104, 4) <> ticks
|
||||
OR evolution.manifest_uint(p_trace, 108, 4) <> 0 THEN
|
||||
RAISE EXCEPTION 'invalid cellular birth trace range' USING ERRCODE = '22023';
|
||||
END IF;
|
||||
FOR tick_index IN 0 .. ticks::integer - 1 LOOP
|
||||
IF off + tick_header_size > octet_length(p_trace) THEN
|
||||
RAISE EXCEPTION 'truncated cellular tick header' USING ERRCODE = '22023';
|
||||
END IF;
|
||||
intent_count := evolution.manifest_uint(p_trace, off, 4);
|
||||
birth_count := evolution.manifest_uint(p_trace, off + 4, 4);
|
||||
mutation_count := 0;
|
||||
IF trace_format = 3 THEN
|
||||
mutation_count := evolution.manifest_uint(p_trace, off + 8, 4);
|
||||
IF evolution.manifest_uint(p_trace, off + 12, 4) <> 0 THEN
|
||||
RAISE EXCEPTION 'invalid cellular tick reserved word' USING ERRCODE = '22023';
|
||||
END IF;
|
||||
END IF;
|
||||
IF intent_count > 256 OR birth_count > 256 OR mutation_count > 256
|
||||
OR off + tick_header_size + intent_count * 24 + birth_count * 40
|
||||
+ mutation_count * 40 > octet_length(p_trace) THEN
|
||||
RAISE EXCEPTION 'invalid cellular tick counts' USING ERRCODE = '22023';
|
||||
END IF;
|
||||
off := off + tick_header_size + intent_count::integer * 24;
|
||||
previous_account := ''::bytea;
|
||||
previous_request := -1;
|
||||
FOR birth_index IN 0 .. birth_count::integer - 1 LOOP
|
||||
account := substring(p_trace FROM off + 1 FOR 16);
|
||||
account_id := encode(account, 'hex')::uuid;
|
||||
request_id := evolution.manifest_uint(p_trace, off + 16, 8);
|
||||
generation := evolution.manifest_uint(p_trace, off + 24, 8);
|
||||
identity := evolution.manifest_uint(p_trace, off + 32, 8);
|
||||
born := c.begin_tick + tick_index + 1;
|
||||
IF account_id = '00000000-0000-0000-0000-000000000000' OR request_id = 0
|
||||
OR generation = 0 OR identity = 0
|
||||
OR (account, request_id) <= (previous_account, previous_request) THEN
|
||||
RAISE EXCEPTION 'invalid cellular birth record' USING ERRCODE = '22023';
|
||||
END IF;
|
||||
previous_account := account;
|
||||
previous_request := request_id;
|
||||
off := off + 40;
|
||||
RETURN NEXT;
|
||||
END LOOP;
|
||||
off := off + mutation_count::integer * 40;
|
||||
END LOOP;
|
||||
IF off <> octet_length(p_trace) THEN
|
||||
RAISE EXCEPTION 'trailing cellular trace bytes' USING ERRCODE = '22023';
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- The trace's mutation selections with their tick, validated record by record.
|
||||
CREATE 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 operator_id = 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 $$;
|
||||
|
||||
-- The mutation block (64-byte records: account, action id, the 34-byte reserved input,
|
||||
-- six zero bytes) must be ordered by (tick, account, action), name exactly one reserved
|
||||
-- choice of this region per trace selection, and carry a known result per record.
|
||||
CREATE 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) > 2
|
||||
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 $$;
|
||||
|
||||
-- True when this commit already bound exactly these actions with these results.
|
||||
CREATE FUNCTION evolution.cellular_mutation_publications_match(p_world evolution.u64,
|
||||
p_commit uuid, p_mutations bytea, p_results bytea)
|
||||
RETURNS boolean LANGUAGE plpgsql STABLE SECURITY INVOKER
|
||||
SET search_path = pg_catalog, pg_temp AS $$
|
||||
DECLARE n integer := octet_length(p_mutations) / 64;
|
||||
BEGIN
|
||||
IF (SELECT count(*) FROM evolution.cellular_mutation_action_publications p
|
||||
WHERE p.world_id = p_world AND p.commit_id = p_commit) <> n THEN
|
||||
RETURN false;
|
||||
END IF;
|
||||
FOR i IN 0 .. n - 1 LOOP
|
||||
IF NOT EXISTS (SELECT 1 FROM evolution.cellular_mutation_action_publications p
|
||||
WHERE p.world_id = p_world AND p.commit_id = p_commit
|
||||
AND p.account_id = encode(substring(p_mutations FROM 64 * i + 1 FOR 16), 'hex')::uuid
|
||||
AND p.action_id = evolution.manifest_uint(p_mutations, 64 * i + 16, 8)
|
||||
AND p.result = get_byte(p_results, i)) THEN
|
||||
RETURN false;
|
||||
END IF;
|
||||
END LOOP;
|
||||
RETURN true;
|
||||
END $$;
|
||||
|
||||
-- The existing four-argument publisher becomes internal; its runtime replacement refuses
|
||||
-- traces with selections, which only the mutation publisher may bind.
|
||||
ALTER FUNCTION evolution.publish_cellular_checkpoint(bytea, bytea, bytea, bytea)
|
||||
RENAME TO publish_cellular_birth_checkpoint;
|
||||
REVOKE ALL ON FUNCTION evolution.publish_cellular_birth_checkpoint(bytea, bytea, bytea, bytea)
|
||||
FROM PUBLIC, :"runtime_role";
|
||||
|
||||
CREATE FUNCTION evolution.publish_cellular_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 $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM evolution.cellular_mutation_selections(p_trace, p_manifest)) THEN
|
||||
RAISE EXCEPTION 'cellular mutation selections need their action block' USING ERRCODE = '22023';
|
||||
END IF;
|
||||
RETURN QUERY SELECT * FROM evolution.publish_cellular_birth_checkpoint(
|
||||
p_manifest, p_decisions, p_actions, p_trace);
|
||||
END $$;
|
||||
|
||||
CREATE FUNCTION evolution.publish_cellular_mutation_checkpoint(p_manifest bytea,
|
||||
p_decisions bytea, p_actions bytea, p_trace bytea, p_mutations bytea, p_results 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;
|
||||
h record;
|
||||
digest bytea;
|
||||
BEGIN
|
||||
IF c.profile <> 4 OR c.commit_sequence = 0 THEN
|
||||
RAISE EXCEPTION 'invalid cellular publication' USING ERRCODE = '22023';
|
||||
END IF;
|
||||
n := evolution.check_cellular_mutation_block(p_manifest, p_trace, p_mutations, p_results);
|
||||
IF EXISTS (SELECT 1 FROM evolution.region_commits x WHERE x.commit_id = c.commit_id) THEN
|
||||
IF NOT evolution.cellular_mutation_publications_match(c.world_id, c.commit_id, p_mutations,
|
||||
p_results) THEN
|
||||
RAISE EXCEPTION 'cellular mutation evidence differs on retry' USING ERRCODE = 'EVL07';
|
||||
END IF;
|
||||
RETURN QUERY SELECT * FROM evolution.publish_cellular_birth_checkpoint(
|
||||
p_manifest, p_decisions, p_actions, p_trace);
|
||||
RETURN;
|
||||
END IF;
|
||||
-- A choice binds to one commit only; another commit's binding is a reused identity.
|
||||
FOR i IN 0 .. n - 1 LOOP
|
||||
IF EXISTS (SELECT 1 FROM evolution.cellular_mutation_action_publications p
|
||||
WHERE p.world_id = c.world_id AND p.commit_id <> c.commit_id
|
||||
AND p.account_id = encode(substring(p_mutations FROM 64 * i + 1 FOR 16), 'hex')::uuid
|
||||
AND p.action_id = evolution.manifest_uint(p_mutations, 64 * i + 16, 8)) THEN
|
||||
RAISE EXCEPTION 'cellular mutation already published' USING ERRCODE = 'EVL07';
|
||||
END IF;
|
||||
END LOOP;
|
||||
-- The birth publisher takes the lease lock and rechecks an uncertain first attempt.
|
||||
SELECT * INTO h FROM evolution.publish_cellular_birth_checkpoint(
|
||||
p_manifest, p_decisions, p_actions, p_trace);
|
||||
IF NOT FOUND THEN
|
||||
RAISE EXCEPTION 'publication returned no head' USING ERRCODE = 'XX000';
|
||||
END IF;
|
||||
IF evolution.cellular_mutation_publications_match(c.world_id, c.commit_id, p_mutations,
|
||||
p_results) THEN
|
||||
RETURN QUERY SELECT h.region_frame, h.head_sequence, h.head_commit, h.head_digest,
|
||||
h.head_tick;
|
||||
RETURN;
|
||||
END IF;
|
||||
SELECT x.commit_digest INTO digest FROM evolution.region_commits x
|
||||
WHERE x.commit_id = c.commit_id;
|
||||
FOR i IN 0 .. n - 1 LOOP
|
||||
INSERT INTO evolution.cellular_mutation_action_publications(world_id, region_id,
|
||||
account_id, action_id, commit_id, commit_digest, commit_sequence, result)
|
||||
VALUES (c.world_id, c.region_id,
|
||||
encode(substring(p_mutations FROM 64 * i + 1 FOR 16), 'hex')::uuid,
|
||||
evolution.manifest_uint(p_mutations, 64 * i + 16, 8), c.commit_id, digest,
|
||||
c.commit_sequence, get_byte(p_results, i));
|
||||
END LOOP;
|
||||
RETURN QUERY SELECT h.region_frame, h.head_sequence, h.head_commit, h.head_digest, h.head_tick;
|
||||
END $$;
|
||||
|
||||
CREATE FUNCTION evolution.resolve_cellular_mutation_commit(p_world evolution.u64,
|
||||
p_region evolution.u64, p_commit uuid, p_manifest bytea, p_decisions bytea,
|
||||
p_actions bytea, p_trace bytea, p_mutations bytea, p_results 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);
|
||||
BEGIN
|
||||
IF c.world_id <> p_world OR c.region_id <> p_region OR c.commit_id <> p_commit THEN
|
||||
RAISE EXCEPTION 'invalid cellular resolution request' USING ERRCODE = '22023';
|
||||
END IF;
|
||||
PERFORM evolution.check_cellular_mutation_block(p_manifest, p_trace, p_mutations, p_results);
|
||||
IF EXISTS (SELECT 1 FROM evolution.region_commits x WHERE x.commit_id = p_commit)
|
||||
AND NOT evolution.cellular_mutation_publications_match(p_world, p_commit, p_mutations,
|
||||
p_results) 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_cellular_region_commit(
|
||||
p_world, p_region, p_commit, p_manifest, p_decisions, p_actions, p_trace);
|
||||
END $$;
|
||||
|
||||
-- Recovery: the account/action keys, reserved inputs and results bound to one commit.
|
||||
CREATE FUNCTION evolution.load_cellular_mutation_publications(p_world evolution.u64,
|
||||
p_region evolution.u64, p_holder uuid, p_epoch evolution.u64, p_commit uuid)
|
||||
RETURNS TABLE(account_id uuid, action_id evolution.u64, input_bytes bytea, result smallint)
|
||||
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 p.account_id, p.action_id, r.input_bytes, p.result
|
||||
FROM evolution.cellular_mutation_action_publications p
|
||||
JOIN evolution.cellular_mutation_reservations r USING (world_id, account_id, action_id)
|
||||
WHERE p.world_id = p_world AND p.region_id = p_region AND p.commit_id = p_commit
|
||||
ORDER BY r.target_tick, p.account_id, p.action_id;
|
||||
END $$;
|
||||
|
||||
REVOKE ALL ON FUNCTION evolution.cellular_mutation_selections(bytea, bytea),
|
||||
evolution.check_cellular_mutation_block(bytea, bytea, bytea, bytea),
|
||||
evolution.cellular_mutation_publications_match(evolution.u64, uuid, bytea, bytea),
|
||||
evolution.publish_cellular_checkpoint(bytea, bytea, bytea, bytea),
|
||||
evolution.publish_cellular_mutation_checkpoint(bytea, bytea, bytea, bytea, bytea, bytea),
|
||||
evolution.resolve_cellular_mutation_commit(evolution.u64, evolution.u64, uuid, bytea, bytea,
|
||||
bytea, bytea, bytea, bytea),
|
||||
evolution.load_cellular_mutation_publications(evolution.u64, evolution.u64, uuid,
|
||||
evolution.u64, uuid)
|
||||
FROM PUBLIC;
|
||||
GRANT EXECUTE ON FUNCTION
|
||||
evolution.publish_cellular_checkpoint(bytea, bytea, bytea, bytea),
|
||||
evolution.publish_cellular_mutation_checkpoint(bytea, bytea, bytea, bytea, bytea, bytea),
|
||||
evolution.resolve_cellular_mutation_commit(evolution.u64, evolution.u64, uuid, bytea, bytea,
|
||||
bytea, bytea, bytea, bytea),
|
||||
evolution.load_cellular_mutation_publications(evolution.u64, evolution.u64, uuid,
|
||||
evolution.u64, uuid)
|
||||
TO :"runtime_role";
|
||||
INSERT INTO evolution.schema_migrations(version, sha256) VALUES (35, :'migration_sha_35');
|
||||
+23
-4
@@ -110,6 +110,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution')
|
||||
SELECT :'history_v31' || ',32:' || :'migration_sha_32' AS history_v32 \gset
|
||||
SELECT :'history_v32' || ',33:' || :'migration_sha_33' AS history_v33 \gset
|
||||
SELECT :'history_v33' || ',34:' || :'migration_sha_34' AS history_v34 \gset
|
||||
SELECT :'history_v34' || ',35:' || :'migration_sha_35' AS history_v35 \gset
|
||||
SELECT :'history' = :'history_v22' AS at_v22,
|
||||
:'history' = :'history_v23' AS at_v23,
|
||||
:'history' = :'history_v24' AS at_v24,
|
||||
@@ -122,8 +123,10 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution')
|
||||
:'history' = :'history_v31' AS at_v31,
|
||||
:'history' = :'history_v32' AS at_v32,
|
||||
:'history' = :'history_v33' AS at_v33,
|
||||
:'history' = :'history_v34' AS at_v34 \gset
|
||||
\if :at_v34
|
||||
:'history' = :'history_v34' AS at_v34,
|
||||
:'history' = :'history_v35' AS at_v35 \gset
|
||||
\if :at_v35
|
||||
\elif :at_v34
|
||||
\elif :at_v33
|
||||
\elif :at_v32
|
||||
\elif :at_v31
|
||||
@@ -485,7 +488,8 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24,
|
||||
coalesce(max(version), 0) = 31 AS at_v31,
|
||||
coalesce(max(version), 0) = 32 AS at_v32,
|
||||
coalesce(max(version), 0) = 33 AS at_v33,
|
||||
coalesce(max(version), 0) = 34 AS at_v34
|
||||
coalesce(max(version), 0) = 34 AS at_v34,
|
||||
coalesce(max(version), 0) = 35 AS at_v35
|
||||
FROM evolution.schema_migrations \gset
|
||||
\if :at_v24
|
||||
\ir 0025_cellular_v2_bundle.sql
|
||||
@@ -498,6 +502,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24,
|
||||
\ir 0032_fix_cellular_v3_manifest_normalization.sql
|
||||
\ir 0033_fix_cellular_action_tick_offset.sql
|
||||
\ir 0034_cellular_v4_engulf_bundle.sql
|
||||
\ir 0035_cellular_mutation_publication.sql
|
||||
\elif :at_v25
|
||||
\ir 0026_cellular_profile_migration.sql
|
||||
\ir 0027_cellular_v3_migration.sql
|
||||
@@ -508,6 +513,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24,
|
||||
\ir 0032_fix_cellular_v3_manifest_normalization.sql
|
||||
\ir 0033_fix_cellular_action_tick_offset.sql
|
||||
\ir 0034_cellular_v4_engulf_bundle.sql
|
||||
\ir 0035_cellular_mutation_publication.sql
|
||||
\elif :at_v26
|
||||
\ir 0027_cellular_v3_migration.sql
|
||||
\ir 0028_cellular_v3_trace_bounds.sql
|
||||
@@ -517,6 +523,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24,
|
||||
\ir 0032_fix_cellular_v3_manifest_normalization.sql
|
||||
\ir 0033_fix_cellular_action_tick_offset.sql
|
||||
\ir 0034_cellular_v4_engulf_bundle.sql
|
||||
\ir 0035_cellular_mutation_publication.sql
|
||||
\elif :at_v27
|
||||
\ir 0028_cellular_v3_trace_bounds.sql
|
||||
\ir 0029_cellular_mutation_reservations.sql
|
||||
@@ -525,6 +532,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24,
|
||||
\ir 0032_fix_cellular_v3_manifest_normalization.sql
|
||||
\ir 0033_fix_cellular_action_tick_offset.sql
|
||||
\ir 0034_cellular_v4_engulf_bundle.sql
|
||||
\ir 0035_cellular_mutation_publication.sql
|
||||
\elif :at_v28
|
||||
\ir 0029_cellular_mutation_reservations.sql
|
||||
\ir 0030_cellular_birth_publication_binding.sql
|
||||
@@ -532,29 +540,37 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24,
|
||||
\ir 0032_fix_cellular_v3_manifest_normalization.sql
|
||||
\ir 0033_fix_cellular_action_tick_offset.sql
|
||||
\ir 0034_cellular_v4_engulf_bundle.sql
|
||||
\ir 0035_cellular_mutation_publication.sql
|
||||
\elif :at_v29
|
||||
\ir 0030_cellular_birth_publication_binding.sql
|
||||
\ir 0031_fix_cellular_birth_publication_record.sql
|
||||
\ir 0032_fix_cellular_v3_manifest_normalization.sql
|
||||
\ir 0033_fix_cellular_action_tick_offset.sql
|
||||
\ir 0034_cellular_v4_engulf_bundle.sql
|
||||
\ir 0035_cellular_mutation_publication.sql
|
||||
\elif :at_v30
|
||||
\ir 0031_fix_cellular_birth_publication_record.sql
|
||||
\ir 0032_fix_cellular_v3_manifest_normalization.sql
|
||||
\ir 0033_fix_cellular_action_tick_offset.sql
|
||||
\ir 0034_cellular_v4_engulf_bundle.sql
|
||||
\ir 0035_cellular_mutation_publication.sql
|
||||
\elif :at_v31
|
||||
\ir 0032_fix_cellular_v3_manifest_normalization.sql
|
||||
\ir 0033_fix_cellular_action_tick_offset.sql
|
||||
\ir 0034_cellular_v4_engulf_bundle.sql
|
||||
\ir 0035_cellular_mutation_publication.sql
|
||||
\elif :at_v32
|
||||
\ir 0033_fix_cellular_action_tick_offset.sql
|
||||
\ir 0034_cellular_v4_engulf_bundle.sql
|
||||
\ir 0035_cellular_mutation_publication.sql
|
||||
\elif :at_v33
|
||||
\ir 0034_cellular_v4_engulf_bundle.sql
|
||||
\ir 0035_cellular_mutation_publication.sql
|
||||
\elif :at_v34
|
||||
\ir 0035_cellular_mutation_publication.sql
|
||||
\elif :at_v35
|
||||
\else
|
||||
\echo 'Unknown or changed migration history after v34'
|
||||
\echo 'Unknown or changed migration history after v35'
|
||||
DO $$ BEGIN RAISE EXCEPTION 'unknown or changed migration history'; END $$;
|
||||
\endif
|
||||
-- Effective-privilege audit on every run, including no-ops (ADR-0060). Attributes
|
||||
@@ -605,6 +621,9 @@ BEGIN
|
||||
('evolution.find_region_action_record(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64)'::regprocedure::oid),
|
||||
('evolution.publish_cellular_checkpoint(bytea,bytea,bytea,bytea)'::regprocedure::oid),
|
||||
('evolution.resolve_cellular_region_commit(evolution.u64,evolution.u64,uuid,bytea,bytea,bytea,bytea)'::regprocedure::oid),
|
||||
('evolution.publish_cellular_mutation_checkpoint(bytea,bytea,bytea,bytea,bytea,bytea)'::regprocedure::oid),
|
||||
('evolution.resolve_cellular_mutation_commit(evolution.u64,evolution.u64,uuid,bytea,bytea,bytea,bytea,bytea,bytea)'::regprocedure::oid),
|
||||
('evolution.load_cellular_mutation_publications(evolution.u64,evolution.u64,uuid,evolution.u64,uuid)'::regprocedure::oid),
|
||||
('evolution.load_cellular_publication_actions(evolution.u64,evolution.u64,uuid,evolution.u64,uuid)'::regprocedure::oid),
|
||||
('evolution.load_cellular_publication_births(evolution.u64,evolution.u64,uuid,evolution.u64,uuid)'::regprocedure::oid),
|
||||
('evolution.publish_cellular_profile_migration(bytea)'::regprocedure::oid),
|
||||
|
||||
@@ -50,8 +50,60 @@ confirm(const RegionHead& head, const CellularPreparedCommit& prepared,
|
||||
return std::unexpected{failure(PublicationError::receipt)};
|
||||
return CommittedCheckpoint{head, prepared.manifest};
|
||||
}
|
||||
// The result byte for each mutation action (ADR-0121), from the replay's outcomes: the
|
||||
// action's account names the predecessor life whose selection replayed at its tick.
|
||||
std::optional<std::vector<std::uint8_t>>
|
||||
mutation_results(const server::CellularLives& base,
|
||||
std::span<const server::CellularMutationActionRecord> actions,
|
||||
std::span<const server::CellularMutationOutcome> outcomes) {
|
||||
if (actions.size() != outcomes.size())
|
||||
return std::nullopt;
|
||||
std::vector<std::uint8_t> results;
|
||||
results.reserve(actions.size());
|
||||
for (const auto& action : actions) {
|
||||
const auto life =
|
||||
std::ranges::find(base.lives(), action.account, &server::CellularOwnedLife::account);
|
||||
if (life == base.lives().end() || life->identity.life != action.input.life_id)
|
||||
return std::nullopt;
|
||||
const auto outcome = std::ranges::find_if(outcomes, [&](const auto& candidate) {
|
||||
return candidate.tick == action.tick && candidate.life_id == action.input.life_id;
|
||||
});
|
||||
if (outcome == outcomes.end())
|
||||
return std::nullopt;
|
||||
results.push_back(static_cast<std::uint8_t>(outcome->result));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
std::size_t selection_count(const server::CellularBatch& trace) {
|
||||
std::size_t count{};
|
||||
for (const auto& tick : trace.ticks)
|
||||
count += tick.mutations.size();
|
||||
return count;
|
||||
}
|
||||
|
||||
// The mutation block and results agree in shape with the trace: none without selections,
|
||||
// otherwise one canonical record and one known result per selection.
|
||||
bool prepared_mutations_are_canonical(const CellularPreparedCommit& prepared,
|
||||
const CheckpointManifest& manifest,
|
||||
const server::CellularBatch& trace) {
|
||||
const auto selections = selection_count(trace);
|
||||
if (prepared.mutations.empty())
|
||||
return selections == 0 && prepared.results.empty();
|
||||
const auto decoded = server::decode_cellular_mutation_action_block(
|
||||
prepared.mutations, manifest.begin_tick, manifest.end_tick);
|
||||
if (!decoded || decoded->size() != selections || prepared.results.size() != selections ||
|
||||
std::ranges::any_of(prepared.results, [](std::uint8_t result) { return result > 2; }))
|
||||
return false;
|
||||
const auto encoded = server::encode_cellular_mutation_action_block(
|
||||
*decoded, manifest.begin_tick, manifest.end_tick);
|
||||
return encoded && *encoded == prepared.mutations;
|
||||
}
|
||||
|
||||
bool prepared_is_canonical(const CellularPreparedCommit& prepared,
|
||||
const CheckpointManifest& manifest, const server::CellularBatch& trace) {
|
||||
if (!prepared_mutations_are_canonical(prepared, manifest, trace))
|
||||
return false;
|
||||
const auto decoded_actions = server::decode_cellular_action_block(
|
||||
prepared.actions, manifest.begin_tick, manifest.end_tick);
|
||||
const auto encoded_actions =
|
||||
@@ -130,7 +182,8 @@ provision_cellular_region(Postgres& owner, const FilesystemObjectStore& objects,
|
||||
if (!inserted) {
|
||||
auto existing = load_region_checkpoint(owner, key, timeout);
|
||||
if (existing && existing->manifest == *encoded)
|
||||
return confirm(existing->head, CellularPreparedCommit{key, commit, *encoded, {}, {}},
|
||||
return confirm(existing->head,
|
||||
CellularPreparedCommit{key, commit, *encoded, {}, {}, {}, {}},
|
||||
*manifest);
|
||||
if (existing)
|
||||
return std::unexpected{failure(PublicationError::rejected)};
|
||||
@@ -145,7 +198,7 @@ provision_cellular_region(Postgres& owner, const FilesystemObjectStore& objects,
|
||||
auto committed = load_region_checkpoint(owner, key, timeout);
|
||||
if (!committed)
|
||||
return std::unexpected{failure(PublicationError::unknown, {}, committed.error())};
|
||||
CellularPreparedCommit prepared{key, commit, *encoded, {}, {}};
|
||||
CellularPreparedCommit prepared{key, commit, *encoded, {}, {}, {}, {}};
|
||||
auto confirmed = confirm(committed->head, prepared, *manifest);
|
||||
if (!confirmed)
|
||||
return std::unexpected{confirmed.error()};
|
||||
@@ -299,8 +352,12 @@ retry_cellular_prepared(Postgres& db, const CellularPreparedCommit& prepared,
|
||||
auto trace = server::decode_cellular_batch(prepared.trace);
|
||||
if (!manifest || !trace || !prepared_is_canonical(prepared, *manifest, *trace))
|
||||
return std::unexpected{failure(PublicationError::candidate)};
|
||||
auto result = publish_cellular_checkpoint(db, prepared.manifest, {}, prepared.actions,
|
||||
prepared.trace, timeout);
|
||||
auto result = prepared.mutations.empty()
|
||||
? publish_cellular_checkpoint(db, prepared.manifest, {}, prepared.actions,
|
||||
prepared.trace, timeout)
|
||||
: publish_cellular_mutation_checkpoint(
|
||||
db, prepared.manifest, {}, prepared.actions, prepared.trace,
|
||||
prepared.mutations, prepared.results, timeout);
|
||||
if (!result) {
|
||||
return std::unexpected{failure(result.error().kind == LeaseStoreError::unknown
|
||||
? PublicationError::unknown
|
||||
@@ -317,6 +374,10 @@ resolve_cellular_prepared(Postgres& db, const CellularPreparedCommit& prepared,
|
||||
auto trace = server::decode_cellular_batch(prepared.trace);
|
||||
if (!manifest || !trace || !prepared_is_canonical(prepared, *manifest, *trace))
|
||||
return std::unexpected{LeaseStoreFailure{LeaseStoreError::invalid, {}}};
|
||||
if (!prepared.mutations.empty())
|
||||
return resolve_cellular_mutation_commit(
|
||||
db, prepared.key, prepared.commit, prepared.manifest, {}, prepared.actions,
|
||||
prepared.trace, prepared.mutations, prepared.results, timeout);
|
||||
return resolve_cellular_region_commit(db, prepared.key, prepared.commit, prepared.manifest, {},
|
||||
prepared.actions, prepared.trace, timeout);
|
||||
}
|
||||
@@ -326,7 +387,8 @@ publish_cellular_batch(Postgres& db, const FilesystemObjectStore& objects, const
|
||||
const CommittedCheckpoint& predecessor, const server::CellularBatch& batch,
|
||||
const server::CellularLives& candidate, Uuid commit,
|
||||
const Sha256Digest& build, milliseconds timeout, const PublishHooks& hooks,
|
||||
std::span<const server::CellularActionRecord> actions) {
|
||||
std::span<const server::CellularActionRecord> actions,
|
||||
std::span<const server::CellularMutationActionRecord> mutation_actions) {
|
||||
const auto previous = decode_manifest(predecessor.manifest);
|
||||
const auto predecessor_digest = sha256(predecessor.manifest);
|
||||
auto committed_batch = batch;
|
||||
@@ -351,9 +413,16 @@ publish_cellular_batch(Postgres& db, const FilesystemObjectStore& objects, const
|
||||
candidate.format_version() != previous->cellular_versions.envelope ||
|
||||
candidate.rules_version() != previous->cellular_versions.rules ||
|
||||
candidate.tick() != batch.header.end_tick ||
|
||||
std::ranges::any_of(committed_batch.ticks,
|
||||
[](const auto& tick) { return !tick.mutations.empty(); }))
|
||||
selection_count(committed_batch) != mutation_actions.size())
|
||||
return std::unexpected{failure(PublicationError::candidate)};
|
||||
std::vector<std::uint8_t> mutation_block;
|
||||
if (!mutation_actions.empty()) {
|
||||
auto encoded = server::encode_cellular_mutation_action_block(
|
||||
mutation_actions, batch.header.begin_tick, batch.header.end_tick);
|
||||
if (!encoded)
|
||||
return std::unexpected{failure(PublicationError::candidate)};
|
||||
mutation_block = std::move(*encoded);
|
||||
}
|
||||
|
||||
auto predecessor_bytes = objects.get({previous->payload_sha256, previous->payload_bytes});
|
||||
if (!predecessor_bytes)
|
||||
@@ -361,10 +430,17 @@ publish_cellular_batch(Postgres& db, const FilesystemObjectStore& objects, const
|
||||
auto base = restore_cellular_payload(*predecessor_bytes, *previous);
|
||||
if (!base)
|
||||
return std::unexpected{failure(PublicationError::predecessor)};
|
||||
auto replay = server::replay_cellular_batch(**base, committed_batch);
|
||||
std::vector<server::CellularMutationOutcome> outcomes;
|
||||
auto replay = server::replay_cellular_batch(**base, committed_batch, outcomes);
|
||||
const auto payload = candidate.snapshot();
|
||||
if (!replay || (*replay)->snapshot() != payload ||
|
||||
!server::match_cellular_actions_to_trace(**base, committed_batch, actions))
|
||||
!server::match_cellular_actions_to_trace(**base, committed_batch, actions) ||
|
||||
(!mutation_actions.empty() && !server::match_cellular_mutation_actions_to_trace(
|
||||
**base, committed_batch, mutation_actions)))
|
||||
return std::unexpected{failure(PublicationError::candidate)};
|
||||
// The bound results are the replay's, never a caller's claim.
|
||||
const auto results = mutation_results(**base, mutation_actions, outcomes);
|
||||
if (!results)
|
||||
return std::unexpected{failure(PublicationError::candidate)};
|
||||
|
||||
const auto payload_digest = sha256(payload);
|
||||
@@ -384,13 +460,18 @@ publish_cellular_batch(Postgres& db, const FilesystemObjectStore& objects, const
|
||||
if (auto stored = put_verified(objects, *trace, {*trace_digest, trace->size()}); !stored)
|
||||
return std::unexpected{stored.error()};
|
||||
|
||||
CellularPreparedCommit prepared{previous->key, commit, *manifest_bytes, *trace, *action_block};
|
||||
CellularPreparedCommit prepared{previous->key, commit, *manifest_bytes, *trace,
|
||||
*action_block, mutation_block, *results};
|
||||
if (hooks.before_transaction)
|
||||
hooks.before_transaction();
|
||||
auto committed = retry_cellular_prepared(db, prepared, timeout);
|
||||
if (!committed)
|
||||
return std::unexpected{committed.error()};
|
||||
return PublishedCellularBatch{std::move(*committed)};
|
||||
std::vector<server::CellularMutationResult> published_results;
|
||||
published_results.reserve(results->size());
|
||||
for (const auto result : *results)
|
||||
published_results.push_back(static_cast<server::CellularMutationResult>(result));
|
||||
return PublishedCellularBatch{std::move(*committed), std::move(published_results)};
|
||||
}
|
||||
|
||||
std::expected<RecoveredCellularCheckpoint, RecoveryFailure>
|
||||
@@ -429,6 +510,8 @@ recover_cellular_checkpoint(Postgres& db, const FilesystemObjectStore& objects,
|
||||
return fail_with(RecoveryError::payload);
|
||||
|
||||
std::vector<server::CellularActionRecord> actions;
|
||||
std::vector<server::CellularMutationActionRecord> mutation_actions_recovered;
|
||||
std::vector<server::CellularMutationResult> mutation_results_recovered;
|
||||
if (manifest->sequence != 0 && manifest->begin_tick == manifest->end_tick) {
|
||||
auto migration = load_cellular_profile_migration(db, key, lease, manifest->commit, timeout);
|
||||
if (!migration)
|
||||
@@ -488,7 +571,8 @@ recover_cellular_checkpoint(Postgres& db, const FilesystemObjectStore& objects,
|
||||
if (!migrated || (*migrated)->tick() != manifest->end_tick ||
|
||||
(*migrated)->snapshot() != *payload)
|
||||
return fail_with(RecoveryError::history);
|
||||
return RecoveredCellularCheckpoint{std::move(*committed), *manifest, std::move(*state), {}};
|
||||
return RecoveredCellularCheckpoint{
|
||||
std::move(*committed), *manifest, std::move(*state), {}, {}, {}};
|
||||
}
|
||||
if (manifest->sequence != 0) {
|
||||
auto action_bytes =
|
||||
@@ -557,10 +641,42 @@ recover_cellular_checkpoint(Postgres& db, const FilesystemObjectStore& objects,
|
||||
if (!batch_actions ||
|
||||
!server::match_cellular_actions_to_trace(**base, *trace, *batch_actions))
|
||||
return fail_with(RecoveryError::history);
|
||||
auto replay = server::replay_cellular_batch(**base, *trace);
|
||||
std::vector<server::CellularMutationOutcome> outcomes;
|
||||
auto replay = server::replay_cellular_batch(**base, *trace, outcomes);
|
||||
if (!replay || (*replay)->snapshot() != *payload)
|
||||
return fail_with(RecoveryError::tail);
|
||||
|
||||
// ADR-0121: every selection in the trace has exactly one bound choice whose stored
|
||||
// result is the replay's.
|
||||
auto published =
|
||||
load_cellular_mutation_publications(db, key, lease, manifest->commit, timeout);
|
||||
if (!published)
|
||||
return std::unexpected{RecoveryFailure{RecoveryError::store, published.error()}};
|
||||
std::vector<server::CellularMutationActionRecord> mutation_actions;
|
||||
std::vector<std::uint8_t> stored_results;
|
||||
mutation_actions.reserve(published->size());
|
||||
for (const auto& row : *published) {
|
||||
auto action =
|
||||
server::decode_cellular_mutation_input(row.account, row.action_id, row.input);
|
||||
if (!action)
|
||||
return fail_with(RecoveryError::history);
|
||||
mutation_actions.push_back(*action);
|
||||
stored_results.push_back(row.result);
|
||||
}
|
||||
if (mutation_actions.size() != selection_count(*trace) ||
|
||||
(!mutation_actions.empty() &&
|
||||
(!server::encode_cellular_mutation_action_block(mutation_actions, manifest->begin_tick,
|
||||
manifest->end_tick) ||
|
||||
!server::match_cellular_mutation_actions_to_trace(**base, *trace, mutation_actions))))
|
||||
return fail_with(RecoveryError::history);
|
||||
const auto replayed_results = mutation_results(**base, mutation_actions, outcomes);
|
||||
if (!replayed_results || *replayed_results != stored_results)
|
||||
return fail_with(RecoveryError::history);
|
||||
for (const auto result : stored_results)
|
||||
mutation_results_recovered.push_back(
|
||||
static_cast<server::CellularMutationResult>(result));
|
||||
mutation_actions_recovered = std::move(mutation_actions);
|
||||
|
||||
std::vector<ActionRef> refs;
|
||||
refs.reserve(batch_actions->size());
|
||||
for (const auto& action : *batch_actions)
|
||||
@@ -583,7 +699,11 @@ recover_cellular_checkpoint(Postgres& db, const FilesystemObjectStore& objects,
|
||||
}
|
||||
actions = std::move(*batch_actions);
|
||||
}
|
||||
return RecoveredCellularCheckpoint{std::move(*committed), *manifest, std::move(*state),
|
||||
std::move(actions)};
|
||||
return RecoveredCellularCheckpoint{std::move(*committed),
|
||||
*manifest,
|
||||
std::move(*state),
|
||||
std::move(actions),
|
||||
std::move(mutation_actions_recovered),
|
||||
std::move(mutation_results_recovered)};
|
||||
}
|
||||
} // namespace evo::persistence
|
||||
|
||||
@@ -529,6 +529,89 @@ load_cellular_publication_births(Postgres& db, RegionKey key, const LeaseGrant&
|
||||
return bindings;
|
||||
}
|
||||
|
||||
std::expected<RegionHead, LeaseStoreFailure> publish_cellular_mutation_checkpoint(
|
||||
Postgres& db, std::span<const std::uint8_t> manifest, std::span<const std::uint8_t> decisions,
|
||||
std::span<const std::uint8_t> actions, std::span<const std::uint8_t> trace,
|
||||
std::span<const std::uint8_t> mutations, std::span<const std::uint8_t> results,
|
||||
milliseconds timeout) {
|
||||
const std::array<std::string, 6> p{bytea(manifest), bytea(decisions), bytea(actions),
|
||||
bytea(trace), bytea(mutations), bytea(results)};
|
||||
auto rows = db.query("SELECT * FROM evolution.publish_cellular_mutation_checkpoint($1::bytea, "
|
||||
"$2::bytea, $3::bytea, $4::bytea, $5::bytea, $6::bytea)",
|
||||
p, timeout);
|
||||
if (!rows)
|
||||
return std::unexpected{classify(rows.error())};
|
||||
auto head = parse_head(*rows);
|
||||
if (!head)
|
||||
return std::unexpected{malformed(true)};
|
||||
return *head;
|
||||
}
|
||||
|
||||
std::expected<ResolvedCommit, LeaseStoreFailure> resolve_cellular_mutation_commit(
|
||||
Postgres& db, RegionKey key, const Uuid& commit, std::span<const std::uint8_t> manifest,
|
||||
std::span<const std::uint8_t> decisions, std::span<const std::uint8_t> actions,
|
||||
std::span<const std::uint8_t> trace, std::span<const std::uint8_t> mutations,
|
||||
std::span<const std::uint8_t> results, milliseconds timeout) {
|
||||
const std::array<std::string, 9> p{std::to_string(key.world),
|
||||
std::to_string(key.region),
|
||||
uuid_text(commit),
|
||||
bytea(manifest),
|
||||
bytea(decisions),
|
||||
bytea(actions),
|
||||
bytea(trace),
|
||||
bytea(mutations),
|
||||
bytea(results)};
|
||||
auto rows =
|
||||
db.query("SELECT * FROM evolution.resolve_cellular_mutation_commit($1::evolution.u64, "
|
||||
"$2::evolution.u64, $3::uuid, $4::bytea, $5::bytea, $6::bytea, $7::bytea, "
|
||||
"$8::bytea, $9::bytea)",
|
||||
p, timeout);
|
||||
if (!rows)
|
||||
return std::unexpected{classify(rows.error())};
|
||||
if (rows->size() != 1 || (*rows)[0].size() != 6 || !(*rows)[0][0])
|
||||
return std::unexpected{malformed(false)};
|
||||
const auto status = (*rows)[0][0].value_or(std::string{});
|
||||
if (status == "absent")
|
||||
return ResolvedCommit{Resolution::absent, {}};
|
||||
if (status == "conflict")
|
||||
return ResolvedCommit{Resolution::conflict, {}};
|
||||
const PgRows receipt{
|
||||
std::vector<std::optional<std::string>>((*rows)[0].begin() + 1, (*rows)[0].end())};
|
||||
auto head = status == "committed" ? parse_head(receipt) : std::nullopt;
|
||||
if (!head)
|
||||
return std::unexpected{malformed(false)};
|
||||
return ResolvedCommit{Resolution::committed, *head};
|
||||
}
|
||||
|
||||
std::expected<std::vector<CellularMutationPublication>, LeaseStoreFailure>
|
||||
load_cellular_mutation_publications(Postgres& db, RegionKey key, const LeaseGrant& lease,
|
||||
const Uuid& commit, milliseconds timeout) {
|
||||
const std::array<std::string, 5> p{std::to_string(key.world), std::to_string(key.region),
|
||||
uuid_text(lease.holder), std::to_string(lease.epoch),
|
||||
uuid_text(commit)};
|
||||
auto rows = db.query("SELECT account_id::text, action_id::text, encode(input_bytes, 'hex'), "
|
||||
"result::text FROM evolution.load_cellular_mutation_publications("
|
||||
"$1::evolution.u64, $2::evolution.u64, $3::uuid, $4::evolution.u64, "
|
||||
"$5::uuid)",
|
||||
p, timeout);
|
||||
if (!rows)
|
||||
return std::unexpected{classify(rows.error())};
|
||||
std::vector<CellularMutationPublication> publications;
|
||||
publications.reserve(rows->size());
|
||||
for (const auto& row : *rows) {
|
||||
if (row.size() != 4)
|
||||
return std::unexpected{malformed(false)};
|
||||
const auto account = parse_uuid(row[0]);
|
||||
const auto action = parse_integer<std::uint64_t>(row[1]);
|
||||
const auto input = unhex<34>(row[2].value_or(std::string{}));
|
||||
const auto result = parse_integer<std::uint32_t>(row[3]);
|
||||
if (!account || !action || *action == 0 || !input || !result || *result > 2)
|
||||
return std::unexpected{malformed(false)};
|
||||
publications.push_back({*account, *action, *input, static_cast<std::uint8_t>(*result)});
|
||||
}
|
||||
return publications;
|
||||
}
|
||||
|
||||
std::expected<std::optional<CommittedAction>, LeaseStoreFailure>
|
||||
find_region_action(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account,
|
||||
std::uint64_t action_id, milliseconds timeout) {
|
||||
|
||||
@@ -356,6 +356,48 @@ decode_cellular_batch(std::span<const std::uint8_t> bytes) {
|
||||
|
||||
std::expected<std::unique_ptr<CellularLives>, CellularBatchError>
|
||||
replay_cellular_batch(const CellularLives& base, const CellularBatch& batch) {
|
||||
std::vector<CellularMutationOutcome> ignored;
|
||||
return replay_cellular_batch(base, batch, ignored);
|
||||
}
|
||||
|
||||
std::array<std::uint8_t, cellular_mutation_input_bytes>
|
||||
encode_cellular_mutation_input(const CellularMutationActionRecord& action) {
|
||||
std::vector<std::uint8_t> bytes;
|
||||
bytes.reserve(cellular_mutation_input_bytes);
|
||||
put(bytes, action.tick, 8);
|
||||
put(bytes, action.input.life_id, 8);
|
||||
put(bytes, action.input.event_index, 8);
|
||||
put(bytes, action.input.offer_tick, 8);
|
||||
put(bytes, action.input.operator_id, 2);
|
||||
std::array<std::uint8_t, cellular_mutation_input_bytes> out{};
|
||||
std::ranges::copy(bytes, out.begin());
|
||||
return out;
|
||||
}
|
||||
|
||||
std::optional<CellularMutationActionRecord>
|
||||
decode_cellular_mutation_input(const std::array<std::uint8_t, 16>& account, std::uint64_t action_id,
|
||||
std::span<const std::uint8_t> input) {
|
||||
if (input.size() != cellular_mutation_input_bytes)
|
||||
return std::nullopt;
|
||||
CellularMutationActionRecord action;
|
||||
action.account = account;
|
||||
action.action_id = action_id;
|
||||
action.tick = get(input, 0, 8);
|
||||
action.input.life_id = get(input, 8, 8);
|
||||
action.input.event_index = get(input, 16, 8);
|
||||
action.input.offer_tick = get(input, 24, 8);
|
||||
action.input.operator_id = static_cast<std::uint16_t>(get(input, 32, 2));
|
||||
if (action_id == 0 || action.tick == 0 || action.input.life_id == 0 ||
|
||||
action.input.event_index == 0 || action.input.offer_tick == 0 ||
|
||||
action.input.offer_tick >= action.tick || action.input.operator_id == 0)
|
||||
return std::nullopt;
|
||||
return action;
|
||||
}
|
||||
|
||||
std::expected<std::unique_ptr<CellularLives>, CellularBatchError>
|
||||
replay_cellular_batch(const CellularLives& base, const CellularBatch& batch,
|
||||
std::vector<CellularMutationOutcome>& mutation_outcomes) {
|
||||
mutation_outcomes.clear();
|
||||
const auto& h = batch.header;
|
||||
if (!valid_header(h, batch.ticks.size()) || !valid_ticks(batch.ticks))
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
@@ -367,8 +409,12 @@ replay_cellular_batch(const CellularLives& base, const CellularBatch& batch) {
|
||||
if (!replay)
|
||||
return std::unexpected{CellularBatchError::state};
|
||||
for (const auto& tick : batch.ticks) {
|
||||
if (!(*replay)->advance_tick_with_mutations(tick.lives, tick.mutations))
|
||||
const auto executed = (*replay)->advance_tick_with_mutations(tick.lives, tick.mutations);
|
||||
if (!executed)
|
||||
return std::unexpected{CellularBatchError::state};
|
||||
for (const auto& mutation : executed->mutations)
|
||||
mutation_outcomes.push_back(
|
||||
{(*replay)->tick(), mutation.life_id, mutation.successor_id, mutation.result});
|
||||
for (const auto& birth : tick.births) {
|
||||
const LifeRequest request{birth.account, birth.request};
|
||||
const LifeAllocation allocation{birth.identity,
|
||||
|
||||
@@ -309,11 +309,166 @@ void cellular_profile_migration_is_durable_and_recovers_without_a_tick() {
|
||||
restarted->state()->snapshot() == *target_payload);
|
||||
restarted->drain();
|
||||
}
|
||||
|
||||
// ADR-0121 (schema v35): a reserved mutation choice publishes with its checkpoint, the
|
||||
// bound result is the replay's, recovery reloads choice and result, and a restarted host
|
||||
// resumes at the same state.
|
||||
void cellular_mutation_publication_binds_results() {
|
||||
using namespace evo::biology;
|
||||
constexpr RegionKey key{9764, 1};
|
||||
auto owner = connect_as(env("PGUSER"));
|
||||
auto runtime = connect_as(env("EVO_PG_RUNTIME_USER"));
|
||||
const auto build = filled<32>(0xD9);
|
||||
CHECK(owner->query("INSERT INTO evolution.retained_builds(build_identity) VALUES "
|
||||
"(decode(repeat('d9',32),'hex')) ON CONFLICT DO NOTHING",
|
||||
{}, 5s));
|
||||
auto endowed = primitive_endowment();
|
||||
CHECK(endowed);
|
||||
auto funding = endowed->body;
|
||||
funding.energy.value = endowed->reserve.energy.value + endowed->construction.value;
|
||||
auto primitive = PrimitiveLives::create({key.world, key.region, 1, 17, funding, {{0, 0}}});
|
||||
CHECK(primitive);
|
||||
// A stocked patch under the spawn site feeds the cell to its first offer.
|
||||
constexpr std::uint64_t stock = 100'000'000;
|
||||
auto habitat = CellularHabitat::create(
|
||||
{key.world,
|
||||
key.region,
|
||||
1,
|
||||
1'000,
|
||||
1'000,
|
||||
{{{0, 0}, 29, {{stock}, {{{stock}, {stock}, {stock}, {stock}}}}}}});
|
||||
CHECK(habitat);
|
||||
auto opening =
|
||||
CellularLives::create(**primitive, std::move(*habitat), {{1'000, 1'000}, 0, 293'150},
|
||||
CellularLivesProfile::engulf);
|
||||
CHECK(opening);
|
||||
auto objects = FilesystemObjectStore::open(env("PGHOST"), key, max_cellular_lives_bytes);
|
||||
CHECK(objects);
|
||||
CHECK(provision_cellular_region(*owner, **objects, key, 1, **opening, filled<16>(0x60), build,
|
||||
5s));
|
||||
CHECK(owner->query("INSERT INTO evolution.life_allocators VALUES(9764,1000) "
|
||||
"ON CONFLICT DO NOTHING",
|
||||
{}, 5s));
|
||||
const auto connect = [&]() -> std::expected<std::unique_ptr<Postgres>, PgFailure> {
|
||||
return Postgres::connect({env("PGHOST"),
|
||||
{},
|
||||
env("PGPORT"),
|
||||
env("PGDATABASE"),
|
||||
env("EVO_PG_RUNTIME_USER"),
|
||||
{},
|
||||
{}},
|
||||
5s);
|
||||
};
|
||||
CellularHostConfig config{key, 1, {}, build, {build}};
|
||||
auto made = CellularRegionHost::create(config, connect, std::move(*objects));
|
||||
CHECK(made);
|
||||
auto host = std::move(*made);
|
||||
CHECK(host->step().ready);
|
||||
const auto authority = checked_value(host->authority());
|
||||
const auto account = filled<16>(0x64);
|
||||
const auto control =
|
||||
claim_account_control(*runtime, key, authority, account, 1, 1, 60'000ms, 5s);
|
||||
CHECK(control);
|
||||
CHECK(host->create_primitive_life(account, control->generation, 11).result ==
|
||||
evo::network::PrimitiveLifeResult::pending);
|
||||
|
||||
// Each batch starts at the host's committed head.
|
||||
const auto header = [&](std::uint64_t ticks) {
|
||||
const auto& committed = checked_value(host->committed());
|
||||
const auto digest = sha256(committed.manifest);
|
||||
CHECK(digest);
|
||||
return CellularBatchHeader{key.world,
|
||||
key.region,
|
||||
1,
|
||||
committed.head.commit,
|
||||
checked_value(digest),
|
||||
host->state()->tick(),
|
||||
host->state()->tick() + ticks};
|
||||
};
|
||||
CellularBatch birth;
|
||||
birth.header = header(1);
|
||||
birth.ticks.push_back({{}, {host->queued_births().front()}, {}});
|
||||
CHECK(host->publish(birth));
|
||||
const auto life_id = host->state()->lives().front().identity.life;
|
||||
for (int round = 0; round < 4 && !host->state()->lives().front().mutation.offer; ++round) {
|
||||
CHECK(host->step().ready);
|
||||
CellularBatch feeding;
|
||||
feeding.header = header(64);
|
||||
CellularStepIntent feed{};
|
||||
feed.intake_target = CellularPatchAddress{0, 0};
|
||||
for (int tick = 0; tick < 64; ++tick)
|
||||
feeding.ticks.push_back({{{life_id, feed}}, {}, {}});
|
||||
CHECK(host->publish(feeding));
|
||||
}
|
||||
const auto& life = host->state()->lives().front();
|
||||
CHECK(life.mutation.offer.has_value());
|
||||
const auto offer = checked_value(life.mutation.offer);
|
||||
|
||||
CellularMutationActionRecord choice;
|
||||
choice.account = account;
|
||||
choice.action_id = 21;
|
||||
choice.tick = host->state()->tick() + 1;
|
||||
choice.input = {life_id, life.mutation.event_index, life.mutation.offer_tick,
|
||||
offer.candidates.front().operator_id};
|
||||
const auto reservation =
|
||||
reserve_cellular_mutation(*runtime, key, authority, account, control->generation,
|
||||
choice.action_id, encode_cellular_mutation_input(choice), 5s);
|
||||
CHECK(reservation);
|
||||
CellularBatch mutation;
|
||||
mutation.header = header(1);
|
||||
mutation.ticks.push_back({{{life_id, {}}},
|
||||
{},
|
||||
{{life_id, choice.input.event_index, choice.input.offer_tick,
|
||||
reservation->successor_identity, choice.input.operator_id}}});
|
||||
auto candidate = replay_cellular_batch(*host->state(), mutation);
|
||||
CHECK(candidate);
|
||||
auto mutation_objects =
|
||||
FilesystemObjectStore::open(env("PGHOST"), key, max_cellular_lives_bytes);
|
||||
CHECK(mutation_objects);
|
||||
const std::array choices{choice};
|
||||
// Publishing without the mutation block is refused: only the block binds the choice.
|
||||
CHECK(!publish_cellular_batch(*runtime, **mutation_objects, authority,
|
||||
checked_value(host->committed()), mutation, **candidate,
|
||||
filled<16>(0x65), build, 5s));
|
||||
const auto published = publish_cellular_batch(
|
||||
*runtime, **mutation_objects, authority, checked_value(host->committed()), mutation,
|
||||
**candidate, filled<16>(0x65), build, 5s, {}, {}, choices);
|
||||
CHECK(published && published->mutation_results.size() == 1);
|
||||
const auto result = published->mutation_results.front();
|
||||
// Applied exactly when the replayed life now carries the reserved successor genome.
|
||||
const bool applied =
|
||||
(*candidate)->lives().front().identity.genome == reservation->successor_identity;
|
||||
CHECK(result == (applied ? CellularMutationResult::applied
|
||||
: CellularMutationResult::insufficient_funding));
|
||||
|
||||
auto recovery_objects =
|
||||
FilesystemObjectStore::open(env("PGHOST"), key, max_cellular_lives_bytes);
|
||||
CHECK(recovery_objects);
|
||||
const std::array<Sha256Digest, 1> retained{build};
|
||||
auto recovered =
|
||||
recover_cellular_checkpoint(*runtime, **recovery_objects, key, authority, retained, 5s);
|
||||
CHECK(recovered && recovered->mutation_actions == std::vector{choice} &&
|
||||
recovered->mutation_results == std::vector{result} &&
|
||||
recovered->state->snapshot() == (*candidate)->snapshot());
|
||||
host->drain();
|
||||
host.reset();
|
||||
|
||||
auto restarted_objects =
|
||||
FilesystemObjectStore::open(env("PGHOST"), key, max_cellular_lives_bytes);
|
||||
CHECK(restarted_objects);
|
||||
auto restarted_made =
|
||||
CellularRegionHost::create(config, connect, std::move(*restarted_objects));
|
||||
CHECK(restarted_made);
|
||||
auto restarted = std::move(*restarted_made);
|
||||
CHECK(restarted->step().ready && restarted->state()->snapshot() == (*candidate)->snapshot());
|
||||
restarted->drain();
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void run_cellular_publication_tests() {
|
||||
cellular_birth_publication_is_durable();
|
||||
cellular_profile_migration_is_durable_and_recovers_without_a_tick();
|
||||
cellular_mutation_publication_binds_results();
|
||||
auto owner = connect_as(env("PGUSER"));
|
||||
const auto build = filled<32>(0xD9);
|
||||
CHECK(owner->query("INSERT INTO evolution.retained_builds(build_identity) VALUES "
|
||||
|
||||
@@ -212,7 +212,7 @@ void upgrade_across_migration_and_barrier() {
|
||||
" EVO_PG_RUNTIME_ROLE=evo_runtime_test bash \"$EVO_SOURCE_ROOT/"
|
||||
"tools/migrate-postgres.sh\" > /dev/null") == 0);
|
||||
CHECK(history(*owner_db) == "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,30,31,32,33,34");
|
||||
"25,26,27,28,29,30,31,32,33,34,35");
|
||||
CHECK(scalar(*owner_db, "SELECT (c.condeferrable AND c.condeferred AND "
|
||||
"c.confrelid = 'evolution.region_commits'::regclass)::text "
|
||||
"FROM pg_catalog.pg_constraint c WHERE c.conrelid = "
|
||||
|
||||
@@ -122,7 +122,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,29,30,31,32,33,34
|
||||
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,30,31,32,33,34,35
|
||||
# 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%% *}"
|
||||
@@ -143,7 +143,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")" = 34: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,30,31,32,33,34
|
||||
test "$(PGDATABASE=v3_commits psql -XAtc "SELECT count(*) || ':' || string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 35: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,30,31,32,33,34,35
|
||||
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.
|
||||
@@ -197,7 +197,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,29,30,31,32,33,34
|
||||
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,30,31,32,33,34,35
|
||||
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
|
||||
phase permission-audit
|
||||
@@ -250,9 +250,9 @@ expect_audit_failure 'REVOKE EXECUTE ON FUNCTION evolution.claim_account_control
|
||||
bash "$root/tools/migrate-postgres.sh" # Restored configuration passes the audit again.
|
||||
phase checksum-drift
|
||||
# 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=35 WHERE version=34'
|
||||
psql -X -v ON_ERROR_STOP=1 -c 'UPDATE evolution.schema_migrations SET version=36 WHERE version=35'
|
||||
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=34 WHERE version=35"
|
||||
psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET version=35 WHERE version=36"
|
||||
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"
|
||||
|
||||
@@ -80,6 +80,8 @@ digest_33="$(sha256sum "$root/migrations/0033_fix_cellular_action_tick_offset.sq
|
||||
digest_33="${digest_33%% *}"
|
||||
digest_34="$(sha256sum "$root/migrations/0034_cellular_v4_engulf_bundle.sql")"
|
||||
digest_34="${digest_34%% *}"
|
||||
digest_35="$(sha256sum "$root/migrations/0035_cellular_mutation_publication.sql")"
|
||||
digest_35="${digest_35%% *}"
|
||||
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" \
|
||||
@@ -107,5 +109,6 @@ psql -X --no-password --set=ON_ERROR_STOP=1 --set="migration_sha=$digest" \
|
||||
--set="migration_sha_32=$digest_32" \
|
||||
--set="migration_sha_33=$digest_33" \
|
||||
--set="migration_sha_34=$digest_34" \
|
||||
--set="migration_sha_35=$digest_35" \
|
||||
--set="runtime_role=$runtime_role" \
|
||||
--file="$root/migrations/apply.sql"
|
||||
|
||||
Reference in New Issue
Block a user