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

Add simple collator election mechanism #2960

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions pallets/collator-selection/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ scale-info = { version = "2.9.0", default-features = false, features = ["derive"
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "master" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "master" }
sp-staking = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "master" }
frame-election-provider-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "master" }
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "master" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-authorship = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "master" }
Expand All @@ -35,13 +36,15 @@ sp-tracing = { git = "https://github.com/paritytech/substrate", branch = "master
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-timestamp = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-consensus-aura = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-bags-list = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-aura = { git = "https://github.com/paritytech/substrate", branch = "master" }

[features]
default = ["std"]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-election-provider-support/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
Expand All @@ -53,6 +56,7 @@ std = [
"sp-runtime/std",
"sp-staking/std",
"sp-std/std",
"frame-election-provider-support/std",
"frame-support/std",
"frame-system/std",
"frame-benchmarking/std",
Expand Down
99 changes: 98 additions & 1 deletion pallets/collator-selection/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ fn register_candidates<T: Config>(count: u32) {
assert!(<CandidacyBond<T>>::get() > 0u32.into(), "Bond cannot be zero!");

for who in candidates {
T::Currency::make_free_balance_be(&who, <CandidacyBond<T>>::get() * 2u32.into());
// TODO[GMP] revisit this, need it for Currency reserve in increase_bid
T::Currency::make_free_balance_be(&who, <CandidacyBond<T>>::get() * 3u32.into());
<CollatorSelection<T>>::register_as_candidate(RawOrigin::Signed(who).into()).unwrap();
}
}
Expand Down Expand Up @@ -238,6 +239,73 @@ mod benchmarks {
Ok(())
}

#[benchmark]
fn increase_bond(
c: Linear<{ min_candidates::<T>() + 1 }, { T::MaxCandidates::get() }>,
) -> Result<(), BenchmarkError> {
<CandidacyBond<T>>::put(T::Currency::minimum_balance());
<DesiredCandidates<T>>::put(c);

register_validators::<T>(c);
register_candidates::<T>(c);

let caller = <Candidates<T>>::get().last().unwrap().who.clone();
v2::whitelist!(caller);

let bond_amount: BalanceOf<T> = T::Currency::minimum_balance();

#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), bond_amount);

assert_last_event::<T>(
Event::CandidateBondIncreased {
account_id: caller,
deposit: T::Currency::minimum_balance() + bond_amount,
}
.into(),
);
assert!(
<Candidates<T>>::get().last().unwrap().deposit ==
T::Currency::minimum_balance() * 2u32.into()
);
Ok(())
}

#[benchmark]
fn decrease_bond(
c: Linear<{ min_candidates::<T>() + 1 }, { T::MaxCandidates::get() }>,
) -> Result<(), BenchmarkError> {
<CandidacyBond<T>>::put(T::Currency::minimum_balance());
<DesiredCandidates<T>>::put(c);

register_validators::<T>(c);
register_candidates::<T>(c);

let caller = <Candidates<T>>::get().last().unwrap().who.clone();
v2::whitelist!(caller);

<CollatorSelection<T>>::increase_bond(
RawOrigin::Signed(caller.clone()).into(),
<CandidacyBond<T>>::get(),
)
.unwrap();

let bond_amount: BalanceOf<T> = T::Currency::minimum_balance();

#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), bond_amount);

assert_last_event::<T>(
Event::CandidateBondDecreased {
account_id: caller,
deposit: T::Currency::minimum_balance(),
}
.into(),
);
assert!(<Candidates<T>>::get().last().unwrap().deposit == T::Currency::minimum_balance());
Ok(())
}

// worse case is when we have all the max-candidate slots filled except one, and we fill that
// one.
#[benchmark]
Expand Down Expand Up @@ -267,6 +335,35 @@ mod benchmarks {
);
}

#[benchmark]
fn take_candidate_slot(c: Linear<{ min_candidates::<T>() + 1 }, { T::MaxCandidates::get() }>) {
<CandidacyBond<T>>::put(T::Currency::minimum_balance());
<DesiredCandidates<T>>::put(1);

register_validators::<T>(c);
register_candidates::<T>(c);

let caller: T::AccountId = whitelisted_caller();
let bond: BalanceOf<T> = T::Currency::minimum_balance() * 10u32.into();
T::Currency::make_free_balance_be(&caller, bond);

<session::Pallet<T>>::set_keys(
RawOrigin::Signed(caller.clone()).into(),
keys::<T>(c + 1),
Vec::new(),
)
.unwrap();

let target = <Candidates<T>>::get().last().unwrap().who.clone();

#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), bond / 2u32.into(), target);

assert_last_event::<T>(
Event::CandidateAdded { account_id: caller, deposit: bond / 2u32.into() }.into(),
);
}

// worse case is the last candidate leaving.
#[benchmark]
fn leave_intent(c: Linear<{ min_candidates::<T>() + 1 }, { T::MaxCandidates::get() }>) {
Expand Down
Loading
Loading