Skip to content

Commit

Permalink
Add some verify checks to court (#1187)
Browse files Browse the repository at this point in the history
add some verify checks to court
  • Loading branch information
Chralt98 authored Nov 13, 2023
1 parent 9333393 commit 30beab9
Showing 1 changed file with 107 additions and 14 deletions.
121 changes: 107 additions & 14 deletions zrml/court/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,30 @@ benchmarks! {
let caller: T::AccountId = whitelisted_caller();
join_with_min_stake::<T>(&caller)?;

// to check that we execute the worst case benchmark path
let joined_at_before = <frame_system::Pallet<T>>::block_number();
let pool = CourtPool::<T>::get();
assert_eq!(
pool.into_inner().iter().find(|i| i.court_participant == caller).unwrap().joined_at,
joined_at_before,
);
<frame_system::Pallet<T>>::set_block_number(
joined_at_before + 1u64.saturated_into::<T::BlockNumber>(),
);

let new_stake = T::MinJurorStake::get()
.saturating_add(1u128.saturated_into::<BalanceOf<T>>());
}: _(RawOrigin::Signed(caller), new_stake)
}: _(RawOrigin::Signed(caller.clone()), new_stake)
verify {
let now = <frame_system::Pallet<T>>::block_number();
let pool = CourtPool::<T>::get();
// joined_at did not change, because it was rewritten to the newly created pool item
assert_ne!(now, joined_at_before);
assert_eq!(
pool.into_inner().iter().find(|i| i.court_participant == caller).unwrap().joined_at,
joined_at_before,
);
}

delegate {
// jurors greater or equal to MaxDelegations,
Expand All @@ -262,6 +283,17 @@ benchmarks! {
let caller: T::AccountId = whitelisted_caller();
join_with_min_stake::<T>(&caller)?;

// to check that we execute the worst case benchmark path
let joined_at_before = <frame_system::Pallet<T>>::block_number();
let pool = CourtPool::<T>::get();
assert_eq!(
pool.into_inner().iter().find(|i| i.court_participant == caller).unwrap().joined_at,
joined_at_before,
);
<frame_system::Pallet<T>>::set_block_number(
joined_at_before + 1u64.saturated_into::<T::BlockNumber>(),
);

let juror_pool = <CourtPool<T>>::get();
let mut delegations = Vec::<T::AccountId>::new();
juror_pool.iter()
Expand All @@ -270,7 +302,17 @@ benchmarks! {

let new_stake = T::MinJurorStake::get()
.saturating_add(1u128.saturated_into::<BalanceOf<T>>());
}: _(RawOrigin::Signed(caller), new_stake, delegations)
}: _(RawOrigin::Signed(caller.clone()), new_stake, delegations)
verify {
let now = <frame_system::Pallet<T>>::block_number();
let pool = CourtPool::<T>::get();
// joined_at did not change, because it was rewritten to the newly created pool item
assert_ne!(now, joined_at_before);
assert_eq!(
pool.into_inner().iter().find(|i| i.court_participant == caller).unwrap().joined_at,
joined_at_before,
);
}

prepare_exit_court {
let j in 0..(T::MaxCourtParticipants::get() - 1);
Expand All @@ -279,7 +321,23 @@ benchmarks! {

let caller: T::AccountId = whitelisted_caller();
join_with_min_stake::<T>(&caller)?;
// query pool participant to check that we execute the worst case benchmark path
let pool_participant = <CourtPool<T>>::get()
.into_inner()
.iter()
.find(|pool_item| pool_item.court_participant == caller)
.map(|pool_item| pool_item.court_participant.clone())
.unwrap();
}: _(RawOrigin::Signed(caller))
verify {
// worst case: pool participant was actually removed
assert!(
!<CourtPool<T>>::get()
.into_inner()
.iter()
.any(|pool_item| pool_item.court_participant == pool_participant)
);
}

exit_court_remove {
let caller: T::AccountId = whitelisted_caller();
Expand All @@ -294,7 +352,10 @@ benchmarks! {
});

let juror = T::Lookup::unlookup(caller.clone());
}: exit_court(RawOrigin::Signed(caller), juror)
}: exit_court(RawOrigin::Signed(caller.clone()), juror)
verify {
assert!(!<Participants<T>>::contains_key(caller));
}

exit_court_set {
let caller: T::AccountId = whitelisted_caller();
Expand All @@ -309,7 +370,10 @@ benchmarks! {
});

let juror = T::Lookup::unlookup(caller.clone());
}: exit_court(RawOrigin::Signed(caller), juror)
}: exit_court(RawOrigin::Signed(caller.clone()), juror)
verify {
assert_eq!(<Participants<T>>::get(caller).unwrap().active_lock, T::MinJurorStake::get());
}

