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

Code before:

		let ElectionResult { assignments, winners } = election_result;

		// convert to staked and reduce.
		let mut staked = assignment_ratio_to_staked_normalized(assignments, &stake_of)
			.map_err::<MinerError, _>(Into::into)?;
		sp_npos_elections::reduce(&mut staked);

		// convert back to ration and make compact.
		let ratio = assignment_staked_to_ratio_normalized(staked)?;
		let compact = <CompactOf<T>>::from_assignment(ratio, &voter_index, &target_index)?;

		let size =
			SolutionOrSnapshotSize { voters: voters.len() as u32, targets: targets.len() as u32 };
		let maximum_allowed_voters = Self::maximum_voter_for_weight::<T::WeightInfo>(
			desired_targets,
			size,
			T::MinerMaxWeight::get(),
		);
		log!(
			debug,
			"miner: current compact solution voters = {}, maximum_allowed = {}",
			compact.voter_count(),
			maximum_allowed_voters,
		);
		let compact = Self::trim_compact(maximum_allowed_voters, compact, &voter_index)?;
		let compact = Self::trim_compact_for_length(
			T::MinerMaxLength::get(),
			compact,
			&voter_index,
		)?;

		// re-calc score.
		let winners = sp_npos_elections::to_without_backing(winners);
		let score = compact.clone().score(&winners, stake_of, voter_at, target_at)?;

		let round = Self::round();
		Ok((RawSolution { compact, score, round }, size))
	}

	/// Get a random number of iterations to run the balancing in the OCW.
	///
	/// Uses the offchain seed to generate a random number, maxed with
	/// [`Config::MinerMaxIterations`].
	pub fn get_balancing_iters() -> usize {
		match T::MinerMaxIterations::get() {
			0 => 0,
			max @ _ => {
				let seed = sp_io::offchain::random_seed();
				let random = <u32>::decode(&mut TrailingZeroInput::new(seed.as_ref()))
					.expect("input is padded with zeroes; qed")

Review comment (suggestion):
```suggestion
		let compact = Self::trim_compact_weight(maximum_allowed_voters, compact, &voter_index)?;
```

Code after:
		let desired_targets = Self::desired_targets().ok_or(MinerError::SnapshotUnAvailable)?;

		// closures.
		let cache = helpers::generate_voter_cache::<T>(&voters);
		let voter_index = helpers::voter_index_fn::<T>(&cache);
		let target_index = helpers::target_index_fn::<T>(&targets);
		let voter_at = helpers::voter_at_fn::<T>(&voters);
		let target_at = helpers::target_at_fn::<T>(&targets);
		let stake_of = helpers::stake_of_fn::<T>(&voters, &cache);

		let ElectionResult { assignments, winners } = election_result;

		// convert to staked and reduce.
		let mut staked = assignment_ratio_to_staked_normalized(assignments, &stake_of)
			.map_err::<MinerError, _>(Into::into)?;
		sp_npos_elections::reduce(&mut staked);

		// convert back to ration and make compact.
		let ratio = assignment_staked_to_ratio_normalized(staked)?;
		let compact = <CompactOf<T>>::from_assignment(ratio, &voter_index, &target_index)?;

		let size =
			SolutionOrSnapshotSize { voters: voters.len() as u32, targets: targets.len() as u32 };
		let maximum_allowed_voters = Self::maximum_voter_for_weight::<T::WeightInfo>(
			desired_targets,
			size,
			T::MinerMaxWeight::get(),
		);

		log!(
			debug,
			"initial solution voters = {}, snapshot = {:?}, maximum_allowed(capped) = {}",
			compact.voter_count(),
			size,
			maximum_allowed_voters,
		);

		// trim length and weight
		let compact = Self::trim_compact_weight(maximum_allowed_voters, compact, &voter_index)?;
		let compact = Self::trim_compact_length(
			T::MinerMaxLength::get(),
			compact,
			&voter_index,
		)?;

		// re-calc score.
		let winners = sp_npos_elections::to_without_backing(winners);
		let score = compact.clone().score(&winners, stake_of, voter_at, target_at)?;

		let round = Self::round();
		Ok((RawSolution { compact, score, round }, size))