use fil_actors_shared::v12::runtime::Policy;
use fil_actors_shared::v12::Array;
use fvm_ipld_blockstore::Blockstore;
use fvm_shared4::clock::{ChainEpoch, QuantSpec};
use fvm_shared4::sector::SectorNumber;
use super::{DeadlineInfo, Deadlines, Partition};
pub fn new_deadline_info(
policy: &Policy,
proving_period_start: ChainEpoch,
deadline_idx: u64,
current_epoch: ChainEpoch,
) -> DeadlineInfo {
DeadlineInfo::new(
proving_period_start,
deadline_idx,
current_epoch,
policy.wpost_period_deadlines,
policy.wpost_proving_period,
policy.wpost_challenge_window,
policy.wpost_challenge_lookback,
policy.fault_declaration_cutoff,
)
}
impl Deadlines {
pub fn find_sector<BS: Blockstore>(
&self,
store: &BS,
sector_number: SectorNumber,
) -> anyhow::Result<(u64, u64)> {
for i in 0..self.due.len() {
let deadline_idx = i as u64;
let deadline = self.load_deadline(store, deadline_idx)?;
let partitions = Array::<Partition, _>::load(&deadline.partitions, store)?;
let mut partition_idx = None;
partitions.for_each_while(|i, partition| {
if partition.sectors.get(sector_number) {
partition_idx = Some(i);
Ok(false)
} else {
Ok(true)
}
})?;
if let Some(partition_idx) = partition_idx {
return Ok((deadline_idx, partition_idx));
}
}
Err(anyhow::anyhow!(
"sector {} not due at any deadline",
sector_number
))
}
}
pub fn deadline_is_mutable(
policy: &Policy,
proving_period_start: ChainEpoch,
deadline_idx: u64,
current_epoch: ChainEpoch,
) -> bool {
let deadline_info =
new_deadline_info(policy, proving_period_start, deadline_idx, current_epoch)
.next_not_elapsed();
current_epoch < deadline_info.open - policy.wpost_challenge_window
}
pub fn quant_spec_for_deadline(policy: &Policy, di: &DeadlineInfo) -> QuantSpec {
QuantSpec {
unit: policy.wpost_proving_period,
offset: di.last(),
}
}
pub fn deadline_available_for_optimistic_post_dispute(
policy: &Policy,
proving_period_start: ChainEpoch,
deadline_idx: u64,
current_epoch: ChainEpoch,
) -> bool {
if proving_period_start > current_epoch {
return false;
}
let dl_info = new_deadline_info(policy, proving_period_start, deadline_idx, current_epoch)
.next_not_elapsed();
!dl_info.is_open()
&& current_epoch
< (dl_info.close - policy.wpost_proving_period) + policy.wpost_dispute_window
}
pub fn deadline_available_for_compaction(
policy: &Policy,
proving_period_start: ChainEpoch,
deadline_idx: u64,
current_epoch: ChainEpoch,
) -> bool {
deadline_is_mutable(policy, proving_period_start, deadline_idx, current_epoch)
&& !deadline_available_for_optimistic_post_dispute(
policy,
proving_period_start,
deadline_idx,
current_epoch,
)
}
pub fn new_deadline_info_from_offset_and_epoch(
policy: &Policy,
period_start_seed: ChainEpoch,
current_epoch: ChainEpoch,
) -> DeadlineInfo {
let q = QuantSpec {
unit: policy.wpost_proving_period,
offset: period_start_seed,
};
let current_period_start = q.quantize_down(current_epoch);
let current_deadline_idx =
((current_epoch - current_period_start) / policy.wpost_challenge_window) as u64;
new_deadline_info(
policy,
current_period_start,
current_deadline_idx,
current_epoch,
)
}