#include "evo/processes/lineage_region.hpp" #include #include #include namespace evo::processes { namespace { constexpr std::uint64_t region_magic = 0x31524C454F564541ULL; // "AEVOELR1" constexpr std::size_t region_header_bytes = 56; std::uint64_t fnv(std::span bytes) noexcept { std::uint64_t value = 14695981039346656037ULL; for (const auto byte : bytes) value = (value ^ byte) * 1099511628211ULL; return value; } // Fills an exactly sized buffer; the caller checks the final cursor (M6.5c pattern, // which also avoids GCC 14 reserve/append false positives). class Cursor { public: explicit Cursor(world::WorldBytes& out) : out_(out) {} void put(std::uint64_t value, unsigned width) { for (unsigned i = 0; i < width; ++i, value >>= 8U) out_[at_++] = static_cast(value & 0xFFU); } void bytes(std::span value) { std::ranges::copy(value, out_.begin() + static_cast(at_)); at_ += value.size(); } [[nodiscard]] std::size_t at() const noexcept { return at_; } private: world::WorldBytes& out_; std::size_t at_{}; }; std::uint64_t get(std::span in, std::size_t at, unsigned width) { std::uint64_t value{}; for (unsigned i = 0; i < width; ++i) value |= static_cast(in[at + i]) << (8U * i); return value; } } // namespace LineageRegion::LineageRegion(std::unique_ptr community, const biology::LineageArchive& archive, LineageBinding binding) : community_(std::move(community)), archive_(archive), binding_(binding) {} bool LineageRegion::closed() const { const auto& b = binding_; if (!community_ || b.producer == 0 || b.consumer == 0 || b.producer == b.consumer) return false; const auto producer = archive_.organism(b.producer); const auto consumer = archive_.organism(b.consumer); if (!producer || !consumer) return false; const auto progress = community_->progress(); const Tick now{progress.tick}; // Terminal lifecycle and archive death agree, and neither is in the future. for (const auto& [record, terminal] : {std::pair{*producer, progress.producer_terminal}, std::pair{*consumer, progress.consumer_terminal}}) { if (terminal > 1 || record.alive != (terminal == 0) || record.born > now || (!record.alive && record.died > now)) return false; } // No living organism exists outside this region's accounts. std::array expected{}; std::size_t count = 0; if (producer->alive) expected[count++] = b.producer; if (consumer->alive) expected[count++] = b.consumer; if (count == 2 && expected[0] > expected[1]) std::swap(expected[0], expected[1]); const auto living = archive_.living_organisms(); return std::ranges::equal(living, std::span{expected}.first(count)) && b.next_organism > archive_.highest_organism() && b.next_genome > archive_.highest_genome(); } std::expected, LineageRegionError> LineageRegion::create(std::unique_ptr community, const biology::LineageArchive& archive, LineageBinding binding) { if (!community) return std::unexpected{LineageRegionError::community}; std::unique_ptr region( new LineageRegion(std::move(community), archive, binding)); if (!region->closed()) return std::unexpected{LineageRegionError::closure}; return region; } std::expected LineageRegion::advance(std::uint64_t tick, const EnvironmentalInput& input) { if (poisoned_) return std::unexpected{LineageRegionError::poisoned}; auto report = community_->advance(tick, input); if (!report) return std::unexpected{LineageRegionError::community}; // Closure guarantees both organisms are alive until their transition and born // no later than the previous tick, so these deaths cannot be refused. const std::pair deaths[] = { {report->terminal_transition, binding_.producer}, {report->consumer_transition, binding_.consumer}}; for (const auto& [transition, organism] : deaths) if (transition == 1 && !archive_.record_death(organism, Tick{tick})) { poisoned_ = true; return std::unexpected{LineageRegionError::poisoned}; } return *report; } std::expected LineageRegion::snapshot() const { if (poisoned_) return std::unexpected{LineageRegionError::poisoned}; auto envelope = community_->snapshot(); if (!envelope) return std::unexpected{LineageRegionError::community}; const auto archive = archive_.encode(); world::WorldBytes out(region_header_bytes + envelope->size() + 8 + archive.size() + 8); Cursor w{out}; w.put(region_magic, 8); w.put(lineage_region_format, 4); w.put(0, 4); w.put(binding_.producer, 8); w.put(binding_.consumer, 8); w.put(binding_.next_organism, 8); w.put(binding_.next_genome, 8); w.put(envelope->size(), 8); w.bytes(*envelope); w.put(archive.size(), 8); w.bytes(archive); w.put(fnv(std::span{out}.first(w.at())), 8); if (w.at() != out.size()) return std::unexpected{LineageRegionError::format}; return out; } std::expected, LineageRegionError> LineageRegion::restore(std::span bytes, world::WorldIdentity identity, std::size_t budget) { if (bytes.size() < region_header_bytes + 24 || bytes.size() > max_lineage_region_bytes || get(bytes, 0, 8) != region_magic) return std::unexpected{LineageRegionError::format}; if (get(bytes, 8, 4) != lineage_region_format) return std::unexpected{LineageRegionError::version}; if (get(bytes, 12, 4) != 0) return std::unexpected{LineageRegionError::format}; const auto body = bytes.first(bytes.size() - 8); if (get(bytes, bytes.size() - 8, 8) != fnv(body)) return std::unexpected{LineageRegionError::checksum}; const LineageBinding binding{get(bytes, 16, 8), get(bytes, 24, 8), get(bytes, 32, 8), get(bytes, 40, 8)}; // Lengths are checked against the remaining bytes before any sub-span is taken. const auto envelope_bytes = get(bytes, 48, 8); if (envelope_bytes == 0 || envelope_bytes > EnvironmentalCommunity::max_snapshot_bytes || envelope_bytes > body.size() - region_header_bytes - 8) return std::unexpected{LineageRegionError::format}; const auto envelope = body.subspan(region_header_bytes, static_cast(envelope_bytes)); const auto after = region_header_bytes + envelope.size(); const auto archive_bytes = get(body, after, 8); if (archive_bytes != body.size() - after - 8) return std::unexpected{LineageRegionError::format}; auto community = EnvironmentalCommunity::restore(envelope, identity, budget); if (!community) return std::unexpected{LineageRegionError::community}; auto archive = biology::LineageArchive::decode(body.subspan(after + 8)); if (!archive) return std::unexpected{LineageRegionError::archive}; auto region = create(std::move(*community), *archive, binding); if (!region) return std::unexpected{region.error()}; const auto again = (*region)->snapshot(); if (!again || !std::ranges::equal(*again, bytes)) return std::unexpected{LineageRegionError::format}; return std::move(*region); } } // namespace evo::processes