Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m59s
Foundation / checks (client) (push) Successful in 12m24s
Foundation / checks (network) (push) Successful in 13m51s
Foundation / checks (tsan) (push) Successful in 8m7s
Foundation / checks (checks) (push) Successful in 33m53s
Foundation / checks (postgres) (push) Successful in 46m23s
Foundation / server-image (push) Successful in 24s
Foundation / postgres-tools (push) Successful in 1m36s
Foundation / image (push) Successful in 2m45s
155 lines
5.8 KiB
C++
155 lines
5.8 KiB
C++
#include "evo/server/lifecycle/primitive_batch.hpp"
|
|
#include <algorithm>
|
|
#include <tuple>
|
|
|
|
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<std::uint8_t> out, std::size_t& at, std::uint64_t value, unsigned width) {
|
|
for (unsigned i = 0; i < width; ++i) {
|
|
out[at++] = static_cast<std::uint8_t>(value & 255U);
|
|
value >>= 8U;
|
|
}
|
|
}
|
|
class Reader {
|
|
public:
|
|
explicit Reader(std::span<const std::uint8_t> 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::size_t N> std::array<std::uint8_t, N> array() {
|
|
std::array<std::uint8_t, N> out{};
|
|
for (auto& b : out)
|
|
b = bytes_[at_++];
|
|
return out;
|
|
}
|
|
|
|
private:
|
|
std::span<const std::uint8_t> bytes_;
|
|
std::size_t at_{};
|
|
};
|
|
} // namespace
|
|
|
|
std::expected<std::vector<std::uint8_t>, PrimitiveBatchError>
|
|
encode_primitive_batch(const PrimitiveBatch& batch) {
|
|
if (!valid(batch))
|
|
return std::unexpected{PrimitiveBatchError::format};
|
|
std::vector<std::uint8_t> 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<PrimitiveBatch, PrimitiveBatchError>
|
|
decode_primitive_batch(std::span<const std::uint8_t> 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<std::size_t>(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<std::unique_ptr<PrimitiveLives>, 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
|