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;
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()
......
......@@ -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));
......
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