Skip to content

Commit

Permalink
refactor: better comments, errors and more consistent naming
Browse files Browse the repository at this point in the history
  • Loading branch information
vmx committed Aug 5, 2020
1 parent 1748c67 commit 9d1617b
Showing 1 changed file with 50 additions and 18 deletions.
68 changes: 50 additions & 18 deletions filecoin-proofs/src/bin/phase2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,7 @@ fn main() {
let split_keys_command = SubCommand::with_name("split-keys")
.about("Splits the keys from the trusted setup into parameter files")
.arg(
Arg::with_name("path-input")
Arg::with_name("input-path")
.required(true)
.help("The path to the file that contains all the data."),
);
Expand Down Expand Up @@ -1462,28 +1462,36 @@ fn main() {
convert_small(path_before)
}
"split-keys" => {
let path_input = matches.value_of("path-input").unwrap();
let input_path = matches
.value_of("input-path")
.expect("failed to read input-path argument");

println!("reading params: {}", path_input);
println!("reading params: {}", input_path);

// Get the identifier for the output files based in the input file's name
let (proof, _hasher, sector_size_enum, _head, param_num, param_size, _read_raw) =
parse_params_filename(path_input);
parse_params_filename(input_path);
assert!(param_size.is_large(), "params must be large");
let sector_size = sector_size_enum.as_u64();
let identifier = with_shape!(sector_size, parameter_identifier, sector_size, proof);

let mut input_file = File::open(path_input).unwrap();
let mut input_file = File::open(input_path)
.unwrap_or_else(|_| panic!("failed to open {}", input_path));

// Extract the vk data into its own file
// Extract the vk data into its own file.
{
let vk_data = groth16::VerifyingKey::<Bls12>::read(&input_file)
.expect("failed to deserialize vk from input file");
let vk_path = verifying_key_id(&identifier);
println!("writing verifying key to file: {}", vk_path);
let mut vk_file = File::create(&vk_path).unwrap();
vk_data.write(&mut vk_file).unwrap();
let vk_file_size = vk_file.seek(SeekFrom::Current(0)).unwrap();
let mut vk_file = File::create(&vk_path)
.unwrap_or_else(|_| panic!("failed to create {}", vk_path));
vk_data.write(&mut vk_file).unwrap_or_else(|_| {
panic!("failed to write verification keys to file {}", vk_path)
});
let vk_file_size = vk_file
.seek(SeekFrom::Current(0))
.unwrap_or_else(|_| panic!("failed to seek in {}", vk_path));
println!("size of the verifying key is {} bytes", vk_file_size);
}

Expand All @@ -1492,22 +1500,35 @@ fn main() {
{
let params_path = parameter_id(&identifier);
println!("writing parameters to file: {}", params_path);
let mut params_file = File::create(&params_path).unwrap();
let mut params_file = File::create(&params_path)
.unwrap_or_else(|_| panic!("failed to create {}", params_path));

// input_file_size - cs_hash - contributions_length -
// (num_contributions * public_key_size)
let params_file_size =
input_file.metadata().unwrap().len() - 64 - 4 - (param_num as u64 * 544);
let params_file_size = input_file
.metadata()
.unwrap_or_else(|_| panic!("failed to get filesize of {}", input_path))
.len()
- 64
- 4
- (param_num as u64 * 544);
println!("size of the parameters file is {} bytes", params_file_size);
// Make sure the cursor is at the beginning of the file (it was moved
// during the extraction of the vk data)
input_file.seek(SeekFrom::Start(0)).unwrap();
input_file
.seek(SeekFrom::Start(0))
.expect("cannot seek to beginning of the input file");

io::copy(
&mut Read::by_ref(&mut input_file).take(params_file_size),
&mut params_file,
)
.unwrap();
.unwrap_or_else(|_| {
panic!(
"Failed to copy params from {} to {}",
input_path, params_path
)
});
}

// Writing the contributions to disk is not needed for the final parameters,
Expand All @@ -1516,21 +1537,32 @@ fn main() {
let contribs_path =
format!("v{}-{}.contribs", parameter_cache::VERSION, &identifier);
println!("writing contributions to file: {}", contribs_path);
let mut contribs_file = File::create(&contribs_path).unwrap();
let mut contribs_file = File::create(&contribs_path)
.unwrap_or_else(|_| panic!("failed to create {}", contribs_path));
// The input file is already sought to the right offset, due to writing the
// params file
let contribs_file_size = io::copy(&mut input_file, &mut contribs_file).unwrap();
let contribs_file_size = io::copy(&mut input_file, &mut contribs_file)
.unwrap_or_else(|_| {
panic!(
"Failed to copy contributions from {} to {}",
input_path, contribs_path
)
});
println!(
"size of the contributions file is {} bytes",
contribs_file_size
);
}

// The metadata is needed for the publication of the parameters.
{
let meta_path = metadata_id(&identifier);
println!("writing metadata to file: {}", meta_path);
let mut meta_file = File::create(&meta_path).unwrap();
write!(&mut meta_file, r#"{{"sector_size":{}}}"#, sector_size).unwrap();
let mut meta_file = File::create(&meta_path)
.unwrap_or_else(|_| panic!("failed to create {}", meta_path));
write!(&mut meta_file, r#"{{"sector_size":{}}}"#, sector_size).unwrap_or_else(
|_| panic!("failed to write meta information to {}", meta_path),
);
}
}
_ => unreachable!(),
Expand Down

0 comments on commit 9d1617b

Please sign in to comment.