Skip to content

Commit

Permalink
fix: add missing election-post checks
Browse files Browse the repository at this point in the history
also update the api to expect no more faulty sectors
  • Loading branch information
dignifiedquire authored Nov 19, 2019
1 parent 6c06b13 commit 379de47
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 44 deletions.
62 changes: 19 additions & 43 deletions filecoin-proofs/src/api/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ use std::path::PathBuf;

pub use storage_proofs::election_post::Candidate;

pub const CHALLENGE_COUNT_DENOMINATOR: f64 = 25.;

/// The minimal information required about a replica, in order to be able to generate
/// a PoSt over it.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
Expand All @@ -38,8 +36,6 @@ pub struct PrivateReplicaInfo {
comm_r: Commitment,
/// Persistent Aux.
aux: PersistentAux,
/// Is this sector marked as a fault?
is_fault: bool,
/// Contains sector-specific (e.g. merkle trees) assets
cache_dir: PathBuf,
}
Expand All @@ -62,22 +58,6 @@ impl PrivateReplicaInfo {
access,
comm_r,
aux,
is_fault: false,
cache_dir,
}
}

pub fn new_faulty(
access: String,
comm_r: Commitment,
aux: PersistentAux,
cache_dir: PathBuf,
) -> Self {
PrivateReplicaInfo {
access,
comm_r,
aux,
is_fault: true,
cache_dir,
}
}
Expand Down Expand Up @@ -113,8 +93,6 @@ impl PrivateReplicaInfo {
pub struct PublicReplicaInfo {
/// The replica commitment.
comm_r: Commitment,
/// Is this sector marked as a fault?
is_fault: bool,
}

impl std::cmp::Ord for PublicReplicaInfo {
Expand All @@ -131,17 +109,7 @@ impl std::cmp::PartialOrd for PublicReplicaInfo {

impl PublicReplicaInfo {
pub fn new(comm_r: Commitment) -> Self {
PublicReplicaInfo {
comm_r,
is_fault: false,
}
}

pub fn new_faulty(comm_r: Commitment) -> Self {
PublicReplicaInfo {
comm_r,
is_fault: true,
}
PublicReplicaInfo { comm_r }
}

pub fn safe_comm_r(&self) -> Result<<DefaultTreeHasher as Hasher>::Domain, failure::Error> {
Expand Down Expand Up @@ -170,17 +138,8 @@ pub fn generate_candidates(
let sector_size = u64::from(PaddedBytesAmount::from(post_config));

let sectors = replicas.keys().copied().collect();
let faults = replicas
.iter()
.filter(|(_id, replica)| replica.is_fault)
.count();

let active_sector_count = sector_count - faults as u64;
let challenged_sectors_count =
(active_sector_count as f64 / CHALLENGE_COUNT_DENOMINATOR).ceil() as usize;

let challenged_sectors =
election_post::generate_sector_challenges(randomness, challenged_sectors_count, &sectors)?;
let challenged_sectors = election_post::generate_sector_challenges(randomness, &sectors)?;

// Match the replicas to the challenges, as these are the only ones required.
let challenged_replicas: Vec<_> = challenged_sectors
Expand Down Expand Up @@ -310,6 +269,7 @@ pub fn verify_post(
"Missmatch between winners and proofs"
);

let sectors = replicas.keys().copied().collect();
let vanilla_params = post_setup_params(post_config);
let setup_params = compound_proof::SetupParams {
vanilla_params,
Expand All @@ -331,6 +291,22 @@ pub fn verify_post(
};
let comm_r = replica.safe_comm_r()?;

if !election_post::is_valid_sector_challenge_index(
sector_count as usize,
winner.sector_challenge_index,
) {
return Ok(false);
}

let expected_sector_id = election_post::generate_sector_challenge(
randomness,
winner.sector_challenge_index as usize,
&sectors,
)?;
if expected_sector_id != winner.sector_id {
return Ok(false);
}

let proof = MultiProof::new_from_reader(None, &proof[..], &verifying_key)?;
let pub_inputs = election_post::PublicInputs {
randomness: *randomness,
Expand Down
9 changes: 8 additions & 1 deletion storage-proofs/src/election_post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::util::NODE_SIZE;

pub const POST_CHALLENGE_COUNT: usize = 8;
pub const POST_CHALLENGED_NODES: usize = 16;
pub const CHALLENGE_COUNT_DENOMINATOR: f64 = 25.;

#[derive(Debug, Clone)]
pub struct SetupParams {
Expand Down Expand Up @@ -217,11 +218,17 @@ pub fn finalize_ticket(partial_ticket: &Fr) -> [u8; 32] {
ticket
}

pub fn is_valid_sector_challenge_index(sector_count: usize, index: u64) -> bool {
let max = (sector_count as f64 / CHALLENGE_COUNT_DENOMINATOR).ceil() as u64;
index < max
}

pub fn generate_sector_challenges(
randomness: &[u8; 32],
challenge_count: usize,
sectors: &OrderedSectorSet,
) -> Result<Vec<SectorId>> {
let challenge_count = (sectors.len() as f64 / CHALLENGE_COUNT_DENOMINATOR).ceil() as usize;

let mut challenges = Vec::with_capacity(challenge_count);

for n in 0..challenge_count as usize {
Expand Down

0 comments on commit 379de47

Please sign in to comment.