use std::collections::HashMap;
use std::sync::RwLock;
pub use storage_proofs_core::drgraph::BASE_DEGREE as DRG_DEGREE;
pub use storage_proofs_porep::stacked::EXP_DEGREE;
use filecoin_hashers::{poseidon::PoseidonHasher, sha256::Sha256Hasher, Hasher};
use lazy_static::lazy_static;
use storage_proofs_core::{
merkle::{BinaryMerkleTree, DiskTree, LCTree},
util::NODE_SIZE,
MAX_LEGACY_POREP_REGISTERED_PROOF_ID,
};
use typenum::{U0, U2, U8};
use crate::types::UnpaddedBytesAmount;
pub const SECTOR_SIZE_2_KIB: u64 = 1 << 11;
pub const SECTOR_SIZE_4_KIB: u64 = 1 << 12;
pub const SECTOR_SIZE_16_KIB: u64 = 1 << 14;
pub const SECTOR_SIZE_32_KIB: u64 = 1 << 15;
pub const SECTOR_SIZE_8_MIB: u64 = 1 << 23;
pub const SECTOR_SIZE_16_MIB: u64 = 1 << 24;
pub const SECTOR_SIZE_512_MIB: u64 = 1 << 29;
pub const SECTOR_SIZE_1_GIB: u64 = 1 << 30;
pub const SECTOR_SIZE_32_GIB: u64 = 1 << 35;
pub const SECTOR_SIZE_64_GIB: u64 = 1 << 36;
pub const WINNING_POST_CHALLENGE_COUNT: usize = 66;
pub const WINNING_POST_SECTOR_COUNT: usize = 1;
pub const WINDOW_POST_CHALLENGE_COUNT: usize = 10;
pub const MAX_LEGACY_REGISTERED_SEAL_PROOF_ID: u64 = MAX_LEGACY_POREP_REGISTERED_PROOF_ID;
pub const FIP92_MIN_NI_POREP_AGGREGATION_PROOFS: usize = 1;
pub const FIP92_MAX_NI_POREP_AGGREGATION_PROOFS: usize = 65;
pub const SUPPORTED_SECTOR_SIZES: [u64; 10] = [
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,
SECTOR_SIZE_32_GIB,
SECTOR_SIZE_64_GIB,
];
pub const PUBLISHED_SECTOR_SIZES: [u64; 5] = [
SECTOR_SIZE_2_KIB,
SECTOR_SIZE_8_MIB,
SECTOR_SIZE_512_MIB,
SECTOR_SIZE_32_GIB,
SECTOR_SIZE_64_GIB,
];
lazy_static! {
pub static ref POREP_PARTITIONS: RwLock<HashMap<u64, u8>> = RwLock::new(
[
(SECTOR_SIZE_2_KIB, 1),
(SECTOR_SIZE_4_KIB, 1),
(SECTOR_SIZE_16_KIB, 1),
(SECTOR_SIZE_32_KIB, 1),
(SECTOR_SIZE_8_MIB, 1),
(SECTOR_SIZE_16_MIB, 1),
(SECTOR_SIZE_512_MIB, 1),
(SECTOR_SIZE_1_GIB, 1),
(SECTOR_SIZE_32_GIB, 10),
(SECTOR_SIZE_64_GIB, 10),
]
.iter()
.copied()
.collect()
);
pub static ref LAYERS: RwLock<HashMap<u64, usize>> = 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, 11),
(SECTOR_SIZE_64_GIB, 11),
]
.iter()
.copied()
.collect()
);
pub static ref WINDOW_POST_SECTOR_COUNT: RwLock<HashMap<u64, usize>> = 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, 2349), (SECTOR_SIZE_64_GIB, 2300), ]
.iter()
.copied()
.collect()
);
}
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(crate) const fn get_porep_non_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 => 26,
SECTOR_SIZE_32_GIB | SECTOR_SIZE_64_GIB => 2253,
_ => panic!("invalid sector size"),
}
}
pub const fn get_porep_non_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 => 13,
SECTOR_SIZE_32_GIB | SECTOR_SIZE_64_GIB => 126,
_ => panic!("invalid sector size"),
}
}
pub const SINGLE_PARTITION_PROOF_LEN: usize = 192;
pub const MINIMUM_RESERVED_LEAVES_FOR_PIECE_IN_SECTOR: u64 = 4;
pub const MINIMUM_RESERVED_BYTES_FOR_PIECE_IN_FULLY_ALIGNED_SECTOR: u64 =
(MINIMUM_RESERVED_LEAVES_FOR_PIECE_IN_SECTOR * NODE_SIZE as u64) - 1;
pub const MIN_PIECE_SIZE: UnpaddedBytesAmount = UnpaddedBytesAmount(127);
pub(crate) const MAX_CHALLENGES_PER_PARTITION: u8 = 18;
pub type DefaultPieceHasher = Sha256Hasher;
pub type DefaultPieceDomain = <DefaultPieceHasher as Hasher>::Domain;
pub type DefaultTreeHasher = PoseidonHasher;
pub type DefaultTreeDomain = <DefaultTreeHasher as Hasher>::Domain;
pub type DefaultBinaryTree = BinaryMerkleTree<DefaultTreeHasher>;
pub type DefaultOctTree = DiskTree<DefaultTreeHasher, U8, U0, U0>;
pub type DefaultOctLCTree = LCTree<DefaultTreeHasher, U8, U0, U0>;
pub type SectorShapeBase = LCTree<DefaultTreeHasher, U8, U0, U0>;
pub type SectorShapeSub2 = LCTree<DefaultTreeHasher, U8, U2, U0>;
pub type SectorShapeSub8 = LCTree<DefaultTreeHasher, U8, U8, U0>;
pub type SectorShapeTop2 = LCTree<DefaultTreeHasher, U8, U8, U2>;
pub type SectorShape2KiB = SectorShapeBase;
pub type SectorShape8MiB = SectorShapeBase;
pub type SectorShape512MiB = SectorShapeBase;
pub type SectorShape4KiB = SectorShapeSub2;
pub type SectorShape16MiB = SectorShapeSub2;
pub type SectorShape1GiB = SectorShapeSub2;
pub type SectorShape16KiB = SectorShapeSub8;
pub type SectorShape32GiB = SectorShapeSub8;
pub type SectorShape32KiB = SectorShapeTop2;
pub type SectorShape64GiB = SectorShapeTop2;
pub fn is_sector_shape_base(sector_size: u64) -> bool {
matches!(
sector_size,
SECTOR_SIZE_2_KIB | SECTOR_SIZE_8_MIB | SECTOR_SIZE_512_MIB
)
}
pub fn is_sector_shape_sub2(sector_size: u64) -> bool {
matches!(
sector_size,
SECTOR_SIZE_4_KIB | SECTOR_SIZE_16_MIB | SECTOR_SIZE_1_GIB
)
}
pub fn is_sector_shape_sub8(sector_size: u64) -> bool {
matches!(sector_size, SECTOR_SIZE_16_KIB | SECTOR_SIZE_32_GIB)
}
pub fn is_sector_shape_top2(sector_size: u64) -> bool {
matches!(sector_size, SECTOR_SIZE_32_KIB | SECTOR_SIZE_64_GIB)
}
#[macro_export]
macro_rules! with_shape {
($size:expr, $f:ident) => {
with_shape!($size, $f,)
};
($size:expr, $f:ident, $($args:expr,)*) => {
match $size {
_x if $size == $crate::constants::SECTOR_SIZE_2_KIB => {
$f::<$crate::constants::SectorShape2KiB>($($args),*)
},
_x if $size == $crate::constants::SECTOR_SIZE_4_KIB => {
$f::<$crate::constants::SectorShape4KiB>($($args),*)
},
_x if $size == $crate::constants::SECTOR_SIZE_16_KIB => {
$f::<$crate::constants::SectorShape16KiB>($($args),*)
},
_x if $size == $crate::constants::SECTOR_SIZE_32_KIB => {
$f::<$crate::constants::SectorShape32KiB>($($args),*)
},
_xx if $size == $crate::constants::SECTOR_SIZE_8_MIB => {
$f::<$crate::constants::SectorShape8MiB>($($args),*)
},
_xx if $size == $crate::constants::SECTOR_SIZE_16_MIB => {
$f::<$crate::constants::SectorShape16MiB>($($args),*)
},
_x if $size == $crate::constants::SECTOR_SIZE_512_MIB => {
$f::<$crate::constants::SectorShape512MiB>($($args),*)
},
_x if $size == $crate::constants::SECTOR_SIZE_1_GIB => {
$f::<$crate::constants::SectorShape1GiB>($($args),*)
},
_x if $size == $crate::constants::SECTOR_SIZE_32_GIB => {
$f::<$crate::constants::SectorShape32GiB>($($args),*)
},
_x if $size == $crate::constants::SECTOR_SIZE_64_GIB => {
$f::<$crate::constants::SectorShape64GiB>($($args),*)
},
_ => panic!("unsupported sector size: {}", $size),
}
};
($size:expr, $f:ident, $($args:expr),*) => {
with_shape!($size, $f, $($args,)*)
};
}
pub const TEST_SEED: [u8; 16] = [
0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc, 0xe5,
];