fn finalize_tally() -> Result {
		<SnapshotedStakes<T>>::kill();
		let (_, coming, expiring): (T::BlockNumber, u32, Vec<T::AccountId>) =
			<NextFinalize<T>>::take().ok_or("finalize can only be called after a tally is started.")?;
		let leaderboard: Vec<(BalanceOf<T>, T::AccountId)> = <Leaderboard<T>>::take().unwrap_or_default();
		let new_expiry = <system::Module<T>>::block_number() + Self::term_duration();

		// return bond to winners.
		let candidacy_bond = Self::candidacy_bond();
		let incoming: Vec<T::AccountId> = leaderboard.iter()
			.rev()
			.take_while(|&&(b, _)| !b.is_zero())
			.take(coming as usize)
			.map(|(_, a)| a)
			.cloned()
			.inspect(|a| {T::Currency::unreserve(a, candidacy_bond);})
			.collect();
		let active_council = Self::active_council();
		let outgoing = active_council.iter().take(expiring.len()).map(|a| a.0.clone()).collect();

		// set the new council.
		let mut new_council: Vec<_> = active_council
			.into_iter()
			.skip(expiring.len())
			.chain(incoming.iter().cloned().map(|a| (a, new_expiry)))
			.collect();
		new_council.sort_by_key(|&(_, expiry)| expiry);
		<ActiveCouncil<T>>::put(new_council);

		// clear all except runners-up from candidate list.
		let candidates = Self::candidates();
		let mut new_candidates = vec![T::AccountId::default(); candidates.len()];	// shrink later.
		let runners_up = leaderboard.into_iter()
			.rev()
			.take_while(|&(b, _)| !b.is_zero())
			.skip(coming as usize)
			.filter_map(|(_, a)| Self::candidate_reg_info(&a).map(|i| (a, i.1)));
		let mut count = 0u32;
		for (address, slot) in runners_up {
			new_candidates[slot as usize] = address;
			count += 1;
		}
		for (old, new) in candidates.iter().zip(new_candidates.iter()) {
			if old != new {
				// removed - kill it
				<RegisterInfoOf<T>>::remove(old);
			}
		}
		// discard any superfluous slots.
		if let Some(last_index) = new_candidates.iter().rposition(|c| *c != T::AccountId::default()) {
			new_candidates.truncate(last_index + 1);
		}

		Self::deposit_event(RawEvent::TallyFinalized(incoming, outgoing));

		<Candidates<T>>::put(new_candidates);
		<CandidateCount<T>>::put(count);
		<VoteCount<T>>::put(Self::vote_index() + 1);
		Ok(())
	}
/// Finalize the vote, removing each of the expiring member seats and inserting seats of the most approved
/// candidates in their place. If the total number of council members is less than the desired membership,
/// a new vote is started.
///
/// Clears all presented candidates, returning the bonds of the elected ones.
Finalize the vote, removing each of the expiring member seats and inserting seats of the most approved
candidates in their place. If the total number of council members is less than the desired membership,
a new vote is started.

Clears all presented candidates, returning the bonds of the elected ones.
Finalize the vote removing each of the expiring member seats and inserting seats of the most approved candidates in their place . If the total number of council members is less than the desired membership a new vote is started . Clears all presented candidates returning the bonds of the elected ones .
fn finalize_tally() -> Result {
		<SnapshotedStakes<T>>::kill();
		let (_, coming, expiring): (T::BlockNumber, u32, Vec<T::AccountId>) =
			<NextFinalize<T>>::take().ok_or("finalize can only be called after a tally is started.")?;
		let leaderboard: Vec<(BalanceOf<T>, T::AccountId)> = <Leaderboard<T>>::take().unwrap_or_default();
		let new_expiry = <system::Module<T>>::block_number() + Self::term_duration();
		// return bond to winners.
		let candidacy_bond = Self::candidacy_bond();
		let incoming: Vec<T::AccountId> = leaderboard.iter()
			.rev()
			.take_while(|&&(b, _)| !b.is_zero())
			.take(coming as usize)
			.map(|(_, a)| a)
			.cloned()
			.inspect(|a| {T::Currency::unreserve(a, candidacy_bond);})
			.collect();
		let active_council = Self::active_council();
		let outgoing = active_council.iter().take(expiring.len()).map(|a| a.0.clone()).collect();
		// set the new council.
		let mut new_council: Vec<_> = active_council
			.into_iter()
			.skip(expiring.len())
			.chain(incoming.iter().cloned().map(|a| (a, new_expiry)))
			.collect();
		new_council.sort_by_key(|&(_, expiry)| expiry);
		<ActiveCouncil<T>>::put(new_council);
		// clear all except runners-up from candidate list.
		let candidates = Self::candidates();
		let mut new_candidates = vec![T::AccountId::default(); candidates.len()];	// shrink later.
		let runners_up = leaderboard.into_iter()
			.rev()
			.take_while(|&(b, _)| !b.is_zero())
			.skip(coming as usize)
			.filter_map(|(_, a)| Self::candidate_reg_info(&a).map(|i| (a, i.1)));
		let mut count = 0u32;
		for (address, slot) in runners_up {
			new_candidates[slot as usize] = address;
			count += 1;
		}
		for (old, new) in candidates.iter().zip(new_candidates.iter()) {
			if old != new {
				// removed - kill it
				<RegisterInfoOf<T>>::remove(old);
			}
		}
		// discard any superfluous slots.
		if let Some(last_index) = new_candidates.iter().rposition(|c| *c != T::AccountId::default()) {
			new_candidates.truncate(last_index + 1);
		}
		Self::deposit_event(RawEvent::TallyFinalized(incoming, outgoing));
		<Candidates<T>>::put(new_candidates);
		<CandidateCount<T>>::put(count);
		<VoteCount<T>>::put(Self::vote_index() + 1);
		Ok(())
	}
