Skip to content

Commit

Permalink
Remove unwrap (#1260)
Browse files Browse the repository at this point in the history
* build(storage-proofs): clippy allow many_single_char_names

clippy emits warning:

        warning: 8 bindings with single-character names in scope

The single character names are a valid use case in this instance,
elements of an array. Elect to allow the usage; instruct clippy to
allow this lint for function `insert_8`.

* refactor(fil-proofs-tooling): remove all uses of unwrap()

Using `expect()` with a useful argument string assists when debugging.

Replace all instances of `unwrap()` with `expect()`.

Makes a start at resolving: #390

* refactor(filecoin-proofs): remove all uses of unwrap()

Using `expect()` with a useful argument string assists when debugging.

Replace all instances of `unwrap()` with `expect()`.

Works towards resolving: #390

* build(fil-proofs-tooling): set clippy lint warn for: unwrap_used

We have removed all the usages of `unwrap()` in `fil-proofs-tooling\`.

In order to resolve #390 instruct clippy to warn for lint
`unwrap_used` for all binaries and the main library within
fil-proofs-tooling.

* build(filecoin-proofs): set clippy lint warn for: unwrap_used

We have removed all the usages of `unwrap()` in `filecoin-proofs\`.

In order to resolve #390 instruct clippy to warn for lint
`unwrap_used` for the filecoin-proofs library.

* build(storage-proofs): set clippy lint warn for: unwrap_used

We have removed all the usages of `unwrap()` in `storage-proofs\`.

In order to resolve #390 instruct clippy to warn for lint
`unwrap_used` for all libraries in store-proofs.

* - fix: manually apply phase2 unwrap changes

* fix: replace all remaining usages of unwrap

For this round of updates, I used nightly to detect all instances, but
have reverted after making sure they've been replaced.

Co-authored-by: tcharding <me@tobin.cc>
  • Loading branch information
cryptonemo and tcharding authored Aug 24, 2020
1 parent d3edf8b commit 4a07a86
Show file tree
Hide file tree
Showing 56 changed files with 646 additions and 375 deletions.
19 changes: 12 additions & 7 deletions fil-proofs-tooling/src/bin/benchy/hash_fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ fn blake2s_count(bytes: usize) -> anyhow::Result<Report> {

let data_bits: Vec<Boolean> = {
let mut cs = cs.namespace(|| "data");
bytes_into_boolean_vec(&mut cs, Some(data.as_slice()), data.len()).unwrap()
bytes_into_boolean_vec(&mut cs, Some(data.as_slice()), data.len())
.expect("failed to convert to boolean vector")
};

let personalization = vec![0u8; 8];
let out: Vec<bool> =
bellperson::gadgets::blake2s::blake2s(&mut cs, &data_bits, &personalization)?
.into_iter()
.map(|b| b.get_value().unwrap())
.map(|b| b.get_value().expect("failed to get bool value"))
.collect();

assert!(cs.is_satisfied(), "constraints not satisfied");
Expand Down Expand Up @@ -53,12 +54,13 @@ fn sha256_count(bytes: usize) -> anyhow::Result<Report> {

let data_bits: Vec<Boolean> = {
let mut cs = cs.namespace(|| "data");
bytes_into_boolean_vec_be(&mut cs, Some(data.as_slice()), data.len()).unwrap()
bytes_into_boolean_vec_be(&mut cs, Some(data.as_slice()), data.len())
.expect("failed to convert bytes into boolean vector big endian")
};

let _out: Vec<bool> = bellperson::gadgets::sha256::sha256(&mut cs, &data_bits)?
.into_iter()
.map(|b| b.get_value().unwrap())
.map(|b| b.get_value().expect("failed to get bool value"))
.collect();

assert!(cs.is_satisfied(), "constraints not satisfied");
Expand All @@ -79,7 +81,8 @@ fn pedersen_count(bytes: usize) -> anyhow::Result<Report> {

let data_bits: Vec<Boolean> = {
let mut cs = cs.namespace(|| "data");
bytes_into_boolean_vec(&mut cs, Some(data.as_slice()), data.len()).unwrap()
bytes_into_boolean_vec(&mut cs, Some(data.as_slice()), data.len())
.expect("failed to convert bytes into boolean vector")
};

if bytes < 128 {
Expand All @@ -89,7 +92,8 @@ fn pedersen_count(bytes: usize) -> anyhow::Result<Report> {
let expected = crypto::pedersen::pedersen(data.as_slice());
assert_eq!(
expected,
out.get_value().unwrap(),
out.get_value()
.expect("failed to get value from pedersen num"),
"circuit and non circuit do not match"
);
} else {
Expand All @@ -99,7 +103,8 @@ fn pedersen_count(bytes: usize) -> anyhow::Result<Report> {
let expected = crypto::pedersen::pedersen_md_no_padding(data.as_slice());
assert_eq!(
expected,
out.get_value().unwrap(),
out.get_value()
.expect("failed to get value from pedersen md"),
"circuit and non circuit do not match"
);
}
Expand Down
5 changes: 4 additions & 1 deletion fil-proofs-tooling/src/bin/benchy/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//requires nightly, or later stable version
//#![warn(clippy::unwrap_used)]

use std::io::{stdin, stdout};

use anyhow::Result;
Expand Down Expand Up @@ -197,7 +200,7 @@ fn main() -> Result<()> {
}
("prodbench", Some(m)) => {
let inputs: ProdbenchInputs = if m.is_present("config") {
let file = value_t!(m, "config", String).unwrap();
let file = value_t!(m, "config", String).expect("failed to get config");
serde_json::from_reader(
std::fs::File::open(&file)
.unwrap_or_else(|_| panic!("invalid file {:?}", file)),
Expand Down
2 changes: 1 addition & 1 deletion fil-proofs-tooling/src/bin/benchy/merkleproofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub fn run_merkleproofs_bench<Tree: 'static + MerkleTreeTrait>(
let (_data, tree) = generate_tree::<Tree, _>(
&mut rng,
base_tree_leaves * tree_count,
Some(temp_path.to_path_buf()),
Some(temp_path.clone()),
);
generate_proofs::<_, Tree>(
&mut rng,
Expand Down
20 changes: 10 additions & 10 deletions fil-proofs-tooling/src/bin/benchy/prodbench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub struct ProdbenchInputs {

impl ProdbenchInputs {
pub fn sector_size_bytes(&self) -> u64 {
bytefmt::parse(&self.sector_size).unwrap()
bytefmt::parse(&self.sector_size).expect("failed to parse sector size")
}
}

Expand Down Expand Up @@ -155,15 +155,15 @@ fn augment_with_op_measurements(mut output: &mut ProdbenchOutputs) {
fn configure_global_config(inputs: &ProdbenchInputs) {
filecoin_proofs::constants::LAYERS
.write()
.unwrap()
.expect("LAYERS poisoned")
.insert(inputs.sector_size_bytes(), inputs.stacked_layers as usize);
filecoin_proofs::constants::POREP_PARTITIONS
.write()
.unwrap()
.expect("POREP_PARTITIONS poisoned")
.insert(inputs.sector_size_bytes(), inputs.porep_partitions);
filecoin_proofs::constants::POREP_MINIMUM_CHALLENGES
.write()
.unwrap()
.expect("POREP_MINIMUM_CHALLENGES poisoned")
.insert(inputs.sector_size_bytes(), inputs.porep_challenges);
}

Expand Down Expand Up @@ -196,7 +196,7 @@ pub fn run(
.expect("failed to retrieve metadata");
}

let (created, replica_measurement) = repls.unwrap();
let (created, replica_measurement) = repls.expect("unreachable: only_add_piece==false");
generate_params(&inputs);

if !skip_seal_proof {
Expand Down Expand Up @@ -292,14 +292,14 @@ fn measure_porep_circuit(i: &ProdbenchInputs) -> usize {
layer_challenges,
};

let pp = StackedDrg::<ProdbenchTree, Sha256Hasher>::setup(&sp).unwrap();
let pp = StackedDrg::<ProdbenchTree, Sha256Hasher>::setup(&sp).expect("failed to setup DRG");

let mut cs = BenchCS::<Bls12>::new();
<StackedCompound<_, _> as CompoundProof<StackedDrg<ProdbenchTree, Sha256Hasher>, _>>::blank_circuit(
&pp,
)
.synthesize(&mut cs)
.unwrap();
.synthesize(&mut cs)
.expect("failed to synthesize stacked compound");

cs.num_constraints()
}
Expand All @@ -309,7 +309,7 @@ fn generate_params(i: &ProdbenchInputs) {
let partitions = PoRepProofPartitions(
*POREP_PARTITIONS
.read()
.unwrap()
.expect("POREP_PARTITIONS poisoned")
.get(&i.sector_size_bytes())
.expect("unknown sector size"),
);
Expand All @@ -336,7 +336,7 @@ fn cache_porep_params(porep_config: PoRepConfig) {
usize::from(PoRepProofPartitions::from(porep_config)),
dummy_porep_id,
)
.unwrap();
.expect("failed to get public_params");

{
let circuit = <StackedCompound<ProdbenchTree, _> as CompoundProof<
Expand Down
8 changes: 4 additions & 4 deletions fil-proofs-tooling/src/bin/benchy/window_post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ fn get_porep_config(sector_size: u64) -> PoRepConfig {
partitions: PoRepProofPartitions(
*POREP_PARTITIONS
.read()
.unwrap()
.expect("POREP_PARTITONS poisoned")
.get(&(sector_size))
.unwrap(),
.expect("unknown sector size"),
),
porep_id: arbitrary_porep_id,
}
Expand Down Expand Up @@ -462,9 +462,9 @@ pub fn run_window_post_bench<Tree: 'static + MerkleTreeTrait>(
challenge_count: WINDOW_POST_CHALLENGE_COUNT,
sector_count: *WINDOW_POST_SECTOR_COUNT
.read()
.unwrap()
.expect("WINDOW_POST_SECTOR_COUNT poisoned")
.get(&sector_size)
.unwrap(),
.expect("unknown sector size"),
typ: PoStType::Window,
priority: true,
};
Expand Down
15 changes: 9 additions & 6 deletions fil-proofs-tooling/src/bin/gpu-cpu-test/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//requires nightly, or later stable version
//#![warn(clippy::unwrap_used)]

use std::collections::HashMap;
use std::process::{self, Child, Command, Stdio};
use std::str;
Expand Down Expand Up @@ -154,7 +157,7 @@ fn threads_mode(parallel: u8, gpu_stealing: bool) {
thread::sleep(timeout);
info!("Waited long enough to kill all threads");
for tx in senders {
tx.send(()).unwrap();
tx.send(()).expect("tx channel send failed");
}

for thread in &mut threads {
Expand All @@ -164,7 +167,7 @@ fn threads_mode(parallel: u8, gpu_stealing: bool) {
.name()
.unwrap_or(&format!("{:?}", handler.thread().id()))
.to_string();
let run_info = handler.join().unwrap();
let run_info = handler.join().expect("thread being joined has panicked");
info!("Thread {} info: {:?}", thread_name, run_info);
// Also print it, so that we can get that information in processes mode
println!("Thread {} info: {:?}", thread_name, run_info);
Expand All @@ -190,11 +193,11 @@ fn processes_mode(parallel: u8, gpu_stealing: bool) {

// Wait for all processes to finish and log their output
for (name, child) in children {
let output = child.wait_with_output().unwrap();
let output = child.wait_with_output().expect("failed to wait for child");
info!(
"Process {} info: {}",
name,
str::from_utf8(&output.stdout).unwrap()
str::from_utf8(&output.stdout).expect("failed to parse UTF-8")
);
}
}
Expand Down Expand Up @@ -249,13 +252,13 @@ fn main() {
)
.get_matches();

let parallel = value_t!(matches, "parallel", u8).unwrap();
let parallel = value_t!(matches, "parallel", u8).expect("failed to get parallel");
if parallel == 1 {
info!("Running high priority proof only")
} else {
info!("Running high and low priority proofs in parallel")
}
let gpu_stealing = value_t!(matches, "gpu-stealing", bool).unwrap();
let gpu_stealing = value_t!(matches, "gpu-stealing", bool).expect("failed to get gpu-stealing");
if gpu_stealing {
info!("Force low piority proofs to CPU")
} else {
Expand Down
15 changes: 8 additions & 7 deletions fil-proofs-tooling/src/bin/micro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn parse_criterion_out(s: impl AsRef<str>) -> Result<Vec<CriterionResult>> {
for line in s.as_ref().lines() {
if let Some(caps) = start_re.captures(line) {
if current.is_some() {
let r = current.take().unwrap();
let r = current.take().expect("unreachable: is_some()");
res.push(CriterionResult {
name: r.0,
samples: r.1.unwrap_or_default(),
Expand Down Expand Up @@ -175,8 +175,8 @@ fn parse_criterion_out(s: impl AsRef<str>) -> Result<Vec<CriterionResult>> {
// R^2
if let Some(caps) = r_2_re.captures(line) {
current.9 = Some(Interval {
start: caps[1].parse().unwrap(),
end: caps[2].parse().unwrap(),
start: caps[1].parse().expect("failed to parse caps[1] string"),
end: caps[2].parse().expect("failed to parse caps[2] string"),
unit: None,
});
}
Expand All @@ -202,7 +202,7 @@ fn parse_criterion_out(s: impl AsRef<str>) -> Result<Vec<CriterionResult>> {
}

if current.is_some() {
let r = current.take().unwrap();
let r = current.take().expect("unreachable: is_some()");
res.push(CriterionResult {
name: r.0,
samples: r.1.unwrap_or_default(),
Expand Down Expand Up @@ -277,7 +277,7 @@ fn run_benches(mut args: Vec<String>) -> Result<()> {
let reader = std::io::BufReader::new(stdout);
let mut stdout = String::new();
reader.lines().for_each(|line| {
let line = line.unwrap();
let line = line.expect("io (stdout) read error");
if is_verbose {
println!("{}", &line);
}
Expand Down Expand Up @@ -339,7 +339,7 @@ slope [141.11 us 159.66 us] R^2 [0.8124914 0.8320154]
mean [140.55 us 150.62 us] std. dev. [5.6028 us 15.213 us]
median [138.33 us 143.23 us] med. abs. dev. [1.7507 ms 8.4109 ms]";

let parsed = parse_criterion_out(stdout).unwrap();
let parsed = parse_criterion_out(stdout).expect("failed to parse criterion output");
assert_eq!(
parsed,
vec![CriterionResult {
Expand Down Expand Up @@ -410,7 +410,8 @@ slope [141.11 us 159.66 us] R^2 [0.8124914 0.8320154]
mean [140.55 us 150.62 us] std. dev. [5.6028 us 15.213 us]
median [138.33 us 143.23 us] med. abs. dev. [1.7507 ms 8.4109 ms]";

let parsed = parse_criterion_out(with_throughput).unwrap();
let parsed =
parse_criterion_out(with_throughput).expect("failed to parse criterion output");
assert_eq!(
parsed,
vec![CriterionResult {
Expand Down
3 changes: 3 additions & 0 deletions fil-proofs-tooling/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//requires nightly, or later stable version
//#![warn(clippy::unwrap_used)]

pub mod measure;
pub mod metadata;
pub mod shared;
Expand Down
2 changes: 1 addition & 1 deletion fil-proofs-tooling/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ mod tests {

#[test]
fn test_metadata() {
let m = Metadata::wrap(()).unwrap();
let m = Metadata::wrap(()).expect("failed to create metadata");
println!("{:#?}", m);

assert!(m.system.memory_total_bytes > 0);
Expand Down
11 changes: 7 additions & 4 deletions fil-proofs-tooling/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ pub fn create_piece(piece_bytes: UnpaddedBytesAmount) -> NamedTempFile {
}
assert_eq!(
u64::from(piece_bytes),
file.as_file().metadata().unwrap().len()
file.as_file()
.metadata()
.expect("failed to get file metadata")
.len()
);

file.as_file_mut()
Expand All @@ -70,7 +73,7 @@ pub fn create_replica<Tree: 'static + MerkleTreeTrait>(
create_replicas::<Tree>(SectorSize(sector_size), 1, false, porep_id);
// Extract the sector ID and replica output out of the result
result
.unwrap()
.expect("create_replicas() failed when called with only_add==false")
.0
.pop()
.expect("failed to create replica outputs")
Expand Down Expand Up @@ -98,7 +101,7 @@ pub fn create_replicas<Tree: 'static + MerkleTreeTrait>(
partitions: PoRepProofPartitions(
*POREP_PARTITIONS
.read()
.unwrap()
.expect("poisoned read access")
.get(&u64::from(sector_size))
.expect("unknown sector size"),
),
Expand Down Expand Up @@ -154,7 +157,7 @@ pub fn create_replicas<Tree: 'static + MerkleTreeTrait>(
sector_size_unpadded_bytes_ammount,
&[],
)
.unwrap();
.expect("failed to add piece");
piece_infos.push(vec![info]);
}

Expand Down
10 changes: 5 additions & 5 deletions filecoin-proofs/benches/preprocessing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use rand::{thread_rng, Rng};
fn start_profile(stage: &str) {
gperftools::profiler::PROFILER
.lock()
.unwrap()
.expect("PROFILER poisoned")
.start(format!("./{}.profile", stage))
.unwrap();
.expect("failed to start profiler");
}

#[cfg(not(feature = "cpu-profile"))]
Expand All @@ -24,9 +24,9 @@ fn start_profile(_stage: &str) {}
fn stop_profile() {
gperftools::profiler::PROFILER
.lock()
.unwrap()
.expect("PROFILER poisoned")
.stop()
.unwrap();
.expect("failed to start profiler");
}

#[cfg(not(feature = "cpu-profile"))]
Expand All @@ -50,7 +50,7 @@ fn preprocessing_benchmark(c: &mut Criterion) {
start_profile(&format!("write_padded_{}", *size));
b.iter(|| {
let mut reader = Fr32Reader::new(io::Cursor::new(&data));
reader.read_to_end(&mut buf).unwrap();
reader.read_to_end(&mut buf).expect("in memory read error");
assert!(buf.len() >= data.len());
});
stop_profile();
Expand Down
Loading

0 comments on commit 4a07a86

Please sign in to comment.