Skip to content

Commit

Permalink
Remove unstake duration
Browse files Browse the repository at this point in the history
  • Loading branch information
aurexav committed Apr 15, 2024
1 parent 1e5761b commit 0bf4278
Show file tree
Hide file tree
Showing 20 changed files with 131 additions and 751 deletions.
3 changes: 1 addition & 2 deletions pallet/account-migration/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ mod benchmarks {
Default::default(),
Default::default()
);
<T as darwinia_staking::Config>::MaxUnstakings::get()
as usize
16
]),
..Default::default()
},
Expand Down
37 changes: 11 additions & 26 deletions pallet/account-migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,16 @@ pub mod pallet {

migration::put_storage_value(b"Identity", b"Registrars", &[], rs);
}
if let Some(mut l) = <Ledgers<T>>::take(from) {
if let Some(l) = <Ledgers<T>>::take(from) {
if l.staked_ring > 0 {
<pallet_balances::Pallet<T> as Currency<_>>::transfer(
to,
&darwinia_staking::account_id(),
l.staked_ring,
AllowDeath,
)?;
}

if let Some(ds) = <Deposits<T>>::take(from) {
<pallet_balances::Pallet<T> as Currency<_>>::transfer(
to,
Expand All @@ -477,33 +486,9 @@ pub mod pallet {
);
}

let now = <frame_system::Pallet<T>>::block_number();

l.unstaking_ring.retain(|(_, t)| t > &now);

let staking_pot = darwinia_staking::account_id();
let r = l.staked_ring + l.unstaking_ring.iter().map(|(r, _)| r).sum::<Balance>();

// To calculated the worst case in benchmark.
debug_assert!(r > 0);

if r > 0 {
<pallet_balances::Pallet<T> as Currency<_>>::transfer(
to,
&staking_pot,
r,
AllowDeath,
)?;
}

<darwinia_staking::Ledgers<T>>::insert(
to,
Ledger {
staked_ring: l.staked_ring,
staked_deposits: l.staked_deposits,
unstaking_ring: l.unstaking_ring,
unstaking_deposits: l.unstaking_deposits,
},
Ledger { ring: l.staked_ring, deposits: l.staked_deposits },
);
}

Expand Down
2 changes: 0 additions & 2 deletions pallet/account-migration/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,6 @@ impl darwinia_staking::Config for Runtime {
type KtonRewardDistributionContract = ();
type KtonStakerNotifier = ();
type MaxDeposits = ();
type MaxUnstakings = ();
type MinStakingDuration = ();
type Ring = Dummy;
type RuntimeEvent = RuntimeEvent;
type ShouldEndSession = ();
Expand Down
55 changes: 3 additions & 52 deletions pallet/staking/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ mod benchmarks {
where
T: Config + darwinia_deposit::Config,
{
(0..count.min(<<T as Config>::MaxUnstakings>::get()) as u8)
(0..count.min(<<T as darwinia_deposit::Config>::MaxDeposits>::get()) as u16)
.map(|x| {
<darwinia_deposit::Pallet<T>>::lock(
RawOrigin::Signed(who.to_owned()).into(),
Expand All @@ -62,7 +62,7 @@ mod benchmarks {

// Worst-case scenario:
//
// The total number of deposit items has reached `Config::MaxUnstakings`.
// The total number of deposit items has reached `darwinia_deposits::Config::MaxDeposits`.
#[extrinsic_call]
_(RawOrigin::Signed(a), UNIT, deposits);
}
Expand All @@ -81,60 +81,11 @@ mod benchmarks {

// Worst-case scenario:
//
// The total number of deposit items has reached `Config::MaxUnstakings`.
// The total number of deposit items has reached `darwinia_deposits::Config::MaxDeposits`.
#[extrinsic_call]
_(RawOrigin::Signed(a), UNIT, deposits);
}

#[benchmark]
fn restake(x: Linear<0, 1_023>) {
let a = frame_benchmarking::whitelisted_caller();

// Remove `+ 1` after https://github.com/paritytech/substrate/pull/13655.
<T as darwinia_deposit::Config>::Ring::make_free_balance_be(&a, 1_024 * UNIT + 1);
<T as darwinia_deposit::Config>::Kton::mint(&a, UNIT).unwrap();

let deposits = deposit_for::<T>(&a, x);

<Pallet<T>>::stake(RawOrigin::Signed(a.clone()).into(), UNIT, deposits.clone()).unwrap();
<Pallet<T>>::unstake(RawOrigin::Signed(a.clone()).into(), UNIT, deposits.clone()).unwrap();

// Worst-case scenario:
//
// The total number of deposit items has reached `Config::MaxUnstakings`.
#[extrinsic_call]
_(RawOrigin::Signed(a), UNIT, deposits);
}

#[benchmark]
fn claim() {
let a = frame_benchmarking::whitelisted_caller();

// Remove `+ 1` after https://github.com/paritytech/substrate/pull/13655.
<T as darwinia_deposit::Config>::Ring::make_free_balance_be(&a, 1_024 * UNIT + 1);
<T as darwinia_deposit::Config>::Kton::mint(&a, UNIT).unwrap();

let deposits = deposit_for::<T>(&a, <T as Config>::MaxUnstakings::get());

<Pallet<T>>::stake(RawOrigin::Signed(a.clone()).into(), UNIT, deposits.clone()).unwrap();
<Pallet<T>>::unstake(RawOrigin::Signed(a.clone()).into(), UNIT, deposits).unwrap();

<frame_system::Pallet<T>>::set_block_number(
<frame_system::Pallet<T>>::block_number() + T::MinStakingDuration::get(),
);

assert!(<Ledgers<T>>::contains_key(&a));

// Worst-case scenario:
//
// The total number of deposit items has reached `Config::MaxUnstakings`.
// In addition, all `RING` and `KTON` should be claimed, to make ledger get killed.
#[extrinsic_call]
_(RawOrigin::Signed(a.clone()));

assert!(!<Ledgers<T>>::contains_key(&a));
}

#[benchmark]
fn collect() {
let a = frame_benchmarking::whitelisted_caller();
Expand Down
Loading

0 comments on commit 0bf4278

Please sign in to comment.