11//! Recomputes the chain from its anchor, checks every checkpoint against it, and
2- //! checks that it verifies before it is pruned.
2+ //! checks that it verifies before it is pruned. Also reads the dashboard's audit
3+ //! page, which shows the newest verification recorded instead of making one.
34
45use super :: {
56 AuditChain , AuditCheckpoint , AuditError , AuditKeys , AuditPage , AuditVerification , Chain ,
6- ChainBreak , ChainProblem , CheckpointText , Pruning ,
7+ ChainBreak , ChainProblem , CheckpointText , Pruning , RecordedVerification , StoredChain ,
78} ;
9+ use piquel_common:: events:: DomainEvent ;
810use piquel_crypto:: CryptoError ;
9- use piquel_repository:: { ChainLink , Checkpoint , DatabaseTransaction } ;
11+ use piquel_repository:: { ChainLink , Checkpoint , DatabaseTransaction , RecordedEvent } ;
1012use std:: collections:: BTreeMap ;
1113
1214/// A checkpoint checked on its own, before the chain is compared with it.
@@ -75,26 +77,46 @@ impl AuditChain {
7577 Ok ( verification)
7678 }
7779
78- /// Verifies the chain, and returns it with up to [`Self::PAGE`] checkpoints after
79- /// `after`, or after the anchor, each judged by [`Self::pruning`], all from one
80- /// snapshot.
80+ /// The dashboard's audit page: the chain as stored, the newest verification
81+ /// the audit log records, and up to [`Self::PAGE`] checkpoints after `after`,
82+ /// or after the anchor, each judged by that verification, all from one
83+ /// snapshot. It recomputes nothing, so it costs the same however long the
84+ /// history: [`Self::verify`] runs only when an operator asks.
8185 ///
8286 /// # Errors
83- /// As [`Self::verify`].
84- pub ( crate ) async fn page (
85- & self ,
86- keys : & AuditKeys ,
87- after : Option < u64 > ,
88- ) -> Result < AuditPage , AuditError > {
87+ /// Returns [`AuditError`] if the database fails or the recorded verification
88+ /// does not decode.
89+ pub ( crate ) async fn page ( & self , after : Option < u64 > ) -> Result < AuditPage , AuditError > {
8990 let mut tx = self . database . begin ( ) . await ?;
9091 tx. audit ( ) . read_snapshot ( ) . await ?;
91- let verification = Self :: verify_in ( & mut tx, keys) . await ?;
92- let after = after. unwrap_or_else ( || {
93- verification
94- . anchor
95- . as_ref ( )
96- . map_or ( 0 , |anchor| anchor. position )
97- } ) ;
92+ let point = |checkpoint : Checkpoint | {
93+ ChainLink {
94+ position : checkpoint. position ,
95+ hash : checkpoint. head ,
96+ }
97+ . point ( )
98+ } ;
99+ let chain = StoredChain {
100+ anchor : tx. audit ( ) . anchor ( ) . await ?. map ( point) ,
101+ newest : tx. audit ( ) . head ( ) . await ?. as_ref ( ) . map ( Chain :: point) ,
102+ unsealed : tx. audit ( ) . unsealed_count ( ) . await ?,
103+ } ;
104+ // A verification a newer release recorded reads as unknown, so as none.
105+ let verified =
106+ tx. events ( )
107+ . newest_audit_verification ( )
108+ . await ?
109+ . and_then ( |stored| match stored. event {
110+ RecordedEvent :: Known ( DomainEvent :: AuditChainVerified ( verification) ) => {
111+ Some ( RecordedVerification {
112+ at : stored. occurred_at ,
113+ verification,
114+ } )
115+ }
116+ _ => None ,
117+ } ) ;
118+ let after =
119+ after. unwrap_or_else ( || chain. anchor . as_ref ( ) . map_or ( 0 , |anchor| anchor. position ) ) ;
98120 // One more than a page tells whether another follows.
99121 let mut stored = tx. audit ( ) . checkpoints ( after, Self :: PAGE + 1 ) . await ?;
100122 tx. commit ( ) . await ?;
@@ -103,38 +125,25 @@ impl AuditChain {
103125 let next = more
104126 . then ( || stored. last ( ) . map ( |checkpoint| checkpoint. position ) )
105127 . flatten ( ) ;
128+ let broken = verified
129+ . as_ref ( )
130+ . and_then ( |verified| verified. verification . broken . as_ref ( ) ) ;
106131 let checkpoints = stored
107132 . into_iter ( )
108133 . map ( |checkpoint| AuditCheckpoint {
109- point : ChainLink {
110- position : checkpoint. position ,
111- hash : checkpoint. head ,
112- }
113- . point ( ) ,
134+ pruning : Pruning :: of ( chain. anchor . as_ref ( ) , broken, checkpoint. position ) ,
114135 signed_at : checkpoint. created_at ,
115- pruning : Self :: pruning ( & verification , checkpoint. position ) ,
136+ point : point ( checkpoint) ,
116137 } )
117138 . collect ( ) ;
118139 Ok ( AuditPage {
119- verification,
140+ chain,
141+ verified,
120142 checkpoints,
121143 next,
122144 } )
123145 }
124146
125- /// Whether `verification`'s chain may be pruned through the checkpoint at
126- /// `position`: only after the anchor, and only if the chain verifies through it.
127- /// `audit prune` and the dashboard's audit page both apply it.
128- fn pruning ( verification : & AuditVerification , position : u64 ) -> Pruning {
129- match ( & verification. anchor , & verification. broken ) {
130- ( Some ( anchor) , _) if anchor. position >= position => Pruning :: Pruned {
131- anchor : anchor. position ,
132- } ,
133- ( _, Some ( broken) ) if broken. position <= position => Pruning :: Broken ( broken. clone ( ) ) ,
134- _ => Pruning :: Prunable ,
135- }
136- }
137-
138147 /// [`Self::verify`], in a transaction that sees one snapshot.
139148 async fn verify_in (
140149 tx : & mut DatabaseTransaction < ' _ > ,
@@ -313,7 +322,8 @@ impl AuditChain {
313322 return Err ( AuditError :: NoCheckpoint ( through) ) ;
314323 }
315324 let verification = self . verify ( keys) . await ?;
316- match Self :: pruning ( & verification, through) {
325+ let ( anchor, broken) = ( verification. anchor . as_ref ( ) , verification. broken . as_ref ( ) ) ;
326+ match Pruning :: of ( anchor, broken, through) {
317327 Pruning :: Prunable => Ok ( ( ) ) ,
318328 Pruning :: Pruned { anchor } => Err ( AuditError :: AlreadyPruned { anchor } ) ,
319329 Pruning :: Broken ( broken) => Err ( AuditError :: Broken ( broken) ) ,
0 commit comments