Skip to content

Commit

Permalink
test: make sure that WindowPoSt also works on read-only files (#1630)
Browse files Browse the repository at this point in the history
* test: make sure that WindowPoSt also works on read-only files

This commit adapts the test to make the generated files read-only
before a WindowPoSt is performed. This simulates the use case when
you mount your sectors read-only after generation.

* test: also set replica to read-only in WindowPoSt test

* test: also make Winning PoSt files read-only

The Window PoSt tests were already working on read-only files where
possible, do the same for Winning PoSt.

Co-authored-by: Artem Storozhuk <[email protected]>
  • Loading branch information
vmx and storojs72 authored Sep 19, 2022
1 parent 367d47b commit 4ff161d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions filecoin-proofs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ tempfile = "3"
ff = "0.12.0"
fil_logger = "0.1.6"
rand_xorshift = "0.3.0"
walkdir = "2.3.2"

[features]
default = ["opencl"]
Expand Down
32 changes: 32 additions & 0 deletions filecoin-proofs/tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,10 @@ fn winning_post<Tree: 'static + MerkleTreeTrait>(
let challenges =
generate_fallback_sector_challenges::<Tree>(&config, &randomness, &[sector_id], prover_id)?;

// Make sure that files can be read-only for a window post.
set_readonly_flag(replica.path(), true);
set_readonly_flag(cache_dir.path(), true);

let single_proof = generate_single_vanilla_proof::<Tree>(
&config,
sector_id,
Expand All @@ -886,6 +890,10 @@ fn winning_post<Tree: 'static + MerkleTreeTrait>(
verify_winning_post::<Tree>(&config, &randomness, &pub_replicas[..], prover_id, &proof)?;
assert!(valid, "proof did not verify");

// Make files writeable again, so that the temporary directory can be removed.
set_readonly_flag(replica.path(), false);
set_readonly_flag(cache_dir.path(), false);

replica.close()?;

Ok(())
Expand Down Expand Up @@ -1229,6 +1237,18 @@ fn partition_window_post<Tree: 'static + MerkleTreeTrait>(
Ok(())
}

/// Make all files recursively read-only/writeable, starting at the given directory/file.
fn set_readonly_flag(path: &Path, readonly: bool) {
for entry in walkdir::WalkDir::new(path) {
let entry = entry.expect("couldn't get file");
let metadata = entry.metadata().expect("couldn't get metadata");
let mut permissions = metadata.permissions();
permissions.set_readonly(readonly);
std::fs::set_permissions(entry.path(), permissions)
.expect("couldn't apply read-only permissions");
}
}

fn window_post<Tree: 'static + MerkleTreeTrait>(
sector_size: u64,
total_sector_count: usize,
Expand Down Expand Up @@ -1311,6 +1331,12 @@ fn window_post<Tree: 'static + MerkleTreeTrait>(

let mut vanilla_proofs = Vec::with_capacity(replica_sectors.len());

// Make sure that files can be read-only for a window post.
for (_, replica, _, cache_dir, _) in &sectors {
set_readonly_flag(replica.path(), true);
set_readonly_flag(cache_dir.path(), true);
}

for (sector_id, replica) in priv_replicas.iter() {
let sector_challenges = &challenges[sector_id];
let single_proof =
Expand All @@ -1326,6 +1352,12 @@ fn window_post<Tree: 'static + MerkleTreeTrait>(
let valid = verify_window_post::<Tree>(&config, &randomness, &pub_replicas, prover_id, &proof)?;
assert!(valid, "proof did not verify");

// Make files writeable again, so that the temporary directory can be removed.
for (_, replica, _, cache_dir, _) in &sectors {
set_readonly_flag(replica.path(), false);
set_readonly_flag(cache_dir.path(), false);
}

Ok(())
}

Expand Down

0 comments on commit 4ff161d

Please sign in to comment.