Files
aevolution/src/persistence/population_checkpoint.cpp
T
dingo ff121c3ed1
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 4m27s
Foundation / postgres-tools (push) Successful in 30s
Foundation / server-image (push) Successful in 4m16s
Foundation / image (push) Successful in 4m54s
feat(cellular): seed dev wildlife and tune budding playtests
2026-10-26 12:01:57 -04:00

281 lines
15 KiB
C++

#include "evo/persistence/region_checkpoint.hpp"
namespace evo::persistence {
namespace {
std::expected<CheckpointManifest, CheckpointError>
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<CheckpointManifest, CheckpointError>
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<CheckpointManifest, CheckpointError> 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<std::unique_ptr<server::CellularPopulation>, CheckpointError>
restore_population_payload(std::span<const std::uint8_t> 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<CheckpointManifest, CheckpointError>
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<std::vector<std::uint8_t>, CheckpointError>
upgrade_payload(std::span<const std::uint8_t> 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<CheckpointManifest, CheckpointError>
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<CheckpointManifest, CheckpointError>
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<std::vector<std::uint8_t>, CheckpointError>
cellular_boundary_upgrade_payload(std::span<const std::uint8_t> payload,
const CheckpointManifest& previous) {
return upgrade_payload(payload, previous, false);
}
std::expected<std::vector<std::uint8_t>, CheckpointError>
cellular_maturation_upgrade_payload(std::span<const std::uint8_t> payload,
const CheckpointManifest& previous) {
return upgrade_payload(payload, previous, true);
}
std::expected<CheckpointManifest, CheckpointError>
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<std::vector<std::uint8_t>, CheckpointError>
cellular_inheritance_upgrade_payload(std::span<const std::uint8_t> 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();
}
std::expected<CheckpointManifest, CheckpointError>
cellular_photosynthesis_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_v12_versions, cellular_v13_versions);
}
std::expected<std::vector<std::uint8_t>, CheckpointError>
cellular_photosynthesis_upgrade_payload(std::span<const std::uint8_t> payload,
const CheckpointManifest& previous,
const server::CellularLight& light) {
if (!supported(previous) || previous.profile != CheckpointProfile::cellular_lives_v1 ||
previous.cellular_versions != cellular_v12_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)->photosynthesis_upgrade_candidate(light);
if (!upgraded)
return std::unexpected(CheckpointError::payload);
return (*upgraded)->snapshot();
}
std::expected<CheckpointManifest, CheckpointError>
cellular_liquid_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_v13_versions, cellular_v14_versions);
}
std::expected<std::vector<std::uint8_t>, CheckpointError>
cellular_liquid_upgrade_payload(std::span<const std::uint8_t> payload,
const CheckpointManifest& previous,
const server::CellularLiquidRules& rules) {
if (!supported(previous) || previous.profile != CheckpointProfile::cellular_lives_v1 ||
previous.cellular_versions != cellular_v13_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)->liquid_upgrade_candidate(rules);
if (!upgraded)
return std::unexpected(CheckpointError::payload);
return (*upgraded)->snapshot();
}
std::expected<CheckpointManifest, CheckpointError>
cellular_revamp_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_v14_versions, cellular_v15_versions);
}
std::expected<std::vector<std::uint8_t>, CheckpointError> cellular_revamp_upgrade_payload(
std::span<const std::uint8_t> payload, const CheckpointManifest& previous,
const server::CellularRevampRules& rules, std::uint32_t count, std::uint64_t first) {
if (!supported(previous) || previous.profile != CheckpointProfile::cellular_lives_v1 ||
previous.cellular_versions != cellular_v14_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)->revamp_upgrade_candidate(rules, count, first);
if (!upgraded)
return std::unexpected(CheckpointError::payload);
return (*upgraded)->snapshot();
}
} // namespace evo::persistence