Implement M9.7 dev region sessions with durable no-effect intentions
Run a non-blocking TLS 1.3 session server inside evo-server's loop: environment views come from the region's own field/terrain stores with an explicitly empty organism presentation; admitted intentions ride the next synthetic input (content unchanged) and return a durable no-effect outcome from the host's final acknowledgement, or not_committed when dropped. Sessions are bound to their connection, epochs close stale sessions, and connections have bounded output, input rate and deadlines. SessionClient and evo-client --connect consume one single-use grant per Hello (refusals before admission keep it), reset to a fresh baseline on every session, keep stale views visible and report in-flight intentions as unknown after loss. evo-dev-session generates dev credentials, grant bundles and runs a restart probe. The dev deployment gains a drill-time, loopback-only listener with an optional Secret, enforced by the policy check, and tools/ops/session-drill.sh. Protocol v1 gains IntentOutcome and the capacity close reason before any deployment. ADR-0083 records the maintainer-confirmed decisions; M8-dev/stage remains not passed and no cluster change was made. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -135,6 +135,11 @@ if(EVO_BUILD_POSTGRES)
|
||||
evo_owned_target(evo_region_host)
|
||||
add_executable(evo-server apps/evo-server/main.cpp)
|
||||
target_link_libraries(evo-server PRIVATE evo::region_host)
|
||||
if(EVO_BUILD_NETWORK)
|
||||
# M9.7 dev sessions inside the region process (ADR-0083).
|
||||
target_link_libraries(evo-server PRIVATE evo_network_session)
|
||||
target_compile_definitions(evo-server PRIVATE EVO_SERVER_SESSIONS=1)
|
||||
endif()
|
||||
evo_owned_target(evo-server)
|
||||
# M7.9 clone and history audit tool (ADR-0067).
|
||||
add_executable(evo-world-clone apps/evo-world-clone/main.cpp)
|
||||
@@ -149,6 +154,12 @@ if(EVO_BUILD_POSTGRES)
|
||||
tests/region_migration_tests.cpp tests/region_store_tests.cpp
|
||||
tests/region_load_tests.cpp tests/region_upgrade_tests.cpp)
|
||||
target_link_libraries(evo-postgres-tests PRIVATE evo::region_host Threads::Threads)
|
||||
if(EVO_BUILD_NETWORK)
|
||||
# M9.7: real evo-server sessions over TLS (ADR-0083).
|
||||
target_sources(evo-postgres-tests PRIVATE tests/region_session_tests.cpp)
|
||||
target_link_libraries(evo-postgres-tests PRIVATE evo_network_session)
|
||||
target_compile_definitions(evo-postgres-tests PRIVATE EVO_POSTGRES_SESSIONS=1)
|
||||
endif()
|
||||
add_dependencies(evo-postgres-tests evo-server evo-world-clone) # Spawned by process tests.
|
||||
evo_owned_target(evo-postgres-tests)
|
||||
# Intentionally not an ordinary CTest: tools/ci/postgres-check.sh owns its private DB.
|
||||
|
||||
+6
-3
@@ -10,7 +10,8 @@
|
||||
{"name": "tsan", "inherits": "debug", "cacheVariables": {"EVO_SANITIZER": "thread"}},
|
||||
{"name": "client", "inherits": "release", "cacheVariables": {"EVO_BUILD_CLIENT": "ON"}},
|
||||
{"name": "client-probe", "inherits": "release", "cacheVariables": {"EVO_BUILD_CLIENT_PROBE": "ON"}},
|
||||
{"name": "network", "inherits": "release", "cacheVariables": {"EVO_BUILD_NETWORK": "ON"}}
|
||||
{"name": "network", "inherits": "release", "cacheVariables": {"EVO_BUILD_NETWORK": "ON"}},
|
||||
{"name": "client-network", "inherits": "release", "cacheVariables": {"EVO_BUILD_CLIENT": "ON", "EVO_BUILD_NETWORK": "ON"}}
|
||||
],
|
||||
"buildPresets": [
|
||||
{"name": "debug", "configurePreset": "debug"},
|
||||
@@ -20,7 +21,8 @@
|
||||
{"name": "tsan", "configurePreset": "tsan"},
|
||||
{"name": "client", "configurePreset": "client", "targets": ["evo-client", "evo-client-lifecycle-tests", "evo-client-environment-tests", "evo-client-organism-tests"]},
|
||||
{"name": "client-probe", "configurePreset": "client-probe", "targets": ["evo-client-probe"]},
|
||||
{"name": "network", "configurePreset": "network", "targets": ["evo-network-tests"]}
|
||||
{"name": "network", "configurePreset": "network", "targets": ["evo-network-tests", "evo-dev-session"]},
|
||||
{"name": "client-network", "configurePreset": "client-network", "targets": ["evo-client", "evo-client-lifecycle-tests", "evo-client-environment-tests", "evo-client-organism-tests", "evo-network-tests", "evo-dev-session"]}
|
||||
],
|
||||
"testPresets": [
|
||||
{"name": "debug", "configurePreset": "debug", "output": {"outputOnFailure": true}},
|
||||
@@ -30,6 +32,7 @@
|
||||
{"name": "tsan", "configurePreset": "tsan", "output": {"outputOnFailure": true}},
|
||||
{"name": "client", "configurePreset": "client", "filter": {"include": {"label": "client"}}, "output": {"outputOnFailure": true}},
|
||||
{"name": "client-probe", "configurePreset": "client-probe", "filter": {"include": {"label": "client"}}, "output": {"outputOnFailure": true}},
|
||||
{"name": "network", "configurePreset": "network", "filter": {"include": {"label": "network"}}, "output": {"outputOnFailure": true}}
|
||||
{"name": "network", "configurePreset": "network", "filter": {"include": {"label": "network"}}, "output": {"outputOnFailure": true}},
|
||||
{"name": "client-network", "configurePreset": "client-network", "filter": {"include": {"label": "client|network"}}, "output": {"outputOnFailure": true}}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -190,4 +190,6 @@ ctest --preset network
|
||||
```
|
||||
|
||||
See [DEV_SESSION_PROTOCOL](docs/networking/DEV_SESSION_PROTOCOL.md) and ADR-0082.
|
||||
M9.7 connects `evo-client` (preset `client-network`) to a dev region running
|
||||
`evo-server --session-listen ...`; see [DEV_SESSION_OPERATIONS](docs/networking/DEV_SESSION_OPERATIONS.md).
|
||||
Windows: `tools/windows-check.ps1 -Preset network -Action check`.
|
||||
|
||||
+53
-14
@@ -2,6 +2,7 @@
|
||||
#include "evo/client/organism_inspector.hpp"
|
||||
#include "evo/render/environment.hpp"
|
||||
#include "evo/render/organism.hpp"
|
||||
#include "remote_session.hpp"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <charconv>
|
||||
@@ -23,10 +24,12 @@ constexpr std::int64_t fixture_z = -16000;
|
||||
// incident energy (never opened: missing). Unrequested fields draw hollow outlines.
|
||||
constexpr std::uint32_t fixture_mask = (1U << 0) | (1U << 1) | (1U << 2) | (1U << 7) | (1U << 8);
|
||||
constexpr unsigned frames_per_authority_tick = 60;
|
||||
// Frame composition: environment bound + organisms + inspector + 3 host UI rectangles.
|
||||
// Frame composition: environment bound + organisms + inspector + 5 host UI rectangles
|
||||
// (capture bar, intention dot, and in remote mode the connection and intent chips).
|
||||
constexpr std::size_t host_ui_rectangles = 5;
|
||||
constexpr std::size_t organism_budget = evo::render::max_rectangles -
|
||||
evo::render::max_environment_rectangles -
|
||||
evo::client::inspector_rectangles - 3;
|
||||
evo::client::inspector_rectangles - host_ui_rectangles;
|
||||
// Local in-memory stand-in for authoritative region views until M9.7. It is never
|
||||
// persisted or published; the client presentation only copies its read-only views.
|
||||
struct FixtureAuthority {
|
||||
@@ -175,12 +178,21 @@ int main(int argc, char** argv) try {
|
||||
unsigned frames = 0, refresh_every = 1;
|
||||
bool inspect = false;
|
||||
evo::render::EnvironmentDisplay display;
|
||||
evo::client_app::RemoteOptions remote_options;
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
const std::string_view arg{argv[i]};
|
||||
if (arg == "--hidden")
|
||||
config.hidden = true;
|
||||
else if (arg == "--inspect")
|
||||
inspect = true;
|
||||
else if (arg == "--connect" && i + 1 < argc)
|
||||
remote_options.connect = argv[++i];
|
||||
else if (arg == "--server-name" && i + 1 < argc)
|
||||
remote_options.server_name = argv[++i];
|
||||
else if (arg == "--ca" && i + 1 < argc)
|
||||
remote_options.authority = argv[++i];
|
||||
else if (arg == "--grants" && i + 1 < argc)
|
||||
remote_options.grants = argv[++i];
|
||||
else if (arg == "--renderer" && i + 1 < argc)
|
||||
config.renderer = argv[++i];
|
||||
else if (arg == "--frames" && i + 1 < argc) {
|
||||
@@ -197,15 +209,36 @@ int main(int argc, char** argv) try {
|
||||
} else {
|
||||
std::fputs("usage: evo-client [--renderer NAME] [--hidden] [--frames 1..100000]\n"
|
||||
" [--layer water|nitrogen|...|heat|moisture]\n"
|
||||
" [--refresh-every 1..1000] [--inspect]\n",
|
||||
" [--refresh-every 1..1000] [--inspect]\n"
|
||||
" [--connect HOST:PORT --server-name NAME --ca FILE "
|
||||
"--grants FILE]\n",
|
||||
stderr);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
auto authority = make_authority();
|
||||
if (!authority) {
|
||||
std::fputs("client environment fixture error\n", stderr);
|
||||
return 1;
|
||||
// Remote mode (M9.7): views and intentions go through a dev session; the local
|
||||
// fixture authority is not created at all.
|
||||
std::optional<evo::client_app::RemoteSession> remote;
|
||||
if (remote_options.any()) {
|
||||
if (!evo::client_app::remote_available || !remote_options.complete()) {
|
||||
std::fputs("remote mode needs --connect, --server-name, --ca and --grants "
|
||||
"(and a build with EVO_BUILD_NETWORK)\n",
|
||||
stderr);
|
||||
return 2;
|
||||
}
|
||||
remote = evo::client_app::RemoteSession::open(remote_options);
|
||||
if (!remote) {
|
||||
std::fputs("client session configuration error\n", stderr);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
std::optional<FixtureAuthority> authority;
|
||||
if (!remote) {
|
||||
authority = make_authority();
|
||||
if (!authority) {
|
||||
std::fputs("client environment fixture error\n", stderr);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
evo::client::EnvironmentLayer environment;
|
||||
evo::client::OrganismLayer population;
|
||||
@@ -239,13 +272,15 @@ int main(int argc, char** argv) try {
|
||||
std::clamp(std::chrono::duration<double>(now - previous).count(), 0.0, 0.1);
|
||||
previous = now;
|
||||
const auto navigation = (*app)->take_navigation();
|
||||
// Fixture ticks only. No server session or simulation clock exists here.
|
||||
// Local input counter; in remote mode the session sets the server tick.
|
||||
const auto intention = (*app)->sample_intention(count);
|
||||
if (!intention)
|
||||
return 1;
|
||||
if (remote)
|
||||
remote->step(now, *intention);
|
||||
// The fixture authority advances at its own cadence. Presentations refresh
|
||||
// every N authority ticks; in between the advanced watermark shows as stale.
|
||||
if (count % frames_per_authority_tick == 0) {
|
||||
if (!remote && count % frames_per_authority_tick == 0) {
|
||||
if (count != 0 && !advance(*authority))
|
||||
return 1;
|
||||
if ((authority->tick - 1) % refresh_every == 0) {
|
||||
@@ -276,14 +311,16 @@ int main(int argc, char** argv) try {
|
||||
if (auto result = camera->zoom(std::exp2(navigation.wheel / 4), anchor);
|
||||
!result && result.error() != evo::client::CameraError::out_of_range)
|
||||
return 1;
|
||||
const auto& shown_environment = remote ? remote->environment() : environment;
|
||||
const auto& shown_organisms = remote ? remote->organisms() : population;
|
||||
// Copied presentation only; clipped to the viewport before float conversion.
|
||||
if (!evo::render::build_environment(boxes, environment.current(),
|
||||
environment.freshness(), *camera, display) ||
|
||||
!evo::render::append_organisms(boxes, organism_budget, population.current(),
|
||||
population.freshness(), *camera))
|
||||
if (!evo::render::build_environment(boxes, shown_environment.current(),
|
||||
shown_environment.freshness(), *camera, display) ||
|
||||
!evo::render::append_organisms(boxes, organism_budget, shown_organisms.current(),
|
||||
shown_organisms.freshness(), *camera))
|
||||
return 1;
|
||||
// Engineering inspector: biology gauges for the hovered organism only.
|
||||
if (const auto* shown = population.current(); inspect && shown != nullptr)
|
||||
if (const auto* shown = shown_organisms.current(); inspect && shown != nullptr)
|
||||
if (const auto index =
|
||||
evo::client::pick_organism(*shown, *camera, navigation.pointer))
|
||||
if (!evo::client::append_inspector(boxes, evo::client::inspector_rectangles,
|
||||
@@ -298,6 +335,8 @@ int main(int argc, char** argv) try {
|
||||
8,
|
||||
8,
|
||||
{240, 240, 240, 255}});
|
||||
if (remote)
|
||||
remote->status(boxes);
|
||||
const auto frame = evo::render::Frame::create(boxes, {16, 20, 28, 255});
|
||||
if (!frame)
|
||||
return 1;
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
#pragma once
|
||||
// M9.7 remote dev session for the engineering client (ADR-0083). Without
|
||||
// EVO_CLIENT_NETWORK (a build without EVO_BUILD_NETWORK) remote options are refused.
|
||||
// The client only presents copies and sends bounded intentions; it never predicts.
|
||||
#include "evo/client/environment_view.hpp"
|
||||
#include "evo/client/input.hpp"
|
||||
#include "evo/client/organism_view.hpp"
|
||||
#include "evo/render/frame.hpp"
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#ifdef EVO_CLIENT_NETWORK
|
||||
#include "evo/network/session_client.hpp"
|
||||
#include <charconv>
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <memory>
|
||||
#endif
|
||||
|
||||
namespace evo::client_app {
|
||||
struct RemoteOptions {
|
||||
std::string connect, server_name, authority, grants;
|
||||
[[nodiscard]] bool any() const noexcept {
|
||||
return !connect.empty() || !server_name.empty() || !authority.empty() || !grants.empty();
|
||||
}
|
||||
[[nodiscard]] bool complete() const noexcept {
|
||||
return !connect.empty() && !server_name.empty() && !authority.empty() && !grants.empty();
|
||||
}
|
||||
};
|
||||
|
||||
#ifdef EVO_CLIENT_NETWORK
|
||||
inline constexpr bool remote_available = true;
|
||||
class RemoteSession {
|
||||
public:
|
||||
static std::optional<RemoteSession> open(const RemoteOptions& o) {
|
||||
const auto colon = o.connect.rfind(':');
|
||||
unsigned port = 0;
|
||||
if (colon == std::string::npos || colon == 0)
|
||||
return std::nullopt;
|
||||
const auto* digits = o.connect.data() + colon + 1;
|
||||
const auto [end, error] =
|
||||
std::from_chars(digits, o.connect.data() + o.connect.size(), port);
|
||||
if (error != std::errc{} || end != o.connect.data() + o.connect.size() || port == 0 ||
|
||||
port > 65535)
|
||||
return std::nullopt;
|
||||
const auto bundle = read(o.grants);
|
||||
if (bundle.empty() || bundle.size() % network::dev_grant_bytes != 0 ||
|
||||
bundle.size() / network::dev_grant_bytes > 64)
|
||||
return std::nullopt;
|
||||
network::SessionClientConfig config;
|
||||
config.host = o.connect.substr(0, colon);
|
||||
config.port = static_cast<std::uint16_t>(port);
|
||||
const auto pem = read(o.authority);
|
||||
config.trust = {{pem.begin(), pem.end()}, o.server_name};
|
||||
for (std::size_t at = 0; at < bundle.size(); at += network::dev_grant_bytes) {
|
||||
network::DevGrant grant{};
|
||||
std::copy_n(bundle.begin() + static_cast<std::ptrdiff_t>(at), grant.size(),
|
||||
grant.begin());
|
||||
config.grants.push_back(grant);
|
||||
}
|
||||
auto made = network::SessionClient::create(std::move(config));
|
||||
if (!made)
|
||||
return std::nullopt;
|
||||
RemoteSession session;
|
||||
session.client_ = std::move(*made);
|
||||
return session;
|
||||
}
|
||||
[[nodiscard]] const client::EnvironmentLayer& environment() const noexcept {
|
||||
return client_->environment();
|
||||
}
|
||||
[[nodiscard]] const client::OrganismLayer& organisms() const noexcept {
|
||||
return client_->organisms();
|
||||
}
|
||||
// Services the connection and sends the intention when its vector changed, at
|
||||
// most once per received server tick (the server also rate-limits).
|
||||
void step(std::chrono::steady_clock::time_point now, const client::Intention& intention) {
|
||||
client_->poll(now);
|
||||
const auto view = client_->latest_view();
|
||||
const bool changed = intention.move_x != sent_x_ || intention.move_y != sent_y_;
|
||||
if (view && changed && (!sent_tick_ || view->server_tick > *sent_tick_) &&
|
||||
client_->send(intention)) {
|
||||
sent_x_ = intention.move_x;
|
||||
sent_y_ = intention.move_y;
|
||||
sent_tick_ = view->server_tick;
|
||||
}
|
||||
for (const auto& e : client_->take_events())
|
||||
report(e);
|
||||
}
|
||||
// Connection chip and last-intention chip, below the capture bar.
|
||||
void status(std::vector<render::Rectangle>& out) const {
|
||||
using network::SessionPhase;
|
||||
const auto phase = client_->phase();
|
||||
const render::Color connection =
|
||||
phase == SessionPhase::established ? render::Color{64, 180, 140, 255}
|
||||
: phase == SessionPhase::exhausted ? render::Color{200, 40, 40, 255}
|
||||
: render::Color{240, 170, 40, 255};
|
||||
out.push_back({8, 72, 28, 6, connection});
|
||||
out.push_back({40, 72, 6, 6, intent_});
|
||||
}
|
||||
|
||||
private:
|
||||
static std::vector<std::uint8_t> read(const std::string& path) {
|
||||
std::ifstream file{path, std::ios::binary};
|
||||
return {std::istreambuf_iterator<char>{file}, std::istreambuf_iterator<char>{}};
|
||||
}
|
||||
void report(const network::ClientEvent& e) {
|
||||
using Kind = network::ClientEvent::Kind;
|
||||
constexpr render::Color pending{240, 240, 240, 255}, durable{64, 180, 140, 255},
|
||||
refused{200, 40, 40, 255}, unknown{130, 130, 130, 255};
|
||||
switch (e.kind) {
|
||||
case Kind::welcome:
|
||||
sent_tick_.reset(); // New session: resend the current intention.
|
||||
sent_x_ = sent_y_ = 0;
|
||||
std::printf("session welcome session=%llu epoch=%llu tick=%llu commit=%llu\n",
|
||||
static_cast<unsigned long long>(e.welcome.session),
|
||||
static_cast<unsigned long long>(e.welcome.epoch),
|
||||
static_cast<unsigned long long>(e.welcome.server_tick),
|
||||
static_cast<unsigned long long>(e.welcome.commit_watermark));
|
||||
break;
|
||||
case Kind::closed:
|
||||
std::printf("session closed reason=%u\n", static_cast<unsigned>(e.reason));
|
||||
break;
|
||||
case Kind::lost:
|
||||
std::puts("session lost (reconnecting with a fresh grant)");
|
||||
break;
|
||||
case Kind::result:
|
||||
intent_ = e.result.status == network::IntentStatus::admitted ? pending : refused;
|
||||
std::printf("intent %llu status=%u\n", static_cast<unsigned long long>(e.sequence),
|
||||
static_cast<unsigned>(e.result.status));
|
||||
break;
|
||||
case Kind::outcome:
|
||||
intent_ = e.outcome.finality == network::IntentFinality::committed_no_effect ? durable
|
||||
: refused;
|
||||
std::printf("intent %llu final=%u tick=%llu commit=%llu\n",
|
||||
static_cast<unsigned long long>(e.sequence),
|
||||
static_cast<unsigned>(e.outcome.finality),
|
||||
static_cast<unsigned long long>(e.outcome.tick),
|
||||
static_cast<unsigned long long>(e.outcome.commit_sequence));
|
||||
break;
|
||||
case Kind::unknown:
|
||||
intent_ = unknown;
|
||||
std::printf("intent %llu outcome unknown (connection lost)\n",
|
||||
static_cast<unsigned long long>(e.sequence));
|
||||
break;
|
||||
case Kind::exhausted:
|
||||
std::puts("session grants exhausted; issue a new bundle to reconnect");
|
||||
break;
|
||||
}
|
||||
std::fflush(stdout);
|
||||
}
|
||||
std::unique_ptr<network::SessionClient> client_;
|
||||
std::optional<std::uint64_t> sent_tick_;
|
||||
std::int16_t sent_x_{}, sent_y_{};
|
||||
render::Color intent_{60, 60, 66, 255};
|
||||
};
|
||||
#else
|
||||
inline constexpr bool remote_available = false;
|
||||
class RemoteSession {
|
||||
public:
|
||||
static std::optional<RemoteSession> open(const RemoteOptions&) {
|
||||
return std::nullopt;
|
||||
}
|
||||
[[nodiscard]] const client::EnvironmentLayer& environment() const noexcept {
|
||||
return environment_;
|
||||
}
|
||||
[[nodiscard]] const client::OrganismLayer& organisms() const noexcept {
|
||||
return organisms_;
|
||||
}
|
||||
void step(std::chrono::steady_clock::time_point, const client::Intention&) {}
|
||||
void status(std::vector<render::Rectangle>&) const {}
|
||||
|
||||
private:
|
||||
client::EnvironmentLayer environment_;
|
||||
client::OrganismLayer organisms_;
|
||||
};
|
||||
#endif
|
||||
} // namespace evo::client_app
|
||||
@@ -0,0 +1,288 @@
|
||||
// M9.7 dev session operator tool (ADR-0083). Dev environment only.
|
||||
//
|
||||
// evo-dev-session credentials --environment dev --server-name NAME --out DIR [--days N]
|
||||
// Writes dev-ca.pem, server.pem, server.key and grant.key into DIR (which must not
|
||||
// already contain them). server.key and grant.key are secrets (owner-only on POSIX):
|
||||
// they belong in the dev namespace Secret, never in images, logs or the repository.
|
||||
// evo-dev-session grants --environment dev --key FILE --world N --frame N --region N
|
||||
// --subject N --out FILE [--count 1..64] [--lifetime 60..3600]
|
||||
// Writes COUNT single-use 108-byte grants for one test subject. Clients consume one
|
||||
// per connection; the HMAC key itself never leaves the operator machine.
|
||||
// evo-dev-session probe --connect HOST:PORT --server-name NAME --ca FILE --grants FILE
|
||||
// [--expect-restart] [--timeout 10..600]
|
||||
// Connects as a dev client, waits for a view, sends one intention and requires its
|
||||
// durable no-effect acknowledgement. With --expect-restart it then waits (printing
|
||||
// "probe awaiting-restart") for the connection to be lost and re-established at a
|
||||
// newer epoch with a fresh baseline, and repeats the acknowledgement check.
|
||||
// Prints "probe passed" on success. Used by tools/ops/session-drill.sh.
|
||||
// Exit: 0 written/passed, 1 failure, 2 usage.
|
||||
#include "evo/network/dev_credentials.hpp"
|
||||
#include "evo/network/session_client.hpp"
|
||||
#include <charconv>
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#ifndef _WIN32
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
using namespace evo::network;
|
||||
std::optional<std::uint64_t> number(std::string_view text) {
|
||||
std::uint64_t value{};
|
||||
const auto [end, error] = std::from_chars(text.data(), text.data() + text.size(), value);
|
||||
if (error != std::errc{} || end != text.data() + text.size())
|
||||
return std::nullopt;
|
||||
return value;
|
||||
}
|
||||
// Exclusive create; secrets get owner-only permissions on POSIX.
|
||||
bool write_new(const std::filesystem::path& path, std::span<const std::uint8_t> bytes,
|
||||
bool secret) {
|
||||
#ifdef _WIN32
|
||||
(void)secret; // Windows ACLs are inherited from the chosen directory.
|
||||
std::FILE* file = nullptr;
|
||||
if (fopen_s(&file, path.string().c_str(), "wbx") != 0 || file == nullptr)
|
||||
return false;
|
||||
#else
|
||||
const int fd =
|
||||
::open(path.c_str(), O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, secret ? 0600 : 0644);
|
||||
if (fd < 0)
|
||||
return false;
|
||||
std::FILE* file = ::fdopen(fd, "wb");
|
||||
if (file == nullptr) {
|
||||
::close(fd);
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
const bool ok = std::fwrite(bytes.data(), 1, bytes.size(), file) == bytes.size();
|
||||
return std::fclose(file) == 0 && ok;
|
||||
}
|
||||
bool write_new(const std::filesystem::path& path, const std::string& text, bool secret) {
|
||||
return write_new(path, {reinterpret_cast<const std::uint8_t*>(text.data()), text.size()},
|
||||
secret);
|
||||
}
|
||||
struct Args {
|
||||
std::vector<std::pair<std::string_view, std::string_view>> values;
|
||||
[[nodiscard]] std::optional<std::string_view> get(std::string_view name) const {
|
||||
for (const auto& [k, v] : values)
|
||||
if (k == name)
|
||||
return v;
|
||||
return std::nullopt;
|
||||
}
|
||||
};
|
||||
std::optional<Args> parse(int argc, char** argv) {
|
||||
Args a;
|
||||
for (int i = 2; i < argc; i += 2) {
|
||||
if (i + 1 >= argc || std::string_view{argv[i]}.substr(0, 2) != "--")
|
||||
return std::nullopt;
|
||||
a.values.emplace_back(argv[i], argv[i + 1]);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
int credentials(const Args& a) {
|
||||
const auto out = a.get("--out");
|
||||
const auto name = a.get("--server-name");
|
||||
const auto days = number(a.get("--days").value_or("90"));
|
||||
if (a.get("--environment") != "dev" || !out || !name || !days || *days > 825)
|
||||
return 2;
|
||||
const auto made = generate_dev_credentials(DeploymentEnvironment::dev, *name,
|
||||
static_cast<std::uint32_t>(*days),
|
||||
std::chrono::system_clock::now());
|
||||
if (!made) {
|
||||
std::fputs("credential generation failed\n", stderr);
|
||||
return 1;
|
||||
}
|
||||
const std::filesystem::path dir{std::string{*out}};
|
||||
std::error_code error;
|
||||
std::filesystem::create_directories(dir, error);
|
||||
if (!write_new(dir / "dev-ca.pem", made->authority_pem, false) ||
|
||||
!write_new(dir / "server.pem", made->server_certificate_pem, false) ||
|
||||
!write_new(dir / "server.key", made->server_key_pem, true) ||
|
||||
!write_new(dir / "grant.key", format_grant_key(made->grant_key) + "\n", true)) {
|
||||
std::fputs("refusing to overwrite or failed to write credential files\n", stderr);
|
||||
return 1;
|
||||
}
|
||||
std::printf("wrote dev-ca.pem server.pem server.key grant.key for %.*s (dev only)\n",
|
||||
static_cast<int>(name->size()), name->data());
|
||||
return 0;
|
||||
}
|
||||
int grants(const Args& a) {
|
||||
const auto key_path = a.get("--key");
|
||||
const auto out = a.get("--out");
|
||||
const auto world = number(a.get("--world").value_or(""));
|
||||
const auto frame = number(a.get("--frame").value_or(""));
|
||||
const auto region = number(a.get("--region").value_or(""));
|
||||
const auto subject = number(a.get("--subject").value_or(""));
|
||||
const auto count = number(a.get("--count").value_or("16"));
|
||||
const auto lifetime = number(a.get("--lifetime").value_or("3600"));
|
||||
if (a.get("--environment") != "dev" || !key_path || !out || !world || !frame || !region ||
|
||||
!subject || !count || *count == 0 || *count > 64 || !lifetime || *lifetime < 60 ||
|
||||
*lifetime > 3600)
|
||||
return 2;
|
||||
std::ifstream file{std::string{*key_path}};
|
||||
const std::string text{std::istreambuf_iterator<char>{file}, std::istreambuf_iterator<char>{}};
|
||||
const auto secret = parse_grant_key(text);
|
||||
if (!secret) {
|
||||
std::fputs("grant key must be 64 lowercase hex digits\n", stderr);
|
||||
return 1;
|
||||
}
|
||||
const auto key = DevGrantKey::import(DeploymentEnvironment::dev, *secret);
|
||||
if (!key)
|
||||
return 1;
|
||||
const auto now =
|
||||
static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::seconds>(
|
||||
std::chrono::system_clock::now().time_since_epoch())
|
||||
.count());
|
||||
std::vector<std::uint8_t> bundle;
|
||||
for (std::uint64_t i = 0; i < *count; ++i) {
|
||||
const auto id = random_grant_id();
|
||||
if (!id)
|
||||
return 1;
|
||||
const auto grant =
|
||||
key->issue({*id, *subject, {*world, *frame}, *region, now, now + *lifetime});
|
||||
if (!grant) {
|
||||
std::fputs("invalid grant claims\n", stderr);
|
||||
return 1;
|
||||
}
|
||||
bundle.insert(bundle.end(), grant->begin(), grant->end());
|
||||
}
|
||||
if (!write_new(std::filesystem::path{std::string{*out}}, bundle, true)) {
|
||||
std::fputs("refusing to overwrite or failed to write the grant bundle\n", stderr);
|
||||
return 1;
|
||||
}
|
||||
std::printf("wrote %llu single-use dev grants for subject %llu, valid %llu s\n",
|
||||
static_cast<unsigned long long>(*count), static_cast<unsigned long long>(*subject),
|
||||
static_cast<unsigned long long>(*lifetime));
|
||||
return 0;
|
||||
}
|
||||
std::vector<std::uint8_t> read_file(std::string_view path) {
|
||||
std::ifstream file{std::string{path}, std::ios::binary};
|
||||
return {std::istreambuf_iterator<char>{file}, std::istreambuf_iterator<char>{}};
|
||||
}
|
||||
int probe(const Args& a, bool expect_restart) {
|
||||
const auto connect = a.get("--connect");
|
||||
const auto name = a.get("--server-name");
|
||||
const auto ca = a.get("--ca");
|
||||
const auto bundle_path = a.get("--grants");
|
||||
const auto timeout = number(a.get("--timeout").value_or("240"));
|
||||
if (!connect || !name || !ca || !bundle_path || !timeout || *timeout < 10 || *timeout > 600)
|
||||
return 2;
|
||||
const auto colon = connect->rfind(':');
|
||||
if (colon == std::string_view::npos || colon == 0)
|
||||
return 2;
|
||||
const std::uint64_t port = number(connect->substr(colon + 1)).value_or(0);
|
||||
const auto bundle = read_file(*bundle_path);
|
||||
if (port == 0 || port > 65535 || bundle.empty() || bundle.size() % dev_grant_bytes != 0)
|
||||
return 2;
|
||||
SessionClientConfig config;
|
||||
config.host = std::string{connect->substr(0, colon)};
|
||||
config.port = static_cast<std::uint16_t>(port);
|
||||
const auto pem = read_file(*ca);
|
||||
config.trust = {{pem.begin(), pem.end()}, std::string{*name}};
|
||||
for (std::size_t at = 0; at < bundle.size(); at += dev_grant_bytes) {
|
||||
DevGrant grant{};
|
||||
std::copy_n(bundle.begin() + static_cast<std::ptrdiff_t>(at), grant.size(), grant.begin());
|
||||
config.grants.push_back(grant);
|
||||
}
|
||||
config.max_retry = std::chrono::milliseconds{2000};
|
||||
auto made = SessionClient::create(std::move(config));
|
||||
if (!made)
|
||||
return 2;
|
||||
auto& c = **made;
|
||||
const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds{*timeout};
|
||||
std::uint64_t sequence = 0, epoch = 0;
|
||||
const auto failed = [](const char* why) {
|
||||
std::printf("probe failed: %s\n", why);
|
||||
return 1;
|
||||
};
|
||||
// One acknowledged round: a session newer than `after_epoch`, a view, one durable intent.
|
||||
const auto round = [&](std::uint64_t after_epoch) -> const char* {
|
||||
bool sent = false;
|
||||
while (std::chrono::steady_clock::now() < deadline) {
|
||||
c.poll(std::chrono::steady_clock::now());
|
||||
for (const auto& e : c.take_events()) {
|
||||
using Kind = ClientEvent::Kind;
|
||||
if (e.kind == Kind::welcome)
|
||||
std::printf("probe welcome session=%llu epoch=%llu tick=%llu commit=%llu\n",
|
||||
static_cast<unsigned long long>(e.welcome.session),
|
||||
static_cast<unsigned long long>(e.welcome.epoch),
|
||||
static_cast<unsigned long long>(e.welcome.server_tick),
|
||||
static_cast<unsigned long long>(e.welcome.commit_watermark));
|
||||
else if (e.kind == Kind::closed)
|
||||
std::printf("probe closed reason=%u\n", static_cast<unsigned>(e.reason));
|
||||
else if (e.kind == Kind::lost)
|
||||
std::puts("probe lost");
|
||||
else if (e.kind == Kind::exhausted)
|
||||
return "grants exhausted";
|
||||
else if (e.kind == Kind::result && e.result.status != IntentStatus::admitted)
|
||||
return "intention rejected";
|
||||
else if (e.kind == Kind::outcome) {
|
||||
if (e.outcome.finality != IntentFinality::committed_no_effect)
|
||||
return "intention not committed";
|
||||
std::printf("probe durable sequence=%llu tick=%llu commit=%llu\n",
|
||||
static_cast<unsigned long long>(e.sequence),
|
||||
static_cast<unsigned long long>(e.outcome.tick),
|
||||
static_cast<unsigned long long>(e.outcome.commit_sequence));
|
||||
epoch = c.welcome()->epoch;
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
std::fflush(stdout);
|
||||
const auto w = c.welcome();
|
||||
if (!sent && w && w->epoch > after_epoch && c.latest_view() &&
|
||||
c.environment().freshness() == evo::client::Freshness::current &&
|
||||
c.send({++sequence, 0, 1000, 0}))
|
||||
sent = true;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{5});
|
||||
}
|
||||
return "timeout";
|
||||
};
|
||||
if (const auto* why = round(0))
|
||||
return failed(why);
|
||||
if (expect_restart) {
|
||||
std::puts("probe awaiting-restart");
|
||||
std::fflush(stdout);
|
||||
if (const auto* why = round(epoch))
|
||||
return failed(why);
|
||||
}
|
||||
std::puts("probe passed");
|
||||
return 0;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
const auto args = parse(argc, argv);
|
||||
const std::string_view command = argc > 1 ? argv[1] : "";
|
||||
int code = 2;
|
||||
if (args && command == "credentials")
|
||||
code = credentials(*args);
|
||||
else if (args && command == "grants")
|
||||
code = grants(*args);
|
||||
else if (command == "probe") {
|
||||
// --expect-restart takes no value; strip it before pairing the rest.
|
||||
std::vector<char*> rest{argv, argv + argc};
|
||||
const bool restart = std::erase_if(rest, [](const char* x) {
|
||||
return std::string_view{x} == "--expect-restart";
|
||||
}) > 0;
|
||||
const auto probe_args = parse(static_cast<int>(rest.size()), rest.data());
|
||||
code = probe_args ? probe(*probe_args, restart) : 2;
|
||||
}
|
||||
if (code == 2)
|
||||
std::fputs("usage: evo-dev-session credentials --environment dev --server-name NAME "
|
||||
"--out DIR [--days N]\n"
|
||||
" evo-dev-session grants --environment dev --key FILE --world N "
|
||||
"--frame N --region N --subject N --out FILE [--count N] [--lifetime S]\n"
|
||||
" evo-dev-session probe --connect HOST:PORT --server-name NAME --ca FILE "
|
||||
"--grants FILE [--expect-restart] [--timeout S]\n",
|
||||
stderr);
|
||||
return code;
|
||||
}
|
||||
@@ -34,12 +34,22 @@
|
||||
// --accelerate <2..1000> runs the region's scheduling clock that many times faster
|
||||
// than real time (lease deadlines stay real). Never in a deployed environment.
|
||||
//
|
||||
// Dev sessions (M9.7, ADR-0083; builds with EVO_BUILD_NETWORK only): --session-listen
|
||||
// <numeric address:port> --session-cert F --session-key F --session-grant-key F
|
||||
// --session-environment dev serve the TLS 1.3 dev session protocol. They require
|
||||
// --synthetic-inputs: an admitted intention rides the next synthetic input (content
|
||||
// unchanged), so its final outcome is the input's durable acknowledgement, with no
|
||||
// biological effect. The driver then looks ahead 8 ticks instead of 64. Prints
|
||||
// "session listening <port>" and "session opened|closed|refused ...". Any environment
|
||||
// other than dev is refused: the dev identity cannot exist elsewhere.
|
||||
//
|
||||
// --crash-at <point>:<n> (tests only, M7.7) SIGKILLs this process at the n-th
|
||||
// occurrence of before-upload, before-transaction, after-commit or after-ack (right
|
||||
// after a step's acknowledgements are written and flushed).
|
||||
#include "evo/processes/development_opening.hpp"
|
||||
#include "evo/server/ops_http.hpp"
|
||||
#include "evo/server/region_host.hpp"
|
||||
#include "session_bridge.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
@@ -78,6 +88,7 @@ struct Options {
|
||||
std::uint64_t synthetic_until{}; // 0: unbounded.
|
||||
std::uint64_t accelerate{1};
|
||||
std::uint64_t objects_reserve_mib{1024};
|
||||
server_app::SessionOptions session;
|
||||
};
|
||||
|
||||
// Kills this process without any cleanup: the real SIGKILL, at a chosen boundary.
|
||||
@@ -188,6 +199,17 @@ std::optional<Options> parse(int argc, char** argv) {
|
||||
o.accelerate = *n;
|
||||
else if (flag == "--objects-reserve-mib" && n && *n <= 1048576)
|
||||
o.objects_reserve_mib = *n;
|
||||
else if (flag == "--session-listen")
|
||||
o.session.listen = value;
|
||||
else if (flag == "--session-cert")
|
||||
o.session.certificate = value;
|
||||
else if (flag == "--session-key")
|
||||
o.session.key = value;
|
||||
else if (flag == "--session-grant-key")
|
||||
o.session.grant_key = value;
|
||||
else if (flag == "--session-environment" &&
|
||||
(value == "dev" || value == "stage" || value == "world0"))
|
||||
o.session.environment = value;
|
||||
else if (flag == "--crash-at") {
|
||||
const auto colon = value.find(':');
|
||||
const auto ordinal =
|
||||
@@ -211,6 +233,10 @@ std::optional<Options> parse(int argc, char** argv) {
|
||||
// Rehearsal controls exist only for the synthetic driver (clones, drills).
|
||||
if ((o.synthetic_until != 0 || o.accelerate != 1) && !o.synthetic)
|
||||
return std::nullopt;
|
||||
// Sessions need every setting, the network build, and the synthetic carrier.
|
||||
if (o.session.any() &&
|
||||
(!server_app::sessions_available || !o.session.complete() || !o.synthetic))
|
||||
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);
|
||||
return o;
|
||||
@@ -332,6 +358,11 @@ template <typename State> int serve(Options& o) {
|
||||
std::printf("ops listening %u\n", static_cast<unsigned>(ops->port()));
|
||||
std::fflush(stdout);
|
||||
}
|
||||
auto sessions = server_app::SessionBridge::open(o.session, o.host.key, o.host.frame);
|
||||
if (!sessions)
|
||||
return 2;
|
||||
std::fflush(stdout);
|
||||
const std::uint64_t lookahead = sessions->enabled() ? server_app::session_lookahead_ticks : 64;
|
||||
server::OpsCounters counters;
|
||||
counters.queue_capacity_inputs = o.host.max_queued_inputs;
|
||||
counters.queue_capacity_bytes = o.host.max_queued_bytes;
|
||||
@@ -432,12 +463,17 @@ template <typename State> int serve(Options& o) {
|
||||
synthetic_next = now_status.simulated_tick + 1; // Resume after the head.
|
||||
synthetic_running = running;
|
||||
while (running && synthetic_next &&
|
||||
*synthetic_next <= host.status().simulated_tick + 64 &&
|
||||
*synthetic_next <= host.status().simulated_tick + lookahead &&
|
||||
(o.synthetic_until == 0 || *synthetic_next <= o.synthetic_until)) {
|
||||
const server::TickInput in{synthetic_request(*synthetic_next),
|
||||
// An admitted session intention may ride this input; content is unchanged.
|
||||
const auto carried = sessions->carry(*synthetic_next);
|
||||
const server::TickInput in{carried.value_or(synthetic_request(*synthetic_next)),
|
||||
processes::development_input(*synthetic_next)};
|
||||
if (!host.submit(in))
|
||||
if (!host.submit(in)) {
|
||||
if (carried)
|
||||
sessions->not_committed(*carried);
|
||||
break; // Full or no longer ready: continue next iteration.
|
||||
}
|
||||
++*synthetic_next;
|
||||
}
|
||||
}
|
||||
@@ -462,10 +498,12 @@ template <typename State> int serve(Options& o) {
|
||||
set_ready(s.ready);
|
||||
const auto acknowledged = host.take_acks();
|
||||
counters.acks += acknowledged.size();
|
||||
for (const auto& a : acknowledged)
|
||||
for (const auto& a : acknowledged) {
|
||||
std::printf("ack %s %llu %llu\n", text(a.request).c_str(),
|
||||
static_cast<unsigned long long>(a.tick),
|
||||
static_cast<unsigned long long>(a.commit_sequence));
|
||||
sessions->committed(a.request, a.tick, a.commit_sequence);
|
||||
}
|
||||
const auto outcomes = host.take_outcomes();
|
||||
counters.outcomes += outcomes.size();
|
||||
for (const auto& x : outcomes)
|
||||
@@ -477,12 +515,16 @@ template <typename State> int serve(Options& o) {
|
||||
const auto dropped = host.take_dropped();
|
||||
for (const auto& d : dropped)
|
||||
++counters.dropped[static_cast<std::size_t>(d.reason)];
|
||||
for (const auto& d : dropped)
|
||||
for (const auto& d : dropped) {
|
||||
std::printf("dropped %s %s\n", text(d.input.request).c_str(),
|
||||
server::unacknowledged_name(d.reason));
|
||||
sessions->not_committed(d.input.request);
|
||||
}
|
||||
std::fflush(stdout);
|
||||
if (ops)
|
||||
ops->serve(handler);
|
||||
sessions->step(host.state(), host.status());
|
||||
std::fflush(stdout);
|
||||
if (o.crash_point == "after-ack" && !acknowledged.empty() && ++ack_steps == o.crash_ordinal)
|
||||
crash_now();
|
||||
const auto finish = [&](int code) {
|
||||
@@ -515,7 +557,9 @@ int main(int argc, char** argv) {
|
||||
"--pg-passfile F --pg-ca F] [--flush-ms N] [--drain-ms N] "
|
||||
"[--lease-profile default|fast] [--ready-file PATH] "
|
||||
"[--ops-listen ADDRESS:PORT] [--synthetic-inputs development-v1 "
|
||||
"[--synthetic-until TICK] [--accelerate N]]\n",
|
||||
"[--synthetic-until TICK] [--accelerate N] [--session-listen ADDRESS:PORT "
|
||||
"--session-cert F --session-key F --session-grant-key F "
|
||||
"--session-environment dev]]\n",
|
||||
stderr);
|
||||
return 2;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
#pragma once
|
||||
// M9.7 dev session bridge for evo-server (ADR-0083). Without EVO_SERVER_SESSIONS (a
|
||||
// build without EVO_BUILD_NETWORK) every operation is a no-op and session flags are
|
||||
// refused at parse time. The bridge reads host state; it never mutates it.
|
||||
#include "evo/persistence/region_lease.hpp"
|
||||
#include "evo/server/ops.hpp"
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#ifdef EVO_SERVER_SESSIONS
|
||||
#include "evo/network/dev_credentials.hpp"
|
||||
#include "evo/network/session_server.hpp"
|
||||
#include "evo/processes/lineage_region.hpp"
|
||||
#include "evo/world/environment.hpp"
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <memory>
|
||||
#endif
|
||||
|
||||
namespace evo::server_app {
|
||||
struct SessionOptions {
|
||||
std::string listen, certificate, key, grant_key, environment;
|
||||
[[nodiscard]] bool any() const noexcept {
|
||||
return !listen.empty() || !certificate.empty() || !key.empty() || !grant_key.empty() ||
|
||||
!environment.empty();
|
||||
}
|
||||
[[nodiscard]] bool complete() const noexcept {
|
||||
return !listen.empty() && !certificate.empty() && !key.empty() && !grant_key.empty() &&
|
||||
!environment.empty();
|
||||
}
|
||||
};
|
||||
// Intentions ride synthetic inputs, so sessions keep the driver's lookahead short.
|
||||
inline constexpr std::uint64_t session_lookahead_ticks = 8;
|
||||
|
||||
#ifdef EVO_SERVER_SESSIONS
|
||||
inline constexpr bool sessions_available = true;
|
||||
class SessionBridge {
|
||||
public:
|
||||
// Empty bridge when no session options were given; nullopt on a startup failure
|
||||
// (the caller exits with a usage/configuration error before serving).
|
||||
static std::optional<SessionBridge> open(const SessionOptions& o, persistence::RegionKey key,
|
||||
std::uint64_t frame) {
|
||||
SessionBridge bridge;
|
||||
if (!o.any())
|
||||
return bridge;
|
||||
const auto environment = o.environment == "dev" ? network::DeploymentEnvironment::dev
|
||||
: o.environment == "stage"
|
||||
? network::DeploymentEnvironment::stage
|
||||
: network::DeploymentEnvironment::world0;
|
||||
const auto secret = network::parse_grant_key(read(o.grant_key));
|
||||
if (!secret)
|
||||
return fail("error session-grant-key");
|
||||
auto grant_key = network::DevGrantKey::import(environment, *secret);
|
||||
if (!grant_key)
|
||||
return fail("error session-environment"); // Dev identity exists only in dev.
|
||||
network::SessionServerConfig config;
|
||||
config.listen = o.listen;
|
||||
config.credentials = {read(o.certificate), read(o.key)};
|
||||
config.admission.world = {key.world, frame};
|
||||
config.admission.region = key.region;
|
||||
config.admission.max_sessions = 4;
|
||||
config.admission.intent_window = session_lookahead_ticks;
|
||||
config.admission.window_ticks = session_lookahead_ticks;
|
||||
config.admission.max_intents_per_window = session_lookahead_ticks;
|
||||
auto server = network::SessionServer::create(std::move(config), std::move(*grant_key));
|
||||
if (!server)
|
||||
return fail(server.error() == network::SessionServerError::listen
|
||||
? "error session-listen"
|
||||
: "error session-credentials");
|
||||
bridge.server_ = std::move(*server);
|
||||
std::printf("session listening %u\n", static_cast<unsigned>(bridge.server_->port()));
|
||||
return bridge;
|
||||
}
|
||||
[[nodiscard]] bool enabled() const noexcept {
|
||||
return server_ != nullptr;
|
||||
}
|
||||
[[nodiscard]] std::optional<persistence::Uuid> carry(std::uint64_t tick) {
|
||||
return server_ ? server_->carry(tick) : std::nullopt;
|
||||
}
|
||||
void committed(const persistence::Uuid& request, std::uint64_t tick, std::uint64_t sequence) {
|
||||
if (server_)
|
||||
server_->committed(request, tick, sequence);
|
||||
}
|
||||
void not_committed(const persistence::Uuid& request) {
|
||||
if (server_)
|
||||
server_->not_committed(request);
|
||||
}
|
||||
// Publishes a view when the simulated tick moved, then services connections.
|
||||
template <typename State> void step(const State* state, const server::HostStatus& s) {
|
||||
if (!server_)
|
||||
return;
|
||||
const network::RegionFacts facts{s.ready && s.phase == server::HostPhase::running, s.epoch,
|
||||
s.simulated_tick, s.committed_tick, s.committed_sequence};
|
||||
if (state != nullptr && facts.ready &&
|
||||
(!published_ || *published_ != s.simulated_tick || epoch_ != s.epoch))
|
||||
publish(community(*state), facts);
|
||||
server_->poll(
|
||||
facts,
|
||||
static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::seconds>(
|
||||
std::chrono::system_clock::now().time_since_epoch())
|
||||
.count()),
|
||||
std::chrono::steady_clock::now());
|
||||
for (const auto& e : server_->take_events())
|
||||
std::printf("session %s %llu subject=%llu reason=%u\n",
|
||||
e.kind == network::SessionEvent::Kind::opened ? "opened"
|
||||
: e.kind == network::SessionEvent::Kind::closed ? "closed"
|
||||
: "refused",
|
||||
static_cast<unsigned long long>(e.session),
|
||||
static_cast<unsigned long long>(e.subject),
|
||||
static_cast<unsigned>(e.reason));
|
||||
}
|
||||
|
||||
private:
|
||||
static std::optional<SessionBridge> fail(const char* message) {
|
||||
std::puts(message);
|
||||
return std::nullopt;
|
||||
}
|
||||
static std::string read(const std::string& path) {
|
||||
std::ifstream file{path, std::ios::binary};
|
||||
return {std::istreambuf_iterator<char>{file}, std::istreambuf_iterator<char>{}};
|
||||
}
|
||||
static const processes::EnvironmentalCommunity&
|
||||
community(const processes::EnvironmentalCommunity& c) {
|
||||
return c;
|
||||
}
|
||||
static const processes::EnvironmentalCommunity& community(const processes::LineageRegion& r) {
|
||||
return r.community();
|
||||
}
|
||||
// A read-only copy of the authoritative stores: every habitat in the first site's
|
||||
// Z slice. The region has pooled producer/consumer inventories and no placed
|
||||
// individuals, so the organism presentation is explicitly empty.
|
||||
void publish(const processes::EnvironmentalCommunity& c, const network::RegionFacts& facts) {
|
||||
const auto& fields = c.resources().fields();
|
||||
const auto sites = fields.sites();
|
||||
if (sites.empty())
|
||||
return;
|
||||
const auto z = sites.front().voxel.z;
|
||||
std::vector<world::VoxelAddress> footprint;
|
||||
for (const auto& site : sites)
|
||||
if (site.voxel.z == z && footprint.size() < world::max_environment_footprint)
|
||||
footprint.push_back(site.voxel);
|
||||
const auto view =
|
||||
world::query_environment(fields, c.resources().terrain(), facts.simulated_tick,
|
||||
footprint, world::all_environment_fields);
|
||||
if (!view) {
|
||||
if (!warned_)
|
||||
std::printf("session view-unavailable %u\n", static_cast<unsigned>(view.error()));
|
||||
warned_ = true;
|
||||
return;
|
||||
}
|
||||
auto environment = client::present_environment(*view, z);
|
||||
auto organisms = client::OrganismPresentation::create(
|
||||
{view->identity(), facts.simulated_tick, 0}, z, {});
|
||||
if (!environment || !organisms)
|
||||
return;
|
||||
server_->publish(*environment, *organisms, facts);
|
||||
published_ = facts.simulated_tick;
|
||||
epoch_ = facts.epoch;
|
||||
}
|
||||
std::unique_ptr<network::SessionServer> server_;
|
||||
std::optional<std::uint64_t> published_;
|
||||
std::uint64_t epoch_{};
|
||||
bool warned_{};
|
||||
};
|
||||
#else
|
||||
inline constexpr bool sessions_available = false;
|
||||
class SessionBridge {
|
||||
public:
|
||||
static std::optional<SessionBridge> open(const SessionOptions&, persistence::RegionKey,
|
||||
std::uint64_t) {
|
||||
return SessionBridge{};
|
||||
}
|
||||
[[nodiscard]] bool enabled() const noexcept {
|
||||
return false;
|
||||
}
|
||||
[[nodiscard]] std::optional<persistence::Uuid> carry(std::uint64_t) {
|
||||
return std::nullopt;
|
||||
}
|
||||
void committed(const persistence::Uuid&, std::uint64_t, std::uint64_t) {}
|
||||
void not_committed(const persistence::Uuid&) {}
|
||||
template <typename State> void step(const State*, const server::HostStatus&) {}
|
||||
};
|
||||
#endif
|
||||
} // namespace evo::server_app
|
||||
@@ -65,6 +65,11 @@ if(EVO_BUILD_CLIENT)
|
||||
evo_owned_target(evo_client_platform)
|
||||
add_executable(evo-client apps/evo-client/main.cpp)
|
||||
target_link_libraries(evo-client PRIVATE evo_client_platform evo_client_ui)
|
||||
if(EVO_BUILD_NETWORK)
|
||||
# M9.7 remote dev session mode (ADR-0083).
|
||||
target_link_libraries(evo-client PRIVATE evo_network_session)
|
||||
target_compile_definitions(evo-client PRIVATE EVO_CLIENT_NETWORK=1)
|
||||
endif()
|
||||
evo_owned_target(evo-client)
|
||||
evo_client_runtime(evo-client)
|
||||
if(BUILD_TESTING)
|
||||
|
||||
+22
-4
@@ -18,6 +18,11 @@ set(USE_SHARED_MBEDTLS_LIBRARY OFF CACHE BOOL "" FORCE)
|
||||
set(USE_STATIC_TF_PSA_CRYPTO_LIBRARY ON CACHE BOOL "" FORCE)
|
||||
set(USE_SHARED_TF_PSA_CRYPTO_LIBRARY OFF CACHE BOOL "" FORCE)
|
||||
set(DISABLE_PACKAGE_CONFIG_AND_INSTALL ON CACHE BOOL "" FORCE)
|
||||
# Warnings-as-errors is policy for owned code. Upstream's own -Werror turns
|
||||
# compiler-version-specific diagnostics (GCC 13 -O3 maybe-uninitialized in
|
||||
# ssl_tls.c) into build failures of unmodified upstream sources.
|
||||
set(MBEDTLS_FATAL_WARNINGS OFF CACHE BOOL "" FORCE)
|
||||
set(TF_PSA_CRYPTO_FATAL_WARNINGS OFF CACHE BOOL "" FORCE)
|
||||
set(MBEDTLS_USER_CONFIG_FILE "${PROJECT_SOURCE_DIR}/third_party/mbedtls/evo_mbedtls_config.h"
|
||||
CACHE FILEPATH "Owned TLS 1.3-only restriction (ADR-0082)" FORCE)
|
||||
# Upstream code is not compiled with project warning policy or our sanitizer
|
||||
@@ -30,14 +35,27 @@ add_library(evo_network_protocol STATIC src/network/protocol/codec.cpp
|
||||
target_link_libraries(evo_network_protocol PUBLIC evo_client_views evo_client_controls)
|
||||
evo_owned_target(evo_network_protocol)
|
||||
# Mbed TLS adapters: dev grants (HMAC-SHA256 via PSA) and the TLS 1.3 channel.
|
||||
add_library(evo_network_tls STATIC src/network/tls/dev_grant.cpp src/network/tls/channel.cpp)
|
||||
add_library(evo_network_tls STATIC src/network/tls/dev_grant.cpp src/network/tls/channel.cpp
|
||||
src/network/tls/dev_credentials.cpp)
|
||||
target_link_libraries(evo_network_tls PUBLIC evo_network_protocol PRIVATE mbedtls)
|
||||
evo_owned_target(evo_network_tls)
|
||||
# M9.7 sessions: non-blocking TCP, the host-agnostic session server and the client.
|
||||
add_library(evo_network_session STATIC src/network/socket/tcp.cpp src/network/session/server.cpp
|
||||
src/network/session/client.cpp)
|
||||
target_link_libraries(evo_network_session PUBLIC evo_network_tls)
|
||||
if(WIN32)
|
||||
target_link_libraries(evo_network_session PRIVATE ws2_32)
|
||||
endif()
|
||||
evo_owned_target(evo_network_session)
|
||||
# Dev credential and grant tool (ADR-0083); generates throwaway-scale dev secrets only.
|
||||
add_executable(evo-dev-session apps/evo-dev-session/main.cpp)
|
||||
target_link_libraries(evo-dev-session PRIVATE evo_network_session)
|
||||
evo_owned_target(evo-dev-session)
|
||||
if(BUILD_TESTING)
|
||||
add_executable(evo-network-tests tests/network_protocol.cpp tests/network_tls.cpp
|
||||
tests/network_main.cpp)
|
||||
# The TLS test generates throwaway dev certificates with Mbed TLS directly.
|
||||
target_link_libraries(evo-network-tests PRIVATE evo_network_tls mbedtls)
|
||||
tests/network_session.cpp tests/network_main.cpp)
|
||||
# The TLS tests generate throwaway dev certificates with Mbed TLS directly.
|
||||
target_link_libraries(evo-network-tests PRIVATE evo_network_session mbedtls)
|
||||
evo_owned_target(evo-network-tests)
|
||||
add_test(NAME network COMMAND evo-network-tests)
|
||||
set_tests_properties(network PROPERTIES TIMEOUT 120 LABELS "network")
|
||||
|
||||
@@ -54,7 +54,14 @@ RUN cmake -P tools/prepare-sdl3.cmake \
|
||||
FROM ci-source AS ci-network
|
||||
# Explicit pinned Mbed TLS preparation (ADR-0082); codec, admission, dev grants and
|
||||
# TLS 1.3 loopback with static analysis, then again under ASan/UBSan.
|
||||
RUN cmake -P tools/prepare-mbedtls.cmake && cmake --preset network -DCMAKE_CXX_COMPILER=clang++-19 -DCMAKE_C_COMPILER=clang-19 -DEVO_ENABLE_TIDY=ON && cmake --build --preset network --parallel 2 && ctest --preset network --no-tests=error && cmake --preset network -DEVO_SANITIZER=address -DEVO_ENABLE_TIDY=OFF && cmake --build --preset network --parallel 2 && ctest --preset network --no-tests=error
|
||||
RUN cmake -P tools/prepare-mbedtls.cmake \
|
||||
&& cmake --preset network -DCMAKE_CXX_COMPILER=clang++-19 -DCMAKE_C_COMPILER=clang-19 \
|
||||
-DEVO_ENABLE_TIDY=ON \
|
||||
&& cmake --build --preset network --parallel 2 \
|
||||
&& ctest --preset network --no-tests=error \
|
||||
&& cmake --preset network -DEVO_SANITIZER=address -DEVO_ENABLE_TIDY=OFF \
|
||||
&& cmake --build --preset network --parallel 2 \
|
||||
&& ctest --preset network --no-tests=error
|
||||
|
||||
FROM ci-source AS ci-ecs
|
||||
# Explicit experiment preparation; normal configure/runtime remains dependency-free.
|
||||
@@ -111,14 +118,21 @@ COPY apps/ apps/
|
||||
COPY tests/ tests/
|
||||
COPY experiments/ experiments/
|
||||
COPY tools/ tools/
|
||||
COPY third_party/ third_party/
|
||||
COPY migrations/ migrations/
|
||||
# M9.7 (ADR-0083): evo-server includes the opt-in dev session listener; the pinned
|
||||
# Mbed TLS source is prepared explicitly and verified against its SHA-256.
|
||||
RUN cmake -P tools/prepare-mbedtls.cmake
|
||||
RUN cmake --preset release -B build/postgres -DCMAKE_CXX_COMPILER=g++-14 \
|
||||
-DCMAKE_C_COMPILER=gcc-14 -DEVO_BUILD_NETWORK=ON \
|
||||
-DEVO_BUILD_POSTGRES=ON -DPostgreSQL_ROOT=/source/.cache/postgres/install \
|
||||
&& cmake --build build/postgres --target evo-postgres-tests --parallel 2
|
||||
&& cmake --build build/postgres --target evo-postgres-tests evo-dev-session --parallel 2
|
||||
RUN cmake --preset analysis -B build/postgres-analysis -DCMAKE_CXX_COMPILER=clang++-19 \
|
||||
-DCMAKE_C_COMPILER=clang-19 -DEVO_BUILD_NETWORK=ON \
|
||||
-DEVO_BUILD_POSTGRES=ON -DPostgreSQL_ROOT=/source/.cache/postgres/install \
|
||||
&& cmake --build build/postgres-analysis --target evo-postgres-tests --parallel 2
|
||||
RUN cmake --preset asan -B build/postgres-asan -DCMAKE_CXX_COMPILER=clang++-19 \
|
||||
-DCMAKE_C_COMPILER=clang-19 -DEVO_BUILD_NETWORK=ON \
|
||||
-DEVO_BUILD_POSTGRES=ON -DPostgreSQL_ROOT=/source/.cache/postgres/install \
|
||||
&& cmake --build build/postgres-asan --target evo-postgres-tests --parallel 2
|
||||
|
||||
|
||||
@@ -58,7 +58,17 @@ spec:
|
||||
# Synthetic load is set only by tools/ops/fault-drill.sh (ADR-0073).
|
||||
inputs=()
|
||||
if [ -n "$REGION_INPUTS" ]; then inputs=(--synthetic-inputs "$REGION_INPUTS"); fi
|
||||
# Dev sessions are a drill-time override too (tools/ops/session-drill.sh,
|
||||
# ADR-0083): loopback only, reached by kubectl port-forward, never a Service.
|
||||
# evo-server refuses any environment but dev and needs the synthetic carrier.
|
||||
sessions=()
|
||||
if [ -n "$REGION_SESSION" ]; then
|
||||
sessions=(--session-listen 127.0.0.1:7400 --session-cert /run/session/server.pem
|
||||
--session-key /run/session/server.key --session-grant-key /run/session/grant.key
|
||||
--session-environment "$REGION_SESSION")
|
||||
fi
|
||||
exec /usr/local/bin/evo-server --profile lineage "${retained[@]}" "${inputs[@]}" \
|
||||
"${sessions[@]}" \
|
||||
--world "$REGION_WORLD" --region "$REGION_ID" --frame "$REGION_FRAME" \
|
||||
--pg-service /run/postgres/pg_service.conf --pg-passfile /tmp/pgpass \
|
||||
--objects /objects --objects-reserve-mib "$REGION_RESERVE_MIB" \
|
||||
@@ -74,6 +84,8 @@ spec:
|
||||
# Drill-only overrides; Git keeps no synthetic load and the 1024 MiB reserve.
|
||||
- {name: REGION_INPUTS, value: ""}
|
||||
- {name: REGION_RESERVE_MIB, value: "1024"}
|
||||
# M9.7 drill-only: "dev" enables the session listener; Git keeps it empty.
|
||||
- {name: REGION_SESSION, value: ""}
|
||||
ports:
|
||||
- {name: ops, containerPort: 9100, protocol: TCP}
|
||||
readinessProbe:
|
||||
@@ -97,6 +109,7 @@ spec:
|
||||
volumeMounts:
|
||||
- {name: objects, mountPath: /objects}
|
||||
- {name: connection, mountPath: /run/postgres, readOnly: true}
|
||||
- {name: session, mountPath: /run/session, readOnly: true}
|
||||
- {name: scratch, mountPath: /tmp}
|
||||
volumes:
|
||||
- name: objects
|
||||
@@ -112,3 +125,14 @@ spec:
|
||||
- {key: ca.crt, path: ca.crt}
|
||||
- name: scratch
|
||||
emptyDir: {medium: Memory, sizeLimit: 1Mi}
|
||||
# Created out of band in evolution-dev only (docs/networking/DEV_SESSION_OPERATIONS.md).
|
||||
# Optional: stage and World0 never have it, and the pod starts without it.
|
||||
- name: session
|
||||
secret:
|
||||
secretName: evolution-dev-session
|
||||
optional: true
|
||||
defaultMode: 0440
|
||||
items:
|
||||
- {key: server.pem, path: server.pem}
|
||||
- {key: server.key, path: server.key}
|
||||
- {key: grant.key, path: grant.key}
|
||||
|
||||
+11
-1
@@ -423,7 +423,17 @@ session/epoch/commit watermark, explicit little-endian codec), session admission
|
||||
(scope, single-use grants, one session per subject, epoch, replay, tick window,
|
||||
rate) and dev-only HMAC grants refused outside `dev`, over a TLS 1.3-only channel
|
||||
with pinned Mbed TLS 4.1.1 LTS under ADR-0082. Sockets and region integration are
|
||||
M9.7. Evidence in engineering/VALIDATION.md. Next gate: M9.7.
|
||||
M9.7. Evidence in engineering/VALIDATION.md.
|
||||
|
||||
**M9.7 status (2026-10-01):** implemented and locally qualified under ADR-0083,
|
||||
proceeding with the maintainer's agreement although M8-dev/stage is not yet passed.
|
||||
Real TLS sessions inside evo-server: views from the region's own stores (explicitly
|
||||
no individual organisms), admitted intentions ride synthetic inputs and return
|
||||
durable no-effect outcomes, reconnect requests a fresh baseline, stale epochs and
|
||||
duplicate sessions are refused. Evidence: Windows/Linux loopback suites and real
|
||||
evo-server + PostgreSQL process tests including SIGKILL and a successor on the same
|
||||
port. The in-cluster session drill (pod restart) awaits an operator with dev cluster
|
||||
access. Next gate: M9.8.
|
||||
|
||||
**Goal:** a native desktop cellular viewer/controller with input, camera, renderer,
|
||||
readable organism/environment views and a real development-server connection.
|
||||
|
||||
+16
-13
@@ -2,20 +2,23 @@
|
||||
|
||||
## Current gate — 2026-10-01
|
||||
|
||||
M9.6 defines the narrow secure dev-session protocol
|
||||
([ADR-0082](adr/0082-dev-session-transport.md),
|
||||
[DEV_SESSION_PROTOCOL](networking/DEV_SESSION_PROTOCOL.md)). Versioned bounded
|
||||
frames carry Hello/Welcome/Intent/IntentResult/views/Close with session, epoch and
|
||||
commit watermark; decoding is explicit and fuzzed. Session admission enforces
|
||||
scope, single-use dev grants, one session per subject, epoch, replay, tick window
|
||||
and rate. Dev grants are HMAC-SHA256 and cannot be created or verified outside
|
||||
`dev`. The channel is TLS 1.3 only, using pinned Mbed TLS 4.1.1 LTS with TLS 1.2,
|
||||
PSK and tickets compiled out. Opt-in via `EVO_BUILD_NETWORK`. Remote Gitea
|
||||
execution of the new `network` CI suite remains unverified locally.
|
||||
M9.7 connects the engineering client to an authoritative dev region
|
||||
([ADR-0083](adr/0083-dev-region-sessions.md),
|
||||
[DEV_SESSION_OPERATIONS](networking/DEV_SESSION_OPERATIONS.md)). evo-server runs a
|
||||
non-blocking TLS 1.3 session server in its loop; clients see environment views from
|
||||
the region's real field/terrain stores and an explicitly empty organism layer. An
|
||||
admitted intention rides a synthetic input and returns a durable, no-effect outcome
|
||||
after the host's commit. Reconnects consume fresh grants and start from a fresh
|
||||
baseline; stale epochs, duplicates, foreign grants and floods are refused.
|
||||
|
||||
Next: **M9.7**, sockets, server/client session adapters, dev secret provisioning
|
||||
and a real connection to one authoritative dev region. No deployment, simulation
|
||||
rule or persistent format changed in M9.6.
|
||||
Proceeding with the maintainer's agreement although **M8-dev/stage is not passed**.
|
||||
Local evidence is real (evo-server + PostgreSQL, SIGKILL + successor); the dev
|
||||
deployment path (drill-time override, loopback listener, optional Secret,
|
||||
`tools/ops/session-drill.sh`) is prepared but **not run**: no cluster access was
|
||||
available. Next: **M9.8** packaging and presentation qualification.
|
||||
|
||||
M9.6 defined the dev-session protocol and TLS 1.3 channel
|
||||
([ADR-0082](adr/0082-dev-session-transport.md)).
|
||||
|
||||
M9.5 added biology-derived organism rendering without controller labels
|
||||
([ADR-0081](adr/0081-organism-presentation.md)).
|
||||
|
||||
@@ -86,6 +86,7 @@ connection byte/time limits. Upstream advisories require prompt pin updates (the
|
||||
July 2026 releases fixed several TLS issues, some in code compiled out here).
|
||||
|
||||
## Consequences
|
||||
M9.7 adds sockets, server/client session adapters, dev secret provisioning and
|
||||
Amended by ADR-0083 before deployment: v1 also carries IntentOutcome (type 8) and
|
||||
close reason `capacity`. M9.7 adds sockets, server/client session adapters, dev secret provisioning and
|
||||
reconnect semantics. Pin upgrades change version, hash and this ADR together and
|
||||
rerun the network suite. M10 reopens transport selection with measurements.
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
# ADR-0083: Dev region sessions, no-effect intentions and the session drill
|
||||
|
||||
## Status
|
||||
Accepted for M9.7 (engineering client to one authoritative **dev** region). Decisions
|
||||
confirmed with the maintainer on 2026-10-01: durable no-effect intentions, an
|
||||
explicitly empty organism presentation, local real-process evidence plus a dev
|
||||
deployment path. No biology, gameplay, format or production identity decision.
|
||||
|
||||
## Context
|
||||
ADR-0082 defined protocol v1, admission, dev grants and TLS. M9.7 needs a real
|
||||
desktop-to-region connection, view display, a minimal admitted intention whose final
|
||||
outcome reflects the server's durable acknowledgement, and recovery after restarts.
|
||||
Findings that constrain the design:
|
||||
|
||||
- The roadmap lists M8-dev/stage as a dependency; it is **not passed** (live gates
|
||||
open). The maintainer chose to proceed: local real-process evidence now, cluster
|
||||
evidence through a drill they run.
|
||||
- The region (`LineageRegion` over `EnvironmentalCommunity`) has pooled producer and
|
||||
consumer inventories driven by host facts. There is no movable organism, so an
|
||||
intention cannot have a biological effect without new rules and formats.
|
||||
- The region has real field/terrain stores (environment views are derivable) but no
|
||||
placed individuals (no honest organism presentation).
|
||||
- Dev runs without synthetic inputs in Git (ADR-0073); NodePort/LoadBalancer are
|
||||
forbidden and network policy is default-deny (ADR-0069). No kubeconfig exists on
|
||||
the implementing workstation.
|
||||
|
||||
## Decision
|
||||
**Session server in the region process.** `SessionServer` (src/network/session) is
|
||||
host-agnostic and non-blocking; `evo-server` calls it from its existing single-thread
|
||||
loop (as it does the ops server). It never mutates host state. Per connection:
|
||||
bounded output (newest view replaces an unsent one; overflow drops the connection),
|
||||
1 s Close flush deadline, 5 s accept-to-Welcome deadline, 64 KiB/s input limit,
|
||||
session bound to its connection (a connection can only speak for its own session).
|
||||
At most 4 connections/sessions. New epochs close existing sessions (`stale_epoch`) and
|
||||
discard intentions not yet carried.
|
||||
|
||||
**Views.** Every new simulated tick (and new epoch), the bridge queries the region's
|
||||
own stores (`query_environment` over the sites in the first site's Z slice, all
|
||||
fields) and publishes an `EnvironmentPresentation` plus an **empty**
|
||||
`OrganismPresentation`. View headers carry session, epoch, simulated tick and the
|
||||
committed tick as the durable watermark. Every session starts from the latest view.
|
||||
|
||||
**Intentions.** Sessions require `--synthetic-inputs development-v1`. An admitted
|
||||
intention is queued and rides the next synthetic input whose tick is at or after its
|
||||
intended tick: the input's request id becomes `INT1 | epoch | session | sequence`
|
||||
(low 32 bits each); its **content stays the host-chosen development input**. The
|
||||
host's FinalAck for that request yields `IntentOutcome{committed_no_effect, tick,
|
||||
commit_sequence}`; a dropped input yields `not_committed`. The driver looks ahead 8
|
||||
ticks (instead of 64) when sessions are on, matching the admission window.
|
||||
|
||||
**Protocol v1 amendment.** Before any v1 peer was deployed, v1 gained message type 8
|
||||
`IntentOutcome` and close reason 9 `capacity` (schemas/view/dev-session-v1.md). Once
|
||||
a dev deployment runs, further changes need a new version.
|
||||
|
||||
**Client.** `SessionClient` connects, verifies the pinned dev CA, consumes one
|
||||
single-use grant per Hello, and on Welcome resets both layers (fresh baseline). On
|
||||
loss it keeps the last presentation marked stale, reports outstanding admitted
|
||||
intentions as `unknown`, and retries with capped exponential backoff until grants run
|
||||
out (explicit `exhausted`). Intended tick = newest view tick + 4. The desktop app
|
||||
sends an intention only when its vector changes, at most once per view tick.
|
||||
`evo-client --connect ... --ca ... --grants ...` replaces the local fixture.
|
||||
|
||||
**Credentials and grants.** `evo-dev-session credentials` writes a dev CA, server
|
||||
certificate/key and a grant HMAC key (secrets owner-only, never overwritten, refused
|
||||
outside dev). `evo-dev-session grants` issues up to 64 single-use grants (lifetime
|
||||
<= 1 h) on the operator machine, so clients never hold the HMAC key.
|
||||
|
||||
**Dev deployment.** The region Deployment gains a drill-time `REGION_SESSION` override
|
||||
(empty in Git, enforced by the policy check), an optional 0440 Secret
|
||||
`evolution-dev-session`, and a listener bound to **127.0.0.1:7400** inside the pod:
|
||||
reachable only by `kubectl port-forward`, never a Service. `tools/ops/session-drill.sh`
|
||||
(evolution-dev only) enables inputs and sessions, runs `evo-dev-session probe
|
||||
--expect-restart` through a port-forward (durable ack, forced pod deletion,
|
||||
reconnect at a newer epoch, second durable ack) and restores Git values on exit.
|
||||
|
||||
Mbed TLS build changes: upstream `-Werror` disabled for upstream sources (owned code
|
||||
keeps warnings-as-errors), and TLS context serialization compiled out.
|
||||
|
||||
## Alternatives considered
|
||||
- Map intentions onto `EnvironmentalInput` (bites/temperature): lets clients author
|
||||
biological inputs; rejected.
|
||||
- Add a movable organism now: new rules, persisted format and converter; deferred.
|
||||
- Synthesize organisms from pools: fabricated presentation; rejected.
|
||||
- Admission result only: does not reflect durable outcomes; rejected.
|
||||
- Expose the dev region via NodePort/Ingress: violates ADR-0069 and widens exposure.
|
||||
- Enable synthetic inputs and sessions permanently in dev: contradicts ADR-0073 and
|
||||
grows dev history continuously; drill-time only instead.
|
||||
- Separate gateway process: needs host IPC for acks and views; unnecessary for one
|
||||
dev region.
|
||||
|
||||
## Benefits
|
||||
Real authority path end to end (TLS, admission, durable ack, recovery) without new
|
||||
game rules; honest presentation; dev identity and listener cannot reach stage/World0.
|
||||
|
||||
## Costs
|
||||
Intentions are delayed until a carrier input (up to ~9 ticks); one intention per
|
||||
carried tick; sessions require synthetic inputs; the session drill needs an operator.
|
||||
|
||||
## Risks
|
||||
Port-forward reachability depends on cluster tooling; the drill's evidence is only as
|
||||
complete as its logs. A session id is bound to its connection but grants are bearer
|
||||
tokens: anyone holding an unused grant (or the dev key) can open a dev session.
|
||||
Restart gaps: an intention admitted just before a crash has an unknown outcome until
|
||||
the client reconnects (reported as such, never assumed).
|
||||
|
||||
## Consequences
|
||||
M9.8 packages the client and qualifies the presentation. M10 replaces the carrier
|
||||
with real intention semantics once organisms move, and reopens transport choice.
|
||||
@@ -94,3 +94,4 @@ design. No unadopted candidate is a link dependency of the proof.
|
||||
- [ADR-0080: Environment presentation](0080-environment-presentation.md) - owned revision-tagged environment presentations, ordered freshness and bounded explicit missing/stale rendering.
|
||||
- [ADR-0081: Organism presentation](0081-organism-presentation.md) - biology-only organism cells without controller labels, budgeted rendering with explicit truncation and an engineering gauge inspector; ImGui deferred.
|
||||
- [ADR-0082: Dev session transport](0082-dev-session-transport.md) - TLS 1.3-only Mbed TLS 4.1.1 LTS (pinned, compiled-down), dev-only HMAC grants, versioned bounded protocol v1 and session admission for M9.
|
||||
- [ADR-0083: Dev region sessions](0083-dev-region-sessions.md) - session server in evo-server, durable no-effect intentions on synthetic carriers, empty organism presentation, reconnect with fresh baselines, loopback-only drill-time dev deployment.
|
||||
|
||||
@@ -69,6 +69,10 @@ and alive/dead only; no controller, owner or level field exists, so players and
|
||||
NPCs share one presentation. Rendering takes an explicit rectangle budget and
|
||||
reports truncation; the inspector draws biology gauges only. ImGui is not adopted.
|
||||
|
||||
M9.7 (ADR-0083): in remote mode the layers are owned by `network::SessionClient`,
|
||||
filled only from server views; on loss they stay visible but stale, and every new
|
||||
session resets them to the server's fresh baseline. The client never predicts.
|
||||
|
||||
Failure tests inject actual SDL events, including foreign window IDs, with no
|
||||
test-only production hooks. Real device failures and physical DPI/input require
|
||||
manual hardware qualification at M9.8. The native client is opt-in and never
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# Native engineering client (M9.2–M9.5)
|
||||
# Native engineering client (M9.2–M9.7)
|
||||
|
||||
This opens a resizable native window showing camera-controlled environment habitats
|
||||
and biology-derived organisms (M9.5) from a local in-memory fixture authority.
|
||||
@@ -8,6 +8,11 @@ Contract: [client architecture](../architecture/CLIENT_ARCHITECTURE.md), ADR-007
|
||||
Camera/input: [contracts and limits](CAMERA_INPUT.md), ADR-0079.
|
||||
Environment: [presentation and encodings](ENVIRONMENT_RENDERING.md), ADR-0080.
|
||||
Organisms/inspector: [encodings and budget](ORGANISM_RENDERING.md), ADR-0081.
|
||||
Remote dev region: [operations](../networking/DEV_SESSION_OPERATIONS.md), ADR-0083.
|
||||
`--connect HOST:PORT --server-name NAME --ca FILE --grants FILE` (preset
|
||||
`client-network`) replaces the local fixture with a dev session: views come from the
|
||||
region, WASD sends intentions, and chips at y=72 show the connection (green/amber/red)
|
||||
and the last intention (white admitted, green durable, red rejected, grey unknown).
|
||||
|
||||
## Build and run
|
||||
|
||||
|
||||
@@ -59,7 +59,7 @@ marker left of it means organisms were not drawn because of the frame budget.
|
||||
marker). If every visible organism fits at 10 rectangles, full detail is used;
|
||||
otherwise each visible organism is a single body; beyond the budget, organisms are
|
||||
counted as visible but not drawn and the marker appears. The engineering app
|
||||
budgets 4096 - 2313 (environment) - 25 (inspector) - 3 (host UI) = 1755, so 1024
|
||||
budgets 4096 - 2313 (environment) - 25 (inspector) - 5 (host UI) = 1753, so 1024
|
||||
organisms always fit as bodies. No textures, caches or history are kept.
|
||||
|
||||
## Engineering inspector
|
||||
|
||||
@@ -3445,3 +3445,67 @@ locally. No sockets, server integration, secret provisioning or deployment exist
|
||||
yet (M9.7). TLS interoperability was tested only between Mbed TLS endpoints. No
|
||||
performance or latency measurement is claimed. Certificates in tests are
|
||||
throwaway; no dev CA or HMAC key was created or committed.
|
||||
|
||||
## M9.7 connection to one authoritative dev region — 2026-10-01
|
||||
|
||||
Implemented ADR-0083 and DEV_SESSION_OPERATIONS.md: non-blocking TCP (Winsock/
|
||||
POSIX), `SessionServer` in evo-server's loop, `SessionClient`, evo-client remote mode,
|
||||
`evo-dev-session credentials|grants|probe`, the drill-time dev deployment override
|
||||
and `tools/ops/session-drill.sh`. Maintainer decisions (2026-10-01): proceed although
|
||||
M8-dev/stage is not passed; durable no-effect intentions; explicitly empty organism
|
||||
presentation; local real-process evidence plus a dev deployment path.
|
||||
|
||||
Executed:
|
||||
|
||||
- Windows MSVC 19.44 Release: `-Preset network` 1/1 and `-Preset client-network`
|
||||
6/6 (network, client environment/organisms/lifecycle/smoke/init-failure). The
|
||||
network suite now includes four loopback session scenarios over real Winsock + TLS:
|
||||
connect/view/intent/durable/not-committed/replay/invalid vector; duplicate
|
||||
subject, reused grant, foreign key and region, capacity; stale epoch with unknown
|
||||
outcome, stale layers, fresh-grant reconnect and fresh baseline, and a server
|
||||
"restart" on the same port at a new epoch with an earlier tick; not-ready refusal
|
||||
without spending the grant, byte-rate flood closure (server-side decision),
|
||||
plaintext and silent connections dropped (the latter at the 200 ms deadline).
|
||||
- `evo-dev-session` on Windows: credentials written, overwrite refused (exit 1),
|
||||
`--environment stage` refused (exit 2), 4-grant bundle = 432 bytes.
|
||||
- Linux GCC 14 Release with PostgreSQL 18.6 + network (`build/m97-postgres`): network
|
||||
suite passed; the **full** `evo-postgres-tests` suite passed (adapter, lease,
|
||||
checkpoint, host, server process, the new region dev session test, recovery, ops,
|
||||
clone, store, load, upgrade, migration). The session process test runs real
|
||||
evo-server processes: dev-only and synthetic-carrier refusals (exit 2), TLS
|
||||
session, server-store view and empty organism layer, admitted intention whose
|
||||
`INT1...` request appears in the host's `ack` line at the reported tick/commit,
|
||||
replay rejected, duplicate session refused, SIGKILL with the client reporting
|
||||
loss and stale layers, successor on the same port ready after lease expiry,
|
||||
reconnect at epoch 2 and a second durable outcome with a higher commit.
|
||||
- End-to-end rehearsal on the development-v1 region (world 100, region 1, frame 9;
|
||||
`evo-world-clone provision`) with tool-generated credentials: probe durable ack
|
||||
(tick 11, commit 1); SIGKILL; successor Ready in 3472 ms at epoch 2 from committed
|
||||
tick 11; two `server_shutdown` refusals during recovery that did not spend grants;
|
||||
reconnect and second durable ack (tick 22, commit 3); `probe passed`. The
|
||||
**Windows** evo-client (`--connect` through WSL2 localhost forwarding, software
|
||||
renderer, 300 frames) then received `session welcome session=2 epoch=2 tick=45
|
||||
commit=42` and exited 0. (The rehearsal script's `server2 exit=127` is a `wait`
|
||||
on a non-child PID in the script, not a server exit status.)
|
||||
- Clang 19 ASan/UBSan: network suite passed. clang-tidy 19 (warnings as errors):
|
||||
network library, session tests, dev tool, evo-client and evo-server/main plus the
|
||||
session process test all clean, after consuming best-effort results explicitly,
|
||||
a `must()` test helper for required optionals, and a GCC-reported genuine
|
||||
dangling reference in the process test fixed. Format check, CI selector fixtures
|
||||
and deploy policy (67 objects, 0 failures) passed; a temporary violation
|
||||
(`REGION_SESSION=dev`, `0.0.0.0:7400`) produced exactly the two new failures.
|
||||
|
||||
Defects found and fixed during validation: grants were spent on `server_shutdown`/
|
||||
`capacity` refusals (exhausting reconnection while a successor recovered); the
|
||||
session process test reused region ids 41/42 owned by the recovery suite; Mbed TLS's
|
||||
own `-Werror` failed on a GCC 13 `-O3` false positive (upstream warnings no longer
|
||||
fatal; context serialization compiled out).
|
||||
|
||||
Limits: **no in-cluster evidence**. This workstation has no kubeconfig and Docker
|
||||
was stopped, so no image was built or pushed, nothing was applied to evolution-dev,
|
||||
and `tools/ops/session-drill.sh` has only been syntax-checked (no shellcheck
|
||||
available). The CI `network`, `client` and `postgres` stages with the new options
|
||||
were not executed remotely. Port-forward reachability of the pod-loopback listener
|
||||
is the documented design, not yet observed. M8-dev/stage remains not passed. The
|
||||
connected desktop run was hidden/software-rendered; no human visual check of remote
|
||||
mode. Intentions have no biological effect by design.
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
# Dev session operations (M9.7)
|
||||
|
||||
Decision: [ADR-0083](../adr/0083-dev-region-sessions.md); protocol:
|
||||
[DEV_SESSION_PROTOCOL](DEV_SESSION_PROTOCOL.md). **evolution-dev only.** Stage and
|
||||
World0 have no session Secret, and `evo-server` refuses any environment but `dev`.
|
||||
|
||||
## 1. Build the tools
|
||||
|
||||
```sh
|
||||
cmake -P tools/prepare-mbedtls.cmake
|
||||
cmake --preset network && cmake --build --preset network # evo-dev-session
|
||||
# Desktop client with remote mode (also needs SDL prepared):
|
||||
cmake --preset client-network && cmake --build --preset client-network
|
||||
```
|
||||
|
||||
The server image (`deploy/docker/Dockerfile`, `server-runtime`) is built with
|
||||
`EVO_BUILD_NETWORK=ON`; a new image digest must be pinned in the dev overlay as usual
|
||||
(ADR-0071) before the session flags exist in the cluster.
|
||||
|
||||
## 2. Create credentials on the operator machine (once)
|
||||
|
||||
```sh
|
||||
evo-dev-session credentials --environment dev \
|
||||
--server-name dev-region.evolution.test --out ~/aevolution-dev-session
|
||||
```
|
||||
|
||||
Writes `dev-ca.pem` (public; give to dev clients), `server.pem` (public),
|
||||
`server.key` and `grant.key` (secrets, owner-only, never overwritten). Keep the
|
||||
directory private. Never commit these files; the policy check rejects Secrets in Git.
|
||||
|
||||
## 3. Provision the Secret (cluster administrator, out of band)
|
||||
|
||||
```sh
|
||||
kubectl -n evolution-dev create secret generic evolution-dev-session \
|
||||
--from-file=server.pem=$HOME/aevolution-dev-session/server.pem \
|
||||
--from-file=server.key=$HOME/aevolution-dev-session/server.key \
|
||||
--from-file=grant.key=$HOME/aevolution-dev-session/grant.key
|
||||
```
|
||||
|
||||
The Deployment mounts it optionally (0440). Rotation: create new credentials,
|
||||
replace the Secret, restart evo-region, redistribute `dev-ca.pem`. Old grants stop
|
||||
verifying with the new key.
|
||||
|
||||
## 4. Session drill (enables sessions temporarily, then restores Git values)
|
||||
|
||||
```sh
|
||||
EVO_DEV_SESSION=./build/network/evo-dev-session \
|
||||
tools/ops/session-drill.sh ~/aevolution-dev-session [--context <name>]
|
||||
```
|
||||
|
||||
Expected: `probe durable ...` before and after the forced pod deletion, a second
|
||||
`probe welcome` with a higher epoch, `PASS session drill`. Record the output in
|
||||
engineering/VALIDATION.md. Exit 1 on any missing observation; Git values are restored
|
||||
on every exit path.
|
||||
|
||||
## 5. Interactive engineering session
|
||||
|
||||
While a drill-style override is active (`REGION_INPUTS=development-v1`,
|
||||
`REGION_SESSION=dev`; revert afterwards: `kubectl -n evolution-dev set env
|
||||
deployment/evo-region REGION_INPUTS= REGION_SESSION=`):
|
||||
|
||||
```sh
|
||||
evo-dev-session grants --environment dev --key ~/aevolution-dev-session/grant.key \
|
||||
--world 100 --frame 9 --region 1 --subject 7 --count 16 --out grants.bin
|
||||
kubectl -n evolution-dev port-forward deploy/evo-region 17400:7400 &
|
||||
evo-client --connect 127.0.0.1:17400 --server-name dev-region.evolution.test \
|
||||
--ca ~/aevolution-dev-session/dev-ca.pem --grants grants.bin
|
||||
```
|
||||
|
||||
The listener binds the pod's loopback, so only port-forward reaches it; nothing is
|
||||
exposed through a Service, NodePort or Ingress. A grant bundle lasts at most one hour
|
||||
and each connection consumes one grant. WASD sends intentions; the small chip under
|
||||
the intention dot turns white (admitted), green (durable, no effect) or red/grey
|
||||
(rejected or outcome unknown). Console lines report every session event.
|
||||
@@ -30,9 +30,15 @@ sequenceDiagram
|
||||
| Session admission | `evo_network_protocol` (`admission.hpp`) | codec, world identity |
|
||||
| Dev grants (HMAC-SHA256) | `evo_network_tls` (`dev_grant.hpp`) | PSA crypto (Mbed TLS) |
|
||||
| TLS 1.3 channel | `evo_network_tls` (`tls_channel.hpp`) | Mbed TLS, caller `ByteTransport` |
|
||||
| Sockets, server/client adapters | M9.7 | this gate |
|
||||
| Non-blocking TCP | `evo_network_session` (`tcp.hpp`) | Winsock/POSIX |
|
||||
| Session server and client | `evo_network_session` (`session_server.hpp`, `session_client.hpp`) | all of the above |
|
||||
| Region integration | `apps/evo-server/session_bridge.hpp` | region host (read-only) |
|
||||
|
||||
Nothing here links into `evo_simulation`, world stores or the region host yet.
|
||||
M9.7 ([ADR-0083](../adr/0083-dev-region-sessions.md)) runs the session server in
|
||||
`evo-server`'s loop: views from the region's real field/terrain stores (empty organism
|
||||
presentation), admitted intentions riding synthetic inputs with durable no-effect
|
||||
outcomes, client reconnect with fresh baselines. Operations:
|
||||
[DEV_SESSION_OPERATIONS](DEV_SESSION_OPERATIONS.md). Nothing links into `evo_simulation`.
|
||||
`EVO_BUILD_NETWORK` is opt-in and requires `cmake -P tools/prepare-mbedtls.cmake`.
|
||||
|
||||
## Trust and authority
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
#include "evo/network/dev_grant.hpp"
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
// Dev-only session credentials (ADR-0083): a self-signed dev CA, a server
|
||||
// certificate/key for one name, and a 32-byte grant HMAC key. Generated on an
|
||||
// operator machine for the dev namespace Secret; refused for stage and World0.
|
||||
namespace evo::network {
|
||||
struct DevCredentials {
|
||||
std::string authority_pem; // Public: given to dev clients.
|
||||
std::string server_certificate_pem; // Public.
|
||||
std::string server_key_pem; // Secret.
|
||||
std::array<std::uint8_t, dev_key_bytes> grant_key{}; // Secret.
|
||||
};
|
||||
[[nodiscard]] std::expected<DevCredentials, GrantError>
|
||||
generate_dev_credentials(DeploymentEnvironment environment, std::string_view server_name,
|
||||
std::uint32_t days, std::chrono::system_clock::time_point now);
|
||||
// Lowercase hex, 64 digits; surrounding whitespace is ignored when parsing.
|
||||
[[nodiscard]] std::string format_grant_key(std::span<const std::uint8_t> key);
|
||||
[[nodiscard]] std::expected<std::array<std::uint8_t, dev_key_bytes>, GrantError>
|
||||
parse_grant_key(std::string_view text);
|
||||
// Random 64-bit nonzero grant ids from the PSA RNG.
|
||||
[[nodiscard]] std::expected<std::uint64_t, GrantError> random_grant_id();
|
||||
} // namespace evo::network
|
||||
@@ -27,7 +27,8 @@ enum class MessageType : std::uint8_t {
|
||||
intent_result,
|
||||
environment_view,
|
||||
organism_view,
|
||||
close
|
||||
close,
|
||||
intent_outcome
|
||||
};
|
||||
enum class ProtocolError : std::uint8_t {
|
||||
truncated,
|
||||
@@ -119,7 +120,16 @@ enum class CloseReason : std::uint8_t {
|
||||
duplicate_session,
|
||||
stale_epoch,
|
||||
rate_limited,
|
||||
server_shutdown
|
||||
server_shutdown,
|
||||
capacity
|
||||
};
|
||||
// Final, after the host's durable commit (or loss): the M9.7 dev region has no
|
||||
// movable organism, so a committed intention has no biological effect.
|
||||
enum class IntentFinality : std::uint8_t { committed_no_effect, not_committed };
|
||||
struct IntentOutcome {
|
||||
std::uint64_t sequence{};
|
||||
IntentFinality finality{};
|
||||
std::uint64_t tick{}, commit_sequence{}; // Zero when not committed.
|
||||
};
|
||||
|
||||
[[nodiscard]] std::expected<std::vector<std::uint8_t>, ProtocolError> encode(const Hello& m);
|
||||
@@ -127,6 +137,7 @@ enum class CloseReason : std::uint8_t {
|
||||
[[nodiscard]] std::vector<std::uint8_t> encode(const IntentMessage& m);
|
||||
[[nodiscard]] std::vector<std::uint8_t> encode(const IntentResult& m);
|
||||
[[nodiscard]] std::vector<std::uint8_t> encode(CloseReason reason);
|
||||
[[nodiscard]] std::vector<std::uint8_t> encode(const IntentOutcome& m);
|
||||
[[nodiscard]] std::vector<std::uint8_t> encode(const ViewHeader& header,
|
||||
const client::EnvironmentPresentation& view);
|
||||
[[nodiscard]] std::vector<std::uint8_t> encode(const ViewHeader& header,
|
||||
@@ -139,6 +150,7 @@ enum class CloseReason : std::uint8_t {
|
||||
[[nodiscard]] std::expected<IntentMessage, ProtocolError> decode_intent(FrameView frame);
|
||||
[[nodiscard]] std::expected<IntentResult, ProtocolError> decode_intent_result(FrameView frame);
|
||||
[[nodiscard]] std::expected<CloseReason, ProtocolError> decode_close(FrameView frame);
|
||||
[[nodiscard]] std::expected<IntentOutcome, ProtocolError> decode_intent_outcome(FrameView frame);
|
||||
[[nodiscard]] std::expected<std::pair<ViewHeader, client::EnvironmentPresentation>, ProtocolError>
|
||||
decode_environment_view(FrameView frame);
|
||||
[[nodiscard]] std::expected<std::pair<ViewHeader, client::OrganismPresentation>, ProtocolError>
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
#pragma once
|
||||
#include "evo/client/environment_view.hpp"
|
||||
#include "evo/client/input.hpp"
|
||||
#include "evo/client/organism_view.hpp"
|
||||
#include "evo/network/dev_grant.hpp"
|
||||
#include "evo/network/protocol.hpp"
|
||||
#include "evo/network/tls_channel.hpp"
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// M9.7 dev session client (ADR-0083). Non-blocking; the desktop host calls poll()
|
||||
// once per frame. Holds presentation copies only: it never predicts, simulates or
|
||||
// treats a local value as authority. Each Hello consumes one single-use grant (except
|
||||
// after a `server_shutdown`/`capacity` refusal, when the server never recorded it);
|
||||
// every new session starts from a fresh server baseline.
|
||||
namespace evo::network {
|
||||
struct SessionClientConfig {
|
||||
std::string host;
|
||||
std::uint16_t port{};
|
||||
ClientTrust trust;
|
||||
std::vector<DevGrant> grants; // Consumed in order, one per Hello.
|
||||
std::uint32_t lead_ticks{4}; // Intended tick = newest view tick + lead.
|
||||
std::chrono::milliseconds attempt_deadline{5000}; // Connect to Welcome.
|
||||
std::chrono::milliseconds first_retry{500}, max_retry{8000};
|
||||
};
|
||||
enum class SessionPhase : std::uint8_t { backoff, connecting, established, exhausted };
|
||||
struct ClientEvent {
|
||||
enum class Kind : std::uint8_t {
|
||||
welcome,
|
||||
closed,
|
||||
lost, // Transport/TLS failure or deadline: no Close received.
|
||||
result, // IntentResult.
|
||||
outcome, // IntentOutcome (durable or not committed).
|
||||
unknown, // Admitted intention whose outcome died with its connection.
|
||||
exhausted // No grants left; reconnection stops.
|
||||
} kind{};
|
||||
Welcome welcome{};
|
||||
CloseReason reason{};
|
||||
IntentResult result{};
|
||||
IntentOutcome outcome{};
|
||||
std::uint64_t sequence{};
|
||||
};
|
||||
enum class SessionClientError : std::uint8_t { configuration };
|
||||
class SessionClient {
|
||||
public:
|
||||
static constexpr std::size_t max_outstanding = 256;
|
||||
[[nodiscard]] static std::expected<std::unique_ptr<SessionClient>, SessionClientError>
|
||||
create(SessionClientConfig config);
|
||||
SessionClient(const SessionClient&) = delete;
|
||||
SessionClient& operator=(const SessionClient&) = delete;
|
||||
~SessionClient();
|
||||
void poll(std::chrono::steady_clock::time_point now);
|
||||
// Sends when established and a view has arrived; never queued across connections.
|
||||
[[nodiscard]] bool send(const client::Intention& intention);
|
||||
[[nodiscard]] SessionPhase phase() const noexcept;
|
||||
[[nodiscard]] const client::EnvironmentLayer& environment() const noexcept;
|
||||
[[nodiscard]] const client::OrganismLayer& organisms() const noexcept;
|
||||
[[nodiscard]] std::optional<Welcome> welcome() const noexcept;
|
||||
[[nodiscard]] std::optional<ViewHeader> latest_view() const noexcept;
|
||||
[[nodiscard]] std::size_t grants_left() const noexcept;
|
||||
[[nodiscard]] std::vector<ClientEvent> take_events();
|
||||
|
||||
private:
|
||||
struct Impl;
|
||||
explicit SessionClient(std::unique_ptr<Impl> impl);
|
||||
std::unique_ptr<Impl> impl_;
|
||||
};
|
||||
} // namespace evo::network
|
||||
@@ -0,0 +1,76 @@
|
||||
#pragma once
|
||||
#include "evo/network/admission.hpp"
|
||||
#include "evo/network/dev_grant.hpp"
|
||||
#include "evo/network/tcp.hpp"
|
||||
#include "evo/network/tls_channel.hpp"
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <deque>
|
||||
#include <expected>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// M9.7 dev session server (ADR-0083). Host-agnostic and non-blocking: the region
|
||||
// host calls poll() from its own loop, publishes views of its state, lets admitted
|
||||
// intentions ride host inputs via carry(), and reports durable/lost outcomes. It
|
||||
// never touches simulation state and never decides an outcome.
|
||||
namespace evo::network {
|
||||
using RequestId = std::array<std::uint8_t, 16>;
|
||||
struct SessionServerConfig {
|
||||
std::string listen; // Numeric address:port.
|
||||
ServerCredentials credentials;
|
||||
AdmissionConfig admission; // `epoch` is ignored: it follows RegionFacts.
|
||||
std::size_t max_connections{4};
|
||||
std::chrono::milliseconds hello_deadline{5000}; // Accept to Welcome.
|
||||
std::size_t max_output_bytes{std::size_t{1} << 20};
|
||||
std::size_t max_input_bytes_per_second{65536};
|
||||
std::size_t max_pending_intents{64}; // Admitted, not yet carried.
|
||||
std::size_t max_carried_intents{1024}; // Carried, awaiting durable/lost.
|
||||
};
|
||||
// The host's current authority facts, read once per loop iteration.
|
||||
struct RegionFacts {
|
||||
bool ready{};
|
||||
std::uint64_t epoch{}, simulated_tick{}, committed_tick{}, commit_sequence{};
|
||||
};
|
||||
enum class SessionServerError : std::uint8_t { configuration, listen, credentials };
|
||||
struct SessionEvent {
|
||||
enum class Kind : std::uint8_t { opened, closed, refused } kind{};
|
||||
std::uint64_t session{}, subject{};
|
||||
CloseReason reason{};
|
||||
};
|
||||
class SessionServer {
|
||||
public:
|
||||
[[nodiscard]] static std::expected<std::unique_ptr<SessionServer>, SessionServerError>
|
||||
create(SessionServerConfig config, DevGrantKey key);
|
||||
SessionServer(const SessionServer&) = delete;
|
||||
SessionServer& operator=(const SessionServer&) = delete;
|
||||
~SessionServer();
|
||||
[[nodiscard]] std::uint16_t port() const noexcept;
|
||||
// Accepts, handshakes, admits and writes; never blocks. `now_seconds` is wall
|
||||
// time for grant validity; `now` is monotonic time for connection deadlines.
|
||||
void poll(const RegionFacts& facts, std::uint64_t now_seconds,
|
||||
std::chrono::steady_clock::time_point now);
|
||||
// The latest views; each session receives only the newest (older unsent views are
|
||||
// replaced, never queued). New sessions receive the latest as their baseline.
|
||||
void publish(const client::EnvironmentPresentation& environment,
|
||||
const client::OrganismPresentation& organisms, const RegionFacts& facts);
|
||||
// The oldest admitted intention whose intended tick is <= `tick`, to ride the host
|
||||
// input for `tick`. The host input's content stays host-chosen.
|
||||
[[nodiscard]] std::optional<RequestId> carry(std::uint64_t tick);
|
||||
// Host final acknowledgement / drop for a request id; ids not carried here are ignored.
|
||||
void committed(const RequestId& request, std::uint64_t tick, std::uint64_t commit_sequence);
|
||||
void not_committed(const RequestId& request);
|
||||
[[nodiscard]] std::vector<SessionEvent> take_events();
|
||||
[[nodiscard]] std::size_t connections() const noexcept;
|
||||
[[nodiscard]] std::size_t sessions() const noexcept;
|
||||
|
||||
private:
|
||||
struct Impl;
|
||||
explicit SessionServer(std::unique_ptr<Impl> impl);
|
||||
std::unique_ptr<Impl> impl_;
|
||||
};
|
||||
} // namespace evo::network
|
||||
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
#include "evo/network/tls_channel.hpp"
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
// Minimal non-blocking TCP for the M9.7 dev session (ADR-0083), Windows and Linux.
|
||||
// Plaintext bytes only: every session byte travels inside TlsChannel. No socket
|
||||
// handle crosses this header. One owner thread per object.
|
||||
namespace evo::network {
|
||||
class TcpStream final : public ByteTransport {
|
||||
public:
|
||||
// Resolves `host` (blocking, bounded by the system resolver) and starts a
|
||||
// non-blocking connect. Reads/writes return 0 until the connection completes.
|
||||
[[nodiscard]] static std::expected<std::unique_ptr<TcpStream>, TransportError>
|
||||
connect(const std::string& host, std::uint16_t port);
|
||||
TcpStream(const TcpStream&) = delete;
|
||||
TcpStream& operator=(const TcpStream&) = delete;
|
||||
~TcpStream() override;
|
||||
[[nodiscard]] std::expected<std::size_t, TransportError>
|
||||
write(std::span<const std::uint8_t> bytes) override;
|
||||
[[nodiscard]] std::expected<std::size_t, TransportError>
|
||||
read(std::span<std::uint8_t> bytes) override;
|
||||
|
||||
private:
|
||||
friend class TcpListener;
|
||||
TcpStream(std::intptr_t socket, bool connecting) : socket_{socket}, connecting_{connecting} {}
|
||||
[[nodiscard]] std::expected<bool, TransportError> ready();
|
||||
std::intptr_t socket_;
|
||||
bool connecting_;
|
||||
};
|
||||
class TcpListener {
|
||||
public:
|
||||
// Numeric address only ("127.0.0.1:7400", "[::1]:7400", "0.0.0.0:7400"); port 0
|
||||
// picks a free port.
|
||||
[[nodiscard]] static std::expected<std::unique_ptr<TcpListener>, TransportError>
|
||||
listen(std::string_view address);
|
||||
TcpListener(const TcpListener&) = delete;
|
||||
TcpListener& operator=(const TcpListener&) = delete;
|
||||
~TcpListener();
|
||||
// A pending connection, or nullptr when none is waiting.
|
||||
[[nodiscard]] std::expected<std::unique_ptr<TcpStream>, TransportError> accept();
|
||||
[[nodiscard]] std::uint16_t port() const noexcept {
|
||||
return port_;
|
||||
}
|
||||
|
||||
private:
|
||||
TcpListener(std::intptr_t socket, std::uint16_t port) : socket_{socket}, port_{port} {}
|
||||
std::intptr_t socket_;
|
||||
std::uint16_t port_;
|
||||
};
|
||||
} // namespace evo::network
|
||||
@@ -12,7 +12,7 @@ message re-encodes to identical bytes (fuzz-tested).
|
||||
|---|---|---|---|
|
||||
| 0 | magic | u32 | `0x53445645` (bytes `E V D S`) |
|
||||
| 4 | version | u16 | exactly 1, otherwise `unsupported_version` |
|
||||
| 6 | type | u8 | 1..7, otherwise `unknown_type` |
|
||||
| 6 | type | u8 | 1..8, otherwise `unknown_type` |
|
||||
| 7 | flags | u8 | 0, otherwise `reserved_flags` |
|
||||
| 8 | length | u32 | payload bytes, <= 262144 (`oversized`) |
|
||||
| 12 | payload | bytes | exactly `length`; short `truncated`, extra `trailing_bytes` |
|
||||
@@ -30,12 +30,15 @@ Payload decoders also reject truncation, trailing bytes, out-of-range enum bytes
|
||||
| 4 | IntentResult | S->C | sequence u64, status u8 (0..7), server_tick u64 |
|
||||
| 5 | EnvironmentView | S->C | view header, environment presentation |
|
||||
| 6 | OrganismView | S->C | view header, organism presentation |
|
||||
| 7 | Close | both | reason u8 (0..8) |
|
||||
| 7 | Close | both | reason u8 (0..9) |
|
||||
| 8 | IntentOutcome | S->C | sequence u64, finality u8 (0 committed_no_effect, 1 not_committed), tick u64, commit_sequence u64; committed requires commit_sequence != 0, not_committed requires tick = commit_sequence = 0 |
|
||||
|
||||
IntentStatus: 0 admitted, 1 unknown_session, 2 stale_epoch, 3 expired,
|
||||
4 invalid_vector, 5 replayed, 6 outside_window, 7 rate_limited.
|
||||
CloseReason: 0 normal, 1 unsupported_version, 2 malformed, 3 unauthenticated,
|
||||
4 expired, 5 duplicate_session, 6 stale_epoch, 7 rate_limited, 8 server_shutdown.
|
||||
4 expired, 5 duplicate_session, 6 stale_epoch, 7 rate_limited, 8 server_shutdown,
|
||||
9 capacity. Type 8 and reason 9 were added in M9.7 before any v1 peer was
|
||||
deployed (ADR-0083); later changes require a new version.
|
||||
|
||||
View header (40 bytes): session, epoch, server_tick, commit_watermark, baseline (u64).
|
||||
|
||||
|
||||
@@ -5,9 +5,10 @@
|
||||
|
||||
namespace evo::network {
|
||||
namespace {
|
||||
constexpr std::uint8_t last_type = static_cast<std::uint8_t>(MessageType::close);
|
||||
constexpr std::uint8_t last_type = static_cast<std::uint8_t>(MessageType::intent_outcome);
|
||||
constexpr std::uint8_t last_status = static_cast<std::uint8_t>(IntentStatus::rate_limited);
|
||||
constexpr std::uint8_t last_reason = static_cast<std::uint8_t>(CloseReason::server_shutdown);
|
||||
constexpr std::uint8_t last_reason = static_cast<std::uint8_t>(CloseReason::capacity);
|
||||
constexpr std::uint8_t last_finality = static_cast<std::uint8_t>(IntentFinality::not_committed);
|
||||
constexpr std::uint8_t last_presence = static_cast<std::uint8_t>(client::FieldPresence::present);
|
||||
constexpr std::uint8_t last_moisture = static_cast<std::uint8_t>(world::MoistureStatus::available);
|
||||
constexpr std::uint8_t last_state = static_cast<std::uint8_t>(client::OrganismState::dead);
|
||||
@@ -279,6 +280,15 @@ std::vector<std::uint8_t> encode(CloseReason reason) {
|
||||
return framed(MessageType::close, w);
|
||||
}
|
||||
|
||||
std::vector<std::uint8_t> encode(const IntentOutcome& m) {
|
||||
Writer w;
|
||||
w.u64(m.sequence);
|
||||
w.u8(static_cast<std::uint8_t>(m.finality));
|
||||
w.u64(m.tick);
|
||||
w.u64(m.commit_sequence);
|
||||
return framed(MessageType::intent_outcome, w);
|
||||
}
|
||||
|
||||
std::vector<std::uint8_t> encode(const ViewHeader& header,
|
||||
const client::EnvironmentPresentation& view) {
|
||||
Writer w;
|
||||
@@ -431,6 +441,24 @@ std::expected<CloseReason, ProtocolError> decode_close(FrameView frame) {
|
||||
return reason;
|
||||
}
|
||||
|
||||
std::expected<IntentOutcome, ProtocolError> decode_intent_outcome(FrameView frame) {
|
||||
auto r = open(frame, MessageType::intent_outcome);
|
||||
if (!r)
|
||||
return std::unexpected(r.error());
|
||||
IntentOutcome m;
|
||||
m.sequence = r->u64();
|
||||
m.finality = enumerator<IntentFinality>(*r, last_finality);
|
||||
m.tick = r->u64();
|
||||
m.commit_sequence = r->u64();
|
||||
if (auto done = r->finish(); !done)
|
||||
return std::unexpected(done.error());
|
||||
// Committed outcomes name their commit; lost ones carry no tick or commit.
|
||||
if ((m.finality == IntentFinality::committed_no_effect) != (m.commit_sequence != 0) ||
|
||||
(m.finality == IntentFinality::not_committed && m.tick != 0))
|
||||
return std::unexpected(ProtocolError::invalid_value);
|
||||
return m;
|
||||
}
|
||||
|
||||
std::expected<std::pair<ViewHeader, client::EnvironmentPresentation>, ProtocolError>
|
||||
decode_environment_view(FrameView frame) {
|
||||
auto r = open(frame, MessageType::environment_view);
|
||||
|
||||
@@ -0,0 +1,297 @@
|
||||
#include "evo/network/session_client.hpp"
|
||||
#include "evo/network/tcp.hpp"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <utility>
|
||||
|
||||
namespace evo::network {
|
||||
namespace {
|
||||
using Clock = std::chrono::steady_clock;
|
||||
constexpr std::size_t max_read_per_poll = std::size_t{1} << 20;
|
||||
} // namespace
|
||||
|
||||
struct SessionClient::Impl {
|
||||
enum class Step : std::uint8_t { handshake, awaiting_welcome, established };
|
||||
explicit Impl(SessionClientConfig c) : config{std::move(c)}, delay{config.first_retry} {}
|
||||
|
||||
SessionClientConfig config;
|
||||
std::size_t next_grant{};
|
||||
SessionPhase phase{SessionPhase::backoff};
|
||||
Step step{Step::handshake};
|
||||
Clock::time_point retry_at{}, deadline{};
|
||||
std::chrono::milliseconds delay;
|
||||
std::unique_ptr<TcpStream> stream; // Declared before the channel that borrows it.
|
||||
std::unique_ptr<TlsChannel> tls;
|
||||
FrameReader reader;
|
||||
std::vector<std::uint8_t> out;
|
||||
std::optional<Welcome> session;
|
||||
std::optional<ViewHeader> view;
|
||||
std::vector<std::uint64_t> outstanding; // Admitted, awaiting a final outcome.
|
||||
client::EnvironmentLayer environment;
|
||||
client::OrganismLayer organisms;
|
||||
std::vector<ClientEvent> events;
|
||||
|
||||
void disconnect(Clock::time_point now, std::optional<CloseReason> reason) {
|
||||
if (step == Step::established || step == Step::awaiting_welcome || reason)
|
||||
events.push_back({reason ? ClientEvent::Kind::closed : ClientEvent::Kind::lost,
|
||||
{},
|
||||
reason.value_or(CloseReason::normal),
|
||||
{},
|
||||
{},
|
||||
0});
|
||||
for (const auto sequence : outstanding)
|
||||
events.push_back({ClientEvent::Kind::unknown, {}, {}, {}, {}, sequence});
|
||||
outstanding.clear();
|
||||
tls.reset();
|
||||
stream.reset();
|
||||
reader = FrameReader{};
|
||||
out.clear();
|
||||
session.reset();
|
||||
view.reset();
|
||||
// Keep showing the last presentation, explicitly stale, until a new baseline.
|
||||
environment.mark_stale();
|
||||
organisms.mark_stale();
|
||||
phase = SessionPhase::backoff;
|
||||
step = Step::handshake;
|
||||
retry_at = now + delay;
|
||||
delay = std::min(delay * 2, config.max_retry);
|
||||
}
|
||||
void connect(Clock::time_point now) {
|
||||
if (next_grant >= config.grants.size()) {
|
||||
phase = SessionPhase::exhausted;
|
||||
events.push_back({ClientEvent::Kind::exhausted, {}, {}, {}, {}, 0});
|
||||
return;
|
||||
}
|
||||
auto s = TcpStream::connect(config.host, config.port);
|
||||
if (!s) {
|
||||
disconnect(now, std::nullopt);
|
||||
return;
|
||||
}
|
||||
stream = std::move(*s);
|
||||
auto t = TlsChannel::client(config.trust, *stream);
|
||||
if (!t) {
|
||||
disconnect(now, std::nullopt);
|
||||
return;
|
||||
}
|
||||
tls = std::move(*t);
|
||||
phase = SessionPhase::connecting;
|
||||
step = Step::handshake;
|
||||
deadline = now + config.attempt_deadline;
|
||||
}
|
||||
bool frame(const OwnedFrame& f, Clock::time_point now) {
|
||||
const FrameView v{f.type, f.payload};
|
||||
if (step == Step::awaiting_welcome) {
|
||||
if (f.type == MessageType::close) {
|
||||
const auto reason = decode_close(v);
|
||||
// Refused before admission (not ready / full): the server never recorded
|
||||
// the grant, so it stays usable for the next attempt.
|
||||
if (reason &&
|
||||
(*reason == CloseReason::server_shutdown || *reason == CloseReason::capacity))
|
||||
--next_grant;
|
||||
disconnect(now, reason.value_or(CloseReason::malformed));
|
||||
return false;
|
||||
}
|
||||
const auto w = decode_welcome(v);
|
||||
if (!w) {
|
||||
disconnect(now, std::nullopt);
|
||||
return false;
|
||||
}
|
||||
session = *w;
|
||||
step = Step::established;
|
||||
phase = SessionPhase::established;
|
||||
delay = config.first_retry;
|
||||
// A new session is a new authority view: drop everything from before.
|
||||
environment.reset();
|
||||
organisms.reset();
|
||||
events.push_back({ClientEvent::Kind::welcome, *w, {}, {}, {}, 0});
|
||||
return true;
|
||||
}
|
||||
switch (f.type) {
|
||||
case MessageType::environment_view: {
|
||||
auto d = decode_environment_view(v);
|
||||
if (!d || d->first.session != session->session || d->first.epoch != session->epoch ||
|
||||
!environment.accept(std::move(d->second))) {
|
||||
disconnect(now, std::nullopt);
|
||||
return false;
|
||||
}
|
||||
view = d->first;
|
||||
return true;
|
||||
}
|
||||
case MessageType::organism_view: {
|
||||
auto d = decode_organism_view(v);
|
||||
if (!d || d->first.session != session->session || d->first.epoch != session->epoch ||
|
||||
!organisms.accept(std::move(d->second))) {
|
||||
disconnect(now, std::nullopt);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
case MessageType::intent_result: {
|
||||
const auto r = decode_intent_result(v);
|
||||
if (!r) {
|
||||
disconnect(now, std::nullopt);
|
||||
return false;
|
||||
}
|
||||
if (r->status == IntentStatus::admitted) {
|
||||
if (outstanding.size() >= max_outstanding) {
|
||||
events.push_back(
|
||||
{ClientEvent::Kind::unknown, {}, {}, {}, {}, outstanding.front()});
|
||||
outstanding.erase(outstanding.begin());
|
||||
}
|
||||
outstanding.push_back(r->sequence);
|
||||
}
|
||||
events.push_back({ClientEvent::Kind::result, {}, {}, *r, {}, r->sequence});
|
||||
return true;
|
||||
}
|
||||
case MessageType::intent_outcome: {
|
||||
const auto o = decode_intent_outcome(v);
|
||||
if (!o) {
|
||||
disconnect(now, std::nullopt);
|
||||
return false;
|
||||
}
|
||||
std::erase(outstanding, o->sequence);
|
||||
events.push_back({ClientEvent::Kind::outcome, {}, {}, {}, *o, o->sequence});
|
||||
return true;
|
||||
}
|
||||
case MessageType::close: {
|
||||
const auto reason = decode_close(v);
|
||||
disconnect(now, reason.value_or(CloseReason::malformed));
|
||||
return false;
|
||||
}
|
||||
default:
|
||||
disconnect(now, std::nullopt);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
void flush(Clock::time_point now) {
|
||||
std::size_t sent = 0;
|
||||
while (sent < out.size()) {
|
||||
const auto n = tls->send(std::span{out}.subspan(sent));
|
||||
if (!n) {
|
||||
disconnect(now, std::nullopt);
|
||||
return;
|
||||
}
|
||||
if (*n == 0)
|
||||
break;
|
||||
sent += *n;
|
||||
}
|
||||
out.erase(out.begin(), out.begin() + static_cast<std::ptrdiff_t>(sent));
|
||||
}
|
||||
void receive(Clock::time_point now) {
|
||||
std::array<std::uint8_t, 16384> buffer{};
|
||||
std::size_t total = 0;
|
||||
while (tls && total < max_read_per_poll) {
|
||||
const auto n = tls->receive(buffer);
|
||||
if (!n) {
|
||||
disconnect(now, std::nullopt);
|
||||
return;
|
||||
}
|
||||
if (*n == 0)
|
||||
return;
|
||||
total += *n;
|
||||
std::span<const std::uint8_t> rest{buffer.data(), *n};
|
||||
while (tls && !rest.empty()) {
|
||||
const auto taken = reader.feed(rest);
|
||||
if (!taken) {
|
||||
disconnect(now, std::nullopt);
|
||||
return;
|
||||
}
|
||||
rest = rest.subspan(*taken);
|
||||
while (tls) {
|
||||
auto f = reader.take();
|
||||
if (!f || !frame(*f, now))
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
SessionClient::SessionClient(std::unique_ptr<Impl> impl) : impl_{std::move(impl)} {}
|
||||
SessionClient::~SessionClient() = default;
|
||||
|
||||
std::expected<std::unique_ptr<SessionClient>, SessionClientError>
|
||||
SessionClient::create(SessionClientConfig config) {
|
||||
if (config.host.empty() || config.port == 0 || config.grants.empty() ||
|
||||
config.grants.size() > 1024 || config.lead_ticks == 0 || config.lead_ticks > 64 ||
|
||||
config.first_retry.count() <= 0 || config.max_retry < config.first_retry ||
|
||||
config.attempt_deadline.count() <= 0)
|
||||
return std::unexpected(SessionClientError::configuration);
|
||||
return std::unique_ptr<SessionClient>(
|
||||
new SessionClient(std::make_unique<Impl>(std::move(config))));
|
||||
}
|
||||
|
||||
void SessionClient::poll(std::chrono::steady_clock::time_point now) {
|
||||
auto& m = *impl_;
|
||||
if (m.phase == SessionPhase::exhausted)
|
||||
return;
|
||||
if (m.phase == SessionPhase::backoff) {
|
||||
if (now < m.retry_at)
|
||||
return;
|
||||
m.connect(now);
|
||||
if (m.phase != SessionPhase::connecting)
|
||||
return;
|
||||
}
|
||||
if (m.step != Impl::Step::established && now > m.deadline) {
|
||||
m.disconnect(now, std::nullopt);
|
||||
return;
|
||||
}
|
||||
if (m.step == Impl::Step::handshake) {
|
||||
const auto p = m.tls->handshake();
|
||||
if (!p) {
|
||||
m.disconnect(now, std::nullopt);
|
||||
return;
|
||||
}
|
||||
if (*p == TlsProgress::want_io)
|
||||
return;
|
||||
// One single-use grant per session attempt, consumed when it is sent.
|
||||
const auto& grant = m.config.grants.at(m.next_grant++);
|
||||
const auto hello = encode(Hello{0, {grant.begin(), grant.end()}});
|
||||
if (!hello) {
|
||||
m.disconnect(now, std::nullopt);
|
||||
return;
|
||||
}
|
||||
m.out.insert(m.out.end(), hello->begin(), hello->end());
|
||||
m.step = Impl::Step::awaiting_welcome;
|
||||
}
|
||||
m.flush(now);
|
||||
if (m.tls)
|
||||
m.receive(now);
|
||||
if (m.tls)
|
||||
m.flush(now);
|
||||
}
|
||||
|
||||
bool SessionClient::send(const client::Intention& intention) {
|
||||
auto& m = *impl_;
|
||||
if (m.phase != SessionPhase::established || !m.session || !m.view)
|
||||
return false;
|
||||
const auto tick = m.view->server_tick + m.config.lead_ticks;
|
||||
const auto bytes =
|
||||
encode(make_intent(m.session->session, m.session->epoch,
|
||||
{intention.sequence, tick, intention.move_x, intention.move_y}));
|
||||
m.out.insert(m.out.end(), bytes.begin(), bytes.end());
|
||||
return true;
|
||||
}
|
||||
|
||||
SessionPhase SessionClient::phase() const noexcept {
|
||||
return impl_->phase;
|
||||
}
|
||||
const client::EnvironmentLayer& SessionClient::environment() const noexcept {
|
||||
return impl_->environment;
|
||||
}
|
||||
const client::OrganismLayer& SessionClient::organisms() const noexcept {
|
||||
return impl_->organisms;
|
||||
}
|
||||
std::optional<Welcome> SessionClient::welcome() const noexcept {
|
||||
return impl_->session;
|
||||
}
|
||||
std::optional<ViewHeader> SessionClient::latest_view() const noexcept {
|
||||
return impl_->view;
|
||||
}
|
||||
std::size_t SessionClient::grants_left() const noexcept {
|
||||
return impl_->config.grants.size() - impl_->next_grant;
|
||||
}
|
||||
std::vector<ClientEvent> SessionClient::take_events() {
|
||||
return std::exchange(impl_->events, {});
|
||||
}
|
||||
} // namespace evo::network
|
||||
@@ -0,0 +1,415 @@
|
||||
#include "evo/network/session_server.hpp"
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
|
||||
namespace evo::network {
|
||||
namespace {
|
||||
using Clock = std::chrono::steady_clock;
|
||||
constexpr std::size_t max_accepts_per_poll = 4;
|
||||
constexpr std::size_t max_read_per_poll = 65536;
|
||||
CloseReason close_reason(AdmissionError e) {
|
||||
switch (e) {
|
||||
case AdmissionError::duplicate_subject:
|
||||
return CloseReason::duplicate_session;
|
||||
case AdmissionError::expired:
|
||||
return CloseReason::expired;
|
||||
case AdmissionError::sessions_full:
|
||||
case AdmissionError::grants_full:
|
||||
return CloseReason::capacity;
|
||||
default:
|
||||
return CloseReason::unauthenticated;
|
||||
}
|
||||
}
|
||||
CloseReason close_reason(ProtocolError e) {
|
||||
return e == ProtocolError::unsupported_version ? CloseReason::unsupported_version
|
||||
: CloseReason::malformed;
|
||||
}
|
||||
void append(std::vector<std::uint8_t>& out, const std::vector<std::uint8_t>& bytes) {
|
||||
out.insert(out.end(), bytes.begin(), bytes.end());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
struct SessionServer::Impl {
|
||||
enum class State : std::uint8_t { handshake, hello, established, closing };
|
||||
struct Connection {
|
||||
std::uint64_t id{};
|
||||
std::unique_ptr<TcpStream> stream; // Declared first: outlives the channel.
|
||||
std::unique_ptr<TlsChannel> tls;
|
||||
State state{State::handshake};
|
||||
Clock::time_point deadline;
|
||||
FrameReader reader;
|
||||
std::vector<std::uint8_t> out, view;
|
||||
std::uint64_t session{}, subject{}, epoch{}, expires_at{};
|
||||
std::size_t window_bytes{};
|
||||
Clock::time_point window_start;
|
||||
bool dead{};
|
||||
};
|
||||
struct Pending {
|
||||
RequestId request{};
|
||||
std::uint64_t intended_tick{}, connection{}, sequence{};
|
||||
};
|
||||
struct Carried {
|
||||
RequestId request{};
|
||||
std::uint64_t connection{}, sequence{};
|
||||
};
|
||||
|
||||
Impl(SessionServerConfig c, DevGrantKey k, std::unique_ptr<TcpListener> l)
|
||||
: config{std::move(c)}, key{std::move(k)}, listener{std::move(l)} {}
|
||||
|
||||
SessionServerConfig config;
|
||||
DevGrantKey key;
|
||||
std::unique_ptr<TcpListener> listener;
|
||||
std::optional<SessionAdmission> admission;
|
||||
std::vector<Connection> connections;
|
||||
std::deque<Pending> pending;
|
||||
std::vector<Carried> carried;
|
||||
std::vector<SessionEvent> events;
|
||||
std::optional<client::EnvironmentPresentation> environment;
|
||||
std::optional<client::OrganismPresentation> organisms;
|
||||
RegionFacts published{};
|
||||
std::uint64_t baseline{}, next_connection{1};
|
||||
Clock::time_point polled_at{}; // Time of the current poll, for Close deadlines.
|
||||
|
||||
Connection* find(std::uint64_t id) {
|
||||
const auto it = std::ranges::find_if(connections, [&](const Connection& c) {
|
||||
return c.id == id && !c.dead && c.state == State::established;
|
||||
});
|
||||
return it == connections.end() ? nullptr : &*it;
|
||||
}
|
||||
void close(Connection& c, CloseReason reason) {
|
||||
if (c.state == State::closing || c.dead)
|
||||
return;
|
||||
if (c.state == State::hello || c.state == State::established)
|
||||
append(c.out, encode(reason));
|
||||
else
|
||||
c.dead = true; // No channel yet to say anything over.
|
||||
events.push_back({c.state == State::established ? SessionEvent::Kind::closed
|
||||
: SessionEvent::Kind::refused,
|
||||
c.session, c.subject, reason});
|
||||
c.state = State::closing;
|
||||
c.deadline = polled_at + std::chrono::seconds{1}; // Bounded flush of the Close frame.
|
||||
c.view.clear();
|
||||
}
|
||||
std::vector<std::uint8_t> views_for(const Connection& c) const {
|
||||
std::vector<std::uint8_t> bytes;
|
||||
if (!environment || !organisms)
|
||||
return bytes;
|
||||
const ViewHeader header{c.session, c.epoch, published.simulated_tick,
|
||||
published.committed_tick, baseline};
|
||||
append(bytes, encode(header, *environment));
|
||||
append(bytes, encode(header, *organisms));
|
||||
return bytes;
|
||||
}
|
||||
void hello(Connection& c, const OwnedFrame& f, const RegionFacts& facts,
|
||||
std::uint64_t now_seconds) {
|
||||
if (f.type != MessageType::hello) {
|
||||
close(c, CloseReason::malformed);
|
||||
return;
|
||||
}
|
||||
const auto message = decode_hello({f.type, f.payload});
|
||||
if (!message) {
|
||||
close(c, close_reason(message.error()));
|
||||
return;
|
||||
}
|
||||
if (!admission || !facts.ready || facts.epoch != admission->epoch()) {
|
||||
close(c, CloseReason::server_shutdown); // No current authority: retry later.
|
||||
return;
|
||||
}
|
||||
const auto grant = key.verify(message->grant);
|
||||
if (!grant) {
|
||||
close(c, CloseReason::unauthenticated);
|
||||
return;
|
||||
}
|
||||
const auto welcome =
|
||||
admission->open(*grant, now_seconds, facts.simulated_tick, facts.committed_tick);
|
||||
if (!welcome) {
|
||||
c.subject = grant->claims().subject;
|
||||
close(c, close_reason(welcome.error()));
|
||||
return;
|
||||
}
|
||||
c.session = welcome->session;
|
||||
c.subject = grant->claims().subject;
|
||||
c.epoch = welcome->epoch;
|
||||
c.expires_at = welcome->grant_expires_at;
|
||||
c.state = State::established;
|
||||
append(c.out, encode(*welcome));
|
||||
c.view = views_for(c); // Fresh baseline for every new session.
|
||||
events.push_back({SessionEvent::Kind::opened, c.session, c.subject, {}});
|
||||
}
|
||||
void intent(Connection& c, const OwnedFrame& f, const RegionFacts& facts,
|
||||
std::uint64_t now_seconds) {
|
||||
const auto m = decode_intent({f.type, f.payload});
|
||||
if (!m) {
|
||||
close(c, close_reason(m.error()));
|
||||
return;
|
||||
}
|
||||
IntentResult result{m->sequence, IntentStatus::unknown_session, facts.simulated_tick};
|
||||
// A connection may only speak for its own session.
|
||||
if (m->session == c.session && admission) {
|
||||
if (pending.size() >= config.max_pending_intents)
|
||||
result.status = IntentStatus::rate_limited; // Host backlog, not admission.
|
||||
else
|
||||
result = admission->admit(*m, facts.simulated_tick, now_seconds);
|
||||
}
|
||||
if (result.status == IntentStatus::admitted)
|
||||
pending.push_back({request_id(c, m->sequence), m->tick, c.id, m->sequence});
|
||||
append(c.out, encode(result));
|
||||
}
|
||||
RequestId request_id(const Connection& c, std::uint64_t sequence) const {
|
||||
RequestId id{'I', 'N', 'T', '1'};
|
||||
const std::array<std::uint64_t, 3> parts{c.epoch, c.session, sequence};
|
||||
for (std::size_t p = 0; p < parts.size(); ++p)
|
||||
for (unsigned i = 0; i < 4; ++i)
|
||||
id.at(4 + 4 * p + i) = static_cast<std::uint8_t>(parts.at(p) >> (8U * (3U - i)));
|
||||
return id;
|
||||
}
|
||||
void receive(Connection& c, const RegionFacts& facts, std::uint64_t now_seconds,
|
||||
Clock::time_point now) {
|
||||
std::array<std::uint8_t, 16384> buffer{};
|
||||
std::size_t total = 0;
|
||||
while (!c.dead && c.state != State::closing && total < max_read_per_poll) {
|
||||
const auto n = c.tls->receive(buffer);
|
||||
if (!n) {
|
||||
c.dead = true;
|
||||
return;
|
||||
}
|
||||
if (*n == 0)
|
||||
return;
|
||||
total += *n;
|
||||
if (now - c.window_start >= std::chrono::seconds{1}) {
|
||||
c.window_start = now;
|
||||
c.window_bytes = 0;
|
||||
}
|
||||
c.window_bytes += *n;
|
||||
if (c.window_bytes > config.max_input_bytes_per_second) {
|
||||
close(c, CloseReason::rate_limited);
|
||||
return;
|
||||
}
|
||||
std::span<const std::uint8_t> rest{buffer.data(), *n};
|
||||
while (!rest.empty() && c.state != State::closing) {
|
||||
const auto taken = c.reader.feed(rest);
|
||||
if (!taken) {
|
||||
close(c, close_reason(taken.error()));
|
||||
return;
|
||||
}
|
||||
rest = rest.subspan(*taken);
|
||||
while (auto f = c.reader.take()) {
|
||||
if (c.state == State::hello)
|
||||
hello(c, *f, facts, now_seconds);
|
||||
else if (f->type == MessageType::intent)
|
||||
intent(c, *f, facts, now_seconds);
|
||||
else if (f->type == MessageType::close) {
|
||||
c.state = State::closing; // Peer is leaving; nothing to say.
|
||||
events.push_back({SessionEvent::Kind::closed, c.session, c.subject,
|
||||
CloseReason::normal});
|
||||
} else
|
||||
close(c, CloseReason::malformed);
|
||||
if (c.state == State::closing)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void transmit(Connection& c) {
|
||||
if (c.out.empty() && !c.view.empty() && c.state == State::established)
|
||||
c.out.swap(c.view); // Only the newest view waits; it is never queued twice.
|
||||
std::size_t sent = 0;
|
||||
while (sent < c.out.size()) {
|
||||
const auto n = c.tls->send(std::span{c.out}.subspan(sent));
|
||||
if (!n) {
|
||||
c.dead = true;
|
||||
return;
|
||||
}
|
||||
if (*n == 0)
|
||||
break;
|
||||
sent += *n;
|
||||
}
|
||||
c.out.erase(c.out.begin(), c.out.begin() + static_cast<std::ptrdiff_t>(sent));
|
||||
if (c.out.size() + c.view.size() > config.max_output_bytes)
|
||||
c.dead = true; // Slow consumer: drop rather than buffer without bound.
|
||||
if (c.state == State::closing && c.out.empty()) {
|
||||
// Best-effort close_notify: the connection is dropped either way.
|
||||
static_cast<void>(c.tls->close().has_value());
|
||||
c.dead = true;
|
||||
}
|
||||
}
|
||||
void step(Connection& c, const RegionFacts& facts, std::uint64_t now_seconds,
|
||||
Clock::time_point now) {
|
||||
if (c.state == State::handshake) {
|
||||
const auto p = c.tls->handshake();
|
||||
if (!p) {
|
||||
c.dead = true;
|
||||
return;
|
||||
}
|
||||
if (*p == TlsProgress::done)
|
||||
c.state = State::hello;
|
||||
}
|
||||
if (c.state == State::closing && now > c.deadline) {
|
||||
c.dead = true; // Peer would not take the Close frame.
|
||||
return;
|
||||
}
|
||||
if ((c.state == State::handshake || c.state == State::hello) && now > c.deadline)
|
||||
close(c, CloseReason::unauthenticated);
|
||||
if (c.state == State::established && now_seconds >= c.expires_at)
|
||||
close(c, CloseReason::expired);
|
||||
if (c.state == State::established && admission && c.epoch != admission->epoch())
|
||||
close(c, CloseReason::stale_epoch);
|
||||
if (c.state == State::hello || c.state == State::established)
|
||||
receive(c, facts, now_seconds, now);
|
||||
if (!c.dead && c.state != State::handshake)
|
||||
transmit(c);
|
||||
}
|
||||
void bury() {
|
||||
for (auto& c : connections) {
|
||||
if (!c.dead)
|
||||
continue;
|
||||
if (c.session != 0 && admission)
|
||||
// The session is ours; only an already-closed session could fail.
|
||||
static_cast<void>(admission->close(c.session).has_value());
|
||||
std::erase_if(pending, [&](const Pending& p) { return p.connection == c.id; });
|
||||
}
|
||||
std::erase_if(connections, [](const Connection& c) { return c.dead; });
|
||||
}
|
||||
};
|
||||
|
||||
SessionServer::SessionServer(std::unique_ptr<Impl> impl) : impl_{std::move(impl)} {}
|
||||
SessionServer::~SessionServer() = default;
|
||||
|
||||
std::expected<std::unique_ptr<SessionServer>, SessionServerError>
|
||||
SessionServer::create(SessionServerConfig config, DevGrantKey key) {
|
||||
if (config.max_connections == 0 || config.max_connections > 64 ||
|
||||
config.max_pending_intents == 0 || config.max_carried_intents == 0 ||
|
||||
config.max_output_bytes < 2 * (frame_header_bytes + max_frame_payload) ||
|
||||
config.max_input_bytes_per_second < frame_header_bytes + max_grant_bytes + 6)
|
||||
return std::unexpected(SessionServerError::configuration);
|
||||
// Validate the admission limits now, with a placeholder epoch.
|
||||
auto probe = config.admission;
|
||||
probe.epoch = 1;
|
||||
if (!SessionAdmission::create(probe))
|
||||
return std::unexpected(SessionServerError::configuration);
|
||||
// Credentials must parse before the port opens.
|
||||
struct Null final : ByteTransport {
|
||||
std::expected<std::size_t, TransportError> write(std::span<const std::uint8_t>) override {
|
||||
return 0;
|
||||
}
|
||||
std::expected<std::size_t, TransportError> read(std::span<std::uint8_t>) override {
|
||||
return 0;
|
||||
}
|
||||
} null;
|
||||
if (!TlsChannel::server(config.credentials, null))
|
||||
return std::unexpected(SessionServerError::credentials);
|
||||
auto listener = TcpListener::listen(config.listen);
|
||||
if (!listener)
|
||||
return std::unexpected(SessionServerError::listen);
|
||||
return std::unique_ptr<SessionServer>(new SessionServer(
|
||||
std::make_unique<Impl>(std::move(config), std::move(key), std::move(*listener))));
|
||||
}
|
||||
|
||||
std::uint16_t SessionServer::port() const noexcept {
|
||||
return impl_->listener->port();
|
||||
}
|
||||
|
||||
void SessionServer::poll(const RegionFacts& facts, std::uint64_t now_seconds,
|
||||
std::chrono::steady_clock::time_point now) {
|
||||
auto& m = *impl_;
|
||||
m.polled_at = now;
|
||||
if (facts.ready && facts.epoch != 0) {
|
||||
if (!m.admission) {
|
||||
auto config = m.config.admission;
|
||||
config.epoch = facts.epoch;
|
||||
if (auto created = SessionAdmission::create(config))
|
||||
m.admission.emplace(std::move(*created));
|
||||
} else if (facts.epoch > m.admission->epoch()) {
|
||||
// Strictly newer (checked above), so the advance cannot be refused.
|
||||
static_cast<void>(m.admission->advance_epoch(facts.epoch).has_value());
|
||||
// Intentions of the old epoch never ride new-epoch inputs.
|
||||
m.pending.clear();
|
||||
}
|
||||
}
|
||||
for (std::size_t i = 0; i < max_accepts_per_poll; ++i) {
|
||||
auto accepted = m.listener->accept();
|
||||
if (!accepted || !*accepted)
|
||||
break;
|
||||
if (m.connections.size() >= m.config.max_connections)
|
||||
continue; // Over capacity: the stream closes as it goes out of scope.
|
||||
Impl::Connection c;
|
||||
c.id = m.next_connection++;
|
||||
c.stream = std::move(*accepted);
|
||||
auto tls = TlsChannel::server(m.config.credentials, *c.stream);
|
||||
if (!tls)
|
||||
continue;
|
||||
c.tls = std::move(*tls);
|
||||
c.deadline = now + m.config.hello_deadline;
|
||||
c.window_start = now;
|
||||
m.connections.push_back(std::move(c));
|
||||
}
|
||||
for (auto& c : m.connections)
|
||||
if (!c.dead)
|
||||
m.step(c, facts, now_seconds, now);
|
||||
m.bury();
|
||||
}
|
||||
|
||||
void SessionServer::publish(const client::EnvironmentPresentation& environment,
|
||||
const client::OrganismPresentation& organisms,
|
||||
const RegionFacts& facts) {
|
||||
auto& m = *impl_;
|
||||
m.environment = environment;
|
||||
m.organisms = organisms;
|
||||
m.published = facts;
|
||||
++m.baseline;
|
||||
for (auto& c : m.connections)
|
||||
if (!c.dead && c.state == Impl::State::established)
|
||||
c.view = m.views_for(c);
|
||||
}
|
||||
|
||||
std::optional<RequestId> SessionServer::carry(std::uint64_t tick) {
|
||||
auto& m = *impl_;
|
||||
if (m.carried.size() >= m.config.max_carried_intents)
|
||||
return std::nullopt;
|
||||
const auto it = std::ranges::find_if(
|
||||
m.pending, [&](const Impl::Pending& p) { return p.intended_tick <= tick; });
|
||||
if (it == m.pending.end())
|
||||
return std::nullopt;
|
||||
const Impl::Carried carried{it->request, it->connection, it->sequence};
|
||||
m.pending.erase(it);
|
||||
m.carried.push_back(carried);
|
||||
return carried.request;
|
||||
}
|
||||
|
||||
void SessionServer::committed(const RequestId& request, std::uint64_t tick,
|
||||
std::uint64_t commit_sequence) {
|
||||
auto& m = *impl_;
|
||||
const auto it = std::ranges::find_if(
|
||||
m.carried, [&](const Impl::Carried& c) { return c.request == request; });
|
||||
if (it == m.carried.end())
|
||||
return;
|
||||
if (auto* c = m.find(it->connection))
|
||||
append(c->out, encode(IntentOutcome{it->sequence, IntentFinality::committed_no_effect, tick,
|
||||
commit_sequence}));
|
||||
m.carried.erase(it);
|
||||
}
|
||||
|
||||
void SessionServer::not_committed(const RequestId& request) {
|
||||
auto& m = *impl_;
|
||||
const auto it = std::ranges::find_if(
|
||||
m.carried, [&](const Impl::Carried& c) { return c.request == request; });
|
||||
if (it == m.carried.end())
|
||||
return;
|
||||
if (auto* c = m.find(it->connection))
|
||||
append(c->out, encode(IntentOutcome{it->sequence, IntentFinality::not_committed, 0, 0}));
|
||||
m.carried.erase(it);
|
||||
}
|
||||
|
||||
std::vector<SessionEvent> SessionServer::take_events() {
|
||||
return std::exchange(impl_->events, {});
|
||||
}
|
||||
|
||||
std::size_t SessionServer::connections() const noexcept {
|
||||
return impl_->connections.size();
|
||||
}
|
||||
|
||||
std::size_t SessionServer::sessions() const noexcept {
|
||||
const auto& admission = impl_->admission;
|
||||
return admission ? admission->sessions() : 0;
|
||||
}
|
||||
} // namespace evo::network
|
||||
@@ -0,0 +1,267 @@
|
||||
#include "evo/network/tcp.hpp"
|
||||
#include <charconv>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#else
|
||||
#include <arpa/inet.h>
|
||||
#include <cerrno>
|
||||
#include <fcntl.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <poll.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace evo::network {
|
||||
namespace {
|
||||
#ifdef _WIN32
|
||||
using Native = SOCKET;
|
||||
constexpr Native invalid = INVALID_SOCKET;
|
||||
bool startup() {
|
||||
static std::once_flag once;
|
||||
static bool ok = false;
|
||||
std::call_once(once, [] {
|
||||
WSADATA data{};
|
||||
ok = WSAStartup(MAKEWORD(2, 2), &data) == 0; // Process lifetime; never cleaned up.
|
||||
});
|
||||
return ok;
|
||||
}
|
||||
void close_native(Native s) {
|
||||
closesocket(s);
|
||||
}
|
||||
bool would_block() {
|
||||
const int e = WSAGetLastError();
|
||||
return e == WSAEWOULDBLOCK || e == WSAEINPROGRESS;
|
||||
}
|
||||
bool non_blocking(Native s) {
|
||||
u_long on = 1;
|
||||
return ioctlsocket(s, FIONBIO, &on) == 0;
|
||||
}
|
||||
int poll_one(Native s, short events, short& revents) {
|
||||
WSAPOLLFD fd{s, events, 0};
|
||||
const int r = WSAPoll(&fd, 1, 0);
|
||||
revents = fd.revents;
|
||||
return r;
|
||||
}
|
||||
constexpr int send_flags = 0;
|
||||
using Length = int;
|
||||
#else
|
||||
using Native = int;
|
||||
constexpr Native invalid = -1;
|
||||
bool startup() {
|
||||
return true;
|
||||
}
|
||||
void close_native(Native s) {
|
||||
::close(s);
|
||||
}
|
||||
bool would_block() {
|
||||
return errno == EAGAIN || errno == EWOULDBLOCK || errno == EINPROGRESS || errno == EINTR;
|
||||
}
|
||||
bool non_blocking(Native s) {
|
||||
const int flags = ::fcntl(s, F_GETFL, 0);
|
||||
return flags >= 0 && ::fcntl(s, F_SETFL, flags | O_NONBLOCK) == 0;
|
||||
}
|
||||
int poll_one(Native s, short events, short& revents) {
|
||||
pollfd fd{s, events, 0};
|
||||
const int r = ::poll(&fd, 1, 0);
|
||||
revents = fd.revents;
|
||||
return r;
|
||||
}
|
||||
constexpr int send_flags = MSG_NOSIGNAL; // A peer reset must not raise SIGPIPE.
|
||||
using Length = std::size_t;
|
||||
#endif
|
||||
Native native(std::intptr_t s) {
|
||||
return static_cast<Native>(s);
|
||||
}
|
||||
std::intptr_t stored(Native s) {
|
||||
return static_cast<std::intptr_t>(s);
|
||||
}
|
||||
void no_delay(Native s) {
|
||||
int on = 1;
|
||||
(void)::setsockopt(s, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<const char*>(&on), sizeof on);
|
||||
}
|
||||
Length clamp(std::size_t size) {
|
||||
constexpr std::size_t limit = 1U << 30; // Bounded single call.
|
||||
return static_cast<Length>(size < limit ? size : limit);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<std::unique_ptr<TcpStream>, TransportError>
|
||||
TcpStream::connect(const std::string& host, std::uint16_t port) {
|
||||
if (!startup() || host.empty() || port == 0)
|
||||
return std::unexpected(TransportError::failed);
|
||||
addrinfo hints{};
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_protocol = IPPROTO_TCP;
|
||||
addrinfo* found = nullptr;
|
||||
if (::getaddrinfo(host.c_str(), std::to_string(port).c_str(), &hints, &found) != 0)
|
||||
return std::unexpected(TransportError::failed);
|
||||
std::unique_ptr<addrinfo, decltype(&::freeaddrinfo)> list{found, ::freeaddrinfo};
|
||||
for (const auto* a = list.get(); a != nullptr; a = a->ai_next) {
|
||||
const Native s = ::socket(a->ai_family, a->ai_socktype, a->ai_protocol);
|
||||
if (s == invalid)
|
||||
continue;
|
||||
if (!non_blocking(s)) {
|
||||
close_native(s);
|
||||
continue;
|
||||
}
|
||||
no_delay(s);
|
||||
const int r = ::connect(s, a->ai_addr, static_cast<socklen_t>(a->ai_addrlen));
|
||||
if (r == 0 || would_block())
|
||||
return std::unique_ptr<TcpStream>(new TcpStream(stored(s), r != 0));
|
||||
close_native(s);
|
||||
}
|
||||
return std::unexpected(TransportError::failed);
|
||||
}
|
||||
|
||||
TcpStream::~TcpStream() {
|
||||
close_native(native(socket_));
|
||||
}
|
||||
|
||||
std::expected<bool, TransportError> TcpStream::ready() {
|
||||
if (!connecting_)
|
||||
return true;
|
||||
short revents = 0;
|
||||
const int r = poll_one(native(socket_), POLLOUT, revents);
|
||||
if (r < 0)
|
||||
return std::unexpected(TransportError::failed);
|
||||
if (r == 0)
|
||||
return false;
|
||||
int error = 0;
|
||||
auto length = static_cast<socklen_t>(sizeof error);
|
||||
if ((revents & (POLLERR | POLLHUP | POLLNVAL)) != 0 ||
|
||||
::getsockopt(native(socket_), SOL_SOCKET, SO_ERROR, reinterpret_cast<char*>(&error),
|
||||
&length) != 0 ||
|
||||
error != 0)
|
||||
return std::unexpected(TransportError::failed);
|
||||
connecting_ = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
std::expected<std::size_t, TransportError> TcpStream::write(std::span<const std::uint8_t> bytes) {
|
||||
const auto open = ready();
|
||||
if (!open)
|
||||
return std::unexpected(open.error());
|
||||
if (!*open || bytes.empty())
|
||||
return 0;
|
||||
const auto n = ::send(native(socket_), reinterpret_cast<const char*>(bytes.data()),
|
||||
clamp(bytes.size()), send_flags);
|
||||
if (n > 0)
|
||||
return static_cast<std::size_t>(n);
|
||||
if (n < 0 && would_block())
|
||||
return 0;
|
||||
return std::unexpected(TransportError::failed);
|
||||
}
|
||||
|
||||
std::expected<std::size_t, TransportError> TcpStream::read(std::span<std::uint8_t> bytes) {
|
||||
const auto open = ready();
|
||||
if (!open)
|
||||
return std::unexpected(open.error());
|
||||
if (!*open || bytes.empty())
|
||||
return 0;
|
||||
const auto n =
|
||||
::recv(native(socket_), reinterpret_cast<char*>(bytes.data()), clamp(bytes.size()), 0);
|
||||
if (n > 0)
|
||||
return static_cast<std::size_t>(n);
|
||||
if (n == 0)
|
||||
return std::unexpected(TransportError::closed);
|
||||
if (would_block())
|
||||
return 0;
|
||||
return std::unexpected(TransportError::failed);
|
||||
}
|
||||
|
||||
std::expected<std::unique_ptr<TcpListener>, TransportError>
|
||||
TcpListener::listen(std::string_view address) {
|
||||
if (!startup())
|
||||
return std::unexpected(TransportError::failed);
|
||||
const auto colon = address.rfind(':');
|
||||
if (colon == std::string_view::npos || colon + 1 >= address.size())
|
||||
return std::unexpected(TransportError::failed);
|
||||
unsigned port{};
|
||||
const auto digits = address.substr(colon + 1);
|
||||
const auto [end, error] = std::from_chars(digits.data(), digits.data() + digits.size(), port);
|
||||
if (error != std::errc{} || end != digits.data() + digits.size() || port > 65535)
|
||||
return std::unexpected(TransportError::failed);
|
||||
auto host = std::string(address.substr(0, colon));
|
||||
sockaddr_storage storage{};
|
||||
socklen_t length{};
|
||||
if (host.size() >= 2 && host.front() == '[' && host.back() == ']') {
|
||||
host = host.substr(1, host.size() - 2);
|
||||
sockaddr_in6 v6{};
|
||||
if (::inet_pton(AF_INET6, host.c_str(), &v6.sin6_addr) != 1)
|
||||
return std::unexpected(TransportError::failed);
|
||||
v6.sin6_family = AF_INET6;
|
||||
v6.sin6_port = htons(static_cast<std::uint16_t>(port));
|
||||
std::memcpy(&storage, &v6, sizeof v6);
|
||||
length = sizeof v6;
|
||||
} else {
|
||||
sockaddr_in v4{};
|
||||
if (::inet_pton(AF_INET, host.c_str(), &v4.sin_addr) != 1)
|
||||
return std::unexpected(TransportError::failed);
|
||||
v4.sin_family = AF_INET;
|
||||
v4.sin_port = htons(static_cast<std::uint16_t>(port));
|
||||
std::memcpy(&storage, &v4, sizeof v4);
|
||||
length = sizeof v4;
|
||||
}
|
||||
const Native s = ::socket(storage.ss_family, SOCK_STREAM, IPPROTO_TCP);
|
||||
if (s == invalid)
|
||||
return std::unexpected(TransportError::failed);
|
||||
#ifndef _WIN32
|
||||
int reuse = 1; // Fast restart after a crash; Windows semantics differ, so not there.
|
||||
(void)::setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof reuse);
|
||||
#endif
|
||||
sockaddr_storage bound{};
|
||||
auto bound_length = static_cast<socklen_t>(sizeof bound);
|
||||
if (!non_blocking(s) || ::bind(s, reinterpret_cast<const sockaddr*>(&storage), length) != 0 ||
|
||||
::listen(s, 16) != 0 ||
|
||||
::getsockname(s, reinterpret_cast<sockaddr*>(&bound), &bound_length) != 0) {
|
||||
close_native(s);
|
||||
return std::unexpected(TransportError::failed);
|
||||
}
|
||||
std::uint16_t actual = 0;
|
||||
if (bound.ss_family == AF_INET6) {
|
||||
sockaddr_in6 v6{};
|
||||
std::memcpy(&v6, &bound, sizeof v6);
|
||||
actual = ntohs(v6.sin6_port);
|
||||
} else {
|
||||
sockaddr_in v4{};
|
||||
std::memcpy(&v4, &bound, sizeof v4);
|
||||
actual = ntohs(v4.sin_port);
|
||||
}
|
||||
return std::unique_ptr<TcpListener>(new TcpListener(stored(s), actual));
|
||||
}
|
||||
|
||||
TcpListener::~TcpListener() {
|
||||
close_native(native(socket_));
|
||||
}
|
||||
|
||||
std::expected<std::unique_ptr<TcpStream>, TransportError> TcpListener::accept() {
|
||||
const Native s = ::accept(native(socket_), nullptr, nullptr);
|
||||
if (s == invalid) {
|
||||
if (would_block())
|
||||
return nullptr;
|
||||
return std::unexpected(TransportError::failed);
|
||||
}
|
||||
if (!non_blocking(s)) {
|
||||
close_native(s);
|
||||
return std::unexpected(TransportError::failed);
|
||||
}
|
||||
no_delay(s);
|
||||
return std::unique_ptr<TcpStream>(new TcpStream(stored(s), false));
|
||||
}
|
||||
} // namespace evo::network
|
||||
@@ -9,6 +9,7 @@
|
||||
// The owned user config (third_party/mbedtls/evo_mbedtls_config.h) must be in effect.
|
||||
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) || defined(MBEDTLS_SSL_PROTO_DTLS) || \
|
||||
defined(MBEDTLS_SSL_SESSION_TICKETS) || defined(MBEDTLS_SSL_RENEGOTIATION) || \
|
||||
defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) || \
|
||||
defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED) || \
|
||||
defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED) || \
|
||||
!defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
#include "evo/network/dev_credentials.hpp"
|
||||
#include <array>
|
||||
#include <cctype>
|
||||
#include <ctime>
|
||||
#include <mbedtls/platform_util.h>
|
||||
#include <mbedtls/x509_crt.h>
|
||||
#include <psa/crypto.h>
|
||||
#include <span>
|
||||
|
||||
namespace evo::network {
|
||||
namespace {
|
||||
constexpr std::string_view authority_name = "CN=AEvolution dev session CA (dev only)";
|
||||
// Owns one PSA-generated ECDSA P-256 key as a PK context; zeroized on destruction.
|
||||
class Key {
|
||||
public:
|
||||
Key() {
|
||||
mbedtls_pk_init(&pk_);
|
||||
}
|
||||
~Key() {
|
||||
mbedtls_pk_free(&pk_);
|
||||
}
|
||||
Key(const Key&) = delete;
|
||||
Key& operator=(const Key&) = delete;
|
||||
bool generate() {
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
|
||||
psa_set_key_bits(&attributes, 256);
|
||||
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_EXPORT);
|
||||
psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_SHA_256));
|
||||
mbedtls_svc_key_id_t id = PSA_KEY_ID_NULL;
|
||||
const bool ok = psa_generate_key(&attributes, &id) == PSA_SUCCESS &&
|
||||
mbedtls_pk_copy_from_psa(id, &pk_) == 0;
|
||||
(void)psa_destroy_key(id);
|
||||
return ok;
|
||||
}
|
||||
mbedtls_pk_context* get() {
|
||||
return &pk_;
|
||||
}
|
||||
|
||||
private:
|
||||
mbedtls_pk_context pk_;
|
||||
};
|
||||
std::string timestamp(std::chrono::system_clock::time_point t) {
|
||||
const std::time_t seconds = std::chrono::system_clock::to_time_t(t);
|
||||
std::tm utc{};
|
||||
#ifdef _WIN32
|
||||
if (gmtime_s(&utc, &seconds) != 0)
|
||||
return {};
|
||||
#else
|
||||
if (gmtime_r(&seconds, &utc) == nullptr)
|
||||
return {};
|
||||
#endif
|
||||
char text[16]{};
|
||||
if (std::strftime(text, sizeof text, "%Y%m%d%H%M%S", &utc) != 14)
|
||||
return {};
|
||||
return text;
|
||||
}
|
||||
std::string certificate(Key& subject, Key& issuer, const std::string& subject_name, bool authority,
|
||||
unsigned char serial, const std::string& from, const std::string& to) {
|
||||
mbedtls_x509write_cert crt;
|
||||
mbedtls_x509write_crt_init(&crt);
|
||||
mbedtls_x509write_crt_set_version(&crt, MBEDTLS_X509_CRT_VERSION_3);
|
||||
mbedtls_x509write_crt_set_md_alg(&crt, MBEDTLS_MD_SHA256);
|
||||
mbedtls_x509write_crt_set_subject_key(&crt, subject.get());
|
||||
mbedtls_x509write_crt_set_issuer_key(&crt, issuer.get());
|
||||
std::array<unsigned char, 4096> buffer{};
|
||||
const std::string issuer_name{authority_name};
|
||||
const bool ok =
|
||||
mbedtls_x509write_crt_set_serial_raw(&crt, &serial, 1) == 0 &&
|
||||
mbedtls_x509write_crt_set_validity(&crt, from.c_str(), to.c_str()) == 0 &&
|
||||
mbedtls_x509write_crt_set_subject_name(&crt, subject_name.c_str()) == 0 &&
|
||||
mbedtls_x509write_crt_set_issuer_name(&crt, issuer_name.c_str()) == 0 &&
|
||||
mbedtls_x509write_crt_set_basic_constraints(&crt, authority ? 1 : 0, authority ? 0 : -1) ==
|
||||
0 &&
|
||||
mbedtls_x509write_crt_set_key_usage(&crt, authority
|
||||
? MBEDTLS_X509_KU_KEY_CERT_SIGN
|
||||
: MBEDTLS_X509_KU_DIGITAL_SIGNATURE) == 0 &&
|
||||
mbedtls_x509write_crt_pem(&crt, buffer.data(), buffer.size()) == 0;
|
||||
mbedtls_x509write_crt_free(&crt);
|
||||
return ok ? std::string{reinterpret_cast<const char*>(buffer.data())} : std::string{};
|
||||
}
|
||||
bool valid_name(std::string_view name) {
|
||||
if (name.empty() || name.size() > 253)
|
||||
return false;
|
||||
for (const char ch : name)
|
||||
if (!(std::isalnum(static_cast<unsigned char>(ch)) != 0 || ch == '-' || ch == '.'))
|
||||
return false; // Plain DNS names only; nothing that could alter the subject DN.
|
||||
return true;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<DevCredentials, GrantError>
|
||||
generate_dev_credentials(DeploymentEnvironment environment, std::string_view server_name,
|
||||
std::uint32_t days, std::chrono::system_clock::time_point now) {
|
||||
if (environment != DeploymentEnvironment::dev)
|
||||
return std::unexpected(GrantError::not_dev_environment);
|
||||
if (!valid_name(server_name) || days == 0 || days > 825)
|
||||
return std::unexpected(GrantError::invalid_claims);
|
||||
if (psa_crypto_init() != PSA_SUCCESS)
|
||||
return std::unexpected(GrantError::crypto);
|
||||
Key authority, server;
|
||||
if (!authority.generate() || !server.generate())
|
||||
return std::unexpected(GrantError::crypto);
|
||||
const auto from = timestamp(now - std::chrono::hours{1}); // Small clock-skew allowance.
|
||||
const auto to = timestamp(now + std::chrono::hours{24} * days);
|
||||
if (from.empty() || to.empty())
|
||||
return std::unexpected(GrantError::crypto);
|
||||
DevCredentials out;
|
||||
out.authority_pem =
|
||||
certificate(authority, authority, std::string{authority_name}, true, 1, from, to);
|
||||
out.server_certificate_pem =
|
||||
certificate(server, authority, "CN=" + std::string{server_name}, false, 2, from, to);
|
||||
std::array<unsigned char, 4096> key{};
|
||||
if (out.authority_pem.empty() || out.server_certificate_pem.empty() ||
|
||||
mbedtls_pk_write_key_pem(server.get(), key.data(), key.size()) != 0 ||
|
||||
psa_generate_random(out.grant_key.data(), out.grant_key.size()) != PSA_SUCCESS)
|
||||
return std::unexpected(GrantError::crypto);
|
||||
out.server_key_pem = reinterpret_cast<const char*>(key.data());
|
||||
mbedtls_platform_zeroize(key.data(), key.size());
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string format_grant_key(std::span<const std::uint8_t> key) {
|
||||
constexpr char digits[] = "0123456789abcdef";
|
||||
std::string out;
|
||||
for (const auto b : key) {
|
||||
out.push_back(digits[b >> 4U]);
|
||||
out.push_back(digits[b & 0x0FU]);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::expected<std::array<std::uint8_t, dev_key_bytes>, GrantError>
|
||||
parse_grant_key(std::string_view text) {
|
||||
while (!text.empty() && std::isspace(static_cast<unsigned char>(text.front())) != 0)
|
||||
text.remove_prefix(1);
|
||||
while (!text.empty() && std::isspace(static_cast<unsigned char>(text.back())) != 0)
|
||||
text.remove_suffix(1);
|
||||
if (text.size() != 2 * dev_key_bytes)
|
||||
return std::unexpected(GrantError::invalid_key);
|
||||
std::array<std::uint8_t, dev_key_bytes> key{};
|
||||
const auto value = [](char ch) -> int {
|
||||
if (ch >= '0' && ch <= '9')
|
||||
return ch - '0';
|
||||
if (ch >= 'a' && ch <= 'f')
|
||||
return ch - 'a' + 10;
|
||||
return -1;
|
||||
};
|
||||
for (std::size_t i = 0; i < key.size(); ++i) {
|
||||
const int hi = value(text[2 * i]), lo = value(text[2 * i + 1]);
|
||||
if (hi < 0 || lo < 0)
|
||||
return std::unexpected(GrantError::invalid_key);
|
||||
key.at(i) = static_cast<std::uint8_t>((hi << 4) | lo);
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
std::expected<std::uint64_t, GrantError> random_grant_id() {
|
||||
if (psa_crypto_init() != PSA_SUCCESS)
|
||||
return std::unexpected(GrantError::crypto);
|
||||
for (int attempt = 0; attempt < 4; ++attempt) {
|
||||
std::array<std::uint8_t, 8> bytes{};
|
||||
if (psa_generate_random(bytes.data(), bytes.size()) != PSA_SUCCESS)
|
||||
return std::unexpected(GrantError::crypto);
|
||||
std::uint64_t id = 0;
|
||||
for (const auto b : bytes)
|
||||
id = (id << 8U) | b;
|
||||
if (id != 0)
|
||||
return id;
|
||||
}
|
||||
return std::unexpected(GrantError::crypto);
|
||||
}
|
||||
} // namespace evo::network
|
||||
@@ -46,6 +46,10 @@ extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t* data, std::size_t size
|
||||
if (const auto m = decode_close(*frame))
|
||||
again = encode(*m);
|
||||
break;
|
||||
case MessageType::intent_outcome:
|
||||
if (const auto m = decode_intent_outcome(*frame))
|
||||
again = encode(*m);
|
||||
break;
|
||||
case MessageType::environment_view:
|
||||
if (const auto m = decode_environment_view(*frame))
|
||||
again = encode(m->first, m->second);
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
int main() try {
|
||||
evo::network_test::protocol_tests();
|
||||
evo::network_test::tls_tests();
|
||||
evo::network_test::session_tests();
|
||||
std::puts("network tests passed");
|
||||
return 0;
|
||||
} catch (const std::exception& e) {
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#include "evo/network/dev_credentials.hpp"
|
||||
#include "evo/network/tls_channel.hpp"
|
||||
#include "network_test.hpp"
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
|
||||
namespace evo::network_test {
|
||||
inline constexpr const char* server_name = "dev-region.evolution.test";
|
||||
// Throwaway credentials from the same generator operators use (dev only).
|
||||
struct Pki {
|
||||
explicit Pki(const char* name = server_name) {
|
||||
const auto made = network::generate_dev_credentials(
|
||||
network::DeploymentEnvironment::dev, name, 30, std::chrono::system_clock::now());
|
||||
CHECK(made);
|
||||
authority = made->authority_pem;
|
||||
server = {made->server_certificate_pem, made->server_key_pem};
|
||||
}
|
||||
std::string authority;
|
||||
network::ServerCredentials server;
|
||||
};
|
||||
} // namespace evo::network_test
|
||||
@@ -1,11 +1,14 @@
|
||||
#include "evo/network/admission.hpp"
|
||||
#include "evo/network/dev_credentials.hpp"
|
||||
#include "evo/network/dev_grant.hpp"
|
||||
#include "evo/network/protocol.hpp"
|
||||
#include "network_test.hpp"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <random>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace evo::network_test {
|
||||
@@ -122,6 +125,20 @@ void round_trips() {
|
||||
CHECK(r && r->sequence == 9 && r->status == IntentStatus::replayed && r->server_tick == 10);
|
||||
const auto c = decode_close(frame(encode(CloseReason::stale_epoch)));
|
||||
CHECK(c && *c == CloseReason::stale_epoch);
|
||||
CHECK(decode_close(frame(encode(CloseReason::capacity))) == CloseReason::capacity);
|
||||
const auto out = decode_intent_outcome(
|
||||
frame(encode(IntentOutcome{4, IntentFinality::committed_no_effect, 120, 9})));
|
||||
CHECK(out && out->sequence == 4 && out->finality == IntentFinality::committed_no_effect &&
|
||||
out->tick == 120 && out->commit_sequence == 9);
|
||||
CHECK(decode_intent_outcome(
|
||||
frame(encode(IntentOutcome{5, IntentFinality::not_committed, 0, 0}))));
|
||||
// Finality and commit facts must agree.
|
||||
CHECK(refused(decode_intent_outcome(
|
||||
frame(encode(IntentOutcome{4, IntentFinality::committed_no_effect, 120, 0}))),
|
||||
ProtocolError::invalid_value));
|
||||
CHECK(refused(decode_intent_outcome(
|
||||
frame(encode(IntentOutcome{4, IntentFinality::not_committed, 120, 0}))),
|
||||
ProtocolError::invalid_value));
|
||||
|
||||
const auto env = environment();
|
||||
const auto env_bytes = encode(view_header, env);
|
||||
@@ -160,7 +177,7 @@ void frame_rejection() {
|
||||
CHECK(refused(decode_frame(with_header(MessageType{0}, 1, 0, 0, 0)),
|
||||
ProtocolError::unknown_type));
|
||||
// NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
|
||||
CHECK(refused(decode_frame(with_header(MessageType{8}, 1, 0, 0, 0)),
|
||||
CHECK(refused(decode_frame(with_header(MessageType{9}, 1, 0, 0, 0)),
|
||||
ProtocolError::unknown_type));
|
||||
CHECK(refused(decode_frame(with_header(MessageType::intent, 1, 1, 36, 36)),
|
||||
ProtocolError::reserved_flags));
|
||||
@@ -190,7 +207,7 @@ void frame_rejection() {
|
||||
status[frame_header_bytes + 8] = 8;
|
||||
CHECK(refused(decode_intent_result(frame(status)), ProtocolError::invalid_value));
|
||||
auto reason = encode(CloseReason::normal);
|
||||
reason[frame_header_bytes] = 9;
|
||||
reason[frame_header_bytes] = 10;
|
||||
CHECK(refused(decode_close(frame(reason)), ProtocolError::invalid_value));
|
||||
CHECK(refused(decode_welcome(frame(encode(Welcome{}))), ProtocolError::invalid_value));
|
||||
// A well-formed encoding of an invalid view is rejected by the presentation rules.
|
||||
@@ -237,13 +254,15 @@ void reader() {
|
||||
// identical bytes (canonical, no hidden fields trusted from the wire).
|
||||
void fuzz() {
|
||||
std::mt19937_64 random{0x9E3779B97F4A7C15ULL};
|
||||
const std::vector<Bytes> seeds{*encode(Hello{0, Bytes(dev_grant_bytes, 7)}),
|
||||
encode(Welcome{1, 11, 12, 13, 14, 15, 16, 17, 8, 8, 16}),
|
||||
encode(IntentMessage{1, 2, 3, 4, 5, 6}),
|
||||
encode(IntentResult{1, IntentStatus::outside_window, 2}),
|
||||
encode(CloseReason::rate_limited),
|
||||
encode(view_header, environment()),
|
||||
encode(view_header, organisms(3))};
|
||||
const std::vector<Bytes> seeds{
|
||||
*encode(Hello{0, Bytes(dev_grant_bytes, 7)}),
|
||||
encode(Welcome{1, 11, 12, 13, 14, 15, 16, 17, 8, 8, 16}),
|
||||
encode(IntentMessage{1, 2, 3, 4, 5, 6}),
|
||||
encode(IntentResult{1, IntentStatus::outside_window, 2}),
|
||||
encode(CloseReason::rate_limited),
|
||||
encode(IntentOutcome{3, IntentFinality::committed_no_effect, 4, 5}),
|
||||
encode(view_header, environment()),
|
||||
encode(view_header, organisms(3))};
|
||||
std::size_t accepted = 0;
|
||||
for (int round = 0; round < 30000; ++round) {
|
||||
auto bytes = seeds[random() % seeds.size()];
|
||||
@@ -290,6 +309,10 @@ void fuzz() {
|
||||
if (const auto m = decode_close(*f))
|
||||
again = encode(*m);
|
||||
break;
|
||||
case MessageType::intent_outcome:
|
||||
if (const auto m = decode_intent_outcome(*f))
|
||||
again = encode(*m);
|
||||
break;
|
||||
case MessageType::environment_view:
|
||||
if (const auto m = decode_environment_view(*f))
|
||||
again = encode(m->first, m->second);
|
||||
@@ -362,6 +385,36 @@ void dev_grants() {
|
||||
CHECK(refused(moved.issue(claims(5, 5)), GrantError::invalid_key));
|
||||
}
|
||||
|
||||
void dev_credentials() {
|
||||
const auto now = std::chrono::system_clock::now();
|
||||
CHECK(refused(generate_dev_credentials(DeploymentEnvironment::stage, "dev.test", 30, now),
|
||||
GrantError::not_dev_environment));
|
||||
CHECK(refused(generate_dev_credentials(DeploymentEnvironment::world0, "dev.test", 30, now),
|
||||
GrantError::not_dev_environment));
|
||||
for (const auto* name : {"", "a,CN=evil", "a b", "x\n"})
|
||||
CHECK(refused(generate_dev_credentials(DeploymentEnvironment::dev, name, 30, now),
|
||||
GrantError::invalid_claims));
|
||||
CHECK(refused(generate_dev_credentials(DeploymentEnvironment::dev, "dev.test", 0, now),
|
||||
GrantError::invalid_claims));
|
||||
CHECK(refused(generate_dev_credentials(DeploymentEnvironment::dev, "dev.test", 826, now),
|
||||
GrantError::invalid_claims));
|
||||
const auto made = generate_dev_credentials(DeploymentEnvironment::dev, "dev.test", 30, now);
|
||||
CHECK(made && made->authority_pem.starts_with("-----BEGIN CERTIFICATE-----") &&
|
||||
made->server_certificate_pem.starts_with("-----BEGIN CERTIFICATE-----") &&
|
||||
made->server_key_pem.find("PRIVATE KEY") != std::string::npos);
|
||||
CHECK(std::ranges::any_of(made->grant_key, [](std::uint8_t b) { return b != 0; }));
|
||||
const auto text = format_grant_key(made->grant_key);
|
||||
CHECK(text.size() == 64 && parse_grant_key(" " + text + "\n") == made->grant_key);
|
||||
CHECK(refused(parse_grant_key(text.substr(1)), GrantError::invalid_key));
|
||||
auto upper = text;
|
||||
upper[0] = 'G';
|
||||
CHECK(refused(parse_grant_key(upper), GrantError::invalid_key));
|
||||
// The generated key is usable for grants.
|
||||
CHECK(DevGrantKey::import(DeploymentEnvironment::dev, made->grant_key));
|
||||
const auto a = random_grant_id(), b = random_grant_id();
|
||||
CHECK(a && b && *a != 0 && *a != *b);
|
||||
}
|
||||
|
||||
SessionAdmission admission(std::size_t max_sessions = 4, std::size_t remembered = 8) {
|
||||
AdmissionConfig config;
|
||||
config.world = identity;
|
||||
@@ -452,6 +505,7 @@ void protocol_tests() {
|
||||
reader();
|
||||
fuzz();
|
||||
dev_grants();
|
||||
dev_credentials();
|
||||
admission_rules();
|
||||
}
|
||||
} // namespace evo::network_test
|
||||
|
||||
@@ -0,0 +1,392 @@
|
||||
#include "evo/network/session_client.hpp"
|
||||
#include "evo/network/session_server.hpp"
|
||||
#include "evo/network/tcp.hpp"
|
||||
#include "network_pki.hpp"
|
||||
#include "network_test.hpp"
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace evo::network_test {
|
||||
namespace {
|
||||
using namespace evo::network;
|
||||
using Clock = std::chrono::steady_clock;
|
||||
constexpr world::WorldIdentity identity{11, 12};
|
||||
constexpr std::uint64_t region = 21;
|
||||
constexpr std::uint64_t now_seconds = 1'000'000;
|
||||
|
||||
DevGrantKey grant_key(std::uint8_t fill = 0x33) {
|
||||
std::array<std::uint8_t, dev_key_bytes> secret{};
|
||||
secret.fill(fill);
|
||||
auto k = DevGrantKey::import(DeploymentEnvironment::dev, secret);
|
||||
CHECK(k);
|
||||
return std::move(*k);
|
||||
}
|
||||
std::vector<DevGrant> grants(std::uint64_t subject, std::size_t count, std::uint64_t first_id,
|
||||
std::uint8_t fill = 0x33, std::uint64_t grant_region = region) {
|
||||
const auto k = grant_key(fill);
|
||||
std::vector<DevGrant> out;
|
||||
for (std::size_t i = 0; i < count; ++i) {
|
||||
const auto g = k.issue(
|
||||
{first_id + i, subject, identity, grant_region, now_seconds - 60, now_seconds + 1800});
|
||||
CHECK(g);
|
||||
out.push_back(*g);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
client::EnvironmentPresentation environment(std::uint64_t tick) {
|
||||
client::EnvironmentCell c;
|
||||
c.voxel = {0, 0, -16000};
|
||||
c.chunk_revision = 1;
|
||||
c.habitat = {{0}, {0}, {0}, {10}, {20}, {3}};
|
||||
c.volume = {600};
|
||||
c.terrain_material = {1};
|
||||
c.presence[0] = client::FieldPresence::present;
|
||||
c.quantity[0] = tick; // Visible change per tick.
|
||||
c.moisture_status = world::MoistureStatus::zero_capacity;
|
||||
auto v = client::EnvironmentPresentation::create({identity, tick, tick}, 1, -16000, {&c, 1});
|
||||
CHECK(v);
|
||||
return std::move(*v);
|
||||
}
|
||||
client::OrganismPresentation no_organisms(std::uint64_t tick) {
|
||||
auto v = client::OrganismPresentation::create({identity, tick, 0}, -16000, {});
|
||||
CHECK(v);
|
||||
return std::move(*v);
|
||||
}
|
||||
|
||||
struct Harness {
|
||||
explicit Harness(std::size_t max_sessions = 4,
|
||||
std::chrono::milliseconds hello = std::chrono::milliseconds{3000}) {
|
||||
server = make_server(0, max_sessions, hello);
|
||||
port = server->port();
|
||||
}
|
||||
std::unique_ptr<SessionServer> make_server(std::uint16_t at, std::size_t max_sessions,
|
||||
std::chrono::milliseconds hello) {
|
||||
SessionServerConfig c;
|
||||
c.listen = "127.0.0.1:" + std::to_string(at);
|
||||
c.credentials = pki.server;
|
||||
c.admission.world = identity;
|
||||
c.admission.region = region;
|
||||
c.admission.max_sessions = max_sessions;
|
||||
c.admission.intent_window = 8;
|
||||
c.admission.window_ticks = 8;
|
||||
c.admission.max_intents_per_window = 8;
|
||||
c.hello_deadline = hello;
|
||||
auto s = SessionServer::create(std::move(c), grant_key());
|
||||
CHECK(s);
|
||||
return std::move(*s);
|
||||
}
|
||||
std::unique_ptr<SessionClient> client(std::vector<DevGrant> g) {
|
||||
SessionClientConfig c;
|
||||
c.host = "127.0.0.1";
|
||||
c.port = port;
|
||||
c.trust = {pki.authority, server_name};
|
||||
c.grants = std::move(g);
|
||||
c.first_retry = std::chrono::milliseconds{50};
|
||||
c.max_retry = std::chrono::milliseconds{200};
|
||||
c.attempt_deadline = std::chrono::milliseconds{3000};
|
||||
auto s = SessionClient::create(std::move(c));
|
||||
CHECK(s);
|
||||
clients.push_back(s->get());
|
||||
return std::move(*s);
|
||||
}
|
||||
void tick() {
|
||||
++facts.simulated_tick;
|
||||
if (server)
|
||||
server->publish(environment(facts.simulated_tick), no_organisms(facts.simulated_tick),
|
||||
facts);
|
||||
}
|
||||
// Pumps everything until `done` or the wall-clock limit.
|
||||
bool until(const std::function<bool()>& done,
|
||||
std::chrono::milliseconds limit = std::chrono::milliseconds{5000}) {
|
||||
const auto end = Clock::now() + limit;
|
||||
while (Clock::now() < end) {
|
||||
const auto now = Clock::now();
|
||||
if (server)
|
||||
server->poll(facts, now_seconds, now);
|
||||
for (auto* c : clients)
|
||||
c->poll(now);
|
||||
if (done())
|
||||
return true;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{1});
|
||||
}
|
||||
return false;
|
||||
}
|
||||
void collect(SessionClient& c, std::vector<ClientEvent>& into) {
|
||||
auto e = c.take_events();
|
||||
into.insert(into.end(), e.begin(), e.end());
|
||||
}
|
||||
Pki pki;
|
||||
RegionFacts facts{true, 1, 100, 90, 5};
|
||||
std::unique_ptr<SessionServer> server;
|
||||
std::uint16_t port{};
|
||||
std::vector<SessionClient*> clients;
|
||||
};
|
||||
bool has(const std::vector<ClientEvent>& events, ClientEvent::Kind kind) {
|
||||
return std::ranges::any_of(events, [&](const ClientEvent& e) { return e.kind == kind; });
|
||||
}
|
||||
const ClientEvent* last(const std::vector<ClientEvent>& events, ClientEvent::Kind kind) {
|
||||
const auto it = std::ranges::find_if(events.rbegin(), events.rend(),
|
||||
[&](const ClientEvent& e) { return e.kind == kind; });
|
||||
return it == events.rend() ? nullptr : &*it;
|
||||
}
|
||||
|
||||
void connect_view_intents_and_outcomes() {
|
||||
Harness h;
|
||||
h.tick();
|
||||
auto c = h.client(grants(7, 4, 100));
|
||||
std::vector<ClientEvent> events;
|
||||
CHECK(h.until([&] {
|
||||
h.collect(*c, events);
|
||||
return c->latest_view().has_value();
|
||||
}));
|
||||
const auto w = c->welcome();
|
||||
CHECK(w && w->session == 1 && w->epoch == 1 && w->world == 11 && w->frame == 12 &&
|
||||
w->region == region && w->commit_watermark == 90 && w->server_tick == 101);
|
||||
CHECK(c->grants_left() == 3 && h.server->sessions() == 1);
|
||||
CHECK(c->environment().freshness() == client::Freshness::current);
|
||||
CHECK(c->environment().current()->cells()[0].quantity[0] == 101);
|
||||
CHECK(must(c->latest_view()).commit_watermark == 90 &&
|
||||
must(c->latest_view()).server_tick == 101);
|
||||
CHECK(h.until([&] { return c->organisms().current() != nullptr; }));
|
||||
CHECK(c->organisms().current()->cells().empty()); // Explicitly no organisms.
|
||||
// Views follow the region: a new tick replaces the presentation.
|
||||
h.tick();
|
||||
CHECK(h.until([&] { return must(c->latest_view()).server_tick == 102; }));
|
||||
CHECK(c->environment().current()->cells()[0].quantity[0] == 102);
|
||||
|
||||
// Admitted intention: carried by a host input, then durably committed.
|
||||
CHECK(c->send({1, 0, 1000, 0}));
|
||||
CHECK(h.until([&] {
|
||||
h.collect(*c, events);
|
||||
return last(events, ClientEvent::Kind::result) != nullptr;
|
||||
}));
|
||||
CHECK(last(events, ClientEvent::Kind::result)->result.status == IntentStatus::admitted);
|
||||
CHECK(!h.server->carry(105)); // Intended tick 102 + 4 = 106.
|
||||
const auto request = h.server->carry(106);
|
||||
CHECK(must(request)[0] == 'I' && must(request)[3] == '1');
|
||||
CHECK(!h.server->carry(200)); // Carried once.
|
||||
h.server->committed(must(request), 106, 6);
|
||||
CHECK(h.until([&] {
|
||||
h.collect(*c, events);
|
||||
return last(events, ClientEvent::Kind::outcome) != nullptr;
|
||||
}));
|
||||
const auto* outcome = last(events, ClientEvent::Kind::outcome);
|
||||
CHECK(outcome->outcome.sequence == 1 &&
|
||||
outcome->outcome.finality == IntentFinality::committed_no_effect &&
|
||||
outcome->outcome.tick == 106 && outcome->outcome.commit_sequence == 6);
|
||||
|
||||
// Rejected: replay and an out-of-bound vector never reach the host.
|
||||
events.clear();
|
||||
CHECK(c->send({1, 0, 0, 1000}) && c->send({2, 0, 1000, 1000}));
|
||||
CHECK(h.until([&] {
|
||||
h.collect(*c, events);
|
||||
return std::ranges::count_if(events, [](const ClientEvent& e) {
|
||||
return e.kind == ClientEvent::Kind::result;
|
||||
}) == 2;
|
||||
}));
|
||||
CHECK(events[0].result.status == IntentStatus::replayed &&
|
||||
events[1].result.status == IntentStatus::invalid_vector);
|
||||
CHECK(!h.server->carry(1000));
|
||||
// A dropped host input is reported as not committed.
|
||||
events.clear();
|
||||
CHECK(c->send({3, 0, -707, 707}));
|
||||
CHECK(h.until([&] {
|
||||
h.collect(*c, events);
|
||||
return has(events, ClientEvent::Kind::result);
|
||||
}));
|
||||
CHECK(last(events, ClientEvent::Kind::result)->result.status == IntentStatus::admitted);
|
||||
const auto dropped = h.server->carry(1000);
|
||||
CHECK(dropped);
|
||||
h.server->not_committed(must(dropped));
|
||||
CHECK(h.until([&] {
|
||||
h.collect(*c, events);
|
||||
return last(events, ClientEvent::Kind::outcome) != nullptr;
|
||||
}));
|
||||
CHECK(last(events, ClientEvent::Kind::outcome)->outcome.finality ==
|
||||
IntentFinality::not_committed);
|
||||
}
|
||||
|
||||
void duplicate_capacity_and_authentication() {
|
||||
Harness h{2};
|
||||
h.tick();
|
||||
auto first = h.client(grants(7, 2, 100));
|
||||
CHECK(h.until([&] { return first->phase() == SessionPhase::established; }));
|
||||
// Same subject, fresh grant: refused; control is never duplicated.
|
||||
auto twin = h.client(grants(7, 1, 200));
|
||||
std::vector<ClientEvent> twin_events;
|
||||
CHECK(h.until([&] {
|
||||
h.collect(*twin, twin_events);
|
||||
return has(twin_events, ClientEvent::Kind::closed);
|
||||
}));
|
||||
CHECK(last(twin_events, ClientEvent::Kind::closed)->reason == CloseReason::duplicate_session);
|
||||
CHECK(first->phase() == SessionPhase::established && h.server->sessions() == 1);
|
||||
// One grant only: after the refusal, reconnection stops explicitly.
|
||||
CHECK(h.until([&] {
|
||||
h.collect(*twin, twin_events);
|
||||
return twin->phase() == SessionPhase::exhausted;
|
||||
}));
|
||||
CHECK(has(twin_events, ClientEvent::Kind::exhausted));
|
||||
// A reused grant is refused even for a different connection.
|
||||
auto reuse = h.client(grants(7, 1, 100)); // Grant id 100 was used by `first`.
|
||||
std::vector<ClientEvent> reuse_events;
|
||||
CHECK(h.until([&] {
|
||||
h.collect(*reuse, reuse_events);
|
||||
return has(reuse_events, ClientEvent::Kind::closed);
|
||||
}));
|
||||
CHECK(last(reuse_events, ClientEvent::Kind::closed)->reason == CloseReason::unauthenticated);
|
||||
// Foreign key and foreign region are unauthenticated.
|
||||
std::vector<std::unique_ptr<SessionClient>> others; // Kept alive: the harness polls them.
|
||||
for (auto g : {grants(8, 1, 300, 0x44), grants(9, 1, 301, 0x33, region + 1)}) {
|
||||
others.push_back(h.client(std::move(g)));
|
||||
auto* other = others.back().get();
|
||||
std::vector<ClientEvent> e;
|
||||
CHECK(h.until([&] {
|
||||
h.collect(*other, e);
|
||||
return has(e, ClientEvent::Kind::closed);
|
||||
}));
|
||||
CHECK(last(e, ClientEvent::Kind::closed)->reason == CloseReason::unauthenticated);
|
||||
}
|
||||
// Capacity: a second subject fits, a third is refused with `capacity`.
|
||||
auto second = h.client(grants(10, 1, 400));
|
||||
CHECK(h.until([&] { return second->phase() == SessionPhase::established; }));
|
||||
auto third = h.client(grants(11, 1, 500));
|
||||
std::vector<ClientEvent> third_events;
|
||||
CHECK(h.until([&] {
|
||||
h.collect(*third, third_events);
|
||||
return has(third_events, ClientEvent::Kind::closed);
|
||||
}));
|
||||
CHECK(last(third_events, ClientEvent::Kind::closed)->reason == CloseReason::capacity);
|
||||
}
|
||||
|
||||
void stale_epoch_restart_and_reconnect() {
|
||||
Harness h;
|
||||
h.tick();
|
||||
auto c = h.client(grants(7, 6, 100));
|
||||
std::vector<ClientEvent> events;
|
||||
CHECK(h.until([&] { return c->latest_view().has_value(); }));
|
||||
CHECK(c->send({1, 0, 1000, 0}));
|
||||
CHECK(h.until([&] {
|
||||
h.collect(*c, events);
|
||||
return has(events, ClientEvent::Kind::result);
|
||||
}));
|
||||
// New ownership epoch: the session is closed as stale, its pending intention's
|
||||
// outcome is unknown, and the presentation is kept but marked stale.
|
||||
h.facts.epoch = 2;
|
||||
CHECK(h.until([&] {
|
||||
h.collect(*c, events);
|
||||
return has(events, ClientEvent::Kind::closed);
|
||||
}));
|
||||
CHECK(last(events, ClientEvent::Kind::closed)->reason == CloseReason::stale_epoch);
|
||||
CHECK(has(events, ClientEvent::Kind::unknown));
|
||||
CHECK(c->environment().freshness() == client::Freshness::stale);
|
||||
CHECK(!h.server->carry(1000)); // Old-epoch intentions never ride new inputs.
|
||||
// Reconnect with a fresh grant: new session, new epoch, fresh baseline.
|
||||
h.tick();
|
||||
CHECK(h.until([&] {
|
||||
h.collect(*c, events);
|
||||
return c->phase() == SessionPhase::established && c->latest_view().has_value();
|
||||
}));
|
||||
CHECK(must(c->welcome()).epoch == 2 && must(c->welcome()).session == 2 &&
|
||||
c->grants_left() == 4);
|
||||
CHECK(c->environment().freshness() == client::Freshness::current);
|
||||
CHECK(must(c->latest_view()).epoch == 2);
|
||||
|
||||
// Server process restart: the listener disappears, then a new one takes the port
|
||||
// with a new epoch. The client reconnects without the old session's state.
|
||||
events.clear();
|
||||
h.server.reset();
|
||||
CHECK(h.until([&] {
|
||||
h.collect(*c, events);
|
||||
return has(events, ClientEvent::Kind::lost);
|
||||
}));
|
||||
CHECK(c->phase() != SessionPhase::established &&
|
||||
c->environment().freshness() == client::Freshness::stale);
|
||||
h.facts = {true, 3, 40, 40, 2}; // Recovered at the committed checkpoint: tick went back.
|
||||
h.server = h.make_server(h.port, 4, std::chrono::milliseconds{3000});
|
||||
h.tick();
|
||||
CHECK(h.until([&] {
|
||||
h.collect(*c, events);
|
||||
return c->phase() == SessionPhase::established && c->latest_view().has_value();
|
||||
}));
|
||||
CHECK(must(c->welcome()).epoch == 3 && must(c->welcome()).session == 1);
|
||||
// The layer was reset for the new session, so the earlier tick does not regress it.
|
||||
CHECK(must(c->latest_view()).server_tick == 41 &&
|
||||
c->environment().current()->revision().tick == 41);
|
||||
}
|
||||
|
||||
void abuse() {
|
||||
// Not ready yet: Hello is refused with server_shutdown and retried later.
|
||||
Harness h;
|
||||
h.facts.ready = false;
|
||||
h.facts.epoch = 0;
|
||||
auto c = h.client(grants(7, 3, 100));
|
||||
std::vector<ClientEvent> events;
|
||||
CHECK(h.until([&] {
|
||||
h.collect(*c, events);
|
||||
return has(events, ClientEvent::Kind::closed);
|
||||
}));
|
||||
CHECK(last(events, ClientEvent::Kind::closed)->reason == CloseReason::server_shutdown);
|
||||
// Refused before admission: the grant was never recorded, so it is not spent.
|
||||
CHECK(c->grants_left() == 3);
|
||||
h.facts = {true, 1, 100, 90, 5};
|
||||
h.tick();
|
||||
CHECK(h.until([&] { return c->phase() == SessionPhase::established; }));
|
||||
CHECK(c->grants_left() == 2);
|
||||
// Flooding: the byte-rate limit closes the connection. The flooding client may see
|
||||
// a transport reset before it reads the Close frame, so check the server's decision.
|
||||
CHECK(h.until([&] { return c->latest_view().has_value(); }));
|
||||
(void)h.server->take_events();
|
||||
for (std::uint64_t s = 10; s < 2000; ++s)
|
||||
CHECK(c->send({s, 0, 0, 0}));
|
||||
std::vector<SessionEvent> server_events;
|
||||
CHECK(h.until([&] {
|
||||
auto e = h.server->take_events();
|
||||
server_events.insert(server_events.end(), e.begin(), e.end());
|
||||
return std::ranges::any_of(server_events, [](const SessionEvent& x) {
|
||||
return x.kind == SessionEvent::Kind::closed && x.reason == CloseReason::rate_limited;
|
||||
});
|
||||
}));
|
||||
CHECK(h.until([&] {
|
||||
h.collect(*c, events);
|
||||
return c->phase() != SessionPhase::established;
|
||||
}));
|
||||
|
||||
// Plaintext and silent connections are dropped without a session: the server
|
||||
// closes the socket (plaintext at the TLS handshake, silence at the deadline).
|
||||
Harness quiet{4, std::chrono::milliseconds{200}};
|
||||
const auto dropped_by_server = [&](TcpStream& s, std::span<const std::uint8_t> bytes) {
|
||||
std::size_t sent = 0;
|
||||
return quiet.until([&] {
|
||||
if (sent < bytes.size())
|
||||
if (const auto n = s.write(bytes.subspan(sent)))
|
||||
sent += *n;
|
||||
std::array<std::uint8_t, 64> sink{};
|
||||
const auto r = s.read(sink);
|
||||
return !r.has_value();
|
||||
});
|
||||
};
|
||||
auto plain = TcpStream::connect("127.0.0.1", quiet.port);
|
||||
CHECK(plain);
|
||||
const auto bytes = encode(IntentMessage{1, 1, 1, 1, 0, 0});
|
||||
CHECK(dropped_by_server(**plain, bytes));
|
||||
auto silent = TcpStream::connect("127.0.0.1", quiet.port);
|
||||
CHECK(silent);
|
||||
const auto start = Clock::now();
|
||||
CHECK(dropped_by_server(**silent, {}));
|
||||
CHECK(Clock::now() - start >= std::chrono::milliseconds{150}); // Held until the deadline.
|
||||
CHECK(quiet.until([&] { return quiet.server->connections() == 0; }));
|
||||
CHECK(quiet.server->sessions() == 0);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void session_tests() {
|
||||
connect_view_intents_and_outcomes();
|
||||
duplicate_capacity_and_authentication();
|
||||
stale_epoch_restart_and_reconnect();
|
||||
abuse();
|
||||
}
|
||||
} // namespace evo::network_test
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include <expected>
|
||||
#include <optional>
|
||||
#include <source_location>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
@@ -10,10 +11,20 @@ inline void check(bool ok, std::source_location at = std::source_location::curre
|
||||
throw std::runtime_error(std::string{"network "} + at.file_name() + ":" +
|
||||
std::to_string(at.line()));
|
||||
}
|
||||
// The value of an optional that the test requires to be present.
|
||||
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(std::string{"network missing value "} + at.file_name() + ":" +
|
||||
std::to_string(at.line()));
|
||||
return *o;
|
||||
}
|
||||
template <typename T, typename E> bool refused(const std::expected<T, E>& r, E e) {
|
||||
return !r && r.error() == e;
|
||||
}
|
||||
void protocol_tests();
|
||||
void tls_tests();
|
||||
void session_tests();
|
||||
} // namespace evo::network_test
|
||||
#define CHECK(...) ::evo::network_test::check(static_cast<bool>(__VA_ARGS__))
|
||||
|
||||
+1
-64
@@ -1,5 +1,6 @@
|
||||
#include "evo/network/protocol.hpp"
|
||||
#include "evo/network/tls_channel.hpp"
|
||||
#include "network_pki.hpp"
|
||||
#include "network_test.hpp"
|
||||
#include <array>
|
||||
#include <deque>
|
||||
@@ -11,70 +12,6 @@
|
||||
namespace evo::network_test {
|
||||
namespace {
|
||||
using namespace evo::network;
|
||||
constexpr const char* server_name = "dev-region.evolution.test";
|
||||
|
||||
// Throwaway test credentials; production/dev deployment keys are never generated here.
|
||||
struct Key {
|
||||
Key() {
|
||||
mbedtls_pk_init(&pk);
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
|
||||
psa_set_key_bits(&attributes, 256);
|
||||
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_EXPORT);
|
||||
psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_SHA_256));
|
||||
mbedtls_svc_key_id_t id = PSA_KEY_ID_NULL;
|
||||
CHECK(psa_crypto_init() == PSA_SUCCESS &&
|
||||
psa_generate_key(&attributes, &id) == PSA_SUCCESS);
|
||||
CHECK(mbedtls_pk_copy_from_psa(id, &pk) == 0);
|
||||
(void)psa_destroy_key(id);
|
||||
}
|
||||
~Key() {
|
||||
mbedtls_pk_free(&pk);
|
||||
}
|
||||
Key(const Key&) = delete;
|
||||
Key& operator=(const Key&) = delete;
|
||||
[[nodiscard]] std::string pem() {
|
||||
std::array<unsigned char, 4096> buffer{};
|
||||
CHECK(mbedtls_pk_write_key_pem(&pk, buffer.data(), buffer.size()) == 0);
|
||||
return reinterpret_cast<const char*>(buffer.data());
|
||||
}
|
||||
mbedtls_pk_context pk;
|
||||
};
|
||||
std::string certificate(Key& subject, Key& issuer, const char* subject_name,
|
||||
const char* issuer_name, bool authority, unsigned char serial) {
|
||||
mbedtls_x509write_cert crt;
|
||||
mbedtls_x509write_crt_init(&crt);
|
||||
mbedtls_x509write_crt_set_version(&crt, MBEDTLS_X509_CRT_VERSION_3);
|
||||
mbedtls_x509write_crt_set_md_alg(&crt, MBEDTLS_MD_SHA256);
|
||||
mbedtls_x509write_crt_set_subject_key(&crt, &subject.pk);
|
||||
mbedtls_x509write_crt_set_issuer_key(&crt, &issuer.pk);
|
||||
CHECK(mbedtls_x509write_crt_set_serial_raw(&crt, &serial, 1) == 0 &&
|
||||
mbedtls_x509write_crt_set_validity(&crt, "20250101000000", "20991231235959") == 0 &&
|
||||
mbedtls_x509write_crt_set_subject_name(&crt, subject_name) == 0 &&
|
||||
mbedtls_x509write_crt_set_issuer_name(&crt, issuer_name) == 0 &&
|
||||
mbedtls_x509write_crt_set_basic_constraints(&crt, authority ? 1 : 0, -1) == 0 &&
|
||||
mbedtls_x509write_crt_set_key_usage(&crt, authority
|
||||
? MBEDTLS_X509_KU_KEY_CERT_SIGN
|
||||
: MBEDTLS_X509_KU_DIGITAL_SIGNATURE) == 0);
|
||||
std::array<unsigned char, 4096> buffer{};
|
||||
const int result = mbedtls_x509write_crt_pem(&crt, buffer.data(), buffer.size());
|
||||
mbedtls_x509write_crt_free(&crt);
|
||||
CHECK(result == 0);
|
||||
return reinterpret_cast<const char*>(buffer.data());
|
||||
}
|
||||
struct Pki {
|
||||
Pki(const char* name = server_name) {
|
||||
authority =
|
||||
certificate(ca_key, ca_key, "CN=AEvolution dev CA", "CN=AEvolution dev CA", true, 1);
|
||||
const std::string subject = std::string{"CN="} + name;
|
||||
server.certificate_chain_pem =
|
||||
certificate(server_key, ca_key, subject.c_str(), "CN=AEvolution dev CA", false, 2);
|
||||
server.private_key_pem = server_key.pem();
|
||||
}
|
||||
Key ca_key, server_key;
|
||||
std::string authority;
|
||||
ServerCredentials server;
|
||||
};
|
||||
|
||||
// Two in-memory byte queues with optional faults.
|
||||
struct Wire {
|
||||
|
||||
@@ -7,6 +7,9 @@ void run_region_lease_store_tests();
|
||||
void run_region_checkpoint_store_tests();
|
||||
void run_region_host_tests();
|
||||
void run_region_server_process_tests();
|
||||
#ifdef EVO_POSTGRES_SESSIONS
|
||||
void run_region_session_tests();
|
||||
#endif
|
||||
void run_region_recovery_tests();
|
||||
void run_region_ops_tests();
|
||||
void run_region_clone_tests();
|
||||
@@ -120,6 +123,10 @@ int main() {
|
||||
std::puts("region host integration passed");
|
||||
run_region_server_process_tests();
|
||||
std::puts("region server process integration passed");
|
||||
#ifdef EVO_POSTGRES_SESSIONS
|
||||
run_region_session_tests();
|
||||
std::puts("region dev session integration passed");
|
||||
#endif
|
||||
run_region_recovery_tests();
|
||||
std::puts("region recovery integration passed");
|
||||
run_region_ops_tests();
|
||||
|
||||
@@ -0,0 +1,257 @@
|
||||
// Real process tests for M9.7 dev sessions (ADR-0083): evo-server with PostgreSQL,
|
||||
// synthetic inputs and the TLS session listener, driven by the real SessionClient.
|
||||
// Run by tools/ci/postgres-check.sh when the build has EVO_BUILD_NETWORK.
|
||||
#include "evo/network/dev_credentials.hpp"
|
||||
#include "evo/network/session_client.hpp"
|
||||
#include "fixtures/server_process.hpp"
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <fcntl.h>
|
||||
#include <functional>
|
||||
#include <source_location>
|
||||
#include <thread>
|
||||
#include <unistd.h>
|
||||
|
||||
void run_region_session_tests();
|
||||
|
||||
namespace {
|
||||
using namespace evo::test::process;
|
||||
using namespace evo;
|
||||
using evo::test::region_identity;
|
||||
using network::ClientEvent;
|
||||
void check(bool value, std::source_location at = std::source_location::current()) {
|
||||
if (!value)
|
||||
throw std::runtime_error("region session test 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("region session missing value line " + std::to_string(at.line()));
|
||||
return *o;
|
||||
}
|
||||
constexpr const char* name = "dev-region.evolution.test";
|
||||
|
||||
void write_secret(const fs::path& path, const std::string& text) {
|
||||
const int fd = ::open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
|
||||
expect(fd >= 0 && ::write(fd, text.data(), text.size()) == static_cast<ssize_t>(text.size()) &&
|
||||
::close(fd) == 0,
|
||||
"write secret");
|
||||
}
|
||||
struct Credentials {
|
||||
network::DevCredentials made;
|
||||
std::vector<std::string> args(const ServerRegion& r, std::uint16_t port,
|
||||
const char* environment = "dev") const {
|
||||
write_secret(r.root / "server.pem", made.server_certificate_pem);
|
||||
write_secret(r.root / "server.key", made.server_key_pem);
|
||||
write_secret(r.root / "grant.key", network::format_grant_key(made.grant_key));
|
||||
return server_args(r,
|
||||
{"--synthetic-inputs", "development-v1", "--session-listen",
|
||||
"127.0.0.1:" + std::to_string(port), "--session-cert",
|
||||
(r.root / "server.pem").string(), "--session-key",
|
||||
(r.root / "server.key").string(), "--session-grant-key",
|
||||
(r.root / "grant.key").string(), "--session-environment", environment});
|
||||
}
|
||||
std::vector<network::DevGrant> grants(const ServerRegion& r, std::uint64_t subject,
|
||||
std::size_t count, std::uint64_t first) const {
|
||||
const auto key =
|
||||
network::DevGrantKey::import(network::DeploymentEnvironment::dev, made.grant_key);
|
||||
expect(key.has_value(), "grant key");
|
||||
const auto now =
|
||||
static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::seconds>(
|
||||
std::chrono::system_clock::now().time_since_epoch())
|
||||
.count());
|
||||
std::vector<network::DevGrant> out;
|
||||
for (std::size_t i = 0; i < count; ++i) {
|
||||
const auto g = key->issue(
|
||||
{first + i, subject, region_identity_of(r), r.key.region, now - 5, now + 900});
|
||||
expect(g.has_value(), "issue grant");
|
||||
out.push_back(*g);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
static world::WorldIdentity region_identity_of(const ServerRegion& r) {
|
||||
return {r.key.world, region_identity.frame};
|
||||
}
|
||||
};
|
||||
std::unique_ptr<network::SessionClient> client(const Credentials& c, std::uint16_t port,
|
||||
std::vector<network::DevGrant> grants) {
|
||||
network::SessionClientConfig config;
|
||||
config.host = "127.0.0.1";
|
||||
config.port = port;
|
||||
config.trust = {c.made.authority_pem, name};
|
||||
config.grants = std::move(grants);
|
||||
config.first_retry = 100ms;
|
||||
config.max_retry = 1000ms;
|
||||
auto made = network::SessionClient::create(std::move(config));
|
||||
expect(made.has_value(), "client");
|
||||
return std::move(*made);
|
||||
}
|
||||
std::uint16_t listening_port(Child& server) {
|
||||
std::uint16_t port = 0;
|
||||
CHECK(server.wait_for(
|
||||
[&](const std::string& l) {
|
||||
unsigned p = 0;
|
||||
if (std::sscanf(l.c_str(), "session listening %u", &p) != 1)
|
||||
return false;
|
||||
port = static_cast<std::uint16_t>(p);
|
||||
return true;
|
||||
},
|
||||
15s));
|
||||
return port;
|
||||
}
|
||||
// Pumps the client while draining the server's output (it must never block on a pipe).
|
||||
bool pump(Child& server, std::vector<network::SessionClient*> clients,
|
||||
std::vector<ClientEvent>& events, const std::function<bool()>& done,
|
||||
std::chrono::milliseconds limit = 20s) {
|
||||
const auto end = Clock::now() + limit;
|
||||
while (Clock::now() < end) {
|
||||
for (auto* c : clients) {
|
||||
c->poll(Clock::now());
|
||||
auto e = c->take_events();
|
||||
if (c == clients.front())
|
||||
events.insert(events.end(), e.begin(), e.end());
|
||||
}
|
||||
if (done())
|
||||
return true;
|
||||
(void)server.wait_for([](const std::string&) { return false; }, 2ms);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool has(const std::vector<ClientEvent>& e, ClientEvent::Kind k) {
|
||||
return std::ranges::any_of(e, [&](const ClientEvent& x) { return x.kind == k; });
|
||||
}
|
||||
const ClientEvent* last(const std::vector<ClientEvent>& e, ClientEvent::Kind k) {
|
||||
const auto it = std::ranges::find_if(e.rbegin(), e.rend(),
|
||||
[&](const ClientEvent& x) { return x.kind == k; });
|
||||
return it == e.rend() ? nullptr : &*it;
|
||||
}
|
||||
std::string hex(const std::array<std::uint8_t, 4>& b) {
|
||||
char out[9]{};
|
||||
std::snprintf(out, sizeof out, "%02x%02x%02x%02x", b[0], b[1], b[2], b[3]);
|
||||
return out;
|
||||
}
|
||||
|
||||
void refuses_unsafe_configurations(const Credentials& c) {
|
||||
const auto r = provision(141, 0x52);
|
||||
// The dev identity is refused outside dev, before anything is served.
|
||||
Child stage{c.args(r, 0, "stage")};
|
||||
CHECK(stage.wait_exit(15s) == 2 && stage.printed("error session-environment"));
|
||||
// Sessions without the synthetic carrier are a usage error.
|
||||
auto args = c.args(r, 0);
|
||||
args.erase(std::ranges::find(args, "--synthetic-inputs"),
|
||||
std::ranges::find(args, "--synthetic-inputs") + 2);
|
||||
Child usage{args};
|
||||
CHECK(usage.wait_exit(15s) == 2);
|
||||
}
|
||||
|
||||
void connects_acknowledges_and_recovers(const Credentials& c) {
|
||||
const auto r = provision(140, 0x51);
|
||||
std::uint16_t port = 0;
|
||||
auto client_grants = c.grants(r, 7, 6, 1000);
|
||||
std::vector<ClientEvent> events;
|
||||
std::uint64_t first_commit = 0;
|
||||
std::unique_ptr<network::SessionClient> session;
|
||||
{
|
||||
Child server{c.args(r, 0)};
|
||||
port = listening_port(server);
|
||||
CHECK(server.wait_for([](const std::string& l) { return l == "ready 1"; }, 15s));
|
||||
session = client(c, port, client_grants);
|
||||
CHECK(pump(server, {session.get()}, events,
|
||||
[&] { return session->latest_view().has_value(); }));
|
||||
const auto w = must(session->welcome()); // Copy: welcome() returns by value.
|
||||
CHECK(w.epoch == 1 && w.world == r.key.world && w.region == r.key.region);
|
||||
// Snapshot display: the region's real field stores, explicitly no individuals.
|
||||
CHECK(session->environment().freshness() == client::Freshness::current);
|
||||
CHECK(!session->environment().current()->cells().empty());
|
||||
CHECK(pump(server, {session.get()}, events,
|
||||
[&] { return session->organisms().current() != nullptr; }));
|
||||
CHECK(session->organisms().current()->cells().empty());
|
||||
CHECK(server.printed("session opened 1 subject=7"));
|
||||
|
||||
// Minimal admitted intention: durable acknowledgement, no biological effect.
|
||||
CHECK(session->send({1, 0, 1000, 0}));
|
||||
CHECK(pump(server, {session.get()}, events,
|
||||
[&] { return has(events, ClientEvent::Kind::outcome); }));
|
||||
CHECK(last(events, ClientEvent::Kind::result)->result.status ==
|
||||
network::IntentStatus::admitted);
|
||||
const auto* outcome = last(events, ClientEvent::Kind::outcome);
|
||||
CHECK(outcome->outcome.finality == network::IntentFinality::committed_no_effect);
|
||||
first_commit = outcome->outcome.commit_sequence;
|
||||
// The host acknowledged exactly that request ("INT1...") at that tick and commit.
|
||||
const std::string prefix = "ack " + hex({'I', 'N', 'T', '1'});
|
||||
const auto ack = std::ranges::find_if(
|
||||
server.lines(), [&](const std::string& l) { return starts(l, prefix); });
|
||||
CHECK(ack != server.lines().end());
|
||||
CHECK(ack->ends_with(" " + std::to_string(outcome->outcome.tick) + " " +
|
||||
std::to_string(outcome->outcome.commit_sequence)));
|
||||
// Rejected: a replay is never scheduled.
|
||||
events.clear();
|
||||
CHECK(session->send({1, 0, 0, 1000}));
|
||||
CHECK(pump(server, {session.get()}, events,
|
||||
[&] { return has(events, ClientEvent::Kind::result); }));
|
||||
CHECK(last(events, ClientEvent::Kind::result)->result.status ==
|
||||
network::IntentStatus::replayed);
|
||||
// Duplicate session for the same subject is refused; the first stays.
|
||||
auto twin = client(c, port, c.grants(r, 7, 1, 2000));
|
||||
std::vector<ClientEvent> twin_events;
|
||||
CHECK(pump(server, {twin.get(), session.get()}, twin_events,
|
||||
[&] { return has(twin_events, ClientEvent::Kind::closed); }));
|
||||
CHECK(last(twin_events, ClientEvent::Kind::closed)->reason ==
|
||||
network::CloseReason::duplicate_session);
|
||||
CHECK(session->phase() == network::SessionPhase::established);
|
||||
|
||||
// Pod restart analogue: SIGKILL. The client loses the session; any admitted
|
||||
// intention without a final outcome is reported unknown, never assumed.
|
||||
events.clear();
|
||||
CHECK(session->send({2, 0, -1000, 0}));
|
||||
server.signal(SIGKILL);
|
||||
CHECK(server.wait_exit(10s) == 128 + SIGKILL);
|
||||
}
|
||||
{
|
||||
const auto end = Clock::now() + 5s;
|
||||
while (Clock::now() < end && !has(events, ClientEvent::Kind::lost)) {
|
||||
session->poll(Clock::now());
|
||||
auto e = session->take_events();
|
||||
events.insert(events.end(), e.begin(), e.end());
|
||||
std::this_thread::sleep_for(5ms);
|
||||
}
|
||||
CHECK(has(events, ClientEvent::Kind::lost));
|
||||
CHECK(session->environment().freshness() == client::Freshness::stale);
|
||||
}
|
||||
// Successor on the same port: ready only after the lease expires; the client
|
||||
// reconnects with a fresh grant and a fresh baseline at the new epoch.
|
||||
Child successor{c.args(r, port)};
|
||||
CHECK(listening_port(successor) == port);
|
||||
CHECK(pump(
|
||||
successor, {session.get()}, events,
|
||||
[&] {
|
||||
return session->phase() == network::SessionPhase::established &&
|
||||
session->latest_view().has_value();
|
||||
},
|
||||
30s));
|
||||
CHECK(must(session->welcome()).epoch == 2 && must(session->welcome()).session == 1);
|
||||
CHECK(session->environment().freshness() == client::Freshness::current);
|
||||
events.clear();
|
||||
CHECK(session->send({3, 0, 0, -1000}));
|
||||
CHECK(pump(successor, {session.get()}, events,
|
||||
[&] { return has(events, ClientEvent::Kind::outcome); }));
|
||||
CHECK(last(events, ClientEvent::Kind::outcome)->outcome.commit_sequence > first_commit);
|
||||
successor.signal(SIGTERM);
|
||||
CHECK(successor.wait_exit(20s) == 0);
|
||||
std::printf("region_session port=%u first_commit=%llu grants_left=%zu\n",
|
||||
static_cast<unsigned>(port), static_cast<unsigned long long>(first_commit),
|
||||
session->grants_left());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void run_region_session_tests() {
|
||||
Credentials c;
|
||||
auto made = network::generate_dev_credentials(network::DeploymentEnvironment::dev, name, 1,
|
||||
std::chrono::system_clock::now());
|
||||
expect(made.has_value(), "credentials");
|
||||
c.made = std::move(*made);
|
||||
refuses_unsafe_configurations(c);
|
||||
connects_acknowledges_and_recovers(c);
|
||||
}
|
||||
+1
@@ -9,6 +9,7 @@
|
||||
#undef MBEDTLS_SSL_DTLS_CONNECTION_ID
|
||||
#undef MBEDTLS_SSL_DTLS_HELLO_VERIFY
|
||||
#undef MBEDTLS_SSL_RENEGOTIATION
|
||||
#undef MBEDTLS_SSL_CONTEXT_SERIALIZATION /* Live sessions are never saved or migrated. */
|
||||
#undef MBEDTLS_SSL_SESSION_TICKETS
|
||||
#undef MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED
|
||||
#undef MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED
|
||||
|
||||
@@ -43,7 +43,7 @@ case "$suite" in
|
||||
pattern="$common|$native|tests/|experiments/|tools/ci/linux-check\.sh|tools/ci/tsan-profile\.mjs|deploy/docker/Dockerfile"
|
||||
;;
|
||||
postgres|postgres-tools)
|
||||
pattern="$common|$native|tests/|$postgres_tools|deploy/docker/Dockerfile"
|
||||
pattern="$common|$native|tests/|$postgres_tools|third_party/mbedtls/|tools/prepare-mbedtls\.cmake|tools/mbedtls-pin\.cmake|deploy/docker/Dockerfile"
|
||||
;;
|
||||
deploy)
|
||||
pattern="$common|deploy/|tools/ops/|tools/backup/|tools/ci/deploy-policy-check\.py|tools/ci/monitoring-check\.sh|tools/ci/ops-lib-check\.sh"
|
||||
|
||||
@@ -235,6 +235,25 @@ for path, doc in objects:
|
||||
for variable in container.get('env', []):
|
||||
if variable.get('name') == 'REGION_INPUTS':
|
||||
check(variable.get('value') == '', f'{path}: synthetic inputs are never enabled in Git')
|
||||
# M9.7 (ADR-0083): dev sessions are a drill-time override as well.
|
||||
if variable.get('name') == 'REGION_SESSION':
|
||||
check(variable.get('value') == '', f'{path}: dev sessions are never enabled in Git')
|
||||
# M9.7 (ADR-0083): the session listener is loopback-only (kubectl port-forward), and its
|
||||
# Secret is optional, 0440 and never exposed through a Service port.
|
||||
for path, doc in objects:
|
||||
if path.startswith('deploy/kubernetes/region/') and doc['kind'] == 'Deployment' and not path.endswith('region-values.yaml'):
|
||||
pod = doc['spec']['template']['spec']
|
||||
script = ' '.join(pod['containers'][0].get('args', []))
|
||||
check('--session-listen 127.0.0.1:' in script and '0.0.0.0:7400' not in script,
|
||||
f'{path}: the session listener binds loopback only')
|
||||
for volume in pod.get('volumes', []):
|
||||
if volume.get('secret', {}).get('secretName') == 'evolution-dev-session':
|
||||
check(volume['secret'].get('optional') is True and volume['secret'].get('defaultMode') == 0o440,
|
||||
f'{path}: the dev session Secret is optional and 0440')
|
||||
if doc['kind'] == 'Service':
|
||||
for port in doc['spec'].get('ports', []):
|
||||
check(port.get('port') != 7400 and port.get('targetPort') not in (7400, 'session'),
|
||||
f'{path}: the dev session port is never exposed by a Service')
|
||||
for env in ('dev', 'stage'):
|
||||
overlay = f'deploy/kubernetes/region/{env}/'
|
||||
images = manifests.get(overlay + 'kustomization.yaml', [{}])[0].get('images', [])
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
#!/usr/bin/env bash
|
||||
# M9.7 dev session drill (ADR-0083) for evolution-dev only.
|
||||
#
|
||||
# tools/ops/session-drill.sh <credentials-dir> [--context <name>] [--subject <n>]
|
||||
#
|
||||
# Requires: kubectl access to evolution-dev, an `evo-dev-session` binary (EVO_DEV_SESSION,
|
||||
# default: on PATH) and an operator credentials directory made by
|
||||
# evo-dev-session credentials --environment dev --server-name <name> --out <dir>
|
||||
# whose server.pem/server.key/grant.key are already in the `evolution-dev-session`
|
||||
# Secret (docs/networking/DEV_SESSION_OPERATIONS.md). The grant key never leaves the
|
||||
# operator machine; the drill issues a short-lived single-use grant bundle locally.
|
||||
#
|
||||
# Steps: enable synthetic inputs and dev sessions on evo-region (`kubectl set env`),
|
||||
# wait for Ready, port-forward 127.0.0.1:<local> to the pod's loopback listener, run
|
||||
# `evo-dev-session probe --expect-restart`: one durable no-effect acknowledgement,
|
||||
# then a forced pod deletion (the "server pod restart"), reconnection at a newer epoch
|
||||
# with a fresh baseline, and a second durable acknowledgement. Everything is reverted
|
||||
# on exit (including on failure) to the Git values, so Argo returns to Synced.
|
||||
# Stage and World0 are refused before anything touches the cluster.
|
||||
set -euo pipefail
|
||||
usage() { echo "usage: $0 <credentials-dir> [--context <name>] [--subject <n>]" >&2; exit 2; }
|
||||
[ $# -ge 1 ] || usage
|
||||
creds="$1"; shift
|
||||
k=(kubectl --request-timeout=30s)
|
||||
subject=7
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
--context) [ -n "${2:-}" ] || usage; k+=(--context "$2"); shift 2 ;;
|
||||
--subject) [ -n "${2:-}" ] || usage; subject="$2"; shift 2 ;;
|
||||
*) usage ;;
|
||||
esac
|
||||
done
|
||||
ns=evolution-dev # Fixed: the dev identity exists nowhere else.
|
||||
tool="${EVO_DEV_SESSION:-evo-dev-session}"
|
||||
for f in dev-ca.pem server.pem grant.key; do
|
||||
[ -f "$creds/$f" ] || { echo "missing $creds/$f" >&2; exit 2; }
|
||||
done
|
||||
command -v "$tool" > /dev/null || { echo "evo-dev-session not found (set EVO_DEV_SESSION)" >&2; exit 2; }
|
||||
selector=app.kubernetes.io/name=evo-region
|
||||
work="$(mktemp -d)"
|
||||
chmod 700 "$work"
|
||||
forward_pid=""
|
||||
say() { echo "$(date -u +%H:%M:%S) $*"; }
|
||||
fail() { say "FAIL $*"; exit 1; }
|
||||
"${k[@]}" get secret -n "$ns" evolution-dev-session > /dev/null ||
|
||||
fail "Secret evolution-dev-session is missing in $ns (see DEV_SESSION_OPERATIONS.md)"
|
||||
server_name="$(openssl x509 -noout -subject -nameopt RFC2253 -in "$creds/server.pem" 2> /dev/null |
|
||||
sed -n 's/^subject=CN=//p')"
|
||||
[ -n "$server_name" ] || fail "cannot read the server name from $creds/server.pem"
|
||||
ready_pod() { # Waits up to $2 seconds for a Ready pod other than $1.
|
||||
local old="$1" limit="${2:-180}"
|
||||
for _ in $(seq 1 $((limit * 5))); do
|
||||
for p in $("${k[@]}" get pods -n "$ns" -l "$selector" \
|
||||
-o go-template='{{range .items}}{{if not .metadata.deletionTimestamp}}{{.metadata.name}}{{"\n"}}{{end}}{{end}}'); do
|
||||
[ "$p" = "$old" ] && continue
|
||||
[ "$("${k[@]}" get pod -n "$ns" "$p" -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}' 2> /dev/null)" = True ] &&
|
||||
{ echo "$p"; return 0; }
|
||||
done
|
||||
sleep 0.2
|
||||
done
|
||||
return 1
|
||||
}
|
||||
forward() { # forward <pod>: local 17400 -> pod loopback 7400 (no Service involved).
|
||||
[ -n "$forward_pid" ] && kill "$forward_pid" 2> /dev/null || true
|
||||
"${k[@]}" port-forward -n "$ns" "pod/$1" 17400:7400 > "$work/forward-$1.log" 2>&1 &
|
||||
forward_pid=$!
|
||||
}
|
||||
restore() {
|
||||
set +e
|
||||
[ -n "$forward_pid" ] && kill "$forward_pid" 2> /dev/null
|
||||
say "restoring the Git configuration of evo-region"
|
||||
"${k[@]}" set env -n "$ns" deployment/evo-region REGION_INPUTS= REGION_SESSION= > /dev/null
|
||||
ready_pod "" 180 > /dev/null && say "restored: evo-region Ready" || say "WARN evo-region not Ready after restore"
|
||||
rm -f "$work/grants.bin"
|
||||
say "logs kept in $work"
|
||||
}
|
||||
trap restore EXIT
|
||||
|
||||
world="$("${k[@]}" get deployment -n "$ns" evo-region -o jsonpath='{.spec.template.spec.containers[0].env[?(@.name=="REGION_WORLD")].value}')"
|
||||
region="$("${k[@]}" get deployment -n "$ns" evo-region -o jsonpath='{.spec.template.spec.containers[0].env[?(@.name=="REGION_ID")].value}')"
|
||||
frame="$("${k[@]}" get deployment -n "$ns" evo-region -o jsonpath='{.spec.template.spec.containers[0].env[?(@.name=="REGION_FRAME")].value}')"
|
||||
"$tool" grants --environment dev --key "$creds/grant.key" --world "$world" --frame "$frame" \
|
||||
--region "$region" --subject "$subject" --count 8 --lifetime 1800 --out "$work/grants.bin" > /dev/null ||
|
||||
fail "could not issue the grant bundle"
|
||||
|
||||
say "enabling synthetic inputs and dev sessions in $ns (world $world region $region)"
|
||||
before="$("${k[@]}" get pods -n "$ns" -l "$selector" -o jsonpath='{.items[0].metadata.name}')"
|
||||
"${k[@]}" set env -n "$ns" deployment/evo-region REGION_INPUTS=development-v1 REGION_SESSION=dev > /dev/null
|
||||
pod="$(ready_pod "$before" 180)" || fail "no Ready evo-region pod with sessions"
|
||||
"${k[@]}" logs -n "$ns" -f "$pod" > "$work/$pod.log" 2> /dev/null &
|
||||
forward "$pod"
|
||||
sleep 2
|
||||
"$tool" probe --connect 127.0.0.1:17400 --server-name "$server_name" --ca "$creds/dev-ca.pem" \
|
||||
--grants "$work/grants.bin" --expect-restart --timeout 300 > "$work/probe.log" 2>&1 &
|
||||
probe_pid=$!
|
||||
for _ in $(seq 1 600); do grep -q "probe awaiting-restart" "$work/probe.log" && break
|
||||
kill -0 "$probe_pid" 2> /dev/null || break; sleep 0.2; done
|
||||
grep -q "probe awaiting-restart" "$work/probe.log" || { cat "$work/probe.log"; fail "first acknowledgement not observed"; }
|
||||
grep "^probe durable" "$work/probe.log" | head -1
|
||||
say "deleting $pod (server pod restart)"
|
||||
started=$(date +%s%3N)
|
||||
"${k[@]}" delete pod -n "$ns" "$pod" --wait=false > /dev/null
|
||||
successor="$(ready_pod "$pod" 180)" || fail "no successor became Ready"
|
||||
say "successor $successor Ready after $(( $(date +%s%3N) - started )) ms"
|
||||
"${k[@]}" logs -n "$ns" -f "$successor" > "$work/$successor.log" 2> /dev/null &
|
||||
forward "$successor"
|
||||
wait "$probe_pid" && verdict=0 || verdict=$?
|
||||
cat "$work/probe.log"
|
||||
[ "$verdict" = 0 ] || fail "probe failed (exit $verdict)"
|
||||
grep -q "^session opened" "$work/$successor.log" || say "WARN successor log shows no session (log stream lag?)"
|
||||
say "PASS session drill: durable acknowledgement before and after a pod restart, new epoch, fresh baseline"
|
||||
@@ -1,5 +1,5 @@
|
||||
param(
|
||||
[ValidateSet('debug', 'release', 'analysis', 'asan', 'tsan', 'client', 'client-probe', 'network')]
|
||||
[ValidateSet('debug', 'release', 'analysis', 'asan', 'tsan', 'client', 'client-probe', 'network', 'client-network')]
|
||||
[string]$Preset = 'debug',
|
||||
[ValidateSet('configure', 'build', 'test', 'format', 'check')]
|
||||
[string]$Action = 'check'
|
||||
|
||||
Reference in New Issue
Block a user