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

feat: fork collator-selection and use escrow for bonding #688

Merged
merged 3 commits into from Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 15 additions & 15 deletions crates/collator-selection/src/benchmarking.rs
Expand Up @@ -30,7 +30,7 @@ use pallet_authorship::EventHandler;
use pallet_session::{self as session, SessionManager};
use sp_std::prelude::*;

pub type BalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
pub type BalanceOf<T> = <<T as Config>::StakingCurrency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

const SEED: u32 = 0;

Expand All @@ -51,8 +51,8 @@ fn assert_last_event<T: Config>(generic_event: <T as Config>::Event) {

fn create_funded_user<T: Config>(string: &'static str, n: u32, balance_factor: u32) -> T::AccountId {
let user = account(string, n, SEED);
let balance = T::Currency::minimum_balance() * balance_factor.into();
let _ = T::Currency::make_free_balance_be(&user, balance);
let balance = T::StakingCurrency::minimum_balance() * balance_factor.into();
let _ = T::StakingCurrency::make_free_balance_be(&user, balance);
user
}

Expand Down Expand Up @@ -92,7 +92,7 @@ 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());
T::StakingCurrency::make_free_balance_be(&who, <CandidacyBond<T>>::get() * 2u32.into());
<CollatorSelection<T>>::register_as_candidate(RawOrigin::Signed(who).into()).unwrap();
}
}
Expand Down Expand Up @@ -126,7 +126,7 @@ benchmarks! {
}

