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

Staking: Introduce MaxBackersPerWinner #11935

Closed
wants to merge 7 commits into from
Closed

Conversation

ggwpez
Copy link
Member

@ggwpez ggwpez commented Jul 29, 2022

Draft for @kianenigma
This is a preparation for bounding the Staking, eg #11585

Helps with paritytech/polkadot-sdk#473

Changes:

  • Add MaxBackersPerWinner and Bounder
  • Use BoundedSupports in ReadySolution to make it a bit more bounded. The BoundedSupports is still an unbound vector though and would otherwise require a second Get<u32> bound.
  • Add TruncateIntoBoundedSupports
  • Feasibility check errors when the input is too long.
  • elect_with_bounds returns bounded supports.
  • set_emergency_election_result errors when the input is too long.

Open:

  • Check if the off-chain miner needs to trim.
  • Fix score function to not consume self
  • Tests for trimming in unsigned.rs
  • From @kianenigma: "[…] once we mine a solution (this code is used by both offchain workers, or by staking-miner binary), we want to do a trim_assignment_backers as well."

kianenigma and others added 7 commits July 24, 2022 15:00
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
@ggwpez ggwpez added A3-in_progress Pull request is in progress. No review needed at this stage. B3-apinoteworthy C1-low PR touches the given topic and has a low impact on builders. D9-needsaudit 👮 PR contains changes to fund-managing logic that should be properly reviewed and externally audited labels Jul 29, 2022
@github-actions github-actions bot added A0-please_review Pull request needs code review. and removed A3-in_progress Pull request is in progress. No review needed at this stage. labels Jul 29, 2022
@ggwpez ggwpez added A3-in_progress Pull request is in progress. No review needed at this stage. and removed A0-please_review Pull request needs code review. labels Jul 29, 2022
@@ -658,6 +659,10 @@ impl onchain::Config for OnChainSeqPhragmen {
>;
type DataProvider = <Runtime as pallet_election_provider_multi_phase::Config>::DataProvider;
type WeightInfo = frame_election_provider_support::weights::SubstrateWeight<Runtime>;

// TODO no idea what to use here
type MaxBackersPerWinner = ConstU32<{ u32::MAX }>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be 1024 or something a few orders larger than MaxNominatorRewardedPerValidator.

/// The final supports of the solution.
///
/// This is target-major vector, storing each winners, total backing, and each individual
/// backer.
pub supports: Supports<A>,
pub supports: BoundedSupports<AccountId, MaxBackersPerWinner>,
Copy link
Contributor

Choose a reason for hiding this comment

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

I generally move structs to be <T: Config> once they need more than one generic from T, just a suggestion.

/// Try to convert the supports of a solution into a [`ReadySolution`].
///
/// Errors if any of the supports has more than [`MaxBackersPerWinner`] backers.
impl<AccountId: PartialEq, MaxBackersPerWinner: Get<u32>>
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of this, I would implement just impl TryFrom<Supports<AccountId>> for BoundedSupports<..> in election-provider-support/src.

The other two elements of the tuple seems very out of context.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, IIRC a lot of such conversions need to re-allocate in safe rust but with unsafe we can circumvent it.

Comment on lines +983 to +986
let solution: ReadySolution<_, _> =
(supports, Default::default(), ElectionCompute::Emergency)
.try_into()
.map_err(|_| Error::<T>::TooManyBackersPerWinner)?;
Copy link
Contributor

@kianenigma kianenigma Jul 30, 2022

Choose a reason for hiding this comment

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

Suggested change
let solution: ReadySolution<_, _> =
(supports, Default::default(), ElectionCompute::Emergency)
.try_into()
.map_err(|_| Error::<T>::TooManyBackersPerWinner)?;
ReadySolution {
supports: supports.into_iter().map(|s| s.try_into()).collect::<Result<Vec<_>, _>>().map_err(|_| Error::<T>::TooManyBackersPerWinner)?,
score: Default::default(),
ElectionCompute::Emergency,
};

Supplement of https://github.com/paritytech/substrate/pull/11935/files#r933816968

pub voters: BoundedVec<(AccountId, ExtendedBalance), Bound>,
}

pub type BoundedSupportOf<E> = BoundedSupport<
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
pub type BoundedSupportOf<E> = BoundedSupport<
/// Same as [`BoundedSupport`], parameterized by something that implements [`ElectionProvider`]
pub type BoundedSupportOf<E> = BoundedSupport<

<E as ElectionProvider>::MaxBackersPerWinner,
>;

pub type BoundedSupports<AccountId, MaxBackersPerWinner> =
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
pub type BoundedSupports<AccountId, MaxBackersPerWinner> =
/// Bounded counter-part of [`sp_npos_elections::Supports`].
pub type BoundedSupports<AccountId, MaxBackersPerWinner> =

pub type BoundedSupports<AccountId, MaxBackersPerWinner> =
Vec<(AccountId, BoundedSupport<AccountId, MaxBackersPerWinner>)>;

/// Convenience to create a [`BoundedSupport`] from a [`ElectionProvider`].
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// Convenience to create a [`BoundedSupport`] from a [`ElectionProvider`].
/// Convenience to create a [`BoundedSupports`] from a [`ElectionProvider`].

I am very open to the idea of not relying on this damn 's' to distinguish Support and Supports. Suggestions?

@kianenigma
Copy link
Contributor

kianenigma commented Jul 30, 2022

From @kianenigma: "[…] once we mine a solution (this code is used by both offchain workers, or by staking-miner binary), we want to do a trim_assignment_backers as well."

This part of this work is quite nasty, deals with a lot of legacy code that I have admittedly not had the time to really re-think since years ago, so I decided to take a stab at it. Feel free to cherry pick this commit if it makes sense, else try your approach if you have one.

only checked things compile, without any test.

https://github.com/paritytech/substrate/compare/kiz-max-backers-per-winner...kiz-max-backers-per-winner-2?expand=1

PS. this is the same as what I meant by

Check if the off-chain miner needs to trim.

So these two TODOs are practically the same.

@kianenigma
Copy link
Contributor

cc @Ank4n

@kianenigma
Copy link
Contributor

Let's close this for now, and revisit later.

@kianenigma kianenigma closed this Dec 5, 2022
@louismerlin louismerlin removed the D9-needsaudit 👮 PR contains changes to fund-managing logic that should be properly reviewed and externally audited label May 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A3-in_progress Pull request is in progress. No review needed at this stage. C1-low PR touches the given topic and has a low impact on builders.
Projects
Status: 👀 Might Revisit 👀
Development

Successfully merging this pull request may close these issues.

4 participants