Foundation / checks (ecs) (push) Successful in 2m22s
Foundation / checks (deploy) (push) Successful in 4m17s
Foundation / checks (network) (push) Successful in 21m4s
Foundation / checks (client) (push) Successful in 22m10s
Foundation / checks (tsan) (push) Successful in 28m58s
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 42m32s
Foundation / checks (checks) (push) Canceled after 42m39s
The DEV server still runs a build that sends every session an empty view each tick around the owner's real one. fcc6c57's client guard covered only an empty view after the real one of the same tick; the next tick's empty view arriving first still made the cell vanish for a frame, so the no-cell panel blinked a few times a minute. While a cell is shown, the client now counts empty views only after 20 ticks (one second) with no real view. A death is unaffected: it arrives as CellularLifeEnded. The session test covers both orders and the grace running out. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1679 lines
82 KiB
C++
1679 lines
82 KiB
C++
// M10.3b session login over QUIC (ADR-0091): grants verified by the region, newest grant
|
|
// wins, replay and stale grants, scope, expiry, refresh, epochs, rate and capacity
|
|
// limits, malformed and silent peers, client close and shutdown. The wall clock used for
|
|
// grants is injected; deadlines use the real monotonic clock.
|
|
#include "evo/client/region_session.hpp"
|
|
#include "evo/network/dev_credentials.hpp"
|
|
#include "evo/network/handoff_ticket.hpp"
|
|
#include "evo/network/session_grant.hpp"
|
|
#include "evo/server/probe_ledger.hpp"
|
|
#include "evo/server/region_sessions.hpp"
|
|
#include "fixtures/fake_handoff_world.hpp"
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <chrono>
|
|
#include <cstdio>
|
|
#include <expected>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <functional>
|
|
#include <map>
|
|
#include <optional>
|
|
#include <source_location>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
using namespace evo;
|
|
using namespace evo::network;
|
|
using client::RegionSessionClient;
|
|
using client::RegionSessionState;
|
|
using server::RegionSessionEvent;
|
|
using server::RegionSessions;
|
|
using Clock = std::chrono::steady_clock;
|
|
using Reason = SessionCloseReason;
|
|
|
|
void check(bool ok, std::source_location at = std::source_location::current()) {
|
|
if (!ok)
|
|
throw std::runtime_error("quic sessions line " + std::to_string(at.line()));
|
|
}
|
|
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
|
|
template <typename T>
|
|
const T& must(const std::optional<T>& o,
|
|
std::source_location at = std::source_location::current()) {
|
|
if (!o)
|
|
throw std::runtime_error("quic sessions missing value line " + std::to_string(at.line()));
|
|
return *o;
|
|
}
|
|
constexpr std::string_view name = "dev-region.evolution.test";
|
|
constexpr world::WorldIdentity world_id{7, 3};
|
|
constexpr std::uint64_t region = 11;
|
|
constexpr std::uint64_t start = 1'800'000'000;
|
|
|
|
struct Files {
|
|
std::filesystem::path dir;
|
|
std::string ca_pem;
|
|
std::filesystem::path ca, certificate, key;
|
|
};
|
|
Files credentials() {
|
|
const auto made = generate_dev_credentials(DeploymentEnvironment::dev, name, 1,
|
|
std::chrono::system_clock::now());
|
|
CHECK(made.has_value());
|
|
Files f;
|
|
f.dir = std::filesystem::temp_directory_path() / "evo-quic-sessions";
|
|
std::filesystem::remove_all(f.dir);
|
|
std::filesystem::create_directories(f.dir);
|
|
f.ca_pem = made->authority_pem;
|
|
f.ca = f.dir / "ca.pem";
|
|
f.certificate = f.dir / "server.pem";
|
|
f.key = f.dir / "server.key";
|
|
for (const auto& [path, text] : {std::pair{f.ca, made->authority_pem},
|
|
std::pair{f.certificate, made->server_certificate_pem},
|
|
std::pair{f.key, made->server_key_pem}}) {
|
|
std::ofstream out{path, std::ios::binary | std::ios::trunc};
|
|
out << text;
|
|
CHECK(out.good());
|
|
}
|
|
return f;
|
|
}
|
|
|
|
AccountId account(std::uint8_t tag) {
|
|
AccountId a;
|
|
a.bytes.fill(tag);
|
|
return a;
|
|
}
|
|
// The issuer's dev key from a fixed scalar, so the same key can also sign as "stage".
|
|
std::array<std::uint8_t, grant_private_key_bytes> scalar() {
|
|
std::array<std::uint8_t, grant_private_key_bytes> s{};
|
|
s.back() = 0x2A;
|
|
return s;
|
|
}
|
|
struct Issuer {
|
|
GrantSigningKey key = *GrantSigningKey::import(DeploymentEnvironment::dev, 1, scalar());
|
|
WireGrant grant(const AccountId& who, std::uint64_t serial, std::uint64_t issued = start,
|
|
std::uint64_t lifetime = 60, std::uint64_t in_region = region) const {
|
|
GrantClaims c;
|
|
c.grant_id = 500 + serial;
|
|
c.account = who;
|
|
c.serial = serial;
|
|
c.world = world_id;
|
|
c.region = in_region;
|
|
c.issued_at = issued;
|
|
c.expires_at = issued + lifetime;
|
|
const auto g = key.sign(c);
|
|
CHECK(g.has_value());
|
|
return *g;
|
|
}
|
|
};
|
|
|
|
struct Harness {
|
|
explicit Harness(const Files& f, std::size_t max_sessions = 4,
|
|
std::uint32_t region_attempts = 64, ControlLedger* ledger = nullptr,
|
|
const std::function<void(server::RegionSessionsConfig&)>& tweak = {})
|
|
: files{f} {
|
|
server::RegionSessionsConfig c;
|
|
c.transport.certificate_pem_file = f.certificate.string();
|
|
c.transport.key_pem_file = f.key.string();
|
|
c.control.environment = DeploymentEnvironment::dev;
|
|
c.control.world = world_id;
|
|
c.control.region = region;
|
|
c.control.epoch = 5;
|
|
c.control.max_sessions = max_sessions;
|
|
c.control.max_account_attempts = 16;
|
|
c.control.max_region_attempts = region_attempts;
|
|
c.issuer_keys.push_back({1, *issuer.key.public_key()});
|
|
c.hello_deadline = std::chrono::milliseconds{500};
|
|
c.close_flush = std::chrono::milliseconds{300};
|
|
c.ledger = ledger;
|
|
c.renew_interval = std::chrono::milliseconds{100};
|
|
c.safety_margin = std::chrono::milliseconds{50};
|
|
if (tweak)
|
|
tweak(c);
|
|
auto made = RegionSessions::create(std::move(c));
|
|
CHECK(made.has_value());
|
|
server = std::move(*made);
|
|
}
|
|
std::unique_ptr<RegionSessionClient>
|
|
connect(const WireGrant& grant, std::uint16_t session_version = session_protocol_version) {
|
|
client::RegionSessionClientConfig c;
|
|
c.transport.port = server->port();
|
|
c.transport.server_name = std::string{name};
|
|
c.transport.authority_pem = files.ca_pem;
|
|
c.transport.authority_pem_file = files.ca.string();
|
|
c.grant = grant;
|
|
c.protocol_version = session_version;
|
|
auto made = RegionSessionClient::connect(std::move(c), Clock::now());
|
|
CHECK(made.has_value());
|
|
clients.push_back(made->get());
|
|
return std::move(*made);
|
|
}
|
|
std::unique_ptr<quic::Endpoint> raw() {
|
|
quic::ClientConfig c;
|
|
c.port = server->port();
|
|
c.server_name = std::string{name};
|
|
c.authority_pem = files.ca_pem;
|
|
c.authority_pem_file = files.ca.string();
|
|
auto made = quic::Endpoint::connect(c);
|
|
CHECK(made.has_value());
|
|
return std::move(*made);
|
|
}
|
|
void pump() {
|
|
server->poll({ready, epoch, tick}, now_seconds, Clock::now());
|
|
if (source && tick != replicated) { // One snapshot per simulated tick (ADR-0093).
|
|
replicated = tick;
|
|
server->replicate(tick, source);
|
|
}
|
|
for (auto& e : server->take_events())
|
|
events.push_back(std::move(e));
|
|
for (auto* c : clients)
|
|
c->poll(Clock::now());
|
|
}
|
|
bool until(const std::function<bool()>& done,
|
|
std::chrono::milliseconds limit = std::chrono::milliseconds{10000}) {
|
|
const auto end = Clock::now() + limit;
|
|
while (Clock::now() < end) {
|
|
pump();
|
|
if (done())
|
|
return true;
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{1});
|
|
}
|
|
pump();
|
|
return done();
|
|
}
|
|
bool settled(RegionSessionClient& c) {
|
|
return until([&] {
|
|
return c.state() == RegionSessionState::established ||
|
|
c.state() == RegionSessionState::closed;
|
|
});
|
|
}
|
|
bool has_event(RegionSessionEvent::Kind kind, Reason reason, std::uint64_t session = 0) const {
|
|
return std::ranges::any_of(events, [&](const RegionSessionEvent& e) {
|
|
return e.kind == kind &&
|
|
(kind == RegionSessionEvent::Kind::opened || e.reason == reason) &&
|
|
(session == 0 || e.session == session);
|
|
});
|
|
}
|
|
void forget(const RegionSessionClient* c) {
|
|
std::erase(clients, c);
|
|
}
|
|
const Files& files;
|
|
Issuer issuer;
|
|
std::unique_ptr<RegionSessions> server;
|
|
std::vector<RegionSessionClient*> clients;
|
|
std::vector<RegionSessionEvent> events;
|
|
std::uint64_t epoch{5}, now_seconds{start}, tick{100};
|
|
bool ready{true};
|
|
std::function<ReplicationSource(const SessionInterest&)> source;
|
|
std::uint64_t replicated{};
|
|
};
|
|
|
|
bool refused_with(Harness& h, const WireGrant& grant, Reason reason) {
|
|
auto c = h.connect(grant);
|
|
const bool ok = h.settled(*c) && c->state() == RegionSessionState::closed &&
|
|
c->close_reason() == reason && !c->welcome();
|
|
h.forget(c.get());
|
|
return ok;
|
|
}
|
|
|
|
// A raw QUIC peer: sends `bytes` (or nothing) and returns the server's Close reason.
|
|
std::optional<Reason> raw_peer(Harness& h, const std::optional<std::vector<std::uint8_t>>& bytes) {
|
|
auto peer = h.raw();
|
|
std::optional<Reason> reason;
|
|
quic::ConnectionId id = 0;
|
|
const bool done = h.until([&] {
|
|
for (auto& e : peer->poll()) {
|
|
if (e.kind == quic::EventKind::connected) {
|
|
id = e.connection;
|
|
if (bytes)
|
|
CHECK(peer->send_message(id, *bytes).has_value());
|
|
} else if (e.kind == quic::EventKind::message) {
|
|
const auto m = decode_session(e.bytes);
|
|
if (m && std::holds_alternative<SessionClose>(*m))
|
|
reason = std::get<SessionClose>(*m).reason;
|
|
}
|
|
}
|
|
return reason.has_value();
|
|
});
|
|
CHECK(done);
|
|
return reason;
|
|
}
|
|
|
|
void login_rules(const Files& f) {
|
|
Harness h{f};
|
|
const auto alice = account(1), bob = account(2), carol = account(3);
|
|
// Login and Welcome.
|
|
auto a1 = h.connect(h.issuer.grant(alice, 1));
|
|
CHECK(h.settled(*a1) && a1->state() == RegionSessionState::established);
|
|
const auto& w = must(a1->welcome());
|
|
CHECK(w.epoch == 5 && w.world == world_id.world && w.frame == world_id.frame &&
|
|
w.region == region && w.expires_at == start + 60 && w.server_seconds == start);
|
|
CHECK(h.server->holder(alice) == w.session && h.server->sessions() == 1);
|
|
CHECK(h.has_event(RegionSessionEvent::Kind::opened, {}, w.session));
|
|
CHECK(std::ranges::all_of(h.events, [&](const RegionSessionEvent& e) {
|
|
return e.account.empty() || e.account == log_label(alice) || e.account == log_label(bob) ||
|
|
e.account == log_label(carol);
|
|
}));
|
|
// Replay of the same grant.
|
|
CHECK(refused_with(h, h.issuer.grant(alice, 1), Reason::stale_grant));
|
|
CHECK(h.server->holder(alice) == w.session);
|
|
// Duplicate login: the newer grant takes over; the old connection is told why.
|
|
auto a2 = h.connect(h.issuer.grant(alice, 3));
|
|
CHECK(h.settled(*a2) && a2->state() == RegionSessionState::established);
|
|
CHECK(h.until([&] { return a1->state() == RegionSessionState::closed; }));
|
|
CHECK(a1->close_reason() == Reason::superseded);
|
|
CHECK(h.has_event(RegionSessionEvent::Kind::closed, Reason::superseded, w.session));
|
|
CHECK(h.server->holder(alice) == must(a2->welcome()).session && h.server->sessions() == 1);
|
|
// A stolen older grant (serial 2, still unexpired) cannot take control back.
|
|
CHECK(refused_with(h, h.issuer.grant(alice, 2), Reason::stale_grant));
|
|
CHECK(a2->state() == RegionSessionState::established);
|
|
// Refresh in place: accepted, then stale, then another account's grant.
|
|
CHECK(a2->refresh(h.issuer.grant(alice, 4, start + 30)));
|
|
std::optional<SessionRefreshResult> r;
|
|
CHECK(h.until([&] { return (r = a2->take_refresh_result()).has_value(); }));
|
|
CHECK(must(r).status == 0 && must(r).expires_at == start + 90 &&
|
|
must(a2->welcome()).expires_at == start + 90);
|
|
CHECK(a2->refresh(h.issuer.grant(alice, 4, start + 30)));
|
|
CHECK(h.until([&] { return (r = a2->take_refresh_result()).has_value(); }));
|
|
CHECK(must(r).status == static_cast<std::uint8_t>(Reason::stale_grant));
|
|
CHECK(a2->refresh(h.issuer.grant(bob, 9)));
|
|
CHECK(h.until([&] { return (r = a2->take_refresh_result()).has_value(); }));
|
|
CHECK(must(r).status == static_cast<std::uint8_t>(Reason::unauthenticated));
|
|
CHECK(a2->state() == RegionSessionState::established &&
|
|
must(a2->welcome()).expires_at == start + 90);
|
|
// Scope and authenticity.
|
|
CHECK(refused_with(h, h.issuer.grant(bob, 1, start, 60, region + 1), Reason::wrong_scope));
|
|
const auto stage = *GrantSigningKey::import(DeploymentEnvironment::stage, 1, scalar());
|
|
GrantClaims staged;
|
|
staged.grant_id = 1;
|
|
staged.account = bob;
|
|
staged.serial = 1;
|
|
staged.world = world_id;
|
|
staged.region = region;
|
|
staged.issued_at = start;
|
|
staged.expires_at = start + 60;
|
|
CHECK(refused_with(h, *stage.sign(staged), Reason::wrong_scope)); // Stage grant, dev region.
|
|
const auto stranger = *GrantSigningKey::generate(DeploymentEnvironment::dev, 1);
|
|
CHECK(refused_with(h, *stranger.sign(staged), Reason::unauthenticated)); // Not our issuer.
|
|
auto forged = h.issuer.grant(bob, 1);
|
|
forged.at(40) ^= 1;
|
|
CHECK(refused_with(h, forged, Reason::unauthenticated));
|
|
// Expiry at login and during a session (injected wall clock).
|
|
CHECK(refused_with(h, h.issuer.grant(bob, 1, start - 120), Reason::expired));
|
|
auto b = h.connect(h.issuer.grant(bob, 2));
|
|
CHECK(h.settled(*b) && b->state() == RegionSessionState::established);
|
|
h.now_seconds = start + 60; // Bob's grant ends; Alice refreshed until start + 90.
|
|
CHECK(h.until([&] { return b->state() == RegionSessionState::closed; }));
|
|
CHECK(b->close_reason() == Reason::expired && a2->state() == RegionSessionState::established);
|
|
// Client close releases control.
|
|
auto c = h.connect(h.issuer.grant(carol, 1, start + 60));
|
|
CHECK(h.settled(*c) && c->state() == RegionSessionState::established);
|
|
const auto carol_session = must(c->welcome()).session;
|
|
c->close();
|
|
CHECK(h.until([&] { return !h.server->holder(carol); }));
|
|
CHECK(h.has_event(RegionSessionEvent::Kind::closed, Reason::client_closed, carol_session));
|
|
// Malformed and silent peers.
|
|
CHECK(raw_peer(h, std::nullopt) == Reason::timeout);
|
|
CHECK(raw_peer(h, std::vector<std::uint8_t>{1, 2, 3}) == Reason::protocol);
|
|
auto future = encode_session(SessionHello{0, h.issuer.grant(carol, 2, start + 60)});
|
|
future.at(4) = 6;
|
|
CHECK(raw_peer(h, future) == Reason::unsupported_version);
|
|
const auto unnegotiated_v5 =
|
|
encode_session(SessionHello{0, h.issuer.grant(carol, 2, start + 60)}, session_protocol_v5);
|
|
CHECK(raw_peer(h, unnegotiated_v5) == Reason::unsupported_version);
|
|
CHECK(raw_peer(h, encode_session(SessionClose{Reason::client_closed})) == Reason::protocol);
|
|
CHECK(raw_peer(h, encode_session(SessionHello{1, h.issuer.grant(carol, 3, start + 60)})) ==
|
|
Reason::protocol); // Unknown required feature.
|
|
// A new ownership epoch ends every session.
|
|
h.epoch = 6;
|
|
CHECK(h.until([&] { return a2->state() == RegionSessionState::closed; }));
|
|
CHECK(a2->close_reason() == Reason::stale_epoch && h.server->sessions() == 0);
|
|
auto a3 = h.connect(h.issuer.grant(alice, 5, start + 60));
|
|
CHECK(h.settled(*a3) && must(a3->welcome()).epoch == 6);
|
|
// Shutdown tells every client.
|
|
h.server->shutdown(Clock::now());
|
|
CHECK(h.until([&] { return a3->state() == RegionSessionState::closed; }));
|
|
CHECK(a3->close_reason() == Reason::shutdown);
|
|
CHECK(h.until([&] { return h.server->connections() == 0; }));
|
|
}
|
|
|
|
void limits(const Files& f) {
|
|
{
|
|
Harness h{f, 1};
|
|
auto a = h.connect(h.issuer.grant(account(1), 1));
|
|
CHECK(h.settled(*a) && a->state() == RegionSessionState::established);
|
|
CHECK(refused_with(h, h.issuer.grant(account(2), 1), Reason::capacity));
|
|
a->close();
|
|
CHECK(h.until([&] { return h.server->sessions() == 0; }));
|
|
// The refused grant was not consumed.
|
|
auto b = h.connect(h.issuer.grant(account(2), 1));
|
|
CHECK(h.settled(*b) && b->state() == RegionSessionState::established);
|
|
}
|
|
{
|
|
Harness h{f, 4, 3};
|
|
for (std::uint64_t serial = 1; serial <= 3; ++serial) {
|
|
auto c = h.connect(h.issuer.grant(account(1), serial));
|
|
CHECK(h.settled(*c) && c->state() == RegionSessionState::established);
|
|
h.forget(c.get());
|
|
}
|
|
CHECK(refused_with(h, h.issuer.grant(account(1), 4), Reason::rate_limited));
|
|
}
|
|
}
|
|
// Scripted durable control (10.3c): records calls, answers as configured.
|
|
struct FakeLedger final : ControlLedger {
|
|
struct Claim {
|
|
AccountId account;
|
|
std::uint64_t serial{}, session{};
|
|
};
|
|
std::expected<ControlLease, ControlLedgerError>
|
|
claim(const AccountId& account, std::uint64_t serial, std::uint64_t session) override {
|
|
if (claim_error)
|
|
return std::unexpected(*claim_error);
|
|
claims.push_back({account, serial, session});
|
|
return ControlLease{next_generation++, wait, granted};
|
|
}
|
|
std::expected<std::optional<std::uint64_t>, ControlLedgerError>
|
|
primitive_life_owner(const AccountId& /*account*/) override {
|
|
if (owner_error)
|
|
return std::unexpected(*owner_error);
|
|
return life_owner;
|
|
}
|
|
std::expected<std::chrono::milliseconds, ControlLedgerError>
|
|
renew(const AccountId& /*account*/, std::uint64_t generation) override {
|
|
renewed.push_back(generation);
|
|
if (renew_error)
|
|
return std::unexpected(*renew_error);
|
|
return granted;
|
|
}
|
|
void release(const AccountId& /*account*/, std::uint64_t generation) override {
|
|
released.push_back(generation);
|
|
}
|
|
std::optional<ControlLedgerError> claim_error, renew_error, owner_error;
|
|
std::optional<std::uint64_t> life_owner;
|
|
std::chrono::milliseconds wait{}, granted{std::chrono::milliseconds{20000}};
|
|
std::uint64_t next_generation{1};
|
|
std::vector<Claim> claims;
|
|
std::vector<std::uint64_t> renewed, released;
|
|
};
|
|
|
|
void durable_life_owner_redirects_before_control_claim(const Files& f) {
|
|
FakeLedger ledger;
|
|
ledger.life_owner = 12;
|
|
Harness h{
|
|
f, 4, 64, &ledger, [](server::RegionSessionsConfig& c) {
|
|
c.create_primitive_life = [](const AccountId&, std::uint64_t, std::uint64_t request) {
|
|
return PrimitiveLifeOutcome{request, PrimitiveLifeResult::born, 77, 88};
|
|
};
|
|
c.query_primitive_life = [](const AccountId&, std::uint64_t, std::uint64_t request) {
|
|
return PrimitiveLifeOutcome{request, PrimitiveLifeResult::unknown, 0, 0};
|
|
};
|
|
c.life_owner_routes = {
|
|
server::LifeOwnerRoute{
|
|
RegionAddress{region, "region-11.example.test:7400", "region-11.example.test"}},
|
|
server::LifeOwnerRoute{
|
|
RegionAddress{12, "region-12.example.test:7400", "region-12.example.test"}}};
|
|
}};
|
|
const auto alice = account(0xB4);
|
|
auto client = h.connect(h.issuer.grant(alice, 1), session_protocol_v5);
|
|
CHECK(h.settled(*client) && client->state() == RegionSessionState::closed);
|
|
CHECK(client->close_reason() == Reason::moved && client->redirect());
|
|
CHECK(must(client->redirect()) ==
|
|
RegionAddress{12, "region-12.example.test:7400", "region-12.example.test"});
|
|
CHECK(ledger.claims.empty()); // The redirect happened before durable control was claimed.
|
|
|
|
FakeLedger incomplete_ledger;
|
|
incomplete_ledger.life_owner = 12;
|
|
Harness incomplete{
|
|
f, 4, 64, &incomplete_ledger, [](server::RegionSessionsConfig& c) {
|
|
c.create_primitive_life = [](const AccountId&, std::uint64_t, std::uint64_t request) {
|
|
return PrimitiveLifeOutcome{request, PrimitiveLifeResult::born, 77, 88};
|
|
};
|
|
c.query_primitive_life = [](const AccountId&, std::uint64_t, std::uint64_t request) {
|
|
return PrimitiveLifeOutcome{request, PrimitiveLifeResult::unknown, 0, 0};
|
|
};
|
|
c.life_owner_routes = {server::LifeOwnerRoute{
|
|
RegionAddress{region, "region-11.example.test:7400", "region-11.example.test"}}};
|
|
}};
|
|
auto no_route = incomplete.connect(incomplete.issuer.grant(alice, 2), session_protocol_v5);
|
|
CHECK(incomplete.settled(*no_route) && no_route->close_reason() == Reason::capacity);
|
|
CHECK(!no_route->redirect() && incomplete_ledger.claims.empty());
|
|
}
|
|
|
|
void primitive_v5_session(const Files& f) {
|
|
FakeLedger ledger;
|
|
Harness h{f, 4, 64, &ledger, [](server::RegionSessionsConfig& c) {
|
|
c.create_primitive_life = [](const AccountId& a, std::uint64_t generation,
|
|
std::uint64_t request) {
|
|
CHECK(a == account(0xA5) && generation == 1);
|
|
return PrimitiveLifeOutcome{request, PrimitiveLifeResult::born, 77, 88};
|
|
};
|
|
c.query_primitive_life = [](const AccountId&, std::uint64_t,
|
|
std::uint64_t request) {
|
|
return PrimitiveLifeOutcome{request, PrimitiveLifeResult::unknown, 0, 0};
|
|
};
|
|
}};
|
|
auto client = h.connect(h.issuer.grant(account(0xA5), 1), session_protocol_v5);
|
|
CHECK(h.settled(*client) && client->state() == RegionSessionState::established);
|
|
CHECK(client->request_primitive_life(42));
|
|
std::vector<PrimitiveLifeReceipt> receipts;
|
|
std::vector<PrimitiveLifeOutcome> outcomes;
|
|
CHECK(h.until([&] {
|
|
auto more_receipts = client->take_primitive_life_receipts();
|
|
auto more_outcomes = client->take_primitive_life_outcomes();
|
|
receipts.insert(receipts.end(), more_receipts.begin(), more_receipts.end());
|
|
outcomes.insert(outcomes.end(), more_outcomes.begin(), more_outcomes.end());
|
|
return !receipts.empty() && !outcomes.empty();
|
|
}));
|
|
CHECK(receipts.front() == PrimitiveLifeReceipt{42, AdmitStatus::admitted, 100});
|
|
CHECK(outcomes.front() == PrimitiveLifeOutcome{42, PrimitiveLifeResult::born, 77, 88});
|
|
CHECK(client->query_primitive_life(42));
|
|
CHECK(h.until([&] {
|
|
outcomes = client->take_primitive_life_outcomes();
|
|
return !outcomes.empty();
|
|
}));
|
|
CHECK(outcomes.front() == PrimitiveLifeOutcome{42, PrimitiveLifeResult::unknown, 0, 0});
|
|
CHECK(!client->send_action(99, ActionKind::probe, 101));
|
|
CHECK(!client->query_action(99));
|
|
CHECK(!client->send_intention(101, 1, 0));
|
|
CHECK(!ledger.claims.empty() && ledger.claims.back().account == account(0xA5));
|
|
}
|
|
|
|
void cellular_v6_session(const Files& f) {
|
|
Harness h{f, 4, 64, nullptr,
|
|
[](server::RegionSessionsConfig& c) { c.accept_cellular_intentions = true; }};
|
|
const auto alice = account(0xA6);
|
|
auto client = h.connect(h.issuer.grant(alice, 1), session_protocol_v6);
|
|
CHECK(h.settled(*client) && client->state() == RegionSessionState::established);
|
|
CHECK(!client->set_interest(SessionInterest{})); // No M10 probe view in cellular v6.
|
|
CHECK(client->view() == nullptr);
|
|
h.server->set_default_interest({0, 0, 0, 1});
|
|
h.source = [](const SessionInterest&) {
|
|
return ReplicationSource{.identity = world_id,
|
|
.field_revision = 0,
|
|
.organism_revision = 0,
|
|
.mask = 0,
|
|
.environment = {},
|
|
.organisms = {},
|
|
.markers = {},
|
|
.marker_area = {}};
|
|
};
|
|
h.pump();
|
|
CHECK(client->view() == nullptr); // Default M10 snapshots are withheld too.
|
|
CHECK(!client->send_intention(101, 0, 0));
|
|
CHECK(!client->send_action(1, ActionKind::probe, 101));
|
|
std::optional<std::uint64_t> sequence;
|
|
CHECK(h.until([&] {
|
|
return (sequence =
|
|
client->send_cellular_intention(101, 250, -250, std::pair{3'000u, 2'000u}))
|
|
.has_value();
|
|
}));
|
|
CHECK(sequence == 1);
|
|
std::vector<IntentionReceipt> receipts;
|
|
CHECK(h.until([&] { return !(receipts = client->take_intention_receipts()).empty(); }));
|
|
CHECK(receipts.front() == IntentionReceipt{1, AdmitStatus::admitted, 100});
|
|
const auto pending = h.server->cellular_intentions(101);
|
|
CHECK(pending.size() == 1);
|
|
CHECK(pending.front().account == alice);
|
|
CHECK(pending.front().intention == CellularIntention{must(client->welcome()).session, 5, 1, 101,
|
|
250, -250, true, 3'000, 2'000});
|
|
h.tick = 101;
|
|
CHECK(h.until([&] { return client->simulated() && client->simulated()->sequence == 1; }));
|
|
CHECK(h.server->cellular_intentions(101).empty());
|
|
|
|
Harness legacy{f};
|
|
auto refused = legacy.connect(legacy.issuer.grant(account(0xA7), 1), session_protocol_v6);
|
|
CHECK(legacy.settled(*refused));
|
|
CHECK(refused->state() == RegionSessionState::closed);
|
|
CHECK(refused->close_reason() == SessionCloseReason::unsupported_version);
|
|
}
|
|
|
|
void cellular_v7_action_session(const Files& f) {
|
|
struct ActionLedgerFake final : ActionLedger {
|
|
AccountId owner{};
|
|
std::uint64_t id{};
|
|
std::optional<DurableActionRecord> record;
|
|
std::expected<std::optional<ActionOutcome>, ActionLedgerError>
|
|
find(const AccountId& account_id, std::uint64_t action_id) override {
|
|
if (record && owner == account_id && id == action_id)
|
|
return std::optional<ActionOutcome>{record->outcome};
|
|
return std::optional<ActionOutcome>{};
|
|
}
|
|
std::expected<std::optional<DurableActionRecord>, ActionLedgerError>
|
|
find_record(const AccountId& account_id, std::uint64_t action_id) override {
|
|
return owner == account_id && id == action_id ? record
|
|
: std::optional<DurableActionRecord>{};
|
|
}
|
|
std::expected<std::vector<std::optional<DurableActionRecord>>, ActionLedgerError>
|
|
find_all_records(std::span<const ActionKey> keys) override {
|
|
std::vector<std::optional<DurableActionRecord>> out;
|
|
out.reserve(keys.size());
|
|
for (const auto& key : keys) {
|
|
auto found = find_record(key.account, key.action_id);
|
|
if (!found)
|
|
return std::unexpected(found.error());
|
|
out.push_back(*found);
|
|
}
|
|
return out;
|
|
}
|
|
} actions;
|
|
FakeLedger control;
|
|
Harness h{f, 4, 64, &control, [&](server::RegionSessionsConfig& c) {
|
|
c.actions = &actions;
|
|
c.accept_cellular_actions = true;
|
|
}};
|
|
const auto alice = account(0xA8);
|
|
auto client = h.connect(h.issuer.grant(alice, 1), session_protocol_v7);
|
|
CHECK(h.settled(*client) && client->state() == RegionSessionState::established);
|
|
CHECK(!client->send_action(88, ActionKind::probe, 101));
|
|
CHECK(client->send_cellular_action(88, 101, 0, 0));
|
|
std::vector<ActionReceipt> receipts;
|
|
CHECK(h.until([&] { return !(receipts = client->take_action_receipts()).empty(); }));
|
|
CHECK(receipts.front() == ActionReceipt{88, AdmitStatus::admitted, 100, 101});
|
|
const auto carried = h.server->carry(101);
|
|
CHECK(carried.size() == 1 && carried.front().account == alice &&
|
|
carried.front().action_id == 88 && carried.front().tick == 101 &&
|
|
carried.front().kind == ActionKind::cellular &&
|
|
carried.front().cellular_input ==
|
|
std::optional{encode_cellular_action_input({101, 0, 0, false, 0, 0})});
|
|
actions.owner = alice;
|
|
actions.id = 88;
|
|
actions.record = DurableActionRecord{ActionOutcome{88, ActionResult::committed, 101, 1},
|
|
carried.front().cellular_input};
|
|
h.server->committed(carried, 1);
|
|
std::vector<ActionOutcome> outcomes;
|
|
CHECK(h.until([&] { return !(outcomes = client->take_action_outcomes()).empty(); }));
|
|
CHECK(outcomes.front() == ActionOutcome{88, ActionResult::committed, 101, 1});
|
|
|
|
// Exact retry returns the same final outcome. Reusing the key with changed bytes is refused.
|
|
CHECK(client->send_cellular_action(88, 101, 0, 0));
|
|
CHECK(h.until([&] { return !(receipts = client->take_action_receipts()).empty(); }));
|
|
CHECK(receipts.front().status == AdmitStatus::duplicate);
|
|
CHECK(h.until([&] { return !(outcomes = client->take_action_outcomes()).empty(); }));
|
|
CHECK(outcomes.front() == ActionOutcome{88, ActionResult::committed, 101, 1});
|
|
CHECK(client->send_cellular_action(88, 101, 1, 0));
|
|
CHECK(h.until([&] { return !(receipts = client->take_action_receipts()).empty(); }));
|
|
CHECK(receipts.front().status == AdmitStatus::invalid_action);
|
|
|
|
// A fresh session host resolves the same action from durable result and input bytes.
|
|
client->close();
|
|
CHECK(h.until([&] { return client->state() == RegionSessionState::closed; }));
|
|
Harness restarted{f, 4, 64, &control, [&](server::RegionSessionsConfig& c) {
|
|
c.actions = &actions;
|
|
c.accept_cellular_actions = true;
|
|
}};
|
|
auto reconnected = restarted.connect(restarted.issuer.grant(alice, 2), session_protocol_v7);
|
|
CHECK(restarted.settled(*reconnected) &&
|
|
reconnected->state() == RegionSessionState::established);
|
|
CHECK(reconnected->send_cellular_action(88, 101, 0, 0));
|
|
CHECK(
|
|
restarted.until([&] { return !(receipts = reconnected->take_action_receipts()).empty(); }));
|
|
CHECK(receipts.front().status == AdmitStatus::duplicate);
|
|
CHECK(
|
|
restarted.until([&] { return !(outcomes = reconnected->take_action_outcomes()).empty(); }));
|
|
CHECK(outcomes.front() == ActionOutcome{88, ActionResult::committed, 101, 1});
|
|
CHECK(reconnected->send_cellular_action(88, 101, 1, 0));
|
|
CHECK(
|
|
restarted.until([&] { return !(receipts = reconnected->take_action_receipts()).empty(); }));
|
|
CHECK(receipts.front().status == AdmitStatus::invalid_action);
|
|
|
|
Harness refused{f, 4, 64, &control};
|
|
auto unsupported = refused.connect(refused.issuer.grant(account(0xA9), 1), session_protocol_v7);
|
|
CHECK(refused.settled(*unsupported) && unsupported->state() == RegionSessionState::closed &&
|
|
unsupported->close_reason() == SessionCloseReason::unsupported_version);
|
|
}
|
|
|
|
void cellular_v5_spawn_and_v7_action_share_region_sessions(const Files& f) {
|
|
struct Actions final : ActionLedger {
|
|
std::expected<std::optional<ActionOutcome>, ActionLedgerError>
|
|
find(const AccountId&, std::uint64_t) override {
|
|
return std::optional<ActionOutcome>{};
|
|
}
|
|
std::expected<std::optional<DurableActionRecord>, ActionLedgerError>
|
|
find_record(const AccountId&, std::uint64_t) override {
|
|
return std::optional<DurableActionRecord>{};
|
|
}
|
|
} actions;
|
|
FakeLedger control;
|
|
Harness h{
|
|
f, 6, 64, &control, [&](server::RegionSessionsConfig& c) {
|
|
c.actions = &actions;
|
|
c.accept_cellular_actions = true;
|
|
// Like the cellular host, a refusal reports the reserved identity; only a
|
|
// born outcome may carry it on the wire.
|
|
c.create_primitive_life = [](const AccountId&, std::uint64_t, std::uint64_t request) {
|
|
return request == 45
|
|
? PrimitiveLifeOutcome{request, PrimitiveLifeResult::existing_life, 902,
|
|
0}
|
|
: PrimitiveLifeOutcome{request, PrimitiveLifeResult::born, 901, 5};
|
|
};
|
|
c.query_primitive_life = [](const AccountId&, std::uint64_t, std::uint64_t request) {
|
|
return PrimitiveLifeOutcome{request, PrimitiveLifeResult::pending, 0, 0};
|
|
};
|
|
c.select_cellular_mutation = [](const AccountId& a, std::uint64_t generation,
|
|
const CellularMutationAction& m) {
|
|
return a == account(0xBB) && generation != 0 && m.event_index == 1
|
|
? server::CellularMutationAnswer{AdmitStatus::admitted, 103, {}}
|
|
: server::CellularMutationAnswer{AdmitStatus::invalid_action, 0, {}};
|
|
};
|
|
}};
|
|
auto spawn_client = h.connect(h.issuer.grant(account(0xB5), 1), session_protocol_v5);
|
|
auto action_client = h.connect(h.issuer.grant(account(0xB7), 1), session_protocol_v7);
|
|
const auto owner_account = account(0xB8);
|
|
auto perception_owner = h.connect(h.issuer.grant(owner_account, 1), session_protocol_v9);
|
|
auto other_owner = h.connect(h.issuer.grant(account(0xB9), 1), session_protocol_v9);
|
|
const auto legacy_account = account(0xBA);
|
|
auto legacy_owner = h.connect(h.issuer.grant(legacy_account, 1), session_protocol_v8);
|
|
CHECK(h.settled(*spawn_client) && spawn_client->state() == RegionSessionState::established);
|
|
CHECK(h.settled(*action_client) && action_client->state() == RegionSessionState::established);
|
|
CHECK(h.settled(*perception_owner) &&
|
|
perception_owner->state() == RegionSessionState::established);
|
|
CHECK(h.settled(*other_owner) && other_owner->state() == RegionSessionState::established);
|
|
CHECK(h.settled(*legacy_owner) && legacy_owner->state() == RegionSessionState::established);
|
|
|
|
CHECK(spawn_client->request_primitive_life(44));
|
|
std::vector<PrimitiveLifeOutcome> birth_outcomes;
|
|
CHECK(h.until(
|
|
[&] { return !(birth_outcomes = spawn_client->take_primitive_life_outcomes()).empty(); }));
|
|
CHECK(birth_outcomes.front() == PrimitiveLifeOutcome{44, PrimitiveLifeResult::born, 901, 5});
|
|
// A later client's new request is refused; the session stays open with a valid outcome.
|
|
CHECK(spawn_client->request_primitive_life(45));
|
|
CHECK(h.until(
|
|
[&] { return !(birth_outcomes = spawn_client->take_primitive_life_outcomes()).empty(); }));
|
|
CHECK(birth_outcomes.front() ==
|
|
PrimitiveLifeOutcome{45, PrimitiveLifeResult::existing_life, 0, 0} &&
|
|
spawn_client->state() == RegionSessionState::established);
|
|
|
|
CHECK(action_client->send_cellular_action(55, 101, 0, 0));
|
|
std::vector<ActionReceipt> receipts;
|
|
CHECK(h.until([&] { return !(receipts = action_client->take_action_receipts()).empty(); }));
|
|
CHECK(receipts.front().status == AdmitStatus::admitted);
|
|
const auto carried = h.server->carry(101);
|
|
CHECK(carried.size() == 1 && carried.front().account == account(0xB7) &&
|
|
carried.front().kind == ActionKind::cellular);
|
|
|
|
// ADR-0124: a v10 session carries an engulf action; a v7 session cannot encode one.
|
|
auto engulf_client = h.connect(h.issuer.grant(account(0xBB), 1), session_protocol_v10);
|
|
CHECK(h.settled(*engulf_client) && engulf_client->state() == RegionSessionState::established);
|
|
CHECK(!action_client->send_cellular_action(56, 102, 0, 0, std::nullopt, true));
|
|
CHECK(engulf_client->send_cellular_action(66, 102, 0, 0, std::nullopt, true));
|
|
CHECK(h.until([&] { return !(receipts = engulf_client->take_action_receipts()).empty(); }));
|
|
CHECK(receipts.front().status == AdmitStatus::admitted);
|
|
const auto engulfing = h.server->carry(102);
|
|
CHECK(engulfing.size() == 1 && engulfing.front().account == account(0xBB) &&
|
|
engulfing.front().cellular_input ==
|
|
std::optional{encode_cellular_action_input({102, 0, 0, false, 0, 0, true})});
|
|
|
|
// ADR-0121: a v10 mutation choice reaches the host with the session's account and
|
|
// generation; its receipt names the publishing tick and the outcome follows the commit.
|
|
CHECK(!action_client->send_cellular_mutation(77, 102, 901, 1, 90, 3));
|
|
CHECK(engulf_client->send_cellular_mutation(77, 102, 901, 1, 90, 3));
|
|
CHECK(h.until([&] { return !(receipts = engulf_client->take_action_receipts()).empty(); }));
|
|
CHECK(receipts.front() ==
|
|
ActionReceipt{77, AdmitStatus::admitted, receipts.front().server_tick, 103});
|
|
CHECK(engulf_client->send_cellular_mutation(78, 102, 901, 2, 90, 3));
|
|
CHECK(h.until([&] { return !(receipts = engulf_client->take_action_receipts()).empty(); }));
|
|
CHECK(receipts.front().status == AdmitStatus::invalid_action);
|
|
CHECK(h.server->send_mutation_outcome(account(0xBB), {77, ActionResult::committed, 103, 9}));
|
|
std::vector<ActionOutcome> outcomes;
|
|
CHECK(h.until([&] { return !(outcomes = engulf_client->take_action_outcomes()).empty(); }));
|
|
CHECK(outcomes.front() == ActionOutcome{77, ActionResult::committed, 103, 9});
|
|
CHECK(!h.server->send_mutation_outcome(account(0xB7), {55, ActionResult::committed, 103, 9}));
|
|
// 11.4d: the owner's offer reaches its v10 session only; an empty view clears it.
|
|
const CellularMutationOffer offered{103, 901, 2, 95, {{3, 1, 0, 40, 150'000, 9'000}}};
|
|
CHECK(!h.server->send_mutation_offer(account(0xB7), offered));
|
|
CHECK(h.server->send_mutation_offer(account(0xBB), offered));
|
|
CHECK(h.until([&] { return engulf_client->mutation_offer().has_value(); }));
|
|
CHECK(engulf_client->mutation_offer() == std::optional{offered});
|
|
CHECK(h.server->send_mutation_offer(account(0xBB), {104, 901, 0, 0, {}}));
|
|
CHECK(h.until([&] { return !engulf_client->mutation_offer().has_value(); }));
|
|
// 11.8 (ADR-0129): the end of the owner's life, with its cause, reaches v10 only.
|
|
const CellularLifeEnded ended{105, 901, CellularDeathReason::starved};
|
|
CHECK(!h.server->send_life_ended(account(0xB7), ended));
|
|
CHECK(h.server->send_life_ended(account(0xBB), ended));
|
|
CHECK(h.until([&] { return engulf_client->life_ended().has_value(); }));
|
|
CHECK(engulf_client->life_ended() == std::optional{ended});
|
|
|
|
h.server->set_default_interest({0, 0, 0, 7});
|
|
h.source = [](const SessionInterest&) {
|
|
ReplicationSource source;
|
|
source.identity = world_id;
|
|
source.organism_revision = 101;
|
|
client::OrganismCell cell;
|
|
cell.id = 901;
|
|
cell.phenotype.structural_mass = 1;
|
|
cell.phenotype.total_mass = 1;
|
|
cell.phenotype.extent = 1;
|
|
source.organisms.push_back(cell);
|
|
return source;
|
|
};
|
|
h.tick = 101;
|
|
CHECK(h.until([&] { return action_client->view() != nullptr; }));
|
|
CHECK(action_client->view()->organisms.size() == 1 &&
|
|
action_client->view()->organisms.front().id == 901);
|
|
CHECK(action_client->set_interest({1, 0, 0, 2}));
|
|
|
|
CellularPerception perception{101, 901, {{1'000, 2'000, 3, {4, 5, 6, 7}}}, std::nullopt};
|
|
CellularLifeDiagnostics diagnostics;
|
|
diagnostics.reserve_energy_pj = 88;
|
|
diagnostics.reserve_matter_fg = {9, 10, 11, 12};
|
|
diagnostics.maintenance_cost_pj = 3;
|
|
diagnostics.movement_cost_pj = 2;
|
|
diagnostics.movement = CellularMovementResult::moved;
|
|
diagnostics.intake = CellularIntakeResult::acquired;
|
|
diagnostics.has_tick_effects = true;
|
|
perception.diagnostics = diagnostics;
|
|
CHECK(h.server->send_cellular_perception(owner_account, perception));
|
|
CHECK(h.until([&] {
|
|
return perception_owner->cellular_perception() &&
|
|
perception_owner->cellular_perception()->tick == perception.tick;
|
|
}));
|
|
CHECK(*perception_owner->cellular_perception() == perception);
|
|
CHECK(!other_owner->cellular_perception() && !legacy_owner->cellular_perception() &&
|
|
!action_client->cellular_perception());
|
|
CHECK(h.server->send_cellular_perception(legacy_account, perception));
|
|
CHECK(h.until([&] {
|
|
return legacy_owner->cellular_perception() &&
|
|
legacy_owner->cellular_perception()->tick == perception.tick;
|
|
}));
|
|
auto legacy_perception = perception;
|
|
legacy_perception.diagnostics.reset();
|
|
CHECK(*legacy_owner->cellular_perception() == legacy_perception);
|
|
// An empty view reaches every v8+ session; a session showing a cell keeps it for now
|
|
// (the client's grace, below), one with none shows the empty view.
|
|
h.server->clear_cellular_perceptions(102);
|
|
CHECK(h.until([&] {
|
|
return other_owner->cellular_perception() &&
|
|
other_owner->cellular_perception()->tick == 102;
|
|
}));
|
|
CHECK(other_owner->cellular_perception()->life_identity == 0 &&
|
|
other_owner->cellular_perception()->patches.empty());
|
|
CHECK(perception_owner->cellular_perception()->life_identity == 901);
|
|
// A session given a view this tick is never cleared in the same tick (the cell would
|
|
// vanish for a frame); one without a view still is.
|
|
auto later = perception;
|
|
later.tick = 103;
|
|
CHECK(h.server->send_cellular_perception(owner_account, later));
|
|
h.server->clear_cellular_perceptions(103);
|
|
CHECK(h.until([&] {
|
|
return other_owner->cellular_perception() &&
|
|
other_owner->cellular_perception()->tick == 103;
|
|
}));
|
|
CHECK(other_owner->cellular_perception()->life_identity == 0);
|
|
CHECK(h.until([&] {
|
|
return perception_owner->cellular_perception() &&
|
|
perception_owner->cellular_perception()->tick == 103;
|
|
}));
|
|
CHECK(*perception_owner->cellular_perception() == later);
|
|
// An older server cleared before the real view: the client keeps the cell through a
|
|
// lone empty view, and drops it only after a second (20 ticks) of nothing else.
|
|
auto next = later;
|
|
next.tick = 104;
|
|
h.server->clear_cellular_perceptions(104); // No view yet this tick: an empty one goes.
|
|
CHECK(h.server->send_cellular_perception(owner_account, next));
|
|
CHECK(h.until([&] {
|
|
return perception_owner->cellular_perception() &&
|
|
perception_owner->cellular_perception()->tick == 104;
|
|
}));
|
|
CHECK(*perception_owner->cellular_perception() == next);
|
|
h.server->clear_cellular_perceptions(110);
|
|
h.server->clear_cellular_perceptions(123);
|
|
CHECK(h.until([&] {
|
|
return other_owner->cellular_perception() &&
|
|
other_owner->cellular_perception()->tick == 123;
|
|
}));
|
|
CHECK(perception_owner->cellular_perception()->life_identity == 901); // Within the grace.
|
|
h.server->clear_cellular_perceptions(131);
|
|
CHECK(h.until([&] {
|
|
return perception_owner->cellular_perception() &&
|
|
perception_owner->cellular_perception()->tick == 131;
|
|
}));
|
|
CHECK(perception_owner->cellular_perception()->life_identity == 0);
|
|
}
|
|
|
|
// M10.4: intentions, actions and their distinct answers against a simulated host.
|
|
void intentions_and_actions(const Files& f) {
|
|
Harness h{f};
|
|
const auto alice = account(1);
|
|
auto a = h.connect(h.issuer.grant(alice, 1));
|
|
CHECK(h.settled(*a) && must(a->welcome()).server_tick == 100 && a->server_tick() == 100);
|
|
// Intention: a receipt, then the simulated watermark once its tick runs.
|
|
// Datagrams may be refused until the QUIC datagram extension is negotiated: retry.
|
|
std::optional<std::uint64_t> first;
|
|
CHECK(h.until([&] { return (first = a->send_intention(101, 10, -10)).has_value(); }));
|
|
CHECK(first == 1u);
|
|
std::vector<IntentionReceipt> receipts;
|
|
CHECK(h.until([&] { return !(receipts = a->take_intention_receipts()).empty(); }));
|
|
CHECK(receipts[0] == IntentionReceipt{1, AdmitStatus::admitted, 100});
|
|
CHECK(a->simulated().value_or(SimulatedThrough{}).sequence == 0); // Ticks only, so far.
|
|
CHECK(a->send_intention(150, 0, 0) == 2u);
|
|
CHECK(h.until([&] { return !(receipts = a->take_intention_receipts()).empty(); }));
|
|
CHECK(receipts[0].status == AdmitStatus::outside_window);
|
|
h.tick = 101;
|
|
CHECK(h.until([&] { return a->simulated() && a->simulated()->sequence == 1; }));
|
|
CHECK(must(a->simulated()) == SimulatedThrough{5, 1, 101} && a->server_tick() == 101);
|
|
h.ready = false;
|
|
CHECK(a->send_intention(103, 0, 0) == 3u);
|
|
CHECK(h.until([&] { return !(receipts = a->take_intention_receipts()).empty(); }));
|
|
CHECK(receipts[0].status == AdmitStatus::not_ready);
|
|
h.ready = true;
|
|
// Action: receipt (scheduled), carried with its tick's batch, final after the commit.
|
|
CHECK(a->send_action(7, ActionKind::probe, 103));
|
|
std::vector<ActionReceipt> action_receipts;
|
|
CHECK(h.until([&] { return !(action_receipts = a->take_action_receipts()).empty(); }));
|
|
CHECK(action_receipts[0] == ActionReceipt{7, AdmitStatus::admitted, 101, 103});
|
|
CHECK(a->query_action(7));
|
|
std::vector<ActionOutcome> outcomes;
|
|
CHECK(h.until([&] { return !(outcomes = a->take_action_outcomes()).empty(); }));
|
|
CHECK(outcomes[0].result == ActionResult::pending);
|
|
CHECK(h.server->carry(102).empty());
|
|
const auto carried = h.server->carry(103);
|
|
CHECK(carried.size() == 1 &&
|
|
carried[0] == CarriedAction{alice, 7, 103, ActionKind::probe, std::nullopt});
|
|
h.server->committed(carried, 4);
|
|
CHECK(h.until([&] { return !(outcomes = a->take_action_outcomes()).empty(); }));
|
|
CHECK(outcomes[0] == ActionOutcome{7, ActionResult::committed_no_effect, 103, 4});
|
|
// A retry gets a duplicate receipt and the same final outcome.
|
|
CHECK(a->send_action(7, ActionKind::probe, 104));
|
|
CHECK(h.until([&] { return !(outcomes = a->take_action_outcomes()).empty(); }));
|
|
CHECK(outcomes[0] == ActionOutcome{7, ActionResult::committed_no_effect, 103, 4});
|
|
// The stream is ordered: the duplicate receipt arrived before the repeated outcome.
|
|
const auto duplicates = a->take_action_receipts();
|
|
CHECK(duplicates.size() == 1 &&
|
|
duplicates[0] == ActionReceipt{7, AdmitStatus::duplicate, 101, 103});
|
|
// Reconnect (lost connection, new grant): the account's action resolves the same way.
|
|
auto b = h.connect(h.issuer.grant(alice, 2));
|
|
CHECK(h.settled(*b) && b->state() == RegionSessionState::established);
|
|
CHECK(b->query_action(7));
|
|
CHECK(h.until([&] { return !(outcomes = b->take_action_outcomes()).empty(); }));
|
|
CHECK(outcomes[0] == ActionOutcome{7, ActionResult::committed_no_effect, 103, 4});
|
|
// An action scheduled but never carried when the epoch moves on is final: not committed.
|
|
CHECK(b->send_action(8, ActionKind::probe, 108));
|
|
CHECK(h.until([&] { return !b->take_action_receipts().empty(); }));
|
|
h.epoch = 6;
|
|
CHECK(h.until([&] { return b->state() == RegionSessionState::closed; }));
|
|
auto c = h.connect(h.issuer.grant(alice, 3));
|
|
CHECK(h.settled(*c) && c->state() == RegionSessionState::established);
|
|
CHECK(c->query_action(8));
|
|
CHECK(h.until([&] { return !(outcomes = c->take_action_outcomes()).empty(); }));
|
|
CHECK(outcomes[0].result == ActionResult::not_committed);
|
|
// Intentions of an old epoch are refused.
|
|
CHECK(h.until([&] { return c->send_intention(h.tick + 1, 0, 0).has_value(); }));
|
|
CHECK(h.until([&] { return !(receipts = c->take_intention_receipts()).empty(); }));
|
|
CHECK(receipts[0].status == AdmitStatus::admitted); // c is in epoch 6.
|
|
|
|
// A peer that appends state claims (position, energy) to an intention is closed.
|
|
auto peer = h.raw();
|
|
quic::ConnectionId id = 0;
|
|
std::optional<SessionWelcome> welcome;
|
|
std::optional<Reason> reason;
|
|
CHECK(h.until([&] {
|
|
for (auto& e : peer->poll()) {
|
|
if (e.kind == quic::EventKind::connected) {
|
|
id = e.connection;
|
|
CHECK(peer->send_message(
|
|
id, encode_session(SessionHello{0, h.issuer.grant(account(4), 1)})));
|
|
} else if (e.kind == quic::EventKind::message) {
|
|
const auto m = decode_session(e.bytes);
|
|
if (m && std::holds_alternative<SessionWelcome>(*m)) {
|
|
welcome = std::get<SessionWelcome>(*m);
|
|
auto claim = encode_session(
|
|
SessionIntention{welcome->session, welcome->epoch, 1, h.tick + 1, 0, 0});
|
|
for (int i = 0; i < 16; ++i)
|
|
claim.push_back(0x7F); // "position" and "energy"
|
|
CHECK(peer->send_datagram(id, claim));
|
|
} else if (m && std::holds_alternative<SessionClose>(*m)) {
|
|
reason = std::get<SessionClose>(*m).reason;
|
|
}
|
|
}
|
|
}
|
|
return reason.has_value();
|
|
}));
|
|
CHECK(welcome && reason == Reason::protocol);
|
|
}
|
|
|
|
// M10.5: snapshots over QUIC from a simulated host's 21 x 21 slice.
|
|
client::EnvironmentCell grid_cell(std::int64_t x, std::int64_t y, std::uint64_t quantity) {
|
|
client::EnvironmentCell c;
|
|
c.voxel = {x, y, -7};
|
|
c.chunk_revision = 40;
|
|
c.habitat = {{100}, {200}, {0}, {1000}, {500}, {10}};
|
|
c.volume = {1000ULL * 500 * 10};
|
|
c.substrate = {1};
|
|
c.terrain_material = {2};
|
|
c.terrain_fill = 1000;
|
|
c.presence[0] = client::FieldPresence::present;
|
|
c.quantity[0] = quantity;
|
|
c.presence[1] = client::FieldPresence::missing;
|
|
c.presence[7] = client::FieldPresence::present;
|
|
c.quantity[7] = 5;
|
|
c.moisture_status = world::MoistureStatus::available;
|
|
c.moisture = {3, 50};
|
|
return c;
|
|
}
|
|
|
|
void snapshots_over_quic(const Files& f) {
|
|
Harness h{f};
|
|
std::uint64_t quantity = 30;
|
|
h.source = [&](const SessionInterest&) {
|
|
ReplicationSource s;
|
|
s.identity = world_id;
|
|
s.field_revision = h.tick;
|
|
s.mask = (1U << 0) | (1U << 1) | (1U << 7); // Water requested: moisture is present.
|
|
for (std::int64_t x = -10; x <= 10; ++x)
|
|
for (std::int64_t y = -10; y <= 10; ++y)
|
|
s.environment.push_back(grid_cell(x, y, quantity));
|
|
return s;
|
|
};
|
|
h.server->set_default_interest({0, 0, -7, 30}); // Clamped to radius 7.
|
|
auto a = h.connect(h.issuer.grant(account(1), 1));
|
|
CHECK(h.settled(*a) && a->state() == RegionSessionState::established);
|
|
const auto ticks_until = [&](const std::function<bool()>& done) {
|
|
for (int i = 0; i < 400 && !done(); ++i) {
|
|
++h.tick;
|
|
h.until(done, std::chrono::milliseconds{20});
|
|
}
|
|
return done();
|
|
};
|
|
// The default interest's 225 cells arrive within the per-tick budget.
|
|
CHECK(ticks_until([&] { return a->view() && a->view()->environment.size() == 225; }));
|
|
const auto env = a->environment();
|
|
CHECK(env && env->cells().size() == 225 && env->revision().identity == world_id);
|
|
CHECK(std::ranges::all_of(env->cells(), [](const auto& c) {
|
|
return c.voxel.x >= -7 && c.voxel.x <= 7 && c.voxel.y >= -7 && c.voxel.y <= 7;
|
|
}));
|
|
CHECK(a->snapshot_chunks() > 0 && a->snapshot_bytes() > std::uint64_t{225} * 50);
|
|
// A change reaches the client; acknowledgements keep deltas small.
|
|
quantity = 31;
|
|
CHECK(ticks_until([&] {
|
|
return a->view() && std::ranges::all_of(a->view()->environment,
|
|
[](const auto& c) { return c.quantity[0] == 31; });
|
|
}));
|
|
const auto stats = h.server->replication_stats();
|
|
CHECK(stats.snapshots > 2 && stats.fresh_baselines < stats.snapshots);
|
|
// Another interest: only the new square.
|
|
CHECK(a->set_interest({5, 5, -7, 2}));
|
|
CHECK(ticks_until([&] { return a->view() && a->view()->environment.size() == 25; }));
|
|
CHECK(std::ranges::all_of(a->view()->environment, [](const auto& c) {
|
|
return c.voxel.x >= 3 && c.voxel.x <= 7 && c.voxel.y >= 3 && c.voxel.y <= 7;
|
|
}));
|
|
// A snapshot ack datagram is accepted (not a protocol error); the session stays open.
|
|
CHECK(a->state() == RegionSessionState::established);
|
|
}
|
|
|
|
// M10.8b: a Hello carrying a handoff ticket (ADR-0096) instead of a grant.
|
|
struct FakeHandoff final : HandoffLedger {
|
|
std::expected<HandoffAdmission, HandoffLedgerError> admit(const TicketClaims& ticket,
|
|
std::uint64_t session) override {
|
|
if (error)
|
|
return std::unexpected(*error);
|
|
admitted.emplace_back(ticket, session);
|
|
return HandoffAdmission{
|
|
ControlLease{100 + admitted.size(), {}, std::chrono::milliseconds{20000}}, 2, 0, 0};
|
|
}
|
|
std::optional<HandoffLedgerError> error;
|
|
std::vector<std::pair<TicketClaims, std::uint64_t>> admitted;
|
|
};
|
|
void ticket_login(const Files& f) {
|
|
auto source = *HandoffSigningKey::generate(DeploymentEnvironment::dev, 41);
|
|
const auto ticket = [&](const AccountId& who, std::uint8_t transfer,
|
|
std::uint64_t destination = region) {
|
|
TicketClaims c;
|
|
c.transfer.fill(transfer);
|
|
c.account = who;
|
|
c.world = world_id;
|
|
c.source = 4;
|
|
c.destination = destination;
|
|
c.expires_at = start + 30;
|
|
c.session_expires_at = start + 90;
|
|
const auto t = source.sign(c);
|
|
CHECK(t.has_value());
|
|
return *t;
|
|
};
|
|
const auto alice = account(1);
|
|
// A region without handoff keys refuses every ticket.
|
|
{
|
|
Harness plain{f};
|
|
CHECK(refused_with(plain, ticket(alice, 0x61), Reason::unauthenticated));
|
|
}
|
|
// A handoff ledger needs the control ledger (control renews through it).
|
|
{
|
|
FakeHandoff handoff;
|
|
server::RegionSessionsConfig c;
|
|
c.transport.certificate_pem_file = f.certificate.string();
|
|
c.transport.key_pem_file = f.key.string();
|
|
c.control.environment = DeploymentEnvironment::dev;
|
|
c.control.world = world_id;
|
|
c.control.region = region;
|
|
c.control.epoch = 5;
|
|
c.issuer_keys.push_back({1, *Issuer{}.key.public_key()});
|
|
c.handoff = &handoff;
|
|
CHECK(!RegionSessions::create(std::move(c)));
|
|
}
|
|
FakeLedger ledger;
|
|
FakeHandoff handoff;
|
|
Harness h{f, 4, 64, &ledger, [&](server::RegionSessionsConfig& c) {
|
|
c.handoff_keys.push_back({4, 41, *source.public_key()});
|
|
c.handoff = &handoff;
|
|
}};
|
|
// Admitted: the durable side ran first, for the session Welcome then names; the
|
|
// session ends at the ticket's session expiry.
|
|
auto a = h.connect(ticket(alice, 0x62));
|
|
CHECK(h.settled(*a) && a->state() == RegionSessionState::established);
|
|
const auto& w = must(a->welcome());
|
|
CHECK(handoff.admitted.size() == 1 && handoff.admitted[0].second == w.session &&
|
|
handoff.admitted[0].first.account == alice && w.expires_at == start + 90);
|
|
CHECK(h.server->holder(alice) == w.session && ledger.claims.empty());
|
|
// Control renews through the control ledger under the handoff's generation.
|
|
CHECK(h.until([&] { return !ledger.renewed.empty(); }));
|
|
CHECK(ledger.renewed.front() == 101);
|
|
// Replay, another destination, and a refused durable step are all refused.
|
|
CHECK(refused_with(h, ticket(alice, 0x62), Reason::stale_grant));
|
|
CHECK(refused_with(h, ticket(alice, 0x63, 12), Reason::wrong_scope));
|
|
handoff.error = HandoffLedgerError::refused;
|
|
CHECK(refused_with(h, ticket(account(2), 0x64), Reason::unauthenticated));
|
|
handoff.error = HandoffLedgerError::unavailable;
|
|
CHECK(refused_with(h, ticket(account(3), 0x65), Reason::capacity));
|
|
CHECK(a->state() == RegionSessionState::established && handoff.admitted.size() == 1);
|
|
// M10.9a (ADR-0097): under load, grant logins are refused before signature work while
|
|
// tickets (crossings) are still admitted; the refused grant stays usable.
|
|
handoff.error.reset();
|
|
h.server->set_load(true, 3);
|
|
CHECK(refused_with(h, h.issuer.grant(account(5), 1), Reason::capacity));
|
|
CHECK(refused_with(h, h.issuer.grant(account(5), 1), Reason::capacity));
|
|
CHECK(h.server->refused_for_load() == 2 && a->state() == RegionSessionState::established);
|
|
auto crossed = h.connect(ticket(account(6), 0x66));
|
|
CHECK(h.settled(*crossed) && crossed->state() == RegionSessionState::established);
|
|
h.server->set_load(false, 0);
|
|
auto later = h.connect(h.issuer.grant(account(5), 1));
|
|
CHECK(h.settled(*later) && later->state() == RegionSessionState::established &&
|
|
h.server->refused_for_load() == 2);
|
|
}
|
|
|
|
// M10.8c (ADR-0096): two regions with adjacent areas in one process, over the fake world
|
|
// database of tests/fixtures/fake_handoff_world.hpp.
|
|
using evo::test::RegionDb;
|
|
using evo::test::WorldDb;
|
|
constexpr std::int64_t m_um = 1'000'000; // One metre (one cell) in micrometres.
|
|
// Region `id` owns x in [x0, x0 + 10 m), y in [0, 10 m) of slice 0.
|
|
network::motion::Area area_of(std::int64_t x0) {
|
|
return {x0, 0, x0 + (10 * m_um) - 1, (10 * m_um) - 1, 0};
|
|
}
|
|
struct Region {
|
|
Region(const Files& f, std::uint64_t id, std::int64_t x0, WorldDb& world,
|
|
std::shared_ptr<const HandoffSigningKey> key, const GrantPublicKey& neighbour_key,
|
|
std::uint64_t neighbour, std::int64_t neighbour_x0, std::uint16_t neighbour_port,
|
|
std::uint16_t own_port = 0)
|
|
: db{world, id} {
|
|
server::RegionSessionsConfig c;
|
|
c.transport.port = own_port;
|
|
c.transport.certificate_pem_file = f.certificate.string();
|
|
c.transport.key_pem_file = f.key.string();
|
|
c.control.environment = DeploymentEnvironment::dev;
|
|
c.control.world = world_id;
|
|
c.control.region = id;
|
|
c.control.epoch = 5;
|
|
c.control.max_account_attempts = 16;
|
|
c.issuer_keys.push_back({1, *Issuer{}.key.public_key()});
|
|
c.hello_deadline = std::chrono::milliseconds{2000};
|
|
c.close_flush = std::chrono::milliseconds{300};
|
|
c.ledger = &db;
|
|
c.handoff = &db;
|
|
c.probes = &db;
|
|
c.handoff_key = std::move(key);
|
|
c.handoff_keys.push_back(
|
|
{neighbour, static_cast<std::uint32_t>(neighbour + 40), neighbour_key});
|
|
c.neighbours.push_back(
|
|
{{neighbour, "127.0.0.1:" + std::to_string(neighbour_port), std::string{name}},
|
|
area_of(neighbour_x0)});
|
|
c.ticket_lifetime = 20;
|
|
c.ghost_poll = std::chrono::milliseconds{20};
|
|
c.overdue_poll = std::chrono::milliseconds{20};
|
|
auto made = RegionSessions::create(std::move(c));
|
|
CHECK(made.has_value());
|
|
sessions = std::move(*made);
|
|
CHECK(sessions->set_motion_area(area_of(x0), x0 + (5 * m_um), 5 * m_um));
|
|
sessions->set_default_interest({(x0 / m_um) + 5, 5, 0, 7});
|
|
}
|
|
void pump(std::uint64_t now_seconds) {
|
|
++tick;
|
|
sessions->poll({true, 5, tick}, now_seconds, Clock::now());
|
|
sessions->replicate(tick, [](const SessionInterest&) {
|
|
ReplicationSource s;
|
|
s.identity = world_id;
|
|
return s;
|
|
});
|
|
for (auto& e : sessions->take_events())
|
|
events.push_back(std::move(e));
|
|
}
|
|
RegionDb db;
|
|
std::unique_ptr<RegionSessions> sessions;
|
|
std::uint64_t tick{100};
|
|
std::vector<RegionSessionEvent> events;
|
|
};
|
|
void two_region_handoff(const Files& f) {
|
|
WorldDb world;
|
|
auto key1 = std::make_shared<const HandoffSigningKey>(
|
|
*HandoffSigningKey::generate(DeploymentEnvironment::dev, 41));
|
|
auto key2 = std::make_shared<const HandoffSigningKey>(
|
|
*HandoffSigningKey::generate(DeploymentEnvironment::dev, 42));
|
|
// Ports are only known once listening: region 1 dials region 2 at a port fixed below.
|
|
std::uint16_t port2 = 0;
|
|
{
|
|
// Find a free port for region 2 by binding a throwaway region first.
|
|
server::RegionSessionsConfig probe;
|
|
probe.transport.certificate_pem_file = f.certificate.string();
|
|
probe.transport.key_pem_file = f.key.string();
|
|
probe.control.environment = DeploymentEnvironment::dev;
|
|
probe.control.world = world_id;
|
|
probe.control.region = 2;
|
|
probe.control.epoch = 5;
|
|
probe.issuer_keys.push_back({1, *Issuer{}.key.public_key()});
|
|
auto made = RegionSessions::create(std::move(probe));
|
|
CHECK(made.has_value());
|
|
port2 = (*made)->port();
|
|
}
|
|
Region r1{f, 1, 0, world, key1, *key2->public_key(), 2, 10 * m_um, port2};
|
|
Region r2{f, 2, 10 * m_um, world, key2, *key1->public_key(), 1, 0, r1.sessions->port(), port2};
|
|
CHECK(r2.sessions->port() == port2);
|
|
std::uint64_t now_s = start;
|
|
Issuer issuer;
|
|
std::vector<RegionSessionClient*> clients;
|
|
const auto dial = [&](std::uint16_t port, const WireGrant& credential) {
|
|
client::RegionSessionClientConfig c;
|
|
c.transport.port = port;
|
|
c.transport.server_name = std::string{name};
|
|
c.transport.authority_pem = f.ca_pem;
|
|
c.transport.authority_pem_file = f.ca.string();
|
|
c.grant = credential;
|
|
auto made = RegionSessionClient::connect(std::move(c), Clock::now());
|
|
CHECK(made.has_value());
|
|
clients.push_back(made->get());
|
|
return std::move(*made);
|
|
};
|
|
const auto pump = [&](const std::function<bool()>& done,
|
|
std::chrono::milliseconds limit = std::chrono::milliseconds{15000}) {
|
|
const auto end = Clock::now() + limit;
|
|
while (Clock::now() < end) {
|
|
r1.pump(now_s);
|
|
r2.pump(now_s);
|
|
for (auto* c : clients)
|
|
c->poll(Clock::now());
|
|
if (done())
|
|
return true;
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{2});
|
|
}
|
|
return done();
|
|
};
|
|
// Sends a held vector until the region admits it (ticks here are fast: a refused, late
|
|
// intention is simply sent again).
|
|
const auto walk = [&](RegionSessionClient& c, std::int16_t vx) {
|
|
return pump([&] {
|
|
for (const auto& r : c.take_intention_receipts())
|
|
if (r.status == AdmitStatus::admitted)
|
|
return true;
|
|
static_cast<void>(c.send_intention(c.server_tick() + 4, vx, 0));
|
|
return false;
|
|
});
|
|
};
|
|
const auto own = [](const RegionSessionClient& c) -> std::optional<ReplicatedMarker> {
|
|
if (const auto* v = c.view())
|
|
for (const auto& m : v->markers)
|
|
if (m.own)
|
|
return m;
|
|
return std::nullopt;
|
|
};
|
|
const auto alice = account(21), bob = account(22);
|
|
const auto grant = [&](const AccountId& who, std::uint64_t serial, std::uint64_t in_region) {
|
|
return issuer.grant(who, serial, start, 300, in_region);
|
|
};
|
|
// Bob watches the border from region 2.
|
|
auto b = dial(r2.sessions->port(), grant(bob, 1, 2));
|
|
CHECK(pump([&] { return b->state() == RegionSessionState::established; }));
|
|
// Alice logs in to region 1: her durable probe is created at the spawn.
|
|
auto a = dial(r1.sessions->port(), grant(alice, 1, 1));
|
|
CHECK(pump([&] { return own(*a).has_value(); }));
|
|
CHECK(must(own(*a)).state.x == 5 * m_um && world.probes.at(alice).owner == 1);
|
|
// She walks east (100 mm per tick) to the border: frozen, prepared, offered, closed.
|
|
CHECK(walk(*a, 1000));
|
|
std::optional<HandoffOffer> offer;
|
|
CHECK(pump([&] {
|
|
if (!offer)
|
|
offer = a->take_handoff();
|
|
return offer.has_value() && a->state() == RegionSessionState::closed;
|
|
}));
|
|
CHECK(a->close_reason() == SessionCloseReason::handed_off &&
|
|
must(offer).destination.region == 2 &&
|
|
must(offer).destination.endpoint == "127.0.0.1:" + std::to_string(r2.sessions->port()));
|
|
const auto& t = world.transfers.at(must(offer).transfer);
|
|
CHECK(t.state == WorldDb::State::prepared && t.x >= 10 * m_um && t.x < 11 * m_um &&
|
|
t.y == 5 * m_um && world.probes.at(alice).transfer == must(offer).transfer);
|
|
// Before the switch, Bob sees her as a read-only ghost at the transferred position.
|
|
const auto ghost = [&] {
|
|
if (const auto* v = b->view())
|
|
for (const auto& m : v->markers)
|
|
if (m.ghost && m.state.x == t.x)
|
|
return true;
|
|
return false;
|
|
};
|
|
CHECK(pump(ghost));
|
|
// Alice continues at region 2 with the ticket: no grant, her probe where it crossed.
|
|
auto a2 = dial(r2.sessions->port(), must(offer).ticket);
|
|
CHECK(pump([&] { return own(*a2).has_value(); }));
|
|
CHECK(must(own(*a2)).state.x == t.x && world.probes.at(alice).owner == 2 &&
|
|
world.probes.at(alice).generation == 2 &&
|
|
world.transfers.at(must(offer).transfer).state == WorldDb::State::committed);
|
|
CHECK(pump([&] { return !ghost(); })); // The ghost gave way to her marker.
|
|
// A login at region 1 is now redirected to region 2.
|
|
auto stale = dial(r1.sessions->port(), grant(alice, 2, 1));
|
|
CHECK(pump([&] { return stale->state() == RegionSessionState::closed; }));
|
|
CHECK(stale->close_reason() == SessionCloseReason::moved && stale->redirect() &&
|
|
must(stale->redirect()).region == 2);
|
|
// Crossing back west: region 1 owns her again, generation 3.
|
|
CHECK(walk(*a2, -1000));
|
|
std::optional<HandoffOffer> back;
|
|
CHECK(pump([&] {
|
|
if (!back)
|
|
back = a2->take_handoff();
|
|
return back.has_value() && a2->state() == RegionSessionState::closed;
|
|
}));
|
|
CHECK(must(back).destination.region == 1);
|
|
auto a3 = dial(r1.sessions->port(), must(back).ticket);
|
|
CHECK(pump([&] { return own(*a3).has_value(); }));
|
|
CHECK(world.probes.at(alice).owner == 1 && world.probes.at(alice).generation == 3 &&
|
|
must(own(*a3)).state.x < 10 * m_um);
|
|
// An abandoned handoff: Carol crosses but never arrives; after the ticket's lifetime the
|
|
// source aborts and she logs in at region 1 again, where she stopped.
|
|
const auto carol = account(23);
|
|
auto c = dial(r1.sessions->port(), grant(carol, 1, 1));
|
|
CHECK(pump([&] { return own(*c).has_value(); }));
|
|
CHECK(walk(*c, 1000));
|
|
std::optional<HandoffOffer> unused;
|
|
CHECK(pump([&] {
|
|
if (!unused)
|
|
unused = c->take_handoff();
|
|
return unused.has_value() && c->state() == RegionSessionState::closed;
|
|
}));
|
|
const auto frozen_at = world.transfers.at(must(unused).transfer).x;
|
|
CHECK(pump([&] {
|
|
++now_s; // A second per round: the ticket's lifetime and the abort deadline pass.
|
|
return !world.probes.at(carol).transfer.has_value();
|
|
}));
|
|
CHECK(world.transfers.at(must(unused).transfer).state == WorldDb::State::aborted);
|
|
auto c2 = dial(r1.sessions->port(), grant(carol, 2, 1));
|
|
CHECK(pump([&] { return own(*c2).has_value(); }));
|
|
CHECK(must(own(*c2)).state.x <= 10 * m_um - 1 && world.probes.at(carol).x == frozen_at);
|
|
// The late ticket is refused: expired by now (and its transfer aborted).
|
|
auto late = dial(r2.sessions->port(), must(unused).ticket);
|
|
CHECK(pump([&] { return late->state() == RegionSessionState::closed; }));
|
|
CHECK(late->close_reason() == SessionCloseReason::expired);
|
|
CHECK(b->state() == RegionSessionState::established && r1.db.releases > 0);
|
|
// M10.8d recovery: a transfer prepared by an earlier process (unknown to this one's
|
|
// in-memory deadlines) is aborted once the store reports it overdue; never before.
|
|
const auto dave = account(24);
|
|
TransferId orphan{};
|
|
orphan.fill(0x6D);
|
|
world.probes[dave] = {1, 1, 9 * m_um, 5 * m_um, orphan};
|
|
world.transfers[orphan] = {dave, 1, 2, 1, 9 * m_um, 5 * m_um, WorldDb::State::prepared};
|
|
const auto reads = world.overdue_reads;
|
|
CHECK(pump([&] { return world.overdue_reads > reads + 2; }));
|
|
CHECK(world.transfers.at(orphan).state == WorldDb::State::prepared);
|
|
world.all_overdue = true;
|
|
CHECK(pump([&] { return !world.probes.at(dave).transfer.has_value(); }));
|
|
CHECK(world.transfers.at(orphan).state == WorldDb::State::aborted &&
|
|
world.probes.at(dave).owner == 1);
|
|
world.all_overdue = false;
|
|
}
|
|
|
|
void durable_control(const Files& f) {
|
|
FakeLedger ledger;
|
|
Harness h{f, 4, 64, &ledger};
|
|
const auto alice = account(1), bob = account(2), carol = account(3);
|
|
// Durable refusals: nothing is admitted and the grant stays usable locally.
|
|
ledger.claim_error = ControlLedgerError::stale_grant;
|
|
CHECK(refused_with(h, h.issuer.grant(alice, 1), Reason::stale_grant));
|
|
ledger.claim_error = ControlLedgerError::fenced;
|
|
CHECK(refused_with(h, h.issuer.grant(alice, 1), Reason::shutdown));
|
|
ledger.claim_error = ControlLedgerError::unavailable;
|
|
CHECK(refused_with(h, h.issuer.grant(alice, 1), Reason::capacity));
|
|
CHECK(!h.server->holder(alice) && ledger.claims.empty());
|
|
ledger.claim_error.reset();
|
|
// The claim names the account, serial and the session that Welcome then reports.
|
|
auto a = h.connect(h.issuer.grant(alice, 1));
|
|
CHECK(h.settled(*a) && a->state() == RegionSessionState::established);
|
|
CHECK(ledger.claims.size() == 1 && ledger.claims[0].account == alice &&
|
|
ledger.claims[0].serial == 1 && ledger.claims[0].session == must(a->welcome()).session);
|
|
CHECK(h.until([&] { return ledger.renewed.size() >= 2; })); // Renewed every 100 ms.
|
|
// Refresh records the newer serial for the same session.
|
|
CHECK(a->refresh(h.issuer.grant(alice, 2)));
|
|
std::optional<SessionRefreshResult> r;
|
|
CHECK(h.until([&] { return (r = a->take_refresh_result()).has_value(); }));
|
|
CHECK(must(r).status == 0 && ledger.claims.size() == 2 && ledger.claims[1].serial == 2 &&
|
|
ledger.claims[1].session == must(a->welcome()).session);
|
|
// Another region took control: the renewal is fenced and the session is superseded,
|
|
// without releasing control it no longer holds.
|
|
ledger.renew_error = ControlLedgerError::fenced;
|
|
CHECK(h.until([&] { return a->state() == RegionSessionState::closed; }));
|
|
CHECK(a->close_reason() == Reason::superseded && ledger.released.empty());
|
|
CHECK(!h.server->holder(alice));
|
|
// Control held by another region: Welcome waits it out.
|
|
ledger.renew_error.reset();
|
|
ledger.wait = std::chrono::milliseconds{400};
|
|
const auto sent = Clock::now();
|
|
auto b = h.connect(h.issuer.grant(bob, 1));
|
|
CHECK(h.until([&] { return h.server->holder(bob).has_value(); }));
|
|
CHECK(b->state() != RegionSessionState::established);
|
|
CHECK(h.settled(*b) && b->state() == RegionSessionState::established);
|
|
CHECK(Clock::now() - sent >= std::chrono::milliseconds{400});
|
|
// Undecided renewals: the session closes before its control could lapse, and releases.
|
|
ledger.wait = {};
|
|
ledger.granted = std::chrono::milliseconds{300};
|
|
auto c = h.connect(h.issuer.grant(carol, 1));
|
|
CHECK(h.settled(*c) && c->state() == RegionSessionState::established);
|
|
const auto carol_generation = ledger.next_generation - 1;
|
|
ledger.renew_error = ControlLedgerError::unavailable;
|
|
CHECK(h.until([&] { return c->state() == RegionSessionState::closed; }));
|
|
CHECK(c->close_reason() == Reason::shutdown);
|
|
CHECK(std::ranges::find(ledger.released, carol_generation) != ledger.released.end());
|
|
// A refresh that finds a newer grant elsewhere closes the session as superseded.
|
|
ledger.renew_error.reset();
|
|
ledger.granted = std::chrono::milliseconds{20000};
|
|
ledger.claim_error = ControlLedgerError::stale_grant;
|
|
CHECK(b->refresh(h.issuer.grant(bob, 2)));
|
|
CHECK(h.until([&] { return b->state() == RegionSessionState::closed; }));
|
|
CHECK(b->close_reason() == Reason::superseded && !b->take_refresh_result());
|
|
// Client close releases durable control.
|
|
ledger.claim_error.reset();
|
|
auto d = h.connect(h.issuer.grant(alice, 3));
|
|
CHECK(h.settled(*d) && d->state() == RegionSessionState::established);
|
|
const auto alice_generation = ledger.next_generation - 1;
|
|
d->close();
|
|
CHECK(h.until([&] {
|
|
return std::ranges::find(ledger.released, alice_generation) != ledger.released.end();
|
|
}));
|
|
}
|
|
|
|
// M10.9c (ADR-0097): abuse against one region while an honest session is measured. Each
|
|
// round is one 50 ms tick: the abuse acts, the honest client sends one intention, and the
|
|
// server replicates. The honest session must keep at least 90 % of its snapshots and
|
|
// receipts, and the server's connections and retained state must stay bounded.
|
|
struct Service {
|
|
std::uint64_t ticks{}, views{}, sent{}, receipts{};
|
|
};
|
|
Service serve(Harness& h, RegionSessionClient& honest, std::size_t rounds,
|
|
const std::function<void(std::size_t)>& abuse) {
|
|
Service out;
|
|
std::uint64_t last_view = honest.view() ? honest.view()->tick : 0;
|
|
for (std::size_t round = 0; round < rounds; ++round) {
|
|
++h.tick;
|
|
++out.ticks;
|
|
abuse(round);
|
|
if (honest.send_intention(h.tick + 2, 100, 0))
|
|
++out.sent;
|
|
const auto end = Clock::now() + std::chrono::milliseconds{50};
|
|
while (Clock::now() < end) {
|
|
h.pump();
|
|
for (const auto& r : honest.take_intention_receipts())
|
|
out.receipts += r.status == AdmitStatus::admitted;
|
|
if (const auto* v = honest.view(); v && v->tick != last_view) {
|
|
last_view = v->tick;
|
|
++out.views;
|
|
}
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{1});
|
|
}
|
|
}
|
|
std::printf(
|
|
"abuse service ticks=%llu views=%llu sent=%llu admitted=%llu\n",
|
|
static_cast<unsigned long long>(out.ticks), static_cast<unsigned long long>(out.views),
|
|
static_cast<unsigned long long>(out.sent), static_cast<unsigned long long>(out.receipts));
|
|
return out;
|
|
}
|
|
bool served(const Service& s) {
|
|
return s.views * 10 >= s.ticks * 9 && s.sent * 10 >= s.ticks * 9 &&
|
|
s.receipts * 10 >= s.sent * 9;
|
|
}
|
|
std::unique_ptr<RegionSessionClient> honest_session(Harness& h) {
|
|
h.source = [&h](const SessionInterest&) {
|
|
ReplicationSource s;
|
|
s.identity = world_id;
|
|
s.field_revision = h.tick;
|
|
s.mask = (1U << 0) | (1U << 1) | (1U << 7);
|
|
for (std::int64_t x = -3; x <= 3; ++x)
|
|
for (std::int64_t y = -3; y <= 3; ++y)
|
|
s.environment.push_back(grid_cell(x, y, 30 + (h.tick % 7)));
|
|
return s;
|
|
};
|
|
h.server->set_default_interest({0, 0, -7, 7});
|
|
auto c = h.connect(h.issuer.grant(account(0xA0), 1));
|
|
CHECK(h.settled(*c) && c->state() == RegionSessionState::established);
|
|
// Wait for the datagram channel and the first snapshot.
|
|
CHECK(h.until([&] {
|
|
++h.tick;
|
|
return c->view() != nullptr && c->send_intention(h.tick + 2, 0, 0).has_value();
|
|
}));
|
|
return c;
|
|
}
|
|
|
|
// Handshake and Hello floods: many connections that send garbage, forged or no Hello. They
|
|
// are closed (protocol, unauthenticated, timeout) or refused at the connection limit; the
|
|
// region's connections never exceed the transport bound; the honest session is served.
|
|
void hello_flood(const Files& f) {
|
|
Harness h{f, 4, 64};
|
|
auto honest = honest_session(h);
|
|
// A key the region does not trust (key id 7): its well-formed grants are refused.
|
|
const auto stranger = *GrantSigningKey::import(DeploymentEnvironment::dev, 7, [] {
|
|
std::array<std::uint8_t, grant_private_key_bytes> s{};
|
|
s.back() = 0x2B;
|
|
return s;
|
|
}());
|
|
// Every peer is polled each round and acts once connected: 0 sends garbage, 1 a
|
|
// well-formed Hello whose grant no trusted key signed, 2 stays silent until the deadline.
|
|
struct Peer {
|
|
std::unique_ptr<quic::Endpoint> endpoint;
|
|
std::size_t kind{};
|
|
};
|
|
std::vector<Peer> peers;
|
|
std::size_t most_connections = 0;
|
|
std::array<unsigned, 3> acted{};
|
|
unsigned sent = 0;
|
|
const auto service = serve(h, *honest, 60, [&](std::size_t round) {
|
|
if (round < 40)
|
|
for (std::size_t k = 0; k < 4; ++k)
|
|
peers.push_back({h.raw(), (round * 4 + k) % 3});
|
|
for (auto& peer : peers)
|
|
for (auto& e : peer.endpoint->poll()) {
|
|
if (e.kind != quic::EventKind::connected)
|
|
continue;
|
|
++acted.at(peer.kind);
|
|
if (peer.kind == 0)
|
|
sent += peer.endpoint
|
|
->send_message(e.connection, std::vector<std::uint8_t>{9, 9, 9, 9})
|
|
.has_value();
|
|
if (peer.kind == 1) {
|
|
GrantClaims c;
|
|
c.grant_id = 9000 + round;
|
|
c.account = account(0xF0);
|
|
c.serial = 1;
|
|
c.world = world_id;
|
|
c.region = region;
|
|
c.issued_at = start;
|
|
c.expires_at = start + 60;
|
|
sent += peer.endpoint
|
|
->send_message(e.connection,
|
|
encode_session(SessionHello{0, *stranger.sign(c)}))
|
|
.has_value();
|
|
}
|
|
}
|
|
most_connections = std::max(most_connections, h.server->connections());
|
|
});
|
|
std::printf("abuse hello_flood peers=%zu most_connections=%zu connected garbage=%u "
|
|
"forged=%u silent=%u\n",
|
|
peers.size(), most_connections, acted[0], acted[1], acted[2]);
|
|
CHECK(sent == acted[0] + acted[1]); // Every garbage and forged message went out.
|
|
CHECK(served(service) && honest->state() == RegionSessionState::established);
|
|
CHECK(most_connections <= server::RegionSessionsConfig{}.transport.limits.max_connections);
|
|
peers.clear();
|
|
CHECK(h.until([&] { return h.server->connections() == 1; }));
|
|
std::map<unsigned, unsigned> reasons;
|
|
for (const auto& e : h.events)
|
|
if (e.kind == RegionSessionEvent::Kind::refused)
|
|
++reasons[static_cast<unsigned>(e.reason)];
|
|
for (const auto& [why, n] : reasons)
|
|
std::printf("abuse hello_flood refused reason=%u count=%u\n", why, n);
|
|
CHECK(h.has_event(RegionSessionEvent::Kind::refused, Reason::protocol) &&
|
|
h.has_event(RegionSessionEvent::Kind::refused, Reason::unauthenticated) &&
|
|
h.has_event(RegionSessionEvent::Kind::refused, Reason::timeout));
|
|
}
|
|
|
|
// Datagram flood from an authenticated peer: hundreds of intentions per tick for any tick
|
|
// (throttled per session: one per tick), then garbage, which closes it (protocol). The
|
|
// honest session keeps its receipts and snapshots.
|
|
void datagram_flood(const Files& f) {
|
|
Harness h{f, 4, 64};
|
|
auto honest = honest_session(h);
|
|
auto peer = h.raw();
|
|
quic::ConnectionId id = 0;
|
|
bool welcomed = false, closed = false;
|
|
std::optional<Reason> reason;
|
|
std::uint64_t flood = 0, refused = 0, garbage = 0;
|
|
const auto drain = [&] {
|
|
for (auto& e : peer->poll()) {
|
|
if (e.kind == quic::EventKind::connected) {
|
|
id = e.connection;
|
|
CHECK(peer->send_message(
|
|
id, encode_session(SessionHello{0, h.issuer.grant(account(0xB0), 1)}))
|
|
.has_value());
|
|
} else if (e.kind == quic::EventKind::message || e.kind == quic::EventKind::datagram) {
|
|
const auto m = decode_session(e.bytes);
|
|
if (m && std::holds_alternative<SessionWelcome>(*m))
|
|
welcomed = true;
|
|
if (m && std::holds_alternative<IntentionReceipt>(*m))
|
|
refused += std::get<IntentionReceipt>(*m).status != AdmitStatus::admitted;
|
|
if (m && std::holds_alternative<SessionClose>(*m))
|
|
reason = std::get<SessionClose>(*m).reason;
|
|
} else if (e.kind == quic::EventKind::closed) {
|
|
closed = true;
|
|
}
|
|
}
|
|
};
|
|
CHECK(h.until([&] {
|
|
drain();
|
|
return welcomed;
|
|
}));
|
|
std::uint64_t sequence = 0;
|
|
const auto service = serve(h, *honest, 40, [&](std::size_t round) {
|
|
drain();
|
|
if (round < 30)
|
|
for (int k = 0; k < 400; ++k) {
|
|
SessionIntention in;
|
|
in.sequence = ++sequence;
|
|
in.tick = h.tick + 1 + static_cast<std::uint64_t>(k % 8);
|
|
in.move_x = 100;
|
|
flood += peer->send_datagram(id, encode_session(in)).has_value();
|
|
}
|
|
if (round == 30)
|
|
for (int k = 0; k < 50; ++k)
|
|
garbage += peer->send_datagram(id, std::vector<std::uint8_t>(64, 0xEE)).has_value();
|
|
});
|
|
CHECK(h.until([&] {
|
|
drain();
|
|
return closed || reason.has_value();
|
|
}));
|
|
std::printf("abuse datagram_flood sent=%llu refused_receipts=%llu close=%u\n",
|
|
static_cast<unsigned long long>(flood), static_cast<unsigned long long>(refused),
|
|
static_cast<unsigned>(reason.value_or(Reason::connection_lost)));
|
|
CHECK(served(service) && honest->state() == RegionSessionState::established);
|
|
CHECK(flood > 1000 && garbage > 0 && reason == Reason::protocol);
|
|
}
|
|
|
|
// A slow peer: established, then never polled (it reads nothing and acknowledges no
|
|
// snapshot). The server keeps at most its baselines for it and stays bounded; the honest
|
|
// session is served throughout.
|
|
void slow_peer(const Files& f) {
|
|
Harness h{f, 4, 64};
|
|
auto honest = honest_session(h);
|
|
auto slow = h.connect(h.issuer.grant(account(0xC0), 1));
|
|
CHECK(h.settled(*slow) && slow->state() == RegionSessionState::established);
|
|
CHECK(slow->set_interest({0, 0, -7, 7}));
|
|
h.forget(slow.get()); // From now on nobody polls it.
|
|
std::size_t most_retained = 0;
|
|
const auto service = serve(h, *honest, 80, [&](std::size_t) {
|
|
most_retained = std::max(most_retained, h.server->replication_stats().retained_entities);
|
|
});
|
|
std::printf("abuse slow_peer most_retained_entities=%zu sessions=%zu\n", most_retained,
|
|
h.server->sessions());
|
|
CHECK(served(service) && honest->state() == RegionSessionState::established);
|
|
// Two sessions, 49 cells each, at most 4 baselines (ADR-0089) plus the working state.
|
|
CHECK(most_retained <= std::size_t{2} * 49 * 5);
|
|
slow.reset();
|
|
CHECK(h.until([&] { return h.server->sessions() == 1; }, std::chrono::milliseconds{15000}));
|
|
}
|
|
|
|
// Connect/disconnect storm: 120 logins across 12 accounts, each closed politely or dropped
|
|
// mid-session; the region returns to the honest session alone, which is served throughout.
|
|
void storm(const Files& f) {
|
|
Harness h{f, 8, 1024};
|
|
auto honest = honest_session(h);
|
|
std::vector<std::unique_ptr<RegionSessionClient>> live;
|
|
std::array<std::uint64_t, 12> serials{};
|
|
std::uint64_t opened = 0;
|
|
const auto service = serve(h, *honest, 60, [&](std::size_t round) {
|
|
if (round >= 40)
|
|
return;
|
|
for (int k = 0; k < 3; ++k) {
|
|
const auto who = (round * 3 + static_cast<std::size_t>(k)) % serials.size();
|
|
auto c = h.connect(
|
|
h.issuer.grant(account(static_cast<std::uint8_t>(0xD0 + who)), ++serials.at(who)));
|
|
live.push_back(std::move(c));
|
|
}
|
|
// Older ones leave: politely, or by vanishing (the transport notices).
|
|
while (live.size() > 6) {
|
|
auto& c = live.front();
|
|
opened += c->state() == RegionSessionState::established;
|
|
if (opened % 2 == 0)
|
|
c->close();
|
|
h.forget(c.get());
|
|
live.erase(live.begin());
|
|
}
|
|
});
|
|
for (auto& c : live) {
|
|
h.forget(c.get());
|
|
c->close();
|
|
}
|
|
live.clear();
|
|
std::printf("abuse storm logins=120 established_seen=%llu\n",
|
|
static_cast<unsigned long long>(opened));
|
|
CHECK(served(service) && honest->state() == RegionSessionState::established);
|
|
CHECK(opened > 20);
|
|
CHECK(h.until([&] { return h.server->sessions() == 1 && h.server->connections() == 1; },
|
|
std::chrono::milliseconds{15000}));
|
|
}
|
|
} // namespace
|
|
|
|
int main() try {
|
|
const auto files = credentials();
|
|
login_rules(files);
|
|
durable_life_owner_redirects_before_control_claim(files);
|
|
primitive_v5_session(files);
|
|
cellular_v6_session(files);
|
|
cellular_v7_action_session(files);
|
|
cellular_v5_spawn_and_v7_action_share_region_sessions(files);
|
|
limits(files);
|
|
durable_control(files);
|
|
intentions_and_actions(files);
|
|
snapshots_over_quic(files);
|
|
ticket_login(files);
|
|
two_region_handoff(files);
|
|
hello_flood(files);
|
|
datagram_flood(files);
|
|
slow_peer(files);
|
|
storm(files);
|
|
std::filesystem::remove_all(files.dir);
|
|
std::puts("quic session tests passed");
|
|
return 0;
|
|
} catch (const std::exception& e) {
|
|
std::fprintf(stderr, "%s\n", e.what());
|
|
return 1;
|
|
}
|