#include "evo/persistence/checkpoint_store.hpp" #include "evo/persistence/sha256.hpp" #include #include #include namespace evo::persistence { namespace { using std::chrono::milliseconds; // Per-profile payload operations; the publication and recovery paths are shared. struct EnvironmentalCodec { using State = processes::EnvironmentalCommunity; using Recovered = RecoveredCheckpoint; static constexpr auto profile = CheckpointProfile::environmental_v1; static auto snapshot(const State& s) { return s.snapshot(); } static std::uint64_t tick(const State& s) { return s.progress().tick; } static std::uint64_t quantum(const State& s) { return s.tick_microseconds(); } static auto restore(std::span payload, const CheckpointManifest& m, std::size_t budget) { return restore_environmental_payload(payload, m, budget); } }; struct LineageCodec { using State = processes::LineageRegion; using Recovered = RecoveredLineageCheckpoint; static constexpr auto profile = CheckpointProfile::environmental_lineage_v1; static auto snapshot(const State& s) { return s.snapshot(); } static std::uint64_t tick(const State& s) { return s.community().progress().tick; } static std::uint64_t quantum(const State& s) { return s.community().tick_microseconds(); } static auto restore(std::span payload, const CheckpointManifest& m, std::size_t budget) { return restore_lineage_payload(payload, m, budget); } }; PublicationFailure fail(PublicationError kind) { return {kind, {}, {}}; } // Validates the exact payload bytes as recovery would, uploads and reads back. // Returns the manifest bytes ready for the database. template std::expected, PublicationFailure> stage(const FilesystemObjectStore& objects, const std::vector& payload, const CheckpointManifest& manifest, std::size_t budget) { auto bytes = encode_manifest(manifest); if (!bytes || manifest.profile != Codec::profile || !Codec::restore(payload, manifest, budget)) return std::unexpected{fail(PublicationError::candidate)}; auto stored = objects.put(payload); if (!stored) return std::unexpected{PublicationFailure{PublicationError::upload, {}, stored.error()}}; auto back = objects.get(*stored); if (!back || *stored != VerifiedObject{manifest.payload_sha256, manifest.payload_bytes} || !std::ranges::equal(*back, payload)) return std::unexpected{PublicationFailure{ PublicationError::verify, {}, back ? ObjectError::corrupt : back.error()}}; return std::move(*bytes); } std::expected confirm(const std::expected& published, std::vector manifest_bytes, const CheckpointManifest& manifest) { if (!published) { const auto kind = published.error().kind == LeaseStoreError::unknown ? PublicationError::unknown : PublicationError::rejected; return std::unexpected{PublicationFailure{kind, published.error(), {}}}; } // The database digested the bytes independently; the receipt must agree. const auto digest = sha256(manifest_bytes); if (!digest || published->digest != *digest || published->sequence != manifest.sequence || published->commit != manifest.commit || published->end_tick != manifest.end_tick || published->frame != manifest.frame) return std::unexpected{fail(PublicationError::receipt)}; return CommittedCheckpoint{*published, std::move(manifest_bytes)}; } template std::expected provision(Postgres& owner, const FilesystemObjectStore& objects, RegionKey key, std::uint64_t frame, const typename Codec::State& opening, Uuid commit, const Sha256Digest& build, std::size_t budget, milliseconds timeout) { auto payload = Codec::snapshot(opening); if (!payload || Codec::tick(opening) != 0) return std::unexpected{fail(PublicationError::candidate)}; const auto digest = sha256(*payload); if (!digest) return std::unexpected{fail(PublicationError::digest)}; auto manifest = genesis_manifest(key, frame, commit, Codec::quantum(opening), build, payload->size(), *digest, Codec::profile); if (!manifest) return std::unexpected{fail(PublicationError::candidate)}; auto bytes = stage(objects, *payload, *manifest, budget); if (!bytes) return std::unexpected{bytes.error()}; constexpr char digits[] = "0123456789abcdef"; std::string parameter = "\\x"; for (const auto b : *bytes) { parameter.push_back(digits[b >> 4U]); parameter.push_back(digits[b & 0x0FU]); } const std::array p{parameter}; auto done = owner.query("SELECT evolution.provision_region_checkpoint($1::bytea)", p, timeout); if (!done) { const bool unknown = done.error().kind == PgError::unknown; return std::unexpected{PublicationFailure{ unknown ? PublicationError::unknown : PublicationError::rejected, {unknown ? LeaseStoreError::unknown : LeaseStoreError::store, done.error()}, {}}}; } auto committed = load_region_checkpoint(owner, key, timeout); if (!committed) return std::unexpected{ PublicationFailure{PublicationError::unknown, committed.error(), {}}}; return confirm(committed->head, std::move(*bytes), *manifest); } template std::expected publish(Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease, const CommittedCheckpoint& predecessor, const typename Codec::State& state, Uuid commit, const Sha256Digest& build, std::size_t budget, milliseconds timeout) { const auto previous = decode_manifest(predecessor.manifest); auto payload = Codec::snapshot(state); if (!previous || !payload || previous->profile != Codec::profile) return std::unexpected{fail(PublicationError::candidate)}; const auto digest = sha256(*payload); if (!digest) return std::unexpected{fail(PublicationError::digest)}; auto manifest = successor_manifest(*previous, predecessor.head.digest, commit, lease, Codec::tick(state), build, payload->size(), *digest); if (!manifest) return std::unexpected{fail(PublicationError::candidate)}; auto bytes = stage(objects, *payload, *manifest, budget); if (!bytes) return std::unexpected{bytes.error()}; auto published = publish_region_checkpoint(db, *bytes, timeout); return confirm(published, std::move(*bytes), *manifest); } template std::expected recover(Postgres& db, const FilesystemObjectStore& objects, RegionKey key, std::span retained_builds, std::size_t budget, milliseconds timeout) { auto committed = load_region_checkpoint(db, key, timeout); if (!committed) return std::unexpected{ RecoveryFailure{committed.error().kind == LeaseStoreError::not_provisioned ? RecoveryError::not_provisioned : RecoveryError::store, committed.error()}}; const auto fail_with = [](RecoveryError kind) { return std::unexpected{RecoveryFailure{kind, {}}}; }; const auto digest = sha256(committed->manifest); auto manifest = decode_manifest(committed->manifest); const auto& head = committed->head; if (!digest || *digest != head.digest) return fail_with(RecoveryError::manifest); if (!manifest) return fail_with(manifest.error() == CheckpointError::unsupported ? RecoveryError::unsupported : RecoveryError::manifest); if (manifest->key != key || manifest->frame != head.frame || manifest->sequence != head.sequence || manifest->commit != head.commit || manifest->end_tick != head.end_tick) return fail_with(RecoveryError::manifest); if (!supported(*manifest) || manifest->profile != Codec::profile || std::ranges::find(retained_builds, manifest->build) == retained_builds.end()) return fail_with(RecoveryError::unsupported); auto payload = objects.get({manifest->payload_sha256, manifest->payload_bytes}); if (!payload) return fail_with(payload.error() == ObjectError::missing ? RecoveryError::missing_payload : payload.error() == ObjectError::corrupt ? RecoveryError::corrupt_payload : RecoveryError::store); auto state = Codec::restore(*payload, *manifest, budget); if (!state) return fail_with(RecoveryError::payload); return typename Codec::Recovered{std::move(*committed), *manifest, std::move(*state)}; } } // namespace std::expected provision_environmental_region( Postgres& owner, const FilesystemObjectStore& objects, RegionKey key, std::uint64_t frame, const processes::EnvironmentalCommunity& opening, Uuid commit, const Sha256Digest& build, std::size_t restore_budget, milliseconds timeout) { return provision(owner, objects, key, frame, opening, commit, build, restore_budget, timeout); } std::expected publish_environmental_checkpoint( Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease, const CommittedCheckpoint& predecessor, const processes::EnvironmentalCommunity& state, Uuid commit, const Sha256Digest& build, std::size_t restore_budget, milliseconds timeout) { return publish(db, objects, lease, predecessor, state, commit, build, restore_budget, timeout); } std::expected recover_environmental_checkpoint(Postgres& db, const FilesystemObjectStore& objects, RegionKey key, std::span retained_builds, std::size_t restore_budget, milliseconds timeout) { return recover(db, objects, key, retained_builds, restore_budget, timeout); } std::expected provision_lineage_region(Postgres& owner, const FilesystemObjectStore& objects, RegionKey key, std::uint64_t frame, const processes::LineageRegion& opening, Uuid commit, const Sha256Digest& build, std::size_t restore_budget, milliseconds timeout) { return provision(owner, objects, key, frame, opening, commit, build, restore_budget, timeout); } std::expected publish_lineage_checkpoint( Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease, const CommittedCheckpoint& predecessor, const processes::LineageRegion& state, Uuid commit, const Sha256Digest& build, std::size_t restore_budget, milliseconds timeout) { return publish(db, objects, lease, predecessor, state, commit, build, restore_budget, timeout); } std::expected recover_lineage_checkpoint(Postgres& db, const FilesystemObjectStore& objects, RegionKey key, std::span retained_builds, std::size_t restore_budget, milliseconds timeout) { return recover(db, objects, key, retained_builds, restore_budget, timeout); } } // namespace evo::persistence