Skip to content

Commit 6e78ba2

Browse files
committed
refactor(stm): applied comments and fixed toml
1 parent 4508af3 commit 6e78ba2

3 files changed

Lines changed: 30 additions & 15 deletions

File tree

mithril-stm/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ future_snark = [
3131
"dep:sha2",
3232
"dep:rand_chacha",
3333
"dep:reqwest",
34+
"rustls", # Temporarily activate rustls before it is activated in the caller
3435
]
36+
# TLS backend for the SRS download client (used by `future_snark`).
37+
# Exactly one of these must be enabled together with `future_snark`.
38+
native-tls = ["reqwest?/native-tls"]
39+
rustls = ["reqwest?/rustls"]
3540
num-integer-backend = ["dep:num-bigint", "dep:num-integer", "dep:num-rational", "dep:num-traits"]
3641
rug-backend = ["rug/default"]
3742

@@ -56,7 +61,7 @@ rand = { version = "0.10.1", optional = true }
5661
rand_chacha = { workspace = true, optional = true }
5762
rand_core = { workspace = true, features = ["std"] }
5863
rayon = { workspace = true }
59-
reqwest = { workspace = true, default-features = true, features = ["blocking"], optional = true }
64+
reqwest = { workspace = true, default-features = false, features = ["blocking"], optional = true }
6065
serde = { workspace = true }
6166
sha2 = { version = "0.10.9", optional = true }
6267
subtle = "2.6.1"

mithril-stm/src/circuits/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,11 @@ pub(crate) use halo2::witness::{
1818

1919
/// Constant holding the current path of the cached values related to the circuits
2020
const MITHRIL_CIRCUIT_CACHE_FOLDER: &str = "mithril-circuit";
21+
22+
#[cfg(all(
23+
feature = "future_snark",
24+
not(any(feature = "rustls", feature = "native-tls"))
25+
))]
26+
compile_error!(
27+
"Enabling `future_snark` requires exactly one of the `rustls` or `native-tls` features."
28+
);

mithril-stm/src/circuits/trusted_setup.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use 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

1313
use 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)]
5653
impl 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

Comments
 (0)