Bind durable mutation reservations to cellular ticks
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m56s
Foundation / checks (checks) (push) Failing after 3m58s
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 6m18s
Foundation / checks (postgres) (push) Canceled after 2m23s
Foundation / checks (network) (push) Canceled after 4m19s

This commit is contained in:
dingo
2026-10-11 08:54:50 -04:00
parent b0cd2b7704
commit de4bbec94c
5 changed files with 206 additions and 20 deletions
+89 -5
View File
@@ -4,15 +4,27 @@
#include <optional>
namespace evo::server {
namespace {
std::uint64_t little_endian(std::span<const std::uint8_t> bytes, std::size_t offset,
std::size_t count) {
std::uint64_t value{};
for (std::size_t i = 0; i < count; ++i)
value |= static_cast<std::uint64_t>(bytes[offset + i]) << (i * 8U);
return value;
}
} // namespace
std::expected<CellularSessionTick, CellularSessionBatchError>
prepare_cellular_session_tick(const CellularLives& state, std::uint64_t tick,
std::span<const CellularLifeIntent> baseline,
std::span<const network::CarriedAction> actions) {
std::span<const network::CarriedAction> actions,
std::span<const persistence::CellularMutationReservation> mutations) {
if (tick == 0 || tick != state.tick() + 1)
return std::unexpected{CellularSessionBatchError::tick};
if (baseline.size() != state.lives().size())
return std::unexpected{CellularSessionBatchError::intents};
if (actions.size() > max_cellular_action_records)
if (actions.size() > max_cellular_action_records ||
mutations.size() > max_cellular_action_records)
return std::unexpected{CellularSessionBatchError::action};
CellularSessionTick prepared;
@@ -72,6 +84,56 @@ prepare_cellular_session_tick(const CellularLives& state, std::uint64_t tick,
return left.account < right.account;
return left.action_id < right.action_id;
});
if (!mutations.empty() && state.format_version() != cellular_lives_revision_format)
return std::unexpected{CellularSessionBatchError::mutation};
prepared.intents.mutations.reserve(mutations.size());
prepared.mutation_actions.reserve(mutations.size());
for (const auto& reservation : mutations) {
const auto bytes = std::span{reservation.input};
const auto target_tick = little_endian(bytes, 0, 8);
const auto life_id = little_endian(bytes, 8, 8);
const auto event_index = little_endian(bytes, 16, 8);
const auto offer_tick = little_endian(bytes, 24, 8);
const auto operator_id = static_cast<std::uint16_t>(little_endian(bytes, 32, 2));
if (reservation.account == persistence::Uuid{} || reservation.action_id == 0 ||
reservation.region != state.region() || reservation.generation == 0 ||
reservation.successor_identity == 0 || target_tick != tick ||
reservation.target_tick != tick || life_id == 0 || event_index == 0 ||
offer_tick == 0 || offer_tick >= tick || operator_id == 0)
return std::unexpected{CellularSessionBatchError::mutation};
const auto life = std::ranges::find(state.lives(), reservation.account,
&CellularOwnedLife::account);
if (life == state.lives().end() || life->identity.life != life_id ||
life->mutation.event_index != event_index || life->mutation.offer_tick != offer_tick ||
!life->mutation.offer ||
!std::ranges::any_of(life->mutation.offer->candidates, [&](const auto& candidate) {
return candidate.operator_id == operator_id;
}))
return std::unexpected{CellularSessionBatchError::mutation};
if (std::ranges::any_of(prepared.intents.mutations, [&](const auto& previous) {
return previous.life_id == life_id ||
previous.successor_id == reservation.successor_identity;
}) ||
std::ranges::any_of(prepared.mutation_actions, [&](const auto& previous) {
return previous.account == reservation.account &&
previous.action_id == reservation.action_id;
}))
return std::unexpected{CellularSessionBatchError::duplicate_mutation};
prepared.intents.mutations.push_back(
{life_id, event_index, offer_tick, reservation.successor_identity, operator_id});
prepared.mutation_actions.push_back(
{reservation.account, reservation.action_id, tick,
{life_id, event_index, offer_tick, operator_id}});
}
std::ranges::sort(prepared.intents.mutations, {}, &CellularMutationSelection::life_id);
std::ranges::sort(prepared.mutation_actions,
[](const auto& left, const auto& right) {
if (left.account != right.account)
return left.account < right.account;
return left.action_id < right.action_id;
});
return prepared;
}
@@ -101,12 +163,16 @@ std::expected<void, CellularSessionBatchError>
CellularSessionBatchBuilder::append(std::uint64_t tick,
std::span<const CellularLifeIntent> baseline,
std::span<const network::CarriedAction> actions,
std::span<const CellularTickIntents::Birth> births) {
std::span<const CellularTickIntents::Birth> births,
std::span<const persistence::CellularMutationReservation>
mutations) {
if (batch_.ticks.size() >= max_cellular_batch_ticks)
return std::unexpected{CellularSessionBatchError::batch_full};
if (actions_.size() + actions.size() > max_cellular_action_records)
return std::unexpected{CellularSessionBatchError::action_limit};
auto prepared = prepare_cellular_session_tick(*working_, tick, baseline, actions);
if (mutation_actions_.size() + mutations.size() > max_cellular_action_records)
return std::unexpected{CellularSessionBatchError::action_limit};
auto prepared = prepare_cellular_session_tick(*working_, tick, baseline, actions, mutations);
if (!prepared)
return std::unexpected{prepared.error()};
for (const auto& action : prepared->actions)
@@ -114,6 +180,12 @@ CellularSessionBatchBuilder::append(std::uint64_t tick,
return previous.account == action.account && previous.action_id == action.action_id;
}) != actions_.end())
return std::unexpected{CellularSessionBatchError::duplicate_action};
for (const auto& action : prepared->mutation_actions)
if (std::ranges::find_if(mutation_actions_, [&](const auto& previous) {
return previous.account == action.account &&
previous.action_id == action.action_id;
}) != mutation_actions_.end())
return std::unexpected{CellularSessionBatchError::duplicate_mutation};
if (births.size() > biology::max_cellular_tick_lives)
return std::unexpected{CellularSessionBatchError::invalid_birth};
prepared->intents.births.assign(births.begin(), births.end());
@@ -138,7 +210,8 @@ CellularSessionBatchBuilder::append(std::uint64_t tick,
auto next = CellularLives::restore(working_->snapshot(), working_->world(), working_->region(),
working_->frame(), working_->tick());
if (!next || !(*next)->advance_tick(prepared->intents.lives))
if (!next || !(*next)->advance_tick_with_mutations(prepared->intents.lives,
prepared->intents.mutations))
return std::unexpected{CellularSessionBatchError::biology};
for (const auto& birth : prepared->intents.births) {
@@ -156,9 +229,20 @@ CellularSessionBatchBuilder::append(std::uint64_t tick,
next_batch.header.end_tick = tick;
if (!encode_cellular_batch(next_batch))
return std::unexpected{CellularSessionBatchError::invalid_birth};
const auto next_mutation_actions = [&] {
auto records = mutation_actions_;
records.insert(records.end(), prepared->mutation_actions.begin(),
prepared->mutation_actions.end());
return records;
}();
if (!encode_cellular_mutation_action_block(next_mutation_actions,
next_batch.header.begin_tick,
next_batch.header.end_tick))
return std::unexpected{CellularSessionBatchError::mutation};
working_ = std::move(*next);
batch_ = std::move(next_batch);
actions_.insert(actions_.end(), prepared->actions.begin(), prepared->actions.end());
mutation_actions_ = next_mutation_actions;
return {};
}
} // namespace evo::server