diff --git a/src/fs.rs b/src/fs.rs index e48e3133bb9a94a9706e87009ed488d5e479240e..cea8ee549f41f95b52442b04433474cd08ac423b 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -14,9 +14,6 @@ use tracing::info; use crate::Block; -const COMPRESS: Compress = Compress::Yes; -const VALIDATE: Validate = Validate::Yes; - /// dump any serializable object to the disk /// /// - `dumpable` can be anything that is _serializable_ @@ -30,10 +27,11 @@ pub fn dump( dumpable: &impl CanonicalSerialize, dump_dir: &Path, filename: Option<&str>, + compress: Compress, ) -> Result<String> { info!("serializing the dumpable"); - let mut serialized = vec![0; dumpable.serialized_size(COMPRESS)]; - dumpable.serialize_with_mode(&mut serialized[..], COMPRESS)?; + let mut serialized = vec![0; dumpable.serialized_size(compress)]; + dumpable.serialize_with_mode(&mut serialized[..], compress)?; let filename = match filename { Some(filename) => filename.to_string(), @@ -55,12 +53,16 @@ pub fn dump( /// dump a bunch of blocks to the disk and return a JSON / NUON compatible table /// of all the hashes that have been dumped -pub fn dump_blocks<E: Pairing>(blocks: &[Block<E>], block_dir: &PathBuf) -> Result<String> { +pub fn dump_blocks<E: Pairing>( + blocks: &[Block<E>], + block_dir: &PathBuf, + compress: Compress, +) -> Result<String> { info!("dumping blocks to `{:?}`", block_dir); let mut hashes = vec![]; std::fs::create_dir_all(block_dir)?; for block in blocks.iter() { - let hash = dump(block, block_dir, None)?; + let hash = dump(block, block_dir, None, compress)?; hashes.push(hash); } @@ -77,6 +79,8 @@ pub fn dump_blocks<E: Pairing>(blocks: &[Block<E>], block_dir: &PathBuf) -> Resu pub fn read_blocks<E: Pairing>( block_hashes: &[String], block_dir: &Path, + compress: Compress, + validate: Validate, ) -> Result<Vec<(String, Block<E>)>> { block_hashes .iter() @@ -85,7 +89,7 @@ pub fn read_blocks<E: Pairing>( let s = std::fs::read(filename)?; Ok(( f.clone(), - Block::<E>::deserialize_with_mode(&s[..], COMPRESS, VALIDATE)?, + Block::<E>::deserialize_with_mode(&s[..], compress, validate)?, )) }) .collect() diff --git a/src/main.rs b/src/main.rs index 68f4dbe497d9a177ff6aece49e05ced00d501212..06ce83332f2371c2d8e54f5546969ed9a144315f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -131,7 +131,7 @@ where info!("generating new powers"); let powers = setup::random::<E, P>(n)?; - fs::dump(&powers, powers_dir, powers_filename)?; + fs::dump(&powers, powers_dir, powers_filename, COMPRESS)?; Ok(()) } @@ -194,15 +194,16 @@ fn main() { } if do_reconstruct_data { - let blocks: Vec<Shard<Bls12_381>> = fs::read_blocks::<Bls12_381>(&block_hashes, &block_dir) - .unwrap_or_else(|e| { - throw_error(1, &format!("could not read blocks: {}", e)); - unreachable!() - }) - .iter() - .cloned() - .map(|b| b.1.shard) - .collect(); + let blocks: Vec<Shard<Bls12_381>> = + fs::read_blocks::<Bls12_381>(&block_hashes, &block_dir, COMPRESS, VALIDATE) + .unwrap_or_else(|e| { + throw_error(1, &format!("could not read blocks: {}", e)); + unreachable!() + }) + .iter() + .cloned() + .map(|b| b.1.shard) + .collect(); eprintln!( "{:?}", decode::<Bls12_381>(blocks).unwrap_or_else(|e| { @@ -215,10 +216,11 @@ fn main() { } if do_combine_blocks { - let blocks = fs::read_blocks::<Bls12_381>(&block_hashes, &block_dir).unwrap_or_else(|e| { - throw_error(1, &format!("could not read blocks: {}", e)); - unreachable!() - }); + let blocks = fs::read_blocks::<Bls12_381>(&block_hashes, &block_dir, COMPRESS, VALIDATE) + .unwrap_or_else(|e| { + throw_error(1, &format!("could not read blocks: {}", e)); + unreachable!() + }); let formatted_output = fs::dump_blocks( &[ @@ -233,6 +235,7 @@ fn main() { }), ], &block_dir, + COMPRESS, ) .unwrap_or_else(|e| { throw_error(1, &format!("could not dump block: {}", e)); @@ -245,10 +248,11 @@ fn main() { } if do_inspect_blocks { - let blocks = fs::read_blocks::<Bls12_381>(&block_hashes, &block_dir).unwrap_or_else(|e| { - throw_error(1, &format!("could not read blocks: {}", e)); - unreachable!() - }); + let blocks = fs::read_blocks::<Bls12_381>(&block_hashes, &block_dir, COMPRESS, VALIDATE) + .unwrap_or_else(|e| { + throw_error(1, &format!("could not read blocks: {}", e)); + unreachable!() + }); eprint!("["); for (_, block) in &blocks { eprint!("{},", block); @@ -280,10 +284,11 @@ fn main() { if do_verify_blocks { verify_blocks::<Bls12_381, UniPoly12_381>( - &fs::read_blocks::<Bls12_381>(&block_hashes, &block_dir).unwrap_or_else(|e| { - throw_error(1, &format!("could not read blocks: {}", e)); - unreachable!() - }), + &fs::read_blocks::<Bls12_381>(&block_hashes, &block_dir, COMPRESS, VALIDATE) + .unwrap_or_else(|e| { + throw_error(1, &format!("could not read blocks: {}", e)); + unreachable!() + }), powers, ) .unwrap_or_else(|e| { @@ -316,6 +321,7 @@ fn main() { unreachable!() }), &block_dir, + COMPRESS, ) .unwrap_or_else(|e| { throw_error(1, &format!("could not dump blocks: {}", e));