From 9747e90b6881955828288056b72bc54ee431bab7 Mon Sep 17 00:00:00 2001 From: Sergej Date: Fri, 23 Aug 2024 15:07:27 +0200 Subject: [PATCH] Cannot fulfill expired order --- pallets/orders/src/benchmarking.rs | 17 +++++++++++------ pallets/orders/src/lib.rs | 28 ++++++++++++++++------------ pallets/processor/src/lib.rs | 4 ++++ primitives/order/src/lib.rs | 3 +++ 4 files changed, 34 insertions(+), 18 deletions(-) diff --git a/pallets/orders/src/benchmarking.rs b/pallets/orders/src/benchmarking.rs index 639f9f1..87e59ea 100644 --- a/pallets/orders/src/benchmarking.rs +++ b/pallets/orders/src/benchmarking.rs @@ -128,7 +128,7 @@ mod benchmarks { let para_id: ParaId = 2000.into(); let requirements = Requirements { begin: 0, - end: 8, + end: 0, core_occupancy: 28800, // Half of a core. }; @@ -143,12 +143,17 @@ mod benchmarks { para_id, requirements, )?; - crate::Pallet::::contribute( - RawOrigin::Signed(creator.clone()).into(), - 0, - ::MinimumContribution::get(), + + // manually contribute since the order 'expired': + <::Currency as Currency>::transfer( + &creator.clone(), + &T::OrderToAccountId::convert(0), + ::MinimumContribution::get(), + ExistenceRequirement::KeepAlive, )?; - crate::Pallet::::do_cancel_order(0, 10)?; + Contributions::::insert(0, creator.clone(), ::MinimumContribution::get()); + + crate::Pallet::::do_cancel_order(0)?; #[extrinsic_call] _(RawOrigin::Signed(creator.clone()), 0); diff --git a/pallets/orders/src/lib.rs b/pallets/orders/src/lib.rs index f7d5902..677b2bc 100644 --- a/pallets/orders/src/lib.rs +++ b/pallets/orders/src/lib.rs @@ -185,7 +185,7 @@ pub mod pallet { pub fn cancel_order(origin: OriginFor, order_id: OrderId) -> DispatchResult { let who = ensure_signed(origin)?; - Self::do_cancel_order(order_id, Self::current_timeslice())?; + Self::do_cancel_order(order_id)?; Self::deposit_event(Event::OrderRemoved { order_id, by: who }); Ok(()) @@ -275,19 +275,11 @@ pub mod pallet { Ok(()) } - pub(crate) fn do_cancel_order( - order_id: OrderId, - current_timeslice: Timeslice, - ) -> DispatchResult { + pub(crate) fn do_cancel_order(order_id: OrderId) -> DispatchResult { let order = Orders::::get(order_id).ok_or(Error::::InvalidOrderId)?; + // Only expired orders can be cancelled. + ensure!(Self::order_expired(&order), Error::::NotAllowed); - // Allowing order cancellation 1 timeslice before it truly expires makes writing - // benchmarks much easier. With this we can set the start and end to 0 and be able to - // cancel the order without having to modify the current timeslice. - #[cfg(feature = "runtime-benchmarks")] - ensure!(order.requirements.end <= current_timeslice, Error::::NotAllowed); - #[cfg(not(feature = "runtime-benchmarks"))] - ensure!(order.requirements.end < current_timeslice, Error::::NotAllowed); Orders::::remove(order_id); Ok(()) } @@ -304,6 +296,18 @@ pub mod pallet { Orders::::get(order_id) } + fn order_expired(order: &Order) -> bool { + // Allowing order cancellation 1 timeslice before it truly expires makes writing + // benchmarks much easier. With this we can set the start and end to 0 and be able to + // cancel the order without having to modify the current timeslice. + + #[cfg(feature = "runtime-benchmarks")] + return order.requirements.end <= Self::current_timeslice(); + + #[cfg(not(feature = "runtime-benchmarks"))] + return order.requirements.end < Self::current_timeslice(); + } + fn remove_order(order_id: &OrderId) { Orders::::remove(order_id) } diff --git a/pallets/processor/src/lib.rs b/pallets/processor/src/lib.rs index f743b7d..35e6dfc 100644 --- a/pallets/processor/src/lib.rs +++ b/pallets/processor/src/lib.rs @@ -154,6 +154,8 @@ pub mod pallet { NotOwner, /// We didn't find the task to which the region is supposed to be assigned. RegionAssignmentNotFound, + /// The order user tried to fulfill expired. + OrderExpired, } #[pallet::call] @@ -187,7 +189,9 @@ pub mod pallet { ensure!(region.owner == who, Error::::NotOwner); let record = region.record.get().ok_or(Error::::RecordUnavailable)?; + let order = T::Orders::order(&order_id).ok_or(Error::::UnknownOrder)?; + ensure!(!T::Orders::order_expired(&order), Error::::OrderExpired); Self::ensure_matching_requirements(region_id, record, order.requirements)?; diff --git a/primitives/order/src/lib.rs b/primitives/order/src/lib.rs index 3fba74f..14a2f85 100644 --- a/primitives/order/src/lib.rs +++ b/primitives/order/src/lib.rs @@ -52,6 +52,9 @@ pub trait OrderInspect { /// If `None` the order was not found. fn order(order_id: &OrderId) -> Option>; + /// Returns whether an order expired or not. + fn order_expired(order_id: &Order) -> bool; + /// Remove an order with the associated id. fn remove_order(order_id: &OrderId); }