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 I #862

Merged
merged 103 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from 98 commits
Commits
Show all changes
103 commits
Select commit Hold shift + click to select a range
0c3e70d
use future resolution inside pm-pallet
Chralt98 Oct 31, 2022
7e77047
wip
Chralt98 Nov 1, 2022
b16eb03
wip
Chralt98 Nov 1, 2022
d20682d
prepare storage migration
Chralt98 Nov 1, 2022
999a2fc
add UpdateMarketIdsPerDisputeBlock migration
Chralt98 Nov 1, 2022
2d69ad2
wip
Chralt98 Nov 1, 2022
19585bf
avoid in place migration
Chralt98 Nov 2, 2022
1e7fd66
default to oracle report for authorized
Chralt98 Nov 2, 2022
c9adaf3
wip
Chralt98 Nov 2, 2022
500060d
wip
Chralt98 Nov 2, 2022
d438701
change test
Chralt98 Nov 3, 2022
26b5dde
fix benchmark
Chralt98 Nov 3, 2022
52fa258
add resolve_failed_mdm benchmarks
Chralt98 Nov 3, 2022
d74c949
Merge branch 'main' into chralt98-mdm-resolution2
Chralt98 Nov 4, 2022
773fe57
correct benchmarks
Chralt98 Nov 4, 2022
8428c24
fix nightly clippy
Chralt98 Nov 4, 2022
ec9260c
Merge branch 'main' into chralt98-mdm-resolution2
Chralt98 Nov 5, 2022
e724498
fix after merge
Chralt98 Nov 5, 2022
db7670d
fix admin for Authorized
Chralt98 Nov 7, 2022
8b015f7
Merge branch 'main' into chralt98-mdm-resolution2
Chralt98 Nov 24, 2022
535ea00
fix global disputes
Chralt98 Nov 24, 2022
81db3ee
add migration tests
Chralt98 Nov 25, 2022
a02b3c9
delete 0.3.7 storage migration
Chralt98 Nov 25, 2022
fedd819
cargo fmt
Chralt98 Nov 25, 2022
818c68a
modify storage versions to test try-runtime
Chralt98 Nov 25, 2022
4a4f2d4
edit doc comment
Chralt98 Dec 5, 2022
722d29c
simplify doc comment
Chralt98 Dec 5, 2022
7281007
update benchmark authorize outcome
Chralt98 Dec 5, 2022
7059171
avoid indentation
Chralt98 Dec 5, 2022
39b2bbf
rename AuthorityReportPeriod to ReportPeriod
Chralt98 Dec 5, 2022
04fce1b
use ensure
Chralt98 Dec 5, 2022
55dc26c
check benchmark
Chralt98 Dec 5, 2022
e54dc31
remove where clauses
Chralt98 Dec 5, 2022
3466a73
Apply suggestions from code review
Chralt98 Dec 5, 2022
556d0f5
Merge branch 'main' into chralt98-mdm-resolution2
Chralt98 Dec 5, 2022
45e51ba
add empty commit
Chralt98 Dec 6, 2022
9654780
use Weight instead of u64
Chralt98 Dec 6, 2022
27721f6
resolve doc comment
Chralt98 Dec 6, 2022
56d0319
rename MarketCommonsAuthorized to MarketCommons
Chralt98 Dec 6, 2022
6628a46
fmt
Chralt98 Dec 6, 2022
a725181
allow only one dispute for authorized
Chralt98 Dec 7, 2022
31af8e8
Merge branch 'main' into chralt98-mdm-resolution2
Chralt98 Dec 7, 2022
09ab27b
clippy
Chralt98 Dec 8, 2022
02ecf9c
fix benchmarks after one dispute limit authorized
Chralt98 Dec 8, 2022
7f6bc00
fix clippy
Chralt98 Dec 8, 2022
7c7098c
add try-runtime tests
Chralt98 Dec 8, 2022
b618605
correct benchmarks
Chralt98 Dec 8, 2022
25d2cfd
down tracing-core to get try-runtime logs
Chralt98 Dec 9, 2022
5e29644
rename failed to expired
Chralt98 Dec 13, 2022
fbf45ab
rename resolution to resolve_at
Chralt98 Dec 13, 2022
b038a96
fix small nitpicks
Chralt98 Dec 13, 2022
a948faa
edit doc comment
Chralt98 Dec 13, 2022
30ce824
use reporting period from last dispute
Chralt98 Dec 13, 2022
d539bf6
modify doc string
Chralt98 Dec 13, 2022
a99b8bc
document private authorozed api
Chralt98 Dec 13, 2022
580bc07
Merge branch 'main' into chralt98-mdm-resolution2
Chralt98 Dec 14, 2022
846d926
fix after merge
Chralt98 Dec 14, 2022
8a8c4b9
print market id for try-runtime tests
Chralt98 Dec 14, 2022
346d892
bump migration versions
Chralt98 Dec 14, 2022
0c00e8f
avoid storage_iter
Chralt98 Dec 14, 2022
e8761d2
add warning
Chralt98 Dec 14, 2022
ac8a7e5
set block number non-zero in migration test
Chralt98 Dec 14, 2022
e603788
move storage migration
Chralt98 Dec 14, 2022
1812e19
recalc migration markets counter
Chralt98 Dec 14, 2022
5fbec34
abstract remove auto resolve
Chralt98 Dec 14, 2022
65be714
fmt
Chralt98 Dec 14, 2022
e5d59ed
remove TODO
Chralt98 Dec 14, 2022
fb48a7e
add expired report period check
Chralt98 Dec 14, 2022
7657bf6
rename to has_failed
Chralt98 Dec 14, 2022
5f036d3
satisfy clippy
Chralt98 Dec 14, 2022
5f3fb41
check dispute resolution blocks
Chralt98 Dec 16, 2022
de1c7d2
use mock storage with unsafe
Chralt98 Dec 16, 2022
f9eb238
use MarketIdsPerDisputeBlock inside mock
Chralt98 Dec 19, 2022
0f3b1e1
prepare authority account id
Chralt98 Dec 19, 2022
9d7f8e2
correct benchmarks
Chralt98 Dec 19, 2022
ccfa04f
add mock storage
Chralt98 Dec 21, 2022
cb038d8
correct start_global_disputes benchmark
Chralt98 Dec 21, 2022
8b81a11
improve migration
Chralt98 Dec 21, 2022
eb7b533
remove fallible authority feature
Chralt98 Dec 22, 2022
05fdef7
remove fallible authority feature
Chralt98 Dec 22, 2022
9d8d3cb
avoid option storage query, handle 432 market
Chralt98 Jan 2, 2023
c127139
Update zrml/authorized/src/mock_storage.rs
Chralt98 Jan 4, 2023
9aa93c4
fix resolve_failed_mdm bug
Chralt98 Jan 4, 2023
c4b623a
Update zrml/prediction-markets/src/tests.rs
Chralt98 Jan 4, 2023
2398ce0
Merge branch 'main' into chralt98-mdm-resolution2
Chralt98 Jan 4, 2023
532b686
fixes after merge
Chralt98 Jan 4, 2023
193dc1b
avoid refreshing correction period
Chralt98 Jan 4, 2023
bce0c39
add description for the changes made
Chralt98 Jan 4, 2023
ceddfa9
remove resolve_failed_mdm
Chralt98 Jan 5, 2023
29a684d
use ord_parameter_types macro
Chralt98 Jan 5, 2023
7a6eaad
check auto resolve in case of existing report
Chralt98 Jan 5, 2023
2651517
Merge branch 'main' into chralt98-mdm-resolution2
Chralt98 Jan 6, 2023
33b554b
Merge branch 'main' into chralt98-mdm-resolution2
Chralt98 Jan 6, 2023
c67a4a1
handle market 432
Chralt98 Jan 6, 2023
956f3f4
Merge branch 'main' into chralt98-mdm-resolution2
Chralt98 Jan 6, 2023
8c79cf6
correct pm disputes authorized benchmarks
Chralt98 Jan 6, 2023
c46c579
fix clippy
Chralt98 Jan 6, 2023
aad23fc
update pm weights
Chralt98 Jan 9, 2023
b41af6a
remove unused error
Chralt98 Jan 10, 2023
aea06f8
test only one dispute error
Chralt98 Jan 10, 2023
7ba0801
remove unused error
Chralt98 Jan 10, 2023
cfc576a
remove unused event
Chralt98 Jan 10, 2023
d328b2f
cargo fmt
Chralt98 Jan 10, 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
6 changes: 6 additions & 0 deletions docs/changelog_for_devs.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
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).
- The market dispute mechanisms are now able to control their resolution. The
`CorrectionPeriod` parameter determines how long the authorized pallet can
call `authorize_market_outcome` again after the first call to it (fat-finger
protection). The authority report now includes its resolution block number.
This is the time of the first call to `authorize_market_outcome` plus the
`CorrectionPeriod`.

