Foundation / checks (ecs) (push) Successful in 2m26s
Foundation / checks (deploy) (push) Successful in 4m12s
Foundation / checks (network) (push) Successful in 20m57s
Foundation / checks (client) (push) Successful in 22m9s
Foundation / checks (tsan) (push) Successful in 28m49s
Foundation / checks (postgres) (push) Successful in 1h5m2s
Foundation / checks (checks) (push) Successful in 1h17m42s
Foundation / postgres-tools (push) Successful in 1m10s
Foundation / server-image (push) Successful in 3m51s
Foundation / image (push) Successful in 4m54s
224 lines
11 KiB
C++
224 lines
11 KiB
C++
#include "evo/server/lifecycle/cellular_migration_trace.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <utility>
|
|
|
|
namespace evo::server {
|
|
namespace {
|
|
constexpr std::array<std::uint8_t, 8> magic{'A', 'E', 'V', 'M', 'I', 'G', '0', '1'};
|
|
constexpr std::array<std::uint8_t, 8> maturing_magic{'A', 'E', 'V', 'M', 'I', 'G', '0', '2'};
|
|
template <typename T> bool zero(const T& bytes) {
|
|
return std::ranges::all_of(bytes, [](auto byte) { return byte == 0; });
|
|
}
|
|
bool valid_request(const CellularMigrationRequest& request) {
|
|
const auto& s = request.source;
|
|
const auto& d = request.destination;
|
|
if (zero(request.request) || request.genome == 0 || s.region == 0 || d.region == 0 ||
|
|
s.region > cellular_bud_max_region || d.region > cellular_bud_max_region ||
|
|
s.region == d.region || zero(s.predecessor_digest) || zero(d.predecessor_digest))
|
|
return false;
|
|
auto ids = std::array{s.predecessor, d.predecessor, s.commit, d.commit};
|
|
if (std::ranges::any_of(ids, [](const auto& id) { return zero(id); }))
|
|
return false;
|
|
std::ranges::sort(ids);
|
|
return std::ranges::adjacent_find(ids) == ids.end();
|
|
}
|
|
bool shape(const CellularMigrationTrace& trace) {
|
|
if ((trace.format_version != 1 && trace.format_version != 2) ||
|
|
(trace.representation != CellularMigrationRepresentation::individual &&
|
|
trace.representation != CellularMigrationRepresentation::population) ||
|
|
trace.world == 0 || trace.frame == 0 || trace.tick == 0 || !valid_request(trace.request) ||
|
|
trace.transferred == accounting::Inventory{} || trace.members.empty() ||
|
|
trace.members.size() > biology::max_cellular_tick_lives ||
|
|
!std::ranges::is_sorted(trace.members) ||
|
|
std::ranges::adjacent_find(trace.members) != trace.members.end())
|
|
return false;
|
|
return std::ranges::all_of(trace.members, [](auto id) {
|
|
return id >= cellular_bud_namespace && ((id >> 32U) & cellular_bud_max_region) != 0 &&
|
|
(id & 0xFFFF'FFFFULL) != 0;
|
|
});
|
|
}
|
|
const CellularLives* state(const CellularLives& owner) {
|
|
return &owner;
|
|
}
|
|
const CellularLives* state(const CellularPopulation& owner) {
|
|
return &owner.state();
|
|
}
|
|
template <typename Recorded, typename Owner>
|
|
std::expected<Recorded, CellularMigrationError>
|
|
record(const Owner& source, const Owner& destination, const CellularMigrationRequest& request,
|
|
CellularMigrationRepresentation representation) {
|
|
if (!valid_request(request) || request.source.region != state(source)->region() ||
|
|
request.destination.region != state(destination)->region())
|
|
return std::unexpected(CellularMigrationError::identity);
|
|
auto execution = CellularMigration::prepare(source, destination, request.genome);
|
|
if (!execution)
|
|
return std::unexpected(execution.error());
|
|
CellularMigrationTrace trace{
|
|
representation, state(source)->world(), state(source)->frame(), state(source)->tick(),
|
|
request, execution->transferred, execution->members};
|
|
trace.format_version =
|
|
state(source)->rules_version() == cellular_lives_maturation_rules ? 2 : 1;
|
|
if (!shape(trace))
|
|
return std::unexpected(CellularMigrationError::format);
|
|
return Recorded{std::move(trace), std::move(*execution)};
|
|
}
|
|
template <typename Owner>
|
|
auto replay(const Owner& source, const Owner& destination, const CellularMigrationRequest& trusted,
|
|
const CellularMigrationTrace& trace, CellularMigrationRepresentation representation)
|
|
-> decltype(CellularMigration::prepare(source, destination, trusted.genome)) {
|
|
if (!shape(trace))
|
|
return std::unexpected(CellularMigrationError::format);
|
|
const auto version =
|
|
state(source)->rules_version() == cellular_lives_maturation_rules ? 2U : 1U;
|
|
if (trace.format_version != version)
|
|
return std::unexpected(CellularMigrationError::version);
|
|
if (trace.request != trusted || trace.representation != representation ||
|
|
trace.world != state(source)->world() || trace.frame != state(source)->frame() ||
|
|
trace.tick != state(source)->tick() || trusted.source.region != state(source)->region() ||
|
|
trusted.destination.region != state(destination)->region())
|
|
return std::unexpected(CellularMigrationError::identity);
|
|
auto execution = CellularMigration::prepare(source, destination, trusted.genome);
|
|
if (!execution)
|
|
return std::unexpected(execution.error());
|
|
if (execution->members != trace.members)
|
|
return std::unexpected(CellularMigrationError::identity);
|
|
if (execution->transferred != trace.transferred)
|
|
return std::unexpected(CellularMigrationError::conservation);
|
|
return execution;
|
|
}
|
|
std::uint64_t checksum(std::span<const std::uint8_t> bytes) {
|
|
std::uint64_t value = 14695981039346656037ULL;
|
|
for (const auto byte : bytes)
|
|
value = (value ^ byte) * 1099511628211ULL;
|
|
return value;
|
|
}
|
|
void put(std::vector<std::uint8_t>& out, std::uint64_t value, unsigned width = 8) {
|
|
for (unsigned i = 0; i < width; ++i)
|
|
out.push_back(static_cast<std::uint8_t>(value >> (8U * i)));
|
|
}
|
|
template <typename T> void append(std::vector<std::uint8_t>& out, const T& bytes) {
|
|
out.insert(out.end(), bytes.begin(), bytes.end());
|
|
}
|
|
class Reader {
|
|
public:
|
|
explicit Reader(std::span<const std::uint8_t> bytes) : bytes_(bytes) {}
|
|
std::uint64_t get(unsigned width = 8) {
|
|
std::uint64_t value = 0;
|
|
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> result{};
|
|
for (auto& byte : result)
|
|
byte = static_cast<std::uint8_t>(get(1));
|
|
return result;
|
|
}
|
|
|
|
private:
|
|
std::span<const std::uint8_t> bytes_;
|
|
std::size_t at_{};
|
|
};
|
|
} // namespace
|
|
|
|
std::expected<RecordedCellularMigration, CellularMigrationError>
|
|
prepare_migration_trace(const CellularLives& source, const CellularLives& destination,
|
|
const CellularMigrationRequest& request) {
|
|
return record<RecordedCellularMigration>(source, destination, request,
|
|
CellularMigrationRepresentation::individual);
|
|
}
|
|
std::expected<RecordedPopulationMigration, CellularMigrationError>
|
|
prepare_migration_trace(const CellularPopulation& source, const CellularPopulation& destination,
|
|
const CellularMigrationRequest& request) {
|
|
return record<RecordedPopulationMigration>(source, destination, request,
|
|
CellularMigrationRepresentation::population);
|
|
}
|
|
std::expected<CellularMigrationExecution, CellularMigrationError>
|
|
replay_migration_trace(const CellularLives& source, const CellularLives& destination,
|
|
const CellularMigrationRequest& trusted,
|
|
const CellularMigrationTrace& trace) {
|
|
return replay(source, destination, trusted, trace, CellularMigrationRepresentation::individual);
|
|
}
|
|
std::expected<CellularPopulationMigrationExecution, CellularMigrationError>
|
|
replay_migration_trace(const CellularPopulation& source, const CellularPopulation& destination,
|
|
const CellularMigrationRequest& trusted,
|
|
const CellularMigrationTrace& trace) {
|
|
return replay(source, destination, trusted, trace, CellularMigrationRepresentation::population);
|
|
}
|
|
std::expected<std::vector<std::uint8_t>, CellularMigrationError>
|
|
encode_migration_trace(const CellularMigrationTrace& trace) {
|
|
if (!shape(trace))
|
|
return std::unexpected(CellularMigrationError::format);
|
|
std::vector<std::uint8_t> bytes;
|
|
bytes.reserve(migration_trace_header_bytes + 8 * trace.members.size() + 8);
|
|
append(bytes, trace.format_version == 1 ? magic : maturing_magic);
|
|
put(bytes, trace.format_version, 4);
|
|
put(bytes, static_cast<std::uint32_t>(trace.representation), 4);
|
|
for (const auto value : {trace.world, trace.frame, trace.tick, trace.request.genome})
|
|
put(bytes, value);
|
|
append(bytes, trace.request.request);
|
|
for (const auto* endpoint : {&trace.request.source, &trace.request.destination}) {
|
|
put(bytes, endpoint->region);
|
|
append(bytes, endpoint->predecessor);
|
|
append(bytes, endpoint->predecessor_digest);
|
|
append(bytes, endpoint->commit);
|
|
}
|
|
put(bytes, trace.transferred.energy.value);
|
|
for (const auto mass : trace.transferred.matter)
|
|
put(bytes, mass.value);
|
|
put(bytes, trace.members.size(), 4);
|
|
put(bytes, 0, 4);
|
|
for (const auto id : trace.members)
|
|
put(bytes, id);
|
|
put(bytes, checksum(bytes));
|
|
return bytes;
|
|
}
|
|
std::expected<CellularMigrationTrace, CellularMigrationError>
|
|
decode_migration_trace(std::span<const std::uint8_t> bytes) {
|
|
if (bytes.size() < migration_trace_header_bytes + 8 || bytes.size() > max_migration_trace_bytes)
|
|
return std::unexpected(CellularMigrationError::format);
|
|
Reader end(bytes.last(8));
|
|
if (end.get() != checksum(bytes.first(bytes.size() - 8)))
|
|
return std::unexpected(CellularMigrationError::checksum);
|
|
Reader in(bytes);
|
|
const auto tag = in.array<8>();
|
|
if (tag != magic && tag != maturing_magic)
|
|
return std::unexpected(CellularMigrationError::format);
|
|
const auto version = in.get(4);
|
|
if ((version != 1 && version != 2) || (version == 1 ? tag != magic : tag != maturing_magic))
|
|
return std::unexpected(CellularMigrationError::version);
|
|
const auto representation = in.get(4);
|
|
if (representation != 1 && representation != 2)
|
|
return std::unexpected(CellularMigrationError::version);
|
|
CellularMigrationTrace trace;
|
|
trace.format_version = static_cast<std::uint32_t>(version);
|
|
trace.representation = static_cast<CellularMigrationRepresentation>(representation);
|
|
trace.world = in.get();
|
|
trace.frame = in.get();
|
|
trace.tick = in.get();
|
|
trace.request.genome = in.get();
|
|
trace.request.request = in.array<16>();
|
|
for (auto* endpoint : {&trace.request.source, &trace.request.destination}) {
|
|
endpoint->region = in.get();
|
|
endpoint->predecessor = in.array<16>();
|
|
endpoint->predecessor_digest = in.array<32>();
|
|
endpoint->commit = in.array<16>();
|
|
}
|
|
trace.transferred.energy.value = in.get();
|
|
for (auto& mass : trace.transferred.matter)
|
|
mass.value = in.get();
|
|
const auto count = in.get(4);
|
|
if (in.get(4) != 0 || count == 0 || count > biology::max_cellular_tick_lives ||
|
|
bytes.size() != migration_trace_header_bytes + 8 * count + 8)
|
|
return std::unexpected(CellularMigrationError::format);
|
|
trace.members.reserve(static_cast<std::size_t>(count));
|
|
for (std::uint64_t i = 0; i < count; ++i)
|
|
trace.members.push_back(in.get());
|
|
const auto encoded = encode_migration_trace(trace);
|
|
if (!encoded || !std::ranges::equal(*encoded, bytes))
|
|
return std::unexpected(CellularMigrationError::format);
|
|
return trace;
|
|
}
|
|
} // namespace evo::server
|