set_candidacy_bond {
let bond_amount: BalanceOf<T> = T::Currency::minimum_balance() * 10u32.into();
let bond_amount: BalanceOf<T> = T::StakingCurrency::minimum_balance() * 10u32.into();
let origin = T::UpdateOrigin::successful_origin();
}: {
assert_ok!(
Expand All @@ -142,15 +142,15 @@ benchmarks! {
register_as_candidate {
let c in 1 .. T::MaxCandidates::get();

<CandidacyBond<T>>::put(T::Currency::minimum_balance());
<CandidacyBond<T>>::put(T::StakingCurrency::minimum_balance());
<DesiredCandidates<T>>::put(c + 1);

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

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

<session::Pallet<T>>::set_keys(
RawOrigin::Signed(caller.clone()).into(),
Expand All @@ -166,7 +166,7 @@ benchmarks! {
// worse case is the last candidate leaving.
leave_intent {
let c in (T::MinCandidates::get() + 1) .. T::MaxCandidates::get();
<CandidacyBond<T>>::put(T::Currency::minimum_balance());
<CandidacyBond<T>>::put(T::StakingCurrency::minimum_balance());
<DesiredCandidates<T>>::put(c);

register_validators::<T>(c);
Expand All @@ -181,20 +181,20 @@ benchmarks! {

// worse case is paying a non-existing candidate account.
note_author {
<CandidacyBond<T>>::put(T::Currency::minimum_balance());
T::Currency::make_free_balance_be(
<CandidacyBond<T>>::put(T::StakingCurrency::minimum_balance());
T::StakingCurrency::make_free_balance_be(
&<CollatorSelection<T>>::account_id(),
T::Currency::minimum_balance() * 4u32.into(),
T::StakingCurrency::minimum_balance() * 4u32.into(),
);
let author = account("author", 0, SEED);
let new_block: T::BlockNumber = 10u32.into();

frame_system::Pallet::<T>::set_block_number(new_block);
assert!(T::Currency::free_balance(&author) == 0u32.into());
assert!(T::StakingCurrency::free_balance(&author) == 0u32.into());
}: {
<CollatorSelection<T> as EventHandler<_, _>>::note_author(author.clone())
} verify {
assert!(T::Currency::free_balance(&author) > 0u32.into());
assert!(T::StakingCurrency::free_balance(&author) > 0u32.into());
assert_eq!(frame_system::Pallet::<T>::block_number(), new_block);
}

Expand All @@ -203,7 +203,7 @@ benchmarks! {
let r in 1 .. T::MaxCandidates::get();
let c in 1 .. T::MaxCandidates::get();

<CandidacyBond<T>>::put(T::Currency::minimum_balance());
<CandidacyBond<T>>::put(T::StakingCurrency::minimum_balance());
<DesiredCandidates<T>>::put(c);
frame_system::Pallet::<T>::set_block_number(0u32.into());

Expand Down
20 changes: 12 additions & 8 deletions crates/collator-selection/src/lib.rs
Expand Up @@ -78,7 +78,7 @@ pub mod pallet {
use sp_runtime::traits::Convert;
use sp_staking::SessionIndex;

type BalanceOf<T> = <<T as Config>::Currency as Currency<<T as SystemConfig>::AccountId>>::Balance;
type BalanceOf<T> = <<T as Config>::StakingCurrency as Currency<<T as SystemConfig>::AccountId>>::Balance;

/// A convertor from collators id. Since this pallet does not have stash/controller, this is
/// just identity.
Expand All @@ -95,8 +95,11 @@ pub mod pallet {
/// Overarching event type.
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;

/// The currency mechanism.
type Currency: ReservableCurrency<Self::AccountId>;
/// The currency used for staking.
type StakingCurrency: ReservableCurrency<Self::AccountId>;

/// The currency used for rewards.
type RewardsCurrency: ReservableCurrency<Self::AccountId>;

/// Origin that can dictate updating parameters of this pallet.
type UpdateOrigin: EnsureOrigin<Self::Origin>;
Expand Down Expand Up @@ -355,7 +358,7 @@ pub mod pallet {
if candidates.iter().any(|candidate| candidate.who == who) {
Err(Error::<T>::AlreadyCandidate)?
} else {
T::Currency::reserve(&who, deposit)?;
T::StakingCurrency::reserve(&who, deposit)?;
candidates.push(incoming);
<LastAuthoredBlock<T>>::insert(
who.clone(),
Expand Down Expand Up @@ -405,7 +408,7 @@ pub mod pallet {
.position(|candidate| candidate.who == *who)
.ok_or(Error::<T>::NotCandidate)?;
let candidate = candidates.remove(index);
T::Currency::unreserve(who, candidate.deposit);
T::StakingCurrency::unreserve(who, candidate.deposit);
<LastAuthoredBlock<T>>::remove(who.clone());
Ok(candidates.len())
})?;
Expand All @@ -427,6 +430,7 @@ pub mod pallet {
/// Kicks out candidates that did not produce a block in the kick threshold
/// and refund their deposits.
pub fn kick_stale_candidates(candidates: Vec<CandidateInfo<T::AccountId, BalanceOf<T>>>) -> Vec<T::AccountId> {
// TODO: also kick candidates when escrow balance drops
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there is consensus on whether or not we should, so fine with leaving this as-is for now

let now = frame_system::Pallet::<T>::block_number();
let kick_threshold = T::KickThreshold::get();
candidates
Expand Down Expand Up @@ -457,12 +461,12 @@ pub mod pallet {
fn note_author(author: T::AccountId) {
let pot = Self::account_id();
// assumes an ED will be sent to pot.
let reward = T::Currency::free_balance(&pot)
.checked_sub(&T::Currency::minimum_balance())
let reward = T::RewardsCurrency::free_balance(&pot)
.checked_sub(&T::RewardsCurrency::minimum_balance())
.unwrap_or_else(Zero::zero)
.div(2u32.into());
// `reward` is half of pot account minus ED, this should never fail.
let _success = T::Currency::transfer(&pot, &author, reward, KeepAlive);
let _success = T::RewardsCurrency::transfer(&pot, &author, reward, KeepAlive);
debug_assert!(_success.is_ok());
<LastAuthoredBlock<T>>::insert(author, frame_system::Pallet::<T>::block_number());

Expand Down
3 changes: 2 additions & 1 deletion crates/collator-selection/src/mock.rs
Expand Up @@ -208,7 +208,8 @@ impl ValidatorRegistration<u64> for IsRegistered {

impl Config for Test {
type Event = Event;
type Currency = Balances;
type StakingCurrency = Balances;
type RewardsCurrency = Balances;
type UpdateOrigin = EnsureSignedBy<RootAccount, u64>;
type PotId = PotId;
type MaxCandidates = MaxCandidates;
Expand Down
3 changes: 2 additions & 1 deletion parachain/runtime/interlay/src/lib.rs
Expand Up @@ -270,7 +270,8 @@ pub type CollatorSelectionUpdateOrigin =

impl collator_selection::Config for Runtime {
type Event = Event;
type Currency = NativeCurrency;
type StakingCurrency = Escrow;
type RewardsCurrency = NativeCurrency;
type UpdateOrigin = CollatorSelectionUpdateOrigin;
type PotId = CollatorPotId;
type MaxCandidates = MaxCandidates;
Expand Down
3 changes: 2 additions & 1 deletion parachain/runtime/kintsugi/src/lib.rs
Expand Up @@ -269,7 +269,8 @@ pub type CollatorSelectionUpdateOrigin =

impl collator_selection::Config for Runtime {
type Event = Event;
type Currency = NativeCurrency;
type StakingCurrency = Escrow;
type RewardsCurrency = NativeCurrency;
type UpdateOrigin = CollatorSelectionUpdateOrigin;
type PotId = CollatorPotId;
type MaxCandidates = MaxCandidates;
Expand Down
3 changes: 2 additions & 1 deletion parachain/runtime/testnet-interlay/src/lib.rs
Expand Up @@ -271,7 +271,8 @@ pub type CollatorSelectionUpdateOrigin =

impl collator_selection::Config for Runtime {
type Event = Event;
type Currency = NativeCurrency;
type StakingCurrency = Escrow;
type RewardsCurrency = NativeCurrency;
type UpdateOrigin = CollatorSelectionUpdateOrigin;
type PotId = CollatorPotId;
type MaxCandidates = MaxCandidates;
Expand Down
3 changes: 2 additions & 1 deletion parachain/runtime/testnet-kintsugi/src/lib.rs
Expand Up @@ -271,7 +271,8 @@ pub type CollatorSelectionUpdateOrigin =

impl collator_selection::Config for Runtime {
type Event = Event;
type Currency = NativeCurrency;
type StakingCurrency = Escrow;
type RewardsCurrency = NativeCurrency;
type UpdateOrigin = CollatorSelectionUpdateOrigin;
type PotId = CollatorPotId;
type MaxCandidates = MaxCandidates;
Expand Down