@@ -38,10 +38,11 @@ use std::{
3838} ;
3939use thiserror:: Error ;
4040use tlog_tiles:: {
41- prove_record, tlog, Hash , HashReader , LogEntry , PendingLogEntry , RecordProof , Tile ,
42- TileHashReader , TileIterator , TileReader , TlogError , TlogTile , TreeProof , TreeWithTimestamp ,
43- UnixTimestamp , HASH_SIZE ,
41+ prove_record, Hash , HashReader , LogEntry , PendingLogEntry , RecordProof , Tile , TileHashReader ,
42+ TileIterator , TileReader , TlogError , TlogTile , TreeWithTimestamp , UnixTimestamp , HASH_SIZE ,
4443} ;
44+ #[ cfg( test) ]
45+ use tlog_tiles:: { tlog, TreeProof } ;
4546use tokio:: sync:: watch:: { channel, Receiver , Sender } ;
4647use worker:: Error as WorkerError ;
4748
@@ -50,7 +51,7 @@ use worker::Error as WorkerError;
5051const DATA_TILE_LEVEL_KEY : u8 = u8:: MAX ;
5152/// Same as above, anything above 63 is fine to use as the level key.
5253const UNHASHED_TILE_LEVEL_KEY : u8 = u8:: MAX - 1 ;
53- const CHECKPOINT_KEY : & str = "checkpoint" ;
54+ pub const CHECKPOINT_KEY : & str = "checkpoint" ;
5455const STAGING_KEY : & str = "staging" ;
5556
5657// Limit on the number of entries per batch. Tune this parameter to avoid
@@ -555,6 +556,7 @@ impl SequenceState {
555556 }
556557
557558 /// Proves inclusion of the last leaf in the current tree.
559+ #[ cfg( test) ]
558560 pub ( crate ) fn prove_inclusion_of_last_elem ( & self ) -> RecordProof {
559561 let tree_size = self . tree . size ( ) ;
560562 let reader = HashReaderWithOverlay {
@@ -572,6 +574,7 @@ impl SequenceState {
572574 /// # Errors
573575 /// Errors when the last tree was size 0. We cannot prove consistency with
574576 /// respect to an empty tree
577+ #[ cfg( test) ]
575578 pub ( crate ) fn prove_consistency_of_single_append ( & self ) -> Result < TreeProof , TlogError > {
576579 let tree_size = self . tree . size ( ) ;
577580 let reader = HashReaderWithOverlay {
@@ -580,62 +583,58 @@ impl SequenceState {
580583 } ;
581584 tlog:: prove_tree ( tree_size, tree_size - 1 , & reader)
582585 }
586+ }
583587
584- /// Returns an inclusion proof for the given leaf index
585- ///
586- /// # Errors
587- /// Errors when the leaf index equals or exceeds the number of leaves, or
588- /// the desired tiles do not exist as bucket objects.
589- pub async fn prove_inclusion (
590- & self ,
591- object : & impl ObjectBackend ,
592- leaf_index : u64 ,
593- ) -> Result < RecordProof , WorkerError > {
594- // Get the size of the tree
595- let num_leaves = self . tree . size ( ) ;
596- let tree_hash = * self . tree . hash ( ) ;
597-
598- if leaf_index >= num_leaves {
599- return Err ( WorkerError :: RustError (
600- "leaf index exceeds number of leaves in the tree" . to_string ( ) ,
601- ) ) ;
602- }
603-
604- let mut all_tile_data = HashMap :: new ( ) ;
605-
606- // Make a fake proof using ProofPreparer to get all the tiles we need
607- // TODO: This seems useful. We can probably move this into the tlog_tiles crate
608- let tiles_to_fetch = {
609- let tile_reader = ProofPreparer :: default ( ) ;
610- let hash_reader = TileHashReader :: new ( num_leaves, tree_hash, & tile_reader) ;
611- // ProofPreparer is guaranteed to make prove_record return a BadMath
612- // error. This is fine, because it already collected the data we
613- // needed
614- let _ = prove_record ( num_leaves, leaf_index, & hash_reader) ;
615- tile_reader. 0 . into_inner ( )
616- } ;
617-
618- // Fetch all the tiles we need for a proof
619- for tile in tiles_to_fetch {
620- let Some ( tile_data) = object. fetch ( & tile. path ( ) ) . await ? else {
621- return Err ( WorkerError :: RustError ( format ! (
622- "missing tile for inclusion proof {}" ,
623- tile. path( )
624- ) ) ) ;
625- } ;
626- all_tile_data. insert ( tile, tile_data) ;
627- }
588+ /// Returns an inclusion proof for the given leaf index, tree size, and root hash.
589+ ///
590+ /// # Errors
591+ /// Errors when the leaf index equals or exceeds the number of leaves, or
592+ /// the desired tiles do not exist as bucket objects.
593+ pub async fn prove_inclusion (
594+ num_leaves : u64 ,
595+ tree_hash : Hash ,
596+ leaf_index : u64 ,
597+ object : & impl ObjectBackend ,
598+ ) -> Result < RecordProof , WorkerError > {
599+ if leaf_index >= num_leaves {
600+ return Err ( WorkerError :: RustError (
601+ "leaf index exceeds number of leaves in the tree" . to_string ( ) ,
602+ ) ) ;
603+ }
604+
605+ let mut all_tile_data = HashMap :: new ( ) ;
606+
607+ // Make a fake proof using ProofPreparer to get all the tiles we need
608+ // TODO: This seems useful. We can probably move this into the tlog_tiles crate
609+ let tiles_to_fetch = {
610+ let tile_reader = ProofPreparer :: default ( ) ;
611+ let hash_reader = TileHashReader :: new ( num_leaves, tree_hash, & tile_reader) ;
612+ // ProofPreparer is guaranteed to make prove_record return a BadMath
613+ // error. This is fine, because it already collected the data we
614+ // needed.
615+ let _ = prove_record ( num_leaves, leaf_index, & hash_reader) ;
616+ tile_reader. 0 . into_inner ( )
617+ } ;
628618
629- // Now make the proof
630- let proof = {
631- // Put the recorded tiles into the appropriate Reader structs for prove_record()
632- let tile_reader = SimpleTlogTileReader ( all_tile_data ) ;
633- let hash_reader = TileHashReader :: new ( num_leaves , tree_hash , & tile_reader ) ;
634- prove_record ( num_leaves , leaf_index , & hash_reader )
635- . map_err ( |e| WorkerError :: RustError ( e . to_string ( ) ) ) ?
619+ // Fetch all the tiles we need for a proof
620+ for tile in tiles_to_fetch {
621+ let Some ( tile_data ) = object . fetch ( & tile . path ( ) ) . await ? else {
622+ return Err ( WorkerError :: RustError ( format ! (
623+ "missing tile for inclusion proof {}" ,
624+ tile . path ( )
625+ ) ) ) ;
636626 } ;
637- Ok ( proof )
627+ all_tile_data . insert ( tile , tile_data ) ;
638628 }
629+
630+ // Now make the proof
631+ let proof = {
632+ // Put the recorded tiles into the appropriate Reader structs for prove_record()
633+ let tile_reader = SimpleTlogTileReader ( all_tile_data) ;
634+ let hash_reader = TileHashReader :: new ( num_leaves, tree_hash, & tile_reader) ;
635+ prove_record ( num_leaves, leaf_index, & hash_reader) . map_err ( |e| e. to_string ( ) ) ?
636+ } ;
637+ Ok ( proof)
639638}
640639
641640/// Result of an [`add_leaf_to_pool`] request containing either a cached log
@@ -1455,7 +1454,13 @@ mod tests {
14551454 let tree_hash = * sequence_state. tree . hash ( ) ;
14561455 for i in 0 ..n {
14571456 // Compute the inclusion proof for leaf i
1458- let proof = block_on ( sequence_state. prove_inclusion ( & log. object , i) ) . unwrap ( ) ;
1457+ let proof = block_on ( prove_inclusion (
1458+ sequence_state. tree . size ( ) ,
1459+ * sequence_state. tree . hash ( ) ,
1460+ i,
1461+ & log. object ,
1462+ ) )
1463+ . unwrap ( ) ;
14591464 // Verify the inclusion proof. We need the leaf hash
14601465 let leaf_hash = {
14611466 // Get the tile the leaf belongs to, and correct the width by getting the 0th parent
0 commit comments