pub(crate) fn prepare_submission_with(
	compute_real_score: bool,
	do_reduce: bool,
	iterations: usize,
	tweak: impl FnOnce(&mut Vec<StakedAssignment<AccountId>>),
) -> (CompactAssignments, Vec<ValidatorIndex>, ElectionScore) {
	// run election on the default stuff.
	let sp_npos_elections::ElectionResult {
		winners,
		assignments,
	} = Staking::do_phragmen::<OffchainAccuracy>(iterations).unwrap();
	let winners = sp_npos_elections::to_without_backing(winners);

	let mut staked = sp_npos_elections::assignment_ratio_to_staked(
		assignments,
		Staking::slashable_balance_of_fn(),
	);

	// apply custom tweaks. awesome for testing.
	tweak(&mut staked);

	if do_reduce {
		reduce(&mut staked);
	}

	// convert back to ratio assignment. This takes less space.
	let snapshot_validators = Staking::snapshot_validators().expect("snapshot not created.");
	let snapshot_nominators = Staking::snapshot_nominators().expect("snapshot not created.");
	let nominator_index = |a: &AccountId| -> Option<NominatorIndex> {
		snapshot_nominators
			.iter()
			.position(|x| x == a)
			.map_or_else(
				|| { println!("unable to find nominator index for {:?}", a); None },
				|i| Some(i as NominatorIndex),
			)
	};
	let validator_index = |a: &AccountId| -> Option<ValidatorIndex> {
		snapshot_validators
			.iter()
			.position(|x| x == a)
			.map_or_else(
				|| { println!("unable to find validator index for {:?}", a); None },
				|i| Some(i as ValidatorIndex),
			)
	};

	let assignments_reduced = sp_npos_elections::assignment_staked_to_ratio(staked);

	// re-compute score by converting, yet again, into staked type
	let score = if compute_real_score {
		let staked = sp_npos_elections::assignment_ratio_to_staked(
			assignments_reduced.clone(),
			Staking::slashable_balance_of_fn(),
		);

		let support_map = build_support_map::<AccountId>(
			winners.as_slice(),
			staked.as_slice(),
		).unwrap();
		evaluate_support::<AccountId>(&support_map)
	} else {
		Default::default()
	};

	let compact =
		CompactAssignments::from_assignment(assignments_reduced, nominator_index, validator_index)
			.expect("Failed to create compact");

	// winner ids to index
	let winners = winners.into_iter().map(|w| validator_index(&w).unwrap()).collect::<Vec<_>>();

	(compact, winners, score)
}
/// Note: this should always logically reproduce [`offchain_election::prepare_submission`], yet we
/// cannot do it since we want to have `tweak` injected into the process.
///
/// If the input is being tweaked in a way that the score cannot be compute accurately,
/// `compute_real_score` can be set to true. In this case a `Default` score is returned.
this should always logically reproduce [`offchain_election::prepare_submission`], yet we
cannot do it since we want to have `tweak` injected into the process.

