Skip to content

Commit

Permalink
fix: improve sector ID logging (#1280)
Browse files Browse the repository at this point in the history
Sector IDs are more often included in log messages/attached to errors.

This PR is based on @IPFS-grandhelmsman work at #1261.

Closes #1261.
  • Loading branch information
vmx committed Sep 14, 2020
1 parent 0450d4d commit 4513979
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 46 deletions.
95 changes: 68 additions & 27 deletions filecoin-proofs/src/api/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ impl<Tree: 'static + MerkleTreeTrait> PrivateReplicaInfo<Tree> {
as_safe_commitment(&self.comm_r, "comm_r")
}

pub fn safe_comm_c(&self) -> Result<<Tree::Hasher as Hasher>::Domain> {
Ok(self.aux.comm_c)
pub fn safe_comm_c(&self) -> <Tree::Hasher as Hasher>::Domain {
self.aux.comm_c
}

pub fn safe_comm_r_last(&self) -> Result<<Tree::Hasher as Hasher>::Domain> {
Ok(self.aux.comm_r_last)
pub fn safe_comm_r_last(&self) -> <Tree::Hasher as Hasher>::Domain {
self.aux.comm_r_last
}

/// Generate the merkle tree of this particular replica.
Expand Down Expand Up @@ -355,20 +355,28 @@ pub fn generate_winning_post<Tree: 'static + MerkleTreeTrait>(

let trees = replicas
.iter()
.map(|(_, replica)| replica.merkle_tree(post_config.sector_size))
.map(|(sector_id, replica)| {
replica
.merkle_tree(post_config.sector_size)
.with_context(|| {
format!("generate_winning_post: merkle_tree failed: {:?}", sector_id)
})
})
.collect::<Result<Vec<_>>>()?;

let mut pub_sectors = Vec::with_capacity(param_sector_count);
let mut priv_sectors = Vec::with_capacity(param_sector_count);

for _ in 0..param_sector_count {
for ((id, replica), tree) in replicas.iter().zip(trees.iter()) {
let comm_r = replica.safe_comm_r()?;
let comm_c = replica.safe_comm_c()?;
let comm_r_last = replica.safe_comm_r_last()?;
for ((sector_id, replica), tree) in replicas.iter().zip(trees.iter()) {
let comm_r = replica.safe_comm_r().with_context(|| {
format!("generate_winning_post: safe_comm_r failed: {:?}", sector_id)
})?;
let comm_c = replica.safe_comm_c();
let comm_r_last = replica.safe_comm_r_last();

pub_sectors.push(fallback::PublicSector::<<Tree::Hasher as Hasher>::Domain> {
id: *id,
id: *sector_id,
comm_r,
});
priv_sectors.push(fallback::PrivateSector {
Expand Down Expand Up @@ -493,7 +501,7 @@ pub fn generate_fallback_sector_challenges<Tree: 'static + MerkleTreeTrait>(
randomness_safe,
u64::from(*sector),
challenge_index,
)?;
);
challenges.push(challenged_leaf);
}

Expand All @@ -514,12 +522,24 @@ pub fn generate_single_vanilla_proof<Tree: 'static + MerkleTreeTrait>(
replica: &PrivateReplicaInfo<Tree>,
challenges: &[u64],
) -> Result<FallbackPoStSectorProof<Tree>> {
info!("generate_single_vanilla_proof:start");

let tree = &replica.merkle_tree(post_config.sector_size)?;
let comm_r = replica.safe_comm_r()?;
let comm_c = replica.safe_comm_c()?;
let comm_r_last = replica.safe_comm_r_last()?;
info!("generate_single_vanilla_proof:start: {:?}", sector_id);

let tree = &replica
.merkle_tree(post_config.sector_size)
.with_context(|| {
format!(
"generate_single_vanilla_proof: merkle_tree failed: {:?}",
sector_id
)
})?;
let comm_r = replica.safe_comm_r().with_context(|| {
format!(
"generate_single_vanilla_poof: safe_comm_r failed: {:?}",
sector_id
)
})?;
let comm_c = replica.safe_comm_c();
let comm_r_last = replica.safe_comm_r_last();

// There is only enough information in the arguments to generate a
// single vanilla proof, so the behaviour is unexpected if the
Expand All @@ -542,9 +562,15 @@ pub fn generate_single_vanilla_proof<Tree: 'static + MerkleTreeTrait>(
sectors: &priv_sectors,
};

let vanilla_proof = fallback::vanilla_proof(sector_id, &priv_inputs, challenges)?;
let vanilla_proof =
fallback::vanilla_proof(sector_id, &priv_inputs, challenges).with_context(|| {
format!(
"generate_single_vanilla_proof: vanilla_proof failed: {:?}",
sector_id
)
})?;

info!("generate_single_vanilla_proof:finish");
info!("generate_single_vanilla_proof:finish: {:?}", sector_id);

Ok(FallbackPoStSectorProof {
sector_id,
Expand Down Expand Up @@ -594,9 +620,14 @@ pub fn verify_winning_post<Tree: 'static + MerkleTreeTrait>(

let mut pub_sectors = Vec::with_capacity(param_sector_count);
for _ in 0..param_sector_count {
for (id, replica) in replicas.iter() {
let comm_r = replica.safe_comm_r()?;
pub_sectors.push(fallback::PublicSector { id: *id, comm_r });
for (sector_id, replica) in replicas.iter() {
let comm_r = replica.safe_comm_r().with_context(|| {
format!("verify_winning_post: safe_comm_r failed: {:?}", sector_id)
})?;
pub_sectors.push(fallback::PublicSector {
id: *sector_id,
comm_r,
});
}
}

Expand Down Expand Up @@ -881,16 +912,24 @@ pub fn generate_window_post<Tree: 'static + MerkleTreeTrait>(

let trees: Vec<_> = replicas
.iter()
.map(|(_id, replica)| replica.merkle_tree(post_config.sector_size))
.map(|(sector_id, replica)| {
replica
.merkle_tree(post_config.sector_size)
.with_context(|| {
format!("generate_window_post: merkle_tree failed: {:?}", sector_id)
})
})
.collect::<Result<_>>()?;

let mut pub_sectors = Vec::with_capacity(sector_count);
let mut priv_sectors = Vec::with_capacity(sector_count);

for ((sector_id, replica), tree) in replicas.iter().zip(trees.iter()) {
let comm_r = replica.safe_comm_r()?;
let comm_c = replica.safe_comm_c()?;
let comm_r_last = replica.safe_comm_r_last()?;
let comm_r = replica.safe_comm_r().with_context(|| {
format!("generate_window_post: safe_comm_r failed: {:?}", sector_id)
})?;
let comm_c = replica.safe_comm_c();
let comm_r_last = replica.safe_comm_r_last();

pub_sectors.push(fallback::PublicSector {
id: *sector_id,
Expand Down Expand Up @@ -958,7 +997,9 @@ pub fn verify_window_post<Tree: 'static + MerkleTreeTrait>(
let pub_sectors: Vec<_> = replicas
.iter()
.map(|(sector_id, replica)| {
let comm_r = replica.safe_comm_r()?;
let comm_r = replica.safe_comm_r().with_context(|| {
format!("verify_window_post: safe_comm_r failed: {:?}", sector_id)
})?;
Ok(fallback::PublicSector {
id: *sector_id,
comm_r,
Expand Down
16 changes: 8 additions & 8 deletions filecoin-proofs/src/api/seal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ where
S: AsRef<Path>,
T: AsRef<Path>,
{
info!("seal_pre_commit_phase1:start");
info!("seal_pre_commit_phase1:start: {:?}", sector_id);

// Sanity check all input path types.
ensure!(
Expand Down Expand Up @@ -185,7 +185,7 @@ where
comm_d,
};

info!("seal_pre_commit_phase1:finish");
info!("seal_pre_commit_phase1:finish: {:?}", sector_id);
Ok(out)
}

Expand Down Expand Up @@ -324,7 +324,7 @@ pub fn seal_commit_phase1<T: AsRef<Path>, Tree: 'static + MerkleTreeTrait>(
pre_commit: SealPreCommitOutput,
piece_infos: &[PieceInfo],
) -> Result<SealCommitPhase1Output<Tree>> {
info!("seal_commit_phase1:start");
info!("seal_commit_phase1:start: {:?}", sector_id);

// Sanity check all input path types.
ensure!(
Expand Down Expand Up @@ -435,7 +435,7 @@ pub fn seal_commit_phase1<T: AsRef<Path>, Tree: 'static + MerkleTreeTrait>(
ticket,
};

info!("seal_commit_phase1:finish");
info!("seal_commit_phase1:finish: {:?}", sector_id);
Ok(out)
}

Expand All @@ -446,7 +446,7 @@ pub fn seal_commit_phase2<Tree: 'static + MerkleTreeTrait>(
prover_id: ProverId,
sector_id: SectorId,
) -> Result<SealCommitOutput> {
info!("seal_commit_phase2:start");
info!("seal_commit_phase2:start: {:?}", sector_id);

let SealCommitPhase1Output {
vanilla_proofs,
Expand Down Expand Up @@ -529,7 +529,7 @@ pub fn seal_commit_phase2<Tree: 'static + MerkleTreeTrait>(

let out = SealCommitOutput { proof: buf };

info!("seal_commit_phase2:finish");
info!("seal_commit_phase2:finish: {:?}", sector_id);
Ok(out)
}

Expand Down Expand Up @@ -571,7 +571,7 @@ pub fn verify_seal<Tree: 'static + MerkleTreeTrait>(
seed: Ticket,
proof_vec: &[u8],
) -> Result<bool> {
info!("verify_seal:start");
info!("verify_seal:start: {:?}", sector_id);
ensure!(comm_d_in != [0; 32], "Invalid all zero commitment (comm_d)");
ensure!(comm_r_in != [0; 32], "Invalid all zero commitment (comm_r)");

Expand Down Expand Up @@ -661,7 +661,7 @@ pub fn verify_seal<Tree: 'static + MerkleTreeTrait>(
)
};

info!("verify_seal:finish");
info!("verify_seal:finish: {:?}", sector_id);
result
}

Expand Down
2 changes: 1 addition & 1 deletion storage-proofs/post/src/fallback/compound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl<'a, Tree: 'static + MerkleTreeTrait>
pub_inputs.randomness,
sector.id.into(),
challenge_index,
)?;
);

let por_pub_inputs = por::PublicInputs {
commitment: None,
Expand Down
23 changes: 13 additions & 10 deletions storage-proofs/post/src/fallback/vanilla.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::marker::PhantomData;
use anyhow::ensure;
use byteorder::{ByteOrder, LittleEndian};
use generic_array::typenum::Unsigned;
use log::trace;
use log::{error, trace};
use paired::bls12_381::Fr;
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -202,7 +202,7 @@ pub fn generate_leaf_challenges<T: Domain>(
randomness: T,
sector_id: u64,
challenge_count: usize,
) -> Result<Vec<u64>> {
) -> Vec<u64> {
let mut challenges = Vec::with_capacity(challenge_count);

for leaf_challenge_index in 0..challenge_count {
Expand All @@ -211,11 +211,11 @@ pub fn generate_leaf_challenges<T: Domain>(
randomness,
sector_id,
leaf_challenge_index as u64,
)?;
);
challenges.push(challenge)
}

Ok(challenges)
challenges
}

/// Generates challenge, such that the range fits into the sector.
Expand All @@ -224,7 +224,7 @@ pub fn generate_leaf_challenge<T: Domain>(
randomness: T,
sector_id: u64,
leaf_challenge_index: u64,
) -> Result<u64> {
) -> u64 {
let mut hasher = Sha256::new();
hasher.update(AsRef::<[u8]>::as_ref(&randomness));
hasher.update(&sector_id.to_le_bytes()[..]);
Expand All @@ -233,9 +233,7 @@ pub fn generate_leaf_challenge<T: Domain>(

let leaf_challenge = LittleEndian::read_u64(&hash[..8]);

let challenged_range_index = leaf_challenge % (pub_params.sector_size / NODE_SIZE as u64);

Ok(challenged_range_index)
leaf_challenge % (pub_params.sector_size / NODE_SIZE as u64)
}

enum ProofOrFault<T> {
Expand Down Expand Up @@ -397,7 +395,7 @@ impl<'a, Tree: 'a + MerkleTreeTrait> ProofScheme<'a> for FallbackPoSt<'a, Tree>
pub_inputs.randomness,
sector_id.into(),
challenge_index,
)?;
);

let proof = tree.gen_cached_proof(
challenged_leaf_start as usize,
Expand All @@ -423,6 +421,7 @@ impl<'a, Tree: 'a + MerkleTreeTrait> ProofScheme<'a> for FallbackPoSt<'a, Tree>
inclusion_proofs.push(proof);
}
ProofOrFault::Fault(sector_id) => {
error!("faulty sector: {:?}", sector_id);
faulty_sectors.insert(sector_id);
}
}
Expand Down Expand Up @@ -507,6 +506,7 @@ impl<'a, Tree: 'a + MerkleTreeTrait> ProofScheme<'a> for FallbackPoSt<'a, Tree>
&comm_r_last,
)) != AsRef::<[u8]>::as_ref(comm_r)
{
error!("comm_c != comm_r_last: {:?}", sector_id);
return Ok(false);
}

Expand All @@ -525,10 +525,11 @@ impl<'a, Tree: 'a + MerkleTreeTrait> ProofScheme<'a> for FallbackPoSt<'a, Tree>
pub_inputs.randomness,
sector_id.into(),
challenge_index,
)?;
);

// validate all comm_r_lasts match
if inclusion_proof.root() != comm_r_last {
error!("inclusion proof root != comm_r_last: {:?}", sector_id);
return Ok(false);
}

Expand All @@ -537,10 +538,12 @@ impl<'a, Tree: 'a + MerkleTreeTrait> ProofScheme<'a> for FallbackPoSt<'a, Tree>
inclusion_proof.expected_len(pub_params.sector_size as usize / NODE_SIZE);

if expected_path_length != inclusion_proof.path().len() {
error!("wrong path length: {:?}", sector_id);
return Ok(false);
}

if !inclusion_proof.validate(challenged_leaf_start as usize) {
error!("invalid inclusion proof: {:?}", sector_id);
return Ok(false);
}
}
Expand Down

0 comments on commit 4513979

Please sign in to comment.