diff --git a/apps/evo-server/cellular_bridge.hpp b/apps/evo-server/cellular_bridge.hpp index e94fd11..ed67fa5 100644 --- a/apps/evo-server/cellular_bridge.hpp +++ b/apps/evo-server/cellular_bridge.hpp @@ -29,9 +29,10 @@ namespace evo::server_app { // owner-filtered v8 perception. class CellularBridge { public: - static std::optional - open(const M10Options& options, server::CellularRegionHost& host, - persistence::RegionKey key, std::uint64_t frame, const persistence::PgOptions& pg) { + static std::optional open(const M10Options& options, + server::CellularRegionHost& host, + persistence::RegionKey key, std::uint64_t frame, + const persistence::PgOptions& pg) { if (!options.complete()) return fail("error cellular-session-config"); CellularBridge bridge; @@ -54,16 +55,19 @@ class CellularBridge { config.transport.port = port; config.transport.certificate_pem_file = options.certificate; config.transport.key_pem_file = options.key; - config.control.environment = options.environment == "dev" - ? network::DeploymentEnvironment::dev - : options.environment == "stage" - ? network::DeploymentEnvironment::stage - : network::DeploymentEnvironment::world0; + config.control.environment = + options.environment == "dev" ? network::DeploymentEnvironment::dev + : options.environment == "stage" ? network::DeploymentEnvironment::stage + : network::DeploymentEnvironment::world0; config.control.world = {key.world, frame}; config.control.region = key.region; config.control.epoch = 1; config.control.max_sessions = 512; config.transport.limits.max_connections = 576; + const auto life_routes = parse_life_owner_routes(options); + if (!life_routes || life_routes->empty()) + return fail("error cellular-life-routes"); + config.life_owner_routes = *life_routes; network::GrantPublicKey public_key{}; std::copy(issuer.begin(), issuer.end(), public_key.begin()); config.issuer_keys.push_back({1, public_key}); @@ -73,19 +77,16 @@ class CellularBridge { const auto lease = [&host] { return host.authority(); }; bridge.control_ = std::make_unique(*bridge.slot_, key, lease); - bridge.actions_ = - std::make_unique(*bridge.slot_, key, lease); + bridge.actions_ = std::make_unique(*bridge.slot_, key, lease); config.ledger = bridge.control_.get(); config.actions = bridge.actions_.get(); config.accept_cellular_actions = true; config.create_primitive_life = [&host](const network::AccountId& account, - std::uint64_t generation, - std::uint64_t request) { + std::uint64_t generation, std::uint64_t request) { return host.create_primitive_life(account.bytes, generation, request); }; config.query_primitive_life = [&host](const network::AccountId& account, - std::uint64_t generation, - std::uint64_t request) { + std::uint64_t generation, std::uint64_t request) { return host.query_primitive_life(account.bytes, generation, request); }; auto sessions = server::RegionSessions::create(std::move(config)); @@ -126,7 +127,7 @@ class CellularBridge { std::printf("cellular %s session=%llu account=%s reason=%u\n", event.kind == server::RegionSessionEvent::Kind::opened ? "opened" : event.kind == server::RegionSessionEvent::Kind::closed ? "closed" - : "refused", + : "refused", static_cast(event.session), event.account.c_str(), static_cast(event.reason)); } @@ -155,12 +156,12 @@ class CellularBridge { const auto genome = state.archive().find(life.identity.genome); if (!genome) continue; - const biology::EnvironmentState environment{ - state.ambient_temperature_millikelvin(), life.reserve}; + const biology::EnvironmentState environment{state.ambient_temperature_millikelvin(), + life.reserve}; auto cell = client::present_organism( - life.identity.organism, voxel, - {life.position.x_um % 1'000'000}, {life.position.y_um % 1'000'000}, *genome, - life.growth, environment, client::OrganismState::alive); + life.identity.organism, voxel, {life.position.x_um % 1'000'000}, + {life.position.y_um % 1'000'000}, *genome, life.growth, environment, + client::OrganismState::alive); if (cell) result.organisms.push_back(*cell); } diff --git a/apps/evo-server/m10_bridge.hpp b/apps/evo-server/m10_bridge.hpp index 4aec485..39e82ac 100644 --- a/apps/evo-server/m10_bridge.hpp +++ b/apps/evo-server/m10_bridge.hpp @@ -63,10 +63,14 @@ struct M10Options { // neighbours "REGION=X0,Y0,X1,Y1=HOST:PORT=SERVER_NAME=PUBLIC_KEY_FILE". std::string area, handoff_key; std::vector neighbours; + // M11 owner routing, independent of --m10-neighbour: + // REGION=HOST:PORT=TLS_NAME, one entry per region in the world. + std::vector life_routes; [[nodiscard]] bool any() const noexcept { return !listen.empty() || !certificate.empty() || !key.empty() || !issuer.empty() || !environment.empty() || !session_window.empty() || max_grant_lifetime != 0 || - !area.empty() || !handoff_key.empty() || !neighbours.empty() || !load_governor; + !area.empty() || !handoff_key.empty() || !neighbours.empty() || + !life_routes.empty() || !load_governor; } [[nodiscard]] bool complete() const noexcept { return !listen.empty() && !certificate.empty() && !key.empty() && !issuer.empty() && @@ -80,6 +84,27 @@ inline constexpr std::uint64_t m10_carry_ahead_ticks = 8; inline constexpr std::size_t m10_max_sessions = 2000; #ifdef EVO_SERVER_M10 +inline std::optional> +parse_life_owner_routes(const M10Options& options) { + std::vector routes; + routes.reserve(options.life_routes.size()); + for (const auto& item : options.life_routes) { + const auto first = item.find('='); + const auto second = + first == std::string::npos ? std::string::npos : item.find('=', first + 1); + if (first == std::string::npos || second == std::string::npos || + item.find('=', second + 1) != std::string::npos) + return std::nullopt; + std::uint64_t region{}; + const auto [end, error] = std::from_chars(item.data(), item.data() + first, region); + if (error != std::errc{} || end != item.data() + first || region == 0) + return std::nullopt; + routes.push_back({network::RegionAddress{region, item.substr(first + 1, second - first - 1), + item.substr(second + 1)}}); + } + return routes; +} + inline constexpr bool m10_available = true; template class M10Bridge { public: @@ -185,6 +210,10 @@ template class M10Bridge { config.handoff = bridge.handoff_.get(); std::printf("m10 handoff neighbours=%zu\n", config.neighbours.size()); } + const auto life_routes = parse_life_owner_routes(o); + if (!life_routes) + return fail("error m10-life-routes"); + config.life_owner_routes = *life_routes; auto sessions = server::RegionSessions::create(std::move(config)); if (!sessions) return fail(sessions.error() == server::RegionSessionsError::keys ? "error m10-issuer" diff --git a/apps/evo-server/main.cpp b/apps/evo-server/main.cpp index 9f0c468..e1cedda 100644 --- a/apps/evo-server/main.cpp +++ b/apps/evo-server/main.cpp @@ -171,9 +171,8 @@ std::optional parse(int argc, char** argv) { return std::nullopt; const std::string_view value = argv[++i]; const auto n = number(value); - if (flag == "--profile" && - (value == "lineage" || value == "environmental" || value == "primitive" || - value == "cellular")) + if (flag == "--profile" && (value == "lineage" || value == "environmental" || + value == "primitive" || value == "cellular")) o.profile = value; else if (flag == "--world" && n) o.host.key.world = *n; @@ -267,6 +266,8 @@ std::optional parse(int argc, char** argv) { o.m10.handoff_key = value; else if (flag == "--m10-neighbour" && o.m10.neighbours.size() < 8) o.m10.neighbours.emplace_back(value); + else if (flag == "--life-route" && o.m10.life_routes.size() < 4096) + o.m10.life_routes.emplace_back(value); else if (flag == "--m10-load-governor" && (value == "on" || value == "off")) o.m10.load_governor = value == "on"; else if (flag == "--primitive-listen") @@ -321,10 +322,9 @@ std::optional parse(int argc, char** argv) { return std::nullopt; if (o.profile == "cellular" && (!server_app::m10_available || !o.m10.complete() || o.primitive_session.any() || - o.synthetic || !o.crash_point.empty() || o.session.any() || - !o.m10.area.empty() || !o.m10.handoff_key.empty() || !o.m10.neighbours.empty() || - o.m10.max_grant_lifetime != 0 || !o.m10.load_governor || - o.host.flush_interval != 1000ms || o.drain != 10000ms)) + o.synthetic || !o.crash_point.empty() || o.session.any() || !o.m10.area.empty() || + !o.m10.handoff_key.empty() || !o.m10.neighbours.empty() || o.m10.max_grant_lifetime != 0 || + !o.m10.load_governor || o.host.flush_interval != 1000ms || o.drain != 10000ms)) return std::nullopt; if (std::ranges::find(o.host.retained_builds, o.host.build) == o.host.retained_builds.end()) o.host.retained_builds.push_back(o.host.build); @@ -946,8 +946,7 @@ int serve_cellular(Options& o) { set_ready(false); const auto& status = host.status(); std::printf("phase %s epoch=%llu committed=%llu simulated=%llu\n", - server::phase_name(status.phase), - static_cast(status.epoch), + server::phase_name(status.phase), static_cast(status.epoch), static_cast(status.committed_tick), static_cast(status.simulated_tick)); std::printf("stats commits=%llu store_failures=%llu\n", @@ -964,10 +963,10 @@ int serve_cellular(Options& o) { return finish(0); } const auto& status = host.step(); - const auto wall = static_cast( - std::chrono::duration_cast( - std::chrono::system_clock::now().time_since_epoch()) - .count()); + const auto wall = + static_cast(std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()) + .count()); static_cast(bridge->accepting(wall)); bridge->poll(status, wall); deliver_finalized(); @@ -1024,8 +1023,8 @@ int serve_cellular(Options& o) { baseline.push_back({life.identity.life, {}}); std::ranges::sort(baseline, {}, &server::CellularLifeIntent::life_id); auto actions = bridge->carry(tick); - auto builder = server::CellularSessionBatchBuilder::create( - *state, host.committed()->head); + auto builder = + server::CellularSessionBatchBuilder::create(*state, host.committed()->head); if (!builder) { bridge->dropped(actions, true); } else { @@ -1047,9 +1046,9 @@ int serve_cellular(Options& o) { deliver_finalized(); } } - *next_tick = std::chrono::steady_clock::now() + std::chrono::microseconds{ - static_cast( - biology::cellular_motion_tick_microseconds)}; + *next_tick = std::chrono::steady_clock::now() + + std::chrono::microseconds{static_cast( + biology::cellular_motion_tick_microseconds)}; } } else { next_tick.reset(); // A disconnected life does not accumulate offline ticks. @@ -1066,26 +1065,26 @@ int serve_cellular(Options& o) { int main(int argc, char** argv) { auto options = parse(argc, argv); if (!options) { - std::fputs( - "usage: evo-server --profile lineage|environmental|primitive|" - "cellular --world N --region N " - "--frame N --pg-host H --pg-port P --pg-database D --pg-user U " - "--objects DIR --build HEX64 [--retained HEX64]... [--pg-hostaddr A " - "--pg-passfile F --pg-ca F] [--flush-ms N] [--drain-ms N] " - "[--lease-profile default|fast] [--ready-file PATH] " - "[--primitive-listen ADDRESS:PORT --primitive-cert F --primitive-key F " - "--primitive-issuer F --primitive-environment dev|stage|world0] " - "[--ops-listen ADDRESS:PORT] [--synthetic-inputs development-v1 " - "[--synthetic-until TICK] [--accelerate N] [--session-listen ADDRESS:PORT " - "--session-cert F --session-key F --session-grant-key F " - "--session-grant-ledger F --session-environment dev]] [--m10-listen " - "ADDRESS:PORT --m10-cert F --m10-key F --m10-issuer F " - "--m10-environment dev|stage|world0 --session-window ACTIVATION:UTC_EXPIRY " - "[--m10-max-grant-lifetime 60..14400] [--m10-area X0,Y0,X1,Y1] " - "[--m10-handoff-key F [--m10-neighbour " - "REGION=X0,Y0,X1,Y1=HOST:PORT=NAME=PUBFILE]...] " - "[--m10-load-governor on|off]]\\n", - stderr); + std::fputs("usage: evo-server --profile lineage|environmental|primitive|" + "cellular --world N --region N " + "--frame N --pg-host H --pg-port P --pg-database D --pg-user U " + "--objects DIR --build HEX64 [--retained HEX64]... [--pg-hostaddr A " + "--pg-passfile F --pg-ca F] [--flush-ms N] [--drain-ms N] " + "[--lease-profile default|fast] [--ready-file PATH] " + "[--primitive-listen ADDRESS:PORT --primitive-cert F --primitive-key F " + "--primitive-issuer F --primitive-environment dev|stage|world0] " + "[--ops-listen ADDRESS:PORT] [--synthetic-inputs development-v1 " + "[--synthetic-until TICK] [--accelerate N] [--session-listen ADDRESS:PORT " + "--session-cert F --session-key F --session-grant-key F " + "--session-grant-ledger F --session-environment dev]] [--m10-listen " + "ADDRESS:PORT --m10-cert F --m10-key F --m10-issuer F " + "--m10-environment dev|stage|world0 --session-window ACTIVATION:UTC_EXPIRY " + "[--m10-max-grant-lifetime 60..14400] [--m10-area X0,Y0,X1,Y1] " + "[--m10-handoff-key F [--m10-neighbour " + "REGION=X0,Y0,X1,Y1=HOST:PORT=NAME=PUBFILE]...] " + "[--life-route REGION=HOST:PORT=TLS_NAME ...] " + "[--m10-load-governor on|off]]\\n", + stderr); return 2; } struct sigaction action{}; diff --git a/docs/engineering/M11_2_DELIVERY_PLAN.md b/docs/engineering/M11_2_DELIVERY_PLAN.md index a41468b..9358d6d 100644 --- a/docs/engineering/M11_2_DELIVERY_PLAN.md +++ b/docs/engineering/M11_2_DELIVERY_PLAN.md @@ -137,13 +137,14 @@ Local checks do not close ADR-0097's reference-worker qualification. reservations route to their original region and a refused/cancelled-only history has no active owner. PostgreSQL and migration CI execution remain pending. -- Cross-region owner routing and reconnect-to-existing-life remain unimplemented. - [ADR-0116](../adr/0116-cellular-life-owner-routing.md) resolves the contract: - resolve the durable world/account owner before claiming control, then reuse - the existing SessionRedirect and an operator-provisioned complete region route - catalog. Missing owner data or route fails closed; client endpoints are never - authoritative. Do not claim 11.2 complete until redirect/client switching, - process-kill qualification and restart retry pass. +- 11.2d-7b implementation: grant login resolves the active life owner after + signature/scope checks and before durable control claim. A different owner + uses the v4+ SessionRedirect from an explicit `--life-route` per world region, + then closes as moved. Cellular startup requires a route catalog containing + this region; malformed, duplicate, unavailable, and missing-owner routes fail + closed. QUIC tests cover redirect-before-claim and missing-route refusal. + PostgreSQL and migration CI, client switching, and process-kill qualification + remain pending. Do not claim 11.2 complete until redirect/restart gates pass. No deployed world has been provisioned or changed by this work. diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index af3bf77..3dd83fc 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -6555,3 +6555,22 @@ because the Windows environment lacks POSIX `poll.h`. PostgreSQL/psql, CTest, and CI have not run for this segment. The CI migration, privilege, and PostgreSQL integration results remain required before closing the database gate. + +## 2026-10-11 — M11.2d-7b owner routing (CI and runtime pending) + +The QUIC session path now resolves the durable life owner after grant +verification/scope checks and before calling the durable control claim. A +different owner is looked up only in the operator-provided `--life-route` +catalog; the listener sends the existing `SessionRedirect` for v4 and later, +then `Close(moved)`. An incomplete catalog or database lookup failure does not +claim control. Cellular server startup requires at least its own route entry. + +GCC 13.2 strict-warning syntax compilation passed for +`src/server/session/region_sessions.cpp`, +`src/server/session/postgres_control_ledger.cpp`, and +`tests/quic_sessions.cpp`. A translation unit including the cellular bridge +also compiled, and the Visual Studio LLVM `clang-format` check and +`git diff --check` passed. The full `evo-server` source could not be compiled +locally because this Windows environment lacks POSIX `poll.h`; the QUIC test +binary, PostgreSQL integration suite, and CI have not run. The database owner +lookup and route behavior still require CI/runtime qualification. diff --git a/include/evo/network/session_control.hpp b/include/evo/network/session_control.hpp index b126df6..fa7e10d 100644 --- a/include/evo/network/session_control.hpp +++ b/include/evo/network/session_control.hpp @@ -186,6 +186,12 @@ class ControlLedger { virtual ~ControlLedger() = default; [[nodiscard]] virtual std::expected claim(const AccountId& account, std::uint64_t serial, std::uint64_t session) = 0; + // Optional M11 lookup, called before claim() when the host has an operator-provisioned + // life-owner route catalog. Implementations without durable life reservations fail closed. + [[nodiscard]] virtual std::expected, ControlLedgerError> + primitive_life_owner(const AccountId&) { + return std::unexpected(ControlLedgerError::unavailable); + } [[nodiscard]] virtual std::expected renew(const AccountId& account, std::uint64_t generation) = 0; virtual void release(const AccountId& account, std::uint64_t generation) = 0; diff --git a/include/evo/server/postgres_control_ledger.hpp b/include/evo/server/postgres_control_ledger.hpp index c79bf1d..9ae7d3a 100644 --- a/include/evo/server/postgres_control_ledger.hpp +++ b/include/evo/server/postgres_control_ledger.hpp @@ -1,6 +1,7 @@ #pragma once #include "evo/network/session_control.hpp" #include "evo/persistence/account_control_store.hpp" +#include "evo/persistence/life_reservation_store.hpp" #include "evo/server/pg_slot.hpp" #include #include @@ -22,6 +23,8 @@ class PostgresControlLedger final : public network::ControlLedger { : db_{&db}, key_{key}, lease_{std::move(lease)}, duration_{duration}, timeout_{timeout} {} [[nodiscard]] std::expected claim(const network::AccountId& account, std::uint64_t serial, std::uint64_t session) override; + [[nodiscard]] std::expected, network::ControlLedgerError> + primitive_life_owner(const network::AccountId& account) override; [[nodiscard]] std::expected renew(const network::AccountId& account, std::uint64_t generation) override; void release(const network::AccountId& account, std::uint64_t generation) override; diff --git a/include/evo/server/region_sessions.hpp b/include/evo/server/region_sessions.hpp index 67ea204..16fed34 100644 --- a/include/evo/server/region_sessions.hpp +++ b/include/evo/server/region_sessions.hpp @@ -59,6 +59,11 @@ struct HandoffKey { std::uint32_t key_id{}; network::GrantPublicKey public_key{}; }; +// Operator-provisioned destination for a durable primitive life. This world catalog is +// independent of physical neighbours and is consulted only before a grant claims control. +struct LifeOwnerRoute { + network::RegionAddress address; +}; struct RegionSessionsConfig { network::quic::ServerConfig transport; network::ControlConfig control; // `epoch` is the initial ownership epoch. @@ -70,6 +75,9 @@ struct RegionSessionsConfig { // claim, waits out another region's control before Welcome, renews every // `renew_interval` and closes before its control can lapse (`safety_margin`). network::ControlLedger* ledger{}; + // When present, every entry must be supplied by the operator, including this region. + // A life in another region is redirected before durable control can move here. + std::vector life_owner_routes; std::chrono::milliseconds renew_interval{5000}, safety_margin{2000}; network::IntentLimits intents{}; // max_sessions follows control.max_sessions. // Durable action records (10.4b), owned by the host; null keeps outcomes in memory only. @@ -157,8 +165,8 @@ class RegionSessions { const network::Perceives& perceives = {}); // V8 reliable cellular perception is unicast to the current established session // for the supplied authenticated account. Empty views clear that owner's stale view. - [[nodiscard]] bool send_cellular_perception( - const network::AccountId& account, const network::CellularPerception& perception); + [[nodiscard]] bool send_cellular_perception(const network::AccountId& account, + const network::CellularPerception& perception); void clear_cellular_perceptions(std::uint64_t tick); [[nodiscard]] network::ReplicationStats replication_stats() const; // M10.9a (ADR-0097): the host's load governor output. While `refuse_logins`, a Hello diff --git a/src/server/session/postgres_control_ledger.cpp b/src/server/session/postgres_control_ledger.cpp index fab9601..4430287 100644 --- a/src/server/session/postgres_control_ledger.cpp +++ b/src/server/session/postgres_control_ledger.cpp @@ -45,6 +45,33 @@ PostgresControlLedger::claim(const network::AccountId& account, std::uint64_t se return network::ControlLease{claimed->generation, claimed->wait, claimed->granted}; } +std::expected, network::ControlLedgerError> +PostgresControlLedger::primitive_life_owner(const network::AccountId& account) { + const auto lease = lease_(); + if (!lease) + return std::unexpected(network::ControlLedgerError::fenced); + auto* db = db_->get(); + if (!db) + return std::unexpected(network::ControlLedgerError::unavailable); + const auto owner = + persistence::primitive_life_owner(*db, key_, *lease, uuid(account), timeout_); + if (!owner) { + note(*db_, owner.error().kind == persistence::LifeReservationError::store + ? ControlStoreError::store + : owner.error().kind == persistence::LifeReservationError::unknown + ? ControlStoreError::unknown + : owner.error().kind == persistence::LifeReservationError::fenced + ? ControlStoreError::fenced + : ControlStoreError::invalid); + return std::unexpected(owner.error().kind == persistence::LifeReservationError::fenced + ? network::ControlLedgerError::fenced + : owner.error().kind == persistence::LifeReservationError::invalid + ? network::ControlLedgerError::fenced + : network::ControlLedgerError::unavailable); + } + return *owner; +} + std::expected PostgresControlLedger::renew(const network::AccountId& account, std::uint64_t generation) { const auto lease = lease_(); diff --git a/src/server/session/region_sessions.cpp b/src/server/session/region_sessions.cpp index 4378f56..7e70a7b 100644 --- a/src/server/session/region_sessions.cpp +++ b/src/server/session/region_sessions.cpp @@ -1,5 +1,6 @@ #include "evo/server/region_sessions.hpp" #include +#include #include namespace evo::server { @@ -63,6 +64,10 @@ SessionCloseReason reason_for(SessionGrantError e) { return e == SessionGrantError::wrong_environment ? SessionCloseReason::wrong_scope : SessionCloseReason::unauthenticated; } +bool valid_route_text(const std::string& value) { + return !value.empty() && value.size() <= network::session_address_bytes && + std::ranges::all_of(value, [](char ch) { return ch > 0x20 && ch < 0x7F; }); +} } // namespace struct RegionSessions::Impl { @@ -232,6 +237,24 @@ struct RegionSessions::Impl { c.account = network::log_label(claims.account); if (const auto checked = control.check(*grant, now_seconds); !checked) return close(c, reason_for(checked.error()), now); + if (!config.life_owner_routes.empty()) { + if (!config.ledger) + return close(c, SessionCloseReason::capacity, now); + const auto owner = config.ledger->primitive_life_owner(claims.account); + if (!owner) + return close(c, reason_for(owner.error()), now); + if (*owner && **owner != config.control.region) { + const auto route = std::ranges::find_if( + config.life_owner_routes, [&](const LifeOwnerRoute& candidate) { + return candidate.address.region == **owner; + }); + if (route == config.life_owner_routes.end()) + return close(c, SessionCloseReason::capacity, now); + if (c.protocol_version >= network::session_protocol_version) + send(c, network::SessionRedirect{route->address}); + return close(c, SessionCloseReason::moved, now); + } + } // Durable control first, so the world never has two controlling regions; then the // local admission, which check() has just confirmed. std::optional lease; @@ -395,11 +418,11 @@ struct RegionSessions::Impl { const auto decoded = network::decode_session(bytes, &wire_version); if (c.state != State::established || !decoded || wire_version != c.protocol_version || (c.protocol_version != network::session_protocol_version && - !((config.accept_cellular_intentions && - c.protocol_version == network::session_protocol_v6) || - (config.accept_cellular_actions && - (c.protocol_version == network::session_protocol_v7 || - c.protocol_version == network::session_protocol_v8))))) + !((config.accept_cellular_intentions && + c.protocol_version == network::session_protocol_v6) || + (config.accept_cellular_actions && + (c.protocol_version == network::session_protocol_v7 || + c.protocol_version == network::session_protocol_v8))))) return close(c, SessionCloseReason::protocol, now); if (const auto* ack = std::get_if(&*decoded)) return replication.acknowledge(c.session, *ack); @@ -566,7 +589,8 @@ struct RegionSessions::Impl { (wire_version == network::session_protocol_v5 && !primitive_v5) || (wire_version == network::session_protocol_v6 && !cellular_v6) || ((wire_version == network::session_protocol_v7 || - wire_version == network::session_protocol_v8) && !cellular_actions) || + wire_version == network::session_protocol_v8) && + !cellular_actions) || wire_version > network::session_protocol_v8) return close(c, SessionCloseReason::unsupported_version, now); } else if (wire_version != c.protocol_version) { @@ -642,8 +666,22 @@ RegionSessions::~RegionSessions() = default; std::expected, RegionSessionsError> RegionSessions::create(RegionSessionsConfig config) { + bool has_own_life_route = config.life_owner_routes.empty(); + std::set life_route_regions; + for (const auto& route : config.life_owner_routes) { + const auto& address = route.address; + if (address.region == 0 || !valid_route_text(address.endpoint) || + !valid_route_text(address.server_name) || + !life_route_regions.insert(address.region).second) + return std::unexpected(RegionSessionsError::configuration); + has_own_life_route |= address.region == config.control.region; + } // Connections beyond sessions are headroom for logins and closes still flushing. - if (config.issuer_keys.empty() || (config.handoff != nullptr && config.ledger == nullptr) || + if (config.issuer_keys.empty() || + (!config.life_owner_routes.empty() && + (config.ledger == nullptr || config.life_owner_routes.size() > 4096 || + !has_own_life_route)) || + (config.handoff != nullptr && config.ledger == nullptr) || (config.handoff_key != nullptr && config.probes == nullptr) || config.ticket_lifetime == 0 || config.ticket_lifetime > network::max_ticket_lifetime || ((config.create_primitive_life || config.query_primitive_life) && @@ -838,12 +876,12 @@ void RegionSessions::replicate( m.send_datagram(*c, chunk); // A refused chunk leaves the snapshot unacknowledged. } -bool RegionSessions::send_cellular_perception( - const network::AccountId& account, const network::CellularPerception& perception) { +bool RegionSessions::send_cellular_perception(const network::AccountId& account, + const network::CellularPerception& perception) { auto& m = *impl_; for (const auto& c : m.connections) { - if (c.state != Impl::State::established || c.protocol_version != network::session_protocol_v8 || - c.account_id != account) + if (c.state != Impl::State::established || + c.protocol_version != network::session_protocol_v8 || c.account_id != account) continue; return m.endpoint ->send_message(c.id, network::encode_session(perception, c.protocol_version)) diff --git a/tests/quic_sessions.cpp b/tests/quic_sessions.cpp index d41e2a4..938a763 100644 --- a/tests/quic_sessions.cpp +++ b/tests/quic_sessions.cpp @@ -374,6 +374,12 @@ struct FakeLedger final : ControlLedger { claims.push_back({account, serial, session}); return ControlLease{next_generation++, wait, granted}; } + std::expected, ControlLedgerError> + primitive_life_owner(const AccountId& /*account*/) override { + if (owner_error) + return std::unexpected(*owner_error); + return life_owner; + } std::expected renew(const AccountId& /*account*/, std::uint64_t generation) override { renewed.push_back(generation); @@ -384,13 +390,43 @@ struct FakeLedger final : ControlLedger { void release(const AccountId& /*account*/, std::uint64_t generation) override { released.push_back(generation); } - std::optional claim_error, renew_error; + std::optional claim_error, renew_error, owner_error; + std::optional life_owner; std::chrono::milliseconds wait{}, granted{std::chrono::milliseconds{20000}}; std::uint64_t next_generation{1}; std::vector claims; std::vector 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.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_v8); + 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.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_v8); + 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) { @@ -512,12 +548,13 @@ void cellular_v7_action_session(const Files& f) { 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 == + 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}; + actions.record = DurableActionRecord{ActionOutcome{88, ActionResult::committed, 101, 1}, + carried.front().cellular_input}; h.server->committed(carried, 1); std::vector outcomes; CHECK(h.until([&] { return !(outcomes = client->take_action_outcomes()).empty(); })); @@ -540,25 +577,23 @@ void cellular_v7_action_session(const Files& f) { c.actions = &actions; c.accept_cellular_actions = true; }}; - auto reconnected = restarted.connect(restarted.issuer.grant(alice, 2), - session_protocol_v7); + 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( + 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( + 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( + 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); + 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); } @@ -575,18 +610,17 @@ void cellular_v5_spawn_and_v7_action_share_region_sessions(const Files& f) { } } actions; FakeLedger control; - Harness h{f, 4, 64, &control, [&](server::RegionSessionsConfig& c) { - c.actions = &actions; - c.accept_cellular_actions = true; - c.create_primitive_life = [](const AccountId&, std::uint64_t, - std::uint64_t request) { - return 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}; - }; - }}; + Harness h{ + f, 4, 64, &control, [&](server::RegionSessionsConfig& c) { + c.actions = &actions; + c.accept_cellular_actions = true; + c.create_primitive_life = [](const AccountId&, std::uint64_t, std::uint64_t request) { + return 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}; + }; + }}; 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); @@ -600,7 +634,8 @@ void cellular_v5_spawn_and_v7_action_share_region_sessions(const Files& f) { CHECK(spawn_client->request_primitive_life(44)); std::vector birth_outcomes; - CHECK(h.until([&] { return !(birth_outcomes = spawn_client->take_primitive_life_outcomes()).empty(); })); + CHECK(h.until( + [&] { return !(birth_outcomes = spawn_client->take_primitive_life_outcomes()).empty(); })); CHECK(birth_outcomes.front() == PrimitiveLifeOutcome{44, PrimitiveLifeResult::born, 901, 5}); CHECK(action_client->send_cellular_action(55, 101, 0, 0)); @@ -1469,6 +1504,7 @@ void storm(const Files& f) { 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);