Skip to content

Commit

Permalink
fix: pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
th7nder committed Nov 5, 2024
1 parent 0486178 commit 6042593
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 21 deletions.
1 change: 0 additions & 1 deletion cli/polka-storage-provider/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ pub struct ServerConfiguration {

/// Proving Parameters for PoRep proof
/// For 2KiB sectors they're ~1GiB of data.
/// We may load them on demand someday.
porep_parameters: PoRepParameters,
}

Expand Down
47 changes: 31 additions & 16 deletions cli/polka-storage-provider/server/src/pipeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod types;
use std::{path::PathBuf, sync::Arc};

use polka_storage_proofs::porep::{
sealer::{prepare_piece, PreCommitOutput, Proof, Sealer, SubstrateProof},
sealer::{prepare_piece, BlstrsProof, PreCommitOutput, Sealer, SubstrateProof},
PoRepError, PoRepParameters,
};
use polka_storage_provider_common::rpc::ServerInfo;
Expand Down Expand Up @@ -49,9 +49,9 @@ pub enum PipelineError {
#[error(transparent)]
DBError(#[from] DBError),
#[error("sector does not exist")]
NotExistentSector,
SectorNotFound,
#[error("precommit scheduled too early, randomness not available")]
NotAvailableRandomness,
RandomnessNotAvailable,
#[error(transparent)]
SendError(#[from] SendError<PipelineMessage>),
}
Expand Down Expand Up @@ -252,7 +252,7 @@ async fn precommit(
let sealer = Sealer::new(state.server_info.seal_proof);
let Some(mut sector) = state.db.get_sector(sector_number)? else {
tracing::error!("Tried to precommit non-existing sector");
return Err(PipelineError::NotExistentSector);
return Err(PipelineError::SectorNotFound);
};
// Pad sector so CommD can be properly calculated.
sector.piece_infos = sealer.pad_sector(&sector.piece_infos, sector.occupied_sector_space)?;
Expand All @@ -265,7 +265,7 @@ async fn precommit(

let Some(digest) = state.xt_client.get_randomness(current_block).await? else {
tracing::error!("Precommit was scheduled too early, when the randomness on-chain was not yet available...");
return Err(PipelineError::NotAvailableRandomness);
return Err(PipelineError::RandomnessNotAvailable);
};
let entropy = state.xt_keypair.account_id().encode();
// Must match pallet's logic or otherwise proof won't be verified:
Expand Down Expand Up @@ -376,17 +376,19 @@ async fn prove_commit(
let sealer = Sealer::new(state.server_info.seal_proof);
let Some(mut sector) = state.db.get_sector(sector_number)? else {
tracing::error!("Tried to precommit non-existing sector");
return Err(PipelineError::NotExistentSector);
return Err(PipelineError::SectorNotFound);
};

let seal_randomness_height = sector.seal_randomness_height.unwrap();
let seal_randomness_height = sector
.seal_randomness_height
.expect("sector to be sealed before proving using randomness from the chain");
let Some(digest) = state
.xt_client
.get_randomness(seal_randomness_height)
.await?
else {
tracing::error!("Out-of-the-state transition, this SHOULD not happen");
return Err(PipelineError::NotAvailableRandomness);
return Err(PipelineError::RandomnessNotAvailable);
};
let entropy = state.xt_keypair.account_id().encode();
// Must match pallet's logic or otherwise proof won't be verified:
Expand All @@ -402,7 +404,10 @@ async fn prove_commit(
// https://github.com/eigerco/polka-storage/blob/5edd4194f08f29d769c277577ccbb70bb6ff63bc/runtime/src/configs/mod.rs#L360
// 10 blocks = 1 minute, only testnet
const PRECOMMIT_CHALLENGE_DELAY: u64 = 10;
let prove_commit_block = sector.precommit_block.unwrap() + PRECOMMIT_CHALLENGE_DELAY;
let precommit_block = sector
.precommit_block
.expect("sector to be pre-committed on-chain before proving");
let prove_commit_block = precommit_block + PRECOMMIT_CHALLENGE_DELAY;

tracing::info!("Wait for block {} to get randomness", prove_commit_block);
state
Expand All @@ -411,7 +416,7 @@ async fn prove_commit(
.await?;
let Some(digest) = state.xt_client.get_randomness(prove_commit_block).await? else {
tracing::error!("Randomness for the block not available.");
return Err(PipelineError::NotAvailableRandomness);
return Err(PipelineError::RandomnessNotAvailable);
};
let seed = draw_randomness(
&digest,
Expand All @@ -422,16 +427,26 @@ async fn prove_commit(

let prover_id = derive_prover_id(state.xt_keypair.account_id());
tracing::debug!("Performing prove commit for, seal_randomness_height {}, pre_commit_block: {}, prove_commit_block: {}, entropy: {}, ticket: {}, seed: {}",
seal_randomness_height, sector.precommit_block.unwrap(), prove_commit_block, hex::encode(entropy), hex::encode(ticket), hex::encode(seed));
tracing::debug!("Prover Id: {}, Sector Number: {}", hex::encode(prover_id), sector_number);
seal_randomness_height, precommit_block, prove_commit_block, hex::encode(entropy), hex::encode(ticket), hex::encode(seed));
tracing::debug!(
"Prover Id: {}, Sector Number: {}",
hex::encode(prover_id),
sector_number
);

let sealing_handle: JoinHandle<Result<Vec<Proof>, _>> = {
let sealing_handle: JoinHandle<Result<Vec<BlstrsProof>, _>> = {
let porep_params = state.porep_parameters.clone();
let cache_dir = state.sealing_cache_dir.clone();
let sealed_path = sector.sealed_path.clone();
let piece_infos = sector.piece_infos.clone();
let comm_r = sector.comm_r.unwrap().raw();
let comm_d = sector.comm_d.unwrap().raw();
let comm_r = sector
.comm_r
.expect("sector to be sealed and it's comm_r set before proving")
.raw();
let comm_d = sector
.comm_d
.expect("sector to be sealed and it's comm_d set before proving")
.raw();
tokio::task::spawn_blocking(move || {
sealer.prove_sector(
porep_params.as_ref(),
Expand Down Expand Up @@ -471,7 +486,7 @@ async fn prove_commit(
true,
)
.await?
.unwrap();
.expect("waiting for finalization should always give results");

let proven_sectors = result
.events
Expand Down
22 changes: 19 additions & 3 deletions cli/polka-storage-provider/server/src/pipeline/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ use storagext::types::market::DealProposal;
/// Represents a task to be executed on the Storage Provider Pipeline
#[derive(Debug)]
pub enum PipelineMessage {
/// Adds a deal to a sector selected by the storage provider
/// Adds a deal to a sector selected by the storage provider.
AddPiece(AddPieceMessage),
/// Pads, seals the sector and pre-commits it on chain
/// Pads, seals a sector and pre-commits it on-chain.
PreCommit(PreCommitMessage),
/// Generates a PoRep for a sector and verifies the proof on-chain.
ProveCommit(ProveCommitMessage),
}

Expand Down Expand Up @@ -73,9 +74,23 @@ pub struct Sector {
/// Only after pipeline [`PipelineMessage::PreCommit`],
/// the file has contents which should not be touched and are used for later steps.
pub sealed_path: std::path::PathBuf,
/// CID of the sealed sector.
///
/// Available at [`SectorState::Sealed`]/[`PipelineMessage::PreCommit`] and later.
pub comm_r: Option<Commitment>,
/// CID of the unsealed data of the sector.
///
/// Available at [`SectorState::Sealed`]/[`PipelineMessage::PreCommit`] and later.
pub comm_d: Option<Commitment>,
/// Block at which randomness has been fetched to perform [`PipelineMessage::PreCommit`].
///
/// It is used as a randomness seed to create a replica.
/// Available at [`SectorState::Sealed`] and later.
pub seal_randomness_height: Option<u64>,
/// Block at which the sector was precommitted (extrinsic submitted on-chain).
///
/// It is used as a randomness seed to create a PoRep.
/// Available at [`SectorState::Precommitted`] and later.
pub precommit_block: Option<u64>,
}

Expand Down Expand Up @@ -117,7 +132,8 @@ pub enum SectorState {
Sealed,
/// Sealed sector has been published on-chain, so now the PoRep must be generated for it.
Precommitted,
/// After a PoRep for a sector has been created and publish on-chain.
/// After a PoRep for a sector has been generated.
Proven,
/// Generated PoRep for a sector has been published and verified on-chain.
ProveCommitted,
}
8 changes: 7 additions & 1 deletion lib/polka-storage-proofs/src/porep/sealer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ use crate::{
ZeroPaddingReader,
};

pub type Proof = groth16::Proof<Bls12>;
/// Proof using [`blstrs::Bls12`] as finite field elements.
///
/// It's the output of PoRep and PoSt proof generation and CANNOT be used in no_std.
pub type BlstrsProof = groth16::Proof<Bls12>;
/// Proof using [`bls12_381::Bls12`] as finite field elements.
///
/// It is used in no_std to verify proofs, converted from [`BlstrsProof`].
pub type SubstrateProof = crate::Proof<bls12_381::Bls12>;

/// Prepares an arbitrary piece to be used by [`Sealer::create_sector`].
Expand Down

0 comments on commit 6042593

Please sign in to comment.