Skip to content
Snippets Groups Projects
Commit f1f2d7f6 authored by STEVAN Antoine's avatar STEVAN Antoine :crab:
Browse files

fix hardcoded compress and validate values in `fs` module (!53)

`fs` was using hardcoded `COMPRESS` and `VALIDATE`, now it uses values passed by the caller :thumbsup: 

thanks @n.dissoubray :wink:
parent 4273d869
No related branches found
No related tags found
1 merge request!53fix hardcoded compress and validate values in `fs` module
Pipeline #4658 passed
...@@ -14,9 +14,6 @@ use tracing::info; ...@@ -14,9 +14,6 @@ use tracing::info;
use crate::Block; use crate::Block;
const COMPRESS: Compress = Compress::Yes;
const VALIDATE: Validate = Validate::Yes;
/// dump any serializable object to the disk /// dump any serializable object to the disk
/// ///
/// - `dumpable` can be anything that is _serializable_ /// - `dumpable` can be anything that is _serializable_
...@@ -30,10 +27,11 @@ pub fn dump( ...@@ -30,10 +27,11 @@ pub fn dump(
dumpable: &impl CanonicalSerialize, dumpable: &impl CanonicalSerialize,
dump_dir: &Path, dump_dir: &Path,
filename: Option<&str>, filename: Option<&str>,
compress: Compress,
) -> Result<String> { ) -> Result<String> {
info!("serializing the dumpable"); info!("serializing the dumpable");
let mut serialized = vec![0; dumpable.serialized_size(COMPRESS)]; let mut serialized = vec![0; dumpable.serialized_size(compress)];
dumpable.serialize_with_mode(&mut serialized[..], COMPRESS)?; dumpable.serialize_with_mode(&mut serialized[..], compress)?;
let filename = match filename { let filename = match filename {
Some(filename) => filename.to_string(), Some(filename) => filename.to_string(),
...@@ -55,12 +53,16 @@ pub fn dump( ...@@ -55,12 +53,16 @@ pub fn dump(
/// dump a bunch of blocks to the disk and return a JSON / NUON compatible table /// dump a bunch of blocks to the disk and return a JSON / NUON compatible table
/// of all the hashes that have been dumped /// 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); info!("dumping blocks to `{:?}`", block_dir);
let mut hashes = vec![]; let mut hashes = vec![];
std::fs::create_dir_all(block_dir)?; std::fs::create_dir_all(block_dir)?;
for block in blocks.iter() { for block in blocks.iter() {
let hash = dump(block, block_dir, None)?; let hash = dump(block, block_dir, None, compress)?;
hashes.push(hash); hashes.push(hash);
} }
...@@ -77,6 +79,8 @@ pub fn dump_blocks<E: Pairing>(blocks: &[Block<E>], block_dir: &PathBuf) -> Resu ...@@ -77,6 +79,8 @@ pub fn dump_blocks<E: Pairing>(blocks: &[Block<E>], block_dir: &PathBuf) -> Resu
pub fn read_blocks<E: Pairing>( pub fn read_blocks<E: Pairing>(
block_hashes: &[String], block_hashes: &[String],
block_dir: &Path, block_dir: &Path,
compress: Compress,
validate: Validate,
) -> Result<Vec<(String, Block<E>)>> { ) -> Result<Vec<(String, Block<E>)>> {
block_hashes block_hashes
.iter() .iter()
...@@ -85,7 +89,7 @@ pub fn read_blocks<E: Pairing>( ...@@ -85,7 +89,7 @@ pub fn read_blocks<E: Pairing>(
let s = std::fs::read(filename)?; let s = std::fs::read(filename)?;
Ok(( Ok((
f.clone(), f.clone(),
Block::<E>::deserialize_with_mode(&s[..], COMPRESS, VALIDATE)?, Block::<E>::deserialize_with_mode(&s[..], compress, validate)?,
)) ))
}) })
.collect() .collect()
......
...@@ -131,7 +131,7 @@ where ...@@ -131,7 +131,7 @@ where
info!("generating new powers"); info!("generating new powers");
let powers = setup::random::<E, P>(n)?; let powers = setup::random::<E, P>(n)?;
fs::dump(&powers, powers_dir, powers_filename)?; fs::dump(&powers, powers_dir, powers_filename, COMPRESS)?;
Ok(()) Ok(())
} }
...@@ -194,15 +194,16 @@ fn main() { ...@@ -194,15 +194,16 @@ fn main() {
} }
if do_reconstruct_data { if do_reconstruct_data {
let blocks: Vec<Shard<Bls12_381>> = fs::read_blocks::<Bls12_381>(&block_hashes, &block_dir) let blocks: Vec<Shard<Bls12_381>> =
.unwrap_or_else(|e| { fs::read_blocks::<Bls12_381>(&block_hashes, &block_dir, COMPRESS, VALIDATE)
throw_error(1, &format!("could not read blocks: {}", e)); .unwrap_or_else(|e| {
unreachable!() throw_error(1, &format!("could not read blocks: {}", e));
}) unreachable!()
.iter() })
.cloned() .iter()
.map(|b| b.1.shard) .cloned()
.collect(); .map(|b| b.1.shard)
.collect();
eprintln!( eprintln!(
"{:?}", "{:?}",
decode::<Bls12_381>(blocks).unwrap_or_else(|e| { decode::<Bls12_381>(blocks).unwrap_or_else(|e| {
...@@ -215,10 +216,11 @@ fn main() { ...@@ -215,10 +216,11 @@ fn main() {
} }
if do_combine_blocks { if do_combine_blocks {
let blocks = fs::read_blocks::<Bls12_381>(&block_hashes, &block_dir).unwrap_or_else(|e| { let blocks = fs::read_blocks::<Bls12_381>(&block_hashes, &block_dir, COMPRESS, VALIDATE)
throw_error(1, &format!("could not read blocks: {}", e)); .unwrap_or_else(|e| {
unreachable!() throw_error(1, &format!("could not read blocks: {}", e));
}); unreachable!()
});
let formatted_output = fs::dump_blocks( let formatted_output = fs::dump_blocks(
&[ &[
...@@ -233,6 +235,7 @@ fn main() { ...@@ -233,6 +235,7 @@ fn main() {
}), }),
], ],
&block_dir, &block_dir,
COMPRESS,
) )
.unwrap_or_else(|e| { .unwrap_or_else(|e| {
throw_error(1, &format!("could not dump block: {}", e)); throw_error(1, &format!("could not dump block: {}", e));
...@@ -245,10 +248,11 @@ fn main() { ...@@ -245,10 +248,11 @@ fn main() {
} }
if do_inspect_blocks { if do_inspect_blocks {
let blocks = fs::read_blocks::<Bls12_381>(&block_hashes, &block_dir).unwrap_or_else(|e| { let blocks = fs::read_blocks::<Bls12_381>(&block_hashes, &block_dir, COMPRESS, VALIDATE)
throw_error(1, &format!("could not read blocks: {}", e)); .unwrap_or_else(|e| {
unreachable!() throw_error(1, &format!("could not read blocks: {}", e));
}); unreachable!()
});
eprint!("["); eprint!("[");
for (_, block) in &blocks { for (_, block) in &blocks {
eprint!("{},", block); eprint!("{},", block);
...@@ -280,10 +284,11 @@ fn main() { ...@@ -280,10 +284,11 @@ fn main() {
if do_verify_blocks { if do_verify_blocks {
verify_blocks::<Bls12_381, UniPoly12_381>( verify_blocks::<Bls12_381, UniPoly12_381>(
&fs::read_blocks::<Bls12_381>(&block_hashes, &block_dir).unwrap_or_else(|e| { &fs::read_blocks::<Bls12_381>(&block_hashes, &block_dir, COMPRESS, VALIDATE)
throw_error(1, &format!("could not read blocks: {}", e)); .unwrap_or_else(|e| {
unreachable!() throw_error(1, &format!("could not read blocks: {}", e));
}), unreachable!()
}),
powers, powers,
) )
.unwrap_or_else(|e| { .unwrap_or_else(|e| {
...@@ -316,6 +321,7 @@ fn main() { ...@@ -316,6 +321,7 @@ fn main() {
unreachable!() unreachable!()
}), }),
&block_dir, &block_dir,
COMPRESS,
) )
.unwrap_or_else(|e| { .unwrap_or_else(|e| {
throw_error(1, &format!("could not dump blocks: {}", e)); throw_error(1, &format!("could not dump blocks: {}", e));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment