Skip to content

Commit

Permalink
feat(commitment): change abstraction (#566)
Browse files Browse the repository at this point in the history
  • Loading branch information
cernicc authored Nov 19, 2024
1 parent f91ecb7 commit 26c7143
Show file tree
Hide file tree
Showing 16 changed files with 274 additions and 244 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ rand_chacha = { version = "0.3.1", default-features = false }
rand_xorshift = "0.3"
rocksdb = { version = "0.21" }
scale-info = { version = "2.11.1", default-features = false }
sealed = "0.6.0"
serde = { version = "1.0.197", default-features = false }
serde-big-array = { version = "0.3.2" }
serde_derive = { version = "1.0.117" }
Expand Down
17 changes: 7 additions & 10 deletions cli/polka-storage-provider/client/src/commands/proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use polka_storage_proofs::{
use polka_storage_provider_common::commp::{calculate_piece_commitment, CommPError};
use primitives_commitment::{
piece::{PaddedPieceSize, PieceInfo},
Commitment, CommitmentKind,
Commitment,
};
use primitives_proofs::{derive_prover_id, RegisteredPoStProof, RegisteredSealProof};
use storagext::multipair::{MultiPairArgs, MultiPairSigner};
Expand Down Expand Up @@ -247,14 +247,11 @@ impl ProofsCommand {
let piece_file_length = PaddedPieceSize::from_arbitrary_size(piece_file_length);
let piece_file = ZeroPaddingReader::new(piece_file, *piece_file_length.unpadded());

let commp = cid::Cid::from_str(commp.as_str())
let commp = cid::Cid::from_str(&commp)
.map_err(|e| UtilsCommandError::InvalidPieceCommP(commp, e))?;
let piece_info = PieceInfo {
commitment: primitives_commitment::Commitment::from_cid(
&commp,
CommitmentKind::Piece,
)
.map_err(|e| UtilsCommandError::InvalidPieceType(commp.to_string(), e))?,
commitment: Commitment::try_from(commp)
.map_err(|e| UtilsCommandError::InvalidPieceType(commp.to_string(), e))?,
size: piece_file_length,
};

Expand Down Expand Up @@ -299,13 +296,13 @@ impl ProofsCommand {
sector_id,
ticket,
Some(seed),
precommit.clone(),
precommit,
&piece_infos,
)
.map_err(|e| UtilsCommandError::GeneratePoRepError(e))?;

println!("CommD: {:?}", Commitment::data(precommit.comm_d).cid());
println!("CommR: {:?}", Commitment::replica(precommit.comm_r).cid());
println!("CommD: {}", precommit.comm_d.cid());
println!("CommR: {}", precommit.comm_r.cid());
println!("Proof: {:?}", proofs);
// We use sector size 2KiB only at this point, which guarantees to have 1 proof, because it has 1 partition in the config.
// That's why `prove_commit` will always generate a 1 proof.
Expand Down
13 changes: 5 additions & 8 deletions cli/polka-storage-provider/common/src/commp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use filecoin_hashers::{
Domain,
};
use fr32::Fr32Reader;
use primitives_commitment::{piece::PaddedPieceSize, Commitment, CommitmentKind, NODE_SIZE};
use primitives_commitment::{piece::PaddedPieceSize, CommP, Commitment, NODE_SIZE};
use storage_proofs_core::merkle::BinaryMerkleTree;
use thiserror::Error;

Expand All @@ -15,7 +15,7 @@ use thiserror::Error;
pub fn calculate_piece_commitment<R: Read>(
source: R,
piece_size: PaddedPieceSize,
) -> Result<Commitment, CommPError> {
) -> Result<Commitment<CommP>, CommPError> {
// This reader adds two zero bits to each 254 bits of data read from the source.
let mut fr32_reader = Fr32Reader::new(source);

Expand All @@ -35,14 +35,12 @@ pub fn calculate_piece_commitment<R: Read>(
.map_err(|err| CommPError::TreeBuild(err.to_string()))?;

// Read and return the root of the tree
let mut commitment = [0; NODE_SIZE];
let mut raw = [0; NODE_SIZE];
tree.root()
.write_bytes(&mut commitment)
.write_bytes(&mut raw)
.expect("destination buffer large enough");

let commitment = Commitment::new(commitment, CommitmentKind::Piece);

Ok(commitment)
Ok(raw.into())
}

#[derive(Debug, Error)]
Expand Down Expand Up @@ -93,7 +91,6 @@ mod tests {

let commitment =
calculate_piece_commitment(zero_padding_reader, padded_piece_size).unwrap();
dbg!(commitment.raw());

assert_eq!(
commitment.raw(),
Expand Down
41 changes: 19 additions & 22 deletions cli/polka-storage-provider/server/src/pipeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use polka_storage_proofs::porep::{
PoRepError, PoRepParameters,
};
use polka_storage_provider_common::rpc::ServerInfo;
use primitives_commitment::Commitment;
use primitives_commitment::{CommD, CommP, CommR, Commitment};
use primitives_proofs::{
derive_prover_id,
randomness::{draw_randomness, DomainSeparationTag},
Expand Down Expand Up @@ -115,15 +115,15 @@ impl PipelineOperations for TaskTracker {
deal,
published_deal_id,
piece_path,
piece_cid,
commitment,
} = msg;
self.spawn(async move {
tokio::select! {
// AddPiece is cancellation safe, as it can be retried and the state will be fine.
res = add_piece(state, piece_path, piece_cid, deal, published_deal_id) => {
res = add_piece(state, piece_path, commitment, deal, published_deal_id) => {
match res {
Ok(_) => tracing::info!("Add Piece for piece {:?}, deal id {}, finished successfully.", piece_cid, published_deal_id),
Err(err) => tracing::error!(%err, "Add Piece for piece {:?}, deal id {}, failed!", piece_cid, published_deal_id),
Ok(_) => tracing::info!("Add Piece for piece {}, deal id {}, finished successfully.", commitment, published_deal_id),
Err(err) => tracing::error!(%err, "Add Piece for piece {}, deal id {}, failed!", commitment, published_deal_id),
}
},
() = token.cancelled() => {
Expand Down Expand Up @@ -206,7 +206,7 @@ async fn find_sector_for_piece(
async fn add_piece(
state: Arc<PipelineState>,
piece_path: PathBuf,
piece_cid: Commitment,
commitment: Commitment<CommP>,
deal: DealProposal,
deal_id: u64,
) -> Result<(), PipelineError> {
Expand All @@ -223,7 +223,7 @@ async fn add_piece(
.open(&sector.unsealed_path)?;

tracing::info!("Preparing piece...");
let (padded_reader, piece_info) = prepare_piece(piece_path, piece_cid)?;
let (padded_reader, piece_info) = prepare_piece(piece_path, commitment)?;
tracing::info!("Adding piece...");
let occupied_piece_space = sealer.add_piece(
padded_reader,
Expand Down Expand Up @@ -322,6 +322,9 @@ async fn precommit(
let sealing_output = sealing_handle.await??;
tracing::info!("Created sector's replica: {:?}", sealing_output);

let sealing_output_commr = Commitment::<CommR>::from(sealing_output.comm_r);
let sealing_output_commd = Commitment::<CommD>::from(sealing_output.comm_d);

tracing::debug!("Precommiting at block: {}", current_block);
let result = state
.xt_client
Expand All @@ -338,16 +341,8 @@ async fn precommit(
+ SECTOR_EXPIRATION_MARGIN,
sector_number: sector_number,
seal_proof: state.server_info.seal_proof,
sealed_cid: primitives_commitment::Commitment::new(
sealing_output.comm_r,
primitives_commitment::CommitmentKind::Replica,
)
.cid(),
unsealed_cid: primitives_commitment::Commitment::new(
sealing_output.comm_d,
primitives_commitment::CommitmentKind::Data,
)
.cid(),
sealed_cid: sealing_output_commr.cid(),
unsealed_cid: sealing_output_commd.cid(),
seal_randomness_height: current_block,
}],
true,
Expand All @@ -367,8 +362,8 @@ async fn precommit(
let sector = PreCommittedSector::create(
sector,
sealed_path,
Commitment::replica(sealing_output.comm_r),
Commitment::data(sealing_output.comm_d),
sealing_output_commr,
sealing_output_commd,
current_block,
precommited_sectors[0].block,
)
Expand Down Expand Up @@ -452,8 +447,7 @@ async fn prove_commit(
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.raw();
let comm_d = sector.comm_d.raw();

tokio::task::spawn_blocking(move || {
sealer.prove_sector(
porep_params.as_ref(),
Expand All @@ -463,7 +457,10 @@ async fn prove_commit(
sector_number,
ticket,
Some(seed),
PreCommitOutput { comm_r, comm_d },
PreCommitOutput {
comm_r: sector.comm_r,
comm_d: sector.comm_d,
},
&piece_infos,
)
})
Expand Down
24 changes: 12 additions & 12 deletions cli/polka-storage-provider/server/src/pipeline/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::PathBuf;

use primitives_commitment::{piece::PieceInfo, Commitment};
use primitives_commitment::{piece::PieceInfo, CommD, CommP, CommR, Commitment};
use primitives_proofs::{DealId, SectorNumber};
use serde::{Deserialize, Serialize};
use storagext::types::market::DealProposal;
Expand All @@ -26,7 +26,7 @@ pub struct AddPieceMessage {
/// Path where the deal data (.car archive) is stored
pub piece_path: PathBuf,
/// CommP of the .car archive stored at `piece_path`
pub piece_cid: Commitment,
pub commitment: Commitment<CommP>,
}

/// Sector to be sealed and pre-commited to the chain
Expand Down Expand Up @@ -95,11 +95,11 @@ pub struct PreCommittedSector {
/// 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.
pub comm_r: Commitment,
/// Sealed sector commitment.
pub comm_r: Commitment<CommR>,

/// CID of the unsealed data of the sector.
pub comm_d: Commitment,
/// Data commitment of the sector.
pub comm_d: Commitment<CommD>,

/// Block at which randomness has been fetched to perform [`PipelineMessage::PreCommit`].
///
Expand Down Expand Up @@ -143,8 +143,8 @@ impl PreCommittedSector {
pub async fn create(
unsealed: UnsealedSector,
sealed_path: std::path::PathBuf,
comm_r: Commitment,
comm_d: Commitment,
comm_r: Commitment<CommR>,
comm_d: Commitment<CommD>,
seal_randomness_height: u64,
precommit_block: u64,
) -> Result<Self, std::io::Error> {
Expand Down Expand Up @@ -181,11 +181,11 @@ pub struct ProvenSector {
/// Path of an existing file where the sealed sector data is stored.
pub sealed_path: std::path::PathBuf,

/// CID of the sealed sector.
pub comm_r: Commitment,
/// Sealed sector commitment.
pub comm_r: Commitment<CommR>,

/// CID of the unsealed data of the sector.
pub comm_d: Commitment,
/// Data commitment of the sector.
pub comm_d: Commitment<CommD>,
}

impl ProvenSector {
Expand Down
17 changes: 7 additions & 10 deletions cli/polka-storage-provider/server/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,19 @@ impl StorageProviderRpcServer for RpcServerState {
// We always publish only 1 deal
let deal_id = published_deals[0].deal_id;

let piece_cid = Commitment::new(
piece_cid.hash().digest().try_into().map_err(|e| {
RpcError::invalid_params(
e,
Some(serde_json::to_value(piece_cid).expect("cid to be serializable")),
)
})?,
primitives_commitment::CommitmentKind::Piece,
);
let commitment = Commitment::from_cid(&piece_cid).map_err(|e| {
RpcError::invalid_params(
e,
Some(serde_json::to_value(piece_cid).expect("cid to be serializable")),
)
})?;

self.pipeline_sender
.send(PipelineMessage::AddPiece(AddPieceMessage {
deal: deal_proposal,
published_deal_id: deal_id,
piece_path,
piece_cid,
commitment,
}))
.map_err(|e| RpcError::internal_error(e, None))?;

Expand Down
Loading

0 comments on commit 26c7143

Please sign in to comment.