fn finalize_tally ( ) -> Result { < SnapshotedStakes < T > > :: kill ( ) ; let ( _ , coming , expiring ) : ( T :: BlockNumber , u32 , Vec < T :: AccountId > ) = < NextFinalize < T > > :: take ( ) . ok_or ( " " ) ? ; let leaderboard : Vec < ( BalanceOf < T > , T :: AccountId ) > = < Leaderboard < T > > :: take ( ) . unwrap_or_default ( ) ; let new_expiry = < system :: Module < T > > :: block_number ( ) + Self :: term_duration ( ) ; // return bond to winners. let candidacy_bond = Self :: candidacy_bond ( ) ; let incoming : Vec < T :: AccountId > = leaderboard . iter ( ) . rev ( ) . take_while ( | & & ( b , _ ) | ! b . is_zero ( ) ) . take ( coming as usize ) . map ( | ( _ , a ) | a ) . cloned ( ) . inspect ( | a | { T :: Currency :: unreserve ( a , candidacy_bond ) ; } ) . collect ( ) ; let active_council = Self :: active_council ( ) ; let outgoing = active_council . iter ( ) . take ( expiring . len ( ) ) . map ( | a | a . 0 . clone ( ) ) . collect ( ) ; // set the new council. let mut new_council : Vec < _ > = active_council . into_iter ( ) . skip ( expiring . len ( ) ) . chain ( incoming . iter ( ) . cloned ( ) . map ( | a | ( a , new_expiry ) ) ) . collect ( ) ; new_council . sort_by_key ( | & ( _ , expiry ) | expiry ) ; < ActiveCouncil < T > > :: put ( new_council ) ; // clear all except runners-up from candidate list. let candidates = Self :: candidates ( ) ; let mut new_candidates = vec ! [ T AccountId default ( ) candidates len ( ) ] ; // shrink later. let runners_up = leaderboard . into_iter ( ) . rev ( ) . take_while ( | & ( b , _ ) | ! b . is_zero ( ) ) . skip ( coming as usize ) . filter_map ( | ( _ , a ) | Self :: candidate_reg_info ( & a ) . map ( | i | ( a , i . 1 ) ) ) ; let mut count = 0u32 ; for ( address , slot ) in runners_up { new_candidates [ slot as usize ] = address ; count += 1 ; } for ( old , new ) in candidates . iter ( ) . zip ( new_candidates . iter ( ) ) { if old != new { // removed - kill it < RegisterInfoOf < T > > :: remove ( old ) ; } } // discard any superfluous slots. if let Some ( last_index ) = new_candidates . iter ( ) . rposition ( | c | * c != T :: AccountId :: default ( ) ) { new_candidates . truncate ( last_index + 1 ) ; } Self :: deposit_event ( RawEvent :: TallyFinalized ( incoming , outgoing ) ) ; < Candidates < T > > :: put ( new_candidates ) ; < CandidateCount < T > > :: put ( count ) ; < VoteCount < T > > :: put ( Self :: vote_index ( ) + 1 ) ; Ok ( ( ) ) }
Finalize the vote, removing each of the expiring member seats and inserting seats of the most approved
candidates in their place.
Finalize the vote removing each of the expiring member seats and inserting seats of the most approved candidates in their place .