refactor(provcache): inject TimeProvider and IGuidProvider for determinism - DET-005
Refactored 8 files across StellaOps.Provcache, StellaOps.Provcache.Postgres, and StellaOps.Provcache.Valkey: Core Provcache library: - EvidenceChunker: Added IGuidProvider for ChunkId generation in ChunkAsync/ChunkStreamAsync - LazyFetchOrchestrator: Added IGuidProvider for ChunkId generation when storing fetched chunks - MinimalProofExporter: Added IGuidProvider for ChunkId generation in ImportAsync - FeedEpochAdvancedEvent: Added optional eventId/timestamp parameters to static Create() - SignerRevokedEvent: Added optional eventId/timestamp parameters to static Create() Postgres implementation: - PostgresProvcacheRepository: Added TimeProvider and IGuidProvider for IncrementHitCountAsync, GetStatisticsAsync, LogRevocationAsync, and MapToEntity - PostgresEvidenceChunkRepository: Added TimeProvider and IGuidProvider for GetManifestAsync and MapToEntity Valkey implementation: - ValkeyProvcacheStore: Added TimeProvider for TTL calculations in GetAsync, SetAsync, SetManyAsync All constructors use optional parameters with defaults to system implementations for backward compatibility. Added StellaOps.Determinism.Abstractions project references where needed.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System.Text.Json;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using StellaOps.Determinism;
|
||||
using StellaOps.Provcache.Entities;
|
||||
|
||||
namespace StellaOps.Provcache.Postgres;
|
||||
@@ -12,14 +13,20 @@ public sealed class PostgresProvcacheRepository : IProvcacheRepository
|
||||
{
|
||||
private readonly ProvcacheDbContext _context;
|
||||
private readonly ILogger<PostgresProvcacheRepository> _logger;
|
||||
private readonly TimeProvider _timeProvider;
|
||||
private readonly IGuidProvider _guidProvider;
|
||||
private readonly JsonSerializerOptions _jsonOptions;
|
||||
|
||||
public PostgresProvcacheRepository(
|
||||
ProvcacheDbContext context,
|
||||
ILogger<PostgresProvcacheRepository> logger)
|
||||
ILogger<PostgresProvcacheRepository> logger,
|
||||
TimeProvider? timeProvider = null,
|
||||
IGuidProvider? guidProvider = null)
|
||||
{
|
||||
_context = context ?? throw new ArgumentNullException(nameof(context));
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
_timeProvider = timeProvider ?? TimeProvider.System;
|
||||
_guidProvider = guidProvider ?? SystemGuidProvider.Instance;
|
||||
_jsonOptions = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
@@ -193,12 +200,13 @@ public sealed class PostgresProvcacheRepository : IProvcacheRepository
|
||||
/// <inheritdoc />
|
||||
public async Task IncrementHitCountAsync(string veriKey, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var now = _timeProvider.GetUtcNow();
|
||||
await _context.ProvcacheItems
|
||||
.Where(e => e.VeriKey == veriKey)
|
||||
.ExecuteUpdateAsync(
|
||||
setters => setters
|
||||
.SetProperty(e => e.HitCount, e => e.HitCount + 1)
|
||||
.SetProperty(e => e.LastAccessedAt, DateTimeOffset.UtcNow),
|
||||
.SetProperty(e => e.LastAccessedAt, now),
|
||||
cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
@@ -206,7 +214,7 @@ public sealed class PostgresProvcacheRepository : IProvcacheRepository
|
||||
/// <inheritdoc />
|
||||
public async Task<ProvcacheStatistics> GetStatisticsAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var now = _timeProvider.GetUtcNow();
|
||||
var hourFromNow = now.AddHours(1);
|
||||
|
||||
var totalEntries = await _context.ProvcacheItems
|
||||
@@ -266,12 +274,12 @@ public sealed class PostgresProvcacheRepository : IProvcacheRepository
|
||||
{
|
||||
var revocation = new ProvcacheRevocationEntity
|
||||
{
|
||||
RevocationId = Guid.NewGuid(),
|
||||
RevocationId = _guidProvider.NewGuid(),
|
||||
RevocationType = type,
|
||||
TargetHash = targetHash,
|
||||
Reason = reason,
|
||||
EntriesAffected = entriesAffected,
|
||||
CreatedAt = DateTimeOffset.UtcNow
|
||||
CreatedAt = _timeProvider.GetUtcNow()
|
||||
};
|
||||
|
||||
_context.Revocations.Add(revocation);
|
||||
@@ -329,7 +337,7 @@ public sealed class PostgresProvcacheRepository : IProvcacheRepository
|
||||
HitCount = entry.HitCount,
|
||||
CreatedAt = entry.CreatedAt,
|
||||
ExpiresAt = entry.ExpiresAt,
|
||||
UpdatedAt = DateTimeOffset.UtcNow,
|
||||
UpdatedAt = _timeProvider.GetUtcNow(),
|
||||
LastAccessedAt = entry.LastAccessedAt
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user