Fix grant ledger ownership and truncated-record recovery

This commit is contained in:
dingo
2026-10-02 06:24:56 -04:00
parent c588be237c
commit 7ac587743c
6 changed files with 180 additions and 9 deletions
+71
View File
@@ -81,6 +81,7 @@ struct Harness {
server.reset();
std::error_code ignored;
std::filesystem::remove(ledger, ignored);
std::filesystem::remove(std::filesystem::path{ledger}.concat(".lock"), ignored);
}
Harness(const Harness&) = delete;
Harness& operator=(const Harness&) = delete;
@@ -519,6 +520,74 @@ void grants_stay_consumed_across_restarts() {
std::filesystem::remove(path, ignored);
}
// Follow-up review: a duplicate startup must not replace an active owner's ledger.
void ledger_exclusive_owner() {
Harness h;
h.tick();
SessionServerConfig duplicate;
duplicate.credentials = h.pki.server;
duplicate.admission.world = identity;
duplicate.admission.region = region;
duplicate.grant_ledger = h.ledger;
for (const auto port : {h.port, std::uint16_t{0}}) {
duplicate.listen = "127.0.0.1:" + std::to_string(port);
CHECK(refused(SessionServer::create(duplicate, grant_key(), now_seconds),
SessionServerError::ledger));
}
CHECK(refused(GrantLedger::open(h.ledger, now_seconds), LedgerError::busy));
// Written AFTER the failed duplicate starts, then recovered through real TLS.
auto first = h.client(grants(7, 1, 100));
CHECK(h.until([&] { return first->phase() == SessionPhase::established; }));
h.clients.clear();
first.reset();
h.server.reset();
h.facts.epoch = 2;
h.server = h.make_server(h.port, 4, std::chrono::milliseconds{3000});
h.tick();
auto replay = h.client(grants(7, 1, 100));
std::vector<ClientEvent> events;
CHECK(h.until([&] {
h.collect(*replay, events);
return has(events, ClientEvent::Kind::closed);
}));
CHECK(last(events, ClientEvent::Kind::closed)->reason == CloseReason::unauthenticated);
}
void ledger_incomplete_records() {
const auto path = fresh_ledger();
const std::string prefix = "0000000000000002 1000\n";
const std::string record = "0000000000000001 1000\n";
const auto reject_unchanged = [&](const std::string& bytes) {
{
std::ofstream out{path, std::ios::binary | std::ios::trunc};
out << bytes;
CHECK(out.good());
}
CHECK(refused(GrantLedger::open(path, 100), LedgerError::corrupt));
std::ifstream in{path, std::ios::binary};
const std::string after{std::istreambuf_iterator<char>{in}, {}};
CHECK(after == bytes);
CHECK(!std::filesystem::exists(std::filesystem::path{path}.concat(".tmp")));
};
// All nonempty partial tails, including a plausible but expired decimal prefix.
for (std::size_t length = 1; length < record.size(); ++length)
reject_unchanged(prefix + record.substr(0, length));
reject_unchanged(prefix + std::string(4096, '1') + "\n");
// A complete file (including empty) remains valid; failed opens released the lock.
for (const auto& bytes : {std::string{}, prefix, prefix + record}) {
{
std::ofstream out{path, std::ios::binary | std::ios::trunc};
out << bytes;
}
const auto ledger = GrantLedger::open(path, 100);
CHECK(ledger);
CHECK((*ledger)->loaded().size() == (bytes.empty() ? 0 : bytes == prefix ? 1 : 2));
}
std::error_code ignored;
std::filesystem::remove(path, ignored);
std::filesystem::remove(std::filesystem::path{path}.concat(".lock"), ignored);
}
// Review M9.4-M9.7 #4: liveness after Welcome, both directions, without spurious drops.
void liveness() {
const auto fast_server = [](SessionServerConfig& c) {
@@ -596,6 +665,8 @@ void liveness() {
} // namespace
void session_tests() {
ledger_exclusive_owner();
ledger_incomplete_records();
connect_view_intents_and_outcomes();
duplicate_capacity_and_authentication();
stale_epoch_restart_and_reconnect();