Add cellular mutation replay trace v3
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (checks) (push) Failing after 2m9s
Foundation / checks (ecs) (push) Successful in 2m2s
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 3m11s
Foundation / checks (postgres) (push) Canceled after 1m3s
Foundation / checks (network) (push) Canceled after 1m9s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (checks) (push) Failing after 2m9s
Foundation / checks (ecs) (push) Successful in 2m2s
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 3m11s
Foundation / checks (postgres) (push) Canceled after 1m3s
Foundation / checks (network) (push) Canceled after 1m9s
This commit is contained in:
@@ -43,7 +43,8 @@ bool valid_ticks(std::span<const CellularTickIntents> ticks) {
|
||||
std::set<LifeAccount> accounts;
|
||||
for (const auto& tick : ticks) {
|
||||
if (tick.lives.size() > biology::max_cellular_tick_lives ||
|
||||
tick.births.size() > biology::max_cellular_tick_lives)
|
||||
tick.births.size() > biology::max_cellular_tick_lives ||
|
||||
tick.mutations.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];
|
||||
@@ -60,6 +61,14 @@ bool valid_ticks(std::span<const CellularTickIntents> ticks) {
|
||||
if (!identities.insert(birth.identity).second || !accounts.insert(birth.account).second)
|
||||
return false;
|
||||
}
|
||||
for (std::size_t i = 0; i < tick.mutations.size(); ++i) {
|
||||
const auto& mutation = tick.mutations[i];
|
||||
if (mutation.life_id == 0 || mutation.event_index == 0 || mutation.offer_tick == 0 ||
|
||||
mutation.successor_id == 0 || mutation.operator_id == 0 ||
|
||||
(i > 0 && tick.mutations[i - 1].life_id >= mutation.life_id) ||
|
||||
!identities.insert(mutation.successor_id).second)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -99,6 +108,31 @@ bool valid_actions(std::span<const CellularActionRecord> actions, std::uint64_t
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool mutation_action_less(const CellularMutationActionRecord& left,
|
||||
const CellularMutationActionRecord& right) {
|
||||
return left.tick < right.tick ||
|
||||
(left.tick == right.tick &&
|
||||
(left.account < right.account ||
|
||||
(left.account == right.account && left.action_id < right.action_id)));
|
||||
}
|
||||
bool valid_mutation_actions(std::span<const CellularMutationActionRecord> 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) {
|
||||
const auto& action = actions[i];
|
||||
const auto& input = action.input;
|
||||
if (all_zero(action.account) || action.action_id == 0 || action.tick <= begin_tick ||
|
||||
action.tick > end_tick || input.life_id == 0 || input.event_index == 0 ||
|
||||
input.offer_tick == 0 || input.offer_tick > begin_tick || input.operator_id == 0 ||
|
||||
(i != 0 && !mutation_action_less(actions[i - 1], action)))
|
||||
return false;
|
||||
for (std::size_t j = 0; j < i; ++j)
|
||||
if (actions[j].account == action.account && actions[j].action_id == action.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);
|
||||
@@ -112,20 +146,40 @@ void put_intent(std::vector<std::uint8_t>& out, const CellularLifeIntent& life)
|
||||
|
||||
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))
|
||||
if ((batch.format_version != cellular_batch_format &&
|
||||
batch.format_version != cellular_batch_v2_format &&
|
||||
batch.format_version != cellular_batch_legacy_format) ||
|
||||
!valid_header(batch.header, batch.ticks.size()) || !valid_ticks(batch.ticks))
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
if (batch.format_version == cellular_batch_legacy_format &&
|
||||
std::ranges::any_of(batch.ticks, [](const auto& tick) {
|
||||
return !tick.births.empty() || !tick.mutations.empty();
|
||||
}))
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
if (batch.format_version == cellular_batch_v2_format &&
|
||||
std::ranges::any_of(batch.ticks, [](const auto& tick) { return !tick.mutations.empty(); }))
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
const auto tick_header_bytes =
|
||||
batch.format_version == cellular_batch_format ? cellular_batch_tick_header_bytes
|
||||
: batch.format_version == cellular_batch_v2_format ? cellular_batch_v2_tick_header_bytes
|
||||
: cellular_batch_v1_tick_header_bytes;
|
||||
std::size_t size = cellular_batch_header_bytes;
|
||||
for (const auto& tick : batch.ticks) {
|
||||
const auto records = tick.lives.size() * cellular_batch_intent_bytes +
|
||||
tick.births.size() * cellular_batch_birth_bytes;
|
||||
if (records > max_cellular_batch_bytes - size - cellular_batch_tick_header_bytes)
|
||||
(batch.format_version >= cellular_batch_v2_format
|
||||
? tick.births.size() * cellular_batch_birth_bytes
|
||||
: 0) +
|
||||
(batch.format_version == cellular_batch_format
|
||||
? tick.mutations.size() * cellular_batch_mutation_bytes
|
||||
: 0);
|
||||
if (records > max_cellular_batch_bytes - size - tick_header_bytes)
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
size += cellular_batch_tick_header_bytes + records;
|
||||
size += 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, batch.format_version, 4);
|
||||
put(out, 4, 4); // Profile 4.
|
||||
put(out, batch.header.world, 8);
|
||||
put(out, batch.header.region, 8);
|
||||
@@ -139,15 +193,32 @@ encode_cellular_batch(const CellularBatch& batch) {
|
||||
put(out, 0, 4);
|
||||
for (const auto& tick : batch.ticks) {
|
||||
put(out, tick.lives.size(), 4);
|
||||
put(out, tick.births.size(), 4);
|
||||
if (batch.format_version >= cellular_batch_v2_format)
|
||||
put(out, tick.births.size(), 4);
|
||||
if (batch.format_version == cellular_batch_format) {
|
||||
put(out, tick.mutations.size(), 4);
|
||||
put(out, 0, 4);
|
||||
}
|
||||
for (const auto& life : tick.lives)
|
||||
put_intent(out, life);
|
||||
for (const auto& birth : tick.births) {
|
||||
if (batch.format_version < cellular_batch_v2_format)
|
||||
break;
|
||||
out.insert(out.end(), birth.account.begin(), birth.account.end());
|
||||
put(out, birth.request, 8);
|
||||
put(out, birth.generation, 8);
|
||||
put(out, birth.identity, 8);
|
||||
}
|
||||
for (const auto& mutation : tick.mutations) {
|
||||
if (batch.format_version != cellular_batch_format)
|
||||
break;
|
||||
put(out, mutation.life_id, 8);
|
||||
put(out, mutation.event_index, 8);
|
||||
put(out, mutation.offer_tick, 8);
|
||||
put(out, mutation.successor_id, 8);
|
||||
put(out, mutation.operator_id, 2);
|
||||
put(out, 0, 6);
|
||||
}
|
||||
}
|
||||
if (out.size() != size)
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
@@ -160,13 +231,15 @@ decode_cellular_batch(std::span<const std::uint8_t> bytes) {
|
||||
!std::ranges::equal(bytes.first(magic.size()), magic))
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
const auto format = get(bytes, 8, 4);
|
||||
if ((format != cellular_batch_format && format != cellular_batch_legacy_format) ||
|
||||
if ((format != cellular_batch_format && format != cellular_batch_v2_format &&
|
||||
format != cellular_batch_legacy_format) ||
|
||||
get(bytes, 12, 4) != 4)
|
||||
return std::unexpected{CellularBatchError::version};
|
||||
if (get(bytes, 108, 4) != 0)
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
|
||||
CellularBatch out;
|
||||
out.format_version = static_cast<std::uint32_t>(format);
|
||||
auto& h = out.header;
|
||||
h.world = get(bytes, 16, 8);
|
||||
h.region = get(bytes, 24, 8);
|
||||
@@ -185,21 +258,30 @@ decode_cellular_batch(std::span<const std::uint8_t> bytes) {
|
||||
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) {
|
||||
const auto tick_header_bytes = format == cellular_batch_format
|
||||
? cellular_batch_tick_header_bytes
|
||||
: sizeof(std::uint32_t);
|
||||
const auto tick_header_bytes =
|
||||
format == cellular_batch_format ? cellular_batch_tick_header_bytes
|
||||
: format == cellular_batch_v2_format ? cellular_batch_v2_tick_header_bytes
|
||||
: cellular_batch_v1_tick_header_bytes;
|
||||
if (bytes.size() - offset < tick_header_bytes)
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
const auto count = get(bytes, offset, 4);
|
||||
offset += 4;
|
||||
std::uint64_t birth_count{};
|
||||
if (format == cellular_batch_format) {
|
||||
if (format >= cellular_batch_v2_format) {
|
||||
birth_count = get(bytes, offset, 4);
|
||||
offset += 4;
|
||||
}
|
||||
std::uint64_t mutation_count{};
|
||||
if (format == cellular_batch_format) {
|
||||
mutation_count = get(bytes, offset, 4);
|
||||
if (get(bytes, offset + 4, 4) != 0)
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
offset += 8;
|
||||
}
|
||||
if (count > biology::max_cellular_tick_lives ||
|
||||
count > (bytes.size() - offset) / cellular_batch_intent_bytes ||
|
||||
birth_count > biology::max_cellular_tick_lives)
|
||||
birth_count > biology::max_cellular_tick_lives ||
|
||||
mutation_count > biology::max_cellular_tick_lives)
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
CellularTickIntents step;
|
||||
step.lives.reserve(static_cast<std::size_t>(count));
|
||||
@@ -245,6 +327,25 @@ decode_cellular_batch(std::span<const std::uint8_t> bytes) {
|
||||
step.births.push_back(birth);
|
||||
offset += cellular_batch_birth_bytes;
|
||||
}
|
||||
if (mutation_count > (bytes.size() - offset) / cellular_batch_mutation_bytes)
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
step.mutations.reserve(static_cast<std::size_t>(mutation_count));
|
||||
for (std::uint64_t i = 0; i < mutation_count; ++i) {
|
||||
const auto record = bytes.subspan(offset, cellular_batch_mutation_bytes);
|
||||
CellularMutationSelection mutation;
|
||||
mutation.life_id = get(record, 0, 8);
|
||||
mutation.event_index = get(record, 8, 8);
|
||||
mutation.offer_tick = get(record, 16, 8);
|
||||
mutation.successor_id = get(record, 24, 8);
|
||||
mutation.operator_id = static_cast<std::uint16_t>(get(record, 32, 2));
|
||||
if (!all_zero(record.subspan(34, 6)) || mutation.life_id == 0 ||
|
||||
mutation.event_index == 0 || mutation.offer_tick == 0 ||
|
||||
mutation.successor_id == 0 || mutation.operator_id == 0 ||
|
||||
(!step.mutations.empty() && step.mutations.back().life_id >= mutation.life_id))
|
||||
return std::unexpected{CellularBatchError::format};
|
||||
step.mutations.push_back(mutation);
|
||||
offset += cellular_batch_mutation_bytes;
|
||||
}
|
||||
out.ticks.push_back(std::move(step));
|
||||
}
|
||||
if (offset != bytes.size())
|
||||
@@ -265,7 +366,7 @@ replay_cellular_batch(const CellularLives& base, const CellularBatch& batch) {
|
||||
if (!replay)
|
||||
return std::unexpected{CellularBatchError::state};
|
||||
for (const auto& tick : batch.ticks) {
|
||||
if (!(*replay)->advance_tick(tick.lives))
|
||||
if (!(*replay)->advance_tick_with_mutations(tick.lives, tick.mutations))
|
||||
return std::unexpected{CellularBatchError::state};
|
||||
for (const auto& birth : tick.births) {
|
||||
const LifeRequest request{birth.account, birth.request};
|
||||
@@ -318,6 +419,55 @@ decode_cellular_action_block(std::span<const std::uint8_t> bytes, std::uint64_t
|
||||
return out;
|
||||
}
|
||||
|
||||
std::expected<std::vector<std::uint8_t>, CellularActionBlockError>
|
||||
encode_cellular_mutation_action_block(std::span<const CellularMutationActionRecord> actions,
|
||||
std::uint64_t begin_tick, std::uint64_t end_tick) {
|
||||
if (!valid_mutation_actions(actions, begin_tick, end_tick))
|
||||
return std::unexpected{CellularActionBlockError::format};
|
||||
std::vector<std::uint8_t> out;
|
||||
out.reserve(actions.size() * cellular_mutation_action_record_bytes);
|
||||
for (const auto& action : actions) {
|
||||
out.insert(out.end(), action.account.begin(), action.account.end());
|
||||
put(out, action.action_id, 8);
|
||||
put(out, action.tick, 8);
|
||||
put(out, action.input.life_id, 8);
|
||||
put(out, action.input.event_index, 8);
|
||||
put(out, action.input.offer_tick, 8);
|
||||
put(out, action.input.operator_id, 2);
|
||||
put(out, 0, 6);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::expected<std::vector<CellularMutationActionRecord>, CellularActionBlockError>
|
||||
decode_cellular_mutation_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_mutation_action_record_bytes != 0 ||
|
||||
bytes.size() / cellular_mutation_action_record_bytes > max_cellular_action_records)
|
||||
return std::unexpected{CellularActionBlockError::format};
|
||||
std::vector<CellularMutationActionRecord> out;
|
||||
out.reserve(bytes.size() / cellular_mutation_action_record_bytes);
|
||||
std::size_t offset{};
|
||||
for (std::size_t i = 0; i < bytes.size() / cellular_mutation_action_record_bytes; ++i) {
|
||||
const auto record = bytes.subspan(offset, cellular_mutation_action_record_bytes);
|
||||
CellularMutationActionRecord action;
|
||||
std::ranges::copy(record.first(16), action.account.begin());
|
||||
action.action_id = get(record, 16, 8);
|
||||
action.tick = get(record, 24, 8);
|
||||
action.input.life_id = get(record, 32, 8);
|
||||
action.input.event_index = get(record, 40, 8);
|
||||
action.input.offer_tick = get(record, 48, 8);
|
||||
action.input.operator_id = static_cast<std::uint16_t>(get(record, 56, 2));
|
||||
if (!all_zero(record.subspan(58, 6)))
|
||||
return std::unexpected{CellularActionBlockError::format};
|
||||
out.push_back(action);
|
||||
offset += cellular_mutation_action_record_bytes;
|
||||
}
|
||||
if (!valid_mutation_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) {
|
||||
@@ -354,4 +504,64 @@ match_cellular_actions_to_trace(const CellularLives& predecessor, const Cellular
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<void, CellularActionBlockError>
|
||||
match_cellular_mutation_actions_to_trace(const CellularLives& predecessor,
|
||||
const CellularBatch& batch,
|
||||
std::span<const CellularMutationActionRecord> actions) {
|
||||
if (!valid_header(batch.header, batch.ticks.size()) || !valid_ticks(batch.ticks) ||
|
||||
!valid_mutation_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};
|
||||
std::size_t trace_selection_count{};
|
||||
for (const auto& tick : batch.ticks)
|
||||
trace_selection_count += tick.mutations.size();
|
||||
if (trace_selection_count != actions.size())
|
||||
return std::unexpected{CellularActionBlockError::mismatch};
|
||||
for (std::size_t tick_index = 0; tick_index < batch.ticks.size(); ++tick_index) {
|
||||
const auto tick = batch.header.begin_tick + tick_index + 1;
|
||||
for (const auto& selection : batch.ticks[tick_index].mutations) {
|
||||
const auto life = std::ranges::find(
|
||||
predecessor.lives(), selection.life_id,
|
||||
[](const CellularOwnedLife& value) { return value.identity.life; });
|
||||
if (life == predecessor.lives().end())
|
||||
return std::unexpected{CellularActionBlockError::mismatch};
|
||||
const auto matches = std::ranges::count_if(actions, [&](const auto& action) {
|
||||
return action.account == life->account && action.tick == tick &&
|
||||
action.input.life_id == selection.life_id &&
|
||||
action.input.event_index == selection.event_index &&
|
||||
action.input.offer_tick == selection.offer_tick &&
|
||||
action.input.operator_id == selection.operator_id;
|
||||
});
|
||||
if (matches != 1)
|
||||
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() ||
|
||||
(predecessor.format_version() != cellular_lives_revision_format))
|
||||
return std::unexpected{CellularActionBlockError::mismatch};
|
||||
const auto tick_index = static_cast<std::size_t>(action.tick - batch.header.begin_tick - 1);
|
||||
const auto& mutations = batch.ticks[tick_index].mutations;
|
||||
const auto selection = std::ranges::lower_bound(mutations, life->identity.life, {},
|
||||
&CellularMutationSelection::life_id);
|
||||
if (selection == mutations.end() || selection->life_id != life->identity.life ||
|
||||
selection->event_index != action.input.event_index ||
|
||||
selection->offer_tick != action.input.offer_tick ||
|
||||
selection->operator_id != action.input.operator_id || !life->mutation.offer ||
|
||||
life->mutation.event_index != action.input.event_index ||
|
||||
life->mutation.offer_tick != action.input.offer_tick ||
|
||||
life->identity.life != action.input.life_id ||
|
||||
life->mutation.offer->parent != life->identity.genome ||
|
||||
!std::ranges::any_of(life->mutation.offer->candidates, [&](const auto& candidate) {
|
||||
return candidate.operator_id == action.input.operator_id;
|
||||
}))
|
||||
return std::unexpected{CellularActionBlockError::mismatch};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
} // namespace evo::server
|
||||
|
||||
Reference in New Issue
Block a user