Bind cellular actions to replay traces
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (checks) (push) Failing after 38s
Foundation / checks (ecs) (push) Successful in 1m56s
Foundation / checks (postgres) (push) Failing after 52s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (network) (push) Canceled after 2m28s
Foundation / checks (tsan) (push) Canceled after 9s
Foundation / checks (client) (push) Canceled after 3m8s

This commit is contained in:
dingo
2026-10-10 23:18:32 -04:00
parent 2133e5086e
commit d057e5ba04
5 changed files with 168 additions and 0 deletions
+114
View File
@@ -4,6 +4,7 @@
#include <array>
#include <bit>
#include <ranges>
#include <tuple>
#include <utility>
namespace evo::server {
@@ -49,6 +50,45 @@ bool valid_ticks(std::span<const CellularTickIntents> ticks) {
}
return true;
}
std::uint64_t action_tick(const CellularActionRecord& action) {
return get(action.input, 0, 8);
}
bool valid_action_input(const CellularActionRecord& action, std::uint64_t begin_tick,
std::uint64_t end_tick) {
const auto input = std::span<const std::uint8_t>{action.input};
const auto tick = action_tick(action);
const auto move_x = std::bit_cast<std::int16_t>(
static_cast<std::uint16_t>(get(input, 8, 2)));
const auto move_y = std::bit_cast<std::int16_t>(
static_cast<std::uint16_t>(get(input, 10, 2)));
return !all_zero(action.account) && action.action_id != 0 && tick > begin_tick &&
tick <= end_tick && move_x >= -movement_limit && move_x <= movement_limit &&
move_y >= -movement_limit && move_y <= movement_limit && input[12] <= 1 &&
all_zero(input.subspan(13, 3)) &&
(input[12] == 1 || all_zero(input.subspan(16, 8)));
}
bool action_less(const CellularActionRecord& a, const CellularActionRecord& b) {
const auto a_tick = action_tick(a);
const auto b_tick = action_tick(b);
return a_tick < b_tick ||
(a_tick == b_tick && (a.account < b.account ||
(a.account == b.account && a.action_id < b.action_id)));
}
bool valid_actions(std::span<const CellularActionRecord> actions, std::uint64_t begin_tick,
std::uint64_t end_tick) {
if (end_tick <= begin_tick || actions.size() > max_cellular_action_records)
return false;
for (std::size_t i = 0; i < actions.size(); ++i) {
if (!valid_action_input(actions[i], begin_tick, end_tick) ||
(i > 0 && !action_less(actions[i - 1], actions[i])))
return false;
for (std::size_t j = 0; j < i; ++j)
if (actions[j].account == actions[i].account &&
actions[j].action_id == actions[i].action_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);
@@ -181,4 +221,78 @@ replay_cellular_batch(const CellularLives& base, const CellularBatch& batch) {
return std::unexpected{CellularBatchError::state};
return std::move(*replay);
}
std::expected<std::vector<std::uint8_t>, CellularActionBlockError>
encode_cellular_action_block(std::span<const CellularActionRecord> actions,
std::uint64_t begin_tick, std::uint64_t end_tick) {
if (!valid_actions(actions, begin_tick, end_tick))
return std::unexpected{CellularActionBlockError::format};
std::vector<std::uint8_t> out;
out.reserve(actions.size() * cellular_action_record_bytes);
for (const auto& action : actions) {
out.insert(out.end(), action.account.begin(), action.account.end());
put(out, action.action_id, 8);
out.insert(out.end(), action.input.begin(), action.input.end());
}
return out;
}
std::expected<std::vector<CellularActionRecord>, CellularActionBlockError>
decode_cellular_action_block(std::span<const std::uint8_t> bytes, std::uint64_t begin_tick,
std::uint64_t end_tick) {
if (end_tick <= begin_tick || bytes.size() % cellular_action_record_bytes != 0 ||
bytes.size() / cellular_action_record_bytes > max_cellular_action_records)
return std::unexpected{CellularActionBlockError::format};
std::vector<CellularActionRecord> out(bytes.size() / cellular_action_record_bytes);
std::size_t offset{};
for (auto& action : out) {
const auto record = bytes.subspan(offset, cellular_action_record_bytes);
std::ranges::copy(record.first(16), action.account.begin());
action.action_id = get(record, 16, 8);
std::ranges::copy(record.subspan(24, 24), action.input.begin());
offset += cellular_action_record_bytes;
}
if (!valid_actions(out, begin_tick, end_tick))
return std::unexpected{CellularActionBlockError::format};
return out;
}
std::expected<void, CellularActionBlockError>
match_cellular_actions_to_trace(const CellularLives& predecessor, const CellularBatch& batch,
std::span<const CellularActionRecord> actions) {
if (!valid_header(batch.header, batch.ticks.size()) || !valid_ticks(batch.ticks) ||
!valid_actions(actions, batch.header.begin_tick, batch.header.end_tick))
return std::unexpected{CellularActionBlockError::format};
if (predecessor.world() != batch.header.world || predecessor.region() != batch.header.region ||
predecessor.frame() != batch.header.frame || predecessor.tick() != batch.header.begin_tick)
return std::unexpected{CellularActionBlockError::mismatch};
for (const auto& action : actions) {
const auto life = std::ranges::find(predecessor.lives(), action.account,
&CellularOwnedLife::account);
if (life == predecessor.lives().end())
return std::unexpected{CellularActionBlockError::mismatch};
const auto index = static_cast<std::size_t>(action_tick(action) - batch.header.begin_tick - 1);
const auto& intents = batch.ticks[index].lives;
const auto intent = std::ranges::lower_bound(
intents, life->identity.life, {}, [](const CellularLifeIntent& value) {
return value.life_id;
});
if (intent == intents.end() || intent->life_id != life->identity.life)
return std::unexpected{CellularActionBlockError::mismatch};
const auto input = std::span<const std::uint8_t>{action.input};
const auto target = input[12] == 0
? std::optional<biology::CellularPatchAddress>{}
: std::optional<biology::CellularPatchAddress>{
biology::CellularPatchAddress{
static_cast<std::uint32_t>(get(input, 16, 4)),
static_cast<std::uint32_t>(get(input, 20, 4))}};
if (intent->intent.movement.x != std::bit_cast<std::int16_t>(
static_cast<std::uint16_t>(get(input, 8, 2))) ||
intent->intent.movement.y != std::bit_cast<std::int16_t>(
static_cast<std::uint16_t>(get(input, 10, 2))) ||
intent->intent.intake_target != target)
return std::unexpected{CellularActionBlockError::mismatch};
}
return {};
}
} // namespace evo::server