Skip to content

Commit 5070de9

Browse files
Reduce unwrap()s in code (#960)
Reduce unwrap()s in code
2 parents cf35bb4 + 514a64b commit 5070de9

File tree

19 files changed

+174
-125
lines changed

19 files changed

+174
-125
lines changed

filecoin-proofs/src/api/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub fn get_unsealed_range<T: Into<PathBuf> + AsRef<Path>>(
7373
let pp = public_params(
7474
PaddedBytesAmount::from(porep_config),
7575
usize::from(PoRepProofPartitions::from(porep_config)),
76-
);
76+
)?;
7777

7878
let offset_padded: PaddedBytesAmount = UnpaddedBytesAmount::from(offset).into();
7979
let num_bytes_padded: PaddedBytesAmount = num_bytes.into();

filecoin-proofs/src/api/seal.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ pub fn seal_pre_commit<R: AsRef<Path>, T: AsRef<Path>, S: AsRef<Path>>(
7171
// Zero-pad the data to the requested size by extending the underlying file if needed.
7272
f_data.set_len(sector_bytes as u64)?;
7373

74-
let mut data = unsafe { MmapOptions::new().map_mut(&f_data).unwrap() };
74+
let mut data = unsafe { MmapOptions::new().map_mut(&f_data)? };
7575

7676
let compound_setup_params = compound_proof::SetupParams {
7777
vanilla_params: setup_params(
7878
PaddedBytesAmount::from(porep_config),
7979
usize::from(PoRepProofPartitions::from(porep_config)),
80-
),
80+
)?,
8181
partitions: Some(usize::from(PoRepProofPartitions::from(porep_config))),
8282
};
8383

@@ -219,7 +219,7 @@ pub fn seal_commit<T: AsRef<Path>>(
219219
vanilla_params: setup_params(
220220
PaddedBytesAmount::from(porep_config),
221221
usize::from(PoRepProofPartitions::from(porep_config)),
222-
),
222+
)?,
223223
partitions: Some(usize::from(PoRepProofPartitions::from(porep_config))),
224224
};
225225

@@ -290,7 +290,7 @@ pub fn verify_seal(
290290
vanilla_params: setup_params(
291291
PaddedBytesAmount::from(porep_config),
292292
usize::from(PoRepProofPartitions::from(porep_config)),
293-
),
293+
)?,
294294
partitions: Some(usize::from(PoRepProofPartitions::from(porep_config))),
295295
};
296296

filecoin-proofs/src/bin/paramcache.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ fn cache_porep_params(porep_config: PoRepConfig) {
3535
let public_params = public_params(
3636
PaddedBytesAmount::from(porep_config),
3737
usize::from(PoRepProofPartitions::from(porep_config)),
38-
);
38+
)
39+
.unwrap();
3940

