From 038c6e66098faf8b62aa92a3f67e9b3847ea7f3d Mon Sep 17 00:00:00 2001 From: Ramiz Siddiqui Date: Tue, 17 Sep 2024 11:39:38 +0200 Subject: [PATCH] wip --- state-chain/pallets/cf-elections/src/lib.rs | 218 +++++++++++++------- 1 file changed, 140 insertions(+), 78 deletions(-) diff --git a/state-chain/pallets/cf-elections/src/lib.rs b/state-chain/pallets/cf-elections/src/lib.rs index 553e49c204..ca38340d65 100644 --- a/state-chain/pallets/cf-elections/src/lib.rs +++ b/state-chain/pallets/cf-elections/src/lib.rs @@ -130,6 +130,9 @@ pub use pallet::UniqueMonotonicIdentifier; #[frame_support::pallet] pub mod pallet { + use core::iter::Map; + use sp_std::vec::IntoIter; + use super::*; use cf_primitives::{AuthorityCount, EpochIndex}; @@ -1825,6 +1828,114 @@ pub mod pallet { <::Vote as VoteStorage>::Vote, >, ) -> BTreeSet> { + Self::for_each_election::<(ElectionIdentifierOf, bool), _, _, _, _>( + validator_id, + |electoral_access, + election_identifier, + option_current_authority_vote, + contains_timed_out_shared_data_references| { + Ok(( + election_identifier, + if let Some(proposed_vote) = proposed_votes.get(&election_identifier) { + if let Some((existing_vote_properties, existing_authority_vote)) = + option_current_authority_vote + .filter(|_| !contains_timed_out_shared_data_references) + { + ::is_vote_needed( + ( + existing_vote_properties, + match &existing_authority_vote { + AuthorityVote::Vote(existing_vote) => { + <::Vote as VoteStorage>::vote_into_partial_vote( + existing_vote, + |shared_data| SharedDataHash::of(&shared_data) + ) + }, + AuthorityVote::PartialVote(existing_partial_vote) => existing_partial_vote.clone(), + }, + existing_authority_vote, + ), + ( + <::Vote as VoteStorage>::vote_into_partial_vote( + proposed_vote, + |shared_data| SharedDataHash::of(&shared_data) + ), + proposed_vote.clone(), + ), + ) + } else { + true + } + } else { + false + }, + )) + }, + |electoral_access, all| { + all.filter_ok(|(_election_identifier, needed)| *needed) + .map_ok(|(election_identifier, _needed)| (election_identifier)) + .collect::, _>>() + }, + ) + .map(|r| r.unwrap_or_default()) + .unwrap_or_default() + } + + fn get_vote_with_timed_out_data_references( + epoch_index: EpochIndex, + unique_monotonic_identifier: UniqueMonotonicIdentifier, + authority: &T::ValidatorId, + authority_index: AuthorityCount, + block_number: BlockNumberFor, + ) -> Result< + ( + Option<(VotePropertiesOf, AuthorityVoteOf)>, + bool, + ), + CorruptStorageError, + > { + let mut contains_timed_out_shared_data_references = false; + Pallet::::get_vote( + epoch_index, + unique_monotonic_identifier, + authority, + authority_index, + |unprovided_shared_data_hash| { + let option_reference_details = SharedDataReferenceCount::::get( + unprovided_shared_data_hash, + unique_monotonic_identifier, + ); + if option_reference_details.is_none() || + option_reference_details.unwrap().expires < block_number + { + contains_timed_out_shared_data_references = true; + } + }, + ) + .map(|option_current_authority_vote| { + (option_current_authority_vote, contains_timed_out_shared_data_references) + }) + } + + fn for_each_election< + R, + S, + Q: FnMut(ElectionIdentifierOf) -> Result, + OnEachElection: FnOnce( + &mut ElectoralAccess, + ElectionIdentifierOf, + Option<(VotePropertiesOf, AuthorityVoteOf)>, + bool, + ) -> Result, + OnAllElectionsIterable: FnOnce( + &mut ElectoralAccess, + Map>, Q>, + ) -> Result, + >( + validator_id: &T::ValidatorId, + on_each_election: OnEachElection, + on_all_elections_iterable: OnAllElectionsIterable, + ) -> Option> { use frame_support::traits::OriginTrait; if let Ok((epoch_index, authority, authority_index)) = @@ -1832,86 +1943,37 @@ pub mod pallet { { let block_number = frame_system::Pallet::::current_block_number(); - Self::with_electoral_access_and_identifiers( - |_electoral_access, election_identifiers| { - election_identifiers - .into_iter() - .map(|election_identifier| { - Ok(( - election_identifier, - if let Some(proposed_vote) = - proposed_votes.get(&election_identifier) - { - let unique_monotonic_identifier = - *election_identifier.unique_monotonic(); - - let mut contains_timed_out_shared_data_references = false; - let option_current_authority_vote = - Pallet::::get_vote( - epoch_index, - unique_monotonic_identifier, - &authority, - authority_index, - |unprovided_shared_data_hash| { - let option_reference_details = - SharedDataReferenceCount::::get( - unprovided_shared_data_hash, - unique_monotonic_identifier, - ); - if option_reference_details.is_none() || - option_reference_details.unwrap().expires < - block_number - { - contains_timed_out_shared_data_references = - true; - } - }, - )?; - - if let Some(( - existing_vote_properties, - existing_authority_vote, - )) = option_current_authority_vote - .filter(|_| !contains_timed_out_shared_data_references) - { - ::is_vote_needed( - ( - existing_vote_properties, - match &existing_authority_vote { - AuthorityVote::Vote(existing_vote) => { - <::Vote as VoteStorage>::vote_into_partial_vote( - existing_vote, - |shared_data| SharedDataHash::of(&shared_data) - ) - }, - AuthorityVote::PartialVote(existing_partial_vote) => existing_partial_vote.clone(), - }, - existing_authority_vote, - ), - ( - <::Vote as VoteStorage>::vote_into_partial_vote( - proposed_vote, - |shared_data| SharedDataHash::of(&shared_data) - ), - proposed_vote.clone(), - ), - ) - } else { - true - } - } else { - false - }, - )) - }) - .filter_ok(|(_election_identifier, needed)| *needed) - .map_ok(|(election_identifier, _needed)| (election_identifier)) - .collect::, _>>() + Some(Self::with_electoral_access_and_identifiers( + |electoral_access, election_identifiers| { + on_all_elections_iterable( + electoral_access, + election_identifiers.into_iter().map( + |election_identifier: ElectionIdentifierOf| { + let unique_monotonic_identifier = + *election_identifier.unique_monotonic(); + let ( + option_current_authority_vote, + contains_timed_out_shared_data_references, + ) = Self::get_vote_with_timed_out_data_references( + epoch_index, + unique_monotonic_identifier, + &authority, + authority_index, + block_number, + )?; + on_each_election( + electoral_access, + election_identifier, + option_current_authority_vote, + contains_timed_out_shared_data_references, + ) + }, + ), + ) }, - ) - .unwrap_or_default() + )) } else { - Default::default() + None } }