Skip to content

Commit

Permalink
feat: file for territory version
Browse files Browse the repository at this point in the history
  • Loading branch information
ytqaljn committed Jun 18, 2024
1 parent f7a96f9 commit 2b76c0a
Show file tree
Hide file tree
Showing 13 changed files with 260 additions and 277 deletions.
2 changes: 1 addition & 1 deletion pallets/cess-treasury/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use frame_support::{
// use sp_std::prelude::*;
use sp_runtime::{
SaturatedConversion, Perbill,
traits::{CheckedAdd, CheckedSub, CheckedDiv, AccountIdConversion},
traits::{CheckedAdd, CheckedSub, AccountIdConversion},
};
use frame_system::{
pallet_prelude::OriginFor,
Expand Down
8 changes: 6 additions & 2 deletions pallets/file-bank/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,13 @@ impl<T: Config> Pallet<T> {
user: &AccountOf<T>,
file_hash: Hash,
file_size: u128,
territory_name: TerrName,
) -> DispatchResult {
let file_info =
UserFileSliceInfo { file_hash: file_hash, file_size };
let file_info = UserFileSliceInfo {
territory_name,
file_hash: file_hash,
file_size,
};
<UserHoldFileList<T>>::try_mutate(user, |v| -> DispatchResult {
ensure!(!v.contains(&file_info), Error::<T>::Existed);
v.try_push(file_info).map_err(|_| Error::<T>::StorageLimitReached)?;
Expand Down
3 changes: 0 additions & 3 deletions pallets/file-bank/src/impls/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
pub mod receptionist;
pub use receptionist::*;

pub mod dealimpl;
pub use dealimpl::*;
4 changes: 2 additions & 2 deletions pallets/file-bank/src/impls/receptionist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl<T: Config> Receptionist<T> {
Pallet::<T>::create_bucket_helper(&user_brief.user, &user_brief.bucket_name, Some(file_hash))?;
}

Pallet::<T>::add_user_hold_fileslice(&user_brief.user, file_hash, needed_space)?;
Pallet::<T>::add_user_hold_fileslice(&user_brief.user, file_hash, needed_space, user_brief.territory_name.clone())?;
file.owner.try_push(user_brief.clone()).map_err(|_e| Error::<T>::BoundedVecError)?;

Ok(())
Expand Down Expand Up @@ -68,7 +68,7 @@ impl<T: Config> Receptionist<T> {
} else {
Pallet::<T>::create_bucket_helper(&deal_info.user.user, &deal_info.user.bucket_name, Some(deal_hash))?;
}
Pallet::<T>::add_user_hold_fileslice(&deal_info.user.user, deal_hash.clone(), needed_space)?;
Pallet::<T>::add_user_hold_fileslice(&deal_info.user.user, deal_hash.clone(), needed_space, deal_info.user.territory_name.clone())?;
<DealMap<T>>::remove(deal_hash);
Pallet::<T>::deposit_event(Event::<T>::StorageCompleted{ file_hash: deal_hash });
}
Expand Down
146 changes: 71 additions & 75 deletions pallets/file-bank/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use frame_support::traits::{
FindAuthor, Randomness,
StorageVersion,
schedule::{Anon as ScheduleAnon, Named as ScheduleNamed},
};
// use sc_network::Multiaddr;

Expand Down Expand Up @@ -64,7 +63,7 @@ use pallet_storage_handler::StorageHandle;
use cp_scheduler_credit::SchedulerCreditCounter;
use sp_runtime::{
traits::{
BlockNumberProvider, CheckedAdd, Dispatchable
BlockNumberProvider, CheckedAdd
},
RuntimeDebug, SaturatedConversion,
};
Expand Down Expand Up @@ -106,13 +105,6 @@ pub mod pallet {

type RuntimeCall: From<Call<Self>>;

type FScheduler: ScheduleNamed<BlockNumberFor<Self>, Self::SProposal, Self::SPalletsOrigin>;

type AScheduler: ScheduleAnon<BlockNumberFor<Self>, Self::SProposal, Self::SPalletsOrigin>;
/// Overarching type of all pallets origins.
type SPalletsOrigin: From<frame_system::RawOrigin<Self::AccountId>>;
/// The SProposal.
type SProposal: Parameter + Dispatchable<RuntimeOrigin = Self::RuntimeOrigin> + From<Call<Self>>;
// Find the consensus of the current block
type FindAuthor: FindAuthor<Self::AccountId>;
// Used to find out whether the schedule exists
Expand Down Expand Up @@ -313,7 +305,7 @@ pub mod pallet {
#[pallet::storage]
#[pallet::getter(fn clear_user_list)]
pub(super) type ClearUserList<T: Config> =
StorageValue<_, BoundedVec<AccountOf<T>, ConstU32<5000>>, ValueQuery>;
StorageValue<_, BoundedVec<(AccountOf<T>, TerrName), ConstU32<2000>>, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn task_failed_count)]
Expand All @@ -331,22 +323,25 @@ pub mod pallet {
let mut weight: Weight = Weight::zero();
// FOR TESTING
if now % days == 0u32.saturated_into() {
let (temp_weight, acc_list) = T::StorageHandle::frozen_task();
let (temp_weight, clear_list) = T::StorageHandle::frozen_task();
weight = weight.saturating_add(temp_weight);
let temp_acc_list: BoundedVec<AccountOf<T>, ConstU32<5000>> =
acc_list.try_into().unwrap_or_default();
let temp_acc_list: BoundedVec<(AccountOf<T>, TerrName), ConstU32<2000>> =
clear_list.try_into().unwrap_or_default();
ClearUserList::<T>::put(temp_acc_list);
weight = weight.saturating_add(T::DbWeight::get().writes(1));
}

let mut count: u32 = 0;
let acc_list = ClearUserList::<T>::get();
let clear_list = ClearUserList::<T>::get();
weight = weight.saturating_add(T::DbWeight::get().reads(1));
for acc in acc_list.iter() {
for (acc, territory_name) in clear_list.iter() {
// todo! Delete in blocks, and delete a part of each block
if let Ok(mut file_info_list) = <UserHoldFileList<T>>::try_get(&acc) {
weight = weight.saturating_add(T::DbWeight::get().reads(1));
while let Some(file_info) = file_info_list.pop() {
if file_info.territory_name != *territory_name {
continue;
}
count = count.checked_add(1).unwrap_or(ONCE_MAX_CLEAR_FILE);
if count == ONCE_MAX_CLEAR_FILE {
<UserHoldFileList<T>>::insert(&acc, file_info_list);
Expand All @@ -368,10 +363,16 @@ pub mod pallet {
weight = weight.saturating_add(temp_weight);
}
}

if let Err(e) = Self::bucket_remove_file(&file_info.file_hash, &acc, &file) {
log::error!("[FileBank]: space lease, delete file from bucket report a bug! {:?}", e);
}
} else {
log::error!("space lease, delete file bug!");
log::error!("acc: {:?}, file_hash: {:?}", &acc, &file_info.file_hash);
}


}

match T::StorageHandle::delete_user_space_storage(&acc) {
Expand All @@ -380,13 +381,8 @@ pub mod pallet {
}

ClearUserList::<T>::mutate(|target_list| {
target_list.retain(|temp_acc| temp_acc != acc);
target_list.retain(|temp_acc| temp_acc.0 != *acc);
});

<UserHoldFileList<T>>::remove(&acc);
// todo! clear all
let _ = <Bucket<T>>::clear_prefix(&acc, 100000, None);
<UserBucketList<T>>::remove(&acc);
}
}

Expand Down Expand Up @@ -454,60 +450,60 @@ pub mod pallet {
/// - `origin`: The origin of the transaction, representing the current owner of the file.
/// - `target_brief`: User brief information of the target user to whom ownership is being transferred.
/// - `file_hash`: The unique hash identifier of the file to be transferred
#[pallet::call_index(2)]
#[transactional]
/// FIX ME
#[pallet::weight(<T as pallet::Config>::WeightInfo::ownership_transfer())]
pub fn ownership_transfer(
origin: OriginFor<T>,
target_brief: UserBrief<T>,
file_hash: Hash,
) -> DispatchResult {
let sender = ensure_signed(origin)?;
let file = <File<T>>::try_get(&file_hash).map_err(|_| Error::<T>::FileNonExistent)?;
//If the file does not exist, false will also be returned
ensure!(Self::check_is_file_owner(&sender, &file_hash), Error::<T>::NotOwner);
ensure!(!Self::check_is_file_owner(&target_brief.user, &file_hash), Error::<T>::IsOwned);

ensure!(file.stat == FileState::Active, Error::<T>::Unprepared);
ensure!(<Bucket<T>>::contains_key(&target_brief.user, &target_brief.bucket_name), Error::<T>::NonExistent);
//Modify the space usage of target acc,
//and determine whether the space is enough to support transfer
let file_size = Self::cal_file_size(file.segment_list.len() as u128);
T::StorageHandle::add_territory_used_space(&target_brief.user, &target_brief.territory_name, file_size)?;
//Increase the ownership of the file for target acc
<File<T>>::try_mutate(&file_hash, |file_opt| -> DispatchResult {
let file = file_opt.as_mut().ok_or(Error::<T>::FileNonExistent)?;
file.owner.try_push(target_brief.clone()).map_err(|_| Error::<T>::BoundedVecError)?;
Ok(())
})?;
//Add files to the bucket of target acc
<Bucket<T>>::try_mutate(
&target_brief.user,
&target_brief.bucket_name,
|bucket_info_opt| -> DispatchResult {
let bucket_info = bucket_info_opt.as_mut().ok_or(Error::<T>::NonExistent)?;
bucket_info.object_list.try_push(file_hash.clone()).map_err(|_| Error::<T>::LengthExceedsLimit)?;
Ok(())
})?;
//Increase the corresponding space usage for target acc
Self::add_user_hold_fileslice(
&target_brief.user,
file_hash.clone(),
file_size,
)?;
//Clean up the file holding information of the original user
let file = <File<T>>::try_get(&file_hash).map_err(|_| Error::<T>::NonExistent)?;

let _ = Self::delete_user_file(&file_hash, &sender, &file)?;

Self::bucket_remove_file(&file_hash, &sender, &file)?;

Self::remove_user_hold_file_list(&file_hash, &sender)?;
// let _ = Self::clear_user_file(file_hash.clone(), &sender, true)?;

Ok(())
}
// #[pallet::call_index(2)]
// #[transactional]
// /// FIX ME
// #[pallet::weight(<T as pallet::Config>::WeightInfo::ownership_transfer())]
// pub fn ownership_transfer(
// origin: OriginFor<T>,
// target_brief: UserBrief<T>,
// file_hash: Hash,
// ) -> DispatchResult {
// let sender = ensure_signed(origin)?;
// let file = <File<T>>::try_get(&file_hash).map_err(|_| Error::<T>::FileNonExistent)?;
// //If the file does not exist, false will also be returned
// ensure!(Self::check_is_file_owner(&sender, &file_hash), Error::<T>::NotOwner);
// ensure!(!Self::check_is_file_owner(&target_brief.user, &file_hash), Error::<T>::IsOwned);

// ensure!(file.stat == FileState::Active, Error::<T>::Unprepared);
// ensure!(<Bucket<T>>::contains_key(&target_brief.user, &target_brief.bucket_name), Error::<T>::NonExistent);
// //Modify the space usage of target acc,
// //and determine whether the space is enough to support transfer
// let file_size = Self::cal_file_size(file.segment_list.len() as u128);
// T::StorageHandle::add_territory_used_space(&target_brief.user, &target_brief.territory_name, file_size)?;
// //Increase the ownership of the file for target acc
// <File<T>>::try_mutate(&file_hash, |file_opt| -> DispatchResult {
// let file = file_opt.as_mut().ok_or(Error::<T>::FileNonExistent)?;
// file.owner.try_push(target_brief.clone()).map_err(|_| Error::<T>::BoundedVecError)?;
// Ok(())
// })?;
// //Add files to the bucket of target acc
// <Bucket<T>>::try_mutate(
// &target_brief.user,
// &target_brief.bucket_name,
// |bucket_info_opt| -> DispatchResult {
// let bucket_info = bucket_info_opt.as_mut().ok_or(Error::<T>::NonExistent)?;
// bucket_info.object_list.try_push(file_hash.clone()).map_err(|_| Error::<T>::LengthExceedsLimit)?;
// Ok(())
// })?;
// //Increase the corresponding space usage for target acc
// Self::add_user_hold_fileslice(
// &target_brief.user,
// file_hash.clone(),
// file_size,
// )?;
// //Clean up the file holding information of the original user
// let file = <File<T>>::try_get(&file_hash).map_err(|_| Error::<T>::NonExistent)?;

// let _ = Self::delete_user_file(&file_hash, &sender, &file)?;

// Self::bucket_remove_file(&file_hash, &sender, &file)?;

// Self::remove_user_hold_file_list(&file_hash, &sender)?;
// // let _ = Self::clear_user_file(file_hash.clone(), &sender, true)?;

// Ok(())
// }

/// Transfer Report for a Storage Deal
///
Expand Down
1 change: 1 addition & 0 deletions pallets/file-bank/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub struct FragmentInfo<T: Config> {

#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, MaxEncodedLen, TypeInfo)]
pub struct UserFileSliceInfo {
pub(super) territory_name: TerrName,
pub(super) file_hash: Hash,
pub(super) file_size: u128,
}
Expand Down
2 changes: 0 additions & 2 deletions pallets/sminer/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@ impl<T: Config> Pallet<T> {
pub(super) fn calculate_miner_reward(
miner: &AccountOf<T>,
) -> DispatchResult {
let now = frame_system::Pallet::<T>::block_number();
let one_day = T::OneDayBlock::get();
let order_list = <CompleteMinerSnapShot<T>>::mutate(&miner, |snap_shot_list| -> Result<Vec<RewardOrder::<BalanceOf<T>, BlockNumberFor<T>>>, DispatchError> {
if snap_shot_list.len() == 0 {
return Ok(Default::default());
Expand Down
2 changes: 1 addition & 1 deletion pallets/sminer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use sp_runtime::traits::Zero;
use codec::{Decode, Encode};
use scale_info::TypeInfo;
use sp_runtime::{
traits::{AccountIdConversion, CheckedAdd, CheckedSub, CheckedMul, CheckedDiv, Dispatchable, SaturatedConversion},
traits::{AccountIdConversion, CheckedAdd, CheckedSub, CheckedMul, Dispatchable, SaturatedConversion},
RuntimeDebug, Perbill
};
use sp_staking::StakingInterface;
Expand Down
10 changes: 5 additions & 5 deletions pallets/staking/src/pallet/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ use frame_support::{
pallet_prelude::*,
traits::{
Currency, Defensive, DefensiveSaturating, EstimateNextNewSession, Get, Imbalance,
InspectLockableCurrency, Len, OnUnbalanced, TryCollect, UnixTime,
InspectLockableCurrency, Len, OnUnbalanced, TryCollect,
},
weights::Weight,
};
use frame_system::{pallet_prelude::BlockNumberFor, RawOrigin};
use pallet_session::historical;
use sp_runtime::{
traits::{Bounded, Convert, One, SaturatedConversion, Saturating, StaticLookup, Zero},
Perbill, Percent,
traits::{Bounded, Convert, One, Saturating, StaticLookup, Zero},
Perbill,
};
use sp_staking::{
currency_to_vote::CurrencyToVote,
Expand All @@ -49,7 +49,7 @@ use sp_std::prelude::*;

use crate::{
election_size_tracker::StaticTracker, log, slashing, weights::WeightInfo, ActiveEraInfo,
BalanceOf, EraInfo, EraPayout, Exposure, ExposureOf, Forcing, IndividualExposure,
BalanceOf, EraInfo, Exposure, ExposureOf, Forcing, IndividualExposure,
LedgerIntegrityState, MaxNominationsOf, MaxWinnersOf, Nominations, NominationsQuota,
PositiveImbalanceOf, RewardDestination, SessionInterface, StakingLedger, ValidatorPrefs,
};
Expand Down Expand Up @@ -533,7 +533,7 @@ impl<T: Config> Pallet<T> {
/// Compute payout for era.
fn end_era(active_era: ActiveEraInfo, _session_index: SessionIndex) {
// Note: active_era_start can be None if end era is called during genesis config.
if let Some(active_era_start) = active_era.start {
if let Some(_active_era_start) = active_era.start {
let (validator_payout, sminer_payout) = Self::rewards_in_era(active_era.index);

Self::deposit_event(Event::<T>::EraPaid {
Expand Down
25 changes: 2 additions & 23 deletions pallets/storage-handler/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ pub trait StorageHandle<AccountId> {
fn sub_total_service_space(decrement: u128) -> DispatchResult;
fn get_total_idle_space() -> u128;
fn get_total_service_space() -> u128;
fn add_purchased_space(size: u128) -> DispatchResult;
fn sub_purchased_space(size: u128) -> DispatchResult;
fn get_avail_space() -> Result<u128, DispatchError>;
fn lock_user_space(acc: &AccountId, name: &TerrName, needed_space: u128) -> DispatchResult;
fn unlock_user_space(acc: &AccountId, name: &TerrName, needed_space: u128) -> DispatchResult;
fn unlock_and_used_user_space(acc: &AccountId, name: &TerrName, needed_space: u128) -> DispatchResult;
fn get_user_avail_space(acc: &AccountId, name: &TerrName) -> Result<u128, DispatchError>;
fn frozen_task() -> (Weight, Vec<AccountId>);
fn frozen_task() -> (Weight, Vec<(AccountId, TerrName)>);
fn delete_user_space_storage(acc: &AccountId) -> Result<Weight, DispatchError>;
}

Expand Down Expand Up @@ -75,25 +73,6 @@ impl<T: Config> StorageHandle<T::AccountId> for Pallet<T> {
})
}

fn add_purchased_space(size: u128) -> DispatchResult {
<PurchasedSpace<T>>::try_mutate(|purchased_space| -> DispatchResult {
let total_space = <TotalIdleSpace<T>>::get().checked_add(<TotalServiceSpace<T>>::get()).ok_or(Error::<T>::Overflow)?;
let new_space = purchased_space.checked_add(size).ok_or(Error::<T>::Overflow)?;
if new_space > total_space {
Err(<Error<T>>::InsufficientAvailableSpace)?;
}
*purchased_space = new_space;
Ok(())
})
}

fn sub_purchased_space(size: u128) -> DispatchResult {
<PurchasedSpace<T>>::try_mutate(|purchased_space| -> DispatchResult {
*purchased_space = purchased_space.checked_sub(size).ok_or(Error::<T>::Overflow)?;
Ok(())
})
}

fn get_avail_space() -> Result<u128, DispatchError> {
let purchased_space = <PurchasedSpace<T>>::get();
let total_space = <TotalIdleSpace<T>>::get().checked_add(<TotalServiceSpace<T>>::get()).ok_or(Error::<T>::Overflow)?;
Expand Down Expand Up @@ -146,7 +125,7 @@ impl<T: Config> StorageHandle<T::AccountId> for Pallet<T> {
Ok(info.remaining_space)
}

fn frozen_task() -> (Weight, Vec<AccountOf<T>>) {
fn frozen_task() -> (Weight, Vec<(AccountOf<T>, TerrName)>) {
Self::frozen_task()
}

Expand Down
Loading

0 comments on commit 2b76c0a

Please sign in to comment.