Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement DIP-6 #1475

Merged
merged 6 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions node/src/chain_spec/crab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ pub fn genesis_config() -> ChainSpec {
darwinia_staking: DarwiniaStakingConfig {
now: SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis(),
elapsed_time: 0,
rate_limit: 20_000_000 * UNIT,
collator_count: 6,
collators: collators
.iter()
Expand Down Expand Up @@ -303,6 +304,7 @@ fn testnet_genesis(
darwinia_staking: DarwiniaStakingConfig {
now: SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis(),
elapsed_time: 0,
rate_limit: 20_000_000 * UNIT,
collator_count: collators.len() as _,
collators: collators.iter().map(|(a, _)| (a.to_owned(), UNIT)).collect(),
},
Expand Down
2 changes: 2 additions & 0 deletions node/src/chain_spec/darwinia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ pub fn genesis_config() -> ChainSpec {
darwinia_staking: DarwiniaStakingConfig {
now: SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis(),
elapsed_time: 0,
rate_limit: 20_000_000 * UNIT,
collator_count: 5,
collators: collators
.iter()
Expand Down Expand Up @@ -301,6 +302,7 @@ fn testnet_genesis(
darwinia_staking: DarwiniaStakingConfig {
now: SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis(),
elapsed_time: 0,
rate_limit: 20_000_000 * UNIT,
collator_count: collators.len() as _,
collators: collators.iter().map(|(a, _)| (a.to_owned(), UNIT)).collect(),
},
Expand Down
2 changes: 2 additions & 0 deletions node/src/chain_spec/pangolin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ pub fn genesis_config() -> ChainSpec {
darwinia_staking: DarwiniaStakingConfig {
now: SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis(),
elapsed_time: 0,
rate_limit: 20_000_000 * UNIT,
collator_count: 3,
collators: vec![
(array_bytes::hex_n_into_unchecked::<_, _, 20>(C1), UNIT),
Expand Down Expand Up @@ -291,6 +292,7 @@ fn testnet_genesis(
darwinia_staking: DarwiniaStakingConfig {
now: SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis(),
elapsed_time: 0,
rate_limit: 20_000_000 * UNIT,
collator_count: collators.len() as _,
collators: collators.iter().map(|(a, _)| (a.to_owned(), UNIT)).collect(),
},
Expand Down
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
64 changes: 12 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 Expand Up @@ -207,6 +158,15 @@ mod benchmarks {
_(RawOrigin::Signed(sender), a);
}

#[benchmark]
fn set_rate_limit() {
// Worst-case scenario:
//
// Set max unstake ring successfully.
#[extrinsic_call]
_(RawOrigin::Root, 1);
}

#[benchmark]
fn set_collator_count() {
// Worst-case scenario:
Expand Down
Loading