#include "evo/server/lifecycle/primitive_batch.hpp" #include #include namespace evo::server { namespace { constexpr std::uint64_t magic = 0x31425446494C5645ULL; bool zero(const auto& bytes) { return std::ranges::all_of(bytes, [](auto b) { return b == 0; }); } bool valid(const PrimitiveBatch& batch) { const auto& h = batch.header; if (h.world == 0 || h.region == 0 || h.frame == 0 || zero(h.predecessor) || zero(h.predecessor_digest) || h.end_tick <= h.begin_tick || h.end_tick - h.begin_tick > 64 || batch.births.size() > max_primitive_lives) return false; for (std::size_t i = 0; i < batch.births.size(); ++i) { const auto& b = batch.births[i]; if (zero(b.request.account) || b.request.action == 0 || b.identity == 0 || b.generation == 0 || b.born.value <= h.begin_tick || b.born.value > h.end_tick) return false; if (i > 0) { const auto& p = batch.births[i - 1]; if (std::tie(p.born.value, p.request.account, p.request.action) >= std::tie(b.born.value, b.request.account, b.request.action)) return false; } for (std::size_t j = 0; j < i; ++j) if (batch.births[j].request.account == b.request.account || batch.births[j].identity == b.identity) return false; } return true; } void put(std::span out, std::size_t& at, std::uint64_t value, unsigned width) { for (unsigned i = 0; i < width; ++i) { out[at++] = static_cast(value & 255U); value >>= 8U; } } class Reader { public: explicit Reader(std::span bytes) : bytes_(bytes) {} std::uint64_t get(unsigned width) { std::uint64_t value{}; for (unsigned i = 0; i < width; ++i) value |= std::uint64_t{bytes_[at_++]} << (8U * i); return value; } template std::array array() { std::array out{}; for (auto& b : out) b = bytes_[at_++]; return out; } private: std::span bytes_; std::size_t at_{}; }; } // namespace std::expected, PrimitiveBatchError> encode_primitive_batch(const PrimitiveBatch& batch) { if (!valid(batch)) return std::unexpected{PrimitiveBatchError::format}; std::vector out(primitive_batch_header_bytes + batch.births.size() * primitive_birth_bytes); std::size_t at{}; const auto array = [&](const auto& bytes) { for (const auto b : bytes) out[at++] = b; }; put(out, at, magic, 8); put(out, at, primitive_batch_format, 4); put(out, at, batch.births.size(), 4); const auto& h = batch.header; for (const auto v : {h.world, h.region, h.frame}) put(out, at, v, 8); array(h.predecessor); array(h.predecessor_digest); put(out, at, h.begin_tick, 8); put(out, at, h.end_tick, 8); put(out, at, 0, 8); for (const auto& b : batch.births) { array(b.request.account); for (const auto v : {b.request.action, b.identity, b.generation, b.born.value}) put(out, at, v, 8); } return out; } std::expected decode_primitive_batch(std::span bytes) { if (bytes.size() < primitive_batch_header_bytes || bytes.size() > max_primitive_batch_bytes) return std::unexpected{PrimitiveBatchError::format}; Reader r{bytes}; if (r.get(8) != magic) return std::unexpected{PrimitiveBatchError::format}; if (r.get(4) != primitive_batch_format) return std::unexpected{PrimitiveBatchError::version}; const auto count = r.get(4); if (count > max_primitive_lives || bytes.size() != primitive_batch_header_bytes + count * primitive_birth_bytes) return std::unexpected{PrimitiveBatchError::format}; PrimitiveBatch out; auto& h = out.header; h.world = r.get(8); h.region = r.get(8); h.frame = r.get(8); h.predecessor = r.array<16>(); h.predecessor_digest = r.array<32>(); h.begin_tick = r.get(8); h.end_tick = r.get(8); if (r.get(8) != 0) return std::unexpected{PrimitiveBatchError::format}; out.births.reserve(static_cast(count)); for (std::uint64_t i = 0; i < count; ++i) { PrimitiveBirth b; b.request.account = r.array<16>(); b.request.action = r.get(8); b.identity = r.get(8); b.generation = r.get(8); b.born.value = r.get(8); out.births.push_back(b); } if (!valid(out)) return std::unexpected{PrimitiveBatchError::format}; return out; } std::expected, PrimitiveBatchError> replay_primitive_batch(const PrimitiveLives& base, const PrimitiveBatch& batch) { if (!valid(batch)) return std::unexpected{PrimitiveBatchError::format}; const auto& c = base.config(); const auto& h = batch.header; if (c.world != h.world || c.region != h.region || c.frame != h.frame || std::ranges::any_of(base.lives(), [&](const auto& life) { return life.identity.born.value > h.begin_tick; })) return std::unexpected{PrimitiveBatchError::scope}; auto candidate = PrimitiveLives::restore(base.snapshot(), c.world, c.region, c.frame); if (!candidate) return std::unexpected{PrimitiveBatchError::state}; for (const auto& b : batch.births) { const LifeAllocation allocation{b.identity, b.identity, b.identity, b.identity, b.born}; const auto created = (*candidate)->spawn(b.request, allocation); if (!created || created->identity != allocation) return std::unexpected{PrimitiveBatchError::state}; } return std::move(*candidate); } } // namespace evo::server