Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m45s
Foundation / checks (network) (push) Successful in 9m37s
Foundation / checks (client) (push) Successful in 12m14s
Foundation / checks (tsan) (push) Successful in 7m39s
Foundation / checks (checks) (push) Successful in 31m15s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 32m59s
Session protocol v1 (not yet deployed, amended in place) gains realtime intentions on datagrams with receipts and a cumulative simulated watermark, and account-scoped actions with stable ids, receipts, final outcomes and queries. IntentAdmission enforces session binding, rate, authority, epoch, vector, sequence, tick window, duplicates and the unresolved bound; RegionSessions and the client carry it over QUIC. Intentions and actions have no biological effect yet. ADR-0092. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
533 lines
26 KiB
C++
533 lines
26 KiB
C++
#include "evo/identity/identity.hpp"
|
|
#include "evo/network/dev_grant.hpp"
|
|
#include "evo/network/intent_admission.hpp"
|
|
#include "evo/network/session_control.hpp"
|
|
#include "evo/network/session_grant.hpp"
|
|
#include "evo/network/session_protocol.hpp"
|
|
#include "network_test.hpp"
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <cstdint>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace evo::network_test {
|
|
namespace {
|
|
using namespace evo::network;
|
|
using evo::identity::IdentityError;
|
|
|
|
constexpr world::WorldIdentity world_id{7, 3};
|
|
constexpr std::uint64_t region = 11;
|
|
constexpr std::uint64_t now = 1'800'000'000;
|
|
|
|
AccountId account(std::uint8_t tag) {
|
|
AccountId a;
|
|
a.bytes.fill(tag);
|
|
return a;
|
|
}
|
|
GrantClaims claims(const AccountId& who, std::uint64_t serial, std::uint64_t issued = now,
|
|
std::uint64_t lifetime = 60) {
|
|
GrantClaims c;
|
|
c.grant_id = 1000 + serial;
|
|
c.account = who;
|
|
c.serial = serial;
|
|
c.world = world_id;
|
|
c.region = region;
|
|
c.issued_at = issued;
|
|
c.expires_at = issued + lifetime;
|
|
return c;
|
|
}
|
|
std::span<const std::uint8_t> bytes(std::string_view text) {
|
|
return {reinterpret_cast<const std::uint8_t*>(text.data()), text.size()};
|
|
}
|
|
|
|
// One environment's issuer key and region verifier.
|
|
struct Keys {
|
|
explicit Keys(DeploymentEnvironment e, std::uint32_t key_id = 1)
|
|
: signer{*GrantSigningKey::generate(e, key_id)}, verifier{*GrantVerifier::create(e)} {
|
|
CHECK(verifier.add_key(key_id, *signer.public_key()));
|
|
}
|
|
VerifiedSessionGrant verified(const GrantClaims& c) const {
|
|
auto grant = signer.sign(c);
|
|
CHECK(grant);
|
|
auto v = verifier.verify(*grant);
|
|
CHECK(v);
|
|
return *v;
|
|
}
|
|
GrantSigningKey signer;
|
|
GrantVerifier verifier;
|
|
};
|
|
|
|
ControlConfig config() {
|
|
ControlConfig c;
|
|
c.environment = DeploymentEnvironment::dev;
|
|
c.world = world_id;
|
|
c.region = region;
|
|
c.epoch = 5;
|
|
c.max_sessions = 4;
|
|
c.max_accounts = 8;
|
|
return c;
|
|
}
|
|
|
|
void grants() {
|
|
Keys dev{DeploymentEnvironment::dev};
|
|
const auto who = account(0x42);
|
|
const auto grant = *dev.signer.sign(claims(who, 1));
|
|
const auto v = dev.verifier.verify(grant);
|
|
CHECK(v && v->claims().account == who && v->claims().serial == 1 &&
|
|
v->claims().world == world_id && v->claims().region == region &&
|
|
v->environment() == DeploymentEnvironment::dev);
|
|
// Every signed byte matters, including the key id and environment.
|
|
for (std::size_t i = 0; i < session_grant_bytes; ++i) {
|
|
auto tampered = grant;
|
|
tampered.at(i) ^= 0x01;
|
|
const auto r = dev.verifier.verify(tampered);
|
|
CHECK(!r);
|
|
CHECK(r.error() == (i >= 20 && i < 24 ? SessionGrantError::unknown_key
|
|
: SessionGrantError::bad_signature));
|
|
}
|
|
CHECK(refused(dev.verifier.verify(std::span{grant}.first(159)), SessionGrantError::malformed));
|
|
// A dev-signed grant never verifies in stage or World0, even if a dev public key were
|
|
// misconfigured there: the environment byte is signed.
|
|
for (const auto e : {DeploymentEnvironment::stage, DeploymentEnvironment::world0}) {
|
|
auto other = *GrantVerifier::create(e);
|
|
CHECK(refused(other.verify(grant), SessionGrantError::unknown_key));
|
|
CHECK(other.add_key(1, *dev.signer.public_key()));
|
|
CHECK(refused(other.verify(grant), SessionGrantError::wrong_environment));
|
|
}
|
|
// M9 dev grants are not session grants.
|
|
std::array<std::uint8_t, dev_key_bytes> secret{};
|
|
secret.fill(9);
|
|
const auto dev_key = *DevGrantKey::import(DeploymentEnvironment::dev, secret);
|
|
DevGrantClaims dc;
|
|
dc.grant_id = 1;
|
|
dc.subject = 2;
|
|
dc.world = world_id;
|
|
dc.region = region;
|
|
dc.issued_at = now;
|
|
dc.expires_at = now + 60;
|
|
CHECK(refused(dev.verifier.verify(*dev_key.issue(dc)), SessionGrantError::malformed));
|
|
// Rotation: a second key verifies alongside; ids are unique; at most 8 keys.
|
|
Keys rotated{DeploymentEnvironment::dev, 2};
|
|
CHECK(dev.verifier.add_key(2, *rotated.signer.public_key()));
|
|
CHECK(dev.verifier.verify(*rotated.signer.sign(claims(who, 2))));
|
|
CHECK(refused(dev.verifier.add_key(2, *rotated.signer.public_key()),
|
|
SessionGrantError::duplicate_key));
|
|
for (std::uint32_t id = 3; id <= GrantVerifier::max_keys; ++id)
|
|
CHECK(dev.verifier.add_key(id, *rotated.signer.public_key()));
|
|
CHECK(refused(dev.verifier.add_key(99, *rotated.signer.public_key()),
|
|
SessionGrantError::too_many_keys));
|
|
// A grant signed by key 2 but labelled key 1 fails (the label is signed too).
|
|
auto relabelled = *rotated.signer.sign(claims(who, 3));
|
|
relabelled.at(20) = 1;
|
|
CHECK(refused(dev.verifier.verify(relabelled), SessionGrantError::bad_signature));
|
|
GrantPublicKey not_a_point{};
|
|
not_a_point.fill(0x04);
|
|
auto fresh = *GrantVerifier::create(DeploymentEnvironment::dev);
|
|
CHECK(refused(fresh.add_key(1, not_a_point), SessionGrantError::invalid_key));
|
|
CHECK(refused(fresh.add_key(0, *dev.signer.public_key()), SessionGrantError::invalid_key));
|
|
// Structural claims and key import.
|
|
auto bad = claims(who, 1);
|
|
bad.serial = 0;
|
|
CHECK(refused(dev.signer.sign(bad), SessionGrantError::invalid_claims));
|
|
bad = claims(who, 1);
|
|
bad.account = {};
|
|
CHECK(refused(dev.signer.sign(bad), SessionGrantError::invalid_claims));
|
|
std::array<std::uint8_t, grant_private_key_bytes> scalar{};
|
|
scalar.back() = 7;
|
|
auto imported = GrantSigningKey::import(DeploymentEnvironment::stage, 4, scalar);
|
|
CHECK(imported);
|
|
auto stage = *GrantVerifier::create(DeploymentEnvironment::stage);
|
|
CHECK(stage.add_key(4, *imported->public_key()));
|
|
CHECK(stage.verify(*imported->sign(claims(who, 1))));
|
|
CHECK(refused(
|
|
GrantSigningKey::import(DeploymentEnvironment::stage, 4, std::span{scalar}.first(31)),
|
|
SessionGrantError::invalid_key));
|
|
scalar.fill(0);
|
|
CHECK(refused(GrantSigningKey::import(DeploymentEnvironment::stage, 4, scalar),
|
|
SessionGrantError::invalid_key));
|
|
// Moved-from keys are inert.
|
|
auto moved = std::move(imported).value();
|
|
CHECK(refused(imported->sign(claims(who, 1)), // NOLINT(bugprone-use-after-move)
|
|
SessionGrantError::invalid_key));
|
|
CHECK(moved.sign(claims(who, 1)));
|
|
}
|
|
|
|
void identity_adapter() {
|
|
using namespace evo::identity;
|
|
for (const auto e : {DeploymentEnvironment::stage, DeploymentEnvironment::world0})
|
|
CHECK(refused(DevTestProvider::create(e), IdentityError::not_dev_environment));
|
|
auto provider = *DevTestProvider::create(DeploymentEnvironment::dev);
|
|
CHECK(provider.authenticate(bytes("dev-test:alice"))->subject == "alice");
|
|
for (const auto bad : {"alice", "dev-test:", "dev-test:Alice", "dev-test:a b",
|
|
"dev-test:0123456789abcdef0123456789abcdefX", "steam:alice"})
|
|
CHECK(refused(provider.authenticate(bytes(bad)), IdentityError::unauthenticated));
|
|
|
|
MemoryAccountDirectory directory{2};
|
|
const ProviderSubject alice{ProviderKind::dev_test, "alice"};
|
|
const auto a1 = *directory.resolve(alice);
|
|
CHECK(a1.valid() && *directory.resolve(alice) == a1);
|
|
const auto b = *directory.resolve({ProviderKind::dev_test, "bob"});
|
|
CHECK(b != a1);
|
|
CHECK(refused(directory.resolve({ProviderKind::dev_test, "carol"}),
|
|
IdentityError::directory_full));
|
|
const auto s1 = *directory.next_grant(a1, now);
|
|
const auto s2 = *directory.next_grant(a1, now - 100); // Clock went back.
|
|
CHECK(s1.serial == 1 && s2.serial == 2 && s2.issued_at == now);
|
|
|
|
// End to end: credential -> account -> signed grant -> region control.
|
|
Keys dev{DeploymentEnvironment::dev};
|
|
MemoryAccountDirectory accounts{16};
|
|
auto issuer = GrantIssuer::create(std::move(dev.signer), provider, accounts, 60);
|
|
CHECK(issuer);
|
|
auto control = *SessionControl::create(config());
|
|
const auto g1 = issuer->issue(bytes("dev-test:alice"), world_id, region, now);
|
|
const auto opened = control.open(*dev.verifier.verify(*g1), now);
|
|
CHECK(opened && opened->superseded == 0);
|
|
const auto g2 = issuer->issue(bytes("dev-test:alice"), world_id, region, now + 1);
|
|
const auto again = control.open(*dev.verifier.verify(*g2), now + 1);
|
|
CHECK(again && again->superseded == opened->session);
|
|
CHECK(refused(issuer->issue(bytes("nobody"), world_id, region, now),
|
|
IdentityError::unauthenticated));
|
|
CHECK(refused(issuer->issue(bytes("dev-test:alice"), {0, 0}, region, now),
|
|
IdentityError::invalid_request));
|
|
// A dev provider cannot drive a stage or World0 key.
|
|
for (const auto e : {DeploymentEnvironment::stage, DeploymentEnvironment::world0})
|
|
CHECK(refused(GrantIssuer::create(*GrantSigningKey::generate(e, 1), provider, accounts, 60),
|
|
IdentityError::wrong_environment));
|
|
CHECK(refused(GrantIssuer::create(*GrantSigningKey::generate(DeploymentEnvironment::dev, 1),
|
|
provider, accounts, 0),
|
|
IdentityError::invalid_request));
|
|
}
|
|
|
|
void control_rules() {
|
|
Keys dev{DeploymentEnvironment::dev};
|
|
const auto alice = account(1), bob = account(2);
|
|
{
|
|
auto bad = config();
|
|
bad.max_accounts = 2; // Fewer than sessions.
|
|
CHECK(refused(SessionControl::create(bad), ControlError::invalid_config));
|
|
bad = config();
|
|
bad.max_grant_lifetime = 3601;
|
|
CHECK(refused(SessionControl::create(bad), ControlError::invalid_config));
|
|
}
|
|
// Scope and time.
|
|
{
|
|
auto control = *SessionControl::create(config());
|
|
auto c = claims(alice, 1);
|
|
c.world = {7, 4};
|
|
CHECK(refused(control.open(dev.verified(c), now), ControlError::wrong_world));
|
|
c = claims(alice, 1);
|
|
c.region = 12;
|
|
CHECK(refused(control.open(dev.verified(c), now), ControlError::wrong_region));
|
|
CHECK(refused(control.open(dev.verified(claims(alice, 1, now, 301)), now),
|
|
ControlError::invalid_lifetime));
|
|
CHECK(refused(control.open(dev.verified(claims(alice, 1, now + 31)), now),
|
|
ControlError::not_yet_valid));
|
|
CHECK(control.open(dev.verified(claims(alice, 1, now + 30)), now));
|
|
CHECK(refused(control.open(dev.verified(claims(bob, 1)), now + 60), ControlError::expired));
|
|
// Grants verified for another environment are refused by this region.
|
|
Keys stage{DeploymentEnvironment::stage};
|
|
CHECK(refused(control.open(stage.verified(claims(bob, 1)), now),
|
|
ControlError::wrong_environment));
|
|
}
|
|
// Replay, duplicate login (newest wins) and stolen stale grants.
|
|
{
|
|
auto c = config();
|
|
c.max_account_attempts = 16; // Rate limits are tested separately.
|
|
auto control = *SessionControl::create(c);
|
|
const auto first = dev.verified(claims(alice, 5));
|
|
const auto s1 = control.open(first, now);
|
|
CHECK(s1 && s1->superseded == 0 && s1->epoch == 5 && s1->expires_at == now + 60);
|
|
CHECK(refused(control.open(first, now + 1), ControlError::stale_grant)); // Replay.
|
|
// An older, unexpired grant (e.g. stolen earlier) cannot take control.
|
|
CHECK(refused(control.open(dev.verified(claims(alice, 4)), now + 1),
|
|
ControlError::stale_grant));
|
|
CHECK(control.holder(alice) == s1->session);
|
|
const auto s2 = control.open(dev.verified(claims(alice, 6)), now + 2);
|
|
CHECK(s2 && s2->superseded == s1->session && s2->session != s1->session);
|
|
CHECK(control.holder(alice) == s2->session && control.sessions() == 1);
|
|
CHECK(!control.account_of(s1->session));
|
|
CHECK(refused(control.close(s1->session), ControlError::unknown_session));
|
|
// After close, the stale serial stays refused until every older grant expired.
|
|
CHECK(control.close(s2->session));
|
|
CHECK(refused(control.open(dev.verified(claims(alice, 6)), now + 3),
|
|
ControlError::stale_grant));
|
|
}
|
|
// Rate limits: account attempts (every verified attempt counts) and region attempts.
|
|
{
|
|
auto c = config();
|
|
c.max_account_attempts = 3;
|
|
c.max_region_attempts = 5;
|
|
c.attempt_window = 10;
|
|
auto control = *SessionControl::create(c);
|
|
const auto replayed = dev.verified(claims(alice, 1));
|
|
CHECK(control.open(replayed, now));
|
|
CHECK(refused(control.open(replayed, now), ControlError::stale_grant));
|
|
CHECK(refused(control.open(replayed, now), ControlError::stale_grant));
|
|
CHECK(
|
|
refused(control.open(dev.verified(claims(alice, 2)), now), ControlError::rate_limited));
|
|
CHECK(control.open(dev.verified(claims(alice, 2)), now + 10)); // New window.
|
|
for (int i = 0; i < 5; ++i)
|
|
CHECK(control.attempt(now));
|
|
CHECK(refused(control.attempt(now), ControlError::rate_limited));
|
|
CHECK(refused(control.attempt(now + 9), ControlError::rate_limited));
|
|
CHECK(control.attempt(now + 10));
|
|
}
|
|
// Capacity: a refused open does not consume the grant; accounts fail closed.
|
|
{
|
|
auto c = config();
|
|
c.max_sessions = 1;
|
|
c.max_accounts = 2;
|
|
auto control = *SessionControl::create(c);
|
|
const auto a = control.open(dev.verified(claims(alice, 1)), now);
|
|
const auto bob_grant = dev.verified(claims(bob, 1));
|
|
CHECK(refused(control.open(bob_grant, now), ControlError::sessions_full));
|
|
CHECK(control.close(a->session));
|
|
CHECK(control.open(bob_grant, now)); // Same grant, now admitted.
|
|
CHECK(refused(control.open(dev.verified(claims(account(3), 1)), now),
|
|
ControlError::accounts_full));
|
|
// Idle accounts are forgotten only after their grants and windows end.
|
|
CHECK(refused(control.open(dev.verified(claims(account(3), 1, now + 299)), now + 299),
|
|
ControlError::accounts_full));
|
|
const auto later = dev.verified(claims(account(3), 1, now + 300));
|
|
CHECK(refused(control.open(later, now + 300), ControlError::sessions_full)); // Bob holds.
|
|
}
|
|
// Refresh, expiry and epochs.
|
|
{
|
|
auto control = *SessionControl::create(config());
|
|
const auto a = control.open(dev.verified(claims(alice, 1)), now);
|
|
const auto b = control.open(dev.verified(claims(bob, 1)), now);
|
|
CHECK(control.refresh(a->session, dev.verified(claims(alice, 2, now + 50)), now + 50) ==
|
|
now + 110);
|
|
CHECK(refused(control.refresh(a->session, dev.verified(claims(alice, 2)), now + 51),
|
|
ControlError::stale_grant));
|
|
CHECK(refused(control.refresh(a->session, dev.verified(claims(bob, 2)), now + 51),
|
|
ControlError::wrong_account));
|
|
CHECK(refused(control.refresh(999, dev.verified(claims(alice, 3)), now + 51),
|
|
ControlError::unknown_session));
|
|
const auto expired = control.expire(now + 60);
|
|
CHECK(expired == std::vector<std::uint64_t>{b->session});
|
|
CHECK(control.holder(alice) == a->session && !control.holder(bob));
|
|
const auto c2 = control.open(dev.verified(claims(bob, 2, now + 60)), now + 60);
|
|
auto closed = control.advance_epoch(6);
|
|
CHECK(closed && closed->size() == 2 && control.sessions() == 0 && control.epoch() == 6);
|
|
CHECK(std::ranges::find(*closed, c2->session) != closed->end());
|
|
CHECK(refused(control.advance_epoch(6), ControlError::invalid_config));
|
|
const auto reopened = control.open(dev.verified(claims(alice, 3, now + 61)), now + 61);
|
|
CHECK(reopened && reopened->epoch == 6 && reopened->superseded == 0);
|
|
}
|
|
// Redaction: the log label carries only the first four id bytes.
|
|
AccountId labelled;
|
|
for (std::size_t i = 0; i < labelled.bytes.size(); ++i)
|
|
labelled.bytes.at(i) = static_cast<std::uint8_t>(0xA0 + i);
|
|
CHECK(log_label(labelled) == "acct-a0a1a2a3");
|
|
}
|
|
void session_codec() {
|
|
WireGrant grant{};
|
|
for (std::size_t i = 0; i < grant.size(); ++i)
|
|
grant.at(i) = static_cast<std::uint8_t>(i * 7);
|
|
SessionWelcome welcome{1, 2, 3, 4, 5, 6, 7, 8};
|
|
const std::vector<SessionMessage> messages{
|
|
SessionHello{0, grant},
|
|
welcome,
|
|
SessionRefresh{grant},
|
|
SessionRefreshResult{0, 99},
|
|
SessionRefreshResult{static_cast<std::uint8_t>(SessionCloseReason::stale_grant), 0},
|
|
SessionClose{SessionCloseReason::superseded},
|
|
SessionClose{SessionCloseReason::shutdown},
|
|
SessionIntention{1, 2, 3, 4, -1000, 7},
|
|
IntentionReceipt{3, AdmitStatus::not_ready, 9},
|
|
SimulatedThrough{2, 3, 4},
|
|
SessionAction{1, 2, 77, 1, 5},
|
|
ActionReceipt{77, AdmitStatus::duplicate, 4, 5},
|
|
ActionOutcome{77, ActionResult::committed_no_effect, 5, 12},
|
|
ActionQuery{77}};
|
|
for (const auto& m : messages) {
|
|
const auto bytes = encode_session(m);
|
|
CHECK(bytes.at(0) == 'E' && bytes.at(1) == 'V' && bytes.at(2) == 'S' && bytes.at(3) == 'N');
|
|
const auto back = decode_session(bytes);
|
|
CHECK(back && back->index() == m.index());
|
|
CHECK(encode_session(*back) == bytes);
|
|
// Every truncation and any extension is refused.
|
|
for (std::size_t n = 0; n < bytes.size(); ++n) {
|
|
const auto r = decode_session(std::span{bytes}.first(n));
|
|
CHECK(!r && r.error() == (n < session_header_bytes ? SessionCodecError::truncated
|
|
: SessionCodecError::wrong_length));
|
|
}
|
|
auto longer = bytes;
|
|
longer.push_back(0);
|
|
CHECK(refused(decode_session(longer), SessionCodecError::wrong_length));
|
|
auto mutated = bytes;
|
|
mutated.at(0) ^= 1;
|
|
CHECK(refused(decode_session(mutated), SessionCodecError::bad_magic));
|
|
mutated = bytes;
|
|
mutated.at(4) = 2;
|
|
CHECK(refused(decode_session(mutated), SessionCodecError::unsupported_version));
|
|
mutated = bytes;
|
|
mutated.at(7) = 1;
|
|
CHECK(refused(decode_session(mutated), SessionCodecError::reserved_flags));
|
|
for (const auto type : std::array<std::uint8_t, 3>{0, 13, 255}) {
|
|
mutated = bytes;
|
|
mutated.at(6) = type;
|
|
CHECK(refused(decode_session(mutated), SessionCodecError::unknown_type));
|
|
}
|
|
}
|
|
CHECK(std::get<SessionWelcome>(*decode_session(encode_session(welcome))) == welcome);
|
|
CHECK(std::get<SessionHello>(*decode_session(encode_session(SessionHello{0, grant}))).grant ==
|
|
grant);
|
|
for (const auto reason : std::array<std::uint8_t, 4>{0, 14, 200, 255}) {
|
|
auto close = encode_session(SessionClose{SessionCloseReason::protocol});
|
|
close.back() = reason;
|
|
CHECK(refused(decode_session(close), SessionCodecError::invalid_value));
|
|
if (reason != 0) {
|
|
auto result = encode_session(SessionRefreshResult{0, 0});
|
|
result.at(session_header_bytes) = reason;
|
|
CHECK(refused(decode_session(result), SessionCodecError::invalid_value));
|
|
}
|
|
}
|
|
// Decoded values survive exactly (signed vectors, every field).
|
|
const SessionIntention intent{9, 8, 7, 6, -1000, 999};
|
|
CHECK(std::get<SessionIntention>(*decode_session(encode_session(intent))) == intent);
|
|
const ActionOutcome outcome{5, ActionResult::not_committed, 0, 0};
|
|
CHECK(std::get<ActionOutcome>(*decode_session(encode_session(outcome))) == outcome);
|
|
// A state claim appended to an intention (position, energy) is not a valid message.
|
|
auto claimed = encode_session(intent);
|
|
for (int i = 0; i < 16; ++i)
|
|
claimed.push_back(0x7F);
|
|
CHECK(refused(decode_session(claimed), SessionCodecError::wrong_length));
|
|
for (const auto bad : std::array<std::uint8_t, 3>{11, 200, 255}) {
|
|
auto receipt = encode_session(IntentionReceipt{1, AdmitStatus::admitted, 1});
|
|
receipt.at(session_header_bytes + 8) = bad;
|
|
CHECK(refused(decode_session(receipt), SessionCodecError::invalid_value));
|
|
}
|
|
for (const auto bad : std::array<std::uint8_t, 3>{0, 5, 255}) {
|
|
auto o = encode_session(outcome);
|
|
o.at(session_header_bytes + 8) = bad;
|
|
CHECK(refused(decode_session(o), SessionCodecError::invalid_value));
|
|
}
|
|
for (const auto zero : {0, 1}) {
|
|
auto w = welcome;
|
|
(zero == 0 ? w.session : w.epoch) = 0;
|
|
CHECK(refused(decode_session(encode_session(w)), SessionCodecError::invalid_value));
|
|
}
|
|
}
|
|
void intent_admission() {
|
|
IntentLimits limits;
|
|
limits.max_unresolved = 6;
|
|
limits.max_finals = 4;
|
|
CHECK(refused(IntentAdmission::create(IntentLimits{0}), IntentConfigError::invalid));
|
|
auto a = *IntentAdmission::create(limits);
|
|
const auto alice = account(1), bob = account(2);
|
|
CHECK(a.open(1, alice) && a.open(2, bob) && !a.open(1, alice) && !a.open(0, alice));
|
|
AdmissionFacts f{true, 5, 100};
|
|
const auto intention = [&](std::uint64_t session, std::uint64_t seq, std::uint64_t tick,
|
|
std::int16_t x = 0, std::uint64_t epoch = 5) {
|
|
return a.intention(session, SessionIntention{session, epoch, seq, tick, x, 0}, f).status;
|
|
};
|
|
// Every refusal, in the order admission applies them.
|
|
CHECK(intention(1, 1, 101) == AdmitStatus::admitted);
|
|
CHECK(a.intention(1, SessionIntention{2, 5, 2, 102, 0, 0}, f).status ==
|
|
AdmitStatus::unknown_session); // Names another session.
|
|
CHECK(a.intention(9, SessionIntention{9, 5, 2, 102, 0, 0}, f).status ==
|
|
AdmitStatus::unknown_session); // No such session.
|
|
CHECK(intention(1, 2, 102, 0, 4) == AdmitStatus::stale_epoch);
|
|
CHECK(intention(1, 2, 102, 1001) == AdmitStatus::invalid_vector);
|
|
CHECK(intention(1, 1, 102) == AdmitStatus::replayed);
|
|
CHECK(intention(1, 2, 100) == AdmitStatus::outside_window); // Not in the future.
|
|
CHECK(intention(1, 2, 109) == AdmitStatus::outside_window); // Beyond 8 ticks.
|
|
CHECK(intention(1, 2, 101) == AdmitStatus::outside_window); // Same tick as before.
|
|
CHECK(intention(1, 2, 102) == AdmitStatus::rate_limited); // 8 attempts this window.
|
|
f.server_tick = 108; // New window.
|
|
f.ready = false;
|
|
CHECK(intention(1, 2, 109) == AdmitStatus::not_ready);
|
|
f.ready = true;
|
|
CHECK(intention(1, 2, 109) == AdmitStatus::admitted);
|
|
CHECK(intention(1, 3, 110) == AdmitStatus::admitted);
|
|
// Simulated watermark: only intentions whose tick ran, cumulatively.
|
|
f.server_tick = 109;
|
|
auto marks = a.simulated(f);
|
|
CHECK(marks.size() == 1 && marks[0].first == 1 &&
|
|
marks[0].second == SimulatedThrough{5, 2, 109});
|
|
CHECK(a.simulated(f).empty()); // Unchanged: not repeated yet.
|
|
f.server_tick = 110;
|
|
marks = a.simulated(f);
|
|
CHECK(marks.size() == 1 && marks[0].second.sequence == 3);
|
|
f.server_tick = 130;
|
|
marks = a.simulated(f); // Repeated after 20 ticks so a lost datagram is covered.
|
|
CHECK(marks.size() == 1 && marks[0].second == SimulatedThrough{5, 3, 130});
|
|
|
|
// Actions: receipt, carried with its tick's batch, final after the commit.
|
|
const auto action = [&](std::uint64_t session, std::uint64_t id, std::uint64_t tick,
|
|
std::uint16_t kind = 1) {
|
|
return a.action(session, SessionAction{session, 5, id, kind, tick}, f);
|
|
};
|
|
f.server_tick = 200;
|
|
auto r = action(1, 10, 203);
|
|
CHECK(r.status == AdmitStatus::admitted && r.tick == 203 && r.server_tick == 200);
|
|
CHECK(a.query(1, {10}).result == ActionResult::pending);
|
|
CHECK(action(1, 0, 203).status == AdmitStatus::invalid_action);
|
|
CHECK(action(1, 11, 203, 9).status == AdmitStatus::invalid_action);
|
|
{ // Wire kind 257 must not truncate to a known kind.
|
|
auto other = *IntentAdmission::create(IntentLimits{});
|
|
CHECK(other.open(1, alice));
|
|
CHECK(other.action(1, SessionAction{1, 5, 1, 257, 101}, AdmissionFacts{true, 5, 100})
|
|
.status == AdmitStatus::invalid_action);
|
|
}
|
|
r = action(1, 10, 204); // Retry with another tick: the original stands.
|
|
CHECK(r.status == AdmitStatus::duplicate && r.tick == 203);
|
|
CHECK(action(1, 12, 203).status == AdmitStatus::rate_limited); // 4 action attempts.
|
|
CHECK(a.carry(202).empty());
|
|
const auto carried = a.carry(203);
|
|
CHECK(carried.size() == 1 && carried[0] == CarriedAction{alice, 10, 203, ActionKind::probe});
|
|
CHECK(a.carry(203).empty()); // Each action is carried once.
|
|
CHECK(a.committed(202, 7).empty());
|
|
auto outcomes = a.committed(203, 8);
|
|
CHECK(outcomes.size() == 1 && outcomes[0].account == alice &&
|
|
outcomes[0].outcome == ActionOutcome{10, ActionResult::committed_no_effect, 203, 8});
|
|
CHECK(a.query(1, {10}) == outcomes[0].outcome);
|
|
CHECK(a.query(2, {10}).result == ActionResult::unknown); // Bob's account has no action 10.
|
|
// A reconnect of the same account (new session) sees the same outcome.
|
|
a.close(1);
|
|
CHECK(a.query(1, {10}).result == ActionResult::unknown); // Closed session asks nothing.
|
|
CHECK(a.open(3, alice) && a.session_of(alice) == 3u);
|
|
CHECK(a.query(3, {10}) == outcomes[0].outcome);
|
|
f.server_tick = 300;
|
|
CHECK(action(3, 10, 301).status == AdmitStatus::duplicate); // Never admitted twice.
|
|
// Unresolved cap counts intentions and actions; abandon finalizes only uncarried actions.
|
|
CHECK(action(3, 20, 301).status == AdmitStatus::admitted);
|
|
CHECK(action(3, 21, 308).status == AdmitStatus::admitted);
|
|
(void)a.carry(301);
|
|
for (std::uint64_t i = 0; i < 4; ++i)
|
|
CHECK(a.intention(3, SessionIntention{3, 5, i + 1, 302 + i, 0, 0}, f).status ==
|
|
AdmitStatus::admitted);
|
|
CHECK(a.unresolved(3) == 6);
|
|
CHECK(a.intention(3, SessionIntention{3, 5, 9, 307, 0, 0}, f).status ==
|
|
AdmitStatus::too_many_unresolved);
|
|
outcomes = a.abandon();
|
|
CHECK(outcomes.size() == 1 &&
|
|
outcomes[0].outcome == ActionOutcome{21, ActionResult::not_committed, 0, 0});
|
|
CHECK(a.query(3, {20}).result == ActionResult::pending); // Carried: awaits its record.
|
|
CHECK(a.unresolved(3) == 1);
|
|
CHECK(a.committed(301, 9).size() == 1); // Its batch turned out durable.
|
|
// Final outcomes are kept within a bound; older ones become unknown here.
|
|
for (std::uint64_t id = 30; id < 34; ++id) {
|
|
f.server_tick += 8;
|
|
CHECK(action(3, id, f.server_tick + 1).status == AdmitStatus::admitted);
|
|
(void)a.carry(f.server_tick + 1);
|
|
CHECK(a.committed(f.server_tick + 1, id).size() == 1);
|
|
}
|
|
CHECK(a.query(3, {10}).result == ActionResult::unknown);
|
|
CHECK(a.query(3, {33}).result == ActionResult::committed_no_effect);
|
|
}
|
|
} // namespace
|
|
|
|
void control_tests() {
|
|
grants();
|
|
identity_adapter();
|
|
control_rules();
|
|
session_codec();
|
|
intent_admission();
|
|
}
|
|
} // namespace evo::network_test
|