feat(persistence): admit durable cellular environment profile
Foundation / checks (ecs) (push) Successful in 2m24s
Foundation / checks (deploy) (push) Successful in 4m51s
Foundation / checks (network) (push) Successful in 20m42s
Foundation / checks (client) (push) Successful in 21m53s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 40m19s
Foundation / checks (postgres) (push) Canceled after 40m9s
Foundation / checks (tsan) (push) Canceled after 37m53s

ADR-0174 and schema 56 add checkpoint profile 6, which stores the joint
cell/environment envelope nesting population envelope 2 (bundles 10 and
11). Entry from a committed population head is explicit, one-way and
no-tick, with the source commit, manifest and payload digests bound in
immutable evidence. Joint intervals use schema 45's owned admission and
settlement; conversions cannot overtake a pending interval. The fenced
publisher validates the joint trace structurally, including the complete
nested population batch, and records per-commit imported and radiated
totals. Recovery replays the head's link and requires the payload
exactly, refusing a forged but structurally valid trace. A trigger
refuses profile-6 commits from every other publisher; profile 4/5 bytes,
parsers and rules are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-19 17:16:50 -04:00
co-authored by Claude Opus 5.5
parent fc6d26d7d6
commit 51991ac7ff
25 changed files with 1599 additions and 63 deletions
@@ -0,0 +1,85 @@
#include "evo/persistence/region_checkpoint.hpp"
namespace evo::persistence {
namespace {
bool leased(const CheckpointManifest& previous, const Sha256Digest& previous_digest, Uuid commit,
const LeaseGrant& lease) noexcept {
return supported(previous) && encode_manifest(previous).has_value() && previous.sequence > 0 &&
previous.sequence != ~std::uint64_t{0} && previous.end_tick > 0 &&
population_envelope_version(previous.cellular_versions) == 2 &&
previous_digest != Sha256Digest{} && lease.frame == previous.frame && lease.epoch != 0 &&
lease.holder != Uuid{} && commit != Uuid{} && commit != previous.commit;
}
CheckpointManifest link(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) {
auto next = previous;
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;
return next;
}
} // namespace
std::expected<CheckpointManifest, CheckpointError>
environment_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) {
if (!leased(previous, previous_digest, commit, lease) ||
previous.profile != CheckpointProfile::cellular_population_v1)
return std::unexpected(CheckpointError::chain);
auto next =
link(previous, previous_digest, commit, lease, previous.end_tick, build, bytes, digest, {});
next.profile = CheckpointProfile::cellular_environment_v1;
next.history_profile = environment_history_profile;
if (!encode_manifest(next))
return std::unexpected(CheckpointError::format);
return next;
}
std::expected<CheckpointManifest, CheckpointError> environment_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 (!leased(previous, previous_digest, commit, lease) ||
previous.profile != CheckpointProfile::cellular_environment_v1 || batch.trace_bytes == 0 ||
end_tick < previous.end_tick || end_tick - previous.end_tick > max_checkpoint_ticks)
return std::unexpected(CheckpointError::chain);
auto next =
link(previous, previous_digest, commit, lease, end_tick, build, bytes, digest, batch);
if (!encode_manifest(next))
return std::unexpected(CheckpointError::format);
return next;
}
std::expected<std::unique_ptr<server::CellularEnvironment>, CheckpointError>
restore_environment_payload(std::span<const std::uint8_t> payload, const CheckpointManifest& m) {
if (!supported(m) || m.profile != CheckpointProfile::cellular_environment_v1)
return std::unexpected(CheckpointError::unsupported);
if (!encode_manifest(m) || payload.size() != m.payload_bytes)
return std::unexpected(CheckpointError::format);
auto state = server::CellularEnvironment::restore(payload, m.key.world, m.key.region, m.frame,
m.end_tick);
if (!state)
return std::unexpected(state.error() == server::CellularEnvironmentError::unsupported
? CheckpointError::unsupported
: CheckpointError::payload);
const auto& population = (*state)->population();
if (population.format_version() != population_envelope_version(m.cellular_versions) ||
population.state().format_version() != m.cellular_versions.envelope ||
population.state().rules_version() != m.cellular_versions.rules)
return std::unexpected(CheckpointError::unsupported);
return std::move(*state);
}
} // namespace evo::persistence
@@ -0,0 +1,249 @@
#include "evo/persistence/environment_checkpoint_store.hpp"
#include "evo/persistence/sha256.hpp"
#include <algorithm>
#include <utility>
namespace evo::persistence {
namespace {
using std::chrono::milliseconds;
PopulationPublicationFailure failure(PublicationError kind, ObjectError object = {},
LeaseStoreFailure store = {},
PopulationPreparedCommit prepared = {}) {
return {kind, store, object, std::move(prepared)};
}
bool bound(const CommittedCheckpoint& checkpoint, const CheckpointManifest& m) {
const auto digest = sha256(checkpoint.manifest);
return digest && *digest == checkpoint.head.digest && m.commit == checkpoint.head.commit &&
m.sequence == checkpoint.head.sequence && m.end_tick == checkpoint.head.end_tick &&
m.frame == checkpoint.head.frame;
}
// The manifest, trace and their bindings agree before anything reaches the database.
bool canonical(const PopulationPreparedCommit& prepared, const CheckpointManifest& m) {
if (!supported(m) || m.profile != CheckpointProfile::cellular_environment_v1 ||
m.key != prepared.key || m.commit != prepared.commit || m.sequence == 0 ||
!encode_manifest(m))
return false;
if (prepared.trace.empty())
return m.begin_tick == m.end_tick && m.batch == BatchDigests{};
const auto trace = server::decode_environment_batch(prepared.trace);
const auto digest = sha256(prepared.trace);
if (!trace || !digest ||
trace->population.format_version != population_envelope_version(m.cellular_versions) ||
m.batch != BatchDigests{prepared.trace.size(), *digest, 0, {}})
return false;
const auto& h = trace->population.header;
return h.world == m.key.world && h.region == m.key.region && h.frame == m.frame &&
h.predecessor == m.predecessor_commit && h.predecessor_digest == m.predecessor_digest &&
h.begin_tick == m.begin_tick && h.end_tick == m.end_tick;
}
std::expected<void, PopulationPublicationFailure> put(const FilesystemObjectStore& objects,
std::span<const std::uint8_t> bytes) {
const auto digest = sha256(bytes);
if (!digest)
return std::unexpected(failure(PublicationError::digest));
auto object = objects.put(bytes);
if (!object)
return std::unexpected(failure(PublicationError::upload, object.error()));
auto readback = objects.get(*object);
if (!readback || object->bytes != bytes.size() || object->sha256 != *digest ||
!std::ranges::equal(*readback, bytes))
return std::unexpected(
failure(PublicationError::verify, readback ? ObjectError::corrupt : readback.error()));
return {};
}
} // namespace
std::expected<CommittedCheckpoint, PopulationPublicationFailure> migrate_population_to_environment(
Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease,
const CommittedCheckpoint& predecessor, Uuid commit, const Sha256Digest& build,
server::CellularEnvironmentConfig config, milliseconds timeout, const PublishHooks& hooks) {
auto previous = decode_manifest(predecessor.manifest);
const auto previous_digest = sha256(predecessor.manifest);
if (!previous || !previous_digest || !bound(predecessor, *previous) ||
previous->profile != CheckpointProfile::cellular_population_v1 ||
population_envelope_version(previous->cellular_versions) != 2)
return std::unexpected(failure(PublicationError::predecessor));
auto source = objects.get({previous->payload_sha256, previous->payload_bytes});
if (!source)
return std::unexpected(failure(PublicationError::predecessor, source.error()));
auto population = restore_population_payload(*source, *previous);
if (!population)
return std::unexpected(failure(PublicationError::predecessor));
auto joint = server::CellularEnvironment::create(std::move(*population), std::move(config));
if (!joint || (*joint)->population().snapshot() != *source)
return std::unexpected(failure(PublicationError::candidate));
const auto payload = (*joint)->snapshot();
const auto digest = sha256(payload);
if (!digest)
return std::unexpected(failure(PublicationError::digest));
auto manifest = environment_profile_transition_manifest(*previous, *previous_digest, commit,
lease, build, payload.size(), *digest);
if (!manifest)
return std::unexpected(failure(PublicationError::candidate));
auto encoded = encode_manifest(*manifest);
if (!encoded)
return std::unexpected(failure(PublicationError::candidate));
if (auto uploaded = put(objects, payload); !uploaded)
return std::unexpected(uploaded.error());
if (hooks.before_transaction)
hooks.before_transaction();
return retry_environment_prepared(db, {previous->key, commit, std::move(*encoded), {}},
timeout);
}
std::expected<CommittedCheckpoint, PopulationPublicationFailure>
retry_environment_prepared(Postgres& db, const PopulationPreparedCommit& prepared,
milliseconds timeout) {
const auto m = decode_manifest(prepared.manifest);
if (!m || !canonical(prepared, *m))
return std::unexpected(failure(PublicationError::candidate));
auto result =
prepared.trace.empty()
? publish_environment_profile_transition(db, prepared.manifest, timeout)
: publish_environment_checkpoint(db, prepared.manifest, prepared.trace, timeout);
if (!result)
return std::unexpected(failure(result.error().kind == LeaseStoreError::unknown
? PublicationError::unknown
: PublicationError::rejected,
{}, result.error(), prepared));
const auto digest = sha256(prepared.manifest);
if (!digest || result->digest != *digest || result->commit != m->commit ||
result->sequence != m->sequence || result->end_tick != m->end_tick ||
result->frame != m->frame)
return std::unexpected(failure(PublicationError::receipt));
return CommittedCheckpoint{*result, prepared.manifest};
}
std::expected<ResolvedCommit, LeaseStoreFailure>
resolve_environment_prepared(Postgres& db, const PopulationPreparedCommit& prepared,
milliseconds timeout) {
const auto manifest = decode_manifest(prepared.manifest);
if (!manifest || !canonical(prepared, *manifest))
return std::unexpected(LeaseStoreFailure{LeaseStoreError::invalid, {}});
return resolve_environment_commit(db, prepared.key, prepared.commit, prepared.manifest,
prepared.trace, timeout);
}
std::expected<CommittedCheckpoint, PopulationPublicationFailure> publish_environment_batch(
Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease,
const CommittedCheckpoint& predecessor, const server::CellularEnvironmentBatch& batch,
const server::CellularEnvironment& candidate, Uuid commit, const Sha256Digest& build,
milliseconds timeout, const PublishHooks& hooks) {
const auto previous = decode_manifest(predecessor.manifest);
const auto previous_digest = sha256(predecessor.manifest);
const auto trace = server::encode_environment_batch(batch);
if (!previous || !previous_digest || !trace || !bound(predecessor, *previous) ||
previous->profile != CheckpointProfile::cellular_environment_v1)
return std::unexpected(failure(PublicationError::predecessor));
auto source = objects.get({previous->payload_sha256, previous->payload_bytes});
if (!source)
return std::unexpected(failure(PublicationError::predecessor, source.error()));
auto base = restore_environment_payload(*source, *previous);
if (!base)
return std::unexpected(failure(PublicationError::predecessor));
auto replay = server::replay_environment_batch(**base, batch);
const auto payload = candidate.snapshot();
if (!replay || (*replay)->snapshot() != payload)
return std::unexpected(failure(PublicationError::candidate));
const auto payload_digest = sha256(payload), trace_digest = sha256(*trace);
if (!payload_digest || !trace_digest)
return std::unexpected(failure(PublicationError::digest));
auto m = environment_successor_manifest(*previous, *previous_digest, commit, lease,
batch.population.header.end_tick, build, payload.size(),
*payload_digest, {trace->size(), *trace_digest, 0, {}});
if (!m)
return std::unexpected(failure(PublicationError::candidate));
auto encoded = encode_manifest(*m);
if (!encoded)
return std::unexpected(failure(PublicationError::candidate));
PopulationPreparedCommit prepared{previous->key, commit, std::move(*encoded), *trace};
if (!canonical(prepared, *m))
return std::unexpected(failure(PublicationError::candidate));
if (auto uploaded = put(objects, payload); !uploaded)
return std::unexpected(uploaded.error());
if (auto uploaded = put(objects, *trace); !uploaded)
return std::unexpected(uploaded.error());
if (hooks.before_transaction)
hooks.before_transaction();
return retry_environment_prepared(db, prepared, timeout);
}
std::expected<RecoveredEnvironmentCheckpoint, RecoveryFailure> recover_environment_checkpoint(
Postgres& db, const FilesystemObjectStore& objects, RegionKey key, const LeaseGrant& lease,
std::span<const Sha256Digest> retained_builds, milliseconds timeout) {
const auto fail = [](RecoveryError kind) { return std::unexpected(RecoveryFailure{kind, {}}); };
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()});
auto m = decode_manifest(committed->manifest);
if (!m || !bound(*committed, *m) || m->key != key)
return fail(RecoveryError::manifest);
if (!supported(*m) || m->profile != CheckpointProfile::cellular_environment_v1 ||
lease.frame != m->frame ||
std::ranges::find(retained_builds, m->build) == retained_builds.end())
return fail(RecoveryError::unsupported);
auto bytes = objects.get({m->payload_sha256, m->payload_bytes});
if (!bytes)
return fail(bytes.error() == ObjectError::io ? RecoveryError::store
: bytes.error() == ObjectError::missing ? RecoveryError::missing_payload
: RecoveryError::corrupt_payload);
auto state = restore_environment_payload(*bytes, *m);
if (!state)
return fail(RecoveryError::payload);
auto evidence = load_environment_evidence(db, key, lease, m->commit, timeout);
if (!evidence)
return std::unexpected(RecoveryFailure{RecoveryError::store, evidence.error()});
const auto record_optional = std::move(*evidence);
if (!record_optional.has_value())
return fail(RecoveryError::history);
const auto& record = *record_optional;
auto predecessor = load_region_commit(db, key, m->sequence - 1, timeout);
if (!predecessor)
return std::unexpected(RecoveryFailure{RecoveryError::tail, predecessor.error()});
auto previous = decode_manifest(predecessor->manifest);
if (!previous || !bound(*predecessor, *previous) || !supported(*previous) ||
previous->commit != m->predecessor_commit ||
predecessor->head.digest != m->predecessor_digest || previous->key != key ||
previous->frame != m->frame || previous->sequence + 1 != m->sequence ||
previous->end_tick != m->begin_tick || previous->cellular_versions != m->cellular_versions)
return fail(RecoveryError::tail);
auto source = objects.get({previous->payload_sha256, previous->payload_bytes});
if (!source)
return fail(RecoveryError::tail);
if (m->batch == BatchDigests{}) {
// The transition: the joint payload nests the source population byte for byte.
if (!record.trace.empty() || record.source_commit != previous->commit ||
record.source_manifest_sha256 != predecessor->head.digest ||
record.source_payload_sha256 != previous->payload_sha256 ||
previous->profile != CheckpointProfile::cellular_population_v1 ||
m->begin_tick != m->end_tick)
return fail(RecoveryError::history);
if (!restore_population_payload(*source, *previous) ||
(*state)->population().snapshot() != *source)
return fail(RecoveryError::history);
} else {
if (record.source_commit.has_value() ||
previous->profile != CheckpointProfile::cellular_environment_v1)
return fail(RecoveryError::history);
auto trace_bytes = objects.get({m->batch.trace_sha256, m->batch.trace_bytes});
if (!trace_bytes || *trace_bytes != record.trace)
return fail(RecoveryError::tail);
PopulationPreparedCommit prepared{key, m->commit, committed->manifest, *trace_bytes};
if (!canonical(prepared, *m))
return fail(RecoveryError::tail);
auto base = restore_environment_payload(*source, *previous);
auto trace = server::decode_environment_batch(*trace_bytes);
if (!base || !trace)
return fail(RecoveryError::tail);
auto replay = server::replay_environment_batch(**base, *trace);
if (!replay || (*replay)->snapshot() != *bytes)
return fail(RecoveryError::tail);
}
return RecoveredEnvironmentCheckpoint{std::move(*committed), *m, std::move(*state)};
}
} // namespace evo::persistence
+23 -9
View File
@@ -61,7 +61,8 @@ bool structural(const CheckpointManifest& m) noexcept {
const auto& b = m.batch;
const bool primitive = m.profile == CheckpointProfile::primitive_lives_v1;
const bool population = m.profile == CheckpointProfile::cellular_population_v1;
const bool cellular = m.profile == CheckpointProfile::cellular_lives_v1 || population;
const bool joint = m.profile == CheckpointProfile::cellular_environment_v1;
const bool cellular = cellular_profile(m.profile);
const bool cellular_migration = cellular && m.sequence > 0 &&
(m.cellular_versions == cellular_v2_versions ||
m.cellular_versions == cellular_v3_versions ||
@@ -71,12 +72,21 @@ bool structural(const CheckpointManifest& m) noexcept {
m.cellular_versions == cellular_v12_versions) &&
m.begin_tick == m.end_tick && m.end_tick > 0 &&
b == BatchDigests{};
const bool population_conversion = population && m.begin_tick == m.end_tick && m.end_tick > 0 &&
b.trace_bytes >= server::population_batch_header_bytes + 16;
const auto joint_min =
server::environment_batch_header_bytes + server::population_batch_header_bytes + 16;
const bool population_conversion =
m.begin_tick == m.end_tick && m.end_tick > 0 &&
((population && b.trace_bytes >= server::population_batch_header_bytes + 16) ||
(joint && b.trace_bytes >= joint_min));
if (population && (m.sequence == 0 || population_envelope_version(m.cellular_versions) == 0 ||
m.history_profile != population_history_profile || b.decision_count != 0 ||
!zero(b.decisions_sha256)))
return false;
// ADR-0174: joint profile 6 nests population envelope 2 only (bundles 10 and 11).
if (joint && (m.sequence == 0 || population_envelope_version(m.cellular_versions) != 2 ||
m.history_profile != environment_history_profile || b.decision_count != 0 ||
!zero(b.decisions_sha256)))
return false;
if ((!cellular && m.cellular_versions != cellular_v1_versions) ||
(cellular && m.cellular_versions != cellular_v1_versions &&
m.cellular_versions != cellular_v2_versions &&
@@ -169,6 +179,8 @@ bool structural(const CheckpointManifest& m) noexcept {
: population
? b.trace_bytes >= server::population_batch_header_bytes + 16 &&
b.trace_bytes <= server::max_population_batch_bytes && b.trace_bytes % 8 == 0
: joint ? b.trace_bytes >= joint_min &&
b.trace_bytes <= server::max_environment_batch_bytes && b.trace_bytes % 8 == 0
: cellular ? (cellular_current_trace || cellular_v2_trace || cellular_legacy_trace)
: b.trace_bytes == trace_bytes_for(ticks);
return link && trace_shape && !zero(b.trace_sha256) &&
@@ -178,6 +190,11 @@ bool structural(const CheckpointManifest& m) noexcept {
} // namespace
bool supported(const CheckpointManifest& m) noexcept {
if (m.profile == CheckpointProfile::cellular_environment_v1)
return m.manifest_version == 2 && m.history_profile == environment_history_profile &&
population_envelope_version(m.cellular_versions) == 2 &&
m.versions == environmental_v1_versions && m.tick_microseconds == 50000 &&
m.rng_streams == 0 && m.durability == DurabilityPolicy::single_volume_development;
if (m.profile == CheckpointProfile::cellular_population_v1)
return m.manifest_version == 2 && m.history_profile == population_history_profile &&
population_envelope_version(m.cellular_versions) != 0 &&
@@ -240,8 +257,7 @@ encode_manifest(const CheckpointManifest& m) {
w.u(m.epoch, 8);
w.u(m.tick_microseconds, 8);
const auto versions =
(m.profile == CheckpointProfile::cellular_lives_v1 ||
m.profile == CheckpointProfile::cellular_population_v1)
cellular_profile(m.profile)
? std::array{m.cellular_versions.rules, m.cellular_versions.envelope,
m.cellular_versions.habitat, m.cellular_versions.coordinate,
m.cellular_versions.genome, m.cellular_versions.development,
@@ -298,8 +314,7 @@ decode_manifest(std::span<const std::uint8_t> bytes) {
std::array<std::uint32_t, 9> versions{};
for (auto& v : versions)
v = static_cast<std::uint32_t>(r.u(4));
if (m.profile == CheckpointProfile::cellular_lives_v1 ||
m.profile == CheckpointProfile::cellular_population_v1) {
if (cellular_profile(m.profile)) {
m.cellular_versions = {versions[0], versions[1], versions[2], versions[3], versions[4],
versions[5], versions[6], versions[7], versions[8]};
} else {
@@ -360,8 +375,7 @@ clone_manifest(const CheckpointManifest& source, const Sha256Digest& source_dige
RegionKey target, Uuid commit, std::uint64_t payload_bytes,
const Sha256Digest& payload_sha256, const BatchDigests& inherited) {
if (!supported(source) || source.profile == CheckpointProfile::primitive_lives_v1 ||
source.profile == CheckpointProfile::cellular_lives_v1 ||
source.profile == CheckpointProfile::cellular_population_v1)
cellular_profile(source.profile))
return std::unexpected{CheckpointError::unsupported};
if (!structural(source) || target.world == source.key.world || commit == source.commit ||
inherited.trace_bytes != 0 || !zero(inherited.trace_sha256))
+74 -34
View File
@@ -1502,26 +1502,11 @@ std::expected<bool, AccountIdentityError> revoke_account_grants(Postgres& db, st
return std::unexpected{AccountIdentityError::unknown};
return *revoked == "true";
}
std::expected<RegionHead, LeaseStoreFailure>
publish_population_checkpoint(Postgres& db, std::span<const std::uint8_t> manifest,
std::span<const std::uint8_t> trace, milliseconds timeout) {
const std::array<std::string, 2> parameters{bytea(manifest), bytea(trace)};
auto rows =
db.query("SELECT * FROM evolution.publish_population_checkpoint($1::bytea,$2::bytea)",
parameters, timeout);
if (!rows)
return std::unexpected(classify(rows.error()));
auto head = parse_head(*rows);
if (!head)
return std::unexpected(malformed(true));
return *head;
}
std::expected<RegionHead, LeaseStoreFailure>
publish_population_profile_transition(Postgres& db, std::span<const std::uint8_t> manifest,
milliseconds timeout) {
const std::array<std::string, 1> parameters{bytea(manifest)};
auto rows = db.query("SELECT * FROM evolution.publish_population_profile_transition($1::bytea)",
parameters, timeout);
namespace {
std::expected<RegionHead, LeaseStoreFailure> publish_query(Postgres& db, const char* sql,
std::span<const std::string> parameters,
milliseconds timeout) {
auto rows = db.query(sql, parameters, timeout);
if (!rows)
return std::unexpected(classify(rows.error()));
auto head = parse_head(*rows);
@@ -1530,15 +1515,15 @@ publish_population_profile_transition(Postgres& db, std::span<const std::uint8_t
return *head;
}
std::expected<ResolvedCommit, LeaseStoreFailure>
resolve_population_commit(Postgres& db, RegionKey key, const Uuid& commit,
std::span<const std::uint8_t> manifest,
std::span<const std::uint8_t> trace, milliseconds timeout) {
resolve_query(Postgres& db, std::string_view function, RegionKey key, const Uuid& commit,
std::span<const std::uint8_t> manifest, std::span<const std::uint8_t> trace,
milliseconds timeout) {
const std::array<std::string, 5> parameters{std::to_string(key.world),
std::to_string(key.region), uuid_text(commit),
bytea(manifest), bytea(trace)};
auto rows = db.query("SELECT * FROM evolution.resolve_population_commit($1::evolution.u64,"
"$2::evolution.u64,$3::uuid,$4::bytea,$5::bytea)",
parameters, timeout);
const auto sql = std::string{"SELECT * FROM evolution."} + std::string{function} +
"($1::evolution.u64,$2::evolution.u64,$3::uuid,$4::bytea,$5::bytea)";
auto rows = db.query(sql, parameters, timeout);
if (!rows)
return std::unexpected(classify(rows.error()));
if (rows->size() != 1 || (*rows)[0].size() != 6 || !(*rows)[0][0])
@@ -1558,17 +1543,17 @@ resolve_population_commit(Postgres& db, RegionKey key, const Uuid& commit,
return ResolvedCommit{Resolution::committed, *head};
}
std::expected<std::optional<PopulationEvidence>, LeaseStoreFailure>
load_population_evidence(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& commit,
milliseconds timeout) {
evidence_query(Postgres& db, std::string_view function, RegionKey key, const LeaseGrant& lease,
const Uuid& commit, milliseconds timeout) {
const std::array<std::string, 5> parameters{std::to_string(key.world),
std::to_string(key.region), uuid_text(lease.holder),
std::to_string(lease.epoch), uuid_text(commit)};
auto rows = db.query(
"SELECT encode(trace,'hex'),source_commit::text,encode(source_manifest_sha256,'hex'),"
"encode(source_payload_sha256,'hex') FROM "
"evolution.load_population_evidence($1::evolution.u64,"
"$2::evolution.u64,$3::uuid,$4::evolution.u64,$5::uuid)",
parameters, timeout);
const auto sql =
std::string{"SELECT encode(trace,'hex'),source_commit::text,encode(source_manifest_sha256,"
"'hex'),encode(source_payload_sha256,'hex') FROM evolution."} +
std::string{function} +
"($1::evolution.u64,$2::evolution.u64,$3::uuid,$4::evolution.u64,$5::uuid)";
auto rows = db.query(sql, parameters, timeout);
if (!rows)
return std::unexpected(classify(rows.error()));
if (rows->empty())
@@ -1598,4 +1583,59 @@ load_population_evidence(Postgres& db, RegionKey key, const LeaseGrant& lease, c
return std::unexpected(malformed(false));
return std::optional<PopulationEvidence>{std::move(evidence)};
}
} // namespace
std::expected<RegionHead, LeaseStoreFailure>
publish_population_checkpoint(Postgres& db, std::span<const std::uint8_t> manifest,
std::span<const std::uint8_t> trace, milliseconds timeout) {
const std::array<std::string, 2> parameters{bytea(manifest), bytea(trace)};
return publish_query(
db, "SELECT * FROM evolution.publish_population_checkpoint($1::bytea,$2::bytea)",
parameters, timeout);
}
std::expected<RegionHead, LeaseStoreFailure>
publish_population_profile_transition(Postgres& db, std::span<const std::uint8_t> manifest,
milliseconds timeout) {
const std::array<std::string, 1> parameters{bytea(manifest)};
return publish_query(db,
"SELECT * FROM evolution.publish_population_profile_transition($1::bytea)",
parameters, timeout);
}
std::expected<ResolvedCommit, LeaseStoreFailure>
resolve_population_commit(Postgres& db, RegionKey key, const Uuid& commit,
std::span<const std::uint8_t> manifest,
std::span<const std::uint8_t> trace, milliseconds timeout) {
return resolve_query(db, "resolve_population_commit", key, commit, manifest, trace, timeout);
}
std::expected<std::optional<PopulationEvidence>, LeaseStoreFailure>
load_population_evidence(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& commit,
milliseconds timeout) {
return evidence_query(db, "load_population_evidence", key, lease, commit, timeout);
}
std::expected<RegionHead, LeaseStoreFailure>
publish_environment_checkpoint(Postgres& db, std::span<const std::uint8_t> manifest,
std::span<const std::uint8_t> trace, milliseconds timeout) {
const std::array<std::string, 2> parameters{bytea(manifest), bytea(trace)};
return publish_query(
db, "SELECT * FROM evolution.publish_environment_checkpoint($1::bytea,$2::bytea)",
parameters, timeout);
}
std::expected<RegionHead, LeaseStoreFailure>
publish_environment_profile_transition(Postgres& db, std::span<const std::uint8_t> manifest,
milliseconds timeout) {
const std::array<std::string, 1> parameters{bytea(manifest)};
return publish_query(
db, "SELECT * FROM evolution.publish_environment_profile_transition($1::bytea)", parameters,
timeout);
}
std::expected<ResolvedCommit, LeaseStoreFailure>
resolve_environment_commit(Postgres& db, RegionKey key, const Uuid& commit,
std::span<const std::uint8_t> manifest,
std::span<const std::uint8_t> trace, milliseconds timeout) {
return resolve_query(db, "resolve_environment_commit", key, commit, manifest, trace, timeout);
}
std::expected<std::optional<PopulationEvidence>, LeaseStoreFailure>
load_environment_evidence(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& commit,
milliseconds timeout) {
return evidence_query(db, "load_environment_evidence", key, lease, commit, timeout);
}
} // namespace evo::persistence