Skip to content

Commit

Permalink
remove event OutcomeOwnersRewardedWithNoFunds
Browse files Browse the repository at this point in the history
  • Loading branch information
Chralt98 committed Dec 20, 2022
1 parent 69ae96c commit 8d6bcac
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 101 deletions.
37 changes: 0 additions & 37 deletions zrml/global-disputes/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,43 +287,6 @@ benchmarks! {
assert!(T::Currency::free_balance(&reward_account) == 0u128.saturated_into());
}

reward_outcome_owner_no_funds {
let o in 1..T::MaxOwners::get();

let market_id: MarketIdOf<T> = 0u128.saturated_into();

let mut owners = Vec::new();
for i in 1..=o {
let owner = account("winners_owner", i, 0);
owners.push(owner);
}
let owners = BoundedVec::try_from(owners.clone()).unwrap();

let winner_info = WinnerInfo {
outcome: OutcomeReport::Scalar(0),
is_finished: true,
outcome_info: OutcomeInfo {
outcome_sum: 42u128.saturated_into(),
owners,
},
};
<Winners<T>>::insert(market_id, winner_info);

let caller: T::AccountId = whitelisted_caller();

let outcome = OutcomeReport::Scalar(20);

let reward_account = GlobalDisputes::<T>::reward_account(&market_id);
assert!(T::Currency::free_balance(&reward_account) == 0u128.saturated_into());

deposit::<T>(&caller);
}: {
<Pallet<T>>::reward_outcome_owner(RawOrigin::Signed(caller.clone()).into(), market_id)
.unwrap();
} verify {
assert_last_event::<T>(Event::OutcomeOwnersRewardedWithNoFunds::<T> { market_id }.into());
}