# v0.3.7

Expand Down
1 change: 1 addition & 0 deletions primitives/src/constants/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use orml_traits::parameter_type_with_key;
// Authorized
parameter_types! {
pub const AuthorizedPalletId: PalletId = PalletId(*b"zge/atzd");
pub const CorrectionPeriod: BlockNumber = 4;
}

// Court
Expand Down
6 changes: 6 additions & 0 deletions primitives/src/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@ pub struct Report<AccountId, BlockNumber> {
pub outcome: OutcomeReport,
}

#[derive(Clone, Decode, Encode, Eq, MaxEncodedLen, PartialEq, RuntimeDebug, TypeInfo)]
pub struct AuthorityReport<BlockNumber> {
pub resolve_at: BlockNumber,
pub outcome: OutcomeReport,
}

/// Contains a market id and the market period.
///
/// * `BN`: Block Number
Expand Down
2 changes: 1 addition & 1 deletion primitives/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mod market_id;
mod swaps;
mod zeitgeist_multi_reservable_currency;

pub use dispute_api::DisputeApi;
pub use dispute_api::{DisputeApi, DisputeResolutionApi};
pub use market_commons_pallet_api::MarketCommonsPalletApi;
pub use market_id::MarketId;
pub use swaps::Swaps;
Expand Down
81 changes: 80 additions & 1 deletion primitives/src/traits/dispute_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// 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;
use frame_support::{dispatch::DispatchResult, pallet_prelude::Weight, BoundedVec};
use sp_runtime::DispatchError;

