Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m53s
Foundation / checks (checks) (push) Failing after 4m11s
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 (postgres) (push) Canceled after 4m28s
Foundation / checks (network) (push) Canceled after 6m39s
Foundation / checks (client) (push) Canceled after 8m43s
359 lines
16 KiB
C++
359 lines
16 KiB
C++
#include "evo/server/lifecycle/primitive_batch.hpp"
|
|
#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));
|
|
}
|
|
void creation_batches_are_atomic_and_replayable() {
|
|
PrimitiveBatch batch;
|
|
batch.header = {90, 1, 7, {}, {}, 0, 20};
|
|
batch.header.predecessor.back() = 1;
|
|
batch.header.predecessor_digest.back() = 2;
|
|
batch.births = {{request(1), 101, 9, {5}}, {request(2), 102, 10, {10}}};
|
|
const auto encoded = encode_primitive_batch(batch);
|
|
CHECK(encoded && encoded->size() == primitive_batch_header_bytes + 2 * primitive_birth_bytes);
|
|
const auto decoded = decode_primitive_batch(*encoded);
|
|
CHECK(decoded && *decoded == batch);
|
|
CHECK(encode_primitive_batch(*decoded) == *encoded);
|
|
auto base = PrimitiveLives::create(config());
|
|
CHECK(base);
|
|
const auto opening = (*base)->snapshot();
|
|
auto replayed = replay_primitive_batch(**base, batch);
|
|
CHECK(replayed && (*replayed)->lives().size() == 2);
|
|
CHECK((*base)->snapshot() == opening);
|
|
CHECK((*replayed)->totals() == (*base)->totals());
|
|
auto direct = PrimitiveLives::create(config());
|
|
CHECK(direct);
|
|
CHECK((*direct)->spawn(request(1), {101, 101, 101, 101, {5}}));
|
|
CHECK((*direct)->spawn(request(2), {102, 102, 102, 102, {10}}));
|
|
CHECK((*direct)->snapshot() == (*replayed)->snapshot());
|
|
// Failure of a later birth discards the earlier funded birth too.
|
|
auto scarce = PrimitiveLives::create(config(1));
|
|
CHECK(scarce);
|
|
const auto before = (*scarce)->snapshot();
|
|
CHECK(!replay_primitive_batch(**scarce, batch));
|
|
CHECK((*scarce)->snapshot() == before);
|
|
auto wrong_scope = batch;
|
|
wrong_scope.header.region = 2;
|
|
CHECK(!replay_primitive_batch(**base, wrong_scope));
|
|
CHECK(!replay_primitive_batch(**replayed, batch)); // Base includes births after begin.
|
|
auto idle = batch;
|
|
idle.header.begin_tick = 20;
|
|
idle.header.end_tick = 40;
|
|
idle.births.clear();
|
|
const auto idle_bytes = encode_primitive_batch(idle);
|
|
CHECK(idle_bytes && idle_bytes->size() == primitive_batch_header_bytes);
|
|
auto advanced = replay_primitive_batch(**replayed, idle);
|
|
CHECK(advanced && (*advanced)->snapshot() == (*replayed)->snapshot());
|
|
auto duplicate = idle;
|
|
duplicate.births = {{request(1), 101, 11, {21}}};
|
|
CHECK(!replay_primitive_batch(**replayed, duplicate)); // Retry isn't a new birth.
|
|
|
|
for (std::size_t size = 0; size < encoded->size(); ++size)
|
|
CHECK(!decode_primitive_batch(std::span{*encoded}.first(size)));
|
|
auto trailing = *encoded;
|
|
trailing.push_back(0);
|
|
CHECK(!decode_primitive_batch(trailing));
|
|
CHECK(!decode_primitive_batch(std::vector<std::uint8_t>(max_primitive_batch_bytes + 1)));
|
|
for (const auto offset : {std::size_t{0}, std::size_t{8}, std::size_t{12}, std::size_t{104}}) {
|
|
auto bad = *encoded;
|
|
bad[offset] ^= 1U;
|
|
CHECK(!decode_primitive_batch(bad));
|
|
}
|
|
const auto refuses = [&](auto mutate) {
|
|
auto bad = batch;
|
|
mutate(bad);
|
|
CHECK(!encode_primitive_batch(bad));
|
|
CHECK(!replay_primitive_batch(**base, bad));
|
|
CHECK((*base)->snapshot() == opening);
|
|
};
|
|
refuses([](auto& b) { b.header.world = 0; });
|
|
refuses([](auto& b) { b.header.predecessor = {}; });
|
|
refuses([](auto& b) { b.header.predecessor_digest = {}; });
|
|
refuses([](auto& b) { b.header.begin_tick = 20; });
|
|
refuses([](auto& b) { b.header.end_tick = 65; });
|
|
refuses([](auto& b) { b.births[0].born.value = 0; });
|
|
refuses([](auto& b) { b.births[1].born.value = 21; });
|
|
refuses([](auto& b) { b.births[0].generation = 0; });
|
|
refuses([](auto& b) { b.births[0].identity = 0; });
|
|
refuses([](auto& b) { b.births[0].request.action = 0; });
|
|
refuses([](auto& b) { b.births[0].request.account = {}; });
|
|
refuses([](auto& b) { b.births[1].identity = b.births[0].identity; });
|
|
refuses([](auto& b) { b.births[1].request.account = b.births[0].request.account; });
|
|
refuses([](auto& b) { std::swap(b.births[0], b.births[1]); });
|
|
auto bound = batch;
|
|
bound.births.clear();
|
|
for (std::size_t i = 1; i <= max_primitive_lives; ++i) {
|
|
auto r = request(1);
|
|
r.account[14] = static_cast<std::uint8_t>(i >> 8U);
|
|
r.account[15] = static_cast<std::uint8_t>(i & 255U);
|
|
bound.births.push_back({r, i, 1, {1}});
|
|
}
|
|
auto maximum = encode_primitive_batch(bound);
|
|
CHECK(maximum && maximum->size() == max_primitive_batch_bytes);
|
|
CHECK(decode_primitive_batch(*maximum) == bound);
|
|
bound.births.push_back(bound.births.back());
|
|
CHECK(!encode_primitive_batch(bound));
|
|
}
|
|
} // 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();
|
|
creation_batches_are_atomic_and_replayable();
|
|
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;
|
|
}
|