Skip to content

Commit

Permalink
tests and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
kylezs committed Oct 23, 2024
1 parent 0105b4e commit a33858b
Show file tree
Hide file tree
Showing 14 changed files with 578 additions and 385 deletions.
58 changes: 33 additions & 25 deletions state-chain/pallets/cf-elections/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
bitmap_components::ElectionBitmapComponents,
electoral_system::{
AuthorityVoteOf, ElectionIdentifierOf, ElectoralSystem, IndividualComponentOf,
VotePropertiesOf,
electoral_system_runner::{
CompositeAuthorityVoteOf, CompositeIndividualComponentOf, CompositeVotePropertiesOf,
ElectoralSystemRunner,
},
vote_storage::VoteStorage,
*,
Expand All @@ -25,13 +25,13 @@ use crate::Call;
#[allow(clippy::multiple_bound_locations)]
#[instance_benchmarks(
where
<<<T as Config<I>>::ElectoralSystem as ElectoralSystem>::Vote as VoteStorage>::Vote: BenchmarkValue,
<<<T as Config<I>>::ElectoralSystem as ElectoralSystem>::Vote as VoteStorage>::SharedData: BenchmarkValue,
<<<T as Config<I>>::ElectoralSystem as ElectoralSystem>::Vote as VoteStorage>::Properties: BenchmarkValue,
<<<T as Config<I>>::ElectoralSystem as ElectoralSystem>::Vote as VoteStorage>::IndividualComponent: BenchmarkValue,
<<<T as Config<I>>::ElectoralSystemRunner as ElectoralSystemRunner>::Vote as VoteStorage>::Vote: BenchmarkValue,
<<<T as Config<I>>::ElectoralSystemRunner as ElectoralSystemRunner>::Vote as VoteStorage>::SharedData: BenchmarkValue,
<<<T as Config<I>>::ElectoralSystemRunner as ElectoralSystemRunner>::Vote as VoteStorage>::Properties: BenchmarkValue,
<<<T as Config<I>>::ElectoralSystemRunner as ElectoralSystemRunner>::Vote as VoteStorage>::IndividualComponent: BenchmarkValue,
InitialStateOf<T, I>: BenchmarkValue,
<T::ElectoralSystem as ElectoralSystem>::ElectoralUnsynchronisedSettings: BenchmarkValue,
<T::ElectoralSystem as ElectoralSystem>::ElectoralSettings: BenchmarkValue,
<T::ElectoralSystemRunner as ElectoralSystemRunner>::ElectoralUnsynchronisedSettings: BenchmarkValue,
<T::ElectoralSystemRunner as ElectoralSystemRunner>::ElectoralSettings: BenchmarkValue,
)]
mod benchmarks {
use super::*;
Expand Down Expand Up @@ -64,8 +64,8 @@ mod benchmarks {

fn setup_validators_and_vote<T: crate::pallet::Config<I>, I: 'static>(
validator_counts: u32,
vote_value: <<<T as Config<I>>::ElectoralSystem as ElectoralSystem>::Vote as VoteStorage>::Vote,
) -> ElectionIdentifierOf<T::ElectoralSystem> {
vote_value: <<<T as Config<I>>::ElectoralSystemRunner as ElectoralSystemRunner>::Vote as VoteStorage>::Vote,
) -> CompositeElectionIdentifierOf<T::ElectoralSystemRunner> {
// Setup a validator set of 150 as in the case of Mainnet.
let validators = ready_validator_for_vote::<T, I>(validator_counts);
let caller = validators[0].clone();
Expand All @@ -82,7 +82,9 @@ mod benchmarks {
BoundedBTreeMap::try_from(
[(
election_identifier,
AuthorityVoteOf::<T::ElectoralSystem>::Vote(vote_value.clone()),
CompositeAuthorityVoteOf::<T::ElectoralSystemRunner>::Vote(
vote_value.clone()
),
)]
.into_iter()
.collect::<BTreeMap<_, _>>(),
Expand All @@ -106,7 +108,9 @@ mod benchmarks {
BoundedBTreeMap::try_from(
iter::repeat((
next_election.0,
AuthorityVoteOf::<T::ElectoralSystem>::Vote(BenchmarkValue::benchmark_value()),
CompositeAuthorityVoteOf::<T::ElectoralSystemRunner>::Vote(
BenchmarkValue::benchmark_value(),
),
))
.take(n as usize)
.collect::<BTreeMap<_, _>>(),
Expand Down Expand Up @@ -166,7 +170,9 @@ mod benchmarks {
BoundedBTreeMap::try_from(
[(
next_election.0,
AuthorityVoteOf::<T::ElectoralSystem>::Vote(BenchmarkValue::benchmark_value()),
CompositeAuthorityVoteOf::<T::ElectoralSystemRunner>::Vote(
BenchmarkValue::benchmark_value()
),
)]
.into_iter()
.collect::<BTreeMap<_, _>>(),
Expand Down Expand Up @@ -201,7 +207,9 @@ mod benchmarks {
BoundedBTreeMap::try_from(
[(
next_election.0,
AuthorityVoteOf::<T::ElectoralSystem>::Vote(BenchmarkValue::benchmark_value()),
CompositeAuthorityVoteOf::<T::ElectoralSystemRunner>::Vote(
BenchmarkValue::benchmark_value()
),
)]
.into_iter()
.collect::<BTreeMap<_, _>>(),
Expand Down Expand Up @@ -239,7 +247,9 @@ mod benchmarks {
BoundedBTreeMap::try_from(
[(
election_identifier,
AuthorityVoteOf::<T::ElectoralSystem>::Vote(BenchmarkValue::benchmark_value()),
CompositeAuthorityVoteOf::<T::ElectoralSystemRunner>::Vote(
BenchmarkValue::benchmark_value()
),
)]
.into_iter()
.collect::<BTreeMap<_, _>>(),
Expand All @@ -252,7 +262,7 @@ mod benchmarks {

assert_eq!(
SharedData::<T, I>::get(SharedDataHash::of::<
<<<T as Config<I>>::ElectoralSystem as ElectoralSystem>::Vote as VoteStorage>::Vote,
<<<T as Config<I>>::ElectoralSystemRunner as ElectoralSystemRunner>::Vote as VoteStorage>::Vote,
>(&BenchmarkValue::benchmark_value())),
Some(BenchmarkValue::benchmark_value())
);
Expand Down Expand Up @@ -351,8 +361,7 @@ mod benchmarks {
}

assert!(!ElectionConsensusHistoryUpToDate::<T, I>::contains_key(
access_impls::ElectionAccess::<T, I>::new(election_identifier)
.unique_monotonic_identifier()
election_identifier.unique_monotonic()
));
}

Expand All @@ -369,8 +378,7 @@ mod benchmarks {
};

let epoch = T::EpochInfo::epoch_index();
let monotonic_identifier = access_impls::ElectionAccess::<T, I>::new(election_identifier)
.unique_monotonic_identifier();
let monotonic_identifier = election_identifier.unique_monotonic();

Pallet::<T, I>::on_finalize(frame_system::Pallet::<T>::block_number());
assert_eq!(
Expand Down Expand Up @@ -489,7 +497,7 @@ mod benchmarks {
(0..b).for_each(|i| {
SharedData::<T, I>::insert(
SharedDataHash::of(&i),
<<T::ElectoralSystem as ElectoralSystem>::Vote as VoteStorage>::SharedData::benchmark_value());
<<T::ElectoralSystemRunner as ElectoralSystemRunner>::Vote as VoteStorage>::SharedData::benchmark_value());
});

(0..c).for_each(|i| {
Expand All @@ -506,8 +514,8 @@ mod benchmarks {
UniqueMonotonicIdentifier::from_u64(i as u64),
T::ValidatorId::from(validators[i as usize].clone()),
(
VotePropertiesOf::<T::ElectoralSystem>::benchmark_value(),
IndividualComponentOf::<T::ElectoralSystem>::benchmark_value(),
CompositeVotePropertiesOf::<T::ElectoralSystemRunner>::benchmark_value(),
CompositeIndividualComponentOf::<T::ElectoralSystemRunner>::benchmark_value(),
),
);
});
Expand Down Expand Up @@ -538,7 +546,7 @@ mod benchmarks {
mod tests {
use super::*;

use crate::{mock::*, tests::ElectoralSystemTestExt, Instance1};
use crate::{mock::*, tests::ElectoralSystemRunnerTestExt, Instance1};

macro_rules! benchmark_tests {
( $( $test_name:ident: $test_fn:ident ( $( $arg:expr ),* ) ),+ $(,)? ) => {
Expand Down
5 changes: 4 additions & 1 deletion state-chain/pallets/cf-elections/src/electoral_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,12 @@ mod access {
use super::{CorruptStorageError, ElectionIdentifierOf, ElectoralSystem};

#[cfg(test)]
use codec::{Decode, Encode};

/// Represents the current consensus, and how it has changed since it was last checked (i.e.
/// 'check_consensus' was called).
#[cfg_attr(test, derive(Clone, Debug, PartialEq, Eq))]
#[cfg_attr(test, derive(Clone, Debug, PartialEq, Eq, Encode, Decode))]
pub enum ConsensusStatus<Consensus> {
/// You did not have consensus when previously checked, but now consensus has been gained.
Gained {
Expand Down
28 changes: 19 additions & 9 deletions state-chain/pallets/cf-elections/src/electoral_system_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,40 @@ pub type CompositeElectionIdentifierOf<E: ElectoralSystemRunner> =
ElectionIdentifier<<E as ElectoralSystemRunner>::ElectionIdentifierExtra>;

#[allow(type_alias_bounds)]
pub type AuthorityVoteOf<E: ElectoralSystemRunner> = AuthorityVote<
pub type CompositeAuthorityVoteOf<E: ElectoralSystemRunner> = AuthorityVote<
<<E as ElectoralSystemRunner>::Vote as VoteStorage>::PartialVote,
<<E as ElectoralSystemRunner>::Vote as VoteStorage>::Vote,
>;
#[allow(type_alias_bounds)]
pub type IndividualComponentOf<E: ElectoralSystemRunner> =
pub type CompositeIndividualComponentOf<E: ElectoralSystemRunner> =
<<E as ElectoralSystemRunner>::Vote as VoteStorage>::IndividualComponent;
#[allow(type_alias_bounds)]
pub type BitmapComponentOf<E: ElectoralSystemRunner> =
<<E as ElectoralSystemRunner>::Vote as VoteStorage>::BitmapComponent;
#[allow(type_alias_bounds)]
pub type VotePropertiesOf<E: ElectoralSystemRunner> =
pub type CompositeVotePropertiesOf<E: ElectoralSystemRunner> =
<<E as ElectoralSystemRunner>::Vote as VoteStorage>::Properties;

pub struct CompositeConsensusVote<ES: ElectoralSystemRunner> {
// If the validator hasn't voted, they will get a None.
pub vote: Option<(VotePropertiesOf<ES>, <ES::Vote as VoteStorage>::Vote)>,
pub vote: Option<(CompositeVotePropertiesOf<ES>, <ES::Vote as VoteStorage>::Vote)>,
pub validator_id: ES::ValidatorId,
}

pub struct CompositeConsensusVotes<ES: ElectoralSystemRunner> {
pub votes: Vec<CompositeConsensusVote<ES>>,
}

#[cfg(test)]
impl<ES: ElectoralSystemRunner> CompositeConsensusVotes<ES> {
pub fn active_votes(self) -> Vec<<ES::Vote as VoteStorage>::Vote> {
self.votes
.into_iter()
.filter_map(|CompositeConsensusVote { vote, .. }| vote.map(|v| v.1))
.collect()
}
}

pub trait ElectoralSystemRunner: 'static + Sized {
type ValidatorId: Parameter + Member + MaybeSerializeDeserialize;

Expand Down Expand Up @@ -107,7 +117,7 @@ pub trait ElectoralSystemRunner: 'static + Sized {
/// per state-chain block, for each active election.
fn is_vote_desired(
_election_identifier_with_extra: CompositeElectionIdentifierOf<Self>,
current_vote: Option<(VotePropertiesOf<Self>, AuthorityVoteOf<Self>)>,
current_vote: Option<(CompositeVotePropertiesOf<Self>, CompositeAuthorityVoteOf<Self>)>,
) -> Result<bool, CorruptStorageError> {
Ok(current_vote.is_none())
}
Expand All @@ -116,9 +126,9 @@ pub trait ElectoralSystemRunner: 'static + Sized {
/// This is a way to decrease the amount of extrinsics a validator needs to send.
fn is_vote_needed(
_current_vote: (
VotePropertiesOf<Self>,
CompositeVotePropertiesOf<Self>,
<Self::Vote as VoteStorage>::PartialVote,
AuthorityVoteOf<Self>,
CompositeAuthorityVoteOf<Self>,
),
_proposed_vote: (
<Self::Vote as VoteStorage>::PartialVote,
Expand Down Expand Up @@ -156,9 +166,9 @@ pub trait ElectoralSystemRunner: 'static + Sized {
/// function.
fn generate_vote_properties(
election_identifier: CompositeElectionIdentifierOf<Self>,
previous_vote: Option<(VotePropertiesOf<Self>, AuthorityVoteOf<Self>)>,
previous_vote: Option<(CompositeVotePropertiesOf<Self>, CompositeAuthorityVoteOf<Self>)>,
vote: &<Self::Vote as VoteStorage>::PartialVote,
) -> Result<VotePropertiesOf<Self>, CorruptStorageError>;
) -> Result<CompositeVotePropertiesOf<Self>, CorruptStorageError>;

/// This is called during the pallet's `on_finalize` callback, if elections aren't paused and
/// the CorruptStorage error hasn't occurred.
Expand Down
Loading

0 comments on commit a33858b

Please sign in to comment.