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

Add simple collator election mechanism #1340

Merged
merged 44 commits into from
Nov 14, 2023
Merged

Conversation

georgepisaltu
Copy link
Contributor

@georgepisaltu georgepisaltu commented Aug 31, 2023

Fixes #106

Port of cumulus PR paritytech/cumulus#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 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>
@georgepisaltu georgepisaltu added A0-needs_burnin Pull request needs to be tested on a live validator node before merge. DevOps is notified via matrix I5-enhancement An additional feature request. T2-pallets This PR/Issue is related to a particular pallet. labels Aug 31, 2023
@georgepisaltu georgepisaltu self-assigned this Aug 31, 2023
@kianenigma
Copy link
Contributor

Can I get a brief summary of why bags-list was not considered for this line of work? It is not a lot of extra work to set up, and it would enable an infinitely list of sorted collators to be kept, showcasing the use of data-structures to solve scalability issues in blockchains.

}

/// The caller `origin` replaces a candidate `target` in the collator
/// candidate list by reserving `deposit`. The amount `deposit`
Copy link
Contributor

Choose a reason for hiding this comment

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

line width is not according to our documentation guideline, please use exactly the correct line-width, not less.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@georgepisaltu
Copy link
Contributor Author

Can I get a brief summary of why bags-list was not considered for this line of work? It is not a lot of extra work to set up, and it would enable an infinitely list of sorted collators to be kept, showcasing the use of data-structures to solve scalability issues in blockchains.

Long discussion here, but in short the benchmarks suggest bags-list starts to make sense when the element count is over 100, but the candidate list will not hold more than ~20 elements in the near future.

In the previous PR linked above I had POCs with bags-list and with the SortedListProvider abstraction satisfied by a simple sorted list. Out of all options, we chose to go simple at this time. It will also be fairly easy to slot in a SortedListProvider if needed and then users can configure it.