// Abstraction of the market type, which is not a part of `DisputeApi` because Rust doesn't support
Expand Down Expand Up @@ -60,4 +60,83 @@ pub trait DisputeApi {
market_id: &Self::MarketId,
market: &MarketOfDisputeApi<Self>,
) -> Result<Option<OutcomeReport>, DispatchError>;

/// Query the future resolution block of a disputed market.
/// **May** assume that `market.dispute_mechanism` refers to the calling dispute API.
///
/// # Returns
///
/// Returns the future resolution block if available, otherwise `None`.
fn get_auto_resolve(
maltekliemann marked this conversation as resolved.
Show resolved Hide resolved
disputes: &[MarketDispute<Self::AccountId, Self::BlockNumber>],
market_id: &Self::MarketId,
market: &MarketOfDisputeApi<Self>,
) -> Result<Option<Self::BlockNumber>, DispatchError>;

/// Returns `true` if the market dispute mechanism
/// 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>;
}

type MarketOfDisputeResolutionApi<T> = Market<
<T as DisputeResolutionApi>::AccountId,
<T as DisputeResolutionApi>::Balance,
<T as DisputeResolutionApi>::BlockNumber,
<T as DisputeResolutionApi>::Moment,
>;

pub trait DisputeResolutionApi {
type AccountId;
type Balance;
type BlockNumber;
type MarketId;
type MaxDisputes;
type Moment;

/// Resolve a market.
///
/// **Should** only be called if the market dispute
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't we cover the case when it shouldn't be called with some Error?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I'm okay either way, but an error might be cleaner than "hoping" that the caller never messes up.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, we could also remove resolve completely, since we don't use it at the moment 😄 . It is just, that we only have current scenarios, in which markets should resolve in the future.

But I can imagine, that this function is pretty useful and logical. I used it first, when I restructured authorized. The idea was to resolve the market directly, when the authority made its report.

It is up to you. Do we want to remove this API method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Malte suggested to leave it in, because it's useful and a common thing to resolve a market directly.

/// mechanism is ready for the resolution ([`DisputeApi::on_resolution`]).
///
/// # Returns
///
/// Returns the consumed weight.
fn resolve(
market_id: &Self::MarketId,
market: &MarketOfDisputeResolutionApi<Self>,
) -> Result<Weight, DispatchError>;

/// Add a future block resolution of a disputed market.
///
/// # Returns
///
/// Returns the number of elements in the storage structure.
fn add_auto_resolve(
market_id: &Self::MarketId,
resolve_at: Self::BlockNumber,
) -> Result<u32, DispatchError>;

/// Check if a future block resolution of a disputed market exists.
///
/// # Returns
///
/// Returns `true` if the future block resolution exists, otherwise `false`.
fn auto_resolve_exists(market_id: &Self::MarketId, resolve_at: Self::BlockNumber) -> bool;

/// Remove a future block resolution of a disputed market.
///
/// # Returns
///
/// 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>;
}
1 change: 1 addition & 0 deletions runtime/battery-station/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub(crate) const FEES_AND_TIPS_BURN_PERCENTAGE: u32 = 0;
parameter_types! {
// Authorized
pub const AuthorizedPalletId: PalletId = AUTHORIZED_PALLET_ID;
pub const CorrectionPeriod: BlockNumber = BLOCKS_PER_DAY;

// Authority
pub const MaxAuthorities: u32 = 32;
Expand Down
16 changes: 13 additions & 3 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ macro_rules! decl_common_types {
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
zrml_prediction_markets::migrations::RecordBonds<Runtime>,
(
zrml_prediction_markets::migrations::RecordBonds<Runtime>,
zrml_prediction_markets::migrations::AddFieldToAuthorityReport<Runtime>,
),
>;

#[cfg(not(feature = "parachain"))]
Expand All @@ -68,7 +71,10 @@ macro_rules! decl_common_types {
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
zrml_prediction_markets::migrations::RecordBonds<Runtime>,
(
zrml_prediction_markets::migrations::RecordBonds<Runtime>,
zrml_prediction_markets::migrations::AddFieldToAuthorityReport<Runtime>,
),
>;

pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
Expand Down Expand Up @@ -912,15 +918,18 @@ macro_rules! impl_config_traits {
impl parachain_info::Config for Runtime {}

impl zrml_authorized::Config for Runtime {
type AuthorizedDisputeResolutionOrigin = EnsureRootOrHalfAdvisoryCommittee;
type CorrectionPeriod = CorrectionPeriod;
type DisputeResolution = zrml_prediction_markets::Pallet<Runtime>;
type Event = Event;
type MarketCommons = MarketCommons;
type AuthorizedDisputeResolutionOrigin = EnsureRootOrHalfAdvisoryCommittee;
type PalletId = AuthorizedPalletId;
type WeightInfo = zrml_authorized::weights::WeightInfo<Runtime>;
}

impl zrml_court::Config for Runtime {
type CourtCaseDuration = CourtCaseDuration;
type DisputeResolution = zrml_prediction_markets::Pallet<Runtime>;
type Event = Event;
type MarketCommons = MarketCommons;
type PalletId = CourtPalletId;
Expand Down Expand Up @@ -1033,6 +1042,7 @@ macro_rules! impl_config_traits {
}

impl zrml_simple_disputes::Config for Runtime {
type DisputeResolution = zrml_prediction_markets::Pallet<Runtime>;
type Event = Event;
type MarketCommons = MarketCommons;
type PalletId = SimpleDisputesPalletId;
Expand Down
1 change: 1 addition & 0 deletions runtime/zeitgeist/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub(crate) const FEES_AND_TIPS_BURN_PERCENTAGE: u32 = 0;
parameter_types! {
// Authorized
pub const AuthorizedPalletId: PalletId = AUTHORIZED_PALLET_ID;
pub const CorrectionPeriod: BlockNumber = BLOCKS_PER_DAY;

// Authority
pub const MaxAuthorities: u32 = 32;
Expand Down
70 changes: 64 additions & 6 deletions zrml/authorized/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,77 @@

#[cfg(test)]
use crate::Pallet as Authorized;
use crate::{market_mock, Call, Config, Pallet};
use crate::{market_mock, AuthorizedOutcomeReports, Call, Config, Pallet};
use frame_benchmarking::benchmarks;
use frame_support::{dispatch::UnfilteredDispatchable, traits::EnsureOrigin};
use zeitgeist_primitives::types::OutcomeReport;
use frame_support::{
dispatch::UnfilteredDispatchable,
traits::{EnsureOrigin, Get},
};
use sp_runtime::traits::Saturating;
use zeitgeist_primitives::{
traits::DisputeResolutionApi,
types::{AuthorityReport, OutcomeReport},
};
use zrml_market_commons::MarketCommonsPalletApi;

benchmarks! {
authorize_market_outcome {
authorize_market_outcome_first_report {
let m in 1..63;

let origin = T::AuthorizedDisputeResolutionOrigin::successful_origin();
let market_id = 0u32.into();
let market = market_mock::<T>();
T::MarketCommons::push_market(market).unwrap();

frame_system::Pallet::<T>::set_block_number(42u32.into());
let now = frame_system::Pallet::<T>::block_number();
let correction_period_ends_at = now.saturating_add(T::CorrectionPeriod::get());
for _ in 1..=m {
let id = T::MarketCommons::push_market(market_mock::<T>()).unwrap();
T::DisputeResolution::add_auto_resolve(&id, correction_period_ends_at).unwrap();
}

let call = Call::<T>::authorize_market_outcome {
market_id,
outcome: OutcomeReport::Scalar(1),
};
}: {
call.dispatch_bypass_filter(origin)?
} verify {
let report = AuthorityReport {
resolve_at: correction_period_ends_at,
outcome: OutcomeReport::Scalar(1)
};
assert_eq!(AuthorizedOutcomeReports::<T>::get(market_id).unwrap(), report);
}

authorize_market_outcome_existing_report {
let origin = T::AuthorizedDisputeResolutionOrigin::successful_origin();
let market_id = 0u32.into();
let market = market_mock::<T>();
T::MarketCommons::push_market(market).unwrap();
let call = Call::<T>::authorize_market_outcome { market_id: 0_u32.into(), outcome: OutcomeReport::Scalar(1) };
}: { call.dispatch_bypass_filter(origin)? }

frame_system::Pallet::<T>::set_block_number(42u32.into());

let now = frame_system::Pallet::<T>::block_number();
let resolve_at = now.saturating_add(T::CorrectionPeriod::get());

let report = AuthorityReport { resolve_at, outcome: OutcomeReport::Scalar(0) };
AuthorizedOutcomeReports::<T>::insert(market_id, report);

let now = frame_system::Pallet::<T>::block_number();
frame_system::Pallet::<T>::set_block_number(now + 42u32.into());

let call = Call::<T>::authorize_market_outcome {
market_id,
outcome: OutcomeReport::Scalar(1),
};
}: {
call.dispatch_bypass_filter(origin)?
} verify {
let report = AuthorityReport { resolve_at, outcome: OutcomeReport::Scalar(1) };
assert_eq!(AuthorizedOutcomeReports::<T>::get(market_id).unwrap(), report);
}

impl_benchmark_test_suite!(
Authorized,
Expand Down
Loading