#include "evo/persistence/region_checkpoint.hpp" #include "fixtures/cellular_ecology.hpp" namespace { using namespace evo; using namespace evo::server; using namespace evo::persistence; using namespace evo::fixture; template std::array filled(std::uint8_t byte) { std::array out{}; out.fill(byte); return out; } std::unique_ptr joint() { auto state = opening(7, true); state = checked(state->migration_upgrade_candidate(), "boundary upgrade"); state = checked(state->maturation_upgrade_candidate(), "maturation upgrade"); auto population = checked(CellularPopulation::create(std::move(state), {false, 900, 1'600}), "population"); CellularEnvironmentConfig config; config.geometry.box = {{0}, {0}, {0}, {6'000}, {6'000}, {10}}; CellularInfallSource source; source.id = 1; source.width_um = 6'000; source.height_um = 6'000; source.start_tick = 1; source.end_tick = ~std::uint64_t{0}; source.mean_interval_ticks = 20; source.per_bubble.energy.value = 1'000; source.entitlement.energy.value = 1'000'000; config.sources.push_back(source); config.radiation_cap = {5}; return checked(CellularEnvironment::create(std::move(population), std::move(config)), "joint"); } // A committed profile-5 head at the joint fixture's cut (bundle 11, population envelope 2). CheckpointManifest population_head(const CellularEnvironment& state) { CheckpointManifest m; m.profile = CheckpointProfile::cellular_population_v1; m.key = {state.state().world(), state.state().region()}; m.frame = state.state().frame(); m.commit = filled<16>(3); m.sequence = 4; m.predecessor_commit = filled<16>(2); m.predecessor_digest = filled<32>(5); m.begin_tick = state.tick(); m.end_tick = state.tick(); m.holder = filled<16>(6); m.epoch = 2; m.tick_microseconds = 50'000; m.versions = environmental_v1_versions; m.cellular_versions = cellular_v11_versions; m.build = filled<32>(7); m.history_profile = population_history_profile; const auto bytes = state.population().snapshot(); m.payload_bytes = bytes.size(); m.payload_sha256 = filled<32>(8); return m; } } // namespace int main() { try { auto state = joint(); const auto head = population_head(*state); require(encode_manifest(head) && supported(head), "profile-5 fixture head"); const LeaseGrant lease{filled<16>(9), 3, head.frame, {}}; const auto payload = state->snapshot(); const auto transition = checked( environment_profile_transition_manifest(head, filled<32>(10), filled<16>(11), lease, filled<32>(12), payload.size(), filled<32>(13)), "profile transition"); require(transition.profile == CheckpointProfile::cellular_environment_v1 && transition.history_profile == environment_history_profile && transition.cellular_versions == cellular_v11_versions && transition.begin_tick == transition.end_tick && transition.batch == BatchDigests{} && transition.sequence == head.sequence + 1 && supported(transition), "transition changed bundle, tick or evidence"); const auto encoded = checked(encode_manifest(transition), "encode transition"); require(checked(decode_manifest(encoded), "decode transition") == transition, "transition manifest round trip"); const auto restored = checked(restore_environment_payload(payload, transition), "restore joint payload"); require(restored->snapshot() == payload, "joint payload restore changed bytes"); // Only a profile-5 owner-7 predecessor converts; there is no reverse transition. require(!environment_profile_transition_manifest(transition, filled<32>(10), filled<16>(14), lease, filled<32>(12), payload.size(), filled<32>(13)), "joint profile re-entered its own transition"); auto raw = head; raw.profile = CheckpointProfile::cellular_lives_v1; raw.history_profile = cellular_revision_history_profile; require(!environment_profile_transition_manifest(raw, filled<32>(10), filled<16>(11), lease, filled<32>(12), payload.size(), filled<32>(13)), "raw profile skipped the population profile"); auto old = head; old.cellular_versions = cellular_v9_versions; require(!environment_profile_transition_manifest(old, filled<32>(10), filled<16>(11), lease, filled<32>(12), payload.size(), filled<32>(13)), "population envelope 1 entered the joint profile"); auto wrong_lease = lease; wrong_lease.frame += 1; require(!environment_profile_transition_manifest(head, filled<32>(10), filled<16>(11), wrong_lease, filled<32>(12), payload.size(), filled<32>(13)), "transition under another frame's lease"); const BatchDigests batch{server::environment_batch_header_bytes + server::population_batch_header_bytes + 16 + 48, filled<32>(15), 0, {}}; const auto next = checked(environment_successor_manifest(transition, filled<32>(16), filled<16>(17), lease, transition.end_tick + 1, filled<32>(12), payload.size(), filled<32>(18), batch), "joint successor"); require(next.profile == transition.profile && next.batch == batch && next.begin_tick == transition.end_tick && supported(next), "joint successor changed profile or evidence"); const auto conversion = checked(environment_successor_manifest(transition, filled<32>(16), filled<16>(17), lease, transition.end_tick, filled<32>(12), payload.size(), filled<32>(18), batch), "joint conversion"); require(conversion.begin_tick == conversion.end_tick, "conversion advanced its tick"); require(!environment_successor_manifest(transition, filled<32>(16), filled<16>(17), lease, transition.end_tick + 1, filled<32>(12), payload.size(), filled<32>(18), {}), "joint successor without a trace"); require(!environment_successor_manifest(transition, filled<32>(16), filled<16>(17), lease, transition.end_tick + 65, filled<32>(12), payload.size(), filled<32>(18), batch), "joint interval above 64 ticks"); require(!environment_successor_manifest(head, filled<32>(16), filled<16>(17), lease, head.end_tick + 1, filled<32>(12), payload.size(), filled<32>(18), batch), "population head published a joint successor"); auto history = transition; history.history_profile = population_history_profile; require(!encode_manifest(history) && !supported(history), "joint history 5 accepted"); auto bundle12 = transition; bundle12.cellular_versions = cellular_v12_versions; require(!supported(bundle12), "joint profile admitted bundle 12"); auto genesis = transition; genesis.sequence = 0; require(!encode_manifest(genesis), "joint profile provisioned without a source"); auto later = transition; later.end_tick += 1; later.begin_tick = later.end_tick; require(!restore_environment_payload(payload, later), "payload restored at another tick"); require(!restore_environment_payload(state->population().snapshot(), transition), "bare population restored as a joint result"); auto unsupported = transition; unsupported.cellular_versions = cellular_v10_versions; require(restore_environment_payload(payload, unsupported).error() == CheckpointError::unsupported, "rules mismatch accepted"); // ADR-0176: bundle 13 follows bundle 12 only, on raw profile 4. auto plans = head; plans.profile = CheckpointProfile::cellular_lives_v1; plans.history_profile = cellular_revision_history_profile; plans.cellular_versions = cellular_v12_versions; require(supported(plans) && encode_manifest(plans), "bundle 12 fixture head"); const auto lit = checked(cellular_photosynthesis_upgrade_manifest( plans, filled<32>(20), filled<16>(21), lease, filled<32>(12), payload.size(), filled<32>(22)), "bundle 13 manifest"); require(lit.cellular_versions == cellular_v13_versions && supported(lit) && lit.begin_tick == lit.end_tick && cellular_checkpoint_trace_format(lit.cellular_versions) == server::cellular_batch_inheritance_format, "bundle 13 manifest changed tick or trace format"); auto skipped = plans; skipped.cellular_versions = cellular_v11_versions; require(!cellular_photosynthesis_upgrade_manifest(skipped, filled<32>(20), filled<16>(21), lease, filled<32>(12), payload.size(), filled<32>(22)), "bundle 11 skipped to bundle 13"); require(!cellular_photosynthesis_upgrade_manifest(head, filled<32>(20), filled<16>(21), lease, filled<32>(12), payload.size(), filled<32>(22)), "population profile entered bundle 13"); std::cout << "environment checkpoint contracts passed\n"; return 0; } catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; } }