From 23bf5bc6ab8c83167043fd2e70e4c776dc086c76 Mon Sep 17 00:00:00 2001 From: Niklas Adolfsson Date: Tue, 14 Mar 2023 23:53:03 +0100 Subject: [PATCH] frame epm: expose `feasibility_check` in MinerConfig (#13555) * frame epm: expose feasibity_check in miner The goal with this commit is to expose the `feasibity_check` such that anyone that implements the `MinerConfig trait` can utilize it * cleanup * fix tests --- bin/node/runtime/src/lib.rs | 1 + .../election-provider-multi-phase/src/lib.rs | 127 +++++------------- .../election-provider-multi-phase/src/mock.rs | 3 + .../src/signed.rs | 2 +- .../src/unsigned.rs | 98 +++++++++++++- 5 files changed, 134 insertions(+), 97 deletions(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 614dbc4d958c8..d8426d3b35e15 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -702,6 +702,7 @@ impl pallet_election_provider_multi_phase::MinerConfig for Runtime { type Solution = NposSolution16; type MaxVotesPerVoter = <::DataProvider as ElectionDataProvider>::MaxVotesPerVoter; + type MaxWinners = MaxActiveValidators; // The unsigned submissions have to respect the weight of the submit_unsigned call, thus their // weight estimate function is wired to this call's weight. diff --git a/frame/election-provider-multi-phase/src/lib.rs b/frame/election-provider-multi-phase/src/lib.rs index 3ba90f4112a35..80bab68074c87 100644 --- a/frame/election-provider-multi-phase/src/lib.rs +++ b/frame/election-provider-multi-phase/src/lib.rs @@ -247,10 +247,7 @@ use sp_arithmetic::{ traits::{CheckedAdd, Zero}, UpperOf, }; -use sp_npos_elections::{ - assignment_ratio_to_staked_normalized, BoundedSupports, ElectionScore, EvaluateSupport, - Supports, VoteWeight, -}; +use sp_npos_elections::{BoundedSupports, ElectionScore, IdentifierT, Supports, VoteWeight}; use sp_runtime::{ transaction_validity::{ InvalidTransaction, TransactionPriority, TransactionSource, TransactionValidity, @@ -430,13 +427,17 @@ impl Default for RawSolution { DefaultNoBound, scale_info::TypeInfo, )] -#[scale_info(skip_type_params(T))] -pub struct ReadySolution { +#[scale_info(skip_type_params(AccountId, MaxWinners))] +pub struct ReadySolution +where + AccountId: IdentifierT, + MaxWinners: Get, +{ /// The final supports of the solution. /// /// This is target-major vector, storing each winners, total backing, and each individual /// backer. - pub supports: BoundedSupports, + pub supports: BoundedSupports, /// The score of the solution. /// /// This is needed to potentially challenge the solution. @@ -451,11 +452,11 @@ pub struct ReadySolution { /// These are stored together because they are often accessed together. #[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, Default, TypeInfo)] #[scale_info(skip_type_params(T))] -pub struct RoundSnapshot { +pub struct RoundSnapshot { /// All of the voters. - pub voters: Vec>, + pub voters: Vec, /// All of the targets. - pub targets: Vec, + pub targets: Vec, } /// Encodes the length of a solution or a snapshot. @@ -614,6 +615,7 @@ pub mod pallet { type MinerConfig: crate::unsigned::MinerConfig< AccountId = Self::AccountId, MaxVotesPerVoter = ::MaxVotesPerVoter, + MaxWinners = Self::MaxWinners, >; /// Maximum number of signed submissions that can be queued. @@ -733,6 +735,11 @@ pub mod pallet { fn max_votes_per_voter() -> u32 { ::MaxVotesPerVoter::get() } + + #[pallet::constant_name(MinerMaxWinners)] + fn max_winners() -> u32 { + ::MaxWinners::get() + } } #[pallet::hooks] @@ -1247,14 +1254,15 @@ pub mod pallet { /// Current best solution, signed or unsigned, queued to be returned upon `elect`. #[pallet::storage] #[pallet::getter(fn queued_solution)] - pub type QueuedSolution = StorageValue<_, ReadySolution>; + pub type QueuedSolution = + StorageValue<_, ReadySolution>; /// Snapshot data of the round. /// /// This is created at the beginning of the signed phase and cleared upon calling `elect`. #[pallet::storage] #[pallet::getter(fn snapshot)] - pub type Snapshot = StorageValue<_, RoundSnapshot>; + pub type Snapshot = StorageValue<_, RoundSnapshot>>; /// Desired number of targets to elect for this round. /// @@ -1385,7 +1393,7 @@ impl Pallet { // instead of using storage APIs, we do a manual encoding into a fixed-size buffer. // `encoded_size` encodes it without storing it anywhere, this should not cause any // allocation. - let snapshot = RoundSnapshot:: { voters, targets }; + let snapshot = RoundSnapshot::> { voters, targets }; let size = snapshot.encoded_size(); log!(debug, "snapshot pre-calculated size {:?}", size); let mut buffer = Vec::with_capacity(size); @@ -1479,89 +1487,22 @@ impl Pallet { pub fn feasibility_check( raw_solution: RawSolution>, compute: ElectionCompute, - ) -> Result, FeasibilityError> { - let RawSolution { solution, score, round } = raw_solution; - - // First, check round. - ensure!(Self::round() == round, FeasibilityError::InvalidRound); - - // Winners are not directly encoded in the solution. - let winners = solution.unique_targets(); - + ) -> Result, FeasibilityError> { let desired_targets = Self::desired_targets().ok_or(FeasibilityError::SnapshotUnavailable)?; - ensure!(winners.len() as u32 == desired_targets, FeasibilityError::WrongWinnerCount); - // Fail early if targets requested by data provider exceed maximum winners supported. - ensure!( - desired_targets <= ::MaxWinners::get(), - FeasibilityError::TooManyDesiredTargets - ); - - // Ensure that the solution's score can pass absolute min-score. - let submitted_score = raw_solution.score; - ensure!( - Self::minimum_untrusted_score().map_or(true, |min_score| { - submitted_score.strict_threshold_better(min_score, Perbill::zero()) - }), - FeasibilityError::UntrustedScoreTooLow - ); - - // Read the entire snapshot. - let RoundSnapshot { voters: snapshot_voters, targets: snapshot_targets } = - Self::snapshot().ok_or(FeasibilityError::SnapshotUnavailable)?; - - // ----- Start building. First, we need some closures. - let cache = helpers::generate_voter_cache::(&snapshot_voters); - let voter_at = helpers::voter_at_fn::(&snapshot_voters); - let target_at = helpers::target_at_fn::(&snapshot_targets); - let voter_index = helpers::voter_index_fn_usize::(&cache); - - // Then convert solution -> assignment. This will fail if any of the indices are gibberish, - // namely any of the voters or targets. - let assignments = solution - .into_assignment(voter_at, target_at) - .map_err::(Into::into)?; - - // Ensure that assignments is correct. - let _ = assignments.iter().try_for_each(|assignment| { - // Check that assignment.who is actually a voter (defensive-only). - // NOTE: while using the index map from `voter_index` is better than a blind linear - // search, this *still* has room for optimization. Note that we had the index when - // we did `solution -> assignment` and we lost it. Ideal is to keep the index - // around. - - // Defensive-only: must exist in the snapshot. - let snapshot_index = - voter_index(&assignment.who).ok_or(FeasibilityError::InvalidVoter)?; - // Defensive-only: index comes from the snapshot, must exist. - let (_voter, _stake, targets) = - snapshot_voters.get(snapshot_index).ok_or(FeasibilityError::InvalidVoter)?; - - // Check that all of the targets are valid based on the snapshot. - if assignment.distribution.iter().any(|(d, _)| !targets.contains(d)) { - return Err(FeasibilityError::InvalidVote) - } - Ok(()) - })?; - - // ----- Start building support. First, we need one more closure. - let stake_of = helpers::stake_of_fn::(&snapshot_voters, &cache); - - // This might fail if the normalization fails. Very unlikely. See `integrity_test`. - let staked_assignments = assignment_ratio_to_staked_normalized(assignments, stake_of) - .map_err::(Into::into)?; - let supports = sp_npos_elections::to_supports(&staked_assignments); - - // Finally, check that the claimed score was indeed correct. - let known_score = supports.evaluate(); - ensure!(known_score == score, FeasibilityError::InvalidScore); - - // Size of winners in miner solution is equal to `desired_targets` <= `MaxWinners`. - let supports = supports - .try_into() - .defensive_map_err(|_| FeasibilityError::BoundedConversionFailed)?; - Ok(ReadySolution { supports, compute, score }) + let snapshot = Self::snapshot().ok_or(FeasibilityError::SnapshotUnavailable)?; + let round = Self::round(); + let minimum_untrusted_score = Self::minimum_untrusted_score(); + + Miner::::feasibility_check( + raw_solution, + compute, + desired_targets, + snapshot, + round, + minimum_untrusted_score, + ) } /// Perform the tasks to be done after a new `elect` has been triggered: diff --git a/frame/election-provider-multi-phase/src/mock.rs b/frame/election-provider-multi-phase/src/mock.rs index 8dee94bb39716..da7a0cf1dd263 100644 --- a/frame/election-provider-multi-phase/src/mock.rs +++ b/frame/election-provider-multi-phase/src/mock.rs @@ -297,6 +297,8 @@ parameter_types! { pub static MockWeightInfo: MockedWeightInfo = MockedWeightInfo::Real; pub static MaxElectingVoters: VoterIndex = u32::max_value(); pub static MaxElectableTargets: TargetIndex = TargetIndex::max_value(); + + #[derive(Debug)] pub static MaxWinners: u32 = 200; pub static EpochLength: u64 = 30; @@ -359,6 +361,7 @@ impl MinerConfig for Runtime { type MaxLength = MinerMaxLength; type MaxWeight = MinerMaxWeight; type MaxVotesPerVoter = ::MaxVotesPerVoter; + type MaxWinners = MaxWinners; type Solution = TestNposSolution; fn solution_weight(v: u32, t: u32, a: u32, d: u32) -> Weight { diff --git a/frame/election-provider-multi-phase/src/signed.rs b/frame/election-provider-multi-phase/src/signed.rs index 75bbcfcf9e537..b8a27fdfafde5 100644 --- a/frame/election-provider-multi-phase/src/signed.rs +++ b/frame/election-provider-multi-phase/src/signed.rs @@ -462,7 +462,7 @@ impl Pallet { /// /// Infallible pub fn finalize_signed_phase_accept_solution( - ready_solution: ReadySolution, + ready_solution: ReadySolution, who: &T::AccountId, deposit: BalanceOf, call_fee: BalanceOf, diff --git a/frame/election-provider-multi-phase/src/unsigned.rs b/frame/election-provider-multi-phase/src/unsigned.rs index 3d80f8f4d9315..9c09cb48c7c05 100644 --- a/frame/election-provider-multi-phase/src/unsigned.rs +++ b/frame/election-provider-multi-phase/src/unsigned.rs @@ -23,12 +23,17 @@ use crate::{ }; use codec::Encode; use frame_election_provider_support::{NposSolution, NposSolver, PerThing128, VoteWeight}; -use frame_support::{dispatch::DispatchResult, ensure, traits::Get, BoundedVec}; +use frame_support::{ + dispatch::DispatchResult, + ensure, + traits::{DefensiveResult, Get}, + BoundedVec, +}; use frame_system::offchain::SubmitTransaction; use scale_info::TypeInfo; use sp_npos_elections::{ assignment_ratio_to_staked_normalized, assignment_staked_to_ratio_normalized, ElectionResult, - ElectionScore, + ElectionScore, EvaluateSupport, }; use sp_runtime::{ offchain::storage::{MutateStorageError, StorageValueRef}, @@ -351,7 +356,7 @@ impl Pallet { // ensure score is being improved. Panic henceforth. ensure!( - Self::queued_solution().map_or(true, |q: ReadySolution<_>| raw_solution + Self::queued_solution().map_or(true, |q: ReadySolution<_, _>| raw_solution .score .strict_threshold_better(q.score, T::BetterUnsignedThreshold::get())), Error::::PreDispatchWeakSubmission, @@ -387,6 +392,8 @@ pub trait MinerConfig { /// /// The weight is computed using `solution_weight`. type MaxWeight: Get; + /// The maximum number of winners that can be elected. + type MaxWinners: Get; /// Something that can compute the weight of a solution. /// /// This weight estimate is then used to trim the solution, based on [`MinerConfig::MaxWeight`]. @@ -689,6 +696,91 @@ impl Miner { ); final_decision } + + /// Checks the feasibility of a solution. + pub fn feasibility_check( + raw_solution: RawSolution>, + compute: ElectionCompute, + desired_targets: u32, + snapshot: RoundSnapshot>, + current_round: u32, + minimum_untrusted_score: Option, + ) -> Result, FeasibilityError> { + let RawSolution { solution, score, round } = raw_solution; + let RoundSnapshot { voters: snapshot_voters, targets: snapshot_targets } = snapshot; + + // First, check round. + ensure!(current_round == round, FeasibilityError::InvalidRound); + + // Winners are not directly encoded in the solution. + let winners = solution.unique_targets(); + + ensure!(winners.len() as u32 == desired_targets, FeasibilityError::WrongWinnerCount); + // Fail early if targets requested by data provider exceed maximum winners supported. + ensure!(desired_targets <= T::MaxWinners::get(), FeasibilityError::TooManyDesiredTargets); + + // Ensure that the solution's score can pass absolute min-score. + let submitted_score = raw_solution.score; + ensure!( + minimum_untrusted_score.map_or(true, |min_score| { + submitted_score.strict_threshold_better(min_score, sp_runtime::Perbill::zero()) + }), + FeasibilityError::UntrustedScoreTooLow + ); + + // ----- Start building. First, we need some closures. + let cache = helpers::generate_voter_cache::(&snapshot_voters); + let voter_at = helpers::voter_at_fn::(&snapshot_voters); + let target_at = helpers::target_at_fn::(&snapshot_targets); + let voter_index = helpers::voter_index_fn_usize::(&cache); + + // Then convert solution -> assignment. This will fail if any of the indices are gibberish, + // namely any of the voters or targets. + let assignments = solution + .into_assignment(voter_at, target_at) + .map_err::(Into::into)?; + + // Ensure that assignments is correct. + let _ = assignments.iter().try_for_each(|assignment| { + // Check that assignment.who is actually a voter (defensive-only). + // NOTE: while using the index map from `voter_index` is better than a blind linear + // search, this *still* has room for optimization. Note that we had the index when + // we did `solution -> assignment` and we lost it. Ideal is to keep the index + // around. + + // Defensive-only: must exist in the snapshot. + let snapshot_index = + voter_index(&assignment.who).ok_or(FeasibilityError::InvalidVoter)?; + // Defensive-only: index comes from the snapshot, must exist. + let (_voter, _stake, targets) = + snapshot_voters.get(snapshot_index).ok_or(FeasibilityError::InvalidVoter)?; + + // Check that all of the targets are valid based on the snapshot. + if assignment.distribution.iter().any(|(d, _)| !targets.contains(d)) { + return Err(FeasibilityError::InvalidVote) + } + Ok(()) + })?; + + // ----- Start building support. First, we need one more closure. + let stake_of = helpers::stake_of_fn::(&snapshot_voters, &cache); + + // This might fail if the normalization fails. Very unlikely. See `integrity_test`. + let staked_assignments = assignment_ratio_to_staked_normalized(assignments, stake_of) + .map_err::(Into::into)?; + let supports = sp_npos_elections::to_supports(&staked_assignments); + + // Finally, check that the claimed score was indeed correct. + let known_score = supports.evaluate(); + ensure!(known_score == score, FeasibilityError::InvalidScore); + + // Size of winners in miner solution is equal to `desired_targets` <= `MaxWinners`. + let supports = supports + .try_into() + .defensive_map_err(|_| FeasibilityError::BoundedConversionFailed)?; + + Ok(ReadySolution { supports, compute, score }) + } } #[cfg(test)]