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>
200 lines
8.9 KiB
C++
200 lines
8.9 KiB
C++
#include "evo/persistence/region_checkpoint.hpp"
|
|
#include "fixtures/environmental_region.hpp"
|
|
|
|
#include <cstdio>
|
|
#include <source_location>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <string_view>
|
|
|
|
namespace {
|
|
using namespace evo::persistence;
|
|
using evo::test::advance_region;
|
|
using evo::test::region_community;
|
|
using evo::test::region_input;
|
|
void check(bool ok, std::source_location at = std::source_location::current()) {
|
|
if (!ok)
|
|
throw std::runtime_error("region checkpoint line " + std::to_string(at.line()));
|
|
}
|
|
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
|
|
template <std::size_t N> std::array<std::uint8_t, N> filled(std::uint8_t value) {
|
|
std::array<std::uint8_t, N> out{};
|
|
out.fill(value);
|
|
return out;
|
|
}
|
|
constexpr RegionKey key{7, 3};
|
|
const auto build = filled<32>(0xB1);
|
|
LeaseGrant lease() {
|
|
return {filled<16>(0x11), 4, 9, std::chrono::milliseconds{20000}};
|
|
}
|
|
CheckpointManifest genesis(std::uint64_t payload_bytes = 100) {
|
|
auto g =
|
|
genesis_manifest(key, 9, filled<16>(0x01), 50000, build, payload_bytes, filled<32>(0xA0));
|
|
CHECK(g);
|
|
return *g;
|
|
}
|
|
CheckpointManifest next(std::uint64_t end_tick = 16) {
|
|
auto n = successor_manifest(genesis(), filled<32>(0xD0), filled<16>(0x02), lease(), end_tick,
|
|
build, 200, filled<32>(0xA1));
|
|
CHECK(n);
|
|
return *n;
|
|
}
|
|
std::uint64_t at(const std::vector<std::uint8_t>& b, std::size_t offset, unsigned width) {
|
|
std::uint64_t value{};
|
|
for (unsigned i = 0; i < width; ++i)
|
|
value |= static_cast<std::uint64_t>(b[offset + i]) << (8U * i);
|
|
return value;
|
|
}
|
|
|
|
void layout_and_round_trip() {
|
|
const auto m = next();
|
|
auto bytes = encode_manifest(m);
|
|
CHECK(bytes && bytes->size() == checkpoint_manifest_bytes);
|
|
// Golden offsets from schemas/region-commit.md.
|
|
CHECK(std::string_view(reinterpret_cast<const char*>(bytes->data()), 8) == "AEVORCM1");
|
|
CHECK(at(*bytes, 8, 4) == 1 && at(*bytes, 12, 4) == 1);
|
|
CHECK(at(*bytes, 16, 8) == 7 && at(*bytes, 24, 8) == 3 && at(*bytes, 32, 8) == 9);
|
|
CHECK((*bytes)[40] == 0x02 && at(*bytes, 56, 8) == 1 && (*bytes)[64] == 0x01);
|
|
CHECK((*bytes)[80] == 0xD0 && at(*bytes, 112, 8) == 0 && at(*bytes, 120, 8) == 16);
|
|
CHECK((*bytes)[128] == 0x11 && at(*bytes, 144, 8) == 4 && at(*bytes, 152, 8) == 50000);
|
|
CHECK(at(*bytes, 160, 4) == 2 && at(*bytes, 164, 4) == 2 && at(*bytes, 192, 4) == 1);
|
|
CHECK((*bytes)[196] == 0xB1 && at(*bytes, 228, 4) == 0 && at(*bytes, 232, 4) == 0);
|
|
CHECK(at(*bytes, 236, 4) == 1 && at(*bytes, 240, 8) == 200 && (*bytes)[248] == 0xA1);
|
|
auto decoded = decode_manifest(*bytes);
|
|
CHECK(decoded && *decoded == m && supported(*decoded));
|
|
auto g = encode_manifest(genesis());
|
|
CHECK(g && decode_manifest(*g) == genesis());
|
|
}
|
|
|
|
void malformed_and_unsupported() {
|
|
auto bytes = *encode_manifest(next());
|
|
CHECK(decode_manifest(std::span{bytes}.first(279)).error() == CheckpointError::format);
|
|
auto grown = bytes;
|
|
grown.push_back(0);
|
|
CHECK(decode_manifest(grown).error() == CheckpointError::format);
|
|
auto magic = bytes;
|
|
magic[0] ^= 1U;
|
|
CHECK(decode_manifest(magic).error() == CheckpointError::format);
|
|
auto version = bytes;
|
|
version[8] = 2;
|
|
CHECK(decode_manifest(version).error() == CheckpointError::unsupported);
|
|
// Structurally invalid: zero world, 65-tick batch, oversized payload, zero digest.
|
|
for (const auto& [offset, value] :
|
|
{std::pair{16, 0}, std::pair{120, 65}, std::pair{242, 0x11}, std::pair{144, 0}}) {
|
|
auto b = bytes;
|
|
b[static_cast<std::size_t>(offset)] = static_cast<std::uint8_t>(value);
|
|
if (offset == 144)
|
|
std::fill_n(b.begin() + 144, 8, std::uint8_t{0}); // Epoch zero off genesis.
|
|
CHECK(decode_manifest(b).error() == CheckpointError::format);
|
|
}
|
|
auto sha = bytes;
|
|
std::fill_n(sha.begin() + 248, 32, std::uint8_t{0});
|
|
CHECK(decode_manifest(sha).error() == CheckpointError::format);
|
|
// Parsable but not admitted by this build: decode succeeds, supported() refuses.
|
|
// 13/237 make profile/durability 257: never truncated onto a supported value.
|
|
for (const std::size_t offset : {12U, 13U, 160U, 164U, 192U, 228U, 232U, 236U, 237U}) {
|
|
auto b = bytes;
|
|
b[offset] = static_cast<std::uint8_t>(b[offset] + 1);
|
|
auto d = decode_manifest(b);
|
|
CHECK(d && !supported(*d));
|
|
}
|
|
auto g = genesis();
|
|
g.holder = filled<16>(1);
|
|
CHECK(encode_manifest(g).error() == CheckpointError::format);
|
|
g = genesis();
|
|
g.end_tick = 1;
|
|
CHECK(encode_manifest(g).error() == CheckpointError::format);
|
|
CHECK(
|
|
!genesis_manifest(key, 9, filled<16>(1), 50000, build, 1024 * 1024 + 1, filled<32>(0xA0)));
|
|
CHECK(!genesis_manifest(key, 0, filled<16>(1), 50000, build, 1, filled<32>(0xA0)));
|
|
}
|
|
|
|
void successor_chain() {
|
|
const auto g = genesis();
|
|
const auto d = filled<32>(0xD0);
|
|
const auto payload = filled<32>(0xA1);
|
|
CHECK(successor_manifest(g, d, filled<16>(2), lease(), 0, build, 1, payload).error() ==
|
|
CheckpointError::chain);
|
|
CHECK(successor_manifest(g, d, filled<16>(2), lease(), 65, build, 1, payload).error() ==
|
|
CheckpointError::chain);
|
|
auto foreign = lease();
|
|
foreign.frame = 8;
|
|
CHECK(successor_manifest(g, d, filled<16>(2), foreign, 1, build, 1, payload).error() ==
|
|
CheckpointError::chain);
|
|
CHECK(successor_manifest(g, d, g.commit, lease(), 1, build, 1, payload).error() ==
|
|
CheckpointError::format); // Reused commit identity.
|
|
auto old = g;
|
|
old.versions.rules = 1;
|
|
CHECK(successor_manifest(old, d, filled<16>(2), lease(), 1, build, 1, payload).error() ==
|
|
CheckpointError::unsupported);
|
|
auto last = next();
|
|
last.sequence = ~std::uint64_t{0};
|
|
CHECK(successor_manifest(last, d, filled<16>(3), lease(), 17, build, 1, payload).error() ==
|
|
CheckpointError::chain);
|
|
const auto n = next(64);
|
|
CHECK(n.sequence == 1 && n.predecessor_commit == g.commit && n.predecessor_digest == d);
|
|
CHECK(n.begin_tick == 0 && n.holder == lease().holder && n.epoch == 4 && n.frame == 9);
|
|
}
|
|
|
|
void payload_validation_and_restored_trajectory() {
|
|
const RegionKey region{evo::test::region_identity.world, 3};
|
|
auto original = region_community();
|
|
auto opening = original->snapshot();
|
|
CHECK(opening);
|
|
auto g = genesis_manifest(region, evo::test::region_identity.frame, filled<16>(1), 50000, build,
|
|
opening->size(), filled<32>(0xA0));
|
|
CHECK(g && restore_environmental_payload(*opening, *g, 8000000));
|
|
|
|
advance_region(*original, 16);
|
|
auto payload = original->snapshot();
|
|
CHECK(payload);
|
|
auto lease_at_frame = lease();
|
|
lease_at_frame.frame = evo::test::region_identity.frame;
|
|
auto m = successor_manifest(*g, filled<32>(0xD0), filled<16>(2), lease_at_frame, 16, build,
|
|
payload->size(), filled<32>(0xA1));
|
|
CHECK(m);
|
|
auto restored = restore_environmental_payload(*payload, *m, 8000000);
|
|
CHECK(restored);
|
|
// The restored checkpoint continues exactly like the uninterrupted original.
|
|
for (std::uint64_t tick = 17; tick <= 40; ++tick) {
|
|
auto a = original->advance(tick, region_input(tick, true));
|
|
auto b = (*restored)->advance(tick, region_input(tick, true));
|
|
CHECK(a && b && *a == *b);
|
|
}
|
|
CHECK(original->snapshot() == (*restored)->snapshot());
|
|
|
|
const auto refused = [&](auto change, CheckpointError error) {
|
|
auto bytes = *payload;
|
|
auto manifest = *m;
|
|
change(bytes, manifest);
|
|
auto r = restore_environmental_payload(bytes, manifest, 8000000);
|
|
return !r && r.error() == error;
|
|
};
|
|
CHECK(refused([](auto&, auto& x) { x.end_tick = 15; }, CheckpointError::payload));
|
|
CHECK(refused([](auto&, auto& x) { x.key.world = 8; }, CheckpointError::payload));
|
|
CHECK(refused([](auto&, auto& x) { x.frame = 10; }, CheckpointError::payload));
|
|
CHECK(refused([](auto&, auto& x) { x.tick_microseconds = 25000; }, CheckpointError::payload));
|
|
CHECK(refused([](auto&, auto& x) { --x.payload_bytes; }, CheckpointError::payload));
|
|
CHECK(refused([](auto& b, auto&) { b[b.size() / 2] ^= 1U; }, CheckpointError::payload));
|
|
CHECK(refused([](auto&, auto& x) { x.versions.rules = 1; }, CheckpointError::unsupported));
|
|
CHECK(refused([](auto&, auto& x) { x.rng_streams = 1; }, CheckpointError::unsupported));
|
|
CHECK(refused([](auto&, auto& x) { x.history_profile = 1; }, CheckpointError::unsupported));
|
|
CHECK(refused([](auto& b, auto&) { b[8] = 1; }, CheckpointError::unsupported));
|
|
CHECK(!restore_environmental_payload(*payload, *m, 1000)); // Budget still charged.
|
|
}
|
|
} // namespace
|
|
|
|
int main() {
|
|
try {
|
|
layout_and_round_trip();
|
|
malformed_and_unsupported();
|
|
successor_chain();
|
|
payload_validation_and_restored_trajectory();
|
|
std::puts("region checkpoint contracts passed");
|
|
return 0;
|
|
} catch (const std::exception& e) {
|
|
std::fprintf(stderr, "%s\n", e.what());
|
|
return 1;
|
|
}
|
|
}
|