Skip to content

Commit

Permalink
feat(pallet-proofs): verify PoSt (#461)
Browse files Browse the repository at this point in the history
  • Loading branch information
th7nder authored Oct 23, 2024
1 parent 183b91e commit 31108c1
Show file tree
Hide file tree
Showing 18 changed files with 616 additions and 199 deletions.
5 changes: 4 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion cli/polka-storage-provider/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ sc-cli = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
subxt = { workspace = true }
tempfile = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
Expand Down
59 changes: 25 additions & 34 deletions cli/polka-storage-provider/client/src/commands/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ const POST_PARAMS_EXT: &str = "post.params";
const POST_VK_EXT: &str = "post.vk";
const POST_VK_EXT_SCALE: &str = "post.vk.scale";

const POREP_PROOF_EXT: &str = "proof.porep.scale";
const POST_PROOF_EXT: &str = "proof.post.scale";
const POREP_PROOF_EXT: &str = "sector.proof.porep.scale";
const POST_PROOF_EXT: &str = "sector.proof.post.scale";

impl UtilsCommand {
/// Run the command.
Expand Down Expand Up @@ -187,18 +187,21 @@ impl UtilsCommand {
output_path,
cache_directory,
} => {
// Those are hardcoded for the showcase only.
// They should come from Storage Provider Node, precommits and other information.
let sector_id = 77;
let prover_id = [0u8; 32];
let ticket = [12u8; 32];
let seed = [13u8; 32];

let output_path = if let Some(output_path) = output_path {
output_path
} else {
std::env::current_dir()?
};
let (proof_scale_filename, mut proof_scale_file) = file_with_extension(
&output_path,
input_path
.file_name()
.expect("input file to have a name")
.to_str()
.expect("to be convertable to str"),
format!("{}", sector_id).as_str(),
POREP_PROOF_EXT,
)?;

Expand Down Expand Up @@ -235,15 +238,11 @@ impl UtilsCommand {
size: *piece_file_length,
};

// Those are hardcoded for the showcase only.
// They should come from Storage Provider Node, precommits and other information.
let sector_id = 77;
let prover_id = [0u8; 32];
let ticket = [12u8; 32];
let seed = [13u8; 32];

let mut unsealed_sector =
tempfile::NamedTempFile::new().map_err(|e| UtilsCommandError::IOError(e))?;
let (unsealed_sector_path, unsealed_sector) = file_with_extension(
&output_path,
format!("{}", sector_id).as_str(),
"sector.unsealed",
)?;

let (sealed_sector_path, _) = file_with_extension(
&output_path,
Expand All @@ -254,17 +253,14 @@ impl UtilsCommand {
println!("Creating sector...");
let sealer = Sealer::new(seal_proof);
let piece_infos = sealer
.create_sector(
vec![(piece_file, piece_info.clone())],
unsealed_sector.as_file_mut(),
)
.create_sector(vec![(piece_file, piece_info.clone())], unsealed_sector)
.map_err(|e| UtilsCommandError::GeneratePoRepError(e))?;

println!("Precommitting...");
let precommit = sealer
.precommit_sector(
&cache_directory,
unsealed_sector.path(),
unsealed_sector_path,
&sealed_sector_path,
prover_id,
sector_id,
Expand Down Expand Up @@ -348,6 +344,12 @@ impl UtilsCommand {
comm_r,
output_path,
} => {
// Those are hardcoded for the showcase only.
// They should come from Storage Provider Node, precommits and other information.
let sector_id = 77;
let randomness = [1u8; 32];
let prover_id = [0u8; 32];

let output_path = if let Some(output_path) = output_path {
output_path
} else {
Expand All @@ -356,19 +358,10 @@ impl UtilsCommand {

let (proof_scale_filename, mut proof_scale_file) = file_with_extension(
&output_path,
replica_path
.file_name()
.expect("input file to have a name")
.to_str()
.expect("to be convertable to str"),
format!("{}", sector_id).as_str(),
POST_PROOF_EXT,
)?;

// Those are hardcoded for the showcase only.
// They should come from Storage Provider Node, precommits and other information.
let sector_id = 77;
let randomness = [1u8; 32];
let prover_id = [0u8; 32];
let replicas = vec![ReplicaInfo {
sector_id,
comm_r: hex::decode(comm_r)
Expand Down Expand Up @@ -425,8 +418,6 @@ pub enum UtilsCommandError {
InvalidPieceFile(PathBuf, std::io::Error),
#[error("provided invalid CommP {0}, error: {1}")]
InvalidPieceCommP(String, cid::Error),
#[error(transparent)]
IOError(std::io::Error),
#[error("file {0} is invalid CARv2 file {1}")]
InvalidCARv2(PathBuf, mater::Error),
}
Expand All @@ -440,7 +431,7 @@ fn file_with_extension(
new_path.push(file_name);
new_path.set_extension(extension);

let file = std::fs::File::create_new(new_path.clone())
let file = std::fs::File::create(new_path.clone())
.map_err(|e| UtilsCommandError::FileCreateError(new_path.clone(), e))?;
Ok((new_path, file))
}
12 changes: 12 additions & 0 deletions lib/polka-storage-proofs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,15 @@ pub use groth16::*;

#[cfg(feature = "std")]
pub mod porep;

/// Calculates the number of partitions existing in `total_sector_count` for `sector_count`.
///
/// Reference:
/// * <https://github.com/filecoin-project/rust-fil-proofs/blob/266acc39a3ebd6f3d28c6ee335d78e2b7cea06bc/filecoin-proofs/src/api/post_util.rs#L217>
pub fn get_partitions_for_window_post(
total_sector_count: usize,
sector_count: usize,
) -> Option<usize> {
let partitions = total_sector_count.div_ceil(sector_count);
(partitions > 1).then_some(partitions)
}
18 changes: 5 additions & 13 deletions lib/polka-storage-proofs/src/post/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ use storage_proofs_post::fallback::{
self, FallbackPoSt, FallbackPoStCompound, PrivateSector, PublicSector,
};

use crate::types::{Commitment, ProverId, Ticket};
use crate::{
get_partitions_for_window_post,
types::{Commitment, ProverId, Ticket},
};

/// Generates parameters for proving and verifying PoSt.
/// It should be called once and then reused across provers and the verifier.
Expand Down Expand Up @@ -86,7 +89,7 @@ pub fn generate_window_post<CacheDirectory: AsRef<Path>>(
let prover_id_safe = as_safe_commitment(&prover_id, "prover_id")?;

let vanilla_params = window_post_setup_params(&post_config);
let partitions = get_partitions_for_window_post(replicas.len(), &post_config);
let partitions = get_partitions_for_window_post(replicas.len(), post_config.sector_count);

let sector_count = vanilla_params.sector_count;
let setup_params = compound_proof::SetupParams {
Expand Down Expand Up @@ -165,14 +168,3 @@ pub enum PoStError {
#[error("failed to load groth16 parameters from path: {0}, because {1}")]
FailedToLoadGrothParameters(std::path::PathBuf, std::io::Error),
}

/// Reference:
/// * <https://github.com/filecoin-project/rust-fil-proofs/blob/266acc39a3ebd6f3d28c6ee335d78e2b7cea06bc/filecoin-proofs/src/api/post_util.rs#L217>
fn get_partitions_for_window_post(
total_sector_count: usize,
post_config: &filecoin_proofs::PoStConfig,
) -> Option<usize> {
let partitions = (total_sector_count as f32 / post_config.sector_count as f32).ceil() as usize;

(partitions > 1).then_some(partitions)
}
22 changes: 14 additions & 8 deletions maat/tests/real_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,20 @@ where
);
}

let retrieved_peer_id = client
.retrieve_storage_provider(&subxt::utils::AccountId32::from(
charlie.account_id().clone(),
))
.await
.unwrap()
// this last unwrap ensures there's something there
.unwrap();
let retrieved_peer_id = String::from_utf8(
client
.retrieve_storage_provider(&subxt::utils::AccountId32::from(
charlie.account_id().clone(),
))
.await
.unwrap()
// this last unwrap ensures there's something there
.unwrap()
.info
.peer_id
.0,
)
.unwrap();

assert_eq!(retrieved_peer_id, peer_id_bs58);
}
Expand Down
4 changes: 4 additions & 0 deletions pallets/proofs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ ff = { workspace = true }
frame-benchmarking = { optional = true, workspace = true }
frame-support.workspace = true
frame-system.workspace = true
log = { workspace = true }
num-bigint = { workspace = true }
polka-storage-proofs = { workspace = true, features = ["substrate"] }
primitives-commitment = { workspace = true }
primitives-proofs = { workspace = true }
rand = { workspace = true, features = ["alloc"] }
rand_chacha = { workspace = true }
scale-info = { features = ["derive"], workspace = true }
sha2 = { workspace = true }
sp-runtime.workspace = true
sp-std.workspace = true

[dev-dependencies]
blstrs = { workspace = true }
Expand Down Expand Up @@ -62,5 +65,6 @@ std = [
"polka-storage-proofs/std",
"scale-info/std",
"sp-runtime/std",
"sp-std/std",
]
try-runtime = ["frame-support/try-runtime", "frame-system/try-runtime", "sp-runtime/try-runtime"]
Loading

0 comments on commit 31108c1

Please sign in to comment.