// 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 #include #include #include #include #include #include #include #include #include #include #include #include #include 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(__VA_ARGS__)) template const T& must(const std::optional& 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 scalar() { std::array 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& 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 connect(const WireGrant& grant) { 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; 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 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& 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 server; std::vector clients; std::vector events; std::uint64_t epoch{5}, now_seconds{start}, tick{100}; bool ready{true}; std::function 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 raw_peer(Harness& h, const std::optional>& bytes) { auto peer = h.raw(); std::optional 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(*m)) reason = std::get(*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 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(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(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{1, 2, 3}) == Reason::protocol); auto future = encode_session(SessionHello{0, h.issuer.grant(carol, 2, start + 60)}); future.at(4) = 5; CHECK(raw_peer(h, future) == 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 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 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 claim_error, renew_error; std::chrono::milliseconds wait{}, granted{std::chrono::milliseconds{20000}}; std::uint64_t next_generation{1}; std::vector claims; std::vector renewed, released; }; // 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 first; CHECK(h.until([&] { return (first = a->send_intention(101, 10, -10)).has_value(); })); CHECK(first == 1u); std::vector 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 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 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}); 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 welcome; std::optional 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(*m)) { welcome = std::get(*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(*m)) { reason = std::get(*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& 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 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 error; std::vector> 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.8c (ADR-0096): two regions with adjacent areas in one process, over a fake world // database with the same transfer state machine as schema v12. struct WorldDb { struct Probe { std::uint64_t owner{}, generation{}; std::int64_t x{}, y{}; std::optional transfer; }; enum class State : std::uint8_t { prepared, committed, aborted }; struct Transfer { AccountId account; std::uint64_t source{}, destination{}, generation{}; std::int64_t x{}, y{}; State state{}; }; std::map probes; std::map transfers; }; struct RegionDb final : server::ProbeLedger, HandoffLedger, ControlLedger { RegionDb(WorldDb& w, std::uint64_t r) : world{&w}, region{r} {} std::expected open(const AccountId& account, std::int64_t x, std::int64_t y, std::int64_t) override { auto [it, created] = world->probes.try_emplace(account, WorldDb::Probe{region, 1, x, y, std::nullopt}); const auto& p = it->second; return server::ProbeHome{p.owner, p.generation, p.x, p.y, p.transfer.has_value()}; } void save(const AccountId& account, std::uint64_t generation, std::int64_t x, std::int64_t y, std::int64_t) override { auto& p = world->probes.at(account); if (p.owner == region && p.generation == generation && !p.transfer) { p.x = x; p.y = y; ++saves; } } std::expected prepare(const AccountId& account, std::uint64_t generation, const TransferId& transfer, std::uint64_t destination, std::int64_t x, std::int64_t y, std::int64_t) override { auto& p = world->probes.at(account); if (p.owner != region || p.generation != generation || p.transfer) return std::unexpected(server::ProbeLedgerError::unavailable); p.transfer = transfer; p.x = x; p.y = y; world->transfers[transfer] = { account, region, destination, generation, x, y, WorldDb::State::prepared}; return {}; } std::expected abort(const TransferId& transfer) override { auto& t = world->transfers.at(transfer); if (t.state == WorldDb::State::prepared) { t.state = WorldDb::State::aborted; world->probes.at(t.account).transfer.reset(); } return t.state == WorldDb::State::aborted; } std::expected, server::ProbeLedgerError> incoming() override { std::vector out; for (const auto& [id, t] : world->transfers) if (t.destination == region && t.state == WorldDb::State::prepared) out.push_back({id, t.x, t.y}); return out; } std::expected admit(const TicketClaims& ticket, std::uint64_t) override { const auto found = world->transfers.find(ticket.transfer); if (found == world->transfers.end() || found->second.destination != region || found->second.account != ticket.account || found->second.state == WorldDb::State::aborted) return std::unexpected(HandoffLedgerError::refused); auto& t = found->second; auto& p = world->probes.at(t.account); if (t.state == WorldDb::State::prepared) { // Stage and commit: the switch. t.state = WorldDb::State::committed; p = {region, t.generation + 1, t.x, t.y, std::nullopt}; } return HandoffAdmission{ControlLease{++control, {}, std::chrono::milliseconds{20000}}, p.generation, p.x, p.y}; } std::expected claim(const AccountId&, std::uint64_t, std::uint64_t) override { return ControlLease{++control, {}, std::chrono::milliseconds{20000}}; } std::expected renew(const AccountId&, std::uint64_t) override { return std::chrono::milliseconds{20000}; } void release(const AccountId&, std::uint64_t) override { ++releases; } WorldDb* world; std::uint64_t region; std::uint64_t control{}, saves{}, releases{}; }; 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 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(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}; 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 sessions; std::uint64_t tick{100}; std::vector events; }; void two_region_handoff(const Files& f) { WorldDb world; auto key1 = std::make_shared( *HandoffSigningKey::generate(DeploymentEnvironment::dev, 41)); auto key2 = std::make_shared( *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 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& 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(c.send_intention(c.server_tick() + 4, vx, 0)); return false; }); }; const auto own = [](const RegionSessionClient& c) -> std::optional { 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 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 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 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); } 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 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(); })); } } // namespace int main() try { const auto files = credentials(); login_rules(files); limits(files); durable_control(files); intentions_and_actions(files); snapshots_over_quic(files); ticket_login(files); two_region_handoff(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; }