Add simple collator election mechanism (#1340)

Fixes https://github.com/paritytech/polkadot-sdk/issues/106

Port of cumulus PR https://github.com/paritytech/cumulus/pull/2960

This PR adds the ability to bid for collator slots even after the max
number of collators have already registered. This eliminates the first
come, first served mechanism that was in place before.

Key changes:
- added `update_bond` extrinsic to allow registered candidates to adjust
their bonds in order to dynamically control their bids
- added `take_candidate_slot` extrinsic to try to replace an already
existing candidate by bidding more than them
- candidates are now kept in a sorted list in the pallet storage, where
the top `DesiredCandidates` out of `MaxCandidates` candidates in the
list will be selected by the session pallet as collators
- if the candidacy bond is increased through a `set_candidacy_bond`
call, candidates which don't meet the new bond requirements are kicked


# Checklist

- [ ] My PR includes a detailed description as outlined in the
"Description" section above
- [ ] My PR follows the [labeling
requirements](https://github.com/paritytech/polkadot-sdk/blob/master/docs/CONTRIBUTING.md#process)
of this project (at minimum one label for `T` required)
- [ ] I have made corresponding changes to the documentation (if
applicable)
- [ ] I have added tests that prove my fix is effective or that my
feature works (if applicable)
- [ ] If this PR alters any external APIs or interfaces used by
Polkadot, the corresponding Polkadot PR is ready as well as the
corresponding Cumulus PR (optional)

---------

Signed-off-by: georgepisaltu <george.pisaltu@parity.io>
This commit is contained in:
georgepisaltu
2023-11-14 18:22:30 +02:00
committed by GitHub
parent cd38ccff7f
commit 7d735fc8ae
13 changed files with 1731 additions and 163 deletions
@@ -121,7 +121,7 @@ impl<T: frame_system::Config> pallet_collator_selection::WeightInfo for WeightIn
}
/// Storage: `CollatorSelection::CandidacyBond` (r:0 w:1)
/// Proof: `CollatorSelection::CandidacyBond` (`max_values`: Some(1), `max_size`: Some(16), added: 511, mode: `MaxEncodedLen`)
fn set_candidacy_bond() -> Weight {
fn set_candidacy_bond(_c: u32, _k: u32) -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
@@ -175,6 +175,30 @@ impl<T: frame_system::Config> pallet_collator_selection::WeightInfo for WeightIn
.saturating_add(T::DbWeight::get().reads(2))
.saturating_add(T::DbWeight::get().writes(2))
}
fn update_bond(c: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `306 + c * (50 ±0)`
// Estimated: `6287`
// Minimum execution time: 34_814_000 picoseconds.
Weight::from_parts(36_371_520, 0)
.saturating_add(Weight::from_parts(0, 6287))
// Standard Error: 2_391
.saturating_add(Weight::from_parts(201_700, 0).saturating_mul(c.into()))
.saturating_add(T::DbWeight::get().reads(2))
.saturating_add(T::DbWeight::get().writes(2))
}
fn take_candidate_slot(c: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `306 + c * (50 ±0)`
// Estimated: `6287`
// Minimum execution time: 34_814_000 picoseconds.
Weight::from_parts(36_371_520, 0)
.saturating_add(Weight::from_parts(0, 6287))
// Standard Error: 2_391
.saturating_add(Weight::from_parts(201_700, 0).saturating_mul(c.into()))
.saturating_add(T::DbWeight::get().reads(2))
.saturating_add(T::DbWeight::get().writes(2))
}
/// Storage: `System::Account` (r:2 w:2)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
/// Storage: `System::BlockWeight` (r:1 w:1)