Skip to content

Commit

Permalink
start a global dispute via private api
Browse files Browse the repository at this point in the history
  • Loading branch information
Chralt98 committed Jan 4, 2023
1 parent e5f5fee commit b14faaf
Show file tree
Hide file tree
Showing 10 changed files with 326 additions and 121 deletions.
3 changes: 3 additions & 0 deletions docs/changelog_for_devs.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
which has three fields: `who` (the account that reserved the bond), `value`
(the amount reserved), `is_settled` (a flag which determines if the bond was
already unreserved and/or (partially) slashed).
- Added new dispatchable function:
- `refund_vote_fees` - Return all vote funds and fees, when a global dispute
was destroyed.

# v0.3.7

Expand Down
16 changes: 13 additions & 3 deletions zrml/global-disputes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,21 @@ on which the market finally resolves.
get their reward. Fails if the global dispute is not concluded yet.
- `reward_outcome_owner` - Reward the collected fees to the owner(s) of a voting
outcome. Fails if not all outcomes are already purged.
- `refund_vote_fees` - Return all vote funds and fees, when a global dispute was
destroyed.

#### Private Pallet API

- `push_vote_outcome` - Start a global dispute, add an initial voting outcome
and vote on it.
- `push_vote_outcome` - Add an initial voting outcome and vote on it with
`initial_vote_balance`.
- `get_voting_outcome_info` - Get the information (`outcome_sum`, `owners`) of a
voting outcome.
- `determine_voting_winner` - Determine the canonical voting outcome based on
total locked tokens.
- `is_started` - Check if the global dispute started already.
- `does_exist` - Check if the global dispute does already exist.
- `is_active` - Check if the global dispute is active to get votes
(`vote_on_outcome`) and allow the addition of new vote outcomes with
`add_vote_outcome`.
- `start_global_dispute` - Start a global dispute.
- `destroy_global_dispute` - Allow the users to get their voting funds and fee
payments back.
73 changes: 68 additions & 5 deletions zrml/global-disputes/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ benchmarks! {
// ensure that we get the worst case
// to actually insert the new item at the end of the binary search
let market_id: MarketIdOf<T> = v.into();
let market = market_mock::<T>();
for i in 0..=v {
T::MarketCommons::push_market(market.clone()).unwrap();
}

let outcome = OutcomeReport::Scalar(0);
let amount: BalanceOf<T> = T::MinOutcomeVoteAmount::get().saturated_into();
deposit::<T>(&caller);
Expand Down Expand Up @@ -203,14 +208,15 @@ benchmarks! {
// this happens in the case, that Outcomes is not none at the query time.
let w in 1..T::MaxOwners::get();

let mut owners = Vec::new();
let mut owners: Vec<AccountIdOf<T>> = Vec::new();
for i in 1..=w {
let owner = account("winners_owner", i, 0);
let owner: AccountIdOf<T> = account("winners_owner", i, 0);
owners.push(owner);
}
let owners = BoundedVec::try_from(owners).unwrap();
let possession = Some(Possession::Shared { owners });
let outcome_info = OutcomeInfo { outcome_sum: 42u128.saturated_into(), possession };
let owners: BoundedVec<AccountIdOf<T>, T::MaxOwners> = BoundedVec::try_from(owners)
.unwrap();

let outcome_info = OutcomeInfo { outcome_sum: 42u128.saturated_into(), possession: None };
let gd_info = GDInfo {
winner_outcome: OutcomeReport::Scalar(0),
status: GDStatus::Active,
Expand Down Expand Up @@ -309,6 +315,8 @@ benchmarks! {
let o in 1..T::MaxOwners::get();

let market_id: MarketIdOf<T> = 0u128.saturated_into();
let market = market_mock::<T>();
T::MarketCommons::push_market(market).unwrap();

for i in 1..=k {
let owner = account("outcomes_owner", i, 0);
Expand Down Expand Up @@ -354,6 +362,61 @@ benchmarks! {
assert!(<Outcomes<T>>::iter_prefix(market_id).next().is_none());
assert_last_event::<T>(Event::OutcomesFullyCleaned::<T> { market_id }.into());
}

refund_vote_fees {
// RemoveKeysLimit - 2 to ensure that we actually fully clean and return at the end
let k in 1..(T::RemoveKeysLimit::get() - 2);

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

let market_id: MarketIdOf<T> = 0u128.saturated_into();
let market = market_mock::<T>();
T::MarketCommons::push_market(market).unwrap();

for i in 1..=k {
let owner = account("outcomes_owner", i, 0);
GlobalDisputes::<T>::push_vote_outcome(
&market_id,
OutcomeReport::Scalar(i.into()),
&owner,
1_000_000_000u128.saturated_into(),
)
.unwrap();
}

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_outcome = OutcomeReport::Scalar(0);

let possession = Some(Possession::Shared { owners });
let outcome_info = OutcomeInfo {
outcome_sum: 42u128.saturated_into(),
possession,
};
<Outcomes<T>>::insert(market_id, winner_outcome.clone(), outcome_info);

let possession = Some(Possession::Shared { owners: Default::default() });
let outcome_info = OutcomeInfo {
outcome_sum: 42u128.saturated_into(),
possession,
};
let gd_info = GDInfo {winner_outcome, status: GDStatus::Destroyed, outcome_info};
<GlobalDisputesInfo<T>>::insert(market_id, gd_info);

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

let outcome = OutcomeReport::Scalar(20);

deposit::<T>(&caller);
}: _(RawOrigin::Signed(caller.clone()), market_id)
verify {
assert!(<Outcomes<T>>::iter_prefix(market_id).next().is_none());
assert_last_event::<T>(Event::OutcomesFullyCleaned::<T> { market_id }.into());
}
}

impl_benchmark_test_suite!(
Expand Down
21 changes: 14 additions & 7 deletions zrml/global-disputes/src/global_disputes_pallet_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,33 @@ pub trait GlobalDisputesPalletApi<MarketId, AccountId, Balance> {
/// Returns the winning outcome.
fn determine_voting_winner(market_id: &MarketId) -> Option<OutcomeReport>;

/// Check if global dispute started.
/// Check if global dispute already exists.
///
/// # Arguments
/// - `market_id` - The id of the market.
fn is_started(market_id: &MarketId) -> bool;
fn does_exist(market_id: &MarketId) -> bool;

/// Check if global dispute is active.
/// Check if global dispute is active or initialized. But not finished.
/// This call is useful to check if a global dispute is ready for a destruction.
///
/// # Arguments
/// - `market_id` - The id of the market.
fn is_active(market_id: &MarketId) -> bool;
fn is_unfinished(market_id: &MarketId) -> bool;

/// Check if a global dispute has not already been started.
/// Check if a global dispute does not exist.
///
/// # Arguments
/// - `market_id` - The id of the market.
fn is_not_started(market_id: &MarketId) -> bool {
!Self::is_started(market_id)
fn does_not_exist(market_id: &MarketId) -> bool {
!Self::does_exist(market_id)
}

/// Start a global dispute.
///
/// # Arguments
/// - `market_id` - The id of the market.
fn start_global_dispute(market_id: &MarketId) -> Result<(), DispatchError>;

/// Destroy a global dispute and allow to return all funds of the participants.
///
/// # Arguments
Expand Down
Loading

0 comments on commit b14faaf

Please sign in to comment.