Files
aevolution/tests/primitive_lives_tests.cpp
T
dingo 48684d30d9
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m53s
Foundation / checks (client) (push) Failing after 9m57s
Foundation / checks (network) (push) Successful in 13m32s
Foundation / checks (tsan) (push) Successful in 7m56s
Foundation / checks (checks) (push) Successful in 33m49s
Foundation / checks (postgres) (push) Successful in 46m39s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped
M11.2b: restore funded life state with canonical closure checks
2026-10-09 18:30:58 -04:00

266 lines
12 KiB
C++

#include "evo/server/lifecycle/primitive_lives.hpp"
#include <cstdio>
#include <limits>
#include <source_location>
#include <stdexcept>
#include <string>
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("primitive lives line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
LifeRequest request(std::uint8_t account, std::uint64_t action = 1) {
LifeRequest r{};
r.account.back() = account;
r.action = action;
return r;
}
LifeAllocation allocation(std::uint64_t id) {
return {id, id + 100, id + 200, id + 300, {10}};
}
Inventory price(std::uint64_t count) {
const auto endowment = evo::biology::primitive_endowment();
CHECK(endowment);
auto cost = endowment->body;
cost.energy.value = endowment->reserve.energy.value + endowment->construction.value;
cost.energy.value *= count;
for (auto& channel : cost.matter)
channel.value *= count;
return cost;
}
NurseryConfig config(std::uint64_t funded = 2) {
return {90, 1, 7, 17, price(funded), {{0, 0}, {20, 0}}};
}
Inventory summed(std::span<const Inventory> balances) {
Inventory sum{};
for (const auto& b : balances) {
sum.energy.value += b.energy.value;
for (std::size_t c = 0; c < 4; ++c)
sum.matter[c].value += b.matter[c].value;
}
return sum;
}
struct Cut {
std::vector<Inventory> balances;
std::vector<PrimitiveLife> lives;
std::vector<std::uint8_t> archive;
auto operator<=>(const Cut&) const = default;
};
Cut cut(const PrimitiveLives& nursery) {
return {{nursery.balances().begin(), nursery.balances().end()},
{nursery.lives().begin(), nursery.lives().end()},
nursery.archive().encode()};
}
void funded_bodies_and_equal_starts() {
auto created = PrimitiveLives::create(config());
CHECK(created);
auto& nursery = **created;
const auto opening = nursery.totals();
CHECK(nursery.archive().recorded() == 0 && nursery.lives().empty());
const auto first = nursery.spawn(request(1), allocation(1));
// A large action number represents a returning account; it grants no power.
const auto second = nursery.spawn(request(2, 999'999), allocation(2));
CHECK(first && second && first->site == (SpawnSite{0, 0}) &&
second->site == (SpawnSite{20, 0}));
CHECK(first->growth == (evo::biology::GrowthState{1000, 0}));
CHECK(first->growth == second->growth && first->source == 17 && second->source == 17);
CHECK(nursery.balances()[first->body.value - 1] == nursery.balances()[second->body.value - 1]);
CHECK(nursery.balances()[first->reserve.value - 1] ==
nursery.balances()[second->reserve.value - 1]);
const auto a = nursery.archive().find(first->identity.genome);
const auto b = nursery.archive().find(second->identity.genome);
CHECK(a && b);
CHECK(evo::biology::metabolic_budget(*a, first->growth, {}) ==
evo::biology::metabolic_budget(*b, second->growth, {}));
CHECK(nursery.archive().organism(first->identity.organism)->born == evo::Tick{10});
CHECK(nursery.balances()[0] == Inventory{});
// Independently calculated body: structure 8e6 fg, membrane 2.88e6 fg.
const auto& body = nursery.balances()[first->body.value - 1];
CHECK(body.energy.value == 0 && body.matter[0].value == 6'016'000);
CHECK(body.matter[1].value == 1'344'000 && body.matter[2].value == 688'000);
CHECK(body.matter[3].value == 2'832'000);
CHECK(nursery.balances()[first->reserve.value - 1].energy.value == 7'824'000);
CHECK(nursery.balances()[1].energy.value == 2ULL * 435'200);
CHECK(summed(nursery.balances()) == opening && nursery.totals() == opening);
// Initial reserve is energy-only; phosphorus still binds the first bud.
const auto budget = evo::biology::metabolic_budget(*a, first->growth, {});
CHECK(budget);
CHECK(nursery.balances()[first->reserve.value - 1].matter == Inventory{}.matter);
for (const std::uint64_t share : {1000ULL, 500ULL, 333ULL}) {
const auto needed = budget->reproduction_investment.matter[2].value;
const auto rate = budget->intake_limit.matter[2].value * share / 1000;
const auto seconds = needed / rate + (needed % rate != 0 ? 1 : 0);
CHECK(seconds >= 300 && seconds <= 901);
}
}
void failures_and_retries_spend_nothing() {
auto made = PrimitiveLives::create(config());
CHECK(made);
auto& nursery = **made;
const auto first = nursery.spawn(request(1), allocation(1));
CHECK(first);
const auto before = cut(nursery);
CHECK(nursery.spawn(request(1), {}) == first); // Retry needs no fresh allocation.
CHECK(!nursery.spawn(request(1, 2), allocation(2)));
CHECK(!nursery.spawn(request(0), allocation(2)));
CHECK(!nursery.spawn(request(2, 0), allocation(2)));
CHECK(!nursery.spawn(request(2), {}));
for (int field = 0; field < 4; ++field) {
auto duplicate = allocation(2);
if (field == 0)
duplicate.life = 1;
if (field == 1)
duplicate.organism = 101;
if (field == 2)
duplicate.genome = 201;
if (field == 3)
duplicate.event = 301;
const auto result = nursery.spawn(request(2), duplicate);
CHECK(!result && result.error() == LifeError::identity_conflict);
}
CHECK(cut(nursery) == before);
CHECK(nursery.spawn(request(2), allocation(2)));
const auto full = cut(nursery);
const auto refused = nursery.spawn(request(3), allocation(3));
CHECK(!refused && refused.error() == LifeError::capacity && cut(nursery) == full);
CHECK(nursery.spawn(request(1), {}) == first); // Even when full and source empty.
CHECK(cut(nursery) == full);
}
void every_channel_must_be_funded() {
for (std::size_t channel = 0; channel < 5; ++channel) {
auto c = config(1);
if (channel == 0)
--c.funding.energy.value;
else
--c.funding.matter[channel - 1].value;
auto made = PrimitiveLives::create(c);
CHECK(made);
const auto before = cut(**made);
const auto result = (*made)->spawn(request(1), allocation(1));
CHECK(!result && result.error() == LifeError::insufficient_funding);
CHECK(cut(**made) == before);
}
auto made = PrimitiveLives::create(config(1));
CHECK(made && (*made)->spawn(request(1), allocation(1)));
const auto before = cut(**made);
CHECK(!(*made)->spawn(request(2), allocation(2)) && cut(**made) == before);
}
void invalid_sites_and_bounds() {
auto c = config();
c.sites[1] = {19, 19}; // 20 um square founder bodies overlap on both axes.
CHECK(!PrimitiveLives::create(c));
c.sites[1] = {0, 0};
CHECK(!PrimitiveLives::create(c));
c.sites = {{20, 0}, {0, 0}};
CHECK(!PrimitiveLives::create(c));
c.sites = {{std::numeric_limits<std::int32_t>::min(), 0}};
CHECK(!PrimitiveLives::create(c));
c.sites.clear();
CHECK(!PrimitiveLives::create(c));
c = config();
c.world = 0;
CHECK(!PrimitiveLives::create(c));
c = config();
c.sites.resize(max_primitive_lives + 1);
CHECK(!PrimitiveLives::create(c));
c = config();
c.funding = {};
auto empty = PrimitiveLives::create(c);
CHECK(empty && !(*empty)->spawn(request(1), allocation(1)));
// Huge valid opening stays exactly conserved, without summing channels into one mass.
c.funding.energy.value = std::numeric_limits<std::uint64_t>::max();
for (auto& m : c.funding.matter)
m.value = std::numeric_limits<std::uint64_t>::max();
auto huge = PrimitiveLives::create(c);
CHECK(huge && (*huge)->spawn(request(1), allocation(1)));
CHECK(summed((*huge)->balances()) == c.funding);
}
void reseal(std::vector<std::uint8_t>& bytes) {
std::uint64_t value = 14695981039346656037ULL;
for (std::size_t i = 0; i < bytes.size() - 8; ++i)
value = (value ^ bytes[i]) * 1099511628211ULL;
for (std::size_t i = bytes.size() - 8; i < bytes.size(); ++i, value >>= 8U)
bytes[i] = static_cast<std::uint8_t>(value & 255U);
}
void recover_complete_creation_state() {
auto made = PrimitiveLives::create(config());
CHECK(made);
const auto genesis = (*made)->snapshot();
auto empty = PrimitiveLives::restore(genesis, 90, 1, 7);
CHECK(empty && (*empty)->snapshot() == genesis && (*empty)->lives().empty());
const auto first = (*made)->spawn(request(1), allocation(1));
CHECK(first);
const auto bytes = (*made)->snapshot();
CHECK(bytes.size() <= max_primitive_lives_bytes);
auto restored = PrimitiveLives::restore(bytes, 90, 1, 7);
CHECK(restored && (*restored)->snapshot() == bytes && cut(**restored) == cut(**made));
CHECK((*restored)->spawn(request(1), {}) == first);
CHECK((*restored)->snapshot() == bytes);
CHECK((*restored)->spawn(request(2), allocation(2)) ==
(*made)->spawn(request(2), allocation(2)));
CHECK((*restored)->snapshot() == (*made)->snapshot());
CHECK(!PrimitiveLives::restore(bytes, 91, 1, 7));
CHECK(!PrimitiveLives::restore(bytes, 90, 2, 7));
CHECK(!PrimitiveLives::restore(bytes, 90, 1, 8));
// Every truncation is refused without reading past the input under ASan.
for (std::size_t size = 0; size < bytes.size(); ++size)
CHECK(!PrimitiveLives::restore(std::span{bytes}.first(size), 90, 1, 7));
auto changed = bytes;
changed.push_back(0);
CHECK(!PrimitiveLives::restore(changed, 90, 1, 7));
// Resealing distinguishes invariant validation from checksum detection.
const auto rejects = [&](std::size_t offset, LifeCodecError error) {
auto altered = bytes;
altered[offset] ^= 1U;
reseal(altered);
const auto result = PrimitiveLives::restore(altered, 90, 1, 7);
CHECK(!result && result.error() == error);
};
rejects(0, LifeCodecError::format); // Magic.
rejects(8, LifeCodecError::version); // Format.
rejects(12, LifeCodecError::version); // Rules.
rejects(48, LifeCodecError::version); // Quantum.
rejects(108, LifeCodecError::format); // Reserved.
rejects(96, LifeCodecError::format); // Site count/length disagreement.
rejects(100, LifeCodecError::format); // Life count/length disagreement.
rejects(104, LifeCodecError::format); // Archive length.
rejects(128 + 64, LifeCodecError::closure); // Source provenance.
rejects(128 + 72, LifeCodecError::closure); // Resolved placement.
rejects(128 + 80, LifeCodecError::closure); // Growth.
rejects(128 + 84, LifeCodecError::closure); // Damage.
rejects(128 + 88, LifeCodecError::closure); // Body ownership.
rejects(128 + 92, LifeCodecError::closure); // Reserve ownership.
rejects(128 + 96, LifeCodecError::closure); // Funding balance.
rejects(bytes.size() - 20, LifeCodecError::closure); // Archived history.
changed = bytes;
changed[128] ^= 1U;
CHECK(!PrimitiveLives::restore(changed, 90, 1, 7)); // Unsealed corruption.
changed.assign(max_primitive_lives_bytes + 1, 0);
CHECK(!PrimitiveLives::restore(changed, 90, 1, 7));
}
} // namespace
int main() try {
funded_bodies_and_equal_starts();
failures_and_retries_spend_nothing();
every_channel_must_be_funded();
invalid_sites_and_bounds();
recover_complete_creation_state();
std::puts("PASS primitive lives: funded bodies, equal starts, atomic refusals and retries");
return 0;
} catch (const std::exception& e) {
std::fprintf(stderr, "%s\n", e.what());
return 1;
}