Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pallet-storage-provider): remove Policy #591

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 27 additions & 68 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 @@ -641,18 +611,16 @@ pub mod pallet {

// Assign sectors to deadlines which specify when sectors needs
// to be proven
sp.assign_sectors_to_deadlines(
sp.assign_sectors_to_deadlines::<T>(
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 @@ -738,16 +706,13 @@ pub mod pallet {
Error::<T>::InvalidDeadlineSubmission
});
let current_deadline = sp
.deadline_info(
.deadline_info::<T>(
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 @@ -1243,16 +1208,13 @@ pub mod pallet {
continue;
}

let Ok(current_deadline) = state.deadline_info(
let Ok(current_deadline) = state.deadline_info::<T>(
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 @@ -1350,16 +1312,13 @@ pub mod pallet {
// Next processing will happen in the next proving period.
deadline.partitions_posted = BoundedBTreeSet::new();
state
.advance_deadline(
.advance_deadline::<T>(
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
87 changes: 60 additions & 27 deletions pallets/storage-provider/src/storage_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ use frame_support::{
pallet_prelude::{ConstU32, RuntimeDebug},
sp_runtime::{BoundedBTreeMap, BoundedVec},
};
use frame_system::pallet_prelude::BlockNumberFor;
use primitives_proofs::{RegisteredPoStProof, SectorNumber, SectorSize};
use scale_info::TypeInfo;
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},
Config,
};

const LOG_TARGET: &'static str = "runtime::storage_provider::storage_provider";
Expand Down Expand Up @@ -102,23 +103,38 @@ where
}

/// Advance the proving period start of the storage provider if the next deadline is the first one.
pub fn advance_deadline(
pub fn advance_deadline<C>(
&mut self,
current_block: BlockNumber,
policy: Policy<BlockNumber>,
) -> Result<(), GeneralPalletError> {
let dl_info = self.deadline_info(current_block, policy)?;
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>
where
C: Config,
BlockNumberFor<C>: sp_runtime::traits::BlockNumber,
jmg-duarte marked this conversation as resolved.
Show resolved Hide resolved
{
let dl_info = self.deadline_info::<C>(
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 @@ -204,13 +220,22 @@ where
///
/// Reference:
/// * <https://github.com/filecoin-project/builtin-actors/blob/17ede2b256bc819dc309edf38e031e246a516486/actors/miner/src/state.rs#L489-L554>
pub fn assign_sectors_to_deadlines(
pub fn assign_sectors_to_deadlines<C>(
&mut self,
current_block: BlockNumber,
mut sectors: BoundedVec<SectorOnChainInfo<BlockNumber>, ConstU32<MAX_SECTORS>>,
partition_size: u64,
policy: Policy<BlockNumber>,
) -> Result<(), GeneralPalletError> {
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>
where
C: Config,
BlockNumberFor<C>: sp_runtime::traits::BlockNumber,
{
sectors.sort_by_key(|info| info.sector_number);

log::debug!(target: LOG_TARGET,
Expand All @@ -219,7 +244,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 +259,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 +279,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 All @@ -285,22 +310,30 @@ where
/// Returns deadline calculations for the current (according to state) proving period.
///
/// **Pre-condition**: `current_block > self.proving_period_start`
pub fn deadline_info(
pub fn deadline_info<C>(
&self,
current_block: BlockNumber,
policy: Policy<BlockNumber>,
) -> Result<DeadlineInfo<BlockNumber>, GeneralPalletError> {
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>
where
C: Config,
BlockNumberFor<C>: sp_runtime::traits::BlockNumber,
{
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