Skip to content

Commit

Permalink
Add remove_assignment extrinsic
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Brown committed Jan 8, 2025
1 parent 4059282 commit 1103644
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
27 changes: 27 additions & 0 deletions substrate/frame/broker/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,33 @@ mod benches {
Ok(())
}

#[benchmark]
fn remove_assignment() -> Result<(), BenchmarkError> {
setup_and_start_sale::<T>()?;

advance_to::<T>(2);

let caller: T::AccountId = whitelisted_caller();
T::Currency::set_balance(
&caller.clone(),
T::Currency::minimum_balance().saturating_add(10_000_000u32.into()),
);

let region = Broker::<T>::do_purchase(caller.clone(), 10_000_000u32.into())
.expect("Offer not high enough for configuration.");

Broker::<T>::do_assign(region, None, 1000, Provisional)
.map_err(|_| BenchmarkError::Weightless)?;

let origin =
T::AdminOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;

#[extrinsic_call]
_(origin as T::RuntimeOrigin, region);

Ok(())
}

// Implements a test for each benchmark. Execute with:
// `cargo test -p pallet-broker --features runtime-benchmarks`.
impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test);
Expand Down
8 changes: 8 additions & 0 deletions substrate/frame/broker/src/dispatchable_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,14 @@ impl<T: Config> Pallet<T> {
Ok(())
}

pub(crate) fn do_remove_assignment(region_id: RegionId) -> DispatchResult {
let workplan_key = (region_id.begin, region_id.core);
ensure!(Workplan::<T>::contains_key(&workplan_key), Error::<T>::AssignmentNotFound);
Workplan::<T>::remove(&workplan_key);
Self::deposit_event(Event::<T>::AssignmentRemoved { region_id });
Ok(())
}

pub(crate) fn do_pool(
region_id: RegionId,
maybe_check_owner: Option<T::AccountId>,
Expand Down
17 changes: 17 additions & 0 deletions substrate/frame/broker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ pub mod pallet {
/// The task to which the Region was assigned.
task: TaskId,
},
/// An assignment has been removed from the workplan.
AssignmentRemoved {
/// The Region which was removed from the workplan.
region_id: RegionId,
},
/// A Region has been added to the Instantaneous Coretime Pool.
Pooled {
/// The Region which was added to the Instantaneous Coretime Pool.
Expand Down Expand Up @@ -544,6 +549,8 @@ pub mod pallet {
SovereignAccountNotFound,
/// Attempted to disable auto-renewal for a core that didn't have it enabled.
AutoRenewalNotEnabled,
/// Attempted to force remove an assignment that doesn't exist.
AssignmentNotFound,
}

#[derive(frame_support::DefaultNoBound)]
Expand Down Expand Up @@ -969,6 +976,16 @@ pub mod pallet {
Ok(Pays::No.into())
}

/// Remove an assignment from the Workplan.
///
/// - `origin`: Must be Root or pass `AdminOrigin`.
/// - `region_id`: The Region to be removed from the workplan.
#[pallet::call_index(26)]
pub fn remove_assignment(origin: OriginFor<T>, region_id: RegionId) -> DispatchResult {
T::AdminOrigin::ensure_origin_or_root(origin)?;
Self::do_remove_assignment(region_id)
}

#[pallet::call_index(99)]
#[pallet::weight(T::WeightInfo::swap_leases())]
pub fn swap_leases(origin: OriginFor<T>, id: TaskId, other: TaskId) -> DispatchResult {
Expand Down
11 changes: 11 additions & 0 deletions substrate/frame/broker/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1818,6 +1818,17 @@ fn disable_auto_renew_works() {
});
}

#[test]
fn remove_assignment_works() {
TestExt::new().endow(1, 1000).execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
let region = Broker::do_purchase(1, u64::max_value()).unwrap();
assert_ok!(Broker::do_assign(region, Some(1), 1001, Provisional));
assert_ok!(Broker::do_remove_assignment(region));
});
}

#[test]
fn start_sales_sets_correct_core_count() {
TestExt::new().endow(1, 1000).execute_with(|| {
Expand Down
9 changes: 9 additions & 0 deletions substrate/frame/broker/src/weights.rs

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

0 comments on commit 1103644

Please sign in to comment.