purge_outcomes {
// RemoveKeysLimit - 2 to ensure that we actually fully clean and return at the end
let k in 1..(T::RemoveKeysLimit::get() - 2);
Expand Down
17 changes: 4 additions & 13 deletions zrml/global-disputes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ mod pallet {
},
/// The winner of the global dispute system is determined.
GlobalDisputeWinnerDetermined { market_id: MarketIdOf<T> },
/// No funds could be spent as reward to the outcome owner(s).
OutcomeOwnersRewardedWithNoFunds { market_id: MarketIdOf<T> },
/// The outcome owner has been rewarded.
OutcomeOwnersRewarded { market_id: MarketIdOf<T>, owners: Vec<AccountIdOf<T>> },
/// The outcomes storage item is partially cleaned.
Expand All @@ -185,6 +183,8 @@ mod pallet {
InsufficientAmount,
/// The maximum amount of owners is reached.
MaxOwnersReached,
/// The amount in the reward pot is zero.
NoFundsToReward,
/// No global dispute present at the moment.
NoGlobalDisputeStarted,
/// The voting outcome has been already added.
Expand Down Expand Up @@ -325,11 +325,7 @@ mod pallet {
///
/// Complexity: `O(n)`, where `n` is the number of owners for the winning outcome.
#[frame_support::transactional]
#[pallet::weight(
T::WeightInfo::reward_outcome_owner_no_funds(T::MaxOwners::get()).max(
T::WeightInfo::reward_outcome_owner_with_funds(T::MaxOwners::get()),
)
)]
#[pallet::weight(T::WeightInfo::reward_outcome_owner_with_funds(T::MaxOwners::get()))]
pub fn reward_outcome_owner(
origin: OriginFor<T>,
#[pallet::compact] market_id: MarketIdOf<T>,
Expand All @@ -347,14 +343,9 @@ mod pallet {

let reward_account = Self::reward_account(&market_id);
let reward_account_free_balance = T::Currency::free_balance(&reward_account);
ensure!(!reward_account_free_balance.is_zero(), Error::<T>::NoFundsToReward);
let owners_len = winner_info.outcome_info.owners.len() as u32;

if reward_account_free_balance.is_zero() {
Self::deposit_event(Event::OutcomeOwnersRewardedWithNoFunds { market_id });
// return early case if there is no reward
return Ok((Some(T::WeightInfo::reward_outcome_owner_no_funds(owners_len))).into());
}

let mut remainder = reward_account_free_balance;
let owners_len_in_balance: BalanceOf<T> = <BalanceOf<T>>::from(owners_len);
if let Some(reward_per_each) =
Expand Down
42 changes: 1 addition & 41 deletions zrml/global-disputes/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,44 +377,6 @@ fn reward_outcome_owner_works_for_one_owner() {
});
}

#[test]
fn reward_outcome_owner_works_for_no_reward_funds() {
ExtBuilder::default().build().execute_with(|| {
let market_id = 0u128;

setup_vote_outcomes_with_hundred(&market_id);

let winner_info = WinnerInfo {
outcome: OutcomeReport::Scalar(20),
is_finished: true,
outcome_info: OutcomeInfo { outcome_sum: 10 * BASE, owners: Default::default() },
};
<Winners<Runtime>>::insert(market_id, winner_info);

assert_ok!(GlobalDisputes::purge_outcomes(Origin::signed(ALICE), market_id,));

System::assert_last_event(Event::<Runtime>::OutcomesFullyCleaned { market_id }.into());

let free_balance_alice_before = Balances::free_balance(&ALICE);

let reward_account_free_balance =
Balances::free_balance(&GlobalDisputes::reward_account(&market_id));
// this case happens, when add_vote_outcome wasn't called
// so no loosers, who provided the VotingOutcomeFee
assert!(reward_account_free_balance.is_zero());

assert_ok!(GlobalDisputes::reward_outcome_owner(Origin::signed(ALICE), market_id));

System::assert_last_event(
Event::<Runtime>::OutcomeOwnersRewardedWithNoFunds { market_id }.into(),
);

assert_eq!(Balances::free_balance(&ALICE), free_balance_alice_before);
assert!(Balances::free_balance(GlobalDisputes::reward_account(&market_id)).is_zero());
assert!(<Outcomes<Runtime>>::iter_prefix(market_id).next().is_none());
});
}

#[test]
fn vote_fails_if_amount_below_min_outcome_vote_amount() {
ExtBuilder::default().build().execute_with(|| {
Expand Down Expand Up @@ -878,7 +840,7 @@ fn determine_voting_winner_works_with_accumulated_votes_for_alice() {
}

#[test]
fn reward_outcome_owner_cleans_outcome_info() {
fn purge_outcomes_works() {
ExtBuilder::default().build().execute_with(|| {
let market_id = 0u128;

Expand Down Expand Up @@ -911,8 +873,6 @@ fn reward_outcome_owner_cleans_outcome_info() {

System::assert_last_event(Event::<Runtime>::OutcomesFullyCleaned { market_id }.into());

assert_ok!(GlobalDisputes::reward_outcome_owner(Origin::signed(BOB), market_id,));

assert_eq!(<Outcomes<Runtime>>::iter_prefix(market_id).next(), None);
});
}
Expand Down
10 changes: 0 additions & 10 deletions zrml/global-disputes/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ pub trait WeightInfoZeitgeist {
fn unlock_vote_balance_remove(l: u32, o: u32) -> Weight;
fn add_vote_outcome(w: u32) -> Weight;
fn reward_outcome_owner_with_funds(o: u32) -> Weight;
fn reward_outcome_owner_no_funds(o: u32) -> Weight;
fn purge_outcomes(k: u32, o: u32) -> Weight;
}

Expand Down Expand Up @@ -118,15 +117,6 @@ impl<T: frame_system::Config> WeightInfoZeitgeist for WeightInfo<T> {
.saturating_add(T::DbWeight::get().writes(1 as Weight))
.saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(o as Weight)))
}
// Storage: GlobalDisputes Outcomes (r:1 w:0)
// Storage: GlobalDisputes Winners (r:1 w:0)
// Storage: System Account (r:1 w:0)
fn reward_outcome_owner_no_funds(o: u32) -> Weight {
(39_929_000 as Weight)
// Standard Error: 3_000
.saturating_add((13_000 as Weight).saturating_mul(o as Weight))
.saturating_add(T::DbWeight::get().reads(3 as Weight))
}
// Storage: GlobalDisputes Winners (r:1 w:1)
// Storage: GlobalDisputes Outcomes (r:3 w:2)
fn purge_outcomes(k: u32, _o: u32) -> Weight {
Expand Down

0 comments on commit 8d6bcac

Please sign in to comment.