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

ZIP-0 Part II #938

Merged
merged 69 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
8c0f2ed
move disputes to simple-disputes
Chralt98 Jan 9, 2023
bddaa72
Merge branch 'main' into chralt98-dispute-restructuring
Chralt98 Jan 11, 2023
1977d2a
move more to simple-disputes
Chralt98 Jan 11, 2023
c8699d4
wip
Chralt98 Jan 11, 2023
ae91749
wip
Chralt98 Jan 12, 2023
cd85642
Merge branch 'main' into chralt98-dispute-restructuring
Chralt98 Jan 13, 2023
228d333
some mock preparation
Chralt98 Jan 13, 2023
1f05799
wip
Chralt98 Jan 13, 2023
0b80b7d
fix tests
Chralt98 Jan 13, 2023
6236d51
taplo fmt
Chralt98 Jan 16, 2023
e4a09a9
update court authorized mdms
Chralt98 Jan 16, 2023
a67c50d
add dispute bond to market storage
Chralt98 Jan 16, 2023
9448ab9
mdm funds flow
Chralt98 Jan 16, 2023
c46621e
fix clippy
Chralt98 Jan 17, 2023
2f12959
fix pm benchmarks
Chralt98 Jan 17, 2023
7430066
Merge branch 'main' into chralt98-dispute-restructuring
Chralt98 Jan 17, 2023
6f10d96
add migration
Chralt98 Jan 17, 2023
9f002a4
simplify migration
Chralt98 Jan 17, 2023
09730a0
correct migration
Chralt98 Jan 18, 2023
f1347c3
migration fixes and call filter
Chralt98 Jan 18, 2023
b42002c
correct admin_destroy_market benchmark
Chralt98 Jan 18, 2023
80b3fa6
improve simple-disputes mock
Chralt98 Jan 18, 2023
bd7c97d
benchmark reserve_outcome
Chralt98 Jan 19, 2023
61e2e1e
Merge branch 'main' into chralt98-dispute-restructuring
Chralt98 Jan 19, 2023
355dad6
benchmark reserve_outcome
Chralt98 Jan 19, 2023
4aaea4e
Merge branch 'main' into chralt98-dispute-restructuring
Chralt98 Jan 20, 2023
af3da23
Merge branch 'main' into chralt98-dispute-restructuring
Chralt98 Jan 23, 2023
ee2681a
fix weights file
Chralt98 Jan 24, 2023
0ff08c0
Merge branch 'main' into chralt98-dispute-restructuring
Chralt98 Jan 31, 2023
06314bb
fix after merge
Chralt98 Jan 31, 2023
78f0b03
add migration tests
Chralt98 Jan 31, 2023
84bb021
add migration reserve id test
Chralt98 Jan 31, 2023
e5302da
Merge branch 'main' into chralt98-dispute-restructuring
Chralt98 Feb 1, 2023
c3c5ca6
apply review suggestions
Chralt98 Mar 2, 2023
5d873d2
rename reserve_outcome to suggest_outcome
Chralt98 Mar 2, 2023
88a43dd
separate resolve_disputed_market into parts
Chralt98 Mar 2, 2023
b8cd91a
edit exchange API documentation
Chralt98 Mar 27, 2023
5d6df79
slash dispute bond
Chralt98 Mar 27, 2023
1fc9b39
Merge branch 'release-dispute-system' into chralt98-dispute-restructu…
Chralt98 Mar 28, 2023
ce13fd2
add empty commit
Chralt98 Mar 28, 2023
7b3ff21
correct admin_destroy_market test
Chralt98 Mar 28, 2023
a052320
remove gd dependency from simple disputes
Chralt98 Mar 28, 2023
c3b61cb
Update zrml/simple-disputes/src/mock.rs
Chralt98 Apr 12, 2023
11960de
Update zrml/simple-disputes/src/mock.rs
Chralt98 Apr 12, 2023
6fc9197
Update zrml/prediction-markets/src/lib.rs
Chralt98 Apr 12, 2023
c638679
add doc string
Chralt98 Apr 12, 2023
553e082
add doc strings
Chralt98 Apr 12, 2023
55fdee0
Reduce settle_bonds LOC
sea212 Apr 3, 2023
0efc405
cargo fmt
Chralt98 Apr 12, 2023
aa09b01
Update zrml/prediction-markets/src/migrations.rs
Chralt98 Apr 12, 2023
2c15b96
Update zrml/prediction-markets/src/migrations.rs
Chralt98 Apr 12, 2023
a71bb5c
apply review suggestion
Chralt98 Apr 12, 2023
afd9d61
add correct mdm benchmarking on_dispute
Chralt98 Apr 17, 2023
af0a25a
use on_dispute_weight inside api
Chralt98 Apr 17, 2023
b88aca4
improve mdm weight technique
Chralt98 Apr 17, 2023
f48a0a3
add mdm weights to on_resolution
Chralt98 Apr 18, 2023
cbe528a
add tests for pm
Chralt98 Apr 18, 2023
3c6c9fe
Merge branch 'main' into chralt98-dispute-restructuring
Chralt98 Apr 18, 2023
7ab62c4
modify migration logs, fix try-runtime
Chralt98 Apr 18, 2023
86bd97e
little benchmark fix
Chralt98 Apr 20, 2023
af60129
Update zrml/authorized/src/benchmarks.rs
Chralt98 Apr 20, 2023
ddf14ff
Update zrml/authorized/src/benchmarks.rs
Chralt98 Apr 20, 2023
8513191
use result with weight struct
Chralt98 Apr 20, 2023
1e0fcbc
improve dispute api weight system
Chralt98 Apr 21, 2023
ae99d7f
Use accurate dispute weight instead of max
sea212 May 3, 2023
1f7f337
Merge branch 'release-dispute-system' into chralt98-dispute-restructu…
Chralt98 May 9, 2023
6025960
fix clippy
Chralt98 May 9, 2023
99836e5
Update zrml/prediction-markets/src/migrations.rs
Chralt98 May 15, 2023
7aae6f1
add copyrights
Chralt98 May 15, 2023
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
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions primitives/src/constants/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ parameter_types! {
// Prediction Market parameters
parameter_types! {
pub const AdvisoryBond: Balance = 25 * CENT;
pub const DisputeBond: Balance = 5 * BASE;
pub const DisputeFactor: Balance = 2 * BASE;
pub const DisputeBond: Balance = 20 * BASE;
pub const GlobalDisputePeriod: BlockNumber = 7 * BLOCKS_PER_DAY;
pub const MaxCategories: u16 = 10;
pub const MaxDisputes: u16 = 6;
Expand All @@ -66,6 +65,8 @@ parameter_types! {
// Simple disputes parameters
parameter_types! {
pub const SimpleDisputesPalletId: PalletId = PalletId(*b"zge/sedp");
pub const OutcomeBond: Balance = 5 * BASE;
pub const OutcomeFactor: Balance = 2 * BASE;
}

// Swaps parameters
Expand Down
18 changes: 15 additions & 3 deletions primitives/src/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl<AI, BA> Bond<AI, BA> {
pub struct MarketBonds<AI, BA> {
pub creation: Option<Bond<AI, BA>>,
pub oracle: Option<Bond<AI, BA>>,
pub dispute: Option<Bond<AI, BA>>,
}

impl<AI: Ord, BA: frame_support::traits::tokens::Balance> MarketBonds<AI, BA> {
Expand All @@ -92,14 +93,16 @@ impl<AI: Ord, BA: frame_support::traits::tokens::Balance> MarketBonds<AI, BA> {
Some(bond) if bond.who == *who => bond.value,
_ => BA::zero(),
};
value_or_default(&self.creation).saturating_add(value_or_default(&self.oracle))
value_or_default(&self.creation)
.saturating_add(value_or_default(&self.oracle))
.saturating_add(value_or_default(&self.dispute))
}
}

// Used primarily for testing purposes.
impl<AI, BA> Default for MarketBonds<AI, BA> {
fn default() -> Self {
MarketBonds { creation: None, oracle: None }
MarketBonds { creation: None, oracle: None, dispute: None }
}
}

Expand Down Expand Up @@ -166,13 +169,22 @@ pub enum MarketCreation {
Advised,
}

// TODO to remove, when Disputes storage item is removed
#[derive(Clone, Decode, Encode, Eq, MaxEncodedLen, PartialEq, RuntimeDebug, TypeInfo)]
pub struct MarketDispute<AccountId, BlockNumber> {
pub struct OldMarketDispute<AccountId, BlockNumber> {
pub at: BlockNumber,
pub by: AccountId,
pub outcome: OutcomeReport,
}

#[derive(Clone, Decode, Encode, Eq, MaxEncodedLen, PartialEq, RuntimeDebug, TypeInfo)]
pub struct MarketDispute<AccountId, BlockNumber, Balance> {
pub at: BlockNumber,
pub by: AccountId,
pub outcome: OutcomeReport,
pub bond: Balance,
maltekliemann marked this conversation as resolved.
Show resolved Hide resolved
}

/// How a market should resolve disputes
#[derive(Clone, Decode, Encode, Eq, MaxEncodedLen, PartialEq, RuntimeDebug, TypeInfo)]
pub enum MarketDisputeMechanism {
Expand Down
29 changes: 14 additions & 15 deletions primitives/src/traits/dispute_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
// You should have received a copy of the GNU General Public License
// along with Zeitgeist. If not, see <https://www.gnu.org/licenses/>.

use crate::{market::MarketDispute, outcome_report::OutcomeReport, types::Market};
use frame_support::{dispatch::DispatchResult, pallet_prelude::Weight, BoundedVec};
use crate::{outcome_report::OutcomeReport, types::Market};
use frame_support::{dispatch::DispatchResult, pallet_prelude::Weight};
use sp_runtime::DispatchError;

// Abstraction of the market type, which is not a part of `DisputeApi` because Rust doesn't support
Expand All @@ -40,11 +40,7 @@ pub trait DisputeApi {
///
/// Further interaction with the dispute API (if necessary) **should** happen through an
/// associated pallet. **May** assume that `market.dispute_mechanism` refers to the calling dispute API.
fn on_dispute(
previous_disputes: &[MarketDispute<Self::AccountId, Self::BlockNumber>],
market_id: &Self::MarketId,
market: &MarketOfDisputeApi<Self>,
) -> DispatchResult;
fn on_dispute(market_id: &Self::MarketId, market: &MarketOfDisputeApi<Self>) -> DispatchResult;

/// Manage market resolution of a disputed market.
///
Expand All @@ -56,7 +52,6 @@ pub trait DisputeApi {
/// Returns the dispute mechanism's report if available, otherwise `None`. If `None` is
/// returned, this means that the dispute could not be resolved.
fn on_resolution(
disputes: &[MarketDispute<Self::AccountId, Self::BlockNumber>],
market_id: &Self::MarketId,
market: &MarketOfDisputeApi<Self>,
) -> Result<Option<OutcomeReport>, DispatchError>;
Expand All @@ -68,7 +63,6 @@ pub trait DisputeApi {
///
/// Returns the future resolution block if available, otherwise `None`.
fn get_auto_resolve(
disputes: &[MarketDispute<Self::AccountId, Self::BlockNumber>],
market_id: &Self::MarketId,
market: &MarketOfDisputeApi<Self>,
) -> Result<Option<Self::BlockNumber>, DispatchError>;
Expand All @@ -77,10 +71,20 @@ pub trait DisputeApi {
/// was unable to come to a conclusion.
/// **May** assume that `market.dispute_mechanism` refers to the calling dispute API.
fn has_failed(
disputes: &[MarketDispute<Self::AccountId, Self::BlockNumber>],
market_id: &Self::MarketId,
market: &MarketOfDisputeApi<Self>,
) -> Result<bool, DispatchError>;

/// Called, when a global dispute is started.
/// **May** assume that `market.dispute_mechanism` refers to the calling dispute API.
fn on_global_dispute(
market_id: &Self::MarketId,
market: &MarketOfDisputeApi<Self>,
) -> DispatchResult;

/// Called, when a market is destroyed.
maltekliemann marked this conversation as resolved.
Show resolved Hide resolved
/// **May** assume that `market.dispute_mechanism` refers to the calling dispute API.
fn clear(market_id: &Self::MarketId, market: &MarketOfDisputeApi<Self>) -> DispatchResult;
}

type MarketOfDisputeResolutionApi<T> = Market<
Expand Down Expand Up @@ -134,9 +138,4 @@ pub trait DisputeResolutionApi {
///
/// Returns the number of elements in the storage structure.
fn remove_auto_resolve(market_id: &Self::MarketId, resolve_at: Self::BlockNumber) -> u32;

/// Get the disputes of a market.
fn get_disputes(
market_id: &Self::MarketId,
) -> BoundedVec<MarketDispute<Self::AccountId, Self::BlockNumber>, Self::MaxDisputes>;
}
14 changes: 9 additions & 5 deletions runtime/battery-station/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,9 @@ parameter_types! {
pub const AdvisoryBond: Balance = 25 * CENT;
pub const AdvisoryBondSlashPercentage: Percent = Percent::from_percent(0);
/// (Slashable) Bond that is provided for disputing the outcome.
/// Slashed in case the final outcome does not match the dispute for which the `DisputeBond`
/// was deposited.
pub const DisputeBond: Balance = 5 * BASE;
/// `DisputeBond` is increased by this factor after every dispute.
pub const DisputeFactor: Balance = 2 * BASE;
/// Unreserved in case the dispute was justified otherwise slashed.
/// This is when the resolved outcome is different to the default (reported) outcome.
pub const DisputeBond: Balance = 25 * BASE;
/// Maximum Categories a prediciton market can have (excluding base asset).
pub const MaxCategories: u16 = MAX_CATEGORIES;
/// Maximum number of disputes.
Expand Down Expand Up @@ -217,6 +215,12 @@ parameter_types! {
// Simple disputes parameters
/// Pallet identifier, mainly used for named balance reserves.
pub const SimpleDisputesPalletId: PalletId = SD_PALLET_ID;
/// (Slashable) Bond that is provided for overriding the last outcome addition.
/// Slashed in case the final outcome does not match the dispute for which the `OutcomeBond`
/// was deposited.
pub const OutcomeBond: Balance = 5 * BASE;
/// `OutcomeBond` is increased by this factor after every new outcome addition.
pub const OutcomeFactor: Balance = 2 * BASE;

// Swaps parameters
/// A precentage from the withdrawal amount a liquidity provider wants to withdraw
Expand Down
10 changes: 9 additions & 1 deletion runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,6 @@ macro_rules! impl_config_traits {
type CloseOrigin = EnsureRootOrTwoThirdsAdvisoryCommittee;
type DestroyOrigin = EnsureRootOrAllAdvisoryCommittee;
type DisputeBond = DisputeBond;
type DisputeFactor = DisputeFactor;
type Event = Event;
#[cfg(feature = "with-global-disputes")]
type GlobalDisputes = GlobalDisputes;
Expand Down Expand Up @@ -1042,10 +1041,19 @@ macro_rules! impl_config_traits {
}

impl zrml_simple_disputes::Config for Runtime {
type AssetManager = AssetManager;
type OutcomeBond = OutcomeBond;
type OutcomeFactor = OutcomeFactor;
type DisputeResolution = zrml_prediction_markets::Pallet<Runtime>;
type Event = Event;
#[cfg(feature = "with-global-disputes")]
type GlobalDisputes = GlobalDisputes;
type MarketCommons = MarketCommons;
type MaxDisputes = MaxDisputes;
type PalletId = SimpleDisputesPalletId;
type PredictionMarketsPalletId = PmPalletId;
type Slash = Treasury;
type WeightInfo = zrml_simple_disputes::weights::WeightInfo<Runtime>;
}

#[cfg(feature = "with-global-disputes")]
Expand Down
14 changes: 9 additions & 5 deletions runtime/zeitgeist/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,9 @@ parameter_types! {
pub const AdvisoryBond: Balance = 200 * BASE;
pub const AdvisoryBondSlashPercentage: Percent = Percent::from_percent(0);
/// (Slashable) Bond that is provided for disputing the outcome.
/// Slashed in case the final outcome does not match the dispute for which the `DisputeBond`
/// was deposited.
pub const DisputeBond: Balance = 2_000 * BASE;
/// `DisputeBond` is increased by this factor after every dispute.
pub const DisputeFactor: Balance = 2 * BASE;
/// Unreserved in case the dispute was justified otherwise slashed.
/// This is when the resolved outcome is different to the default (reported) outcome.
pub const DisputeBond: Balance = 5_000 * BASE;
Chralt98 marked this conversation as resolved.
Show resolved Hide resolved
/// Maximum Categories a prediciton market can have (excluding base asset).
pub const MaxCategories: u16 = MAX_CATEGORIES;
/// Maximum number of disputes.
Expand Down Expand Up @@ -217,6 +215,12 @@ parameter_types! {
// Simple disputes parameters
/// Pallet identifier, mainly used for named balance reserves. DO NOT CHANGE.
pub const SimpleDisputesPalletId: PalletId = SD_PALLET_ID;
/// (Slashable) Bond that is provided for overriding the last outcome addition.
/// Slashed in case the final outcome does not match the dispute for which the `OutcomeBond`
/// was deposited.
pub const OutcomeBond: Balance = 2_000 * BASE;
/// `OutcomeBond` is increased by this factor after every new outcome addition.
pub const OutcomeFactor: Balance = 2 * BASE;

// Swaps parameters
/// A precentage from the withdrawal amount a liquidity provider wants to withdraw
Expand Down
57 changes: 35 additions & 22 deletions zrml/authorized/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ mod pallet {
use sp_runtime::{traits::Saturating, DispatchError};
use zeitgeist_primitives::{
traits::{DisputeApi, DisputeResolutionApi},
types::{
AuthorityReport, Market, MarketDispute, MarketDisputeMechanism, MarketStatus,
OutcomeReport,
},
types::{AuthorityReport, Market, MarketDisputeMechanism, MarketStatus, OutcomeReport},
};
use zrml_market_commons::MarketCommonsPalletApi;

Expand Down Expand Up @@ -99,16 +96,21 @@ mod pallet {

let report_opt = AuthorizedOutcomeReports::<T>::get(market_id);
let (report, ids_len) = match &report_opt {
Some(report) => (AuthorityReport { resolve_at: report.resolve_at, outcome }, 0u32),
Some(report) => (
AuthorityReport { resolve_at: report.resolve_at, outcome: outcome.clone() },
0u32,
),
None => {
let resolve_at = now.saturating_add(T::CorrectionPeriod::get());
let ids_len = T::DisputeResolution::add_auto_resolve(&market_id, resolve_at)?;
(AuthorityReport { resolve_at, outcome }, ids_len)
(AuthorityReport { resolve_at, outcome: outcome.clone() }, ids_len)
}
};

AuthorizedOutcomeReports::<T>::insert(market_id, report);

Self::deposit_event(Event::AuthorityReported { market_id, outcome });

if report_opt.is_none() {
Ok(Some(T::WeightInfo::authorize_market_outcome_first_report(ids_len)).into())
} else {
Expand Down Expand Up @@ -156,16 +158,19 @@ mod pallet {
MarketDoesNotHaveDisputeMechanismAuthorized,
/// An account attempts to submit a report to an undisputed market.
MarketIsNotDisputed,
/// Only one dispute is allowed.
OnlyOneDisputeAllowed,
/// The report does not match the market's type.
OutcomeMismatch,
}

#[pallet::event]
#[pallet::generate_deposit(pub(crate) fn deposit_event)]
pub enum Event<T>
where
T: Config, {}
T: Config,
{
/// The Authority reported.
AuthorityReported { market_id: MarketIdOf<T>, outcome: OutcomeReport },
}

#[pallet::hooks]
impl<T: Config> Hooks<T::BlockNumber> for Pallet<T> {}
Expand Down Expand Up @@ -195,21 +200,15 @@ mod pallet {
type Moment = MomentOf<T>;
type Origin = T::Origin;

fn on_dispute(
disputes: &[MarketDispute<Self::AccountId, Self::BlockNumber>],
_: &Self::MarketId,
market: &MarketOf<T>,
) -> DispatchResult {
fn on_dispute(_: &Self::MarketId, market: &MarketOf<T>) -> DispatchResult {
ensure!(
market.dispute_mechanism == MarketDisputeMechanism::Authorized,
Error::<T>::MarketDoesNotHaveDisputeMechanismAuthorized
);
ensure!(disputes.is_empty(), Error::<T>::OnlyOneDisputeAllowed);
Ok(())
}

fn on_resolution(
_: &[MarketDispute<Self::AccountId, Self::BlockNumber>],
market_id: &Self::MarketId,
market: &MarketOf<T>,
) -> Result<Option<OutcomeReport>, DispatchError> {
Expand All @@ -222,7 +221,6 @@ mod pallet {
}

fn get_auto_resolve(
_: &[MarketDispute<Self::AccountId, Self::BlockNumber>],
market_id: &Self::MarketId,
market: &MarketOf<T>,
) -> Result<Option<Self::BlockNumber>, DispatchError> {
Expand All @@ -233,18 +231,33 @@ mod pallet {
Ok(Self::get_auto_resolve(market_id))
}

fn has_failed(
_: &[MarketDispute<Self::AccountId, Self::BlockNumber>],
_: &Self::MarketId,
market: &MarketOf<T>,
) -> Result<bool, DispatchError> {
fn has_failed(_: &Self::MarketId, market: &MarketOf<T>) -> Result<bool, DispatchError> {
ensure!(
market.dispute_mechanism == MarketDisputeMechanism::Authorized,
Error::<T>::MarketDoesNotHaveDisputeMechanismAuthorized
);

Ok(false)
}

fn on_global_dispute(_: &Self::MarketId, market: &MarketOf<T>) -> DispatchResult {
ensure!(
market.dispute_mechanism == MarketDisputeMechanism::Authorized,
Error::<T>::MarketDoesNotHaveDisputeMechanismAuthorized
);

Ok(())
}

fn clear(market_id: &Self::MarketId, market: &MarketOf<T>) -> DispatchResult {
ensure!(
market.dispute_mechanism == MarketDisputeMechanism::Authorized,
Error::<T>::MarketDoesNotHaveDisputeMechanismAuthorized
);

AuthorizedOutcomeReports::<T>::remove(market_id);
Ok(())
}
}

impl<T> AuthorizedPalletApi for Pallet<T> where T: Config {}
Expand Down
Loading