Foundation / checks (ecs) (push) Successful in 2m51s
Foundation / checks (deploy) (push) Successful in 5m18s
Foundation / checks (network) (push) Successful in 25m49s
Foundation / checks (client) (push) Successful in 26m34s
Foundation / checks (tsan) (push) Successful in 37m12s
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 47m59s
Foundation / checks (postgres) (push) Canceled after 47m53s
351 lines
17 KiB
C++
351 lines
17 KiB
C++
#include "evo/persistence/region_checkpoint.hpp"
|
|
#include "fixtures/cellular_ecology.hpp"
|
|
|
|
#include <fstream>
|
|
#include <iterator>
|
|
|
|
namespace {
|
|
using namespace evo;
|
|
using namespace evo::server;
|
|
using namespace evo::persistence;
|
|
using namespace evo::fixture;
|
|
template <std::size_t N> std::array<std::uint8_t, N> filled(std::uint8_t value) {
|
|
std::array<std::uint8_t, N> result{};
|
|
result.fill(value);
|
|
return result;
|
|
}
|
|
std::vector<CellularLifeIntent> idle(const CellularLives& state, bool bud = false,
|
|
bool frozen = false) {
|
|
std::vector<CellularLifeIntent> result;
|
|
for (const auto& life : state.lives()) {
|
|
CellularLifeIntent input{life.identity.life, {}};
|
|
if (life.account != LifeAccount{}) {
|
|
input.intent.frozen = frozen;
|
|
input.intent.bud = bud && !frozen;
|
|
}
|
|
result.push_back(input);
|
|
}
|
|
return result;
|
|
}
|
|
const CellularOwnedLife& parent(const CellularLives& state) {
|
|
const auto found =
|
|
std::ranges::find(state.lives(), 100, [](const auto& life) { return life.identity.life; });
|
|
require(found != state.lives().end(), "checkpoint parent missing");
|
|
return *found;
|
|
}
|
|
Owner source_owner() {
|
|
auto state = opening(7, true, 1);
|
|
state = checked(state->migration_upgrade_candidate(), "checkpoint boundary source");
|
|
state = checked(state->maturation_upgrade_candidate(), "checkpoint maturation source");
|
|
for (unsigned tick = 0; tick < 60'000 && parent(*state).identity.genome == 100; ++tick) {
|
|
const auto cell = parent(*state);
|
|
if (cell.mutation.offer) {
|
|
const CellularMutationSelection choice{
|
|
100, cell.mutation.event_index, cell.mutation.offer_tick, 500'000,
|
|
cell.mutation.offer->candidates.front().operator_id};
|
|
checked(state->advance_tick_with_mutations(idle(*state), std::span{&choice, 1}),
|
|
"legacy expressed source mutation");
|
|
} else
|
|
checked(state->advance_tick_with_mutations(idle(*state), {}), "source earned offer");
|
|
}
|
|
require(parent(*state).identity.genome == 500'000 && !state->archive().revisions().empty(),
|
|
"source has no legacy expressed revision");
|
|
bool born = false;
|
|
for (unsigned tick = 0; tick < 30'000 && !born; ++tick) {
|
|
const auto result = checked(state->advance_tick_with_mutations(idle(*state, true), {}),
|
|
"fund source offspring");
|
|
born = std::ranges::any_of(
|
|
result.buds, [](const auto& bud) { return bud.result == CellularBudResult::budded; });
|
|
}
|
|
require(born, "source did not produce funded offspring");
|
|
for (unsigned tick = 0; tick < 205; ++tick)
|
|
checked(state->advance_tick_with_mutations(idle(*state, false, true), {}),
|
|
"suspend source parent");
|
|
require(parent(*state).presence == CellularPresence::suspended && state->lives().size() > 1,
|
|
"source lost its suspended parent or funded offspring");
|
|
return state;
|
|
}
|
|
// Opaque digest inputs exercise this storage-free API; the I/O caller must verify SHA-256.
|
|
CheckpointManifest source_manifest(const CellularLives& source) {
|
|
CheckpointManifest m;
|
|
m.profile = CheckpointProfile::cellular_lives_v1;
|
|
m.key = {source.world(), source.region()};
|
|
m.frame = source.frame();
|
|
m.commit = filled<16>(3);
|
|
m.sequence = 8;
|
|
m.predecessor_commit = filled<16>(2);
|
|
m.predecessor_digest = filled<32>(4);
|
|
m.begin_tick = source.tick() - 1;
|
|
m.end_tick = source.tick();
|
|
m.holder = filled<16>(5);
|
|
m.epoch = 4;
|
|
m.tick_microseconds = biology::cellular_motion_tick_microseconds;
|
|
m.cellular_versions = cellular_v11_versions;
|
|
m.build = filled<32>(6);
|
|
m.history_profile = cellular_revision_history_profile;
|
|
m.payload_bytes = source.snapshot().size();
|
|
m.payload_sha256 = filled<32>(7);
|
|
m.batch.trace_bytes = cellular_batch_header_bytes + cellular_batch_tick_header_bytes +
|
|
source.lives().size() * cellular_batch_intent_bytes;
|
|
m.batch.trace_sha256 = filled<32>(8);
|
|
require(supported(m) && encode_manifest(m).has_value(), "invalid source manifest fixture");
|
|
return m;
|
|
}
|
|
LeaseGrant lease_for(const CheckpointManifest& m) {
|
|
return {filled<16>(9), 5, m.frame};
|
|
}
|
|
struct Upgraded {
|
|
CheckpointManifest manifest;
|
|
std::vector<std::uint8_t> payload;
|
|
};
|
|
Upgraded upgrade(std::span<const std::uint8_t> bytes, const CheckpointManifest& source) {
|
|
auto payload = checked(cellular_inheritance_upgrade_payload(bytes, source), "upgrade payload");
|
|
auto manifest = checked(cellular_inheritance_upgrade_manifest(
|
|
source, filled<32>(10), filled<16>(11), lease_for(source),
|
|
filled<32>(12), payload.size(), filled<32>(13)),
|
|
"upgrade manifest");
|
|
return {manifest, std::move(payload)};
|
|
}
|
|
void continuation(const Upgraded& target) {
|
|
auto state = checked(restore_cellular_payload(target.payload, target.manifest),
|
|
"upgraded continuation source");
|
|
auto head = target.manifest;
|
|
for (unsigned tick = 0; tick < 60'000; ++tick) {
|
|
const auto cell = parent(*state);
|
|
const bool selecting =
|
|
cell.mutation.offer.has_value() && cell.presence == CellularPresence::active;
|
|
CellularBatch batch;
|
|
batch.format_version = cellular_batch_inheritance_format;
|
|
batch.header = {state->world(), state->region(), state->frame(), {}, {},
|
|
state->tick(), state->tick() + 1};
|
|
batch.header.predecessor.fill(14);
|
|
batch.header.predecessor_digest.fill(15);
|
|
batch.ticks.push_back({idle(*state), {}, {}});
|
|
if (selecting) {
|
|
const auto offer = cell.mutation.offer;
|
|
if (!offer)
|
|
throw std::runtime_error("continuation offer missing");
|
|
batch.ticks.front().mutations.push_back({100, cell.mutation.event_index,
|
|
cell.mutation.offer_tick, 600'000,
|
|
offer->candidates.front().operator_id});
|
|
}
|
|
const auto before = restore(*state);
|
|
const auto result = checked(state->advance_tick_with_mutations(
|
|
batch.ticks.front().lives, batch.ticks.front().mutations),
|
|
"inherited continuation tick");
|
|
const auto recorded = checked(encode_cellular_batch(batch), "continuation trace");
|
|
require(checked(replay_cellular_batch(*before, checked(decode_cellular_batch(recorded),
|
|
"continuation trace decode")),
|
|
"continuation replay")
|
|
->snapshot() == state->snapshot(),
|
|
"continued trace differs from direct owner");
|
|
auto commit = filled<16>(21);
|
|
for (unsigned offset = 0; offset < 8; ++offset)
|
|
commit[offset] = static_cast<std::uint8_t>((head.sequence + 1) >> (offset * 8U));
|
|
head = checked(successor_manifest(head, filled<32>(20), commit, lease_for(head),
|
|
state->tick(), head.build, state->snapshot().size(),
|
|
filled<32>(22), {recorded.size(), filled<32>(23), 0, {}}),
|
|
"inherited successor manifest");
|
|
require(head.cellular_versions == cellular_v12_versions &&
|
|
checked(restore_cellular_payload(state->snapshot(), head),
|
|
"continued checkpoint restore")
|
|
->snapshot() == state->snapshot(),
|
|
"ordinary successor lost retained-plan tuple or biology");
|
|
if (selecting) {
|
|
require(result.mutations.size() == 1 &&
|
|
result.mutations.front().result ==
|
|
CellularMutationResult::offspring_plan_applied &&
|
|
parent(*state).identity.genome == 500'000 &&
|
|
parent(*state).offspring_genome == 600'000,
|
|
"continuation rebuilt expressed body or lost selected plan");
|
|
return;
|
|
}
|
|
}
|
|
throw std::runtime_error("continued upgraded owner did not earn an offer");
|
|
}
|
|
void contracts() {
|
|
auto source = source_owner();
|
|
const auto original = source->snapshot();
|
|
const auto manifest = source_manifest(*source);
|
|
const auto manifest_bytes = checked(encode_manifest(manifest), "source manifest bytes");
|
|
const auto target = upgrade(original, manifest);
|
|
const auto restored =
|
|
checked(restore_cellular_payload(target.payload, target.manifest), "target full restore");
|
|
require(source->snapshot() == original &&
|
|
checked(encode_manifest(manifest), "unchanged source manifest") == manifest_bytes &&
|
|
restored->snapshot() ==
|
|
checked(source->inheritance_upgrade_candidate(), "owner oracle")->snapshot() &&
|
|
restored->archive().encode() == source->archive().encode() &&
|
|
restored->current_total() == source->current_total() &&
|
|
parent(*restored).identity == parent(*source).identity &&
|
|
parent(*restored).offspring_genome == 500'000 &&
|
|
parent(*restored).presence == CellularPresence::suspended &&
|
|
restored->offspring_revisions().empty(),
|
|
"checkpoint upgrade changed source or retained biology");
|
|
for (const auto& life : restored->lives())
|
|
require(life.offspring_genome == life.identity.genome, "initial inherited plan mismatch");
|
|
const auto& m = target.manifest;
|
|
require(supported(m) && m.cellular_versions == cellular_v12_versions &&
|
|
m.profile == manifest.profile && m.history_profile == manifest.history_profile &&
|
|
m.key == manifest.key && m.frame == manifest.frame &&
|
|
m.sequence == manifest.sequence + 1 && m.predecessor_commit == manifest.commit &&
|
|
m.predecessor_digest == filled<32>(10) && m.commit == filled<16>(11) &&
|
|
m.begin_tick == source->tick() && m.end_tick == source->tick() &&
|
|
m.tick_microseconds == manifest.tick_microseconds && m.batch == BatchDigests{} &&
|
|
m.holder == lease_for(manifest).holder && m.epoch == lease_for(manifest).epoch &&
|
|
m.build == filled<32>(12) && m.payload_bytes == target.payload.size() &&
|
|
m.payload_sha256 == filled<32>(13) &&
|
|
checked(decode_manifest(checked(encode_manifest(m), "target encode")),
|
|
"target decode") == m,
|
|
"upgrade manifest lost its exact bindings");
|
|
require(!cellular_inheritance_upgrade_payload(target.payload, m) &&
|
|
!cellular_inheritance_upgrade_manifest(m, filled<32>(1), filled<16>(2),
|
|
lease_for(m), m.build, m.payload_bytes,
|
|
m.payload_sha256),
|
|
"repeated inheritance upgrade admitted");
|
|
auto mislabeled = manifest;
|
|
mislabeled.cellular_versions = cellular_v12_versions;
|
|
require(!restore_cellular_payload(original, mislabeled), "old bytes admitted as bundle 12");
|
|
mislabeled = m;
|
|
mislabeled.cellular_versions = cellular_v11_versions;
|
|
require(!restore_cellular_payload(target.payload, mislabeled),
|
|
"new bytes admitted as bundle 11");
|
|
auto corrupt = original;
|
|
corrupt.back() ^= 1;
|
|
require(!cellular_inheritance_upgrade_payload(corrupt, manifest) &&
|
|
!cellular_inheritance_upgrade_payload(std::span{original}.first(16), manifest),
|
|
"bad source payload upgraded");
|
|
auto genesis = m;
|
|
genesis.sequence = 0;
|
|
genesis.begin_tick = genesis.end_tick = 0;
|
|
genesis.predecessor_commit = {};
|
|
genesis.predecessor_digest = {};
|
|
genesis.holder = {};
|
|
genesis.epoch = 0;
|
|
require(!encode_manifest(genesis), "retained-plan genesis bypassed upgrade");
|
|
require(population_envelope_version(cellular_v12_versions) == 0 &&
|
|
!cellular_revision_bundle(cellular_v12_versions) &&
|
|
!population_profile_transition_manifest(m, filled<32>(1), filled<16>(2),
|
|
lease_for(m), m.build, m.payload_bytes,
|
|
m.payload_sha256),
|
|
"legacy population/trace admission included retained plans");
|
|
auto population = manifest;
|
|
population.profile = CheckpointProfile::cellular_population_v1;
|
|
population.history_profile = population_history_profile;
|
|
require(!cellular_inheritance_upgrade_payload(original, population) &&
|
|
!cellular_inheritance_upgrade_manifest(population, filled<32>(1), filled<16>(2),
|
|
lease_for(population), population.build,
|
|
m.payload_bytes, m.payload_sha256),
|
|
"population inheritance upgrade admitted");
|
|
population.cellular_versions = cellular_v12_versions;
|
|
require(!supported(population) && !encode_manifest(population),
|
|
"bundle 12 population admitted");
|
|
for (unsigned fault = 0; fault < 15; ++fault) {
|
|
auto bad = manifest;
|
|
auto lease = lease_for(bad);
|
|
auto reference = filled<32>(10);
|
|
auto commit = filled<16>(11);
|
|
auto build = filled<32>(12);
|
|
auto digest = filled<32>(13);
|
|
auto bytes = m.payload_bytes;
|
|
switch (fault) {
|
|
case 0:
|
|
bad.sequence = ~std::uint64_t{0};
|
|
break;
|
|
case 1:
|
|
bad.sequence = 0;
|
|
break;
|
|
case 2:
|
|
bad.cellular_versions = cellular_v10_versions;
|
|
break;
|
|
case 3:
|
|
bad.cellular_versions = cellular_v9_versions;
|
|
break;
|
|
case 4:
|
|
++lease.frame;
|
|
break;
|
|
case 5:
|
|
lease.epoch = 0;
|
|
break;
|
|
case 6:
|
|
lease.holder = {};
|
|
break;
|
|
case 7:
|
|
reference = {};
|
|
break;
|
|
case 8:
|
|
commit = manifest.commit;
|
|
break;
|
|
case 9:
|
|
commit = {};
|
|
break;
|
|
case 10:
|
|
build = {};
|
|
break;
|
|
case 11:
|
|
digest = {};
|
|
break;
|
|
case 12:
|
|
bytes = 0;
|
|
break;
|
|
case 13:
|
|
bytes = max_cellular_lives_bytes + 1;
|
|
break;
|
|
case 14:
|
|
bad.history_profile = cellular_history_profile;
|
|
break;
|
|
default:
|
|
throw std::runtime_error("unknown checkpoint fault");
|
|
}
|
|
require(!cellular_inheritance_upgrade_manifest(bad, reference, commit, lease, build, bytes,
|
|
digest),
|
|
"invalid upgrade binding admitted");
|
|
}
|
|
continuation(target);
|
|
}
|
|
void save(const std::string& path, std::span<const std::uint8_t> bytes) {
|
|
std::ofstream file(path, std::ios::binary | std::ios::trunc);
|
|
file.write(reinterpret_cast<const char*>(bytes.data()),
|
|
static_cast<std::streamsize>(bytes.size()));
|
|
require(file.good(), "checkpoint fixture write");
|
|
}
|
|
std::vector<std::uint8_t> read(const std::string& path, std::size_t bound) {
|
|
std::ifstream file(path, std::ios::binary);
|
|
require(file.good(), "checkpoint fixture read");
|
|
std::vector<std::uint8_t> bytes{std::istreambuf_iterator<char>{file}, {}};
|
|
require(bytes.size() <= bound, "checkpoint fixture bound");
|
|
return bytes;
|
|
}
|
|
std::vector<std::uint8_t> proof(const Upgraded& candidate) {
|
|
checked(restore_cellular_payload(candidate.payload, candidate.manifest), "process target");
|
|
auto bytes = checked(encode_manifest(candidate.manifest), "proof manifest");
|
|
bytes.insert(bytes.end(), candidate.payload.begin(), candidate.payload.end());
|
|
return bytes; // Test-only complete manifest + payload comparison, not a storage codec.
|
|
}
|
|
} // namespace
|
|
int main(int argc, char** argv) try {
|
|
if (argc == 4 && std::string_view{argv[1]} == "--checkpoint") {
|
|
const auto source = source_owner();
|
|
const auto bytes = source->snapshot();
|
|
const auto manifest = source_manifest(*source);
|
|
save(argv[2], bytes);
|
|
save(std::string{argv[2]} + ".manifest", checked(encode_manifest(manifest), "save source"));
|
|
save(argv[3], proof(upgrade(bytes, manifest)));
|
|
return 0;
|
|
}
|
|
if (argc == 4 && std::string_view{argv[1]} == "--resume") {
|
|
const auto source = read(argv[2], max_cellular_lives_bytes);
|
|
const auto manifest = checked(
|
|
decode_manifest(read(std::string{argv[2]} + ".manifest", checkpoint_manifest_bytes)),
|
|
"new process source manifest");
|
|
save(argv[3], proof(upgrade(source, manifest)));
|
|
return 0;
|
|
}
|
|
require(argc == 1, "unknown inheritance checkpoint arguments");
|
|
contracts();
|
|
return 0;
|
|
} catch (const std::exception& error) {
|
|
std::cerr << error.what() << '\n';
|
|
return 1;
|
|
}
|