From 294f4783af21cbbb469b7e07aad896f350c7f6f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Duarte?= Date: Mon, 18 Nov 2024 17:23:46 +0000 Subject: [PATCH 1/2] fix(pallet-storage-provider): remove Policy --- pallets/storage-provider/src/lib.rs | 95 ++++++------------- .../storage-provider/src/storage_provider.rs | 87 +++++++++++------ 2 files changed, 87 insertions(+), 95 deletions(-) diff --git a/pallets/storage-provider/src/lib.rs b/pallets/storage-provider/src/lib.rs index 2bc4772a..c1b07f8d 100644 --- a/pallets/storage-provider/src/lib.rs +++ b/pallets/storage-provider/src/lib.rs @@ -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 = + pub type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; #[pallet::pallet] @@ -368,36 +368,6 @@ pub mod pallet { } } - #[derive(RuntimeDebug, Clone, Copy, Decode, Encode, TypeInfo)] - pub struct Policy { - 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 Policy { - 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 Pallet { pub fn register_storage_provider( @@ -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::, BlockNumberFor>::new( info.clone(), local_proving_start, // Always zero since we're calculating the absolute first start @@ -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::( 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::::GeneralPalletError(e))?; @@ -738,16 +706,13 @@ pub mod pallet { Error::::InvalidDeadlineSubmission }); let current_deadline = sp - .deadline_info( + .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::::GeneralPalletError(e))?; @@ -1243,16 +1208,13 @@ pub mod pallet { continue; } - let Ok(current_deadline) = state.deadline_info( + 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; @@ -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::( 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::::insert(storage_provider, state); diff --git a/pallets/storage-provider/src/storage_provider.rs b/pallets/storage-provider/src/storage_provider.rs index 2e539943..ac41882e 100644 --- a/pallets/storage-provider/src/storage_provider.rs +++ b/pallets/storage-provider/src/storage_provider.rs @@ -7,6 +7,7 @@ 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}; @@ -14,9 +15,9 @@ 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"; @@ -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( &mut self, current_block: BlockNumber, - policy: Policy, - ) -> 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: sp_runtime::traits::BlockNumber, + { + 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)?; @@ -204,13 +220,22 @@ where /// /// Reference: /// * - pub fn assign_sectors_to_deadlines( + pub fn assign_sectors_to_deadlines( &mut self, current_block: BlockNumber, mut sectors: BoundedVec, ConstU32>, partition_size: u64, - policy: Policy, - ) -> 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: sp_runtime::traits::BlockNumber, + { sectors.sort_by_key(|info| info.sector_number); log::debug!(target: LOG_TARGET, @@ -219,7 +244,7 @@ where ); let mut deadline_vec: Vec>> = - (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 @@ -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"); @@ -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, §ors, - policy.w_post_period_deadlines, + w_post_period_deadlines, )?; for (deadline_idx, deadline_sectors) in deadline_to_sectors.iter().enumerate() { @@ -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( &self, current_block: BlockNumber, - policy: Policy, - ) -> Result, 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, GeneralPalletError> + where + C: Config, + BlockNumberFor: 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, ) } From 3e1abd0fbbe9f76d7b427e4fdf49b53ccb71be98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Duarte?= Date: Tue, 19 Nov 2024 11:03:33 +0000 Subject: [PATCH 2/2] address changes --- pallets/storage-provider/src/lib.rs | 8 +++--- .../storage-provider/src/storage_provider.rs | 28 +++++-------------- 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/pallets/storage-provider/src/lib.rs b/pallets/storage-provider/src/lib.rs index c1b07f8d..9c4f29d6 100644 --- a/pallets/storage-provider/src/lib.rs +++ b/pallets/storage-provider/src/lib.rs @@ -611,7 +611,7 @@ 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( current_block, new_sectors, sp.info.window_post_partition_sectors, @@ -706,7 +706,7 @@ pub mod pallet { Error::::InvalidDeadlineSubmission }); let current_deadline = sp - .deadline_info::( + .deadline_info( current_block, T::WPoStPeriodDeadlines::get(), T::WPoStProvingPeriod::get(), @@ -1208,7 +1208,7 @@ pub mod pallet { continue; } - let Ok(current_deadline) = state.deadline_info::( + let Ok(current_deadline) = state.deadline_info( current_block, T::WPoStPeriodDeadlines::get(), T::WPoStProvingPeriod::get(), @@ -1312,7 +1312,7 @@ pub mod pallet { // Next processing will happen in the next proving period. deadline.partitions_posted = BoundedBTreeSet::new(); state - .advance_deadline::( + .advance_deadline( current_block, T::WPoStPeriodDeadlines::get(), T::WPoStProvingPeriod::get(), diff --git a/pallets/storage-provider/src/storage_provider.rs b/pallets/storage-provider/src/storage_provider.rs index ac41882e..183e883c 100644 --- a/pallets/storage-provider/src/storage_provider.rs +++ b/pallets/storage-provider/src/storage_provider.rs @@ -7,7 +7,6 @@ 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}; @@ -17,7 +16,6 @@ use crate::{ error::GeneralPalletError, partition::TerminationResult, sector::{SectorOnChainInfo, SectorPreCommitOnChainInfo, MAX_SECTORS}, - Config, }; const LOG_TARGET: &'static str = "runtime::storage_provider::storage_provider"; @@ -103,7 +101,7 @@ 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( &mut self, current_block: BlockNumber, w_post_period_deadlines: u64, @@ -111,12 +109,8 @@ where w_post_challenge_window: BlockNumber, w_post_challenge_lookback: BlockNumber, fault_declaration_cutoff: BlockNumber, - ) -> Result<(), GeneralPalletError> - where - C: Config, - BlockNumberFor: sp_runtime::traits::BlockNumber, - { - let dl_info = self.deadline_info::( + ) -> Result<(), GeneralPalletError> { + let dl_info = self.deadline_info( current_block, w_post_period_deadlines, w_post_proving_period, @@ -220,7 +214,7 @@ where /// /// Reference: /// * - pub fn assign_sectors_to_deadlines( + pub fn assign_sectors_to_deadlines( &mut self, current_block: BlockNumber, mut sectors: BoundedVec, ConstU32>, @@ -231,11 +225,7 @@ where w_post_challenge_window: BlockNumber, w_post_challenge_lookback: BlockNumber, fault_declaration_cutoff: BlockNumber, - ) -> Result<(), GeneralPalletError> - where - C: Config, - BlockNumberFor: sp_runtime::traits::BlockNumber, - { + ) -> Result<(), GeneralPalletError> { sectors.sort_by_key(|info| info.sector_number); log::debug!(target: LOG_TARGET, @@ -310,7 +300,7 @@ 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( &self, current_block: BlockNumber, w_post_period_deadlines: u64, @@ -318,11 +308,7 @@ where w_post_challenge_window: BlockNumber, w_post_challenge_lookback: BlockNumber, fault_declaration_cutoff: BlockNumber, - ) -> Result, GeneralPalletError> - where - C: Config, - BlockNumberFor: sp_runtime::traits::BlockNumber, - { + ) -> Result, GeneralPalletError> { let current_deadline_index = self.current_deadline; DeadlineInfo::new(