#include "evo/persistence/region_checkpoint.hpp" namespace evo::persistence { namespace { std::expected next_population(const CheckpointManifest& previous, const Sha256Digest& previous_digest, Uuid commit, const LeaseGrant& lease, std::uint64_t end_tick, const Sha256Digest& build, std::uint64_t bytes, const Sha256Digest& digest, BatchDigests batch, bool transition) { if (!supported(previous) || !encode_manifest(previous) || previous.sequence == 0 || previous.sequence == ~std::uint64_t{0} || previous.end_tick == 0 || population_envelope_version(previous.cellular_versions) == 0 || previous_digest == Sha256Digest{} || lease.frame != previous.frame || lease.epoch == 0 || lease.holder == Uuid{} || commit == Uuid{} || commit == previous.commit || end_tick < previous.end_tick || end_tick - previous.end_tick > max_checkpoint_ticks || (previous.profile != CheckpointProfile::cellular_lives_v1 && previous.profile != CheckpointProfile::cellular_population_v1) || (!transition && previous.profile != CheckpointProfile::cellular_population_v1)) return std::unexpected(CheckpointError::chain); auto next = previous; if (transition) { next.profile = previous.profile == CheckpointProfile::cellular_lives_v1 ? CheckpointProfile::cellular_population_v1 : CheckpointProfile::cellular_lives_v1; next.history_profile = next.profile == CheckpointProfile::cellular_population_v1 ? population_history_profile : cellular_revision_history_profile; } next.commit = commit; ++next.sequence; next.predecessor_commit = previous.commit; next.predecessor_digest = previous_digest; next.begin_tick = previous.end_tick; next.end_tick = end_tick; next.holder = lease.holder; next.epoch = lease.epoch; next.build = build; next.payload_bytes = bytes; next.payload_sha256 = digest; next.batch = batch; if (!encode_manifest(next)) return std::unexpected(CheckpointError::format); return next; } } // namespace std::expected population_profile_transition_manifest(const CheckpointManifest& previous, const Sha256Digest& previous_digest, Uuid commit, const LeaseGrant& lease, const Sha256Digest& build, std::uint64_t bytes, const Sha256Digest& digest) { return next_population(previous, previous_digest, commit, lease, previous.end_tick, build, bytes, digest, {}, true); } std::expected population_successor_manifest( const CheckpointManifest& previous, const Sha256Digest& previous_digest, Uuid commit, const LeaseGrant& lease, std::uint64_t end_tick, const Sha256Digest& build, std::uint64_t bytes, const Sha256Digest& digest, const BatchDigests& batch) { if (batch.trace_bytes == 0) return std::unexpected(CheckpointError::chain); return next_population(previous, previous_digest, commit, lease, end_tick, build, bytes, digest, batch, false); } std::expected, CheckpointError> restore_population_payload(std::span payload, const CheckpointManifest& m) { if (!supported(m) || m.profile != CheckpointProfile::cellular_population_v1) return std::unexpected(CheckpointError::unsupported); if (!encode_manifest(m) || payload.size() != m.payload_bytes) return std::unexpected(CheckpointError::format); auto state = server::CellularPopulation::restore(payload, m.key.world, m.key.region, m.frame, m.end_tick); if (!state) return std::unexpected(state.error() == server::CellularPopulationError::unsupported ? CheckpointError::unsupported : CheckpointError::payload); if ((*state)->format_version() != population_envelope_version(m.cellular_versions) || (*state)->state().format_version() != m.cellular_versions.envelope || (*state)->state().rules_version() != m.cellular_versions.rules) return std::unexpected(CheckpointError::unsupported); return std::move(*state); } namespace { std::expected upgrade_manifest(const CheckpointManifest& previous, const Sha256Digest& previous_digest, Uuid commit, const LeaseGrant& lease, const Sha256Digest& build, std::uint64_t bytes, const Sha256Digest& digest, const CellularVersionBundle& from, const CellularVersionBundle& to) { if (!supported(previous)) return std::unexpected(CheckpointError::unsupported); if (!encode_manifest(previous) || previous.sequence == 0 || previous.sequence == ~std::uint64_t{0} || previous.end_tick == 0 || previous.cellular_versions != from || (previous.profile != CheckpointProfile::cellular_lives_v1 && previous.profile != CheckpointProfile::cellular_population_v1) || previous_digest == Sha256Digest{} || commit == Uuid{} || commit == previous.commit || lease.frame != previous.frame || lease.epoch == 0 || lease.holder == Uuid{}) return std::unexpected(CheckpointError::chain); auto next = previous; next.cellular_versions = to; next.commit = commit; ++next.sequence; next.predecessor_commit = previous.commit; next.predecessor_digest = previous_digest; next.begin_tick = previous.end_tick; next.holder = lease.holder; next.epoch = lease.epoch; next.build = build; next.payload_bytes = bytes; next.payload_sha256 = digest; next.batch = {}; if (!encode_manifest(next)) return std::unexpected(CheckpointError::format); return next; } std::expected, CheckpointError> upgrade_payload(std::span payload, const CheckpointManifest& previous, bool maturation) { if (!supported(previous) || previous.cellular_versions != (maturation ? cellular_v10_versions : cellular_v9_versions)) return std::unexpected(CheckpointError::unsupported); if (!encode_manifest(previous) || previous.sequence == 0 || previous.end_tick == 0) return std::unexpected(CheckpointError::chain); if (previous.profile == CheckpointProfile::cellular_lives_v1) { auto state = restore_cellular_payload(payload, previous); if (!state) return std::unexpected(state.error()); auto upgraded = maturation ? (*state)->maturation_upgrade_candidate() : (*state)->migration_upgrade_candidate(); if (!upgraded) return std::unexpected(CheckpointError::payload); return (*upgraded)->snapshot(); } if (previous.profile == CheckpointProfile::cellular_population_v1) { auto state = restore_population_payload(payload, previous); if (!state) return std::unexpected(state.error()); auto upgraded = maturation ? (*state)->maturation_upgrade_candidate() : (*state)->migration_upgrade_candidate(); if (!upgraded) return std::unexpected(CheckpointError::payload); return (*upgraded)->snapshot(); } return std::unexpected(CheckpointError::unsupported); } } // namespace std::expected cellular_boundary_upgrade_manifest(const CheckpointManifest& previous, const Sha256Digest& previous_digest, Uuid commit, const LeaseGrant& lease, const Sha256Digest& build, std::uint64_t bytes, const Sha256Digest& digest) { return upgrade_manifest(previous, previous_digest, commit, lease, build, bytes, digest, cellular_v9_versions, cellular_v10_versions); } std::expected cellular_maturation_upgrade_manifest(const CheckpointManifest& previous, const Sha256Digest& previous_digest, Uuid commit, const LeaseGrant& lease, const Sha256Digest& build, std::uint64_t bytes, const Sha256Digest& digest) { return upgrade_manifest(previous, previous_digest, commit, lease, build, bytes, digest, cellular_v10_versions, cellular_v11_versions); } std::expected, CheckpointError> cellular_boundary_upgrade_payload(std::span payload, const CheckpointManifest& previous) { return upgrade_payload(payload, previous, false); } std::expected, CheckpointError> cellular_maturation_upgrade_payload(std::span payload, const CheckpointManifest& previous) { return upgrade_payload(payload, previous, true); } std::expected cellular_inheritance_upgrade_manifest(const CheckpointManifest& previous, const Sha256Digest& previous_digest, Uuid commit, const LeaseGrant& lease, const Sha256Digest& build, std::uint64_t bytes, const Sha256Digest& digest) { if (previous.profile != CheckpointProfile::cellular_lives_v1) return std::unexpected(CheckpointError::unsupported); return upgrade_manifest(previous, previous_digest, commit, lease, build, bytes, digest, cellular_v11_versions, cellular_v12_versions); } std::expected, CheckpointError> cellular_inheritance_upgrade_payload(std::span payload, const CheckpointManifest& previous) { if (!supported(previous) || previous.profile != CheckpointProfile::cellular_lives_v1 || previous.cellular_versions != cellular_v11_versions) return std::unexpected(CheckpointError::unsupported); if (!encode_manifest(previous) || previous.sequence == 0 || previous.end_tick == 0) return std::unexpected(CheckpointError::chain); auto state = restore_cellular_payload(payload, previous); if (!state) return std::unexpected(state.error()); auto upgraded = (*state)->inheritance_upgrade_candidate(); if (!upgraded) return std::unexpected(CheckpointError::payload); return (*upgraded)->snapshot(); } } // namespace evo::persistence