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

Bounded Validators / Authorities in New Sessions #8640

Closed
wants to merge 32 commits into from

Conversation

shawntabrizi
Copy link
Member

@shawntabrizi shawntabrizi commented Apr 20, 2021

This adds a limit to the number of validators and authorities that can be set by the session handler.

@shawntabrizi shawntabrizi changed the title Bounded Aura Bounded Validators / Authorities in New Sessions Apr 20, 2021
@shawntabrizi shawntabrizi added B7-runtimenoteworthy D9-needsaudit 👮 PR contains changes to fund-managing logic that should be properly reviewed and externally audited C1-low PR touches the given topic and has a low impact on builders. labels Apr 20, 2021
frame/aura/src/lib.rs Outdated Show resolved Hide resolved
@@ -332,6 +332,7 @@ parameter_types! {
pub const ExpectedBlockTime: Moment = MILLISECS_PER_BLOCK;
pub const ReportLongevity: u64 =
BondingDuration::get() as u64 * SessionsPerEra::get() as u64 * EpochDuration::get();
pub const MaxAuthorities: u32 = 100;
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably we should document that this should be the upper bound of what Staking::validator_count is ever going to be.

frame/aura/src/lib.rs Outdated Show resolved Hide resolved
frame/aura/src/lib.rs Outdated Show resolved Hide resolved
frame/aura/src/lib.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@kianenigma kianenigma left a comment

Choose a reason for hiding this comment

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

Only major issue: We are silently dropping authorities at session listener pallets (aura, babe, grandpa etc.) and I think we should not.

Pushed a truncating_from as you asked for, should help also with my only comment. Basically, we should use this instead of a combination of .take and force_from to ensure we get the logs somewhere at least.

@shawntabrizi
Copy link
Member Author

Part of paritytech/polkadot-sdk#323

@@ -165,7 +187,7 @@ impl<T: Config> sp_runtime::BoundToRuntimeAppPublic for Pallet<T> {
type Public = T::AuthorityId;
}

impl<T: Config> OneSessionHandler<T::AccountId> for Pallet<T> {
impl<T: Config> OneSessionHandler<T::AccountId, T::MaxAuthorities> for Pallet<T> {
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 very very good because now OneSessionHandler<_, X> is a different trait than OneSessionHandler<_, Y> and we should get some good compile time checks, ensuring that we can't faff with them:

if babe implements OneSessionHandler<_, 10> and aura implements OneSessionHandler<_, 20>, I think if you then say impl session::Config { type SessionHandlers = (babe, aura) } we should get an error 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

But the missing point is:

  • If session's MaxAuthority is 100
  • and babe and aura's MaxAuthority is 50

I think we should prevent this at compile time but I don't it in the current state of the PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

oh no we have this guarantee as well, because of

type SessionHandler: SessionHandler<Self::ValidatorId, Self::MaxValidators>;

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, the point here is that there should be a single global config of MaxAuthorities / MaxValidators in the runtime, and this should be shared with all of the pallets which are using a session handler.

Anything else would result in a compile error, which ensures that these systems are working together correctly.

I dont think it would ever be that session and babe would have different max authorities, but maybe @andresilva could give insight

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, we should have an uniform number of authorities throughout the whole runtime.

Copy link
Contributor

@kianenigma kianenigma left a comment

Choose a reason for hiding this comment

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

So far LGTM

shawntabrizi and others added 2 commits April 22, 2021 05:22
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
Copy link
Contributor

@andresilva andresilva left a comment

Choose a reason for hiding this comment

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

Overall the changes LGTM but I feel like we could maximize the utility of this by spreading the typed sized bound to more places, this is so that we can avoid truncating_from which I think diminishes the value of this change. I believe the two main causes for the back and forth between vec and bounded vec are:

  • SessionManager still uses regular vecs - I didn't look much deeply but I think it's probably not too hard to migrate it to BoundedVec as well.
  • The SessionHandler/OneSessionHandler methods use slices / iterators.
    • SessionHandler methods are probably not too hard to convert to BoundedVec as well.
    • OneSessionHandler I'm not sure what we should do. Ideally we would have a BoundedIterator trait so that we don't lose the size type information. A weaker alternative would be some extension method on iterators that allows us to essentialy do a .collect_bounded<N>() where we get back a BoundedVec<_, N> (basically just hiding the truncating_from).

Comment on lines +599 to +600
// Note this should never truncate because queued keys is also bounded to `MaxValidators`,
// but we do so defensively.
Copy link
Contributor

Choose a reason for hiding this comment

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

If BoundedVec add a map method we could use it here and guarantee at compile-time that we're getting a BoundedVec with the same size bound.

Comment on lines +623 to +626
let validators = BoundedVec::<T::ValidatorId, T::MaxValidators>::truncating_from(
validators,
Some("Session Rotate Session Maybe Next Validators"),
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't it make sense that SessionManager is also parameterized on MaxValidators and SessionManager::new_session returns a BoundedVec instead?

@@ -165,7 +187,7 @@ impl<T: Config> sp_runtime::BoundToRuntimeAppPublic for Pallet<T> {
type Public = T::AuthorityId;
}

impl<T: Config> OneSessionHandler<T::AccountId> for Pallet<T> {
impl<T: Config> OneSessionHandler<T::AccountId, T::MaxAuthorities> for Pallet<T> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, we should have an uniform number of authorities throughout the whole runtime.

@cwerling cwerling added D1-audited 👍 PR contains changes to fund-managing logic that has been properly reviewed and externally audited and removed D9-needsaudit 👮 PR contains changes to fund-managing logic that should be properly reviewed and externally audited labels May 4, 2021
@shawntabrizi
Copy link
Member Author

stale

@shawntabrizi shawntabrizi deleted the shawntabrizi-bounded-aura branch June 17, 2021 22:50
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. C1-low PR touches the given topic and has a low impact on builders. D1-audited 👍 PR contains changes to fund-managing logic that has been properly reviewed and externally audited
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants