Skip to content

Commit 379de47

Browse files
fix: add missing election-post checks
also update the api to expect no more faulty sectors
1 parent 6c06b13 commit 379de47

File tree

2 files changed

+27
-44
lines changed

2 files changed

+27
-44
lines changed

filecoin-proofs/src/api/post.rs

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ use std::path::PathBuf;
2626

2727
pub use storage_proofs::election_post::Candidate;
2828

29-
pub const CHALLENGE_COUNT_DENOMINATOR: f64 = 25.;
30-
3129
/// The minimal information required about a replica, in order to be able to generate
3230
/// a PoSt over it.
3331
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
@@ -38,8 +36,6 @@ pub struct PrivateReplicaInfo {
3836
comm_r: Commitment,
3937
/// Persistent Aux.
4038
aux: PersistentAux,
41-
/// Is this sector marked as a fault?
42-
is_fault: bool,
4339
/// Contains sector-specific (e.g. merkle trees) assets
4440
cache_dir: PathBuf,
4541
}
@@ -62,22 +58,6 @@ impl PrivateReplicaInfo {
6258
access,
6359
comm_r,
6460
aux,
65-
is_fault: false,
66-
cache_dir,
67-
}
68-
}
69-
70-
pub fn new_faulty(
71-
access: String,
72-
comm_r: Commitment,
73-
aux: PersistentAux,
74-
cache_dir: PathBuf,
75-
) -> Self {
76-
PrivateReplicaInfo {
77-
access,
78-
comm_r,
79-
aux,
80-
is_fault: true,
8161
cache_dir,
8262
}
8363
}
@@ -113,8 +93,6 @@ impl PrivateReplicaInfo {
11393
pub struct PublicReplicaInfo {
11494
/// The replica commitment.
11595
comm_r: Commitment,
116-
/// Is this sector marked as a fault?
117-
is_fault: bool,
11896
}
11997

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

132110
impl PublicReplicaInfo {
133111
pub fn new(comm_r: Commitment) -> Self {
134-
PublicReplicaInfo {
135-
comm_r,
136-
is_fault: false,
137-
}
138-
}
139-
140-
pub fn new_faulty(comm_r: Commitment) -> Self {
141-
PublicReplicaInfo {
142-
comm_r,
143-
is_fault: true,
144-
}
112+
PublicReplicaInfo { comm_r }
145113
}
146114

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

172140
let sectors = replicas.keys().copied().collect();
173-
let faults = replicas
174-
.iter()
175-
.filter(|(_id, replica)| replica.is_fault)
176-
.count();
177-
178-
let active_sector_count = sector_count - faults as u64;
179-
let challenged_sectors_count =
180-
(active_sector_count as f64 / CHALLENGE_COUNT_DENOMINATOR).ceil() as usize;
181141

182-
let challenged_sectors =
183-
election_post::generate_sector_challenges(randomness, challenged_sectors_count, &sectors)?;
142+
let challenged_sectors = election_post::generate_sector_challenges(randomness, &sectors)?;
184143

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

272+
let sectors = replicas.keys().copied().collect();
313273
let vanilla_params = post_setup_params(post_config);
314274
let setup_params = compound_proof::SetupParams {
315275
vanilla_params,
@@ -331,6 +291,22 @@ pub fn verify_post(
331291
};
332292
let comm_r = replica.safe_comm_r()?;
333293

294+
if !election_post::is_valid_sector_challenge_index(
295+
sector_count as usize,
296+
winner.sector_challenge_index,
297+
) {
298+
return Ok(false);
299+
}
300+
301+
let expected_sector_id = election_post::generate_sector_challenge(
302+
randomness,
303+
winner.sector_challenge_index as usize,
304+
&sectors,
305+
)?;
306+
if expected_sector_id != winner.sector_id {
307+
return Ok(false);
308+
}
309+
334310
let proof = MultiProof::new_from_reader(None, &proof[..], &verifying_key)?;
335311
let pub_inputs = election_post::PublicInputs {
336312
randomness: *randomness,

storage-proofs/src/election_post.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::util::NODE_SIZE;
2222

2323
pub const POST_CHALLENGE_COUNT: usize = 8;
2424
pub const POST_CHALLENGED_NODES: usize = 16;
25+
pub const CHALLENGE_COUNT_DENOMINATOR: f64 = 25.;
2526

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

221+
pub fn is_valid_sector_challenge_index(sector_count: usize, index: u64) -> bool {
222+
let max = (sector_count as f64 / CHALLENGE_COUNT_DENOMINATOR).ceil() as u64;
223+
index < max
224+
}
225+
220226
pub fn generate_sector_challenges(
221227
randomness: &[u8; 32],
222-
challenge_count: usize,
223228
sectors: &OrderedSectorSet,
224229
) -> Result<Vec<SectorId>> {
230+
let challenge_count = (sectors.len() as f64 / CHALLENGE_COUNT_DENOMINATOR).ceil() as usize;
231+
225232
let mut challenges = Vec::with_capacity(challenge_count);
226233

227234
for n in 0..challenge_count as usize {

0 commit comments

Comments
 (0)