Add cellular replay trace and recovery lookup
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (checks) (push) Failing after 41s
Foundation / checks (postgres) (push) Canceled after 0s
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 (client) (push) Canceled after 1m46s
Foundation / checks (network) (push) Canceled after 1m9s
Foundation / checks (ecs) (push) Canceled after 1m38s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (checks) (push) Failing after 41s
Foundation / checks (postgres) (push) Canceled after 0s
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 (client) (push) Canceled after 1m46s
Foundation / checks (network) (push) Canceled after 1m9s
Foundation / checks (ecs) (push) Canceled after 1m38s
This commit is contained in:
@@ -0,0 +1,184 @@
|
||||
#include "evo/server/lifecycle/cellular_batch.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <bit>
|
||||
#include <ranges>
|
||||
#include <utility>
|
||||
|
||||
namespace evo::server {
|
||||
namespace {
|
||||
constexpr std::array<std::uint8_t, 8> magic{'E', 'V', 'L', 'C', 'B', 'T', '0', '1'};
|
||||
constexpr std::int16_t movement_limit = 1'000;
|
||||
|
||||
void put(std::vector<std::uint8_t>& out, std::uint64_t value, std::size_t width) {
|
||||
for (std::size_t i = 0; i < width; ++i)
|
||||
out.push_back(static_cast<std::uint8_t>(value >> (i * 8U)));
|
||||
}
|
||||
std::uint64_t get(std::span<const std::uint8_t> bytes, std::size_t offset, std::size_t width) {
|
||||
std::uint64_t value{};
|
||||
for (std::size_t i = 0; i < width; ++i)
|
||||
value |= static_cast<std::uint64_t>(bytes[offset + i]) << (i * 8U);
|
||||
return value;
|
||||
}
|
||||
bool all_zero(std::span<const std::uint8_t> bytes) {
|
||||
return std::ranges::all_of(bytes, [](std::uint8_t byte) { return byte == 0; });
|
||||
}
|
||||
bool valid_header(const CellularBatchHeader& h, std::size_t ticks) {
|
||||
return h.world != 0 && h.region != 0 && h.frame != 0 && !all_zero(h.predecessor) &&
|
||||
!all_zero(h.predecessor_digest) && h.end_tick > h.begin_tick &&
|
||||
h.end_tick - h.begin_tick == ticks && ticks > 0 &&
|
||||
ticks <= max_cellular_batch_ticks;
|
||||
}
|
||||
bool valid_intent(const CellularLifeIntent& life) {
|
||||
const auto& step = life.intent;
|
||||
return life.life_id != 0 && step.movement.x >= -movement_limit &&
|
||||
step.movement.x <= movement_limit && step.movement.y >= -movement_limit &&
|
||||
step.movement.y <= movement_limit;
|
||||
}
|
||||
bool valid_ticks(std::span<const CellularTickIntents> ticks) {
|
||||
for (const auto& tick : ticks) {
|
||||
if (tick.lives.size() > biology::max_cellular_tick_lives)
|
||||
return false;
|
||||
for (std::size_t i = 0; i < tick.lives.size(); ++i) {
|
||||
const auto& life = tick.lives[i];
|
||||
if (!valid_intent(life) ||
|
||||
(i > 0 && tick.lives[i - 1].life_id >= life.life_id))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void put_intent(std::vector<std::uint8_t>& out, const CellularLifeIntent& life) {
|
||||
put(out, life.life_id, 8);
|
||||
put(out, static_cast<std::uint16_t>(life.intent.movement.x), 2);
|
||||
put(out, static_cast<std::uint16_t>(life.intent.movement.y), 2);
|
||||
out.push_back(life.intent.intake_target ? 1 : 0);
|
||||
put(out, 0, 3);
|
||||
put(out, life.intent.intake_target ? life.intent.intake_target->x_um : 0, 4);
|
||||
put(out, life.intent.intake_target ? life.intent.intake_target->y_um : 0, 4);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<std::vector<std::uint8_t>, CellularBatchError>
|
||||
encode_cellular_batch(const CellularBatch& batch) {
|
||||
if (!valid_header(batch.header, batch.ticks.size()) || !valid_ticks(batch.ticks))
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
std::size_t size = cellular_batch_header_bytes;
|
||||
for (const auto& tick : batch.ticks) {
|
||||
const auto records = tick.lives.size() * cellular_batch_intent_bytes;
|
||||
if (records > max_cellular_batch_bytes - size - cellular_batch_tick_header_bytes)
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
size += cellular_batch_tick_header_bytes + records;
|
||||
}
|
||||
std::vector<std::uint8_t> out;
|
||||
out.reserve(size);
|
||||
out.insert(out.end(), magic.begin(), magic.end());
|
||||
put(out, cellular_batch_format, 4);
|
||||
put(out, 4, 4); // Profile 4.
|
||||
put(out, batch.header.world, 8);
|
||||
put(out, batch.header.region, 8);
|
||||
put(out, batch.header.frame, 8);
|
||||
out.insert(out.end(), batch.header.predecessor.begin(), batch.header.predecessor.end());
|
||||
out.insert(out.end(), batch.header.predecessor_digest.begin(),
|
||||
batch.header.predecessor_digest.end());
|
||||
put(out, batch.header.begin_tick, 8);
|
||||
put(out, batch.header.end_tick, 8);
|
||||
put(out, batch.ticks.size(), 4);
|
||||
put(out, 0, 4);
|
||||
for (const auto& tick : batch.ticks) {
|
||||
put(out, tick.lives.size(), 4);
|
||||
for (const auto& life : tick.lives)
|
||||
put_intent(out, life);
|
||||
}
|
||||
if (out.size() != size)
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
return out;
|
||||
}
|
||||
|
||||
std::expected<CellularBatch, CellularBatchError>
|
||||
decode_cellular_batch(std::span<const std::uint8_t> bytes) {
|
||||
if (bytes.size() < cellular_batch_header_bytes || bytes.size() > max_cellular_batch_bytes ||
|
||||
!std::ranges::equal(bytes.first(magic.size()), magic))
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
if (get(bytes, 8, 4) != cellular_batch_format || get(bytes, 12, 4) != 4)
|
||||
return std::unexpected{CellularBatchError::version};
|
||||
if (get(bytes, 108, 4) != 0)
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
|
||||
CellularBatch out;
|
||||
auto& h = out.header;
|
||||
h.world = get(bytes, 16, 8);
|
||||
h.region = get(bytes, 24, 8);
|
||||
h.frame = get(bytes, 32, 8);
|
||||
std::ranges::copy(bytes.subspan(40, 16), h.predecessor.begin());
|
||||
std::ranges::copy(bytes.subspan(56, 32), h.predecessor_digest.begin());
|
||||
h.begin_tick = get(bytes, 88, 8);
|
||||
h.end_tick = get(bytes, 96, 8);
|
||||
const auto tick_count = get(bytes, 104, 4);
|
||||
if (tick_count > max_cellular_batch_ticks ||
|
||||
!valid_header(h, static_cast<std::size_t>(tick_count)))
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
|
||||
// Bound all per-tick allocation by checking each encoded count against the
|
||||
// remaining bytes before resizing the tick or intent vectors.
|
||||
std::size_t offset = cellular_batch_header_bytes;
|
||||
out.ticks.reserve(static_cast<std::size_t>(tick_count));
|
||||
for (std::uint64_t tick_index = 0; tick_index < tick_count; ++tick_index) {
|
||||
if (bytes.size() - offset < cellular_batch_tick_header_bytes)
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
const auto count = get(bytes, offset, 4);
|
||||
offset += cellular_batch_tick_header_bytes;
|
||||
if (count > biology::max_cellular_tick_lives ||
|
||||
count > (bytes.size() - offset) / cellular_batch_intent_bytes)
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
CellularTickIntents step;
|
||||
step.lives.reserve(static_cast<std::size_t>(count));
|
||||
for (std::uint64_t i = 0; i < count; ++i) {
|
||||
const auto record = bytes.subspan(offset, cellular_batch_intent_bytes);
|
||||
if (record[12] > 1 || !all_zero(record.subspan(13, 3)))
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
CellularLifeIntent life;
|
||||
life.life_id = get(record, 0, 8);
|
||||
life.intent.movement.x = std::bit_cast<std::int16_t>(
|
||||
static_cast<std::uint16_t>(get(record, 8, 2)));
|
||||
life.intent.movement.y = std::bit_cast<std::int16_t>(
|
||||
static_cast<std::uint16_t>(get(record, 10, 2)));
|
||||
if (record[12] == 1) {
|
||||
life.intent.intake_target = biology::CellularPatchAddress{
|
||||
static_cast<std::uint32_t>(get(record, 16, 4)),
|
||||
static_cast<std::uint32_t>(get(record, 20, 4))};
|
||||
} else if (!all_zero(record.subspan(16, 8))) {
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
}
|
||||
if (!valid_intent(life) ||
|
||||
(!step.lives.empty() && step.lives.back().life_id >= life.life_id))
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
step.lives.push_back(life);
|
||||
offset += cellular_batch_intent_bytes;
|
||||
}
|
||||
out.ticks.push_back(std::move(step));
|
||||
}
|
||||
if (offset != bytes.size())
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
return out;
|
||||
}
|
||||
|
||||
std::expected<std::unique_ptr<CellularLives>, CellularBatchError>
|
||||
replay_cellular_batch(const CellularLives& base, const CellularBatch& batch) {
|
||||
const auto& h = batch.header;
|
||||
if (!valid_header(h, batch.ticks.size()) || !valid_ticks(batch.ticks))
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
if (base.world() != h.world || base.region() != h.region || base.frame() != h.frame ||
|
||||
base.tick() != h.begin_tick)
|
||||
return std::unexpected{CellularBatchError::scope};
|
||||
auto replay = CellularLives::restore(base.snapshot(), base.world(), base.region(), base.frame(),
|
||||
base.tick());
|
||||
if (!replay)
|
||||
return std::unexpected{CellularBatchError::state};
|
||||
for (const auto& tick : batch.ticks)
|
||||
if (!(*replay)->advance_tick(tick.lives))
|
||||
return std::unexpected{CellularBatchError::state};
|
||||
return std::move(*replay);
|
||||
}
|
||||
} // namespace evo::server
|
||||
Reference in New Issue
Block a user