校验 p2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.5 KiB

3 years ago
  1. use anyhow::Result;
  2. use filecoin_proofs_v1::{constants::SectorShape32GiB, verify_seal, VerifyInput};
  3. use std::env;
  4. use std::fs::File;
  5. use std::io::Read;
  6. use std::ops::Range;
  7. use std::path::Path;
  8. use std::process::exit;
  9. fn main() {
  10. let args: Vec<String> = env::args().collect();
  11. if args.len() == 1{
  12. println!("用法:\n./verify-tool <sector_id> // 校验单个扇区\n./verify-tool <sector_id>..<sector_id> // (检验范围内的扇区, 不包含最后一个)");
  13. exit(0);
  14. }
  15. if args.len() > 2 {
  16. println!("格式错误");
  17. exit(0);
  18. }
  19. if let Ok(sector_id) = args[1].parse::<u64>() {
  20. verify_single(sector_id);
  21. } else {
  22. let mut split = args[1].split("..");
  23. if split.clone().count() != 2 {
  24. println!("sector_id 格式错误");
  25. exit(0);
  26. }
  27. let start = split.next().unwrap().parse().unwrap();
  28. let end = split.next().unwrap().parse().unwrap();
  29. verify_range(start..end);
  30. }
  31. }
  32. fn verify_range(range: Range<u64>) {
  33. for sector in range {
  34. verify_single(sector);
  35. }
  36. }
  37. fn verify_single(sector_id: u64) {
  38. if let Ok(path) = env::var("VERIFY_INPUT_DIR") {
  39. let path = Path::new(&path).join(format!("{}", sector_id));
  40. if path.exists() {
  41. let mut file = File::open(&path).unwrap();
  42. let mut buf: Vec<u8> = Vec::new();
  43. file.read(&mut buf).unwrap();
  44. let decode: VerifyInput = bincode::deserialize(&buf).unwrap();
  45. match verify_call(decode) {
  46. Ok(true) => {
  47. println!("sector: {} 校验通过", sector_id);
  48. }
  49. Ok(false) => {
  50. println!("sector: {} 未通过", sector_id);
  51. }
  52. Err(err) => {
  53. println!("校验失败, {}", err);
  54. }
  55. }
  56. } else {
  57. println!("sector {} verify 文件不存在", sector_id);
  58. }
  59. } else {
  60. println!("请设置 VERIFY_INPUT_DIR");
  61. exit(0);
  62. }
  63. }
  64. fn verify_call(input: VerifyInput) -> Result<bool> {
  65. let VerifyInput {
  66. porep_config,
  67. comm_r_in,
  68. comm_d_in,
  69. prover_id,
  70. sector_id,
  71. ticket,
  72. seed,
  73. proof_vec,
  74. } = input;
  75. verify_seal::<SectorShape32GiB>(
  76. porep_config,
  77. comm_r_in,
  78. comm_d_in,
  79. prover_id,
  80. sector_id,
  81. ticket,
  82. seed,
  83. &proof_vec,
  84. )
  85. }