Foundation / checks (client) (push) Successful in 5s
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 4m27s
Foundation / postgres-tools (push) Successful in 30s
Foundation / server-image (push) Successful in 4m16s
Foundation / image (push) Successful in 4m54s
992 lines
58 KiB
C++
992 lines
58 KiB
C++
#include "evo/persistence/cellular_migration_store.hpp"
|
|
#include "evo/persistence/checkpoint_store.hpp"
|
|
#include "evo/persistence/population_checkpoint_store.hpp"
|
|
#include "evo/persistence/sha256.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <functional>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <tuple>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace evo::persistence {
|
|
namespace {
|
|
using std::chrono::milliseconds;
|
|
|
|
std::uint64_t input_tick(const server::CellularActionRecord& action) {
|
|
std::uint64_t tick{};
|
|
for (std::size_t i = 0; i < 8; ++i)
|
|
tick |= static_cast<std::uint64_t>(action.input[i]) << (i * 8U);
|
|
return tick;
|
|
}
|
|
|
|
CellularPublicationFailure failure(PublicationError kind, ObjectError object = {},
|
|
LeaseStoreFailure store = {},
|
|
CellularPreparedCommit prepared = {}) {
|
|
return {kind, store, object, std::move(prepared)};
|
|
}
|
|
CellularProfileMigrationFailure migration_failure(PublicationError kind, ObjectError object = {},
|
|
LeaseStoreFailure store = {},
|
|
CellularProfileMigrationPrepared prepared = {}) {
|
|
return {kind, store, object, std::move(prepared)};
|
|
}
|
|
std::expected<void, CellularPublicationFailure> put_verified(const FilesystemObjectStore& objects,
|
|
std::span<const std::uint8_t> bytes,
|
|
VerifiedObject expected) {
|
|
auto stored = objects.put(bytes);
|
|
if (!stored)
|
|
return std::unexpected{failure(PublicationError::upload, stored.error())};
|
|
auto readback = objects.get(*stored);
|
|
if (!readback || *stored != expected || !std::ranges::equal(*readback, bytes))
|
|
return std::unexpected{
|
|
failure(PublicationError::verify, readback ? ObjectError::corrupt : readback.error())};
|
|
return {};
|
|
}
|
|
std::expected<CommittedCheckpoint, CellularPublicationFailure>
|
|
confirm(const RegionHead& head, const CellularPreparedCommit& prepared,
|
|
const CheckpointManifest& manifest) {
|
|
const auto digest = sha256(prepared.manifest);
|
|
if (!digest || head.digest != *digest || head.sequence != manifest.sequence ||
|
|
head.commit != manifest.commit || head.end_tick != manifest.end_tick ||
|
|
head.frame != manifest.frame)
|
|
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) {
|
|
// A life that ended before the batch (ADR-0121 amendment, schema 59) has no owner
|
|
// entry; one still present must be the account's.
|
|
const auto life = std::ranges::find(
|
|
base.lives(), action.input.life_id,
|
|
[](const server::CellularOwnedLife& value) { return value.identity.life; });
|
|
if (life != base.lives().end() && life->account != action.account)
|
|
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;
|
|
}
|
|
|
|
// The tick of the trace selection a bound choice names (its life, offer and operator).
|
|
std::optional<std::uint64_t> applied_tick(const server::CellularBatch& trace,
|
|
const server::CellularMutationActionRecord& action) {
|
|
std::optional<std::uint64_t> found;
|
|
for (std::size_t i = 0; i < trace.ticks.size(); ++i)
|
|
for (const auto& selection : trace.ticks[i].mutations)
|
|
if (selection.life_id == action.input.life_id &&
|
|
selection.event_index == action.input.event_index &&
|
|
selection.offer_tick == action.input.offer_tick &&
|
|
selection.operator_id == action.input.operator_id &&
|
|
selection.become == action.input.become) {
|
|
if (found)
|
|
return std::nullopt;
|
|
found = trace.header.begin_tick + i + 1;
|
|
}
|
|
return found;
|
|
}
|
|
|
|
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) {
|
|
// Rules 13 (bundle 15): applied (an organism), life died, insufficient funding,
|
|
// declined, split applied and split refused.
|
|
return manifest.cellular_versions == cellular_v15_versions ? result == 4 || result > 6
|
|
: cellular_plan_bundle(manifest.cellular_versions)
|
|
? result != 1 && result != 3 && result != 4
|
|
: result > 3;
|
|
}))
|
|
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 =
|
|
decoded_actions
|
|
? server::encode_cellular_action_block(*decoded_actions, manifest.begin_tick,
|
|
manifest.end_tick)
|
|
: std::expected<std::vector<std::uint8_t>, server::CellularActionBlockError>{
|
|
std::unexpected{server::CellularActionBlockError::format}};
|
|
const auto encoded_trace = server::encode_cellular_batch(trace);
|
|
const auto digest = sha256(prepared.trace);
|
|
const bool trace_version_matches =
|
|
trace.format_version == cellular_checkpoint_trace_format(manifest.cellular_versions);
|
|
return manifest.profile == CheckpointProfile::cellular_lives_v1 && trace_version_matches &&
|
|
manifest.key == prepared.key && manifest.commit == prepared.commit &&
|
|
manifest.sequence > 0 && manifest.batch.trace_bytes == prepared.trace.size() && digest &&
|
|
*digest == manifest.batch.trace_sha256 && manifest.batch.decision_count == 0 &&
|
|
manifest.batch.decisions_sha256 == Sha256Digest{} &&
|
|
trace.header.world == prepared.key.world && trace.header.region == prepared.key.region &&
|
|
trace.header.frame == manifest.frame &&
|
|
trace.header.predecessor == manifest.predecessor_commit &&
|
|
trace.header.predecessor_digest == manifest.predecessor_digest &&
|
|
trace.header.begin_tick == manifest.begin_tick &&
|
|
trace.header.end_tick == manifest.end_tick && encoded_actions &&
|
|
*encoded_actions == prepared.actions && encoded_trace &&
|
|
*encoded_trace == prepared.trace;
|
|
}
|
|
} // namespace
|
|
|
|
std::expected<CommittedCheckpoint, CellularPublicationFailure>
|
|
provision_cellular_region(Postgres& owner, const FilesystemObjectStore& objects, RegionKey key,
|
|
std::uint64_t frame, const server::CellularLives& opening, Uuid commit,
|
|
const Sha256Digest& build, milliseconds timeout) {
|
|
const auto payload = opening.snapshot();
|
|
if (opening.world() != key.world || opening.region() != key.region ||
|
|
opening.frame() != frame || opening.tick() != 0 || !opening.lives().empty() ||
|
|
!opening.corpses().empty() || opening.archive().recorded() != 0 ||
|
|
opening.archive().archived() != 0)
|
|
return std::unexpected{failure(PublicationError::candidate)};
|
|
const auto digest = sha256(payload);
|
|
if (!digest)
|
|
return std::unexpected{failure(PublicationError::digest)};
|
|
auto manifest =
|
|
genesis_manifest(key, frame, commit, biology::cellular_motion_tick_microseconds, build,
|
|
payload.size(), *digest, CheckpointProfile::cellular_lives_v1);
|
|
if (manifest && opening.format_version() == server::cellular_lives_format &&
|
|
opening.rules_version() == server::cellular_lives_rules) {
|
|
manifest->cellular_versions = cellular_v2_versions;
|
|
} else if (manifest && opening.format_version() == server::cellular_lives_engulf_format &&
|
|
opening.rules_version() == server::cellular_lives_engulf_rules) {
|
|
// ADR-0124: a rules-3 region starts on the v4 bundle with genome revisions.
|
|
manifest->cellular_versions = cellular_v4_versions;
|
|
manifest->history_profile = cellular_revision_history_profile;
|
|
} else if (manifest && opening.format_version() == server::cellular_lives_bud_format &&
|
|
opening.rules_version() == server::cellular_lives_bud_rules) {
|
|
// ADR-0125: a rules-4 region starts on the v5 bundle with genome revisions.
|
|
manifest->cellular_versions = cellular_v5_versions;
|
|
manifest->history_profile = cellular_revision_history_profile;
|
|
} else if (manifest && opening.format_version() == server::cellular_lives_presence_format &&
|
|
opening.rules_version() == server::cellular_lives_presence_rules) {
|
|
// ADR-0128: a rules-5 region starts on the v6 bundle with genome revisions.
|
|
manifest->cellular_versions = cellular_v6_versions;
|
|
manifest->history_profile = cellular_revision_history_profile;
|
|
} else if (manifest && opening.format_version() == server::cellular_lives_presence_format &&
|
|
opening.rules_version() == server::cellular_lives_foraging_rules) {
|
|
// ADR-0136 step A: a rules-8 region starts on the v9 bundle with genome revisions.
|
|
manifest->cellular_versions = cellular_v9_versions;
|
|
manifest->history_profile = cellular_revision_history_profile;
|
|
} else if (manifest && opening.format_version() == server::cellular_lives_presence_format &&
|
|
opening.rules_version() == server::cellular_lives_reproduction_rules) {
|
|
// ADR-0137: a rules-7 region starts on the v8 bundle with genome revisions.
|
|
manifest->cellular_versions = cellular_v8_versions;
|
|
manifest->history_profile = cellular_revision_history_profile;
|
|
} else if (manifest && opening.format_version() == server::cellular_lives_presence_format &&
|
|
opening.rules_version() == server::cellular_lives_balance_rules) {
|
|
// 2026-10-15 balance: a rules-6 region starts on the v7 bundle with genome revisions.
|
|
manifest->cellular_versions = cellular_v7_versions;
|
|
manifest->history_profile = cellular_revision_history_profile;
|
|
} else if (manifest && (opening.format_version() != server::cellular_lives_legacy_format ||
|
|
opening.rules_version() != 1))
|
|
return std::unexpected{failure(PublicationError::candidate)};
|
|
auto encoded = manifest ? encode_manifest(*manifest)
|
|
: std::expected<std::vector<std::uint8_t>, CheckpointError>{
|
|
std::unexpected{CheckpointError::format}};
|
|
if (!encoded || !restore_cellular_payload(payload, *manifest))
|
|
return std::unexpected{failure(PublicationError::candidate)};
|
|
if (auto stored = put_verified(objects, payload, {*digest, payload.size()}); !stored)
|
|
return std::unexpected{stored.error()};
|
|
|
|
std::string manifest_hex = "\\x";
|
|
constexpr std::string_view digits = "0123456789abcdef";
|
|
manifest_hex.reserve(2 + encoded->size() * 2);
|
|
for (const auto byte : *encoded) {
|
|
manifest_hex.push_back(digits[byte >> 4U]);
|
|
manifest_hex.push_back(digits[byte & 0x0fU]);
|
|
}
|
|
const std::array<std::string, 1> parameters{std::move(manifest_hex)};
|
|
auto inserted =
|
|
owner.query("SELECT evolution.provision_region_checkpoint($1::bytea)", parameters, timeout);
|
|
if (!inserted) {
|
|
auto existing = load_region_checkpoint(owner, key, timeout);
|
|
if (existing && existing->manifest == *encoded)
|
|
return confirm(existing->head,
|
|
CellularPreparedCommit{key, commit, *encoded, {}, {}, {}, {}},
|
|
*manifest);
|
|
if (existing)
|
|
return std::unexpected{failure(PublicationError::rejected)};
|
|
if (existing.error().kind != LeaseStoreError::not_provisioned &&
|
|
inserted.error().kind == PgError::unknown)
|
|
return std::unexpected{failure(PublicationError::unknown, {}, existing.error())};
|
|
const bool unknown = inserted.error().kind == PgError::unknown;
|
|
return std::unexpected{failure(
|
|
unknown ? PublicationError::unknown : PublicationError::rejected, {},
|
|
{unknown ? LeaseStoreError::unknown : LeaseStoreError::store, inserted.error()})};
|
|
}
|
|
auto committed = load_region_checkpoint(owner, key, timeout);
|
|
if (!committed)
|
|
return std::unexpected{failure(PublicationError::unknown, {}, committed.error())};
|
|
CellularPreparedCommit prepared{key, commit, *encoded, {}, {}, {}, {}};
|
|
auto confirmed = confirm(committed->head, prepared, *manifest);
|
|
if (!confirmed)
|
|
return std::unexpected{confirmed.error()};
|
|
return *confirmed;
|
|
}
|
|
|
|
std::expected<CommittedCheckpoint, CellularProfileMigrationFailure>
|
|
retry_cellular_profile_migration(Postgres& db, const CellularProfileMigrationPrepared& prepared,
|
|
milliseconds timeout) {
|
|
const auto manifest = decode_manifest(prepared.manifest);
|
|
if (!manifest || manifest->key != prepared.key || manifest->commit != prepared.commit ||
|
|
!supported(*manifest) || !encode_manifest(*manifest) ||
|
|
!((manifest->profile == CheckpointProfile::cellular_lives_v1 &&
|
|
(manifest->cellular_versions == cellular_v2_versions ||
|
|
manifest->cellular_versions == cellular_v3_versions ||
|
|
cellular_plan_bundle(manifest->cellular_versions))) ||
|
|
((manifest->profile == CheckpointProfile::cellular_lives_v1 ||
|
|
manifest->profile == CheckpointProfile::cellular_population_v1) &&
|
|
(manifest->cellular_versions == cellular_v10_versions ||
|
|
manifest->cellular_versions == cellular_v11_versions))) ||
|
|
manifest->begin_tick != manifest->end_tick || manifest->batch != BatchDigests{})
|
|
return std::unexpected{migration_failure(PublicationError::candidate, {}, {}, prepared)};
|
|
|
|
const auto confirm_commit = [&](const RegionHead& head) {
|
|
const auto digest = sha256(prepared.manifest);
|
|
if (!digest || head.sequence != manifest->sequence || head.commit != manifest->commit ||
|
|
head.digest != *digest || head.end_tick != manifest->end_tick ||
|
|
head.frame != manifest->frame)
|
|
return std::expected<CommittedCheckpoint, CellularProfileMigrationFailure>{
|
|
std::unexpected{migration_failure(PublicationError::receipt, {}, {}, prepared)}};
|
|
return std::expected<CommittedCheckpoint, CellularProfileMigrationFailure>{
|
|
CommittedCheckpoint{head, prepared.manifest}};
|
|
};
|
|
|
|
auto published = publish_cellular_profile_migration(db, prepared.manifest, timeout);
|
|
if (published)
|
|
return confirm_commit(*published);
|
|
|
|
if (published.error().kind == LeaseStoreError::unknown) {
|
|
const auto stored = load_region_commit(db, prepared.key, manifest->sequence, timeout);
|
|
if (stored && stored->manifest == prepared.manifest)
|
|
return confirm_commit(stored->head);
|
|
if (!stored && stored.error().kind != LeaseStoreError::not_provisioned)
|
|
return std::unexpected{
|
|
migration_failure(PublicationError::unknown, {}, stored.error(), prepared)};
|
|
return std::unexpected{
|
|
migration_failure(PublicationError::unknown, {}, published.error(), prepared)};
|
|
}
|
|
return std::unexpected{
|
|
migration_failure(PublicationError::rejected, {}, published.error(), prepared)};
|
|
}
|
|
|
|
namespace {
|
|
std::expected<CommittedCheckpoint, CellularProfileMigrationFailure> migrate_rules_profile(
|
|
Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease,
|
|
const CommittedCheckpoint& predecessor, Uuid commit, const Sha256Digest& build,
|
|
milliseconds timeout, const PublishHooks& hooks,
|
|
const std::function<std::expected<std::vector<std::uint8_t>, CheckpointError>(
|
|
std::span<const std::uint8_t>, const CheckpointManifest&)>& upgrade_payload,
|
|
decltype(&cellular_maturation_upgrade_manifest) derive) {
|
|
const auto previous = decode_manifest(predecessor.manifest);
|
|
const auto previous_digest = sha256(predecessor.manifest);
|
|
if (!previous || !previous_digest || predecessor.head.digest != *previous_digest ||
|
|
predecessor.head.sequence != previous->sequence ||
|
|
predecessor.head.commit != previous->commit ||
|
|
predecessor.head.end_tick != previous->end_tick ||
|
|
predecessor.head.frame != previous->frame)
|
|
return std::unexpected(migration_failure(PublicationError::predecessor));
|
|
auto source = objects.get({previous->payload_sha256, previous->payload_bytes});
|
|
if (!source)
|
|
return std::unexpected(migration_failure(PublicationError::predecessor, source.error()));
|
|
auto payload = upgrade_payload(*source, *previous);
|
|
if (!payload)
|
|
return std::unexpected(migration_failure(PublicationError::candidate));
|
|
const auto digest = sha256(*payload);
|
|
if (!digest)
|
|
return std::unexpected(migration_failure(PublicationError::digest));
|
|
auto manifest =
|
|
derive(*previous, *previous_digest, commit, lease, build, payload->size(), *digest);
|
|
if (!manifest)
|
|
return std::unexpected(migration_failure(PublicationError::candidate));
|
|
auto encoded = encode_manifest(*manifest);
|
|
if (!encoded)
|
|
return std::unexpected(migration_failure(PublicationError::candidate));
|
|
if (auto stored = put_verified(objects, *payload, {*digest, payload->size()}); !stored)
|
|
return std::unexpected(migration_failure(stored.error().kind, stored.error().object));
|
|
if (hooks.before_transaction)
|
|
hooks.before_transaction();
|
|
return retry_cellular_profile_migration(db, {previous->key, commit, std::move(*encoded)},
|
|
timeout);
|
|
}
|
|
} // namespace
|
|
std::expected<CommittedCheckpoint, CellularProfileMigrationFailure>
|
|
migrate_cellular_boundary_profile(Postgres& db, const FilesystemObjectStore& objects,
|
|
const LeaseGrant& lease, const CommittedCheckpoint& predecessor,
|
|
Uuid commit, const Sha256Digest& build, milliseconds timeout,
|
|
const PublishHooks& hooks) {
|
|
return migrate_rules_profile(db, objects, lease, predecessor, commit, build, timeout, hooks,
|
|
cellular_boundary_upgrade_payload,
|
|
cellular_boundary_upgrade_manifest);
|
|
}
|
|
std::expected<CommittedCheckpoint, CellularProfileMigrationFailure>
|
|
migrate_cellular_maturation_profile(Postgres& db, const FilesystemObjectStore& objects,
|
|
const LeaseGrant& lease, const CommittedCheckpoint& predecessor,
|
|
Uuid commit, const Sha256Digest& build, milliseconds timeout,
|
|
const PublishHooks& hooks) {
|
|
return migrate_rules_profile(db, objects, lease, predecessor, commit, build, timeout, hooks,
|
|
cellular_maturation_upgrade_payload,
|
|
cellular_maturation_upgrade_manifest);
|
|
}
|
|
std::expected<CommittedCheckpoint, CellularProfileMigrationFailure>
|
|
migrate_cellular_inheritance_profile(Postgres& db, const FilesystemObjectStore& objects,
|
|
const LeaseGrant& lease,
|
|
const CommittedCheckpoint& predecessor, Uuid commit,
|
|
const Sha256Digest& build, milliseconds timeout,
|
|
const PublishHooks& hooks) {
|
|
return migrate_rules_profile(db, objects, lease, predecessor, commit, build, timeout, hooks,
|
|
cellular_inheritance_upgrade_payload,
|
|
cellular_inheritance_upgrade_manifest);
|
|
}
|
|
std::expected<CommittedCheckpoint, CellularProfileMigrationFailure>
|
|
migrate_cellular_photosynthesis_profile(Postgres& db, const FilesystemObjectStore& objects,
|
|
const LeaseGrant& lease,
|
|
const CommittedCheckpoint& predecessor, Uuid commit,
|
|
const Sha256Digest& build, server::CellularLight light,
|
|
milliseconds timeout, const PublishHooks& hooks) {
|
|
return migrate_rules_profile(
|
|
db, objects, lease, predecessor, commit, build, timeout, hooks,
|
|
[light](std::span<const std::uint8_t> payload, const CheckpointManifest& previous) {
|
|
return cellular_photosynthesis_upgrade_payload(payload, previous, light);
|
|
},
|
|
cellular_photosynthesis_upgrade_manifest);
|
|
}
|
|
std::expected<CommittedCheckpoint, CellularProfileMigrationFailure> migrate_cellular_liquid_profile(
|
|
Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease,
|
|
const CommittedCheckpoint& predecessor, Uuid commit, const Sha256Digest& build,
|
|
const server::CellularLiquidRules& rules, milliseconds timeout, const PublishHooks& hooks) {
|
|
return migrate_rules_profile(
|
|
db, objects, lease, predecessor, commit, build, timeout, hooks,
|
|
[rules](std::span<const std::uint8_t> payload, const CheckpointManifest& previous) {
|
|
return cellular_liquid_upgrade_payload(payload, previous, rules);
|
|
},
|
|
cellular_liquid_upgrade_manifest);
|
|
}
|
|
std::expected<CommittedCheckpoint, CellularProfileMigrationFailure> migrate_cellular_revamp_profile(
|
|
Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease,
|
|
const CommittedCheckpoint& predecessor, Uuid commit, const Sha256Digest& build,
|
|
const server::CellularRevampRules& rules, milliseconds timeout, const PublishHooks& hooks,
|
|
std::uint32_t count, std::uint64_t first) {
|
|
return migrate_rules_profile(
|
|
db, objects, lease, predecessor, commit, build, timeout, hooks,
|
|
[rules, count, first](std::span<const std::uint8_t> payload,
|
|
const CheckpointManifest& previous) {
|
|
return cellular_revamp_upgrade_payload(payload, previous, rules, count, first);
|
|
},
|
|
cellular_revamp_upgrade_manifest);
|
|
}
|
|
|
|
std::expected<CommittedCheckpoint, CellularProfileMigrationFailure>
|
|
migrate_cellular_profile_to_v2(Postgres& db, const FilesystemObjectStore& objects,
|
|
const LeaseGrant& lease, const CommittedCheckpoint& predecessor,
|
|
Uuid commit, const Sha256Digest& build, milliseconds timeout) {
|
|
const auto previous = decode_manifest(predecessor.manifest);
|
|
const auto previous_digest = sha256(predecessor.manifest);
|
|
if (!previous || !previous_digest ||
|
|
previous->profile != CheckpointProfile::cellular_lives_v1 ||
|
|
previous->cellular_versions != cellular_v1_versions ||
|
|
predecessor.head.digest != *previous_digest ||
|
|
predecessor.head.sequence != previous->sequence ||
|
|
predecessor.head.commit != previous->commit ||
|
|
predecessor.head.end_tick != previous->end_tick || lease.frame != previous->frame ||
|
|
previous->sequence == 0 || previous->end_tick == 0)
|
|
return std::unexpected{migration_failure(PublicationError::candidate)};
|
|
|
|
auto source_payload = objects.get({previous->payload_sha256, previous->payload_bytes});
|
|
if (!source_payload)
|
|
return std::unexpected{
|
|
migration_failure(PublicationError::predecessor, source_payload.error())};
|
|
const auto source_digest = sha256(*source_payload);
|
|
if (!source_digest || *source_digest != previous->payload_sha256)
|
|
return std::unexpected{
|
|
migration_failure(PublicationError::predecessor, ObjectError::corrupt)};
|
|
auto source = restore_cellular_payload(*source_payload, *previous);
|
|
if (!source || (*source)->format_version() != server::cellular_lives_legacy_format ||
|
|
(*source)->rules_version() != 1)
|
|
return std::unexpected{migration_failure(PublicationError::predecessor)};
|
|
auto migrated = (*source)->migrate_to_v2();
|
|
if (!migrated || (*migrated)->tick() != (*source)->tick() ||
|
|
(*migrated)->current_total() != (*source)->current_total())
|
|
return std::unexpected{migration_failure(PublicationError::candidate)};
|
|
|
|
const auto payload = (*migrated)->snapshot();
|
|
const auto payload_digest = sha256(payload);
|
|
if (!payload_digest)
|
|
return std::unexpected{migration_failure(PublicationError::digest)};
|
|
auto manifest = cellular_profile_migration_manifest(*previous, *previous_digest, commit, lease,
|
|
build, payload.size(), *payload_digest);
|
|
auto encoded = manifest ? encode_manifest(*manifest)
|
|
: std::expected<std::vector<std::uint8_t>, CheckpointError>{
|
|
std::unexpected{CheckpointError::format}};
|
|
if (!encoded || !restore_cellular_payload(payload, *manifest))
|
|
return std::unexpected{migration_failure(PublicationError::candidate)};
|
|
if (auto stored = put_verified(objects, payload, {*payload_digest, payload.size()}); !stored)
|
|
return std::unexpected{migration_failure(stored.error().kind, stored.error().object)};
|
|
return retry_cellular_profile_migration(
|
|
db, CellularProfileMigrationPrepared{previous->key, commit, std::move(*encoded)}, timeout);
|
|
}
|
|
|
|
std::expected<CommittedCheckpoint, CellularProfileMigrationFailure>
|
|
migrate_cellular_profile_to_v3(Postgres& db, const FilesystemObjectStore& objects,
|
|
const LeaseGrant& lease, const CommittedCheckpoint& predecessor,
|
|
Uuid commit, const Sha256Digest& build, milliseconds timeout) {
|
|
const auto previous = decode_manifest(predecessor.manifest);
|
|
const auto previous_digest = sha256(predecessor.manifest);
|
|
if (!previous || !previous_digest ||
|
|
previous->profile != CheckpointProfile::cellular_lives_v1 ||
|
|
previous->cellular_versions != cellular_v2_versions ||
|
|
predecessor.head.digest != *previous_digest ||
|
|
predecessor.head.sequence != previous->sequence ||
|
|
predecessor.head.commit != previous->commit ||
|
|
predecessor.head.end_tick != previous->end_tick || lease.frame != previous->frame ||
|
|
previous->sequence == 0 || previous->end_tick == 0)
|
|
return std::unexpected{migration_failure(PublicationError::candidate)};
|
|
|
|
auto source_payload = objects.get({previous->payload_sha256, previous->payload_bytes});
|
|
if (!source_payload)
|
|
return std::unexpected{
|
|
migration_failure(PublicationError::predecessor, source_payload.error())};
|
|
const auto source_digest = sha256(*source_payload);
|
|
if (!source_digest || *source_digest != previous->payload_sha256)
|
|
return std::unexpected{
|
|
migration_failure(PublicationError::predecessor, ObjectError::corrupt)};
|
|
auto source = restore_cellular_payload(*source_payload, *previous);
|
|
if (!source || (*source)->format_version() != server::cellular_lives_format ||
|
|
(*source)->rules_version() != server::cellular_lives_rules)
|
|
return std::unexpected{migration_failure(PublicationError::predecessor)};
|
|
auto migrated = (*source)->migrate_to_v3();
|
|
if (!migrated || (*migrated)->tick() != (*source)->tick() ||
|
|
(*migrated)->current_total() != (*source)->current_total())
|
|
return std::unexpected{migration_failure(PublicationError::candidate)};
|
|
|
|
const auto payload = (*migrated)->snapshot();
|
|
const auto payload_digest = sha256(payload);
|
|
if (!payload_digest)
|
|
return std::unexpected{migration_failure(PublicationError::digest)};
|
|
auto manifest = cellular_profile_migration_manifest(*previous, *previous_digest, commit, lease,
|
|
build, payload.size(), *payload_digest);
|
|
auto encoded = manifest ? encode_manifest(*manifest)
|
|
: std::expected<std::vector<std::uint8_t>, CheckpointError>{
|
|
std::unexpected{CheckpointError::format}};
|
|
if (!encoded || !restore_cellular_payload(payload, *manifest))
|
|
return std::unexpected{migration_failure(PublicationError::candidate)};
|
|
if (auto stored = put_verified(objects, payload, {*payload_digest, payload.size()}); !stored)
|
|
return std::unexpected{migration_failure(stored.error().kind, stored.error().object)};
|
|
return retry_cellular_profile_migration(
|
|
db, CellularProfileMigrationPrepared{previous->key, commit, std::move(*encoded)}, timeout);
|
|
}
|
|
|
|
std::expected<CommittedCheckpoint, CellularPublicationFailure>
|
|
retry_cellular_prepared(Postgres& db, const CellularPreparedCommit& prepared,
|
|
milliseconds timeout) {
|
|
auto manifest = decode_manifest(prepared.manifest);
|
|
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 = 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
|
|
: PublicationError::rejected,
|
|
{}, result.error(), prepared)};
|
|
}
|
|
return confirm(*result, prepared, *manifest);
|
|
}
|
|
|
|
std::expected<ResolvedCommit, LeaseStoreFailure>
|
|
resolve_cellular_prepared(Postgres& db, const CellularPreparedCommit& prepared,
|
|
milliseconds timeout) {
|
|
auto manifest = decode_manifest(prepared.manifest);
|
|
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);
|
|
}
|
|
|
|
std::expected<PublishedCellularBatch, CellularPublicationFailure>
|
|
publish_cellular_batch(Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease,
|
|
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::CellularMutationActionRecord> mutation_actions) {
|
|
const auto previous = decode_manifest(predecessor.manifest);
|
|
const auto predecessor_digest = sha256(predecessor.manifest);
|
|
auto committed_batch = batch;
|
|
if (previous)
|
|
committed_batch.format_version =
|
|
cellular_checkpoint_trace_format(previous->cellular_versions);
|
|
const auto trace = server::encode_cellular_batch(committed_batch);
|
|
const auto action_block = server::encode_cellular_action_block(actions, batch.header.begin_tick,
|
|
batch.header.end_tick);
|
|
if (!previous || !predecessor_digest || !trace || !action_block ||
|
|
previous->profile != CheckpointProfile::cellular_lives_v1 ||
|
|
predecessor.head.digest != *predecessor_digest ||
|
|
previous->sequence != predecessor.head.sequence ||
|
|
previous->commit != predecessor.head.commit ||
|
|
previous->end_tick != predecessor.head.end_tick ||
|
|
batch.header.world != previous->key.world || batch.header.region != previous->key.region ||
|
|
batch.header.frame != previous->frame || batch.header.predecessor != previous->commit ||
|
|
batch.header.predecessor_digest != *predecessor_digest ||
|
|
batch.header.begin_tick != previous->end_tick || candidate.world() != previous->key.world ||
|
|
candidate.region() != previous->key.region || candidate.frame() != previous->frame ||
|
|
candidate.format_version() != previous->cellular_versions.envelope ||
|
|
candidate.rules_version() != previous->cellular_versions.rules ||
|
|
candidate.tick() != batch.header.end_tick ||
|
|
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)
|
|
return std::unexpected{failure(PublicationError::predecessor, predecessor_bytes.error())};
|
|
auto base = restore_cellular_payload(*predecessor_bytes, *previous);
|
|
if (!base)
|
|
return std::unexpected{failure(PublicationError::predecessor)};
|
|
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) ||
|
|
(!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);
|
|
const auto trace_digest = sha256(*trace);
|
|
if (!payload_digest || !trace_digest)
|
|
return std::unexpected{failure(PublicationError::digest)};
|
|
auto manifest = successor_manifest(*previous, *predecessor_digest, commit, lease,
|
|
batch.header.end_tick, build, payload.size(),
|
|
*payload_digest, {trace->size(), *trace_digest, 0, {}});
|
|
auto manifest_bytes = manifest ? encode_manifest(*manifest)
|
|
: std::expected<std::vector<std::uint8_t>, CheckpointError>{
|
|
std::unexpected{CheckpointError::format}};
|
|
if (!manifest || !manifest_bytes || !restore_cellular_payload(payload, *manifest))
|
|
return std::unexpected{failure(PublicationError::candidate)};
|
|
if (auto stored = put_verified(objects, payload, {*payload_digest, payload.size()}); !stored)
|
|
return std::unexpected{stored.error()};
|
|
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, 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()};
|
|
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<void, RecoveryFailure>
|
|
verify_cellular_no_tick_checkpoint(Postgres& db, const FilesystemObjectStore& objects,
|
|
const LeaseGrant& lease, const CommittedCheckpoint& committed,
|
|
const CheckpointManifest& m,
|
|
std::span<const std::uint8_t> payload, milliseconds timeout) {
|
|
const auto fail = [](RecoveryError kind) { return std::unexpected(RecoveryFailure{kind, {}}); };
|
|
const auto digest = sha256(committed.manifest);
|
|
if (!digest || *digest != committed.head.digest || m.commit != committed.head.commit ||
|
|
m.sequence != committed.head.sequence || m.end_tick != committed.head.end_tick ||
|
|
m.frame != committed.head.frame || lease.frame != m.frame || m.sequence == 0 ||
|
|
m.begin_tick != m.end_tick || m.batch != BatchDigests{})
|
|
return fail(RecoveryError::manifest);
|
|
if (m.cellular_versions == cellular_v10_versions ||
|
|
m.cellular_versions == cellular_v11_versions) {
|
|
auto paired =
|
|
verify_cellular_migration_checkpoint(db, objects, lease, committed, payload, timeout);
|
|
if (!paired)
|
|
return std::unexpected(paired.error());
|
|
if (*paired)
|
|
return {};
|
|
}
|
|
auto evidence = load_cellular_profile_migration(db, m.key, lease, m.commit, timeout);
|
|
if (!evidence)
|
|
return std::unexpected(RecoveryFailure{RecoveryError::store, evidence.error()});
|
|
const auto optional_record = std::move(*evidence);
|
|
if (!optional_record.has_value())
|
|
return verify_population_profile_transition(db, objects, lease, committed, m, payload,
|
|
timeout);
|
|
const auto& record = *optional_record;
|
|
auto predecessor = load_region_commit(db, m.key, m.sequence - 1, timeout);
|
|
if (!predecessor)
|
|
return std::unexpected(RecoveryFailure{RecoveryError::tail, predecessor.error()});
|
|
const auto source_digest = sha256(predecessor->manifest);
|
|
const auto source = decode_manifest(predecessor->manifest);
|
|
const auto target_bytes = encode_manifest(m);
|
|
const auto payload_digest = sha256(payload);
|
|
if (!source || !source_digest || !target_bytes || *target_bytes != committed.manifest ||
|
|
payload.size() != m.payload_bytes || !payload_digest ||
|
|
*payload_digest != m.payload_sha256 || *source_digest != predecessor->head.digest ||
|
|
source->commit != predecessor->head.commit ||
|
|
source->sequence != predecessor->head.sequence ||
|
|
source->end_tick != predecessor->head.end_tick ||
|
|
source->frame != predecessor->head.frame || record.source_sequence != source->sequence ||
|
|
record.source_commit != source->commit || record.source_manifest_sha256 != *source_digest ||
|
|
record.source_payload_sha256 != source->payload_sha256 ||
|
|
record.target_sequence != m.sequence || record.target_commit != m.commit ||
|
|
record.target_manifest_sha256 != *digest ||
|
|
record.target_payload_sha256 != m.payload_sha256 ||
|
|
!std::ranges::equal(record.source_versions,
|
|
std::span{predecessor->manifest}.subspan(160, 36)) ||
|
|
!std::ranges::equal(record.target_versions, std::span{*target_bytes}.subspan(160, 36)))
|
|
return fail(RecoveryError::history);
|
|
// Reconstruct with the recorded writer's lease fields. Loading the immutable
|
|
// evidence above is fenced by the CURRENT lease, which may be a newer epoch.
|
|
const LeaseGrant recorded{m.holder, m.epoch, m.frame, milliseconds{0}};
|
|
const bool boundary = m.cellular_versions == cellular_v10_versions;
|
|
const bool maturation = m.cellular_versions == cellular_v11_versions;
|
|
const bool inheritance = m.cellular_versions == cellular_v12_versions;
|
|
const bool photosynthesis = m.cellular_versions == cellular_v13_versions;
|
|
const bool liquid = m.cellular_versions == cellular_v14_versions;
|
|
const bool revamp = m.cellular_versions == cellular_v15_versions;
|
|
const auto derive = revamp ? cellular_revamp_upgrade_manifest
|
|
: liquid ? cellular_liquid_upgrade_manifest
|
|
: photosynthesis ? cellular_photosynthesis_upgrade_manifest
|
|
: inheritance ? cellular_inheritance_upgrade_manifest
|
|
: maturation ? cellular_maturation_upgrade_manifest
|
|
: boundary ? cellular_boundary_upgrade_manifest
|
|
: cellular_profile_migration_manifest;
|
|
auto expected = derive(*source, *source_digest, m.commit, recorded, m.build, m.payload_bytes,
|
|
m.payload_sha256);
|
|
if (!expected || *expected != m)
|
|
return fail(RecoveryError::history);
|
|
auto bytes = objects.get({source->payload_sha256, source->payload_bytes});
|
|
if (!bytes)
|
|
return fail(RecoveryError::tail);
|
|
if (revamp) {
|
|
// The declared revamp rules are the upgrade's only input: read them from the target,
|
|
// then require the source to re-derive the exact target bytes with them.
|
|
auto target = restore_cellular_payload(payload, m);
|
|
if (!target)
|
|
return fail(RecoveryError::payload);
|
|
auto upgraded = cellular_revamp_upgrade_payload(*bytes, *source, (*target)->revamp_rules());
|
|
if (upgraded && !std::ranges::equal(*upgraded, payload) && !(*target)->lives().empty()) {
|
|
// ADR-0180: the only additional opening input is the explicit fixture identity range.
|
|
upgraded = cellular_revamp_upgrade_payload(
|
|
*bytes, *source, (*target)->revamp_rules(),
|
|
static_cast<std::uint32_t>((*target)->lives().size()),
|
|
(*target)->lives().front().identity.life);
|
|
}
|
|
if (!upgraded || !std::ranges::equal(*upgraded, payload))
|
|
return fail(RecoveryError::history);
|
|
} else if (liquid) {
|
|
// The declared liquid rules are the upgrade's only input: read them from the target,
|
|
// then require the source to re-derive the exact target bytes with them.
|
|
auto target = restore_cellular_payload(payload, m);
|
|
if (!target)
|
|
return fail(RecoveryError::payload);
|
|
auto upgraded = cellular_liquid_upgrade_payload(*bytes, *source, (*target)->liquid_rules());
|
|
if (!upgraded || !std::ranges::equal(*upgraded, payload))
|
|
return fail(RecoveryError::history);
|
|
} else if (photosynthesis) {
|
|
// The declared light is the upgrade's only input: read it from the target, then
|
|
// require the source to re-derive the exact target bytes with it.
|
|
auto target = restore_cellular_payload(payload, m);
|
|
if (!target)
|
|
return fail(RecoveryError::payload);
|
|
auto upgraded =
|
|
cellular_photosynthesis_upgrade_payload(*bytes, *source, (*target)->light());
|
|
if (!upgraded || !std::ranges::equal(*upgraded, payload))
|
|
return fail(RecoveryError::history);
|
|
} else if (boundary || maturation || inheritance) {
|
|
auto upgraded = inheritance ? cellular_inheritance_upgrade_payload(*bytes, *source)
|
|
: maturation ? cellular_maturation_upgrade_payload(*bytes, *source)
|
|
: cellular_boundary_upgrade_payload(*bytes, *source);
|
|
if (!upgraded || !std::ranges::equal(*upgraded, payload))
|
|
return fail(RecoveryError::history);
|
|
} else {
|
|
auto state = restore_cellular_payload(*bytes, *source);
|
|
if (!state)
|
|
return fail(RecoveryError::tail);
|
|
auto upgraded = source->cellular_versions == cellular_v1_versions
|
|
? (*state)->migrate_to_v2()
|
|
: (*state)->migrate_to_v3();
|
|
if (!upgraded || !std::ranges::equal((*upgraded)->snapshot(), payload))
|
|
return fail(RecoveryError::history);
|
|
}
|
|
return {};
|
|
}
|
|
|
|
namespace {
|
|
std::expected<RecoveredCellularCheckpoint, RecoveryFailure>
|
|
recover_cellular_loaded(Postgres& db, const FilesystemObjectStore& objects, RegionKey key,
|
|
const LeaseGrant& lease, std::span<const Sha256Digest> retained_builds,
|
|
milliseconds timeout,
|
|
std::expected<CommittedCheckpoint, LeaseStoreFailure> committed) {
|
|
const auto fail_with = [](RecoveryError kind) {
|
|
return std::unexpected{RecoveryFailure{kind, {}}};
|
|
};
|
|
if (!committed)
|
|
return std::unexpected{
|
|
RecoveryFailure{committed.error().kind == LeaseStoreError::not_provisioned
|
|
? RecoveryError::not_provisioned
|
|
: RecoveryError::store,
|
|
committed.error()}};
|
|
const auto digest = sha256(committed->manifest);
|
|
auto manifest = decode_manifest(committed->manifest);
|
|
if (!digest || *digest != committed->head.digest || !manifest || manifest->key != key ||
|
|
manifest->sequence != committed->head.sequence ||
|
|
manifest->commit != committed->head.commit ||
|
|
manifest->end_tick != committed->head.end_tick || manifest->frame != committed->head.frame)
|
|
return fail_with(RecoveryError::manifest);
|
|
if (!supported(*manifest) || manifest->profile != CheckpointProfile::cellular_lives_v1 ||
|
|
std::ranges::find(retained_builds, manifest->build) == retained_builds.end())
|
|
return fail_with(RecoveryError::unsupported);
|
|
if (lease.frame != manifest->frame)
|
|
return fail_with(RecoveryError::unsupported);
|
|
|
|
auto payload = objects.get({manifest->payload_sha256, manifest->payload_bytes});
|
|
if (!payload)
|
|
return fail_with(payload.error() == ObjectError::io ? RecoveryError::store
|
|
: RecoveryError::corrupt_payload);
|
|
auto state = restore_cellular_payload(*payload, *manifest);
|
|
if (!state)
|
|
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 verified = verify_cellular_no_tick_checkpoint(db, objects, lease, *committed,
|
|
*manifest, *payload, timeout);
|
|
if (!verified)
|
|
return std::unexpected(verified.error());
|
|
return RecoveredCellularCheckpoint{
|
|
std::move(*committed), *manifest, std::move(*state), {}, {}, {}};
|
|
}
|
|
if (manifest->sequence != 0) {
|
|
auto action_bytes =
|
|
load_cellular_publication_actions(db, key, lease, manifest->commit, timeout);
|
|
if (!action_bytes)
|
|
return std::unexpected{RecoveryFailure{RecoveryError::store, action_bytes.error()}};
|
|
if (!*action_bytes)
|
|
return fail_with(RecoveryError::history);
|
|
const auto encoded_actions = action_bytes->value_or(std::vector<std::uint8_t>{});
|
|
|
|
auto trace_bytes = objects.get({manifest->batch.trace_sha256, manifest->batch.trace_bytes});
|
|
if (!trace_bytes)
|
|
return fail_with(trace_bytes.error() == ObjectError::io ? RecoveryError::store
|
|
: RecoveryError::tail);
|
|
auto trace = server::decode_cellular_batch(*trace_bytes);
|
|
if (!trace)
|
|
return fail_with(RecoveryError::tail);
|
|
if (trace->format_version != cellular_checkpoint_trace_format(manifest->cellular_versions))
|
|
return fail_with(RecoveryError::tail);
|
|
if (trace->header.world != key.world || trace->header.region != key.region ||
|
|
trace->header.frame != manifest->frame ||
|
|
trace->header.predecessor != manifest->predecessor_commit ||
|
|
trace->header.predecessor_digest != manifest->predecessor_digest ||
|
|
trace->header.begin_tick != manifest->begin_tick ||
|
|
trace->header.end_tick != manifest->end_tick)
|
|
return fail_with(RecoveryError::tail);
|
|
|
|
auto committed_births =
|
|
load_cellular_publication_births(db, key, lease, manifest->commit, timeout);
|
|
if (!committed_births)
|
|
return std::unexpected{RecoveryFailure{RecoveryError::store, committed_births.error()}};
|
|
std::vector<CellularBirthBinding> traced_births;
|
|
for (std::size_t tick_index = 0; tick_index < trace->ticks.size(); ++tick_index) {
|
|
for (const auto& birth : trace->ticks[tick_index].births) {
|
|
traced_births.push_back({birth.account, manifest->commit, birth.request,
|
|
birth.identity, manifest->begin_tick + tick_index + 1,
|
|
manifest->sequence});
|
|
}
|
|
}
|
|
if (traced_births != *committed_births)
|
|
return fail_with(RecoveryError::history);
|
|
|
|
auto predecessor = load_region_commit(db, key, manifest->sequence - 1, timeout);
|
|
if (!predecessor)
|
|
return std::unexpected{RecoveryFailure{RecoveryError::tail, predecessor.error()}};
|
|
const auto predecessor_digest = sha256(predecessor->manifest);
|
|
auto previous = decode_manifest(predecessor->manifest);
|
|
if (!predecessor_digest || *predecessor_digest != manifest->predecessor_digest ||
|
|
predecessor->head.digest != manifest->predecessor_digest ||
|
|
predecessor->head.commit != manifest->predecessor_commit ||
|
|
predecessor->head.sequence != manifest->sequence - 1 || !previous ||
|
|
previous->profile != CheckpointProfile::cellular_lives_v1 ||
|
|
previous->end_tick != manifest->begin_tick)
|
|
return fail_with(RecoveryError::tail);
|
|
auto base_bytes = objects.get({previous->payload_sha256, previous->payload_bytes});
|
|
if (!base_bytes)
|
|
return fail_with(base_bytes.error() == ObjectError::io ? RecoveryError::store
|
|
: RecoveryError::tail);
|
|
auto base = restore_cellular_payload(*base_bytes, *previous);
|
|
if (!base)
|
|
return fail_with(RecoveryError::tail);
|
|
auto batch_actions = server::decode_cellular_action_block(
|
|
encoded_actions, manifest->begin_tick, manifest->end_tick);
|
|
if (!batch_actions ||
|
|
!server::match_cellular_actions_to_trace(**base, *trace, *batch_actions))
|
|
return fail_with(RecoveryError::history);
|
|
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);
|
|
// The stored input names the earliest tick; the choice bound at its selection's.
|
|
const auto bound = applied_tick(*trace, *action);
|
|
if (!bound || *bound < action->tick)
|
|
return fail_with(RecoveryError::history);
|
|
action->tick = *bound;
|
|
mutation_actions.push_back(*action);
|
|
stored_results.push_back(row.result);
|
|
}
|
|
// Results follow the block's canonical (tick, account, action) order.
|
|
std::vector<std::size_t> order(mutation_actions.size());
|
|
for (std::size_t i = 0; i < order.size(); ++i)
|
|
order[i] = i;
|
|
std::ranges::sort(order, [&](std::size_t left, std::size_t right) {
|
|
const auto& a = mutation_actions[left];
|
|
const auto& b = mutation_actions[right];
|
|
return std::tie(a.tick, a.account, a.action_id) <
|
|
std::tie(b.tick, b.account, b.action_id);
|
|
});
|
|
{
|
|
std::vector<server::CellularMutationActionRecord> sorted_actions;
|
|
std::vector<std::uint8_t> sorted_results;
|
|
for (const auto i : order) {
|
|
sorted_actions.push_back(mutation_actions[i]);
|
|
sorted_results.push_back(stored_results[i]);
|
|
}
|
|
mutation_actions = std::move(sorted_actions);
|
|
stored_results = std::move(sorted_results);
|
|
}
|
|
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)
|
|
refs.push_back({action.account, action.action_id});
|
|
auto records = find_region_action_records(db, key, lease, refs, timeout);
|
|
if (!records)
|
|
return std::unexpected{RecoveryFailure{RecoveryError::store, records.error()}};
|
|
for (std::size_t i = 0; i < batch_actions->size(); ++i) {
|
|
const auto found_record = (*records)[i];
|
|
if (!found_record)
|
|
return fail_with(RecoveryError::history);
|
|
const auto record = found_record.value_or(CommittedActionRecord{});
|
|
const auto& action = (*batch_actions)[i];
|
|
if (record.kind != 2 || record.result != 5 || !record.cellular_input ||
|
|
*record.cellular_input !=
|
|
std::vector<std::uint8_t>(action.input.begin(), action.input.end()) ||
|
|
record.action.region != key.region || record.action.tick != input_tick(action) ||
|
|
record.action.commit_sequence != manifest->sequence)
|
|
return fail_with(RecoveryError::history);
|
|
}
|
|
actions = std::move(*batch_actions);
|
|
}
|
|
return RecoveredCellularCheckpoint{std::move(*committed),
|
|
*manifest,
|
|
std::move(*state),
|
|
std::move(actions),
|
|
std::move(mutation_actions_recovered),
|
|
std::move(mutation_results_recovered)};
|
|
}
|
|
} // namespace
|
|
std::expected<RecoveredCellularCheckpoint, RecoveryFailure>
|
|
recover_cellular_checkpoint(Postgres& db, const FilesystemObjectStore& objects, RegionKey key,
|
|
const LeaseGrant& lease, std::span<const Sha256Digest> retained_builds,
|
|
milliseconds timeout) {
|
|
return recover_cellular_loaded(db, objects, key, lease, retained_builds, timeout,
|
|
load_region_checkpoint(db, key, timeout));
|
|
}
|
|
std::expected<RecoveredCellularCheckpoint, RecoveryFailure>
|
|
recover_cellular_commit(Postgres& db, const FilesystemObjectStore& objects, RegionKey key,
|
|
std::uint64_t sequence, const LeaseGrant& lease,
|
|
std::span<const Sha256Digest> retained_builds, milliseconds timeout) {
|
|
return recover_cellular_loaded(db, objects, key, lease, retained_builds, timeout,
|
|
load_region_commit(db, key, sequence, timeout));
|
|
}
|
|
} // namespace evo::persistence
|