If the input is being tweaked in a way that the score cannot be compute accurately,
`compute_real_score` can be set to true. In this case a `Default` score is returned.
this should always logically reproduce [ ` offchain_election :: prepare_submission ` ] yet we cannot do it since we want to have ` tweak ` injected into the process . If the input is being tweaked in a way that the score cannot be compute accurately ` compute_real_score ` can be set to true . In this case a ` Default ` score is returned .
pub(crate) fn prepare_submission_with(
	compute_real_score: bool,
	do_reduce: bool,
	iterations: usize,
	tweak: impl FnOnce(&mut Vec<StakedAssignment<AccountId>>),
) -> (CompactAssignments, Vec<ValidatorIndex>, ElectionScore) {
	// run election on the default stuff.
	let sp_npos_elections::ElectionResult {
		winners,
		assignments,
	} = Staking::do_phragmen::<OffchainAccuracy>(iterations).unwrap();
	let winners = sp_npos_elections::to_without_backing(winners);
	let mut staked = sp_npos_elections::assignment_ratio_to_staked(
		assignments,
		Staking::slashable_balance_of_fn(),
	);
	// apply custom tweaks. awesome for testing.
	tweak(&mut staked);
	if do_reduce {
		reduce(&mut staked);
	}
	// convert back to ratio assignment. This takes less space.
	let snapshot_validators = Staking::snapshot_validators().expect("snapshot not created.");
	let snapshot_nominators = Staking::snapshot_nominators().expect("snapshot not created.");
	let nominator_index = |a: &AccountId| -> Option<NominatorIndex> {
		snapshot_nominators
			.iter()
			.position(|x| x == a)
			.map_or_else(
				|| { println!("unable to find nominator index for {:?}", a); None },
				|i| Some(i as NominatorIndex),
			)
	};
	let validator_index = |a: &AccountId| -> Option<ValidatorIndex> {
		snapshot_validators
			.iter()
			.position(|x| x == a)
			.map_or_else(
				|| { println!("unable to find validator index for {:?}", a); None },
				|i| Some(i as ValidatorIndex),
			)
	};
	let assignments_reduced = sp_npos_elections::assignment_staked_to_ratio(staked);
	// re-compute score by converting, yet again, into staked type
	let score = if compute_real_score {
		let staked = sp_npos_elections::assignment_ratio_to_staked(
			assignments_reduced.clone(),
			Staking::slashable_balance_of_fn(),
		);
		let support_map = build_support_map::<AccountId>(
			winners.as_slice(),
			staked.as_slice(),
		).unwrap();
		evaluate_support::<AccountId>(&support_map)
	} else {
		Default::default()
	};
	let compact =
		CompactAssignments::from_assignment(assignments_reduced, nominator_index, validator_index)
			.expect("Failed to create compact");
	// winner ids to index
	let winners = winners.into_iter().map(|w| validator_index(&w).unwrap()).collect::<Vec<_>>();
	(compact, winners, score)
}
pub ( crate ) fn prepare_submission_with ( compute_real_score : bool , do_reduce : bool , iterations : usize , tweak : impl FnOnce ( & mut Vec < StakedAssignment < AccountId > > ) , ) -> ( CompactAssignments , Vec < ValidatorIndex > , ElectionScore ) { // run election on the default stuff. let sp_npos_elections :: ElectionResult { winners , assignments , } = Staking :: do_phragmen :: < OffchainAccuracy > ( iterations ) . unwrap ( ) ; let winners = sp_npos_elections :: to_without_backing ( winners ) ; let mut staked = sp_npos_elections :: assignment_ratio_to_staked ( assignments , Staking :: slashable_balance_of_fn ( ) , ) ; // apply custom tweaks. awesome for testing. tweak ( & mut staked ) ; if do_reduce { reduce ( & mut staked ) ; } // convert back to ratio assignment. This takes less space. let snapshot_validators = Staking :: snapshot_validators ( ) . expect ( " " ) ; let snapshot_nominators = Staking :: snapshot_nominators ( ) . expect ( " " ) ; let nominator_index = | a : & AccountId | -> Option < NominatorIndex > { snapshot_nominators . iter ( ) . position ( | x | x == a ) . map_or_else ( | | { println ! ( " " a ) ; None } , | i | Some ( i as NominatorIndex ) , ) } ; let validator_index = | a : & AccountId | -> Option < ValidatorIndex > { snapshot_validators . iter ( ) . position ( | x | x == a ) . map_or_else ( | | { println ! ( " " a ) ; None } , | i | Some ( i as ValidatorIndex ) , ) } ; let assignments_reduced = sp_npos_elections :: assignment_staked_to_ratio ( staked ) ; // re-compute score by converting, yet again, into staked type let score = if compute_real_score { let staked = sp_npos_elections :: assignment_ratio_to_staked ( assignments_reduced . clone ( ) , Staking :: slashable_balance_of_fn ( ) , ) ; let support_map = build_support_map :: < AccountId > ( winners . as_slice ( ) , staked . as_slice ( ) , ) . unwrap ( ) ; evaluate_support :: < AccountId > ( & support_map ) } else { Default :: default ( ) } ; let compact = CompactAssignments :: from_assignment ( assignments_reduced , nominator_index , validator_index ) . expect ( " " ) ; // winner ids to index let winners = winners . into_iter ( ) . map ( | w | validator_index ( & w ) . unwrap ( ) ) . collect :: < Vec < _ > > ( ) ; ( compact , winners , score ) }
Note: this should always logically reproduce [`offchain_election::prepare_submission`], yet we
cannot do it since we want to have `tweak` injected into the process.
Note : this should always logically reproduce [ ` offchain_election :: prepare_submission ` ] yet we cannot do it since we want to have ` tweak ` injected into the process .
compute_real_score
bool
do_reduce
bool
iterations
usize
tweak
impl FnOnce(&mut Vec<StakedAssignment<AccountId>>)
compute_real_score
bool
do_reduce
bool
iterations
usize
tweak
impl FnOnce(&mut Vec<StakedAssignment<AccountId>>)