Repository: paritytech/substrate
File: frame/election-provider-multi-phase/src/unsigned.rs (Rust)

Code before:
				for (maybe_index, _stake) in
					voters_sorted.iter().map(|(who, stake)| (voter_index(&who), stake))
				{
					let index = maybe_index.ok_or(MinerError::SnapshotUnAvailable)?;
					if compact.remove_voter(index) {
						removed += 1
					}

					if removed >= to_remove {
						break;
					}
				}

				Ok(compact)
			}
			_ => {
				// nada, return as-is
				Ok(compact)
			}
		}
	}

	/// Greedily reduce the size of the solution to fit into the block w.r.t length.
	///
	/// The length of the solution is largely a function of the number of voters. The number of
	/// winners cannot be changed, and the `ElectionSize` is a proxy for the total number of stakers.
	///
	/// Thus, to reduce the solution size, we need to strip voters.
	///
	/// Note that this solution is already computed, and winners are elected based on the merit of
	/// the total stake in the system. Nevertheless, some of the voters may be removed here.
	///
	/// The score must be computed **after** this step. If this step reduces the score too much,
	/// then the solution must be discarded.
	pub fn trim_compact_for_length(
		max_allowed_length: u32,
		mut compact: CompactOf<T>,
		voter_index: impl Fn(&T::AccountId) -> Option<CompactVoterIndexOf<T>>,
	) -> Result<CompactOf<T>, MinerError> {
		// grab all voters and sort them by least stake.
		let RoundSnapshot { voters, .. } =
			Self::snapshot().ok_or(MinerError::SnapshotUnAvailable)?;
		let mut voters_sorted = voters
			.into_iter()
			.map(|(who, stake, _)| (who.clone(), stake))
			.collect::<Vec<_>>();
		voters_sorted.sort_by_key(|(_, y)| *y);
		voters_sorted.reverse();

		// we run through the reduction process twice: once using the size_hint function, and once
		// by actually encoding the data. Given a well-behaved size_hint implementation, this will

Review comment (suggestion):
```suggestion
	/// winners cannot be changed, and the `ElectionSize` is a proxy for the total number of stakers.
```

what is `ElectionSize`? copy pasta? :D

Code after:
			Some(to_remove) if to_remove > 0 => {
				// grab all voters and sort them by least stake.
				let RoundSnapshot { voters, .. } =
					Self::snapshot().ok_or(MinerError::SnapshotUnAvailable)?;
				let mut voters_sorted = voters
					.into_iter()
					.map(|(who, stake, _)| (who.clone(), stake))
					.collect::<Vec<_>>();
				voters_sorted.sort_by_key(|(_, y)| *y);

				// start removing from the least stake. Iterate until we know enough have been
				// removed.
				let mut removed = 0;
				for (maybe_index, _stake) in
					voters_sorted.iter().map(|(who, stake)| (voter_index(&who), stake))
				{
					let index = maybe_index.ok_or(MinerError::SnapshotUnAvailable)?;
					if compact.remove_voter(index) {
						removed += 1
					}

					if removed >= to_remove {
						break;
					}
				}

				log!(debug, "removed {} voter to meet the max weight limit.", to_remove);
				Ok(compact)
			}
			_ => {
				// nada, return as-is
				log!(debug, "didn't remove any voter for weight limits.");
				Ok(compact)
			}
		}
	}

	/// Greedily reduce the size of the solution to fit into the block w.r.t length.
	///
	/// The length of the solution is largely a function of the number of voters. The number of
	/// winners cannot be changed. Thus, to reduce the solution size, we need to strip voters.
	///
	/// Note that this solution is already computed, and winners are elected based on the merit of
	/// the total stake in the system. Nevertheless, some of the voters may be removed here.
	///
	/// Sometimes, removing a voter can cause a validator to also be implicitly removed, if
	/// that voter was the only backer of that winner. In such cases, this solution is invalid, which
	/// will be caught prior to submission.
	///
	/// The score must be computed **after** this step. If this step reduces the score too much,
	/// then the solution must be discarded.