Skip to content

Commit a6ec360

Browse files
authored
refactor(auth): audit reads the link a checkpoint signs in one place (#589)
Part of #544 The audit module built a `ChainLink { position: checkpoint.position, hash: checkpoint.head }` by hand in five places: the public checkpoints route, the sealer's starting point, a checkpoint's own check, the dashboard's audit page and the verification's anchor. `Chain::signed(&Checkpoint)`, beside `extend` and `point` on the `Chain` trait, now builds it once. `Checkpoint` and `ChainLink` both live in the repository crate, so the conversion is a method of the audit module's own trait rather than a `From`. - Tests merged or removed: none. - Files edited outside the Operations area: none. - Proof that behaviour is unchanged: each replaced literal copied the same two fields, so the change is mechanical. `just db test` passes in full, 497 of 497, including the 26 `audit::` tests that cover sealing, verification, pruning and both checkpoint routes. Checks: `just fmt`, `cargo clippy` with and without `--all-features` (`-D warnings`), `cargo doc -D warnings`, `just db test`. > [!NOTE] > Written by Claude Opus 5.5 in Claude Code and reviewed by Claude Opus 5.5 (via T3 Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
1 parent 4a5336b commit a6ec360

4 files changed

Lines changed: 16 additions & 25 deletions

File tree

‎apps/auth-service/src/audit/mod.rs‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use chrono::{DateTime, SecondsFormat, Utc};
3333
use serde::Serialize;
3434
use sourceorigin_common::config::Origin;
3535
use sourceorigin_crypto::{CryptoError, Digest, NoteVerifier, NoteVerifiers};
36-
use sourceorigin_repository::{ChainLink, Database, DatabaseError, EventContent};
36+
use sourceorigin_repository::{ChainLink, Checkpoint, Database, DatabaseError, EventContent};
3737
use thiserror::Error;
3838

3939
/// The verifier keys committed to the repository, one per line: each environment's
@@ -112,6 +112,9 @@ trait Chain: Sized {
112112
/// [`EventContent::NEWEST_FORMAT`], or [`AuditError::Canonical`].
113113
fn extend(&self, content: &EventContent) -> Result<Self, AuditError>;
114114

115+
/// The link `checkpoint` signs: its position and the head stored beside it.
116+
fn signed(checkpoint: &Checkpoint) -> Self;
117+
115118
/// This link as verification reports it.
116119
fn point(&self) -> ChainPoint;
117120
}
@@ -151,6 +154,13 @@ impl Chain for ChainLink {
151154
})
152155
}
153156

157+
fn signed(checkpoint: &Checkpoint) -> Self {
158+
Self {
159+
position: checkpoint.position,
160+
hash: checkpoint.head,
161+
}
162+
}
163+
154164
fn point(&self) -> ChainPoint {
155165
ChainPoint {
156166
position: self.position,

‎apps/auth-service/src/audit/routes.rs‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,7 @@ impl From<Checkpoint> for PublishedCheckpoint {
7272
/// Keeps the stored values; the note is what readers verify.
7373
fn from(checkpoint: Checkpoint) -> Self {
7474
Self {
75-
head: ChainLink {
76-
position: checkpoint.position,
77-
hash: checkpoint.head,
78-
}
79-
.point(),
75+
head: ChainLink::signed(&checkpoint).point(),
8076
time: checkpoint.created_at,
8177
note: checkpoint.note,
8278
}

‎apps/auth-service/src/audit/sealer.rs‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,7 @@ impl Sealer {
192192
.into_iter()
193193
.flatten()
194194
.max_by_key(|checkpoint| checkpoint.position)
195-
.map_or(ChainLink::GENESIS, |checkpoint| ChainLink {
196-
position: checkpoint.position,
197-
hash: checkpoint.head,
198-
});
195+
.map_or(ChainLink::GENESIS, ChainLink::signed);
199196
// Nothing new to sign, or an untrusted checkpoint holds the head's position
200197
// until the next event is sealed: return before recomputing anything.
201198
let newest = untrusted

‎apps/auth-service/src/audit/verify.rs‎

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,7 @@ impl AuditKeys {
6262
/// Returns [`AuditError::Verifying`] if the cryptographic provider fails, which
6363
/// says nothing about the checkpoint.
6464
fn check(&self, checkpoint: &Checkpoint) -> Result<Checked, AuditError> {
65-
let stored = ChainLink {
66-
position: checkpoint.position,
67-
hash: checkpoint.head,
68-
};
65+
let stored = ChainLink::signed(checkpoint);
6966
let problem = match self.verifiers.open(&checkpoint.note) {
7067
Ok(text) => match CheckpointText::parse(text) {
7168
Some(text) if text.head == stored && text.time == checkpoint.created_at => None,
@@ -202,13 +199,7 @@ impl AuditChain {
202199
pub(crate) async fn page(&self, after: Option<u64>) -> Result<AuditPage, AuditError> {
203200
let mut tx = self.database.begin().await?;
204201
tx.audit().read_snapshot().await?;
205-
let point = |checkpoint: Checkpoint| {
206-
ChainLink {
207-
position: checkpoint.position,
208-
hash: checkpoint.head,
209-
}
210-
.point()
211-
};
202+
let point = |checkpoint: Checkpoint| ChainLink::signed(&checkpoint).point();
212203
let chain = StoredChain {
213204
anchor: tx.audit().anchor().await?.map(point),
214205
newest: tx.audit().head().await?.as_ref().map(Chain::point),
@@ -263,10 +254,7 @@ impl AuditChain {
263254
keys: &AuditKeys,
264255
) -> Result<AuditVerification, AuditError> {
265256
let checkpoints = Self::checkpoints(tx, keys).await?;
266-
let anchor = tx.audit().anchor().await?.map(|anchor| ChainLink {
267-
position: anchor.position,
268-
hash: anchor.head,
269-
});
257+
let anchor = tx.audit().anchor().await?.as_ref().map(ChainLink::signed);
270258
let start = anchor.unwrap_or(ChainLink::GENESIS);
271259
// Pruning deletes every event through the anchor, so one left there means
272260
// the anchor was moved to hide it.

0 commit comments

Comments
 (0)