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 1 commit
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: 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
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
5 changes: 3 additions & 2 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,7 @@ macro_rules! impl_config_traits {
type Court = Court;
type CloseOrigin = EnsureRootOrTwoThirdsAdvisoryCommittee;
type DestroyOrigin = EnsureRootOrAllAdvisoryCommittee;
type DisputeBond = DisputeBond;
type Event = Event;
#[cfg(feature = "with-global-disputes")]
type GlobalDisputes = GlobalDisputes;
Expand Down Expand Up @@ -1041,8 +1042,8 @@ macro_rules! impl_config_traits {

impl zrml_simple_disputes::Config for Runtime {
type AssetManager = AssetManager;
type DisputeBond = DisputeBond;
type DisputeFactor = DisputeFactor;
type OutcomeBond = OutcomeBond;
type OutcomeFactor = OutcomeFactor;
type DisputeResolution = zrml_prediction_markets::Pallet<Runtime>;
type Event = Event;
#[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
11 changes: 5 additions & 6 deletions zrml/court/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,7 @@ mod pallet {
// TODO take a bond from the caller
ensure_signed(origin)?;
let market = T::MarketCommons::market(&market_id)?;
ensure!(
market.status == MarketStatus::Disputed,
Error::<T>::MarketIsNotDisputed
);
ensure!(market.status == MarketStatus::Disputed, Error::<T>::MarketIsNotDisputed);
ensure!(
market.dispute_mechanism == MarketDisputeMechanism::Court,
Error::<T>::MarketDoesNotHaveCourtMechanism
Expand Down Expand Up @@ -427,7 +424,9 @@ mod pallet {
//
// Result is capped to `usize::MAX` or in other words, capped to a very, very, very
// high number of jurors.
fn necessary_jurors_num(disputes: &[MarketDispute<T::AccountId, T::BlockNumber>]) -> usize {
fn necessary_jurors_num(
disputes: &[MarketDispute<T::AccountId, T::BlockNumber, BalanceOf<T>>],
) -> usize {
let len = disputes.len();
INITIAL_JURORS_NUM.saturating_add(SUBSEQUENT_JURORS_FACTOR.saturating_mul(len))
}
Expand Down Expand Up @@ -545,7 +544,7 @@ mod pallet {
type Moment = MomentOf<T>;
type Origin = T::Origin;

fn on_dispute(market_id: &Self::MarketId, market: &MarketOf<T>) -> DispatchResult {
fn on_dispute(_: &Self::MarketId, market: &MarketOf<T>) -> DispatchResult {
ensure!(
market.dispute_mechanism == MarketDisputeMechanism::Court,
Error::<T>::MarketDoesNotHaveCourtMechanism
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 @@ -52,7 +52,7 @@ const DEFAULT_MARKET: MarketOf<Runtime> = Market {
resolved_outcome: None,
status: MarketStatus::Disputed,
scoring_rule: ScoringRule::CPMM,
bonds: MarketBonds { creation: None, oracle: None },
bonds: MarketBonds { creation: None, oracle: None, dispute: None },
};
const DEFAULT_SET_OF_JURORS: &[(u128, Juror)] = &[
(7, Juror { status: JurorStatus::Ok }),
Expand Down
2 changes: 1 addition & 1 deletion zrml/market-commons/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const MARKET_DUMMY: Market<AccountIdTest, Balance, BlockNumber, Moment> = Market
resolved_outcome: None,
scoring_rule: ScoringRule::CPMM,
status: MarketStatus::Disputed,
bonds: MarketBonds { creation: None, oracle: None },
bonds: MarketBonds { creation: None, oracle: None, dispute: None },
};

#[test]
Expand Down
51 changes: 31 additions & 20 deletions zrml/prediction-markets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ mod pallet {
constants::MILLISECS_PER_BLOCK,
traits::{DisputeApi, DisputeResolutionApi, Swaps, ZeitgeistAssetManager},
types::{
Asset, Bond, Deadlines, Market, MarketBonds, MarketCreation, MarketDispute,
MarketDisputeMechanism, MarketPeriod, MarketStatus, MarketType, MultiHash,
OutcomeReport, Report, ScalarPosition, ScoringRule, SubsidyUntil,
Asset, Bond, Deadlines, Market, MarketBonds, MarketCreation, MarketDisputeMechanism,
MarketPeriod, MarketStatus, MarketType, MultiHash, OldMarketDispute, OutcomeReport,
Report, ScalarPosition, ScoringRule, SubsidyUntil,
},
};
#[cfg(feature = "with-global-disputes")]
Expand Down Expand Up @@ -520,7 +520,7 @@ mod pallet {
origin: OriginFor<T>,
#[pallet::compact] market_id: MarketIdOf<T>,
) -> DispatchResultWithPostInfo {
ensure_signed(origin)?;
let who = ensure_signed(origin)?;

let market = <zrml_market_commons::Pallet<T>>::market(&market_id)?;
ensure!(market.status == MarketStatus::Reported, Error::<T>::InvalidMarketStatus);
Expand All @@ -536,7 +536,15 @@ mod pallet {
}
}

Self::set_market_as_disputed(&market, &market_id)?;
let dispute_bond = T::DisputeBond::get();

T::AssetManager::reserve_named(&Self::reserve_id(), Asset::Ztg, &who, dispute_bond)?;

<zrml_market_commons::Pallet<T>>::mutate_market(&market_id, |m| {
m.status = MarketStatus::Disputed;
m.bonds.dispute = Some(Bond::new(who.clone(), dispute_bond));
Ok(())
})?;

Self::deposit_event(Event::MarketDisputed(market_id, MarketStatus::Disputed));

Expand Down Expand Up @@ -650,10 +658,12 @@ mod pallet {
MarketCreation::Advised => MarketBonds {
creation: Some(Bond::new(sender.clone(), T::AdvisoryBond::get())),
oracle: Some(Bond::new(sender.clone(), T::OracleBond::get())),
..Default::default()
},
MarketCreation::Permissionless => MarketBonds {
creation: Some(Bond::new(sender.clone(), T::ValidityBond::get())),
oracle: Some(Bond::new(sender.clone(), T::OracleBond::get())),
..Default::default()
},
};

Expand Down Expand Up @@ -1399,6 +1409,10 @@ mod pallet {
/// The origin that is allowed to destroy markets.
type DestroyOrigin: EnsureOrigin<Self::Origin>;

/// The base amount of currency that must be bonded in order to create a dispute.
#[pallet::constant]
type DisputeBond: Get<BalanceOf<Self>>;

/// Event
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;

Expand Down Expand Up @@ -1793,7 +1807,7 @@ mod pallet {
_,
Blake2_128Concat,
MarketIdOf<T>,
BoundedVec<MarketDispute<T::AccountId, T::BlockNumber>, T::MaxDisputes>,
BoundedVec<OldMarketDispute<T::AccountId, T::BlockNumber>, T::MaxDisputes>,
ValueQuery,
>;

Expand Down Expand Up @@ -1879,8 +1893,10 @@ mod pallet {
impl<T: Config> Pallet<T> {
impl_unreserve_bond!(unreserve_creation_bond, creation);
impl_unreserve_bond!(unreserve_oracle_bond, oracle);
impl_unreserve_bond!(unreserve_dispute_bond, dispute);
impl_slash_bond!(slash_creation_bond, creation);
impl_slash_bond!(slash_oracle_bond, oracle);
impl_slash_bond!(slash_dispute_bond, dispute);

pub fn outcome_assets(
market_id: MarketIdOf<T>,
Expand Down Expand Up @@ -2340,6 +2356,15 @@ mod pallet {
// overall_imbalance.subsume(imbalance);
}

if report.outcome != resolved_outcome {
// If the report outcome was wrong, the dispute was justified
Self::unreserve_dispute_bond(market_id)?;
} else {
// TODO what should be done with the DisputeBond imbalance?
let _imbalance = Self::slash_dispute_bond(market_id, None)?;
// overall_imbalance.subsume(imbalance);
}

Ok(resolved_outcome)
}

Expand Down Expand Up @@ -2621,20 +2646,6 @@ mod pallet {
))
}

// If the market is already disputed, does nothing.
fn set_market_as_disputed(
market: &MarketOf<T>,
market_id: &MarketIdOf<T>,
) -> DispatchResult {
if market.status != MarketStatus::Disputed {
<zrml_market_commons::Pallet<T>>::mutate_market(market_id, |m| {
m.status = MarketStatus::Disputed;
Ok(())
})?;
}
Ok(())
}

// If a market has a pool that is `Active`, then changes from `Active` to `Clean`. If
// the market does not exist or the market does not have a pool, does nothing.
fn clean_up_pool(
Expand Down
8 changes: 7 additions & 1 deletion zrml/prediction-markets/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl<T: Config + zrml_market_commons::Config> OnRuntimeUpgrade for RecordBonds<T
resolved_outcome: old_market.resolved_outcome,
dispute_mechanism: old_market.dispute_mechanism,
deadlines: old_market.deadlines,
bonds: MarketBonds { creation, oracle },
bonds: MarketBonds { creation, oracle, dispute: None },
};
(key, new_market)
})
Expand Down Expand Up @@ -318,6 +318,7 @@ mod tests {
value: <Runtime as Config>::OracleBond::get(),
is_settled: false,
}),
dispute: None,
},
),
construct_markets(
Expand All @@ -334,6 +335,7 @@ mod tests {
value: <Runtime as Config>::OracleBond::get(),
is_settled: true,
}),
dispute: None,
},
),
construct_markets(
Expand All @@ -350,6 +352,7 @@ mod tests {
value: <Runtime as Config>::OracleBond::get(),
is_settled: false,
}),
dispute: None,
},
),
construct_markets(
Expand All @@ -366,6 +369,7 @@ mod tests {
value: <Runtime as Config>::OracleBond::get(),
is_settled: false,
}),
dispute: None,
},
),
construct_markets(
Expand All @@ -382,6 +386,7 @@ mod tests {
value: <Runtime as Config>::OracleBond::get(),
is_settled: true,
}),
dispute: None,
},
),
// Technically, the market below has the wrong scoring rule, but that's irrelevant to
Expand All @@ -400,6 +405,7 @@ mod tests {
value: <Runtime as Config>::OracleBond::get(),
is_settled: true,
}),
dispute: None,
},
),
]
Expand Down
Loading