Foundation / checks (client) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (checks) (push) Failing after 41s
Foundation / checks (postgres) (push) Failing after 50s
Foundation / checks (tsan) (push) Failing after 32s
Foundation / checks (ecs) (push) Successful in 1m55s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped
117 lines
4.9 KiB
C++
117 lines
4.9 KiB
C++
// Profile-4 publication and recovery against the migration-backed PostgreSQL adapter.
|
|
#include "evo/persistence/checkpoint_store.hpp"
|
|
#include "evo/persistence/sha256.hpp"
|
|
|
|
#include <cstdlib>
|
|
#include <source_location>
|
|
#include <stdexcept>
|
|
|
|
void run_cellular_publication_tests();
|
|
namespace {
|
|
using namespace evo::persistence;
|
|
using namespace evo::server;
|
|
using namespace std::chrono_literals;
|
|
void check(bool value, std::source_location at = std::source_location::current()) {
|
|
if (!value)
|
|
throw std::runtime_error("cellular publication test line " + std::to_string(at.line()));
|
|
}
|
|
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
|
|
std::string env(const char* name) {
|
|
const auto* value = std::getenv(name);
|
|
if (!value)
|
|
throw std::runtime_error("missing PostgreSQL test environment");
|
|
return value;
|
|
}
|
|
auto connect_as(const std::string& user) {
|
|
auto db = Postgres::connect(
|
|
{env("PGHOST"), {}, env("PGPORT"), env("PGDATABASE"), user, {}, {}}, 5s);
|
|
CHECK(db);
|
|
return std::move(*db);
|
|
}
|
|
template <std::size_t N> std::array<std::uint8_t, N> filled(std::uint8_t value) {
|
|
std::array<std::uint8_t, N> out{};
|
|
out.fill(value);
|
|
return out;
|
|
}
|
|
std::string hex(std::span<const std::uint8_t> bytes) {
|
|
constexpr char digits[] = "0123456789abcdef";
|
|
std::string out = "\\x";
|
|
for (const auto byte : bytes) {
|
|
out.push_back(digits[byte >> 4U]);
|
|
out.push_back(digits[byte & 15U]);
|
|
}
|
|
return out;
|
|
}
|
|
} // namespace
|
|
|
|
void run_cellular_publication_tests() {
|
|
auto owner = connect_as(env("PGUSER"));
|
|
auto db = connect_as(env("EVO_PG_RUNTIME_USER"));
|
|
constexpr RegionKey key{9756, 1};
|
|
const auto build = filled<32>(0xD9);
|
|
CHECK(owner->query("INSERT INTO evolution.retained_builds(build_identity) VALUES "
|
|
"(decode(repeat('d9',32),'hex')) ON CONFLICT DO NOTHING",
|
|
{}, 5s));
|
|
|
|
auto endowed = evo::biology::primitive_endowment();
|
|
CHECK(endowed);
|
|
auto funding = endowed->body;
|
|
funding.energy.value = endowed->reserve.energy.value + endowed->construction.value;
|
|
auto primitive = PrimitiveLives::create({key.world, key.region, 1, 17, funding, {{0, 0}}});
|
|
CHECK(primitive);
|
|
LifeRequest founder;
|
|
founder.account = filled<16>(0x43);
|
|
founder.action = 7;
|
|
CHECK((*primitive)->spawn(founder, {1, 101, 201, 301, {0}}));
|
|
auto habitat = evo::biology::CellularHabitat::create(
|
|
{key.world, key.region, 1, 1'000, 1'000, {}});
|
|
CHECK(habitat);
|
|
auto opening = CellularLives::create(**primitive, std::move(*habitat),
|
|
{{1'000, 1'000}, 0, 293'150});
|
|
CHECK(opening && (*opening)->lives().size() == 1);
|
|
|
|
const auto payload = (*opening)->snapshot();
|
|
const auto payload_digest = sha256(payload);
|
|
CHECK(payload_digest);
|
|
const auto genesis = genesis_manifest(key, 1, filled<16>(0x21), 50'000, build,
|
|
payload.size(), *payload_digest,
|
|
CheckpointProfile::cellular_lives_v1);
|
|
CHECK(genesis);
|
|
const auto genesis_bytes = encode_manifest(*genesis);
|
|
CHECK(genesis_bytes);
|
|
auto objects = FilesystemObjectStore::open(env("PGHOST"), key, max_cellular_lives_bytes);
|
|
CHECK(objects && (*objects)->put(payload));
|
|
CHECK(owner->query("SELECT evolution.provision_region_checkpoint($1::bytea)",
|
|
std::array{hex(*genesis_bytes)}, 5s));
|
|
auto predecessor = load_region_checkpoint(*owner, key, 5s);
|
|
CHECK(predecessor);
|
|
const auto lease = acquire_region_lease(*db, key, build, 20'000ms, 5s);
|
|
CHECK(lease);
|
|
|
|
const auto predecessor_digest = sha256(predecessor->manifest);
|
|
CHECK(predecessor_digest);
|
|
CellularBatch batch;
|
|
batch.header = {key.world, key.region, 1, predecessor->head.commit, *predecessor_digest, 0, 2};
|
|
batch.ticks.resize(2);
|
|
for (auto& tick : batch.ticks)
|
|
tick.lives.push_back({(*opening)->lives()[0].identity.life, {}});
|
|
const auto replay = replay_cellular_batch(**opening, batch);
|
|
CHECK(replay);
|
|
CellularActionRecord action;
|
|
action.account = founder.account;
|
|
action.action_id = 77;
|
|
action.input[0] = 1; // Tick 1, neutral movement, no patch target.
|
|
const std::array actions{action};
|
|
auto published = publish_cellular_batch(*db, **objects, *lease, *predecessor, batch,
|
|
**replay, filled<16>(0x22), build, 5s, {}, actions);
|
|
CHECK(published && published->committed.head.sequence == 1 &&
|
|
published->committed.head.end_tick == 2);
|
|
|
|
auto recovered = recover_cellular_checkpoint(*db, **objects, key, *lease,
|
|
std::span{&build, 1}, 5s);
|
|
CHECK(recovered && recovered->manifest.sequence == 1 && recovered->state->tick() == 2 &&
|
|
recovered->state->snapshot() == (**replay).snapshot() &&
|
|
recovered->actions == std::vector<CellularActionRecord>{action});
|
|
CHECK(release_region_lease(*db, key, *lease, 5s));
|
|
}
|