Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sixth iteration of the Court pallet #244

Merged
merged 3 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ mod market;
mod outcome_report;
mod pool;
mod pool_status;
mod resolution_counters;
mod serde_wrapper;
pub mod traits;
pub mod types;
7 changes: 0 additions & 7 deletions primitives/src/resolution_counters.rs

This file was deleted.

20 changes: 11 additions & 9 deletions primitives/src/traits/dispute_api.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{market::MarketDispute, types::Market};
use crate::{market::MarketDispute, outcome_report::OutcomeReport, types::Market};
use frame_support::dispatch::DispatchResult;
use sp_runtime::DispatchError;

/// Dispute Api
pub trait DisputeApi {
Expand All @@ -10,17 +11,18 @@ pub trait DisputeApi {
type Origin;

/// Disputes a reported outcome.
fn on_dispute<D>(
dispute_bond: D,
fn on_dispute(
disputes: &[MarketDispute<Self::AccountId, Self::BlockNumber>],
market_id: Self::MarketId,
who: Self::AccountId,
) -> DispatchResult
where
D: Fn(usize) -> Self::Balance;
) -> DispatchResult;

/// Manages markets resolutions moving all reported markets to resolved.
fn on_resolution<F>(now: Self::BlockNumber, cb: F) -> DispatchResult
fn on_resolution<D>(
dispute_bound: &D,
disputes: &[MarketDispute<Self::AccountId, Self::BlockNumber>],
market_id: &Self::MarketId,
market: &Market<Self::AccountId, Self::BlockNumber>,
) -> Result<OutcomeReport, DispatchError>
where
F: FnMut(&Self::MarketId, &Market<Self::AccountId, Self::BlockNumber>) -> DispatchResult;
D: Fn(usize) -> Self::Balance;
}
2 changes: 1 addition & 1 deletion primitives/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub use crate::{
asset::*, market::*, outcome_report::OutcomeReport, pool::Pool, pool_status::PoolStatus,
resolution_counters::ResolutionCounters, serde_wrapper::*,
serde_wrapper::*,
};
#[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Result, Unstructured};
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ impl zrml_prediction_markets::Config for Runtime {
type ApprovalOrigin = EnsureRoot<AccountId>;
type DisputeBond = DisputeBond;
type DisputeFactor = DisputeFactor;
type DisputePeriod = DisputePeriod;
type Event = Event;
type MarketCommons = MarketCommons;
type LiquidityMining = LiquidityMining;
Expand All @@ -468,7 +469,6 @@ impl zrml_prediction_markets::Config for Runtime {
}

impl zrml_simple_disputes::Config for Runtime {
type DisputePeriod = DisputePeriod;
type Event = Event;
type LiquidityMining = LiquidityMining;
type MarketCommons = MarketCommons;
Expand Down
30 changes: 14 additions & 16 deletions zrml/court/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ mod pallet {
rng: &mut R,
) -> ArrayVec<&'b (T::AccountId, Juror<BalanceOf<T>>), MAX_RANDOM_JURORS>
where
R: RngCore,
'a: 'b,
R: RngCore,
{
let actual_len = jurors.len().min(len);
jurors.choose_multiple(rng, actual_len).collect()
Expand Down Expand Up @@ -205,34 +205,32 @@ mod pallet {
type Origin = T::Origin;
type MarketId = MarketIdOf<T>;

fn on_dispute<D>(
dispute_bond: D,
fn on_dispute(
disputes: &[MarketDispute<Self::AccountId, Self::BlockNumber>],
market_id: Self::MarketId,
who: Self::AccountId,
) -> DispatchResult
where
D: Fn(usize) -> Self::Balance,
{
CurrencyOf::<T>::reserve(&who, dispute_bond(disputes.len()))?;
) -> DispatchResult {
let jurors: Vec<_> = Jurors::<T>::iter().collect();
let necessary_jurors_num = Self::necessary_jurors_num(disputes);
let mut rng = Self::rng();
let random_jurors = Self::random_jurors(&jurors, necessary_jurors_num, &mut rng);
let curr_block_num = <frame_system::Pallet<T>>::block_number();
let block_limit = curr_block_num.saturating_add(T::CourtCaseDuration::get());
for (ai, _) in random_jurors {
RequestedJurors::<T>::insert(ai, market_id, T::CourtCaseDuration::get());
RequestedJurors::<T>::insert(ai, market_id, block_limit);
}
Ok(())
}

fn on_resolution<F>(_now: Self::BlockNumber, _cb: F) -> DispatchResult
fn on_resolution<D>(
_: &D,
_: &[MarketDispute<Self::AccountId, Self::BlockNumber>],
_: &Self::MarketId,
_: &Market<Self::AccountId, Self::BlockNumber>,
) -> Result<OutcomeReport, DispatchError>
where
F: FnMut(
&Self::MarketId,
&Market<Self::AccountId, Self::BlockNumber>,
) -> DispatchResult,
D: Fn(usize) -> Self::Balance,
{
Ok(())
Ok(OutcomeReport::Scalar(Default::default()))
}
}

Expand Down
2 changes: 1 addition & 1 deletion zrml/court/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn on_dispute_stores_jurors_that_should_vote() {
setup_blocks(1..123);
let _ = Court::join_court(Origin::signed(ALICE));
let _ = Court::join_court(Origin::signed(BOB));
let _ = Court::on_dispute(|_| 0, &[], 0, ALICE);
let _ = Court::on_dispute(&[], 0);
assert_noop!(
Court::join_court(Origin::signed(ALICE)),
Error::<Runtime>::JurorAlreadyExists
Expand Down
2 changes: 0 additions & 2 deletions zrml/prediction-markets/fuzz/pm_full_workflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ fuzz_target!(|data: Data| {

let dispute_market_id = data.dispute_market_id.into();
let _ = SimpleDisputes::on_dispute(
zrml_prediction_markets::default_dispute_bound::<Runtime>,
&zrml_prediction_markets::Disputes::<Runtime>::get(&dispute_market_id),
dispute_market_id,
data.dispute_origin.into(),
);

let _ = PredictionMarkets::on_initialize(5);
Expand Down
19 changes: 9 additions & 10 deletions zrml/prediction-markets/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use zeitgeist_primitives::{
},
};
use zrml_market_commons::MarketCommonsPalletApi;
use zrml_simple_disputes::SimpleDisputesPalletApi;

// Get default values for market creation. Also spawns an account with maximum
// amount of native currency
Expand Down Expand Up @@ -192,7 +191,7 @@ benchmarks! {
for i in 0..c.min(T::MaxDisputes::get() as u32) {
let origin = caller.clone();
let disputes = crate::Disputes::<T>::get(&marketid);
let _ = T::SimpleDisputes::on_dispute(default_dispute_bound::<T>, &disputes, marketid, origin)?;
let _ = T::SimpleDisputes::on_dispute(&disputes, marketid)?;
}

let approval_origin = T::ApprovalOrigin::successful_origin();
Expand Down Expand Up @@ -296,7 +295,7 @@ benchmarks! {
}: {
let origin = caller.clone();
let disputes = crate::Disputes::<T>::get(&marketid);
let _ = T::SimpleDisputes::on_dispute(default_dispute_bound::<T>, &disputes, marketid, origin)?;
let _ = T::SimpleDisputes::on_dispute(&disputes, marketid)?;
}

internal_resolve_categorical_reported {
Expand All @@ -312,7 +311,7 @@ benchmarks! {
}: {
let market = T::MarketCommons::market(&marketid)?;
let disputes = crate::Disputes::<T>::get(&marketid);
T::SimpleDisputes::internal_resolve(&default_dispute_bound::<T>, &disputes, &marketid, &market)?
T::SimpleDisputes::on_resolution(&default_dispute_bound::<T>, &disputes, &marketid, &market)?
}

internal_resolve_categorical_disputed {
Expand All @@ -331,12 +330,12 @@ benchmarks! {
for i in 0..c.min(d) {
let origin = caller.clone();
let disputes = crate::Disputes::<T>::get(&marketid);
let _ = T::SimpleDisputes::on_dispute(default_dispute_bound::<T>, &disputes, marketid, origin)?;
let _ = T::SimpleDisputes::on_dispute(&disputes, marketid)?;
}
}: {
let market = T::MarketCommons::market(&marketid)?;
let disputes = crate::Disputes::<T>::get(&marketid);
T::SimpleDisputes::internal_resolve(&default_dispute_bound::<T>, &disputes, &marketid, &market)?
T::SimpleDisputes::on_resolution(&default_dispute_bound::<T>, &disputes, &marketid, &market)?
}

internal_resolve_scalar_reported {
Expand All @@ -346,7 +345,7 @@ benchmarks! {
}: {
let market = T::MarketCommons::market(&marketid)?;
let disputes = crate::Disputes::<T>::get(&marketid);
T::SimpleDisputes::internal_resolve(&default_dispute_bound::<T>, &disputes, &marketid, &market)?
T::SimpleDisputes::on_resolution(&default_dispute_bound::<T>, &disputes, &marketid, &market)?
}

internal_resolve_scalar_disputed {
Expand All @@ -359,17 +358,17 @@ benchmarks! {
for i in 0..d {
let disputes = crate::Disputes::<T>::get(&marketid);
let origin = caller.clone();
let _ = T::SimpleDisputes::on_dispute(default_dispute_bound::<T>, &disputes, marketid, origin)?;
let _ = T::SimpleDisputes::on_dispute(&disputes, marketid)?;
}
}: {
let market = T::MarketCommons::market(&marketid)?;
let disputes = crate::Disputes::<T>::get(&marketid);
T::SimpleDisputes::internal_resolve(&default_dispute_bound::<T>, &disputes, &marketid, &market)?
T::SimpleDisputes::on_resolution(&default_dispute_bound::<T>, &disputes, &marketid, &market)?
}

// This benchmark measures the cost of fn `on_initialize` minus the resolution.
on_initialize_resolve_overhead {
let starting_block = frame_system::Pallet::<T>::block_number() + T::SimpleDisputes::dispute_period();
let starting_block = frame_system::Pallet::<T>::block_number() + T::DisputePeriod::get();
}: { Pallet::<T>::on_initialize(starting_block * 2u32.into()) }

redeem_shares_categorical {
Expand Down
Loading