Skip to content

Commit

Permalink
fix(pallet-storage-provider): remove Policy (#591)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmg-duarte authored Nov 20, 2024
1 parent 41260b7 commit 339107f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 85 deletions.
87 changes: 23 additions & 64 deletions pallets/storage-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub mod pallet {

/// Allows to extract Balance of an account via the Config::Currency associated type.
/// BalanceOf is a sophisticated way of getting an u128.
type BalanceOf<T> =
pub type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as SystemConfig>::AccountId>>::Balance;

#[pallet::pallet]
Expand Down Expand Up @@ -368,36 +368,6 @@ pub mod pallet {
}
}

#[derive(RuntimeDebug, Clone, Copy, Decode, Encode, TypeInfo)]
pub struct Policy<BlockNumber> {
pub max_partitions_per_deadline: u64,
pub w_post_period_deadlines: u64,
pub w_post_proving_period: BlockNumber,
pub w_post_challenge_window: BlockNumber,
pub w_post_challenge_lookback: BlockNumber,
pub fault_declaration_cutoff: BlockNumber,
}

impl<BlockNumber> Policy<BlockNumber> {
pub fn new(
max_partitions_per_deadline: u64,
w_post_period_deadlines: u64,
w_post_proving_period: BlockNumber,
w_post_challenge_window: BlockNumber,
w_post_challenge_lookback: BlockNumber,
fault_declaration_cutoff: BlockNumber,
) -> Self {
Self {
max_partitions_per_deadline,
w_post_period_deadlines,
w_post_proving_period,
w_post_challenge_window,
w_post_challenge_lookback,
fault_declaration_cutoff,
}
}
}

#[pallet::call]
impl<T: Config> Pallet<T> {
pub fn register_storage_provider(
Expand Down Expand Up @@ -427,7 +397,7 @@ pub mod pallet {
proving_period,
);
let info = StorageProviderInfo::new(peer_id, window_post_proof_type);
let state = StorageProviderState::new(
let state = StorageProviderState::<T::PeerId, BalanceOf<T>, BlockNumberFor<T>>::new(
info.clone(),
local_proving_start,
// Always zero since we're calculating the absolute first start
Expand Down Expand Up @@ -645,14 +615,12 @@ pub mod pallet {
current_block,
new_sectors,
sp.info.window_post_partition_sectors,
Policy::new(
T::MaxPartitionsPerDeadline::get(),
T::WPoStPeriodDeadlines::get(),
T::WPoStProvingPeriod::get(),
T::WPoStChallengeWindow::get(),
T::WPoStChallengeLookBack::get(),
T::FaultDeclarationCutoff::get(),
),
T::MaxPartitionsPerDeadline::get(),
T::WPoStPeriodDeadlines::get(),
T::WPoStProvingPeriod::get(),
T::WPoStChallengeWindow::get(),
T::WPoStChallengeLookBack::get(),
T::FaultDeclarationCutoff::get(),
)
.map_err(|e| Error::<T>::GeneralPalletError(e))?;

Expand Down Expand Up @@ -740,14 +708,11 @@ pub mod pallet {
let current_deadline = sp
.deadline_info(
current_block,
Policy::new(
T::MaxPartitionsPerDeadline::get(),
T::WPoStPeriodDeadlines::get(),
T::WPoStProvingPeriod::get(),
T::WPoStChallengeWindow::get(),
T::WPoStChallengeLookBack::get(),
T::FaultDeclarationCutoff::get(),
),
T::WPoStPeriodDeadlines::get(),
T::WPoStProvingPeriod::get(),
T::WPoStChallengeWindow::get(),
T::WPoStChallengeLookBack::get(),
T::FaultDeclarationCutoff::get(),
)
.map_err(|e| Error::<T>::GeneralPalletError(e))?;

Expand Down Expand Up @@ -1245,14 +1210,11 @@ pub mod pallet {

let Ok(current_deadline) = state.deadline_info(
current_block,
Policy::new(
T::MaxPartitionsPerDeadline::get(),
T::WPoStPeriodDeadlines::get(),
T::WPoStProvingPeriod::get(),
T::WPoStChallengeWindow::get(),
T::WPoStChallengeLookBack::get(),
T::FaultDeclarationCutoff::get(),
),
T::WPoStPeriodDeadlines::get(),
T::WPoStProvingPeriod::get(),
T::WPoStChallengeWindow::get(),
T::WPoStChallengeLookBack::get(),
T::FaultDeclarationCutoff::get(),
) else {
log::error!(target: LOG_TARGET, "block: {:?}, there are no deadlines for storage provider {:?}", current_block, storage_provider);
continue;
Expand Down Expand Up @@ -1352,14 +1314,11 @@ pub mod pallet {
state
.advance_deadline(
current_block,
Policy::new(
T::MaxPartitionsPerDeadline::get(),
T::WPoStPeriodDeadlines::get(),
T::WPoStProvingPeriod::get(),
T::WPoStChallengeWindow::get(),
T::WPoStChallengeLookBack::get(),
T::FaultDeclarationCutoff::get(),
),
T::WPoStPeriodDeadlines::get(),
T::WPoStProvingPeriod::get(),
T::WPoStChallengeWindow::get(),
T::WPoStChallengeLookBack::get(),
T::FaultDeclarationCutoff::get(),
)
.expect("Could not advance deadline");
StorageProviders::<T>::insert(storage_provider, state);
Expand Down
61 changes: 40 additions & 21 deletions pallets/storage-provider/src/storage_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use sp_arithmetic::{traits::BaseArithmetic, ArithmeticError};
use crate::{
deadline::{assign_deadlines, deadline_is_mutable, Deadline, DeadlineInfo, Deadlines},
error::GeneralPalletError,
pallet::Policy,
partition::TerminationResult,
sector::{SectorOnChainInfo, SectorPreCommitOnChainInfo, MAX_SECTORS},
};
Expand Down Expand Up @@ -105,20 +104,31 @@ where
pub fn advance_deadline(
&mut self,
current_block: BlockNumber,
policy: Policy<BlockNumber>,
w_post_period_deadlines: u64,
w_post_proving_period: BlockNumber,
w_post_challenge_window: BlockNumber,
w_post_challenge_lookback: BlockNumber,
fault_declaration_cutoff: BlockNumber,
) -> Result<(), GeneralPalletError> {
let dl_info = self.deadline_info(current_block, policy)?;
let dl_info = self.deadline_info(
current_block,
w_post_period_deadlines,
w_post_proving_period,
w_post_challenge_window,
w_post_challenge_lookback,
fault_declaration_cutoff,
)?;

if !dl_info.period_started() {
return Ok(());
}

self.current_deadline = (self.current_deadline + 1) % policy.w_post_period_deadlines;
self.current_deadline = (self.current_deadline + 1) % w_post_period_deadlines;
log::debug!(target: LOG_TARGET, "new deadline {:?}, period deadlines {:?}",
self.current_deadline, policy.w_post_period_deadlines);
self.current_deadline, w_post_period_deadlines);

if self.current_deadline == 0 {
self.proving_period_start = self.proving_period_start + policy.w_post_proving_period;
self.proving_period_start = self.proving_period_start + w_post_proving_period;
}

let deadline = self.deadlines.load_deadline_mut(dl_info.idx as usize)?;
Expand Down Expand Up @@ -209,7 +219,12 @@ where
current_block: BlockNumber,
mut sectors: BoundedVec<SectorOnChainInfo<BlockNumber>, ConstU32<MAX_SECTORS>>,
partition_size: u64,
policy: Policy<BlockNumber>,
max_partitions_per_deadline: u64,
w_post_period_deadlines: u64,
w_post_proving_period: BlockNumber,
w_post_challenge_window: BlockNumber,
w_post_challenge_lookback: BlockNumber,
fault_declaration_cutoff: BlockNumber,
) -> Result<(), GeneralPalletError> {
sectors.sort_by_key(|info| info.sector_number);

Expand All @@ -219,7 +234,7 @@ where
);

let mut deadline_vec: Vec<Option<Deadline<BlockNumber>>> =
(0..policy.w_post_period_deadlines).map(|_| None).collect();
(0..w_post_period_deadlines).map(|_| None).collect();

// required otherwise the logic gets complicated really fast
// the issue is that filecoin supports negative epoch numbers
Expand All @@ -234,11 +249,11 @@ where
self.proving_period_start,
idx as u64,
current_block,
policy.w_post_period_deadlines,
policy.w_post_proving_period,
policy.w_post_challenge_window,
policy.w_post_challenge_lookback,
policy.fault_declaration_cutoff,
w_post_period_deadlines,
w_post_proving_period,
w_post_challenge_window,
w_post_challenge_lookback,
fault_declaration_cutoff,
)?;
if is_deadline_mutable {
log::debug!(target: LOG_TARGET, "deadline[{idx}] is mutable");
Expand All @@ -254,11 +269,11 @@ where

// Assign sectors to deadlines.
let deadline_to_sectors = assign_deadlines(
policy.max_partitions_per_deadline,
max_partitions_per_deadline,
partition_size,
&deadline_vec,
&sectors,
policy.w_post_period_deadlines,
w_post_period_deadlines,
)?;

for (deadline_idx, deadline_sectors) in deadline_to_sectors.iter().enumerate() {
Expand Down Expand Up @@ -288,19 +303,23 @@ where
pub fn deadline_info(
&self,
current_block: BlockNumber,
policy: Policy<BlockNumber>,
w_post_period_deadlines: u64,
w_post_proving_period: BlockNumber,
w_post_challenge_window: BlockNumber,
w_post_challenge_lookback: BlockNumber,
fault_declaration_cutoff: BlockNumber,
) -> Result<DeadlineInfo<BlockNumber>, GeneralPalletError> {
let current_deadline_index = self.current_deadline;

DeadlineInfo::new(
current_block,
self.proving_period_start,
current_deadline_index,
policy.w_post_period_deadlines,
policy.w_post_proving_period,
policy.w_post_challenge_window,
policy.w_post_challenge_lookback,
policy.fault_declaration_cutoff,
w_post_period_deadlines,
w_post_proving_period,
w_post_challenge_window,
w_post_challenge_lookback,
fault_declaration_cutoff,
)
}

Expand Down

0 comments on commit 339107f

Please sign in to comment.