Skip to content

Commit

Permalink
Part.2
Browse files Browse the repository at this point in the history
  • Loading branch information
AurevoirXavier committed Oct 17, 2024
1 parent fb26050 commit 48443c5
Show file tree
Hide file tree
Showing 7 changed files with 311 additions and 1,001 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions pallet/staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ scale-info = { workspace = true }
darwinia-ethtx-forwarder = { workspace = true }
dc-inflation = { workspace = true }
dc-types = { workspace = true }
# darwinia optional
darwinia-deposit = { workspace = true, optional = true }

# frontier
fp-evm = { workspace = true }
Expand All @@ -39,9 +37,6 @@ frame-benchmarking = { workspace = true, optional = true }
pretty_env_logger = { version = "0.5" }
serde = { version = "1.0", features = ["derive"] }

# darwinia
darwinia-deposit = { workspace = true, features = ["std"] }

# polkadot-sdk
pallet-balances = { workspace = true, features = ["std"] }
pallet-session = { workspace = true, features = ["std"] }
Expand All @@ -61,8 +56,6 @@ std = [
# darwinia
"darwinia-ethtx-forwarder/std",
"dc-inflation/std",
# darwinia optional
"darwinia-deposit?/std",

# frontier
"fp-evm/std",
Expand All @@ -82,7 +75,6 @@ std = [

runtime-benchmarks = [
# darwinia
"darwinia-deposit",
"darwinia-ethtx-forwarder/runtime-benchmarks",

# polkadot-sdk
Expand Down
99 changes: 5 additions & 94 deletions pallet/staking/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

// darwinia
use crate::*;
use darwinia_deposit::SimpleAsset;
use dc_types::UNIT;
// polkadot-sdk
use frame_benchmarking::v2;
Expand All @@ -32,108 +31,20 @@ mod benchmarks {
// polkadot-sdk
use frame_support::traits::Currency;

fn deposit_for<T>(who: &T::AccountId, count: u32) -> Vec<DepositId<T>>
where
T: Config + darwinia_deposit::Config,
{
(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(),
UNIT,
1,
)
.unwrap();

x.into()
})
.collect()
}

#[benchmark]
fn stake(x: Linear<0, 1_023>) {
fn unstake_all_for() {
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);

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

#[benchmark]
fn unstake(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();

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

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

// Worst-case scenario:
//
// None.
#[extrinsic_call]
_(RawOrigin::Signed(a), Default::default());
}

#[benchmark]
fn nominate() {
let a = frame_benchmarking::whitelisted_caller::<T::AccountId>();
let a_cloned = a.clone();

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

<Pallet<T>>::stake(RawOrigin::Signed(a.clone()).into(), UNIT, Default::default()).unwrap();
<Pallet<T>>::collect(RawOrigin::Signed(a.clone()).into(), Default::default()).unwrap();

// Worst-case scenario:
//
// Nominate the target collator successfully.
// The total number of deposit items has reached `darwinia_deposits::Config::MaxDeposits`.
#[extrinsic_call]
_(RawOrigin::Signed(a), a_cloned);
}

#[benchmark]
fn chill() {
let a = frame_benchmarking::whitelisted_caller::<T::AccountId>();

<T as darwinia_deposit::Config>::Ring::make_free_balance_be(&a, UNIT);

<Pallet<T>>::stake(RawOrigin::Signed(a.clone()).into(), UNIT, Default::default()).unwrap();
<Pallet<T>>::collect(RawOrigin::Signed(a.clone()).into(), Default::default()).unwrap();
<Pallet<T>>::nominate(RawOrigin::Signed(a.clone()).into(), a.clone()).unwrap();

// Worst-case scenario:
//
// Collect and nominate at the same time.
#[extrinsic_call]
_(RawOrigin::Signed(a));
}

#[benchmark]
fn payout() {
fn allocate_ring_staking_reward_of() {
let a = frame_benchmarking::whitelisted_caller::<T::AccountId>();
let a_cloned = a.clone();

Expand All @@ -142,12 +53,12 @@ mod benchmarks {
}

#[benchmark]
fn set_rate_limit() {
fn set_ring_staking_contract() {
// Worst-case scenario:
//
// Set successfully.
#[extrinsic_call]
_(RawOrigin::Root, 1);
_(RawOrigin::Root, frame_benchmarking::whitelisted_caller::<T::AccountId>());
}

#[benchmark]
Expand Down
Loading

0 comments on commit 48443c5

Please sign in to comment.