#include "evo/persistence/region_batch.hpp" #include #include #include namespace evo::persistence { namespace { constexpr std::uint64_t trace_magic = 0x314352544F564541ULL; // "AEVOTRC1" constexpr std::uint32_t trace_format = 1; constexpr std::uint64_t max_trace_ticks = 64; template bool zero(const std::array& bytes) { return std::ranges::all_of(bytes, [](std::uint8_t b) { return b == 0; }); } // Fills an exactly sized buffer; callers check the final cursor. class Writer { public: explicit Writer(std::vector& out) : out_(out) {} void u(std::uint64_t value, unsigned width) { for (unsigned i = 0; i < width; ++i, value >>= 8U) out_[at_++] = static_cast(value & 0xFFU); } template void bytes(const std::array& value) { std::ranges::copy(value, out_.begin() + static_cast(at_)); at_ += N; } template void all(const std::array& values) { for (const auto v : values) u(v, 8); } [[nodiscard]] std::size_t at() const noexcept { return at_; } private: std::vector& out_; std::size_t at_{}; }; class Reader { public: explicit Reader(std::span in) : in_(in) {} std::uint64_t u(unsigned width) { std::uint64_t value{}; for (unsigned i = 0; i < width; ++i) value |= static_cast(in_[at_ + i]) << (8U * i); at_ += width; return value; } template std::array bytes() { std::array value{}; std::copy_n(in_.begin() + static_cast(at_), N, value.begin()); at_ += N; return value; } template void all(std::array& values) { for (auto& v : values) v = u(8); } private: std::span in_; std::size_t at_{}; }; bool valid(const TraceHeader& h) noexcept { return h.profile != 0 && h.key.world != 0 && h.key.region != 0 && !zero(h.predecessor_commit) && !zero(h.predecessor_digest) && h.end_tick > h.begin_tick && h.end_tick - h.begin_tick <= max_trace_ticks; } bool contiguous(const TraceHeader& h, std::span steps) noexcept { if (steps.size() != h.end_tick - h.begin_tick) return false; for (std::size_t i = 0; i < steps.size(); ++i) if (steps[i].report.tick != h.begin_tick + i + 1) return false; return true; } } // namespace std::expected, BatchError> encode_trace(const TraceHeader& h, std::span steps) { if (!valid(h) || !contiguous(h, steps)) return std::unexpected{BatchError::format}; std::vector out(trace_bytes_for(steps.size())); Writer w{out}; w.u(trace_magic, 8); w.u(trace_format, 4); w.u(h.profile, 4); w.u(h.key.world, 8); w.u(h.key.region, 8); w.bytes(h.predecessor_commit); w.bytes(h.predecessor_digest); w.u(h.begin_tick, 8); w.u(h.end_tick, 8); for (const auto& s : steps) { const auto& in = s.input; const auto& r = s.report; w.u(in.temperature_mK, 8); w.u(in.source_enabled, 8); w.all(in.bite); w.all(in.terminal); for (const auto v : {r.tick, r.captured, r.retained, r.capture_heat, r.grown, r.unmet, r.source_status}) w.u(v, 8); w.all(r.assimilated); w.all(r.waste); w.u(r.terminal_transition, 8); w.u(r.consumer_transition, 8); w.all(r.decomposed); } if (w.at() != out.size()) return std::unexpected{BatchError::format}; return out; } std::expected decode_trace(std::span bytes) { if (bytes.size() < trace_header_bytes) return std::unexpected{BatchError::format}; Reader r{bytes}; if (r.u(8) != trace_magic || r.u(4) != trace_format) return std::unexpected{BatchError::format}; DecodedTrace t; auto& h = t.header; h.profile = static_cast(r.u(4)); h.key.world = r.u(8); h.key.region = r.u(8); h.predecessor_commit = r.bytes<16>(); h.predecessor_digest = r.bytes<32>(); h.begin_tick = r.u(8); h.end_tick = r.u(8); // Size is fixed by the header before any step is allocated. if (!valid(h) || bytes.size() != trace_bytes_for(h.end_tick - h.begin_tick)) return std::unexpected{BatchError::format}; t.steps.resize(static_cast(h.end_tick - h.begin_tick)); for (auto& s : t.steps) { auto& in = s.input; auto& rep = s.report; in.temperature_mK = r.u(8); in.source_enabled = r.u(8); r.all(in.bite); r.all(in.terminal); for (auto* v : {&rep.tick, &rep.captured, &rep.retained, &rep.capture_heat, &rep.grown, &rep.unmet, &rep.source_status}) *v = r.u(8); r.all(rep.assimilated); r.all(rep.waste); rep.terminal_transition = r.u(8); rep.consumer_transition = r.u(8); r.all(rep.decomposed); } if (!contiguous(h, t.steps)) return std::unexpected{BatchError::format}; return t; } namespace { bool valid(std::span d, std::uint64_t begin, std::uint64_t end) noexcept { if (d.size() > max_batch_decisions) return false; for (std::size_t i = 0; i < d.size(); ++i) { const auto& x = d[i]; if (x.outcome != Outcome::death || (x.actor != Actor::producer && x.actor != Actor::consumer) || x.tick <= begin || x.tick > end) return false; for (std::size_t j = 0; j < i; ++j) if (d[j].actor == x.actor) return false; // Terminal: at most one death per actor. if (i > 0 && (d[i - 1].tick > x.tick || (d[i - 1].tick == x.tick && d[i - 1].actor >= x.actor))) return false; // Canonical order (tick, actor). } return true; } } // namespace std::expected, BatchError> encode_decisions(std::span decisions, std::uint64_t begin_tick, std::uint64_t end_tick) { if (!valid(decisions, begin_tick, end_tick)) return std::unexpected{BatchError::format}; std::vector out(decisions.size() * decision_bytes); Writer w{out}; for (const auto& d : decisions) { w.u(d.tick, 8); w.u(static_cast(d.actor), 4); w.u(static_cast(d.outcome), 4); w.u(d.organism, 8); } return out; } std::expected, BatchError> decode_decisions(std::span bytes, std::uint64_t begin_tick, std::uint64_t end_tick) { if (bytes.size() % decision_bytes != 0 || bytes.size() > max_batch_decisions * decision_bytes) return std::unexpected{BatchError::format}; std::vector out(bytes.size() / decision_bytes); Reader r{bytes}; for (auto& d : out) { d.tick = r.u(8); const auto actor = r.u(4); const auto outcome = r.u(4); // Unknown values are refused, never truncated onto known ones. if (actor > 1 || outcome != 1) return std::unexpected{BatchError::format}; d.actor = static_cast(actor); d.outcome = Outcome::death; d.organism = r.u(8); } if (!valid(out, begin_tick, end_tick)) return std::unexpected{BatchError::format}; return out; } std::vector derive_decisions(std::span steps, std::uint64_t producer_organism, std::uint64_t consumer_organism) { std::vector out; for (const auto& s : steps) { if (s.report.terminal_transition == 1) out.push_back({s.report.tick, Actor::producer, Outcome::death, producer_organism}); if (s.report.consumer_transition == 1) out.push_back({s.report.tick, Actor::consumer, Outcome::death, consumer_organism}); } return out; } std::expected, BatchError> encode_actions(std::span actions, std::uint64_t begin_tick, std::uint64_t end_tick) { if (actions.size() > max_batch_actions) return std::unexpected{BatchError::format}; std::vector sorted(actions.begin(), actions.end()); const auto key = [](const ActionRecord& a) { return std::tuple{a.tick, a.account, a.action_id}; }; std::ranges::sort(sorted, {}, key); // An action id appears once, whatever its tick. std::vector> ids; ids.reserve(sorted.size()); for (const auto& a : sorted) ids.emplace_back(a.account, a.action_id); std::ranges::sort(ids); if (std::ranges::adjacent_find(ids) != ids.end()) return std::unexpected{BatchError::format}; std::vector out; out.reserve(sorted.size() * action_record_bytes); for (std::size_t i = 0; i < sorted.size(); ++i) { const auto& a = sorted[i]; if (a.account == Uuid{} || a.action_id == 0 || a.kind != 1 || a.tick <= begin_tick || a.tick > end_tick || (i > 0 && key(sorted[i - 1]) == key(a))) return std::unexpected{BatchError::format}; out.insert(out.end(), a.account.begin(), a.account.end()); for (unsigned b = 0; b < 8; ++b) out.push_back(static_cast(a.action_id >> (8U * b))); for (unsigned b = 0; b < 8; ++b) out.push_back(static_cast(a.tick >> (8U * b))); out.push_back(static_cast(a.kind)); out.push_back(static_cast(a.kind >> 8U)); } return out; } } // namespace evo::persistence