4041
{
4142
let circuit = <StackedCompound as CompoundProof<
@@ -70,7 +71,7 @@ fn cache_post_params(post_config: PoStConfig) {
7071
n
7172
);
7273

73-
let post_public_params = post_public_params(post_config);
74+
let post_public_params = post_public_params(post_config).unwrap();
7475

7576
{
7677
let post_circuit: ElectionPoStCircuit<Bls12, PedersenHasher> =

filecoin-proofs/src/caches.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub fn get_stacked_params(porep_config: PoRepConfig) -> Result<Arc<groth16::Para
8181
let public_params = public_params(
8282
PaddedBytesAmount::from(porep_config),
8383
usize::from(PoRepProofPartitions::from(porep_config)),
84-
);
84+
)?;
8585

8686
let parameters_generator = || {
8787
<StackedCompound as CompoundProof<
@@ -102,7 +102,7 @@ pub fn get_stacked_params(porep_config: PoRepConfig) -> Result<Arc<groth16::Para
102102
}
103103

104104
pub fn get_post_params(post_config: PoStConfig) -> Result<Arc<groth16::Parameters<Bls12>>> {
105-
let post_public_params = post_public_params(post_config);
105+
let post_public_params = post_public_params(post_config)?;
106106

107107
let parameters_generator = || {
108108
<ElectionPoStCompound<DefaultTreeHasher> as CompoundProof<
@@ -126,7 +126,7 @@ pub fn get_stacked_verifying_key(porep_config: PoRepConfig) -> Result<Arc<Bls12V
126126
let public_params = public_params(
127127
PaddedBytesAmount::from(porep_config),
128128
usize::from(PoRepProofPartitions::from(porep_config)),
129-
);
129+
)?;
130130

131131
let vk_generator = || {
132132
<StackedCompound as CompoundProof<
@@ -147,7 +147,7 @@ pub fn get_stacked_verifying_key(porep_config: PoRepConfig) -> Result<Arc<Bls12V
147147
}
148148

149149
pub fn get_post_verifying_key(post_config: PoStConfig) -> Result<Arc<Bls12VerifyingKey>> {
150-
let post_public_params = post_public_params(post_config);
150+
let post_public_params = post_public_params(post_config)?;
151151

152152
let vk_generator = || {
153153
<ElectionPoStCompound<DefaultTreeHasher> as CompoundProof<

filecoin-proofs/src/parameters.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@ pub type PostPublicParams = election_post::PublicParams;
2222
pub fn public_params(
2323
sector_bytes: PaddedBytesAmount,
2424
partitions: usize,
25-
) -> stacked::PublicParams<DefaultTreeHasher> {
25+
) -> Result<stacked::PublicParams<DefaultTreeHasher>> {
2626
StackedDrg::<DefaultTreeHasher, DefaultPieceHasher>::setup(&setup_params(
2727
sector_bytes,
2828
partitions,
29-
))
30-
.unwrap()
29+
)?)
3130
}
3231

3332
pub fn window_size_nodes_for_sector_bytes(sector_size: PaddedBytesAmount) -> Result<usize> {
@@ -42,8 +41,8 @@ pub fn window_size_nodes_for_sector_bytes(sector_size: PaddedBytesAmount) -> Res
4241
}
4342
}
4443

45-
pub fn post_public_params(post_config: PoStConfig) -> PostPublicParams {
46-
ElectionPoSt::<DefaultTreeHasher>::setup(&post_setup_params(post_config)).unwrap()
44+
pub fn post_public_params(post_config: PoStConfig) -> Result<PostPublicParams> {
45+
ElectionPoSt::<DefaultTreeHasher>::setup(&post_setup_params(post_config))
4746
}
4847

4948
pub fn post_setup_params(post_config: PoStConfig) -> PostSetupParams {
@@ -54,7 +53,10 @@ pub fn post_setup_params(post_config: PoStConfig) -> PostSetupParams {
5453
}
5554
}
5655

57-
pub fn setup_params(sector_bytes: PaddedBytesAmount, partitions: usize) -> stacked::SetupParams {
56+
pub fn setup_params(
57+
sector_bytes: PaddedBytesAmount,
58+
partitions: usize,
59+
) -> Result<stacked::SetupParams> {
5860
let window_challenges = select_challenges(partitions, POREP_WINDOW_MINIMUM_CHALLENGES, LAYERS);
5961
let wrapper_challenges =
6062
select_challenges(partitions, POREP_WRAPPER_MINIMUM_CHALLENGES, LAYERS);
@@ -80,14 +82,14 @@ pub fn setup_params(sector_bytes: PaddedBytesAmount, partitions: usize) -> stack
8082
);
8183

8284
let nodes = sector_bytes / 32;
83-
stacked::SetupParams {
85+
Ok(stacked::SetupParams {
8486
nodes,
8587
degree: BASE_DEGREE,
8688
expansion_degree: EXP_DEGREE,
8789
seed: DRG_SEED,
8890
config,
8991
window_size_nodes,
90-
}
92+
})
9193
}
9294

9395
fn select_challenges(

filecoin-proofs/src/types/porep_config.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::path::PathBuf;
22

3+
use anyhow::Result;
4+
35
use paired::bls12_381::Bls12;
46
use storage_proofs::circuit::stacked::{StackedCircuit, StackedCompound};
57
use storage_proofs::drgraph::DefaultTreeHasher;
@@ -47,29 +49,29 @@ impl From<PoRepConfig> for SectorSize {
4749

4850
impl PoRepConfig {
4951
/// Returns the cache identifier as used by `storage-proofs::paramater_cache`.
50-
pub fn get_cache_identifier(&self) -> String {
52+
pub fn get_cache_identifier(&self) -> Result<String> {
5153
let params =
52-
crate::parameters::public_params(self.sector_size.into(), self.partitions.into());
54+
crate::parameters::public_params(self.sector_size.into(), self.partitions.into())?;
5355

54-
<StackedCompound as CacheableParameters<
56+
Ok(<StackedCompound as CacheableParameters<
5557
Bls12,
5658
StackedCircuit<_, DefaultTreeHasher, DefaultPieceHasher>,
5759
_,
58-
>>::cache_identifier(&params)
60+
>>::cache_identifier(&params))
5961
}
6062

61-
pub fn get_cache_metadata_path(&self) -> PathBuf {
62-
let id = self.get_cache_identifier();
63-
parameter_cache::parameter_cache_metadata_path(&id)
63+
pub fn get_cache_metadata_path(&self) -> Result<PathBuf> {
64+
let id = self.get_cache_identifier()?;
65+
Ok(parameter_cache::parameter_cache_metadata_path(&id))
6466
}
6567

66-
pub fn get_cache_verifying_key_path(&self) -> PathBuf {
67-
let id = self.get_cache_identifier();
68-
parameter_cache::parameter_cache_verifying_key_path(&id)
68+
pub fn get_cache_verifying_key_path(&self) -> Result<PathBuf> {
69+
let id = self.get_cache_identifier()?;
70+
Ok(parameter_cache::parameter_cache_verifying_key_path(&id))
6971
}
7072

71-
pub fn get_cache_params_path(&self) -> PathBuf {
72-
let id = self.get_cache_identifier();
73-
parameter_cache::parameter_cache_params_path(&id)
73+
pub fn get_cache_params_path(&self) -> Result<PathBuf> {
74+
let id = self.get_cache_identifier()?;
75+
Ok(parameter_cache::parameter_cache_params_path(&id))
7476
}
7577
}

filecoin-proofs/src/types/post_config.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::path::PathBuf;
22

3+
use anyhow::Result;
4+
35
use paired::bls12_381::Bls12;
46
use storage_proofs::circuit::election_post::{ElectionPoStCircuit, ElectionPoStCompound};
57
use storage_proofs::drgraph::DefaultTreeHasher;
@@ -30,28 +32,30 @@ impl From<PoStConfig> for UnpaddedBytesAmount {
3032

3133
impl PoStConfig {
3234
/// Returns the cache identifier as used by `storage-proofs::paramater_cache`.
33-
pub fn get_cache_identifier(self) -> String {
34-
let params = crate::parameters::post_public_params(self);
35-
36-
<ElectionPoStCompound<DefaultTreeHasher> as CacheableParameters<
37-
Bls12,
38-
ElectionPoStCircuit<_, DefaultTreeHasher>,
39-
_,
40-
>>::cache_identifier(&params)
35+
pub fn get_cache_identifier(self) -> Result<String> {
36+
let params = crate::parameters::post_public_params(self)?;
37+
38+
Ok(
39+
<ElectionPoStCompound<DefaultTreeHasher> as CacheableParameters<
40+
Bls12,
41+
ElectionPoStCircuit<_, DefaultTreeHasher>,
42+
_,
43+
>>::cache_identifier(&params),
44+
)
4145
}
4246

43-
pub fn get_cache_metadata_path(self) -> PathBuf {
44-
let id = self.get_cache_identifier();
45-
parameter_cache::parameter_cache_metadata_path(&id)
47+
pub fn get_cache_metadata_path(self) -> Result<PathBuf> {
48+
let id = self.get_cache_identifier()?;
49+
Ok(parameter_cache::parameter_cache_metadata_path(&id))
4650
}
4751

48-
pub fn get_cache_verifying_key_path(self) -> PathBuf {
49-
let id = self.get_cache_identifier();
50-
parameter_cache::parameter_cache_verifying_key_path(&id)
52+
pub fn get_cache_verifying_key_path(self) -> Result<PathBuf> {
53+
let id = self.get_cache_identifier()?;
54+
Ok(parameter_cache::parameter_cache_verifying_key_path(&id))
5155
}
5256

53-
pub fn get_cache_params_path(self) -> PathBuf {
54-
let id = self.get_cache_identifier();
55-
parameter_cache::parameter_cache_params_path(&id)
57+
pub fn get_cache_params_path(self) -> Result<PathBuf> {
58+
let id = self.get_cache_identifier()?;
59+
Ok(parameter_cache::parameter_cache_params_path(&id))
5660
}
5761
}

storage-proofs/benches/encode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn encode_single_node<H: Hasher>(
5454

5555
let node_data = H::Domain::try_from_bytes(&data[start..end]).unwrap();
5656
let key_data = H::Domain::try_from_bytes(&key).unwrap();
57-
let encoded = H::sloth_encode(&key_data, &node_data);
57+
let encoded = H::sloth_encode(&key_data, &node_data).unwrap();
5858
encoded.write_bytes(&mut data[start..end]).unwrap();
5959
}
6060

storage-proofs/src/circuit/create_label.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ where
4444
let fr = if alloc_bits[0].get_value().is_some() {
4545
let be_bits = alloc_bits
4646
.iter()
47-
.map(|v| v.get_value().unwrap())
48-
.collect::<Vec<bool>>();
47+
.map(|v| v.get_value().ok_or(SynthesisError::AssignmentMissing))
48+
.collect::<Result<Vec<bool>, SynthesisError>>()?;
4949

5050
let le_bits = be_bits
5151
.chunks(8)
@@ -119,7 +119,7 @@ mod tests {
119119
acc
120120
});
121121

122-
let expected = crypto::create_label::create_label(input_bytes.as_slice(), m);
122+
let expected = crypto::create_label::create_label(input_bytes.as_slice(), m).unwrap();
123123

124124
assert_eq!(
125125
expected,
@@ -178,7 +178,7 @@ mod tests {
178178
input_bytes.extend_from_slice(parent);
179179
}
180180

181-
let expected = crypto::create_label::create_label(input_bytes.as_slice(), m);
181+
let expected = crypto::create_label::create_label(input_bytes.as_slice(), m).unwrap();
182182

183183
assert_eq!(
184184
expected,

storage-proofs/src/circuit/variables.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::fmt;
22

3+
use anyhow::Result;
4+
35
use bellperson::gadgets::num::AllocatedNum;
46
use bellperson::{ConstraintSystem, SynthesisError};
57
use paired::Engine;
@@ -39,8 +41,8 @@ impl<E: Engine> Root<E> {
3941
}
4042
}
4143

42-
pub fn var<CS: ConstraintSystem<E>>(cs: CS, fr: E::Fr) -> Self {
43-
Root::Var(AllocatedNum::alloc(cs, || Ok(fr)).unwrap())
44+
pub fn var<CS: ConstraintSystem<E>>(cs: CS, fr: E::Fr) -> Result<Self> {
45+
Ok(Root::Var(AllocatedNum::alloc(cs, || Ok(fr))?))
4446
}
4547

4648
pub fn is_some(&self) -> bool {

storage-proofs/src/crypto/create_label.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
use anyhow::Result;
12
use ff::PrimeField;
23
use paired::bls12_381::Fr;
34
use sha2::{Digest, Sha256};
45

56
use crate::fr32::bytes_into_fr_repr_safe;
67

78
/// Key derivation function, based on pedersen hashing.
8-
pub fn create_label(data: &[u8], _m: usize) -> Fr {
9+
pub fn create_label(data: &[u8], _m: usize) -> Result<Fr> {
910
let hash = Sha256::digest(data);
10-
Fr::from_repr(bytes_into_fr_repr_safe(hash.as_ref())).unwrap()
11+
Ok(Fr::from_repr(bytes_into_fr_repr_safe(hash.as_ref()))?)
1112
}
1213

1314
#[cfg(test)]
@@ -30,7 +31,7 @@ mod tests {
3031
];
3132
let expected = Fr::from_repr(FrRepr(repr)).unwrap();
3233

33-
let res = create_label(&data, m);
34+
let res = create_label(&data, m).unwrap();
3435
assert_eq!(res, expected);
3536
}
3637
}

storage-proofs/src/drgporep.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ where
459459
let end = start + NODE_SIZE;
460460

461461
let node_data = H::Domain::try_from_bytes(&data[start..end])?;
462-
let encoded = H::sloth_encode(key.as_ref(), &node_data);
462+
let encoded = H::sloth_encode(key.as_ref(), &node_data)?;
463463

464464
encoded.write_bytes(&mut data[start..end])?;
465465
}

storage-proofs/src/hasher/blake2s.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl Hasher for Blake2sHasher {
2626
"Blake2sHasher".into()
2727
}
2828

29-
fn create_label(data: &[u8], m: usize) -> Self::Domain {
29+
fn create_label(data: &[u8], m: usize) -> Result<Self::Domain> {
3030
assert_eq!(
3131
data.len(),
3232
32 * (1 + m),
@@ -35,20 +35,20 @@ impl Hasher for Blake2sHasher {
3535
m
3636
);
3737

38-
<Self::Function as HashFunction<Self::Domain>>::hash(data)
38+
Ok(<Self::Function as HashFunction<Self::Domain>>::hash(data))
3939
}
4040

41-
fn sloth_encode(key: &Self::Domain, ciphertext: &Self::Domain) -> Self::Domain {
41+
fn sloth_encode(key: &Self::Domain, ciphertext: &Self::Domain) -> Result<Self::Domain> {
4242
// TODO: validate this is how sloth should work in this case
4343
let k = (*key).into();
4444
let c = (*ciphertext).into();
4545

46-
sloth::encode::<Bls12>(&k, &c).into()
46+
Ok(sloth::encode::<Bls12>(&k, &c).into())
4747
}
4848

49-
fn sloth_decode(key: &Self::Domain, ciphertext: &Self::Domain) -> Self::Domain {
49+
fn sloth_decode(key: &Self::Domain, ciphertext: &Self::Domain) -> Result<Self::Domain> {
5050
// TODO: validate this is how sloth should work in this case
51-
sloth::decode::<Bls12>(&(*key).into(), &(*ciphertext).into()).into()
51+
Ok(sloth::decode::<Bls12>(&(*key).into(), &(*ciphertext).into()).into())
5252
}
5353
}
5454

@@ -247,8 +247,8 @@ impl HashFunction<Blake2sDomain> for Blake2sFunction {
247247
Some(_) => {
248248
let bits = alloc_bits
249249
.iter()
250-
.map(|v| v.get_value().unwrap())
251-
.collect::<Vec<bool>>();
250+
.map(|v| v.get_value().ok_or(SynthesisError::AssignmentMissing))
251+
.collect::<std::result::Result<Vec<bool>, SynthesisError>>()?;
252252
// TODO: figure out if we can avoid this
253253
let frs = multipack::compute_multipacking::<E>(&bits);
254254
Ok(frs[0])

0 commit comments

Comments
 (0)