vote {
let d in 1..T::MaxSelectedDraws::get();
Expand All @@ -333,14 +397,23 @@ benchmarks! {
weight: 1u32,
slashable: BalanceOf::<T>::zero(),
};
let index = draws.binary_search_by_key(&caller, |draw| draw.court_participant.clone()).unwrap_or_else(|j| j);
let index = draws
.binary_search_by_key(&caller, |draw| draw.court_participant.clone())
.unwrap_or_else(|j| j);
draws.try_insert(index, draw).unwrap();
<SelectedDraws<T>>::insert(court_id, draws);

<frame_system::Pallet<T>>::set_block_number(pre_vote + 1u64.saturated_into::<T::BlockNumber>());
<frame_system::Pallet<T>>::set_block_number(
pre_vote + 1u64.saturated_into::<T::BlockNumber>(),
);

let commitment_vote = Default::default();
}: _(RawOrigin::Signed(caller), court_id, commitment_vote)
}: _(RawOrigin::Signed(caller.clone()), court_id, commitment_vote)
verify {
let draws = <SelectedDraws<T>>::get(court_id);
let draw = draws.iter().find(|draw| draw.court_participant == caller).unwrap();
assert_eq!(draw.vote, Vote::Secret { commitment: commitment_vote });
}

denounce_vote {
let d in 1..T::MaxSelectedDraws::get();
Expand Down Expand Up @@ -373,18 +446,27 @@ benchmarks! {
let mut draws = <SelectedDraws<T>>::get(court_id);
draws.remove(0);
let draws_len = draws.len();
let index = draws.binary_search_by_key(&denounced_juror, |draw| draw.court_participant.clone()).unwrap_or_else(|j| j);
let index = draws
.binary_search_by_key(&denounced_juror, |draw| draw.court_participant.clone())
.unwrap_or_else(|j| j);
let draw = Draw {
court_participant: denounced_juror,
court_participant: denounced_juror.clone(),
vote: Vote::Secret { commitment },
weight: 1u32,
slashable: T::MinJurorStake::get(),
};
draws.try_insert(index, draw).unwrap();
<SelectedDraws<T>>::insert(court_id, draws);

<frame_system::Pallet<T>>::set_block_number(pre_vote + 1u64.saturated_into::<T::BlockNumber>());
}: _(RawOrigin::Signed(caller), court_id, denounced_juror_unlookup, vote_item, salt)
<frame_system::Pallet<T>>::set_block_number(
pre_vote + 1u64.saturated_into::<T::BlockNumber>(),
);
}: _(RawOrigin::Signed(caller), court_id, denounced_juror_unlookup, vote_item.clone(), salt)
verify {
let draws = <SelectedDraws<T>>::get(court_id);
let draw = draws.iter().find(|draw| draw.court_participant == denounced_juror).unwrap();
assert_eq!(draw.vote, Vote::Denounced { commitment, vote_item, salt });
}

reveal_vote {
let d in 1..T::MaxSelectedDraws::get();
Expand Down Expand Up @@ -414,7 +496,9 @@ benchmarks! {
let mut draws = <SelectedDraws<T>>::get(court_id);
let draws_len = draws.len();
draws.remove(0);
let index = draws.binary_search_by_key(&caller, |draw| draw.court_participant.clone()).unwrap_or_else(|j| j);
let index = draws
.binary_search_by_key(&caller, |draw| draw.court_participant.clone())
.unwrap_or_else(|j| j);
draws.try_insert(index, Draw {
court_participant: caller.clone(),
vote: Vote::Secret { commitment },
Expand All @@ -423,8 +507,15 @@ benchmarks! {
}).unwrap();
<SelectedDraws<T>>::insert(court_id, draws);

<frame_system::Pallet<T>>::set_block_number(vote_end + 1u64.saturated_into::<T::BlockNumber>());
}: _(RawOrigin::Signed(caller), court_id, vote_item, salt)
<frame_system::Pallet<T>>::set_block_number(
vote_end + 1u64.saturated_into::<T::BlockNumber>()
);
}: _(RawOrigin::Signed(caller.clone()), court_id, vote_item.clone(), salt)
verify {
let draws = <SelectedDraws<T>>::get(court_id);
let draw = draws.iter().find(|draw| draw.court_participant == caller).unwrap();
assert_eq!(draw.vote, Vote::Revealed { commitment, vote_item, salt });
}

appeal {
// from 255 because in the last appeal round we need at least 255 jurors
Expand Down Expand Up @@ -505,6 +596,8 @@ benchmarks! {
verify {
let court = <Courts<T>>::get(court_id).unwrap();
assert_eq!(court.round_ends.appeal, new_resolve_at);
assert!(T::DisputeResolution::auto_resolve_exists(&market_id, new_resolve_at));
assert!(!T::DisputeResolution::auto_resolve_exists(&market_id, appeal_end));
}

reassign_court_stakes {
Expand Down

0 comments on commit 30beab9

Please sign in to comment.