Skip to content

Commit

Permalink
fix: fix the bug where replacing space records still exists after the…
Browse files Browse the repository at this point in the history
… storage node exits (#274)
  • Loading branch information
ytqaljn authored Dec 14, 2023
1 parent b543038 commit 9a9d4de
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 42 deletions.
1 change: 1 addition & 0 deletions pallets/file-bank/src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub(super) const ONCE_MAX_CLEAR_FILE: u32 = 300;
1 change: 1 addition & 0 deletions pallets/file-bank/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ impl<T: Config> Pallet<T> {
let file_info =
UserFileSliceInfo { 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)?;
Ok(())
})?;
Expand Down
10 changes: 2 additions & 8 deletions pallets/file-bank/src/impls/dealimpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,10 @@ impl<T: Config> DealInfo<T> {

pub fn completed_all(&mut self) -> DispatchResult {
for complete_info in self.complete_list.iter() {
<PendingReplacements<T>>::try_mutate(&complete_info.miner, |pending_space| -> DispatchResult {
let replace_space = FRAGMENT_SIZE
let replace_space = FRAGMENT_SIZE
.checked_mul(self.segment_list.len() as u128)
.ok_or(Error::<T>::Overflow)?;

*pending_space = pending_space
.checked_add(replace_space).ok_or(Error::<T>::Overflow)?;

Ok(())
})?;
T::MinerControl::increase_replace_space(&complete_info.miner, replace_space)?;
}

Ok(())
Expand Down
44 changes: 11 additions & 33 deletions pallets/file-bank/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub use types::*;
mod functions;

mod constants;
use constants::*;

mod impls;
use impls::receptionist::Receptionist;
Expand Down Expand Up @@ -112,13 +113,13 @@ pub mod pallet {
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
// Find the consensus of the current block
type FindAuthor: FindAuthor<Self::AccountId>;
//Used to find out whether the schedule exists
// Used to find out whether the schedule exists
type TeeWorkerHandler: TeeWorkerHandler<Self::AccountId>;
//It is used to control the computing power and space of miners
// It is used to control the computing power and space of miners
type MinerControl: MinerControl<Self::AccountId, BlockNumberFor<Self>>;
//Interface that can generate random seeds
// Interface that can generate random seeds
type MyRandomness: Randomness<Option<Self::Hash>, BlockNumberFor<Self>>;

type StorageHandle: StorageHandle<Self::AccountId>;
Expand Down Expand Up @@ -261,8 +262,6 @@ pub mod pallet {

VerifyTeeSigFailed,

InsufficientReplaceable,

TeeNoPermission,
}

Expand All @@ -285,10 +284,6 @@ pub mod pallet {
ValueQuery,
>;

#[pallet::storage]
#[pallet::getter(fn pending_replacements)]
pub(super) type PendingReplacements<T: Config> = StorageMap<_, Blake2_128Concat, AccountOf<T>, u128, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn miner_lock)]
pub(super) type MinerLock<T: Config> =
Expand Down Expand Up @@ -359,8 +354,8 @@ pub mod pallet {
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() {
count = count.checked_add(1).unwrap_or(300);
if count == 300 {
count = count.checked_add(1).unwrap_or(ONCE_MAX_CLEAR_FILE);
if count == ONCE_MAX_CLEAR_FILE {
<UserHoldFileList<T>>::insert(&acc, file_info_list);
return weight;
}
Expand Down Expand Up @@ -637,22 +632,9 @@ pub mod pallet {
idle_sig_info.rear,
tee_sig,
)?;

<PendingReplacements<T>>::try_mutate(&sender, |pending_space| -> DispatchResult {
if *pending_space / IDLE_SEG_SIZE == 0 {
Err(Error::<T>::InsufficientReplaceable)?;
}
let replace_space = IDLE_SEG_SIZE.checked_mul(count.into()).ok_or(Error::<T>::Overflow)?;
log::info!("pending_space: {:?}, replace_space: {:?}, count: {:?}", pending_space, replace_space, count);
let pending_space_temp = pending_space.checked_sub(replace_space).ok_or(Error::<T>::Overflow)?;
*pending_space = pending_space_temp;

Self::deposit_event(Event::<T>::ReplaceIdleSpace { acc: sender.clone(), space: replace_space });

Ok(())
})?;


let replace_space = IDLE_SEG_SIZE.checked_mul(count.into()).ok_or(Error::<T>::Overflow)?;
T::MinerControl::decrease_replace_space(&sender, replace_space)?;
Self::deposit_event(Event::<T>::ReplaceIdleSpace { acc: sender.clone(), space: replace_space });

Ok(())
}
Expand Down Expand Up @@ -1020,11 +1002,7 @@ pub mod pallet {

// TODO!
T::MinerControl::delete_idle_update_space(&sender, FRAGMENT_SIZE)?;
<PendingReplacements<T>>::try_mutate(&sender, |pending_space| -> DispatchResult {
let pending_space_temp = pending_space.checked_add(FRAGMENT_SIZE).ok_or(Error::<T>::Overflow)?;
*pending_space = pending_space_temp;
Ok(())
})?;
T::MinerControl::increase_replace_space(&sender, FRAGMENT_SIZE)?;

if T::MinerControl::restoral_target_is_exist(&fragment.miner) {
T::MinerControl::update_restoral_target(&fragment.miner, FRAGMENT_SIZE)?;
Expand Down
3 changes: 2 additions & 1 deletion pallets/sminer/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ impl<T: Config> Pallet<T> {
let encoding = space_proof_info.pois_key.encode();
let hashing = sp_io::hashing::sha2_256(&encoding);
MinerPublicKey::<T>::remove(hashing);
<MinerItems<T>>::remove(miner);
<MinerItems<T>>::remove(miner.clone());
<PendingReplacements<T>>::remove(miner);

Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions pallets/sminer/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ impl<T: Config> Pallet<T> {
})?;

<RewardMap<T>>::remove(acc);
<PendingReplacements<T>>::remove(miner);

Ok(())
}
Expand Down
32 changes: 32 additions & 0 deletions pallets/sminer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ pub mod pallet {
InsufficientStakingPeriod,

ExceedingDeclarationSpace,

InsufficientReplaceable,
}

/// The hashmap for info of storage miners.
Expand Down Expand Up @@ -287,6 +289,12 @@ pub mod pallet {
pub(super) type StakingStartBlock<T: Config> =
StorageMap<_, Blake2_128Concat, AccountOf<T>, BlockNumberFor<T>>;


#[pallet::storage]
#[pallet::getter(fn pending_replacements)]
pub(super) type PendingReplacements<T: Config> =
StorageMap<_, Blake2_128Concat, AccountOf<T>, u128, ValueQuery>;

#[pallet::pallet]
pub struct Pallet<T>(_);

Expand Down Expand Up @@ -989,6 +997,9 @@ pub trait MinerControl<AccountId, BlockNumber> {
fn update_miner_state(miner: &AccountId, state: &str) -> DispatchResult;
fn get_expenders() -> Result<(u64, u64, u64), DispatchError>;
fn get_miner_snapshot(miner: &AccountId) -> Result<(u128, u128, BloomFilter, SpaceProofInfo<AccountId>, TeeRsaSignature), DispatchError>;

fn increase_replace_space(miner: &AccountId, space: u128) -> DispatchResult;
fn decrease_replace_space(miner: &AccountId, space: u128) -> DispatchResult;
}

impl<T: Config> MinerControl<<T as frame_system::Config>::AccountId, BlockNumberFor<T>> for Pallet<T> {
Expand Down Expand Up @@ -1214,4 +1225,25 @@ impl<T: Config> MinerControl<<T as frame_system::Config>::AccountId, BlockNumber
fn restoral_target_is_exist(miner: &AccountOf<T>) -> bool {
RestoralTarget::<T>::contains_key(miner)
}

fn increase_replace_space(miner: &AccountOf<T>, space: u128) -> DispatchResult {
<PendingReplacements<T>>::try_mutate(&miner, |pending_space| -> DispatchResult {
*pending_space = pending_space
.checked_add(space).ok_or(Error::<T>::Overflow)?;
Ok(())
})
}

fn decrease_replace_space(miner: &AccountOf<T>, space: u128) -> DispatchResult {
<PendingReplacements<T>>::try_mutate(&miner, |pending_space| -> DispatchResult {
if *pending_space / IDLE_SEG_SIZE == 0 {
Err(Error::<T>::InsufficientReplaceable)?;
}

*pending_space = pending_space
.checked_sub(space).ok_or(Error::<T>::Overflow)?;

Ok(())
})
}
}

0 comments on commit 9a9d4de

Please sign in to comment.