Skip to content

Commit

Permalink
feat: add new extrinsic (#391)
Browse files Browse the repository at this point in the history
  • Loading branch information
ytqaljn authored Aug 12, 2024
1 parent d89e54e commit c742eb4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
51 changes: 51 additions & 0 deletions pallets/sminer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ pub mod pallet {
miner: AccountOf<T>,
space: u128,
},
DecreaseDeclarationSpace {
miner: AccountOf<T>,
space: u128,
},
}

/// Error for the sminer pallet.
Expand Down Expand Up @@ -262,6 +266,8 @@ pub mod pallet {
WrongOrigin,
/// Exceeding the maximum release volume of the faucet in one day
ExceedRelease,
/// The reduced declared space is smaller than the currently certified space
UnableReduceDeclaration,
}

/// The hashmap for info of storage miners.
Expand Down Expand Up @@ -608,6 +614,51 @@ pub mod pallet {
Ok(())
}

#[pallet::call_index(4)]
#[transactional]
#[pallet::weight(<T as pallet::Config>::WeightInfo::increase_collateral())]
pub fn decrease_declaration_space(origin: OriginFor<T>, tib_count: u32) -> DispatchResult {
let sender = ensure_signed(origin)?;

ensure!(MinerItems::<T>::contains_key(&sender), Error::<T>::NotMiner);
let decrease_space = T_BYTE.checked_mul(tib_count as u128).ok_or(Error::<T>::Overflow)?;

<MinerItems<T>>::try_mutate(&sender, |miner_info_opt| -> DispatchResult {
let miner_info = miner_info_opt.as_mut().ok_or(Error::<T>::ConversionError)?;

ensure!(
miner_info.state.to_vec() == STATE_POSITIVE.as_bytes().to_vec() || miner_info.state.to_vec() == STATE_FROZEN.as_bytes().to_vec(),
Error::<T>::StateError
);

let estimate_space = miner_info
.declaration_space
.checked_sub(decrease_space)
.ok_or(Error::<T>::Overflow)?;

ensure!(
estimate_space >= miner_info.idle_space + miner_info.service_space + miner_info.lock_space,
Error::<T>::UnableReduceDeclaration
);

miner_info.declaration_space = estimate_space;

let base_limit: BalanceOf<T> = Self::calculate_limit_by_space(miner_info.declaration_space)?
.try_into()
.map_err(|_| Error::<T>::Overflow)?;

if base_limit <= miner_info.collaterals {
miner_info.state = Self::str_to_bound(STATE_POSITIVE)?;
}

Ok(())
})?;

Self::deposit_event(Event::<T>::DecreaseDeclarationSpace { miner: sender, space: decrease_space });

Ok(())
}

/// Receive Miner's Reward
///
/// This function allows a registered Miner in a positive state to receive their available rewards from the
Expand Down
2 changes: 1 addition & 1 deletion standalone/chain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 115,
spec_version: 116,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down

0 comments on commit c742eb4

Please sign in to comment.