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
+6 -4
View File
@@ -14,9 +14,10 @@
// refuses grants its predecessor already used. Append-only text lines
// "<grant id hex> <expires_at>", flushed and fsynced before the session is welcomed.
// Opening drops expired entries by an atomic rewrite. Corrupt content, I/O failure or
// a full ledger fail closed. Single owner; not a general database.
// a full ledger fail closed. Exclusive lifetime ownership via a stable .lock file; not a general
// database.
namespace evo::network {
enum class LedgerError : std::uint8_t { io, corrupt, full };
enum class LedgerError : std::uint8_t { io, corrupt, full, busy };
class GrantLedger {
public:
static constexpr std::size_t max_entries = 65536;
@@ -33,8 +34,9 @@ class GrantLedger {
[[nodiscard]] std::expected<void, LedgerError> record(ConsumedGrant grant);
private:
GrantLedger(std::FILE* file, std::vector<ConsumedGrant> loaded)
: file_{file}, loaded_{std::move(loaded)}, entries_{loaded_.size()} {}
struct Lock;
GrantLedger(std::FILE* file, std::vector<ConsumedGrant> loaded, std::unique_ptr<Lock> lock);
std::unique_ptr<Lock> lock_; // Outlives the append handle; never unlink its pathname.
std::FILE* file_;
std::vector<ConsumedGrant> loaded_;
std::size_t entries_;