Skip to content

Commit

Permalink
Less asserts (#965)
Browse files Browse the repository at this point in the history
Less asserts
  • Loading branch information
dignifiedquire committed Dec 3, 2019
2 parents 3abf8c7 + aa41575 commit f5a2e0a
Show file tree
Hide file tree
Showing 37 changed files with 567 additions and 416 deletions.
2 changes: 1 addition & 1 deletion fil-proofs-tooling/src/bin/benchy/stacked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ pub struct RunOpts {
}

pub fn run(opts: RunOpts) -> anyhow::Result<()> {
let config = StackedConfig::new(opts.layers, opts.window_challenges, opts.wrapper_challenges);
let config = StackedConfig::new(opts.layers, opts.window_challenges, opts.wrapper_challenges)?;

let params = Params {
config,
Expand Down
3 changes: 2 additions & 1 deletion filecoin-proofs/examples/stacked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,8 @@ fn main() {
let circuit = matches.is_present("circuit");
let extract = matches.is_present("extract");

let config = StackedConfig::new(layers, window_challenge_count, wrapper_challenge_count);
let config =
StackedConfig::new(layers, window_challenge_count, wrapper_challenge_count).unwrap();

info!("hasher: {}", hasher);
match hasher.as_ref() {
Expand Down
27 changes: 12 additions & 15 deletions filecoin-proofs/src/api/seal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fs::{self, File, OpenOptions};
use std::io::prelude::*;
use std::path::Path;

use anyhow::Result;
use anyhow::{Context, Result};
use bincode::{deserialize, serialize};
use memmap::MmapOptions;
use merkletree::store::{StoreConfig, DEFAULT_CACHED_ABOVE_BASE_LAYER};
Expand Down Expand Up @@ -243,20 +243,17 @@ pub fn seal_commit<T: AsRef<Path>>(

// Verification is cheap when parameters are cached,
// and it is never correct to return a proof which does not verify.
assert!(
verify_seal(
porep_config,
comm_r,
comm_d,
prover_id,
sector_id,
ticket,
seed,
&buf,
)
.expect("post-seal verification sanity check failed"),
"invalid seal generated, bad things have happened"
);
verify_seal(
porep_config,
comm_r,
comm_d,
prover_id,
sector_id,
ticket,
seed,
&buf,
)
.context("post-seal verification sanity check failed")?;

info!("seal_commit:end");

Expand Down
19 changes: 14 additions & 5 deletions filecoin-proofs/src/fr32.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::cmp::min;
use std::io::{self, Error, ErrorKind, Read, Seek, SeekFrom, Write};

use anyhow::{ensure, Result};
use bitvec::{BitVec, LittleEndian};

/** PaddingMap represents a mapping between data and its padded equivalent.
Expand Down Expand Up @@ -255,16 +256,24 @@ impl BitByte {
}

impl PaddingMap {
pub fn new(data_bits: usize, element_bits: usize) -> PaddingMap {
pub fn new(data_bits: usize, element_bits: usize) -> Result<PaddingMap> {
// Check that we add less than 1 byte of padding (sub-byte padding).
assert!(element_bits - data_bits <= 7);
ensure!(
element_bits - data_bits <= 7,
"Padding (num bits: {}) must be less than 1 byte.",
element_bits - data_bits
);
// Check that the element is byte aligned.
assert_eq!(element_bits % 8, 0);
ensure!(
element_bits % 8 == 0,
"Element (num bits: {}) must be byte aligned.",
element_bits
);

PaddingMap {
Ok(PaddingMap {
data_bits,
element_bits,
}
})
}

pub fn pad(&self, bits_out: &mut BitVecLEu8) {
Expand Down
26 changes: 15 additions & 11 deletions filecoin-proofs/src/parameters.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Result;
use anyhow::{ensure, Result};
use storage_proofs::drgraph::{DefaultTreeHasher, BASE_DEGREE};
use storage_proofs::election_post::{self, ElectionPoSt};
use storage_proofs::proof::ProofScheme;
Expand Down Expand Up @@ -57,24 +57,24 @@ pub fn setup_params(
sector_bytes: PaddedBytesAmount,
partitions: usize,
) -> Result<stacked::SetupParams> {
let window_challenges = select_challenges(partitions, POREP_WINDOW_MINIMUM_CHALLENGES, LAYERS);
let window_challenges = select_challenges(partitions, POREP_WINDOW_MINIMUM_CHALLENGES, LAYERS)?;
let wrapper_challenges =
select_challenges(partitions, POREP_WRAPPER_MINIMUM_CHALLENGES, LAYERS);
let window_size_nodes = window_size_nodes_for_sector_bytes(sector_bytes).unwrap();
select_challenges(partitions, POREP_WRAPPER_MINIMUM_CHALLENGES, LAYERS)?;
let window_size_nodes = window_size_nodes_for_sector_bytes(sector_bytes)?;
let sector_bytes = usize::from(sector_bytes);

let config = StackedConfig {
window_challenges,
wrapper_challenges,
};

assert!(
ensure!(
sector_bytes % 32 == 0,
"sector_bytes ({}) must be a multiple of 32",
sector_bytes,
);

assert!(
ensure!(
sector_bytes % window_size_nodes * 32 == 0,
"sector_bytes ({}) must be a multiple of the window size ({})",
sector_bytes,
Expand All @@ -96,14 +96,14 @@ fn select_challenges(
partitions: usize,
minimum_total_challenges: usize,
layers: usize,
) -> LayerChallenges {
) -> Result<LayerChallenges> {
let mut count = 1;
let mut guess = LayerChallenges::new(layers, count);
let mut guess = LayerChallenges::new(layers, count)?;
while partitions * guess.challenges_count_all() < minimum_total_challenges {
count += 1;
guess = LayerChallenges::new(layers, count);
guess = LayerChallenges::new(layers, count)?;
}
guess
Ok(guess)
}

#[cfg(test)]
Expand All @@ -114,7 +114,11 @@ mod tests {

#[test]
fn partition_layer_challenges_test() {
let f = |partitions| select_challenges(partitions, 12, LAYERS).challenges_count_all();
let f = |partitions| {
select_challenges(partitions, 12, LAYERS)
.unwrap()
.challenges_count_all()
};
// Update to ensure all supported PoRepProofPartitions options are represented here.
assert_eq!(6, f(usize::from(PoRepProofPartitions(2))));

Expand Down
51 changes: 30 additions & 21 deletions filecoin-proofs/src/pieces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,17 @@ pub fn compute_comm_d(sector_size: SectorSize, piece_infos: &[PieceInfo]) -> Res
);

while stack.peek().size < piece_info.size {
stack.shift_reduce(zero_padding(stack.peek().size))
stack.shift_reduce(zero_padding(stack.peek().size)?)?
}

stack.shift_reduce(piece_info.clone());
stack.shift_reduce(piece_info.clone())?;
}

while stack.len() > 1 {
stack.shift_reduce(zero_padding(stack.peek().size));
stack.shift_reduce(zero_padding(stack.peek().size)?)?;
}

assert_eq!(stack.len(), 1);
ensure!(stack.len() == 1, "Stack size ({}) must be 1.", stack.len());

let comm_d_calculated = stack.pop().commitment;

Expand Down Expand Up @@ -111,29 +111,30 @@ impl Stack {
self.0.pop().expect("empty stack popped")
}

pub fn reduce1(&mut self) -> bool {
pub fn reduce1(&mut self) -> Result<bool> {
if self.len() < 2 {
return false;
return Ok(false);
}

if self.peek().size == self.peek2().size {
let right = self.pop();
let left = self.pop();
let joined = join_piece_infos(left, right);
let joined = join_piece_infos(left, right)?;
self.shift(joined);
return true;
return Ok(true);
}

false
Ok(false)
}

pub fn reduce(&mut self) {
while self.reduce1() {}
pub fn reduce(&mut self) -> Result<()> {
while self.reduce1()? {}
Ok(())
}

pub fn shift_reduce(&mut self, piece: PieceInfo) {
pub fn shift_reduce(&mut self, piece: PieceInfo) -> Result<()> {
self.shift(piece);
self.reduce();
self.reduce()
}

pub fn len(&self) -> usize {
Expand All @@ -142,7 +143,7 @@ impl Stack {
}

/// Create a padding `PieceInfo` of size `size`.
fn zero_padding(size: UnpaddedBytesAmount) -> PieceInfo {
fn zero_padding(size: UnpaddedBytesAmount) -> Result<PieceInfo> {
let padded_size: PaddedBytesAmount = size.into();
let mut commitment = [0u8; 32];

Expand All @@ -157,19 +158,27 @@ fn zero_padding(size: UnpaddedBytesAmount) -> PieceInfo {
hashed_size *= 2;
}

assert_eq!(hashed_size, u64::from(padded_size));
ensure!(
hashed_size == u64::from(padded_size),
"Hashed size must equal padded size"
);

PieceInfo { size, commitment }
Ok(PieceInfo { size, commitment })
}

/// Join two equally sized `PieceInfo`s together, by hashing them and adding their sizes.
fn join_piece_infos(mut left: PieceInfo, right: PieceInfo) -> PieceInfo {
assert_eq!(left.size, right.size);
fn join_piece_infos(mut left: PieceInfo, right: PieceInfo) -> Result<PieceInfo> {
ensure!(
left.size == right.size,
"Piece sizes must be equal (left: {:?}, right: {:?})",
left.size,
right.size
);
let h = piece_hash(&left.commitment, &right.commitment);

left.commitment.copy_from_slice(AsRef::<[u8]>::as_ref(&h));
left.size = left.size + right.size;
left
Ok(left)
}

fn piece_hash(a: &[u8], b: &[u8]) -> <DefaultPieceHasher as Hasher>::Domain {
Expand Down Expand Up @@ -436,7 +445,7 @@ mod tests {
// ]

let sector_size = SectorSize(32 * 128);
let pad = zero_padding(UnpaddedBytesAmount(127));
let pad = zero_padding(UnpaddedBytesAmount(127)).unwrap();

let pieces = vec![
PieceInfo {
Expand Down Expand Up @@ -637,7 +646,7 @@ mod tests {
BASE_DEGREE,
EXP_DEGREE,
new_seed(),
);
)?;

let mut staged_sector = Vec::with_capacity(u64::from(sector_size) as usize);
let mut staged_sector_io = std::io::Cursor::new(&mut staged_sector);
Expand Down
4 changes: 2 additions & 2 deletions storage-proofs/benches/drgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn drgraph(c: &mut Criterion) {
.iter()
.map(|n| {
(
BucketGraph::<PedersenHasher>::new(*n, BASE_DEGREE, 0, new_seed()),
BucketGraph::<PedersenHasher>::new(*n, BASE_DEGREE, 0, new_seed()).unwrap(),
2,
)
})
Expand All @@ -22,7 +22,7 @@ fn drgraph(c: &mut Criterion) {
|b, (graph, i)| {
b.iter(|| {
let mut parents = vec![0; 6];
black_box(graph.parents(*i, &mut parents));
black_box(graph.parents(*i, &mut parents).unwrap());
})
},
params,
Expand Down
2 changes: 1 addition & 1 deletion storage-proofs/benches/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn pregenerate_data<H: Hasher>(degree: usize) -> Pregenerated<H> {
let parents: Vec<u32> = (0..degree as u32).map(|pos| pos).collect();
let replica_id: H::Domain = H::Domain::random(&mut rng);

let graph = StackedBucketGraph::<H>::new_stacked(degree + 1, degree, 0, new_seed());
let graph = StackedBucketGraph::<H>::new_stacked(degree + 1, degree, 0, new_seed()).unwrap();

Pregenerated {
data,
Expand Down
6 changes: 4 additions & 2 deletions storage-proofs/benches/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ fn merkle_benchmark(c: &mut Criterion) {
BASE_DEGREE,
EXP_DEGREE,
new_seed(),
);
)
.unwrap();

b.iter(|| black_box(graph.merkle_tree(&data).unwrap()))
},
Expand All @@ -40,7 +41,8 @@ fn merkle_benchmark(c: &mut Criterion) {
BASE_DEGREE,
EXP_DEGREE,
new_seed(),
);
)
.unwrap();

b.iter(|| black_box(graph.merkle_tree(&data).unwrap()))
})
Expand Down
4 changes: 2 additions & 2 deletions storage-proofs/benches/parents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ fn stop_profile() {}

fn pregenerate_graph<H: Hasher>(size: usize) -> StackedBucketGraph<H> {
let seed = [1u8; 28];
StackedBucketGraph::<H>::new_stacked(size, BASE_DEGREE, EXP_DEGREE, seed)
StackedBucketGraph::<H>::new_stacked(size, BASE_DEGREE, EXP_DEGREE, seed).unwrap()
}

fn parents_loop<H: Hasher, G: Graph<H>>(graph: &G, parents: &mut [u32]) {
(0..graph.size())
.map(|node| graph.parents(node, parents))
.map(|node| graph.parents(node, parents).unwrap())
.collect()
}

Expand Down

0 comments on commit f5a2e0a

Please sign in to comment.