11use std:: {
22 fs:: File ,
3- io:: { BufReader , BufWriter , Write } ,
3+ io:: { BufReader , Write } ,
44 path:: PathBuf ,
55 time:: Duration ,
66} ;
@@ -12,7 +12,6 @@ use sha2::{Digest, Sha256};
1212
1313use crate :: { StmResult , circuits:: MITHRIL_CIRCUIT_CACHE_FOLDER } ;
1414
15- #[ allow( dead_code) ]
1615/// Constant storing the hash of the SRS of degree 22 used to create proof in production.
1716/// This SRS is coming from the trusted setup done by Midnight and available in the following
1817/// repository: https://github.com/midnightntwrk/midnight-trusted-setup.
@@ -51,12 +50,9 @@ pub struct TrustedSetupProvider {
5150 download_timeout_limit : Duration ,
5251}
5352
54- /// TODO: remove allow(dead_code) when used
55- #[ allow( dead_code) ]
5653impl TrustedSetupProvider {
5754 /// Create a new TrustedSetupProvider
58- /// Prepares a subfolder in the given `local_srs_folder_path` to put the SRS in
59- fn new < P : Into < PathBuf > , S : Into < String > , U : Into < String > > (
55+ pub fn new < P : Into < PathBuf > , S : Into < String > , U : Into < String > > (
6056 local_srs_folder_path : P ,
6157 srs_expected_hash : S ,
6258 url_to_download_srs : U ,
@@ -95,7 +91,6 @@ impl TrustedSetupProvider {
9591 /// Fetches the SRS from `self.url_to_download_srs` and returns its bytes.
9692 fn download_srs_file ( & self ) -> StmResult < Vec < u8 > > {
9793 let response = reqwest:: blocking:: Client :: builder ( )
98- // TODO: For now a timeout but this should be updated depending on the behavior we want
9994 . timeout ( self . download_timeout_limit )
10095 . build ( ) ?
10196 . get ( & self . url_to_download_srs )
@@ -119,14 +114,21 @@ impl TrustedSetupProvider {
119114 . local_srs_folder_path
120115 . join ( MITHRIL_CIRCUIT_SRS_FILENAME )
121116 . with_extension ( "temp" ) ;
122- let mut temporary_file = File :: create ( & temp_path) ?;
123- BufWriter :: new ( & mut temporary_file) . write_all ( srs_bytes) ?;
117+ let final_path = self . local_srs_folder_path . join ( MITHRIL_CIRCUIT_SRS_FILENAME ) ;
124118
125- std:: fs:: rename (
126- temp_path,
127- self . local_srs_folder_path . join ( MITHRIL_CIRCUIT_SRS_FILENAME ) ,
128- ) ?;
119+ let mut temporary_file = File :: create ( & temp_path)
120+ . with_context ( || format ! ( "Failed to create temporary SRS file at {temp_path:?}." ) ) ?;
121+ temporary_file. write_all ( srs_bytes) ?;
122+ temporary_file
123+ . sync_all ( )
124+ . with_context ( || "Failed to fsync temporary SRS file before rename." ) ?;
125+ drop ( temporary_file) ;
129126
127+ std:: fs:: rename ( temp_path, final_path) ?;
128+
129+ File :: open ( & self . local_srs_folder_path )
130+ . and_then ( |dir| dir. sync_all ( ) )
131+ . with_context ( || "Failed to fsync SRS directory after rename." ) ?;
130132 Ok ( ( ) )
131133 }
132134
@@ -147,7 +149,7 @@ impl TrustedSetupProvider {
147149
148150 /// Ensures the SRS file is available, downloading it if necessary
149151 /// and deserializes it into memory.
150- fn get_trusted_setup_parameters ( & self ) -> StmResult < ParamsKZG < Bls12 > > {
152+ pub fn get_trusted_setup_parameters ( & self ) -> StmResult < ParamsKZG < Bls12 > > {
151153 self . download_srs_file_if_not_cached ( ) ?;
152154
153155 let file = File :: open ( self . local_srs_folder_path . join ( MITHRIL_CIRCUIT_SRS_FILENAME ) )
0 commit comments