Implement M7.4a checkpoint publication and verified recovery
ADR-0061 publishes environmental-v1 full checkpoints. Manifest v1 is a fixed 280-byte record (identity, chain, lease, versions, build, zero RNG/history declarations, durability policy, payload length/SHA-256) that PostgreSQL parses and digests itself. Schema v3 adds region_commits with a digest CHECK and a composite head FK, owner-managed retained builds, fenced publish/load functions and owner-only genesis; it drops the bare v2 head kernel and fails closed while v2 heads exist. Linux evo::region_store adds OpenSSL SHA-256, an immutable content-addressed filesystem object store (temp+fsync+link, no overwrite/delete, O_NOFOLLOW exact-length verified reads), and publication (validate -> upload -> read back -> fenced transaction -> receipt check) and recovery (re-digest, allowlist, verify, full restore). Portable evo::region_checkpoint holds the codec and payload validation. Validated: Windows CTest 56/56; WSL Release, clang-tidy and ASan/UBSan private-PostgreSQL suites; four SQL and one host mutation each caught. M7.4 stays open for 7.4b (lineage archive codec and history binding). No Docker/Gitea CI, remote store, process kill or deployment. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
#include "evo/persistence/checkpoint_store.hpp"
|
||||
#include "evo/persistence/sha256.hpp"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <string>
|
||||
|
||||
namespace evo::persistence {
|
||||
namespace {
|
||||
using std::chrono::milliseconds;
|
||||
PublicationFailure fail(PublicationError kind) {
|
||||
return {kind, {}, {}};
|
||||
}
|
||||
// Encodes the candidate, validates the exact payload bytes as recovery would,
|
||||
// uploads and reads back. Returns the manifest bytes ready for the database.
|
||||
std::expected<std::vector<std::uint8_t>, PublicationFailure>
|
||||
stage(const FilesystemObjectStore& objects, const std::vector<std::uint8_t>& payload,
|
||||
const CheckpointManifest& manifest, std::size_t budget) {
|
||||
auto bytes = encode_manifest(manifest);
|
||||
if (!bytes || !restore_environmental_payload(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<CommittedCheckpoint, PublicationFailure>
|
||||
confirm(const std::expected<RegionHead, LeaseStoreFailure>& published,
|
||||
std::vector<std::uint8_t> 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)};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<CommittedCheckpoint, PublicationFailure> 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) {
|
||||
auto payload = opening.snapshot();
|
||||
if (!payload || opening.progress().tick != 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, opening.tick_microseconds(), build,
|
||||
payload->size(), *digest);
|
||||
if (!manifest)
|
||||
return std::unexpected{fail(PublicationError::candidate)};
|
||||
auto bytes = stage(objects, *payload, *manifest, restore_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<std::string, 1> p{parameter};
|
||||
auto done = owner.query("SELECT evolution.provision_region_checkpoint($1::bytea)", p, timeout);
|
||||
if (!done) {
|
||||
const auto kind = done.error().kind == PgError::unknown ? PublicationError::unknown
|
||||
: PublicationError::rejected;
|
||||
return std::unexpected{
|
||||
PublicationFailure{kind,
|
||||
{done.error().kind == PgError::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);
|
||||
}
|
||||
|
||||
std::expected<CommittedCheckpoint, PublicationFailure> 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) {
|
||||
const auto previous = decode_manifest(predecessor.manifest);
|
||||
auto payload = state.snapshot();
|
||||
if (!previous || !payload)
|
||||
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,
|
||||
state.progress().tick, build, payload->size(), *digest);
|
||||
if (!manifest)
|
||||
return std::unexpected{fail(PublicationError::candidate)};
|
||||
auto bytes = stage(objects, *payload, *manifest, restore_budget);
|
||||
if (!bytes)
|
||||
return std::unexpected{bytes.error()};
|
||||
auto published = publish_region_checkpoint(db, *bytes, timeout);
|
||||
return confirm(published, std::move(*bytes), *manifest);
|
||||
}
|
||||
|
||||
std::expected<RecoveredCheckpoint, RecoveryFailure>
|
||||
recover_environmental_checkpoint(Postgres& db, const FilesystemObjectStore& objects, RegionKey key,
|
||||
std::span<const Sha256Digest> retained_builds,
|
||||
std::size_t restore_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) ||
|
||||
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 = restore_environmental_payload(*payload, *manifest, restore_budget);
|
||||
if (!state)
|
||||
return fail_with(RecoveryError::payload);
|
||||
return RecoveredCheckpoint{std::move(*committed), *manifest, std::move(*state)};
|
||||
}
|
||||
} // namespace evo::persistence
|
||||
@@ -0,0 +1,200 @@
|
||||
#include "evo/persistence/object_store.hpp"
|
||||
#include "evo/persistence/sha256.hpp"
|
||||
#include <atomic>
|
||||
#include <cerrno>
|
||||
#include <fcntl.h>
|
||||
#include <string>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace evo::persistence {
|
||||
namespace {
|
||||
constexpr std::size_t maximum_object_limit = std::size_t{64} * 1024 * 1024;
|
||||
std::atomic<std::uint64_t> temporary_counter{0};
|
||||
|
||||
class Descriptor {
|
||||
public:
|
||||
explicit Descriptor(int fd) noexcept : fd_(fd) {}
|
||||
Descriptor(const Descriptor&) = delete;
|
||||
Descriptor& operator=(const Descriptor&) = delete;
|
||||
~Descriptor() {
|
||||
if (fd_ >= 0)
|
||||
static_cast<void>(::close(fd_));
|
||||
}
|
||||
[[nodiscard]] int get() const noexcept {
|
||||
return fd_;
|
||||
}
|
||||
[[nodiscard]] int release() noexcept {
|
||||
const int fd = fd_;
|
||||
fd_ = -1;
|
||||
return fd;
|
||||
}
|
||||
// Explicit close so a deferred write error is observed.
|
||||
[[nodiscard]] bool close() noexcept {
|
||||
const int fd = release();
|
||||
return fd < 0 || ::close(fd) == 0;
|
||||
}
|
||||
|
||||
private:
|
||||
int fd_;
|
||||
};
|
||||
std::string key_name(const Sha256Digest& d) {
|
||||
constexpr char digits[] = "0123456789abcdef";
|
||||
std::string out;
|
||||
out.reserve(64);
|
||||
for (const auto b : d) {
|
||||
out.push_back(digits[b >> 4U]);
|
||||
out.push_back(digits[b & 0x0FU]);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
// Opens or creates a private subdirectory without following symlinks.
|
||||
int subdirectory(int parent, const std::string& name) {
|
||||
bool created = false;
|
||||
if (::mkdirat(parent, name.c_str(), 0700) == 0)
|
||||
created = true;
|
||||
else if (errno != EEXIST)
|
||||
return -1;
|
||||
const int fd = ::openat(parent, name.c_str(), O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
|
||||
if (fd >= 0 && created && ::fsync(parent) != 0) {
|
||||
static_cast<void>(::close(fd));
|
||||
return -1;
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
bool write_all(int fd, std::span<const std::uint8_t> bytes) {
|
||||
while (!bytes.empty()) {
|
||||
const auto n = ::write(fd, bytes.data(), bytes.size());
|
||||
if (n < 0 && errno == EINTR)
|
||||
continue;
|
||||
if (n <= 0)
|
||||
return false;
|
||||
bytes = bytes.subspan(static_cast<std::size_t>(n));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// Exact-length regular file whose content has the expected digest.
|
||||
std::expected<std::vector<std::uint8_t>, ObjectError> read_verified(int directory,
|
||||
const VerifiedObject& object) {
|
||||
const auto name = key_name(object.sha256);
|
||||
const Descriptor fd{::openat(directory, name.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC)};
|
||||
if (fd.get() < 0)
|
||||
return std::unexpected{errno == ENOENT ? ObjectError::missing
|
||||
: errno == ELOOP ? ObjectError::corrupt
|
||||
: ObjectError::io};
|
||||
struct stat info{};
|
||||
if (::fstat(fd.get(), &info) != 0)
|
||||
return std::unexpected{ObjectError::io};
|
||||
// Size is checked before any allocation.
|
||||
if (!S_ISREG(info.st_mode) || info.st_size < 0 ||
|
||||
static_cast<std::uint64_t>(info.st_size) != object.bytes)
|
||||
return std::unexpected{ObjectError::corrupt};
|
||||
std::vector<std::uint8_t> bytes(static_cast<std::size_t>(object.bytes));
|
||||
std::size_t done = 0;
|
||||
while (done < bytes.size()) {
|
||||
const auto n = ::read(fd.get(), bytes.data() + done, bytes.size() - done);
|
||||
if (n < 0 && errno == EINTR)
|
||||
continue;
|
||||
if (n < 0)
|
||||
return std::unexpected{ObjectError::io};
|
||||
if (n == 0)
|
||||
return std::unexpected{ObjectError::corrupt}; // Shrunk after fstat.
|
||||
done += static_cast<std::size_t>(n);
|
||||
}
|
||||
std::uint8_t extra{};
|
||||
if (::read(fd.get(), &extra, 1) != 0)
|
||||
return std::unexpected{ObjectError::corrupt}; // Grew after fstat.
|
||||
const auto digest = sha256(bytes);
|
||||
if (!digest)
|
||||
return std::unexpected{ObjectError::io};
|
||||
if (*digest != object.sha256)
|
||||
return std::unexpected{ObjectError::corrupt};
|
||||
return bytes;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
FilesystemObjectStore::FilesystemObjectStore(std::filesystem::path directory, int descriptor,
|
||||
std::size_t maximum)
|
||||
: directory_(std::move(directory)), descriptor_(descriptor), maximum_(maximum) {}
|
||||
|
||||
FilesystemObjectStore::~FilesystemObjectStore() {
|
||||
static_cast<void>(::close(descriptor_));
|
||||
}
|
||||
|
||||
std::expected<std::unique_ptr<FilesystemObjectStore>, ObjectError>
|
||||
FilesystemObjectStore::open(const std::filesystem::path& trusted_root, RegionKey key,
|
||||
std::size_t max_object_bytes) {
|
||||
if (key.world == 0 || key.region == 0 || max_object_bytes == 0 ||
|
||||
max_object_bytes > maximum_object_limit || trusted_root.empty())
|
||||
return std::unexpected{ObjectError::invalid};
|
||||
const Descriptor root{
|
||||
::open(trusted_root.c_str(), O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC)};
|
||||
if (root.get() < 0)
|
||||
return std::unexpected{ObjectError::invalid};
|
||||
const auto world = "w" + std::to_string(key.world);
|
||||
const auto region = "r" + std::to_string(key.region);
|
||||
const Descriptor world_fd{subdirectory(root.get(), world)};
|
||||
if (world_fd.get() < 0)
|
||||
return std::unexpected{ObjectError::io};
|
||||
Descriptor region_fd{subdirectory(world_fd.get(), region)};
|
||||
if (region_fd.get() < 0)
|
||||
return std::unexpected{ObjectError::io};
|
||||
return std::unique_ptr<FilesystemObjectStore>(new FilesystemObjectStore(
|
||||
trusted_root / world / region, region_fd.release(), max_object_bytes));
|
||||
}
|
||||
|
||||
std::expected<VerifiedObject, ObjectError>
|
||||
FilesystemObjectStore::put(std::span<const std::uint8_t> bytes) const {
|
||||
if (bytes.empty())
|
||||
return std::unexpected{ObjectError::invalid};
|
||||
if (bytes.size() > maximum_)
|
||||
return std::unexpected{ObjectError::bounds};
|
||||
const auto digest = sha256(bytes);
|
||||
if (!digest)
|
||||
return std::unexpected{ObjectError::io};
|
||||
const VerifiedObject object{*digest, bytes.size()};
|
||||
const auto existing = [&]() -> std::expected<VerifiedObject, ObjectError> {
|
||||
auto stored = read_verified(descriptor_, object);
|
||||
if (!stored)
|
||||
return std::unexpected{stored.error()};
|
||||
return object;
|
||||
};
|
||||
// Immutable: identical content is reused; anything else under the key is corrupt.
|
||||
if (auto found = existing(); found || found.error() != ObjectError::missing)
|
||||
return found;
|
||||
|
||||
const auto name = key_name(*digest);
|
||||
const auto temporary = ".tmp-" + name + "-" + std::to_string(::getpid()) + "-" +
|
||||
std::to_string(temporary_counter.fetch_add(1));
|
||||
Descriptor fd{::openat(descriptor_, temporary.c_str(),
|
||||
O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0600)};
|
||||
if (fd.get() < 0)
|
||||
return std::unexpected{ObjectError::io};
|
||||
const auto discard = [&] { static_cast<void>(::unlinkat(descriptor_, temporary.c_str(), 0)); };
|
||||
if (!write_all(fd.get(), bytes) || ::fsync(fd.get()) != 0 || !fd.close()) {
|
||||
discard();
|
||||
return std::unexpected{ObjectError::io};
|
||||
}
|
||||
// link() never replaces an existing name, unlike rename().
|
||||
if (::linkat(descriptor_, temporary.c_str(), descriptor_, name.c_str(), 0) != 0) {
|
||||
const bool raced = errno == EEXIST;
|
||||
discard();
|
||||
if (raced)
|
||||
return existing();
|
||||
return std::unexpected{ObjectError::io};
|
||||
}
|
||||
discard();
|
||||
if (::fsync(descriptor_) != 0)
|
||||
return std::unexpected{ObjectError::io};
|
||||
return object;
|
||||
}
|
||||
|
||||
std::expected<std::vector<std::uint8_t>, ObjectError>
|
||||
FilesystemObjectStore::get(const VerifiedObject& object) const {
|
||||
if (object.bytes == 0)
|
||||
return std::unexpected{ObjectError::invalid};
|
||||
if (object.bytes > maximum_)
|
||||
return std::unexpected{ObjectError::bounds};
|
||||
return read_verified(descriptor_, object);
|
||||
}
|
||||
} // namespace evo::persistence
|
||||
@@ -0,0 +1,220 @@
|
||||
#include "evo/persistence/region_checkpoint.hpp"
|
||||
#include <algorithm>
|
||||
#include <ranges>
|
||||
|
||||
namespace evo::persistence {
|
||||
namespace {
|
||||
constexpr std::uint64_t manifest_magic = 0x314D43524F564541ULL; // "AEVORCM1"
|
||||
constexpr std::uint32_t manifest_version = 1;
|
||||
constexpr std::uint64_t maximum = ~std::uint64_t{0};
|
||||
|
||||
template <std::size_t N> bool zero(const std::array<std::uint8_t, N>& bytes) {
|
||||
return std::ranges::all_of(bytes, [](std::uint8_t b) { return b == 0; });
|
||||
}
|
||||
class Writer {
|
||||
public:
|
||||
explicit Writer(std::vector<std::uint8_t>& out) : out_(out) {}
|
||||
void u(std::uint64_t value, unsigned width) {
|
||||
for (unsigned i = 0; i < width; ++i) {
|
||||
out_.push_back(static_cast<std::uint8_t>(value & 0xFFU));
|
||||
value >>= 8U;
|
||||
}
|
||||
}
|
||||
template <std::size_t N> void bytes(const std::array<std::uint8_t, N>& value) {
|
||||
out_.insert(out_.end(), value.begin(), value.end());
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::uint8_t>& out_;
|
||||
};
|
||||
class Reader {
|
||||
public:
|
||||
explicit Reader(std::span<const std::uint8_t> in) : in_(in) {}
|
||||
std::uint64_t u(unsigned width) {
|
||||
std::uint64_t value{};
|
||||
for (unsigned i = 0; i < width; ++i)
|
||||
value |= static_cast<std::uint64_t>(in_[at_ + i]) << (8U * i);
|
||||
at_ += width;
|
||||
return value;
|
||||
}
|
||||
template <std::size_t N> std::array<std::uint8_t, N> bytes() {
|
||||
std::array<std::uint8_t, N> value{};
|
||||
std::copy_n(in_.begin() + static_cast<std::ptrdiff_t>(at_), N, value.begin());
|
||||
at_ += N;
|
||||
return value;
|
||||
}
|
||||
|
||||
private:
|
||||
std::span<const std::uint8_t> in_;
|
||||
std::size_t at_{};
|
||||
};
|
||||
|
||||
// Every field, independent of which versions this build supports.
|
||||
bool structural(const CheckpointManifest& m) noexcept {
|
||||
if (m.key.world == 0 || m.key.region == 0 || m.frame == 0 || zero(m.commit) ||
|
||||
m.tick_microseconds == 0 || zero(m.build) || m.payload_bytes == 0 ||
|
||||
m.payload_bytes > max_environmental_payload_bytes || zero(m.payload_sha256))
|
||||
return false;
|
||||
if (m.sequence == 0)
|
||||
return zero(m.predecessor_commit) && zero(m.predecessor_digest) && m.begin_tick == 0 &&
|
||||
m.end_tick == 0 && zero(m.holder) && m.epoch == 0;
|
||||
return !zero(m.predecessor_commit) && m.predecessor_commit != m.commit &&
|
||||
!zero(m.predecessor_digest) && !zero(m.holder) && m.epoch != 0 &&
|
||||
m.end_tick > m.begin_tick && m.end_tick - m.begin_tick <= max_checkpoint_ticks;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
bool supported(const CheckpointManifest& m) noexcept {
|
||||
return m.profile == CheckpointProfile::environmental_v1 &&
|
||||
m.versions == environmental_v1_versions && m.rng_streams == 0 &&
|
||||
m.history_profile == 0 && m.durability == DurabilityPolicy::single_volume_development;
|
||||
}
|
||||
|
||||
std::expected<std::vector<std::uint8_t>, CheckpointError>
|
||||
encode_manifest(const CheckpointManifest& m) {
|
||||
if (!structural(m))
|
||||
return std::unexpected{CheckpointError::format};
|
||||
std::vector<std::uint8_t> out;
|
||||
out.reserve(checkpoint_manifest_bytes);
|
||||
Writer w{out};
|
||||
w.u(manifest_magic, 8);
|
||||
w.u(manifest_version, 4);
|
||||
w.u(static_cast<std::uint8_t>(m.profile), 4);
|
||||
w.u(m.key.world, 8);
|
||||
w.u(m.key.region, 8);
|
||||
w.u(m.frame, 8);
|
||||
w.bytes(m.commit);
|
||||
w.u(m.sequence, 8);
|
||||
w.bytes(m.predecessor_commit);
|
||||
w.bytes(m.predecessor_digest);
|
||||
w.u(m.begin_tick, 8);
|
||||
w.u(m.end_tick, 8);
|
||||
w.bytes(m.holder);
|
||||
w.u(m.epoch, 8);
|
||||
w.u(m.tick_microseconds, 8);
|
||||
for (const auto v : {m.versions.rules, m.versions.envelope, m.versions.resources,
|
||||
m.versions.fields, m.versions.terrain, m.versions.coordinate,
|
||||
m.versions.generator, m.versions.material, m.versions.interval_adapter})
|
||||
w.u(v, 4);
|
||||
w.bytes(m.build);
|
||||
w.u(m.rng_streams, 4);
|
||||
w.u(m.history_profile, 4);
|
||||
w.u(static_cast<std::uint8_t>(m.durability), 4);
|
||||
w.u(m.payload_bytes, 8);
|
||||
w.bytes(m.payload_sha256);
|
||||
return out;
|
||||
}
|
||||
|
||||
std::expected<CheckpointManifest, CheckpointError>
|
||||
decode_manifest(std::span<const std::uint8_t> bytes) {
|
||||
if (bytes.size() != checkpoint_manifest_bytes)
|
||||
return std::unexpected{CheckpointError::format};
|
||||
Reader r{bytes};
|
||||
if (r.u(8) != manifest_magic)
|
||||
return std::unexpected{CheckpointError::format};
|
||||
if (r.u(4) != manifest_version)
|
||||
return std::unexpected{CheckpointError::unsupported};
|
||||
CheckpointManifest m;
|
||||
const auto profile = r.u(4);
|
||||
m.profile =
|
||||
profile <= 0xFFU ? static_cast<CheckpointProfile>(profile) : CheckpointProfile::unknown;
|
||||
m.key.world = r.u(8);
|
||||
m.key.region = r.u(8);
|
||||
m.frame = r.u(8);
|
||||
m.commit = r.bytes<16>();
|
||||
m.sequence = r.u(8);
|
||||
m.predecessor_commit = r.bytes<16>();
|
||||
m.predecessor_digest = r.bytes<32>();
|
||||
m.begin_tick = r.u(8);
|
||||
m.end_tick = r.u(8);
|
||||
m.holder = r.bytes<16>();
|
||||
m.epoch = r.u(8);
|
||||
m.tick_microseconds = r.u(8);
|
||||
for (auto* v : {&m.versions.rules, &m.versions.envelope, &m.versions.resources,
|
||||
&m.versions.fields, &m.versions.terrain, &m.versions.coordinate,
|
||||
&m.versions.generator, &m.versions.material, &m.versions.interval_adapter})
|
||||
*v = static_cast<std::uint32_t>(r.u(4));
|
||||
m.build = r.bytes<32>();
|
||||
m.rng_streams = static_cast<std::uint32_t>(r.u(4));
|
||||
m.history_profile = static_cast<std::uint32_t>(r.u(4));
|
||||
const auto durability = r.u(4);
|
||||
m.durability =
|
||||
durability <= 0xFFU ? static_cast<DurabilityPolicy>(durability) : DurabilityPolicy::unknown;
|
||||
m.payload_bytes = r.u(8);
|
||||
m.payload_sha256 = r.bytes<32>();
|
||||
if (!structural(m))
|
||||
return std::unexpected{CheckpointError::format};
|
||||
return m;
|
||||
}
|
||||
|
||||
std::expected<CheckpointManifest, CheckpointError>
|
||||
genesis_manifest(RegionKey key, std::uint64_t frame, Uuid commit, std::uint64_t tick_microseconds,
|
||||
const Sha256Digest& build, std::uint64_t payload_bytes,
|
||||
const Sha256Digest& payload_sha256) {
|
||||
CheckpointManifest m;
|
||||
m.key = key;
|
||||
m.frame = frame;
|
||||
m.commit = commit;
|
||||
m.tick_microseconds = tick_microseconds;
|
||||
m.build = build;
|
||||
m.payload_bytes = payload_bytes;
|
||||
m.payload_sha256 = payload_sha256;
|
||||
if (!structural(m))
|
||||
return std::unexpected{CheckpointError::format};
|
||||
return m;
|
||||
}
|
||||
|
||||
std::expected<CheckpointManifest, CheckpointError>
|
||||
successor_manifest(const CheckpointManifest& p, const Sha256Digest& predecessor_digest, Uuid commit,
|
||||
const LeaseGrant& lease, std::uint64_t end_tick, const Sha256Digest& build,
|
||||
std::uint64_t payload_bytes, const Sha256Digest& payload_sha256) {
|
||||
// This build writes only its own versions; a change requires a migration (M7.9).
|
||||
if (!supported(p))
|
||||
return std::unexpected{CheckpointError::unsupported};
|
||||
if (!structural(p) || p.sequence == maximum || lease.frame != p.frame ||
|
||||
end_tick <= p.end_tick || end_tick - p.end_tick > max_checkpoint_ticks)
|
||||
return std::unexpected{CheckpointError::chain};
|
||||
CheckpointManifest m = p;
|
||||
m.commit = commit;
|
||||
m.sequence = p.sequence + 1;
|
||||
m.predecessor_commit = p.commit;
|
||||
m.predecessor_digest = predecessor_digest;
|
||||
m.begin_tick = p.end_tick;
|
||||
m.end_tick = end_tick;
|
||||
m.holder = lease.holder;
|
||||
m.epoch = lease.epoch;
|
||||
m.build = build;
|
||||
m.payload_bytes = payload_bytes;
|
||||
m.payload_sha256 = payload_sha256;
|
||||
if (!structural(m))
|
||||
return std::unexpected{CheckpointError::format};
|
||||
return m;
|
||||
}
|
||||
|
||||
std::expected<std::unique_ptr<processes::EnvironmentalCommunity>, CheckpointError>
|
||||
restore_environmental_payload(std::span<const std::uint8_t> payload, const CheckpointManifest& m,
|
||||
std::size_t budget) {
|
||||
if (!structural(m))
|
||||
return std::unexpected{CheckpointError::format};
|
||||
if (!supported(m))
|
||||
return std::unexpected{CheckpointError::unsupported};
|
||||
if (payload.size() != m.payload_bytes || payload.size() < 16)
|
||||
return std::unexpected{CheckpointError::payload};
|
||||
Reader header{payload};
|
||||
header.u(8); // Envelope magic is checked by the envelope decoder itself.
|
||||
if (header.u(8) != m.versions.envelope)
|
||||
return std::unexpected{CheckpointError::unsupported};
|
||||
auto restored =
|
||||
processes::EnvironmentalCommunity::restore(payload, {m.key.world, m.frame}, budget);
|
||||
if (!restored)
|
||||
return std::unexpected{CheckpointError::payload};
|
||||
const auto& state = **restored;
|
||||
if (state.progress().tick != m.end_tick || state.tick_microseconds() != m.tick_microseconds)
|
||||
return std::unexpected{CheckpointError::payload};
|
||||
// Canonical: the restored state re-encodes to exactly the committed bytes.
|
||||
const auto again = state.snapshot();
|
||||
if (!again || !std::ranges::equal(*again, payload))
|
||||
return std::unexpected{CheckpointError::payload};
|
||||
return std::move(*restored);
|
||||
}
|
||||
} // namespace evo::persistence
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <array>
|
||||
#include <charconv>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
@@ -27,15 +28,19 @@ LeaseStoreFailure classify(const PgFailure& f) {
|
||||
kind = LeaseStoreError::not_provisioned;
|
||||
else if (state == "EVL05")
|
||||
kind = LeaseStoreError::exhausted;
|
||||
else if (state == "EVL06")
|
||||
kind = LeaseStoreError::unsupported;
|
||||
else if (state == "EVL07")
|
||||
kind = LeaseStoreError::commit_conflict;
|
||||
return {kind, f};
|
||||
}
|
||||
LeaseStoreFailure malformed(bool mutation) {
|
||||
// A mutation whose reply cannot be parsed may still have committed.
|
||||
return {mutation ? LeaseStoreError::unknown : LeaseStoreError::store, {}};
|
||||
}
|
||||
template <std::size_t N> std::string hex(const std::array<std::uint8_t, N>& bytes) {
|
||||
std::string hex(std::span<const std::uint8_t> bytes) {
|
||||
std::string out;
|
||||
out.reserve(N * 2);
|
||||
out.reserve(bytes.size() * 2);
|
||||
for (const auto b : bytes) {
|
||||
out.push_back(digits[static_cast<std::size_t>(b >> 4U)]);
|
||||
out.push_back(digits[static_cast<std::size_t>(b & 0x0FU)]);
|
||||
@@ -61,6 +66,19 @@ template <std::size_t N> std::optional<std::array<std::uint8_t, N>> unhex(std::s
|
||||
}
|
||||
return out;
|
||||
}
|
||||
std::optional<std::vector<std::uint8_t>> unhex_bytes(std::string_view s) {
|
||||
if (s.size() % 2 != 0)
|
||||
return std::nullopt;
|
||||
std::vector<std::uint8_t> out(s.size() / 2);
|
||||
for (std::size_t i = 0; i < out.size(); ++i) {
|
||||
const auto high = nibble(s[i * 2]);
|
||||
const auto low = nibble(s[(i * 2) + 1]);
|
||||
if (!high || !low)
|
||||
return std::nullopt;
|
||||
out[i] = static_cast<std::uint8_t>((*high << 4U) | *low);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
std::string uuid_text(const Uuid& id) {
|
||||
const auto h = hex(id);
|
||||
return h.substr(0, 8) + '-' + h.substr(8, 4) + '-' + h.substr(12, 4) + '-' + h.substr(16, 4) +
|
||||
@@ -89,11 +107,12 @@ template <typename T> std::optional<T> parse_integer(const std::optional<std::st
|
||||
return std::nullopt;
|
||||
return value;
|
||||
}
|
||||
std::string bytea(const Sha256Digest& d) {
|
||||
return "\\x" + hex(d);
|
||||
std::string bytea(std::span<const std::uint8_t> b) {
|
||||
return "\\x" + hex(b);
|
||||
}
|
||||
std::optional<RegionHead> parse_head(const PgRows& rows) {
|
||||
if (rows.size() != 1 || rows[0].size() != 5)
|
||||
// Head columns: frame, sequence, commit, digest hex, end tick (+ optional extras).
|
||||
std::optional<RegionHead> parse_head(const PgRows& rows, std::size_t columns = 5) {
|
||||
if (rows.size() != 1 || rows[0].size() != columns)
|
||||
return std::nullopt;
|
||||
const auto& r = rows[0];
|
||||
const auto frame = parse_integer<std::uint64_t>(r[0]);
|
||||
@@ -177,23 +196,28 @@ std::expected<RegionHead, LeaseStoreFailure> load_region_head(Postgres& db, Regi
|
||||
return *head;
|
||||
}
|
||||
|
||||
std::expected<CommittedCheckpoint, LeaseStoreFailure>
|
||||
load_region_checkpoint(Postgres& db, RegionKey key, milliseconds timeout) {
|
||||
const std::array<std::string, 2> p{std::to_string(key.world), std::to_string(key.region)};
|
||||
auto rows = db.query(
|
||||
"SELECT * FROM evolution.load_region_checkpoint($1::evolution.u64, $2::evolution.u64)", p,
|
||||
timeout);
|
||||
if (!rows)
|
||||
return std::unexpected{classify(rows.error())};
|
||||
auto head = parse_head(*rows, 6);
|
||||
const auto& manifest_text = head ? (*rows)[0][5] : std::optional<std::string>{};
|
||||
auto manifest = manifest_text ? unhex_bytes(*manifest_text) : std::nullopt;
|
||||
if (!head || !manifest)
|
||||
return std::unexpected{malformed(false)};
|
||||
return CommittedCheckpoint{*head, std::move(*manifest)};
|
||||
}
|
||||
|
||||
std::expected<RegionHead, LeaseStoreFailure>
|
||||
advance_region_head(Postgres& db, RegionKey key, const LeaseGrant& lease,
|
||||
const RegionHead& expected, const HeadAdvance& next, milliseconds timeout) {
|
||||
const std::array<std::string, 10> p{std::to_string(key.world),
|
||||
std::to_string(key.region),
|
||||
uuid_text(lease.holder),
|
||||
std::to_string(lease.epoch),
|
||||
std::to_string(expected.sequence),
|
||||
uuid_text(expected.commit),
|
||||
bytea(expected.digest),
|
||||
uuid_text(next.commit),
|
||||
bytea(next.digest),
|
||||
std::to_string(next.end_tick)};
|
||||
auto rows = db.query("SELECT * FROM evolution.advance_region_head($1::evolution.u64, "
|
||||
"$2::evolution.u64, $3::uuid, $4::evolution.u64, $5::evolution.u64, "
|
||||
"$6::uuid, $7::bytea, $8::uuid, $9::bytea, $10::evolution.u64)",
|
||||
p, timeout);
|
||||
publish_region_checkpoint(Postgres& db, std::span<const std::uint8_t> manifest,
|
||||
milliseconds timeout) {
|
||||
const std::array<std::string, 1> p{bytea(manifest)};
|
||||
auto rows =
|
||||
db.query("SELECT * FROM evolution.publish_region_checkpoint($1::bytea)", p, timeout);
|
||||
if (!rows)
|
||||
return std::unexpected{classify(rows.error())};
|
||||
auto head = parse_head(*rows);
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
#include "evo/persistence/sha256.hpp"
|
||||
#include <openssl/evp.h>
|
||||
|
||||
namespace evo::persistence {
|
||||
std::expected<Sha256Digest, DigestError> sha256(std::span<const std::uint8_t> bytes) noexcept {
|
||||
Sha256Digest out{};
|
||||
unsigned int length = 0;
|
||||
if (EVP_Digest(bytes.data(), bytes.size(), out.data(), &length, EVP_sha256(), nullptr) != 1 ||
|
||||
length != out.size())
|
||||
return std::unexpected{DigestError::provider};
|
||||
return out;
|
||||
}
|
||||
} // namespace evo::persistence
|
||||
Reference in New Issue
Block a user