cumulus/pallets/collator-selection/src/benchmarking.rs Outdated Show resolved Hide resolved
cumulus/pallets/collator-selection/src/lib.rs Outdated Show resolved Hide resolved
cumulus/pallets/collator-selection/src/lib.rs Outdated Show resolved Hide resolved
cumulus/pallets/collator-selection/src/lib.rs Outdated Show resolved Hide resolved
cumulus/pallets/collator-selection/src/lib.rs Outdated Show resolved Hide resolved
Comment on lines 619 to 627
idx += 1;
while idx < candidate_count && candidates[idx].deposit < new_deposit {
candidates.as_mut().swap(idx - 1, idx);
idx += 1;
}
} else {
while idx > 0 && candidates[idx].deposit >= new_deposit {
candidates.as_mut().swap(idx - 1, idx);
idx -= 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is safe as long as candidates is less than u32::MAX, but not sure if it's still FRAME best practice to use something like saturating_accrue. @gpestana ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The length of candidates can be at most u32::MAX, since the BoundedVec is capped by MaxCandidates, which is Get<u32>. However, idx must be less than u32::MAX. When creating idx through candidates.iter().position(), if a match is found, it will be at most u32::MAX - 1 even if it's the last element of a u32::MAX-sized list. As things stand, no operation above should overflow or underflow.

cumulus/pallets/collator-selection/src/lib.rs Outdated Show resolved Hide resolved
cumulus/pallets/collator-selection/src/lib.rs Outdated Show resolved Hide resolved
Comment on lines 705 to 708
idx += 1;
while idx < list.len() && list[idx].deposit < deposit {
list.as_mut().swap(idx - 1, idx);
idx += 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you should use checked_add here and return an Overflow error to prevent any state mutations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The same assumptions mentioned previously about the list being capped by MaxCandidates: Get<u32> and the idx of elements still hold here. Would it be ok to leave as is if there were UTs targeting specifically these potential overflows?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think so but prefer someone else on FRAME to comment.

cumulus/pallets/collator-selection/src/lib.rs Outdated Show resolved Hide resolved
Signed-off-by: georgepisaltu <george.pisaltu@parity.io>
Signed-off-by: georgepisaltu <george.pisaltu@parity.io>
@georgepisaltu georgepisaltu requested review from joepetrowski and a team and removed request for gpestana, muharem, ggwpez and liamaharon September 20, 2023 11:48
Copy link
Contributor

@joepetrowski joepetrowski left a comment

Choose a reason for hiding this comment

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

Few comments but overall good. Still prefer safe math in the runtime even if it's paranoid.

cumulus/pallets/collator-selection/src/lib.rs Outdated Show resolved Hide resolved
cumulus/pallets/collator-selection/src/lib.rs Show resolved Hide resolved
cumulus/pallets/collator-selection/src/lib.rs Outdated Show resolved Hide resolved
Signed-off-by: georgepisaltu <george.pisaltu@parity.io>
Signed-off-by: georgepisaltu <george.pisaltu@parity.io>
Signed-off-by: georgepisaltu <george.pisaltu@parity.io>
bkontur added a commit to bkontur/runtimes that referenced this pull request Jan 11, 2024
bkontur added a commit to bkontur/runtimes that referenced this pull request Jan 12, 2024
bkontur added a commit to bkontur/runtimes that referenced this pull request Jan 12, 2024
bkontur added a commit to bkontur/runtimes that referenced this pull request Jan 22, 2024
bkontur added a commit to bkontur/runtimes that referenced this pull request Jan 23, 2024
claravanstaden pushed a commit to Snowfork/runtimes that referenced this pull request Feb 5, 2024
serban300 pushed a commit to serban300/polkadot-sdk that referenced this pull request Apr 8, 2024
* ignore errors when dumping logs and container is missing

* fixed typo

* print correct payload length

* increase conversion rate a bit when estimating fee (to avoid message rejects when rate update tx is active)

* fmt
serban300 pushed a commit to serban300/polkadot-sdk that referenced this pull request Apr 8, 2024
* ignore errors when dumping logs and container is missing

* fixed typo

* print correct payload length

* increase conversion rate a bit when estimating fee (to avoid message rejects when rate update tx is active)

* fmt
serban300 pushed a commit to serban300/polkadot-sdk that referenced this pull request Apr 8, 2024
* ignore errors when dumping logs and container is missing

* fixed typo

* print correct payload length

* increase conversion rate a bit when estimating fee (to avoid message rejects when rate update tx is active)

* fmt
serban300 pushed a commit to serban300/polkadot-sdk that referenced this pull request Apr 8, 2024
* ignore errors when dumping logs and container is missing

* fixed typo

* print correct payload length

* increase conversion rate a bit when estimating fee (to avoid message rejects when rate update tx is active)

* fmt
serban300 pushed a commit to serban300/polkadot-sdk that referenced this pull request Apr 9, 2024
* ignore errors when dumping logs and container is missing

* fixed typo

* print correct payload length

* increase conversion rate a bit when estimating fee (to avoid message rejects when rate update tx is active)

* fmt
serban300 pushed a commit to serban300/polkadot-sdk that referenced this pull request Apr 9, 2024
* ignore errors when dumping logs and container is missing

* fixed typo

* print correct payload length

* increase conversion rate a bit when estimating fee (to avoid message rejects when rate update tx is active)

* fmt
serban300 pushed a commit to serban300/polkadot-sdk that referenced this pull request Apr 9, 2024
* ignore errors when dumping logs and container is missing

* fixed typo

* print correct payload length

* increase conversion rate a bit when estimating fee (to avoid message rejects when rate update tx is active)

* fmt
serban300 pushed a commit to serban300/polkadot-sdk that referenced this pull request Apr 9, 2024
* ignore errors when dumping logs and container is missing

* fixed typo

* print correct payload length

* increase conversion rate a bit when estimating fee (to avoid message rejects when rate update tx is active)

* fmt
serban300 pushed a commit to serban300/polkadot-sdk that referenced this pull request Apr 9, 2024
* ignore errors when dumping logs and container is missing

* fixed typo

* print correct payload length

* increase conversion rate a bit when estimating fee (to avoid message rejects when rate update tx is active)

* fmt
serban300 pushed a commit to serban300/polkadot-sdk that referenced this pull request Apr 9, 2024
* ignore errors when dumping logs and container is missing

* fixed typo

* print correct payload length

* increase conversion rate a bit when estimating fee (to avoid message rejects when rate update tx is active)

* fmt
serban300 pushed a commit to serban300/polkadot-sdk that referenced this pull request Apr 10, 2024
* ignore errors when dumping logs and container is missing

* fixed typo

* print correct payload length

* increase conversion rate a bit when estimating fee (to avoid message rejects when rate update tx is active)

* fmt
serban300 pushed a commit to serban300/polkadot-sdk that referenced this pull request Apr 10, 2024
* ignore errors when dumping logs and container is missing

* fixed typo

* print correct payload length

* increase conversion rate a bit when estimating fee (to avoid message rejects when rate update tx is active)

* fmt
bkchr pushed a commit that referenced this pull request Apr 10, 2024
* ignore errors when dumping logs and container is missing

* fixed typo

* print correct payload length

* increase conversion rate a bit when estimating fee (to avoid message rejects when rate update tx is active)

* fmt
github-merge-queue bot pushed a commit that referenced this pull request Apr 23, 2024
Fixes #4206

In #1340 one of the storage types was changed from `Candidates` to
`CandidateList`. Since the actual key includes the hash of this value,
all of the candidates stored here are (a) "missing" and (b) unable to
unreserve their candidacy bond.

This migration kills the storage values and refunds the deposit held for
each candidate.

---------

Signed-off-by: georgepisaltu <george.pisaltu@parity.io>
Co-authored-by: georgepisaltu <52418509+georgepisaltu@users.noreply.github.com>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: georgepisaltu <george.pisaltu@parity.io>
EgorPopelyaev pushed a commit that referenced this pull request Apr 24, 2024
Fixes #4206

In #1340 one of the storage types was changed from `Candidates` to
`CandidateList`. Since the actual key includes the hash of this value,
all of the candidates stored here are (a) "missing" and (b) unable to
unreserve their candidacy bond.

This migration kills the storage values and refunds the deposit held for
each candidate.

---------

Signed-off-by: georgepisaltu <george.pisaltu@parity.io>
Co-authored-by: georgepisaltu <52418509+georgepisaltu@users.noreply.github.com>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: georgepisaltu <george.pisaltu@parity.io>
EgorPopelyaev pushed a commit that referenced this pull request Apr 24, 2024
Fixes #4206

In #1340 one of the storage types was changed from `Candidates` to
`CandidateList`. Since the actual key includes the hash of this value,
all of the candidates stored here are (a) "missing" and (b) unable to
unreserve their candidacy bond.

This migration kills the storage values and refunds the deposit held for
each candidate.

---------

Signed-off-by: georgepisaltu <george.pisaltu@parity.io>
Co-authored-by: georgepisaltu <52418509+georgepisaltu@users.noreply.github.com>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: georgepisaltu <george.pisaltu@parity.io>
EgorPopelyaev pushed a commit that referenced this pull request Apr 24, 2024
Fixes #4206

In #1340 one of the storage types was changed from `Candidates` to
`CandidateList`. Since the actual key includes the hash of this value,
all of the candidates stored here are (a) "missing" and (b) unable to
unreserve their candidacy bond.

This migration kills the storage values and refunds the deposit held for
each candidate.

---------

Signed-off-by: georgepisaltu <george.pisaltu@parity.io>
Co-authored-by: georgepisaltu <52418509+georgepisaltu@users.noreply.github.com>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: georgepisaltu <george.pisaltu@parity.io>
EgorPopelyaev pushed a commit that referenced this pull request Apr 24, 2024
Fixes #4206

In #1340 one of the storage types was changed from `Candidates` to
`CandidateList`. Since the actual key includes the hash of this value,
all of the candidates stored here are (a) "missing" and (b) unable to
unreserve their candidacy bond.

This migration kills the storage values and refunds the deposit held for
each candidate.

---------

Signed-off-by: georgepisaltu <george.pisaltu@parity.io>
Co-authored-by: georgepisaltu <52418509+georgepisaltu@users.noreply.github.com>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: georgepisaltu <george.pisaltu@parity.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A0-needs_burnin Pull request needs to be tested on a live validator node before merge. DevOps is notified via matrix I5-enhancement An additional feature request. T2-pallets This PR/Issue is related to a particular pallet.
Projects
Status: Audited
Development

Successfully merging this pull request may close these issues.

Add Simple Election for Permissionless Collators
7 participants