Skip to content

Commit

Permalink
refactor: use functions instead of lazy constants
Browse files Browse the repository at this point in the history
Instead of using one-time initialized HashMaps, use functions, that
directly return the expected values. That reduces the amount of
boilerplate code when those values are used.

Fixes #1631.
  • Loading branch information
vmx committed Oct 19, 2023
1 parent a09defe commit 26a1965
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 133 deletions.
10 changes: 3 additions & 7 deletions fil-proofs-param/src/bin/paramcache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::str::FromStr;
use dialoguer::{theme::ColorfulTheme, MultiSelect};
use filecoin_proofs::{
constants::{
DefaultPieceHasher, PUBLISHED_SECTOR_SIZES, WINDOW_POST_CHALLENGE_COUNT,
WINDOW_POST_SECTOR_COUNT, WINNING_POST_CHALLENGE_COUNT, WINNING_POST_SECTOR_COUNT,
self, DefaultPieceHasher, PUBLISHED_SECTOR_SIZES, WINDOW_POST_CHALLENGE_COUNT,
WINNING_POST_CHALLENGE_COUNT, WINNING_POST_SECTOR_COUNT,
},
parameters::{public_params, window_post_public_params, winning_post_public_params},
types::{PoRepConfig, PoStConfig, SectorSize},
Expand Down Expand Up @@ -192,11 +192,7 @@ fn generate_params_post(sector_size: u64, api_version: ApiVersion) {
&PoStConfig {
sector_size: SectorSize(sector_size),
challenge_count: WINDOW_POST_CHALLENGE_COUNT,
sector_count: *WINDOW_POST_SECTOR_COUNT
.read()
.expect("WINDOW_POST_SECTOR_COUNT poisoned")
.get(&sector_size)
.expect("unknown sector size"),
sector_count: constants::get_window_post_sector_count(sector_size),
typ: PoStType::Window,
priority: true,
api_version,
Expand Down
8 changes: 2 additions & 6 deletions fil-proofs-tooling/src/bin/benchy/window_post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use bincode::{deserialize, serialize};
use fil_proofs_tooling::measure::FuncMeasurement;
use fil_proofs_tooling::shared::{PROVER_ID, RANDOMNESS, TICKET_BYTES};
use fil_proofs_tooling::{measure, Metadata};
use filecoin_proofs::constants::{WINDOW_POST_CHALLENGE_COUNT, WINDOW_POST_SECTOR_COUNT};
use filecoin_proofs::constants::{self, WINDOW_POST_CHALLENGE_COUNT};
use filecoin_proofs::types::{
PaddedBytesAmount, PieceInfo, PoRepConfig, PoStConfig, SealCommitPhase1Output,
SealPreCommitOutput, SealPreCommitPhase1Output, SectorSize, UnpaddedBytesAmount,
Expand Down Expand Up @@ -524,11 +524,7 @@ pub fn run_window_post_bench<Tree: 'static + MerkleTreeTrait>(
let post_config = PoStConfig {
sector_size: SectorSize(sector_size),
challenge_count: WINDOW_POST_CHALLENGE_COUNT,
sector_count: *WINDOW_POST_SECTOR_COUNT
.read()
.expect("WINDOW_POST_SECTOR_COUNT poisoned")
.get(&sector_size)
.expect("unknown sector size"),
sector_count: constants::get_window_post_sector_count(sector_size),
typ: PoStType::Window,
priority: true,
api_version,
Expand Down
8 changes: 2 additions & 6 deletions fil-proofs-tooling/src/bin/benchy/window_post_fake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::io::stdout;

use fil_proofs_tooling::shared::{create_replica, PROVER_ID, RANDOMNESS};
use fil_proofs_tooling::{measure, Metadata};
use filecoin_proofs::constants::{WINDOW_POST_CHALLENGE_COUNT, WINDOW_POST_SECTOR_COUNT};
use filecoin_proofs::constants::{self, WINDOW_POST_CHALLENGE_COUNT};
use filecoin_proofs::types::{PoStConfig, SectorSize};
use filecoin_proofs::{
generate_window_post, verify_window_post, with_shape, PoStType, PrivateReplicaInfo,
Expand Down Expand Up @@ -56,11 +56,7 @@ pub fn run_window_post_bench<Tree: 'static + MerkleTreeTrait>(
api_features: Vec<ApiFeature>,
) -> anyhow::Result<()> {
let arbitrary_porep_id = [66; 32];
let sector_count = *WINDOW_POST_SECTOR_COUNT
.read()
.expect("WINDOW_POST_SECTOR_COUNT poisoned")
.get(&sector_size)
.expect("unknown sector size");
let sector_count = constants::get_window_post_sector_count(sector_size);

let (sector_id, replica_output) = create_replica::<Tree>(
sector_size,
Expand Down
19 changes: 5 additions & 14 deletions fil-proofs-tooling/src/bin/circuitinfo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use bellperson::{util_cs::bench_cs::BenchCS, Circuit};
use blstrs::Scalar as Fr;
use dialoguer::{theme::ColorfulTheme, MultiSelect};
use filecoin_proofs::{
constants,
parameters::{public_params, window_post_public_params, winning_post_public_params},
with_shape, DefaultPieceHasher, PoRepConfig, PoRepProofPartitions, PoStConfig, PoStType,
SectorSize, POREP_PARTITIONS, PUBLISHED_SECTOR_SIZES, WINDOW_POST_CHALLENGE_COUNT,
WINDOW_POST_SECTOR_COUNT, WINNING_POST_CHALLENGE_COUNT, WINNING_POST_SECTOR_COUNT,
SectorSize, PUBLISHED_SECTOR_SIZES, WINDOW_POST_CHALLENGE_COUNT, WINNING_POST_CHALLENGE_COUNT,
WINNING_POST_SECTOR_COUNT,
};
use humansize::{file_size_opts, FileSize};
use log::{info, warn};
Expand Down Expand Up @@ -114,11 +115,7 @@ fn window_post_info(sector_size: u64, api_version: ApiVersion) -> CircuitInfo {
&PoStConfig {
sector_size: SectorSize(sector_size),
challenge_count: WINDOW_POST_CHALLENGE_COUNT,
sector_count: *WINDOW_POST_SECTOR_COUNT
.read()
.expect("WINDOW_POST_SECTOR_COUNT poisoned")
.get(&sector_size)
.expect("unknown sector size"),
sector_count: constants::get_window_post_sector_count(sector_size),
typ: PoStType::Window,
priority: true,
api_version,
Expand All @@ -127,13 +124,7 @@ fn window_post_info(sector_size: u64, api_version: ApiVersion) -> CircuitInfo {
}

fn porep_info(sector_size: u64, api_version: ApiVersion) -> (CircuitInfo, usize) {
let partitions = PoRepProofPartitions(
*POREP_PARTITIONS
.read()
.expect("POREP_PARTITIONS poisoned")
.get(&sector_size)
.expect("unknown sector size"),
);
let partitions = PoRepProofPartitions(constants::get_porep_interactive_partitions(sector_size));
let info = with_shape!(
sector_size,
get_porep_info,
Expand Down
13 changes: 7 additions & 6 deletions filecoin-proofs/src/api/seal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@ use storage_proofs_porep::stacked::{
use storage_proofs_update::vanilla::prepare_tree_r_data;
use typenum::{Unsigned, U11, U2};

use crate::POREP_MINIMUM_CHALLENGES;
use crate::{
api::{as_safe_commitment, commitment_from_fr, get_base_tree_leafs, get_base_tree_size},
caches::{
get_stacked_params, get_stacked_srs_key, get_stacked_srs_verifier_key,
get_stacked_verifying_key,
},
constants::{
DefaultBinaryTree, DefaultPieceDomain, DefaultPieceHasher, SINGLE_PARTITION_PROOF_LEN,
self, DefaultBinaryTree, DefaultPieceDomain, DefaultPieceHasher, SINGLE_PARTITION_PROOF_LEN,
},
parameters::setup_params,
pieces::{self, verify_pieces},
Expand Down Expand Up @@ -1052,8 +1051,9 @@ pub fn verify_seal<Tree: 'static + MerkleTreeTrait>(
&public_inputs,
&proof,
&ChallengeRequirements {
minimum_challenges: POREP_MINIMUM_CHALLENGES
.from_sector_size(u64::from(porep_config.sector_size)),
minimum_challenges: constants::get_porep_interactive_minimum_challenges(u64::from(
porep_config.sector_size,
)),
},
)
};
Expand Down Expand Up @@ -1167,8 +1167,9 @@ pub fn verify_batch_seal<Tree: 'static + MerkleTreeTrait>(
&public_inputs,
&proofs,
&ChallengeRequirements {
minimum_challenges: POREP_MINIMUM_CHALLENGES
.from_sector_size(u64::from(porep_config.sector_size)),
minimum_challenges: constants::get_porep_interactive_minimum_challenges(u64::from(
porep_config.sector_size,
)),
},
)
.map_err(Into::into);
Expand Down
78 changes: 44 additions & 34 deletions filecoin-proofs/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::HashMap;
use std::sync::RwLock;
use std::{collections::HashMap, sync::RwLockWriteGuard};

pub use storage_proofs_core::drgraph::BASE_DEGREE as DRG_DEGREE;
pub use storage_proofs_porep::stacked::EXP_DEGREE;
Expand Down Expand Up @@ -47,47 +47,57 @@ pub const PUBLISHED_SECTOR_SIZES: [u64; 10] = [
SECTOR_SIZE_64_GIB,
];

pub struct PorepMinimumChallenges(RwLock<HashMap<u64, usize>>);
impl PorepMinimumChallenges {
fn new() -> Self {
Self(RwLock::new(
[
(SECTOR_SIZE_2_KIB, 2),
(SECTOR_SIZE_4_KIB, 2),
(SECTOR_SIZE_16_KIB, 2),
(SECTOR_SIZE_32_KIB, 2),
(SECTOR_SIZE_8_MIB, 2),
(SECTOR_SIZE_16_MIB, 2),
(SECTOR_SIZE_512_MIB, 2),
(SECTOR_SIZE_1_GIB, 2),
(SECTOR_SIZE_32_GIB, 176),
(SECTOR_SIZE_64_GIB, 176),
]
.iter()
.copied()
.collect(),
))
/// Returns the minimum number of challenges used for a certain sector size.
pub(crate) const fn get_porep_interactive_minimum_challenges(sector_size: u64) -> usize {
match sector_size {
SECTOR_SIZE_2_KIB | SECTOR_SIZE_4_KIB | SECTOR_SIZE_16_KIB | SECTOR_SIZE_32_KIB
| SECTOR_SIZE_8_MIB | SECTOR_SIZE_16_MIB | SECTOR_SIZE_512_MIB | SECTOR_SIZE_1_GIB => 2,
SECTOR_SIZE_32_GIB | SECTOR_SIZE_64_GIB => 176,
_ => panic!("invalid sector size"),
}
}

pub fn get_mut(&self) -> RwLockWriteGuard<'_, HashMap<u64, usize>> {
self.0.write().expect("POREP_MINIMUM_CHALLENGES poisoned")
/// Returns the number of partitions for a certain sector size.
pub const fn get_porep_interactive_partitions(sector_size: u64) -> u8 {
match sector_size {
SECTOR_SIZE_2_KIB | SECTOR_SIZE_4_KIB | SECTOR_SIZE_16_KIB | SECTOR_SIZE_32_KIB
| SECTOR_SIZE_8_MIB | SECTOR_SIZE_16_MIB | SECTOR_SIZE_512_MIB | SECTOR_SIZE_1_GIB => 1,
SECTOR_SIZE_32_GIB | SECTOR_SIZE_64_GIB => 10,
_ => panic!("invalid sector size"),
}
}

pub fn from_sector_size(&self, sector_size: u64) -> usize {
match self
.0
.read()
.expect("POREP_MINIMUM_CHALLENGES poisoned")
.get(&sector_size)
{
Some(c) => *c,
None => panic!("invalid sector size"),
}
/// Returns the number of layers for a certain sector size.
pub const fn get_layers(sector_size: u64) -> usize {
match sector_size {
SECTOR_SIZE_2_KIB | SECTOR_SIZE_4_KIB | SECTOR_SIZE_16_KIB | SECTOR_SIZE_32_KIB
| SECTOR_SIZE_8_MIB | SECTOR_SIZE_16_MIB | SECTOR_SIZE_512_MIB | SECTOR_SIZE_1_GIB => 2,
SECTOR_SIZE_32_GIB | SECTOR_SIZE_64_GIB => 11,
_ => panic!("invalid sector size"),
}
}

/// Returns the number of layers for a certain sector size.
///
/// These numbers must match those used for Window PoSt scheduling in the miner actor.
/// Please coordinate changes with actor code.
/// https://github.com/filecoin-project/specs-actors/blob/master/actors/abi/sector.go
pub const fn get_window_post_sector_count(sector_size: u64) -> usize {
match sector_size {
SECTOR_SIZE_2_KIB | SECTOR_SIZE_4_KIB | SECTOR_SIZE_16_KIB | SECTOR_SIZE_32_KIB
| SECTOR_SIZE_8_MIB | SECTOR_SIZE_16_MIB | SECTOR_SIZE_512_MIB | SECTOR_SIZE_1_GIB => 2,
// this gives 125,279,217 constraints, fitting in a single partition
SECTOR_SIZE_32_GIB => 2349,
// this gives 129,887,900 constraints, fitting in a single partition
SECTOR_SIZE_64_GIB => 2300,
_ => panic!("invalid sector size"),
}
}

// NOTE vmx 2023-10-17: Those constants are deprecated, use the functions above instead. The
// constants are only kept, so that filecoin-proofs-api can still be compiled without any changes.
// They cane be removed, once filecoin-api-proofs are upgraded to this version.
lazy_static! {
pub static ref POREP_MINIMUM_CHALLENGES: PorepMinimumChallenges = PorepMinimumChallenges::new();
pub static ref POREP_PARTITIONS: RwLock<HashMap<u64, u8>> = RwLock::new(
[
(SECTOR_SIZE_2_KIB, 1),
Expand Down
11 changes: 3 additions & 8 deletions filecoin-proofs/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use storage_proofs_porep::stacked::{self, Challenges, StackedDrg};
use storage_proofs_post::fallback::{self, FallbackPoSt};

use crate::{
constants::{DefaultPieceHasher, DRG_DEGREE, EXP_DEGREE, LAYERS},
constants::{self, DefaultPieceHasher, DRG_DEGREE, EXP_DEGREE},
types::{MerkleTreeTrait, PoRepConfig, PoStConfig},
POREP_MINIMUM_CHALLENGES,
};

type WinningPostSetupParams = fallback::SetupParams;
Expand Down Expand Up @@ -71,15 +70,11 @@ pub fn setup_params(porep_config: &PoRepConfig) -> Result<stacked::SetupParams>
let sector_bytes = porep_config.padded_bytes_amount();
let challenges = select_challenges(
usize::from(porep_config.partitions),
POREP_MINIMUM_CHALLENGES.from_sector_size(u64::from(sector_bytes)),
constants::get_porep_interactive_minimum_challenges(u64::from(sector_bytes)),
&porep_config.api_features,
);
let num_layers = *LAYERS
.read()
.expect("LAYERS poisoned")
.get(&u64::from(sector_bytes))
.expect("unknown sector size");
let sector_bytes = u64::from(sector_bytes);
let num_layers = constants::get_layers(sector_bytes);

ensure!(
sector_bytes % 32 == 0,
Expand Down
14 changes: 5 additions & 9 deletions filecoin-proofs/src/types/porep_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use storage_proofs_core::{
use storage_proofs_porep::stacked::{StackedCircuit, StackedCompound};

use crate::{
constants::DefaultPieceHasher,
constants::{self, DefaultPieceHasher},
parameters::public_params,
types::{PaddedBytesAmount, PoRepProofPartitions, SectorSize, UnpaddedBytesAmount},
POREP_PARTITIONS,
};
// This is only public due to the use in some tooling.

#[derive(Clone, Debug)]
pub struct PoRepConfig {
Expand Down Expand Up @@ -60,13 +60,9 @@ impl PoRepConfig {
pub fn new_groth16(sector_size: u64, porep_id: [u8; 32], api_version: ApiVersion) -> Self {
Self {
sector_size: SectorSize(sector_size),
partitions: PoRepProofPartitions(
*POREP_PARTITIONS
.read()
.expect("POREP_PARTITIONS poisoned")
.get(&sector_size)
.expect("unknown sector size"),
),
partitions: PoRepProofPartitions(constants::get_porep_interactive_partitions(
sector_size,
)),
porep_id,
api_version,
api_features: vec![],
Expand Down
Loading

0 comments on commit 26a1965

Please sign in to comment.