Skip to content
This repository has been archived by the owner on Mar 13, 2023. It is now read-only.

KTON Slashes Test #541

Closed
aurexav opened this issue Mar 16, 2021 · 0 comments · Fixed by #552
Closed

KTON Slashes Test #541

aurexav opened this issue Mar 16, 2021 · 0 comments · Fixed by #552
Assignees

Comments

@aurexav
Copy link
Member

aurexav commented Mar 16, 2021

[darwinia-common] frame/staking/src/lib.rs (Lines 1619-1695)


#[weight = 50 * WEIGHT_PER_MICROS + T::DbWeight::get().reads_writes(3, 2)]
		fn try_claim_deposits_with_punish(origin, expire_time: TsInMs) {
			let controller = ensure_signed(origin)?;
			let mut ledger = Self::ledger(&controller).ok_or(<Error<T>>::NotController)?;
			let now = T::UnixTime::now().as_millis().saturated_into::<TsInMs>();

			if expire_time <= now {
				return Ok(());
			}

			let mut claim_deposits_with_punish = (false, Zero::zero());

			{
				let StakingLedger {
					stash,
					active_deposit_ring,
					deposit_items,
					..
				} = &mut ledger;

				deposit_items.retain(|item| {
					if item.expire_time != expire_time {
						return true;
					}

					let kton_slash = {
						let plan_duration_in_months = {
							let plan_duration_in_milliseconds =
								item.expire_time.saturating_sub(item.start_time);

							plan_duration_in_milliseconds / MONTH_IN_MILLISECONDS
						};
						let passed_duration_in_months = {
							let passed_duration_in_milliseconds =
								now.saturating_sub(item.start_time);

							passed_duration_in_milliseconds / MONTH_IN_MILLISECONDS
						};

						(
							inflation::compute_kton_reward::<T>(
								item.value, plan_duration_in_months as _
							)
							-
							inflation::compute_kton_reward::<T>(
								item.value, passed_duration_in_months as _
							)
						).max(1u32.into()) * 3u32.into()
					};

					// check total free balance and locked one
					// strict on punishing in kton
					if T::KtonCurrency::usable_balance(stash) >= kton_slash {
						*active_deposit_ring = active_deposit_ring.saturating_sub(item.value);

						let imbalance = T::KtonCurrency::slash(stash, kton_slash).0;
						T::KtonSlash::on_unbalanced(imbalance);

						claim_deposits_with_punish = (true, kton_slash);

						false
					} else {
						true
					}
				});
			}

			<Ledger<T>>::insert(&controller, &ledger);

			if claim_deposits_with_punish.0 {
				Self::deposit_event(
					RawEvent::DepositsClaimedWithPunish(
						ledger.stash.clone(),
						claim_deposits_with_punish.1,
					));
			}
		}

Open in IDE · Open on GitHub

Created from VS Code using CodeStream

aurexav added a commit that referenced this issue Mar 20, 2021
* test: #541 add test for try_claim_deposits_with_punish and inflation

* optimize kton slash test

* update format

* remove deadcode

Co-authored-by: Xavier Lau <c.estlavie@icloud.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants