Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (checks) (push) Failing after 39s
Foundation / checks (ecs) (push) Successful in 1m54s
Foundation / checks (postgres) (push) Failing after 51s
Foundation / checks (tsan) (push) Failing after 32s
Foundation / checks (network) (push) Failing after 3m4s
Foundation / checks (client) (push) Failing after 4m35s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped
284 lines
14 KiB
C++
284 lines
14 KiB
C++
#include "evo/server/lifecycle/cellular_lives.hpp"
|
|
#include "evo/server/lifecycle/cellular_batch.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <span>
|
|
#include <source_location>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
using namespace evo::server;
|
|
using evo::accounting::Inventory;
|
|
|
|
void check(bool value, std::source_location at = std::source_location::current()) {
|
|
if (!value)
|
|
throw std::runtime_error("cellular lives line " + std::to_string(at.line()));
|
|
}
|
|
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
|
|
|
|
Inventory founder_price(std::uint64_t count) {
|
|
const auto endowment = evo::biology::primitive_endowment();
|
|
CHECK(endowment);
|
|
auto funding = endowment->body;
|
|
funding.energy.value = endowment->reserve.energy.value + endowment->construction.value;
|
|
funding.energy.value *= count;
|
|
for (auto& channel : funding.matter)
|
|
channel.value *= count;
|
|
return funding;
|
|
}
|
|
|
|
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 refresh_checksum(std::vector<std::uint8_t>& bytes) {
|
|
const auto value = checksum(std::span{bytes}.first(bytes.size() - 8));
|
|
for (unsigned i = 0; i < 8; ++i)
|
|
bytes[bytes.size() - 8 + i] = static_cast<std::uint8_t>(value >> (i * 8U));
|
|
}
|
|
|
|
std::unique_ptr<CellularLives> empty_cellular_owner(Inventory funding,
|
|
std::vector<SpawnSite> sites) {
|
|
auto primitive = PrimitiveLives::create({91, 1, 1, 17, funding, std::move(sites)});
|
|
CHECK(primitive);
|
|
auto habitat = evo::biology::CellularHabitat::create({91, 1, 1, 1'000, 1'000, {}});
|
|
CHECK(habitat);
|
|
auto owner = CellularLives::create(**primitive, std::move(*habitat), {{1'000, 1'000}, 0, 293'150});
|
|
CHECK(owner);
|
|
return std::move(*owner);
|
|
}
|
|
|
|
void reserved_birth_is_funded_and_detached() {
|
|
auto opening = empty_cellular_owner(founder_price(1), {{0, 0}, {20, 0}});
|
|
const std::span<const CellularLifeIntent> no_lives{};
|
|
CHECK(opening->advance_tick(no_lives));
|
|
const auto predecessor = opening->snapshot();
|
|
LifeRequest request{};
|
|
request.account.back() = 9;
|
|
request.action = 17;
|
|
const LifeAllocation allocation{501, 501, 501, 501, {1}};
|
|
auto born = opening->birth_candidate(request, allocation);
|
|
CHECK(born && opening->tick() == 1 && opening->lives().empty());
|
|
CHECK((*born)->tick() == opening->tick() && (*born)->lives().size() == 1 &&
|
|
(*born)->lives()[0].account == request.account &&
|
|
(*born)->lives()[0].identity == allocation && (*born)->lives()[0].source == 17 &&
|
|
(*born)->lives()[0].position == (evo::biology::CellularPosition{0, 0}));
|
|
const auto endowment = evo::biology::primitive_endowment();
|
|
CHECK(endowment);
|
|
CHECK((*born)->nursery().energy.value == opening->nursery().energy.value -
|
|
endowment->reserve.energy.value -
|
|
endowment->construction.value);
|
|
CHECK((*born)->heat().energy.value == endowment->construction.value &&
|
|
(*born)->current_total() == (*born)->opening_total() &&
|
|
(*born)->archive().recorded() == 1);
|
|
CHECK(opening->snapshot() == predecessor);
|
|
const auto recovered = CellularLives::restore((*born)->snapshot(), 91, 1, 1, 1);
|
|
CHECK(recovered && (*recovered)->snapshot() == (*born)->snapshot());
|
|
auto retry_account = request;
|
|
retry_account.action++;
|
|
CHECK((*born)->birth_candidate(retry_account, {502, 502, 502, 502, {1}}).error() ==
|
|
CellularLivesError::life_exists);
|
|
CHECK(opening->birth_candidate(request, {502, 503, 502, 502, {1}}).error() ==
|
|
CellularLivesError::identity);
|
|
CHECK(opening->birth_candidate(request, {502, 502, 502, 502, {2}}).error() ==
|
|
CellularLivesError::invalid_tick);
|
|
const std::array next_tick{CellularLifeIntent{allocation.life, {}}};
|
|
CHECK((*born)->advance_tick(next_tick) && (*born)->tick() == 2 &&
|
|
(*born)->lives().size() == 1);
|
|
|
|
auto unfunded = empty_cellular_owner({}, {{0, 0}});
|
|
CHECK(unfunded->advance_tick(no_lives));
|
|
const auto unfunded_snapshot = unfunded->snapshot();
|
|
CHECK(unfunded->birth_candidate(request, {601, 601, 601, 601, {1}}).error() ==
|
|
CellularLivesError::insufficient_funding);
|
|
CHECK(unfunded->snapshot() == unfunded_snapshot && unfunded->lives().empty());
|
|
|
|
auto occupied_primitive =
|
|
PrimitiveLives::create({92, 1, 1, 17, founder_price(2), {{0, 0}}});
|
|
CHECK(occupied_primitive);
|
|
LifeRequest existing{};
|
|
existing.account.back() = 11;
|
|
existing.action = 1;
|
|
CHECK((*occupied_primitive)->spawn(existing, {701, 701, 701, 701, {1}}));
|
|
auto occupied_habitat =
|
|
evo::biology::CellularHabitat::create({92, 1, 1, 1'000, 1'000, {}});
|
|
CHECK(occupied_habitat);
|
|
auto occupied = CellularLives::create(**occupied_primitive, std::move(*occupied_habitat),
|
|
{{1'000, 1'000}, 1, 293'150});
|
|
CHECK(occupied);
|
|
LifeRequest arriving{};
|
|
arriving.account.back() = 12;
|
|
arriving.action = 2;
|
|
CHECK((*occupied)->birth_candidate(arriving, {702, 702, 702, 702, {1}}).error() ==
|
|
CellularLivesError::no_placement);
|
|
}
|
|
|
|
void owner_advances_and_closes_death_atomically() {
|
|
auto primitive = PrimitiveLives::create({90, 1, 7, 17, founder_price(2), {{0, 0}, {20, 0}}});
|
|
CHECK(primitive);
|
|
LifeRequest request{};
|
|
request.account.back() = 1;
|
|
request.action = 1;
|
|
CHECK((*primitive)->spawn(request, {1, 101, 201, 301, {1}}));
|
|
request.account.back() = 2;
|
|
CHECK((*primitive)->spawn(request, {2, 102, 202, 302, {1}}));
|
|
|
|
evo::biology::CellularHabitatConfig habitat_config{
|
|
90, 1, 7, 3'000, 2'000, {{{0, 0}, 29, {{10}, {{{10}, {10}, {10}, {10}}}}}}};
|
|
auto habitat = evo::biology::CellularHabitat::create(std::move(habitat_config));
|
|
CHECK(habitat);
|
|
auto created = CellularLives::create(
|
|
**primitive, std::move(*habitat), {{3'000, 2'000}, 1, 293'150});
|
|
CHECK(created);
|
|
auto& owner = **created;
|
|
CHECK(owner.world() == 90 && owner.region() == 1 && owner.frame() == 7);
|
|
CHECK(owner.lives().size() == 2 && owner.tick() == 1);
|
|
CHECK(owner.current_total() == owner.opening_total());
|
|
|
|
const std::array invalid{CellularLifeIntent{1, {}}, CellularLifeIntent{1, {}}};
|
|
const auto refused = owner.advance_tick(invalid);
|
|
CHECK(!refused && owner.tick() == 1 && owner.lives().size() == 2);
|
|
|
|
const auto first_life_id = owner.lives()[0].identity.life;
|
|
const auto second_life_id = owner.lives()[1].identity.life;
|
|
const auto visible_patches = owner.perceive(first_life_id);
|
|
CHECK(visible_patches && visible_patches->size() == 1 &&
|
|
visible_patches->front().address == (evo::biology::CellularPatchAddress{0, 0}) &&
|
|
visible_patches->front().available == owner.habitat().patches()[0].stock);
|
|
CHECK(owner.perceive(0).error() == CellularLivesError::invalid_life);
|
|
const std::array invalid_motion{
|
|
CellularLifeIntent{first_life_id, {}},
|
|
CellularLifeIntent{second_life_id, {{1'001, 0}, std::nullopt}}};
|
|
const auto partial = owner.advance_tick(invalid_motion);
|
|
CHECK(!partial && partial.error() == CellularLivesError::region_step && owner.tick() == 1 &&
|
|
owner.lives().size() == 2);
|
|
|
|
const std::array feed{CellularLifeIntent{first_life_id, {{0, 0}, {{0, 0}}}},
|
|
CellularLifeIntent{second_life_id, {{0, 0}, {{0, 0}}}}};
|
|
const auto fed = owner.advance_tick(feed);
|
|
CHECK(fed && owner.tick() == 2 && owner.lives().size() == 2);
|
|
CHECK(fed->lives.size() == 2 &&
|
|
fed->lives[0].candidate.intake_status == evo::biology::CellularIntakeStatus::acquired);
|
|
CHECK(owner.habitat().patches()[0].stock == (Inventory{{0}, {}}));
|
|
CHECK(owner.current_total() == owner.opening_total());
|
|
|
|
const auto saved = owner.snapshot();
|
|
CHECK(saved.size() <= max_cellular_lives_bytes);
|
|
auto recovered = CellularLives::restore(saved, 90, 1, 7, owner.tick());
|
|
CHECK(recovered && (*recovered)->snapshot() == saved);
|
|
CHECK(!CellularLives::restore(saved, 91, 1, 7, owner.tick()));
|
|
CHECK(!CellularLives::restore(saved, 90, 1, 7, owner.tick() + 1));
|
|
auto damaged = saved;
|
|
damaged[100] ^= 1;
|
|
const auto damaged_result = CellularLives::restore(damaged, 90, 1, 7, owner.tick());
|
|
CHECK(!damaged_result && damaged_result.error() == CellularLivesError::checksum);
|
|
auto unsupported = saved;
|
|
unsupported[8] = static_cast<std::uint8_t>(cellular_lives_format + 1);
|
|
refresh_checksum(unsupported);
|
|
const auto unsupported_result =
|
|
CellularLives::restore(unsupported, 90, 1, 7, owner.tick());
|
|
CHECK(!unsupported_result && unsupported_result.error() == CellularLivesError::version);
|
|
auto outside = saved;
|
|
std::fill(outside.begin() + 300, outside.begin() + 308, uint8_t{0xff});
|
|
refresh_checksum(outside);
|
|
CHECK(!CellularLives::restore(outside, 90, 1, 7, owner.tick()));
|
|
|
|
CellularBatch replay_batch;
|
|
replay_batch.header = {owner.world(), owner.region(), owner.frame(), {}, {}, owner.tick(),
|
|
owner.tick() + 2};
|
|
replay_batch.header.predecessor.fill(0x31);
|
|
replay_batch.header.predecessor_digest.fill(0x41);
|
|
for (int i = 0; i < 2; ++i) {
|
|
CellularTickIntents tick;
|
|
for (const auto& life : owner.lives())
|
|
tick.lives.push_back({life.identity.life, {}});
|
|
replay_batch.ticks.push_back(std::move(tick));
|
|
}
|
|
const auto batch_bytes = encode_cellular_batch(replay_batch);
|
|
CHECK(batch_bytes && batch_bytes->size() <= max_cellular_batch_bytes);
|
|
const auto decoded_batch = decode_cellular_batch(*batch_bytes);
|
|
CHECK(decoded_batch && *decoded_batch == replay_batch);
|
|
CellularActionRecord action;
|
|
action.account = owner.lives()[0].account;
|
|
action.action_id = 17;
|
|
action.input[0] = static_cast<std::uint8_t>(owner.tick() + 1);
|
|
const std::array actions{action};
|
|
const auto action_bytes = encode_cellular_action_block(actions, owner.tick(), owner.tick() + 2);
|
|
CHECK(action_bytes && action_bytes->size() == cellular_action_record_bytes);
|
|
const auto decoded_actions = decode_cellular_action_block(
|
|
*action_bytes, owner.tick(), owner.tick() + 2);
|
|
CHECK(decoded_actions && *decoded_actions == std::vector<CellularActionRecord>{action});
|
|
CHECK(match_cellular_actions_to_trace(owner, *decoded_batch, *decoded_actions));
|
|
auto changed_action = action;
|
|
changed_action.input[8] = 1;
|
|
const std::array changed_actions{changed_action};
|
|
CHECK(match_cellular_actions_to_trace(owner, *decoded_batch, changed_actions).error() ==
|
|
CellularActionBlockError::mismatch);
|
|
const auto replayed_batch = replay_cellular_batch(owner, *decoded_batch);
|
|
CHECK(replayed_batch && (*replayed_batch)->tick() == owner.tick() + 2);
|
|
CHECK((*replayed_batch)->current_total() == owner.opening_total());
|
|
auto short_batch = *batch_bytes;
|
|
short_batch.pop_back();
|
|
CHECK(!decode_cellular_batch(short_batch));
|
|
auto noncanonical_batch = *batch_bytes;
|
|
noncanonical_batch[noncanonical_batch.size() - 1] = 1;
|
|
CHECK(!decode_cellular_batch(noncanonical_batch));
|
|
auto wrong_scope = replay_batch;
|
|
wrong_scope.header.region++;
|
|
CHECK(replay_cellular_batch(owner, wrong_scope).error() == CellularBatchError::scope);
|
|
|
|
std::size_t steps{};
|
|
while (!owner.lives().empty() && steps < 2'000) {
|
|
std::vector<CellularLifeIntent> idle;
|
|
idle.reserve(owner.lives().size());
|
|
for (const auto& life : owner.lives())
|
|
idle.push_back({life.identity.life, {}});
|
|
const auto advanced = owner.advance_tick(idle);
|
|
const auto replayed = (*recovered)->advance_tick(idle);
|
|
CHECK(advanced && replayed && *advanced == *replayed);
|
|
CHECK(owner.snapshot() == (*recovered)->snapshot());
|
|
CHECK(owner.current_total() == owner.opening_total());
|
|
++steps;
|
|
}
|
|
CHECK(owner.lives().empty() && owner.corpses().size() == 2 && steps < 2'000);
|
|
CHECK(owner.corpses()[0].organism == 101 && owner.corpses()[0].died_tick <= owner.tick());
|
|
CHECK(owner.corpses()[1].organism == 102 && owner.corpses()[1].died_tick <= owner.tick());
|
|
CHECK(owner.corpses()[0].life_id == 1 && owner.corpses()[0].source == 17 &&
|
|
owner.corpses()[0].growth == (evo::biology::GrowthState{1000, 0}) &&
|
|
owner.corpses()[0].extent_um == 20);
|
|
const auto lineage = owner.archive().organism(101);
|
|
CHECK(lineage && !lineage->alive && lineage->died.value == owner.corpses()[0].died_tick);
|
|
const auto second_lineage = owner.archive().organism(102);
|
|
CHECK(second_lineage && !second_lineage->alive &&
|
|
second_lineage->died.value == owner.corpses()[1].died_tick);
|
|
CHECK(owner.heat().energy.value != 0);
|
|
CHECK(owner.current_total() == owner.opening_total());
|
|
const auto terminal_snapshot = owner.snapshot();
|
|
auto terminal_recovery = CellularLives::restore(terminal_snapshot, 90, 1, 7, owner.tick());
|
|
CHECK(terminal_recovery && (*terminal_recovery)->snapshot() == terminal_snapshot);
|
|
CHECK((*terminal_recovery)->lives().empty() &&
|
|
std::ranges::equal((*terminal_recovery)->corpses(), owner.corpses()));
|
|
CHECK((*terminal_recovery)->heat() == owner.heat());
|
|
CHECK((*terminal_recovery)->archive().organism(101) == owner.archive().organism(101));
|
|
}
|
|
} // namespace
|
|
|
|
int main() {
|
|
try {
|
|
reserved_birth_is_funded_and_detached();
|
|
owner_advances_and_closes_death_atomically();
|
|
std::puts("PASS cellular lives: feeding, tick ownership and terminal closure");
|
|
} catch (const std::exception& error) {
|
|
std::fprintf(stderr, "%s\n", error.what());
|
|
return 1;
|
|
}
|
|
}
|