Add bounded funded-birth batch replay (M11.2c-2a)
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m53s
Foundation / checks (checks) (push) Failing after 4m11s
Foundation / checks (tsan) (push) Canceled after 0s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 4m28s
Foundation / checks (network) (push) Canceled after 6m39s
Foundation / checks (client) (push) Canceled after 8m43s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m53s
Foundation / checks (checks) (push) Failing after 4m11s
Foundation / checks (tsan) (push) Canceled after 0s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 4m28s
Foundation / checks (network) (push) Canceled after 6m39s
Foundation / checks (client) (push) Canceled after 8m43s
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
#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::vector<std::uint8_t>& out, std::uint64_t value, unsigned width) {
|
||||
for (unsigned i = 0; i < width; ++i) {
|
||||
out.push_back(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;
|
||||
out.reserve(primitive_batch_header_bytes + batch.births.size() * primitive_birth_bytes);
|
||||
put(out, magic, 8);
|
||||
put(out, primitive_batch_format, 4);
|
||||
put(out, batch.births.size(), 4);
|
||||
const auto& h = batch.header;
|
||||
for (const auto v : {h.world, h.region, h.frame})
|
||||
put(out, v, 8);
|
||||
out.insert(out.end(), h.predecessor.begin(), h.predecessor.end());
|
||||
out.insert(out.end(), h.predecessor_digest.begin(), h.predecessor_digest.end());
|
||||
put(out, h.begin_tick, 8);
|
||||
put(out, h.end_tick, 8);
|
||||
put(out, 0, 8);
|
||||
for (const auto& b : batch.births) {
|
||||
out.insert(out.end(), b.request.account.begin(), b.request.account.end());
|
||||
for (const auto v : {b.request.action, b.identity, b.generation, b.born.value})
|
||||
put(out, 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
|
||||
Reference in New Issue
Block a user