Skip to content

Commit

Permalink
Merge branch 'chralt98-court-overhaul' into chralt98-fix-global-disputes
Browse files Browse the repository at this point in the history
  • Loading branch information
Chralt98 committed Aug 23, 2023
2 parents 30dc38f + 7b399bb commit b792304
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
3 changes: 2 additions & 1 deletion runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ macro_rules! decl_common_types {
type Migrations = (
pallet_contracts::Migration<Runtime>,
pallet_scheduler::migration::v4::CleanupAgendas<Runtime>,
zrml_prediction_markets::migrations::AddOutsiderAndDisputeBond<Runtime>,
// IMPORTANT that AddDisputeBond comes before MoveDataToSimpleDisputes!!!
zrml_prediction_markets::migrations::AddDisputeBond<Runtime>,
zrml_prediction_markets::migrations::MoveDataToSimpleDisputes<Runtime>,
// TODO check when to execute this migration and what happens for main-net, since global-disputes was only present on battery station
zrml_global_disputes::migrations::ModifyGlobalDisputesStructures<Runtime>,
Expand Down
12 changes: 10 additions & 2 deletions zrml/court/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,15 @@ mod pallet {
MaxCourtIdReached,
/// The caller has not enough funds to join the court with the specified amount.
AmountExceedsBalance,
/// After the first join of the court the amount has to be higher than the current stake.
/// After the first join of the court the amount has to be equal or higher than the current stake.
/// This is to ensure the slashable amount in active court rounds
/// is still smaller or equal to the stake.
/// It is also necessary to calculate the `unconsumed` stake properly.
/// Otherwise a juror could just reduce the probability to get selected whenever they want.
/// But this has to be done by `prepare_exit_court` and `exit_court`.
/// Additionally, the `join_court` and `delegate` extrinsics
/// use `extend_lock` and not `set_lock` or `remove_lock`.
/// This means those extrinsics are not meant to get out, but only to get into the court.
AmountBelowLastJoin,
/// The amount is too low to kick the lowest juror out of the stake-weighted pool.
AmountBelowLowestJuror,
Expand Down Expand Up @@ -1161,7 +1169,7 @@ mod pallet {
let mut joined_at = now;

if let Some(prev_p_info) = <Participants<T>>::get(who) {
ensure!(amount > prev_p_info.stake, Error::<T>::AmountBelowLastJoin);
ensure!(amount >= prev_p_info.stake, Error::<T>::AmountBelowLastJoin);

if let Some((index, pool_item)) = Self::get_pool_item(&pool, prev_p_info.stake, who)
{
Expand Down
1 change: 1 addition & 0 deletions zrml/prediction-markets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1986,6 +1986,7 @@ mod pallet {
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(PhantomData<T>);

// TODO after storage migration of release-dispute-system branch is complete, delete this Disputes storage item
/// For each market, this holds the dispute information for each dispute that's
/// been issued.
#[pallet::storage]
Expand Down
33 changes: 16 additions & 17 deletions zrml/prediction-markets/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const MARKET_COMMONS_NEXT_STORAGE_VERSION: u16 = 6;
pub struct OldMarketBonds<AI, BA> {
pub creation: Option<Bond<AI, BA>>,
pub oracle: Option<Bond<AI, BA>>,
pub outsider: Option<Bond<AI, BA>>,
}

#[derive(Clone, Decode, Encode, Eq, PartialEq, RuntimeDebug, TypeInfo)]
Expand Down Expand Up @@ -91,21 +92,21 @@ pub(crate) type Markets<T: Config> = StorageMap<
OldMarketOf<T>,
>;

pub struct AddOutsiderAndDisputeBond<T>(PhantomData<T>);
pub struct AddDisputeBond<T>(PhantomData<T>);

impl<T: Config + zrml_market_commons::Config> OnRuntimeUpgrade for AddOutsiderAndDisputeBond<T> {
impl<T: Config + zrml_market_commons::Config> OnRuntimeUpgrade for AddDisputeBond<T> {
fn on_runtime_upgrade() -> Weight {
let mut total_weight = T::DbWeight::get().reads(1);
let market_commons_version = StorageVersion::get::<MarketCommonsPallet<T>>();
if market_commons_version != MARKET_COMMONS_REQUIRED_STORAGE_VERSION {
log::info!(
"AddOutsiderAndDisputeBond: market-commons version is {:?}, but {:?} is required",
"AddDisputeBond: market-commons version is {:?}, but {:?} is required",
market_commons_version,
MARKET_COMMONS_REQUIRED_STORAGE_VERSION,
);
return total_weight;
}
log::info!("AddOutsiderAndDisputeBond: Starting...");
log::info!("AddDisputeBond: Starting...");

let mut translated = 0u64;
zrml_market_commons::Markets::<T>::translate::<OldMarketOf<T>, _>(
Expand Down Expand Up @@ -141,21 +142,21 @@ impl<T: Config + zrml_market_commons::Config> OnRuntimeUpgrade for AddOutsiderAn
bonds: MarketBonds {
creation: old_market.bonds.creation,
oracle: old_market.bonds.oracle,
outsider: None,
outsider: old_market.bonds.outsider,
dispute: dispute_bond,
},
};

Some(new_market)
},
);
log::info!("AddOutsiderAndDisputeBond: Upgraded {} markets.", translated);
log::info!("AddDisputeBond: Upgraded {} markets.", translated);
total_weight =
total_weight.saturating_add(T::DbWeight::get().reads_writes(translated, translated));

StorageVersion::new(MARKET_COMMONS_NEXT_STORAGE_VERSION).put::<MarketCommonsPallet<T>>();
total_weight = total_weight.saturating_add(T::DbWeight::get().writes(1));
log::info!("AddOutsiderAndDisputeBond: Done!");
log::info!("AddDisputeBond: Done!");
total_weight
}

Expand Down Expand Up @@ -211,8 +212,8 @@ impl<T: Config + zrml_market_commons::Config> OnRuntimeUpgrade for AddOutsiderAn
assert_eq!(new_market.dispute_mechanism, old_market.dispute_mechanism);
assert_eq!(new_market.bonds.oracle, old_market.bonds.oracle);
assert_eq!(new_market.bonds.creation, old_market.bonds.creation);
assert_eq!(new_market.bonds.outsider, old_market.bonds.outsider);
// new fields
assert_eq!(new_market.bonds.outsider, None);
// other dispute mechanisms are regarded in the migration after this migration
if let MarketDisputeMechanism::Authorized = new_market.dispute_mechanism {
let old_disputes = crate::Disputes::<T>::get(market_id);
Expand All @@ -232,10 +233,7 @@ impl<T: Config + zrml_market_commons::Config> OnRuntimeUpgrade for AddOutsiderAn
}
}

log::info!(
"AddOutsiderAndDisputeBond: Market Counter post-upgrade is {}!",
new_market_count
);
log::info!("AddDisputeBond: Market Counter post-upgrade is {}!", new_market_count);
assert!(new_market_count > 0);
Ok(())
}
Expand All @@ -257,7 +255,7 @@ mod tests {
fn on_runtime_upgrade_increments_the_storage_version() {
ExtBuilder::default().build().execute_with(|| {
set_up_version();
AddOutsiderAndDisputeBond::<Runtime>::on_runtime_upgrade();
AddDisputeBond::<Runtime>::on_runtime_upgrade();
assert_eq!(
StorageVersion::get::<MarketCommonsPallet<Runtime>>(),
MARKET_COMMONS_NEXT_STORAGE_VERSION
Expand All @@ -275,7 +273,7 @@ mod tests {
MARKETS,
new_markets.clone(),
);
AddOutsiderAndDisputeBond::<Runtime>::on_runtime_upgrade();
AddDisputeBond::<Runtime>::on_runtime_upgrade();
let actual = <zrml_market_commons::Pallet<Runtime>>::market(&0u128).unwrap();
assert_eq!(actual, new_markets[0]);
});
Expand All @@ -291,7 +289,7 @@ mod tests {
MARKETS,
old_markets,
);
AddOutsiderAndDisputeBond::<Runtime>::on_runtime_upgrade();
AddDisputeBond::<Runtime>::on_runtime_upgrade();
let actual = <zrml_market_commons::Pallet<Runtime>>::market(&0u128).unwrap();
assert_eq!(actual, new_markets[0]);
});
Expand All @@ -313,7 +311,7 @@ mod tests {
MARKETS,
old_markets,
);
AddOutsiderAndDisputeBond::<Runtime>::on_runtime_upgrade();
AddDisputeBond::<Runtime>::on_runtime_upgrade();
let actual = <zrml_market_commons::Pallet<Runtime>>::market(&0u128).unwrap();
assert_eq!(actual, new_markets[0]);
});
Expand Down Expand Up @@ -344,12 +342,13 @@ mod tests {
let old_bonds = OldMarketBonds {
creation: Some(Bond::new(creator, <Runtime as Config>::ValidityBond::get())),
oracle: Some(Bond::new(creator, <Runtime as Config>::OracleBond::get())),
outsider: Some(Bond::new(creator, <Runtime as Config>::OutsiderBond::get())),
};
let dispute_bond = disputor.map(|disputor| Bond::new(disputor, DisputeBond::get()));
let new_bonds = MarketBonds {
creation: Some(Bond::new(creator, <Runtime as Config>::ValidityBond::get())),
oracle: Some(Bond::new(creator, <Runtime as Config>::OracleBond::get())),
outsider: None,
outsider: Some(Bond::new(creator, <Runtime as Config>::OutsiderBond::get())),
dispute: dispute_bond,
};

Expand Down

0 comments on commit b792304

Please sign in to comment.