referrerpolicy=no-referrer-when-downgrade

pallet_broker/
dispatchable_impls.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18use core::cmp;
19
20use super::*;
21use frame_support::{
22	pallet_prelude::*,
23	traits::{fungible::Mutate, tokens::Preservation::Expendable, DefensiveResult},
24};
25use sp_arithmetic::traits::{CheckedDiv, Saturating, Zero};
26use sp_runtime::traits::{BlockNumberProvider, Convert};
27use CompletionStatus::{Complete, Partial};
28
29impl<T: Config> Pallet<T> {
30	pub(crate) fn do_configure(config: ConfigRecordOf<T>) -> DispatchResult {
31		config.validate().map_err(|()| Error::<T>::InvalidConfig)?;
32		Configuration::<T>::put(config);
33		Ok(())
34	}
35
36	pub(crate) fn do_request_core_count(core_count: CoreIndex) -> DispatchResult {
37		T::Coretime::request_core_count(core_count);
38		Self::deposit_event(Event::<T>::CoreCountRequested { core_count });
39		Ok(())
40	}
41
42	pub(crate) fn do_notify_core_count(core_count: CoreIndex) -> DispatchResult {
43		CoreCountInbox::<T>::put(core_count);
44		Ok(())
45	}
46
47	pub(crate) fn do_reserve(workload: Schedule) -> DispatchResult {
48		let mut r = Reservations::<T>::get();
49		let index = r.len() as u32;
50		r.try_push(workload.clone()).map_err(|_| Error::<T>::TooManyReservations)?;
51		Reservations::<T>::put(r);
52		Self::deposit_event(Event::<T>::ReservationMade { index, workload });
53		Ok(())
54	}
55
56	pub(crate) fn do_unreserve(index: u32) -> DispatchResult {
57		let mut r = Reservations::<T>::get();
58		ensure!(index < r.len() as u32, Error::<T>::UnknownReservation);
59		let workload = r.remove(index as usize);
60		Reservations::<T>::put(r);
61		Self::deposit_event(Event::<T>::ReservationCancelled { index, workload });
62		Ok(())
63	}
64
65	pub(crate) fn do_force_reserve(workload: Schedule, core: CoreIndex) -> DispatchResult {
66		// Sales must have started, otherwise reserve is equivalent.
67		let sale = SaleInfo::<T>::get().ok_or(Error::<T>::NoSales)?;
68
69		// Reserve - starts at second sale period boundary from now.
70		Self::do_reserve(workload.clone())?;
71
72		// Add to ForceReservations for dynamic core assignment in rotate_sale.
73		ForceReservations::<T>::try_mutate(|r| {
74			r.try_push(workload.clone()).map_err(|_| Error::<T>::TooManyReservations)
75		})?;
76
77		// Assign now until the next sale boundary unless the next timeslice is already the sale
78		// boundary.
79		let status = Status::<T>::get().ok_or(Error::<T>::Uninitialized)?;
80		let timeslice = status.last_committed_timeslice.saturating_add(1);
81		if timeslice < sale.region_begin {
82			Workplan::<T>::insert((timeslice, core), &workload);
83		}
84
85		Ok(())
86	}
87
88	pub(crate) fn do_set_lease(task: TaskId, until: Timeslice) -> DispatchResult {
89		let mut r = Leases::<T>::get();
90		ensure!(until > Self::current_timeslice(), Error::<T>::AlreadyExpired);
91		r.try_push(LeaseRecordItem { until, task })
92			.map_err(|_| Error::<T>::TooManyLeases)?;
93		Leases::<T>::put(r);
94		Self::deposit_event(Event::<T>::Leased { until, task });
95		Ok(())
96	}
97
98	pub(crate) fn do_remove_lease(task: TaskId) -> DispatchResult {
99		let mut r = Leases::<T>::get();
100		let i = r.iter().position(|lease| lease.task == task).ok_or(Error::<T>::LeaseNotFound)?;
101		r.remove(i);
102		Leases::<T>::put(r);
103		Self::deposit_event(Event::<T>::LeaseRemoved { task });
104		Ok(())
105	}
106
107	pub(crate) fn do_start_sales(
108		end_price: BalanceOf<T>,
109		extra_cores: CoreIndex,
110	) -> DispatchResult {
111		let config = Configuration::<T>::get().ok_or(Error::<T>::Uninitialized)?;
112
113		// Determine the core count
114		let core_count = Leases::<T>::decode_len().unwrap_or(0) as CoreIndex +
115			Reservations::<T>::decode_len().unwrap_or(0) as CoreIndex +
116			extra_cores;
117
118		Self::do_request_core_count(core_count)?;
119
120		let commit_timeslice = Self::latest_timeslice_ready_to_commit(&config);
121		let status = StatusRecord {
122			core_count,
123			private_pool_size: 0,
124			system_pool_size: 0,
125			last_committed_timeslice: commit_timeslice.saturating_sub(1),
126			last_timeslice: Self::current_timeslice(),
127		};
128		let now = RCBlockNumberProviderOf::<T::Coretime>::current_block_number();
129		// Imaginary old sale for bootstrapping the first actual sale:
130		let old_sale = SaleInfoRecord {
131			sale_start: now,
132			leadin_length: Zero::zero(),
133			end_price,
134			sellout_price: None,
135			region_begin: commit_timeslice,
136			region_end: commit_timeslice.saturating_add(config.region_length),
137			first_core: 0,
138			ideal_cores_sold: 0,
139			cores_offered: 0,
140			cores_sold: 0,
141			sale_index: 0,
142		};
143		Self::deposit_event(Event::<T>::SalesStarted { price: end_price, core_count });
144		Self::rotate_sale(old_sale, &config, &status);
145		Status::<T>::put(&status);
146		Ok(())
147	}
148
149	pub(crate) fn do_purchase(
150		who: T::AccountId,
151		price_limit: BalanceOf<T>,
152	) -> Result<RegionId, DispatchError> {
153		let status = Status::<T>::get().ok_or(Error::<T>::Uninitialized)?;
154		let mut sale = SaleInfo::<T>::get().ok_or(Error::<T>::NoSales)?;
155		Self::ensure_cores_for_sale(&status, &sale)?;
156
157		let now = RCBlockNumberProviderOf::<T::Coretime>::current_block_number();
158		ensure!(now > sale.sale_start, Error::<T>::TooEarly);
159		let price = Self::sale_price(&sale, now);
160		ensure!(price_limit >= price, Error::<T>::Overpriced);
161
162		let core = Self::purchase_core(&who, price, &mut sale)?;
163
164		SaleInfo::<T>::put(&sale);
165		let id = Self::issue(
166			core,
167			sale.region_begin,
168			CoreMask::complete(),
169			sale.region_end,
170			Some(who.clone()),
171			Some(price),
172		);
173		let duration = sale.region_end.saturating_sub(sale.region_begin);
174		Self::deposit_event(Event::Purchased { who, region_id: id, price, duration });
175		Ok(id)
176	}
177
178	/// Must be called on a core in `PotentialRenewals` whose value is a timeslice equal to the
179	/// current sale status's `region_end`.
180	pub(crate) fn do_renew(who: T::AccountId, core: CoreIndex) -> Result<CoreIndex, DispatchError> {
181		let config = Configuration::<T>::get().ok_or(Error::<T>::Uninitialized)?;
182		let status = Status::<T>::get().ok_or(Error::<T>::Uninitialized)?;
183		let mut sale = SaleInfo::<T>::get().ok_or(Error::<T>::NoSales)?;
184		Self::ensure_cores_for_sale(&status, &sale)?;
185
186		let renewal_id = PotentialRenewalId { core, when: sale.region_begin };
187		let record = PotentialRenewals::<T>::get(renewal_id).ok_or(Error::<T>::NotAllowed)?;
188		let workload =
189			record.completion.drain_complete().ok_or(Error::<T>::IncompleteAssignment)?;
190
191		let old_core = core;
192
193		let core = Self::purchase_core(&who, record.price, &mut sale)?;
194
195		Self::deposit_event(Event::Renewed {
196			who,
197			old_core,
198			core,
199			price: record.price,
200			begin: sale.region_begin,
201			duration: sale.region_end.saturating_sub(sale.region_begin),
202			workload: workload.clone(),
203		});
204
205		Workplan::<T>::insert((sale.region_begin, core), &workload);
206
207		let begin = sale.region_end;
208		let end_price = sale.end_price;
209		// Renewals should never be priced lower than the current `end_price`:
210		let price_cap = cmp::max(record.price + config.renewal_bump * record.price, end_price);
211		let now = RCBlockNumberProviderOf::<T::Coretime>::current_block_number();
212		let price = Self::sale_price(&sale, now).min(price_cap);
213		log::debug!(
214			"Renew with: sale price: {:?}, price cap: {:?}, old price: {:?}",
215			price,
216			price_cap,
217			record.price
218		);
219		let new_record = PotentialRenewalRecord { price, completion: Complete(workload) };
220		PotentialRenewals::<T>::remove(renewal_id);
221		PotentialRenewals::<T>::insert(PotentialRenewalId { core, when: begin }, &new_record);
222		SaleInfo::<T>::put(&sale);
223		if let Some(workload) = new_record.completion.drain_complete() {
224			log::debug!("Recording renewable price for next run: {:?}", price);
225			Self::deposit_event(Event::Renewable { core, price, begin, workload });
226		}
227		Ok(core)
228	}
229
230	pub(crate) fn do_transfer(
231		region_id: RegionId,
232		maybe_check_owner: Option<T::AccountId>,
233		new_owner: T::AccountId,
234	) -> Result<(), Error<T>> {
235		let mut region = Regions::<T>::get(&region_id).ok_or(Error::<T>::UnknownRegion)?;
236
237		if let Some(check_owner) = maybe_check_owner {
238			ensure!(Some(check_owner) == region.owner, Error::<T>::NotOwner);
239		}
240
241		let old_owner = region.owner;
242		region.owner = Some(new_owner);
243		Regions::<T>::insert(&region_id, &region);
244		let duration = region.end.saturating_sub(region_id.begin);
245		Self::deposit_event(Event::Transferred {
246			region_id,
247			old_owner,
248			owner: region.owner,
249			duration,
250		});
251
252		Ok(())
253	}
254
255	pub(crate) fn do_partition(
256		region_id: RegionId,
257		maybe_check_owner: Option<T::AccountId>,
258		pivot_offset: Timeslice,
259	) -> Result<(RegionId, RegionId), Error<T>> {
260		let status = Status::<T>::get().ok_or(Error::<T>::Uninitialized)?;
261		let mut region = Regions::<T>::get(&region_id).ok_or(Error::<T>::UnknownRegion)?;
262
263		if let Some(check_owner) = maybe_check_owner {
264			ensure!(Some(check_owner) == region.owner, Error::<T>::NotOwner);
265		}
266		let pivot = region_id.begin.saturating_add(pivot_offset);
267		ensure!(pivot < region.end, Error::<T>::PivotTooLate);
268		ensure!(pivot > region_id.begin, Error::<T>::PivotTooEarly);
269
270		region.paid = None;
271		let new_region_ids = (region_id, RegionId { begin: pivot, ..region_id });
272
273		// Remove this region from the pool in case it has been assigned provisionally. If we get
274		// this far then it is still in `Regions` and thus could only have been pooled
275		// provisionally.
276		Self::force_unpool_region(region_id, &region, &status);
277
278		// Overwrite the previous region with its new end and create a new region for the second
279		// part of the partition.
280		Regions::<T>::insert(&new_region_ids.0, &RegionRecord { end: pivot, ..region.clone() });
281		Regions::<T>::insert(&new_region_ids.1, &region);
282		Self::deposit_event(Event::Partitioned { old_region_id: region_id, new_region_ids });
283
284		Ok(new_region_ids)
285	}
286
287	pub(crate) fn do_interlace(
288		region_id: RegionId,
289		maybe_check_owner: Option<T::AccountId>,
290		pivot: CoreMask,
291	) -> Result<(RegionId, RegionId), Error<T>> {
292		let status = Status::<T>::get().ok_or(Error::<T>::Uninitialized)?;
293		let region = Regions::<T>::get(&region_id).ok_or(Error::<T>::UnknownRegion)?;
294
295		if let Some(check_owner) = maybe_check_owner {
296			ensure!(Some(check_owner) == region.owner, Error::<T>::NotOwner);
297		}
298
299		ensure!((pivot & !region_id.mask).is_void(), Error::<T>::ExteriorPivot);
300		ensure!(!pivot.is_void(), Error::<T>::VoidPivot);
301		ensure!(pivot != region_id.mask, Error::<T>::CompletePivot);
302
303		// Remove this region from the pool in case it has been assigned provisionally. If we get
304		// this far then it is still in `Regions` and thus could only have been pooled
305		// provisionally.
306		Self::force_unpool_region(region_id, &region, &status);
307
308		// The old region should be removed.
309		Regions::<T>::remove(&region_id);
310
311		let one = RegionId { mask: pivot, ..region_id };
312		Regions::<T>::insert(&one, &region);
313		let other = RegionId { mask: region_id.mask ^ pivot, ..region_id };
314		Regions::<T>::insert(&other, &region);
315
316		let new_region_ids = (one, other);
317		Self::deposit_event(Event::Interlaced { old_region_id: region_id, new_region_ids });
318		Ok(new_region_ids)
319	}
320
321	pub(crate) fn do_assign(
322		region_id: RegionId,
323		maybe_check_owner: Option<T::AccountId>,
324		target: TaskId,
325		finality: Finality,
326	) -> Result<(), Error<T>> {
327		let config = Configuration::<T>::get().ok_or(Error::<T>::Uninitialized)?;
328		let status = Status::<T>::get().ok_or(Error::<T>::Uninitialized)?;
329
330		if let Some((region_id, region)) = Self::utilize(region_id, maybe_check_owner, finality)? {
331			let workplan_key = (region_id.begin, region_id.core);
332			let mut workplan = Workplan::<T>::get(&workplan_key).unwrap_or_default();
333
334			// Remove this region from the pool in case it has been assigned provisionally. If we
335			// get this far then it is still in `Regions` and thus could only have been pooled
336			// provisionally.
337			Self::force_unpool_region(region_id, &region, &status);
338
339			// Ensure no previous allocations exist.
340			workplan.retain(|i| (i.mask & region_id.mask).is_void());
341			if workplan
342				.try_push(ScheduleItem {
343					mask: region_id.mask,
344					assignment: CoreAssignment::Task(target),
345				})
346				.is_ok()
347			{
348				Workplan::<T>::insert(&workplan_key, &workplan);
349			}
350
351			let duration = region.end.saturating_sub(region_id.begin);
352			if duration == config.region_length && finality == Finality::Final {
353				if let Some(price) = region.paid {
354					let renewal_id = PotentialRenewalId { core: region_id.core, when: region.end };
355					let assigned = match PotentialRenewals::<T>::get(renewal_id) {
356						Some(PotentialRenewalRecord { completion: Partial(w), price: p })
357							if price == p =>
358						{
359							w
360						},
361						_ => CoreMask::void(),
362					} | region_id.mask;
363					let workload =
364						if assigned.is_complete() { Complete(workplan) } else { Partial(assigned) };
365					let record = PotentialRenewalRecord { price, completion: workload };
366					// Note: This entry alone does not yet actually allow renewals (the completion
367					// status has to be complete for `do_renew` to accept it).
368					PotentialRenewals::<T>::insert(&renewal_id, &record);
369					if let Some(workload) = record.completion.drain_complete() {
370						Self::deposit_event(Event::Renewable {
371							core: region_id.core,
372							price,
373							begin: region.end,
374							workload,
375						});
376					}
377				}
378			}
379			Self::deposit_event(Event::Assigned { region_id, task: target, duration });
380		}
381		Ok(())
382	}
383
384	pub(crate) fn do_remove_assignment(region_id: RegionId) -> DispatchResult {
385		let workplan_key = (region_id.begin, region_id.core);
386		ensure!(Workplan::<T>::contains_key(&workplan_key), Error::<T>::AssignmentNotFound);
387		Workplan::<T>::remove(&workplan_key);
388		Self::deposit_event(Event::<T>::AssignmentRemoved { region_id });
389		Ok(())
390	}
391
392	pub(crate) fn do_pool(
393		region_id: RegionId,
394		maybe_check_owner: Option<T::AccountId>,
395		payee: T::AccountId,
396		finality: Finality,
397	) -> Result<(), Error<T>> {
398		if let Some((region_id, region)) = Self::utilize(region_id, maybe_check_owner, finality)? {
399			let workplan_key = (region_id.begin, region_id.core);
400			let mut workplan = Workplan::<T>::get(&workplan_key).unwrap_or_default();
401			let duration = region.end.saturating_sub(region_id.begin);
402			if workplan
403				.try_push(ScheduleItem { mask: region_id.mask, assignment: CoreAssignment::Pool })
404				.is_ok()
405			{
406				Workplan::<T>::insert(&workplan_key, &workplan);
407				let size = region_id.mask.count_ones() as i32;
408				InstaPoolIo::<T>::mutate(region_id.begin, |a| a.private.saturating_accrue(size));
409				InstaPoolIo::<T>::mutate(region.end, |a| a.private.saturating_reduce(size));
410				let record = ContributionRecord { length: duration, payee };
411				InstaPoolContribution::<T>::insert(&region_id, record);
412			}
413
414			Self::deposit_event(Event::Pooled { region_id, duration });
415		}
416		Ok(())
417	}
418
419	pub(crate) fn do_claim_revenue(
420		mut region: RegionId,
421		max_timeslices: Timeslice,
422	) -> DispatchResult {
423		ensure!(max_timeslices > 0, Error::<T>::NoClaimTimeslices);
424		let mut contribution =
425			InstaPoolContribution::<T>::take(region).ok_or(Error::<T>::UnknownContribution)?;
426		let contributed_parts = region.mask.count_ones();
427
428		Self::deposit_event(Event::RevenueClaimBegun { region, max_timeslices });
429
430		let mut payout = BalanceOf::<T>::zero();
431		let last = region.begin + contribution.length.min(max_timeslices);
432		for r in region.begin..last {
433			region.begin = r + 1;
434			contribution.length.saturating_dec();
435
436			let Some(mut pool_record) = InstaPoolHistory::<T>::get(r) else { continue };
437			let Some(total_payout) = pool_record.maybe_payout else { break };
438			let p = total_payout
439				.saturating_mul(contributed_parts.into())
440				.checked_div(&pool_record.private_contributions.into())
441				.unwrap_or_default();
442
443			payout.saturating_accrue(p);
444			pool_record.private_contributions.saturating_reduce(contributed_parts);
445
446			let remaining_payout = total_payout.saturating_sub(p);
447			if !remaining_payout.is_zero() && pool_record.private_contributions > 0 {
448				pool_record.maybe_payout = Some(remaining_payout);
449				InstaPoolHistory::<T>::insert(r, &pool_record);
450			} else {
451				InstaPoolHistory::<T>::remove(r);
452			}
453			if !p.is_zero() {
454				Self::deposit_event(Event::RevenueClaimItem { when: r, amount: p });
455			}
456		}
457
458		if contribution.length > 0 {
459			InstaPoolContribution::<T>::insert(region, &contribution);
460		}
461		T::Currency::transfer(&Self::account_id(), &contribution.payee, payout, Expendable)
462			.defensive_ok();
463		let next = if last < region.begin + contribution.length { Some(region) } else { None };
464		Self::deposit_event(Event::RevenueClaimPaid {
465			who: contribution.payee,
466			amount: payout,
467			next,
468		});
469		Ok(())
470	}
471
472	pub(crate) fn do_purchase_credit(
473		who: T::AccountId,
474		amount: BalanceOf<T>,
475		beneficiary: RelayAccountIdOf<T>,
476	) -> DispatchResult {
477		ensure!(amount >= T::MinimumCreditPurchase::get(), Error::<T>::CreditPurchaseTooSmall);
478		T::Currency::transfer(&who, &Self::account_id(), amount, Expendable)?;
479		let rc_amount = T::ConvertBalance::convert(amount);
480		T::Coretime::credit_account(beneficiary.clone(), rc_amount);
481		Self::deposit_event(Event::<T>::CreditPurchased { who, beneficiary, amount });
482		Ok(())
483	}
484
485	pub(crate) fn do_drop_region(region_id: RegionId) -> DispatchResult {
486		let status = Status::<T>::get().ok_or(Error::<T>::Uninitialized)?;
487		let region = Regions::<T>::get(&region_id).ok_or(Error::<T>::UnknownRegion)?;
488		ensure!(status.last_committed_timeslice >= region.end, Error::<T>::StillValid);
489
490		Regions::<T>::remove(&region_id);
491		let duration = region.end.saturating_sub(region_id.begin);
492		Self::deposit_event(Event::RegionDropped { region_id, duration });
493		Ok(())
494	}
495
496	pub(crate) fn do_drop_contribution(region_id: RegionId) -> DispatchResult {
497		let config = Configuration::<T>::get().ok_or(Error::<T>::Uninitialized)?;
498		let status = Status::<T>::get().ok_or(Error::<T>::Uninitialized)?;
499		let contrib =
500			InstaPoolContribution::<T>::get(&region_id).ok_or(Error::<T>::UnknownContribution)?;
501		let end = region_id.begin.saturating_add(contrib.length);
502		ensure!(
503			status.last_timeslice >= end.saturating_add(config.contribution_timeout),
504			Error::<T>::StillValid
505		);
506		InstaPoolContribution::<T>::remove(region_id);
507		Self::deposit_event(Event::ContributionDropped { region_id });
508		Ok(())
509	}
510
511	pub(crate) fn do_drop_history(when: Timeslice) -> DispatchResult {
512		let config = Configuration::<T>::get().ok_or(Error::<T>::Uninitialized)?;
513		let status = Status::<T>::get().ok_or(Error::<T>::Uninitialized)?;
514		ensure!(
515			status.last_timeslice > when.saturating_add(config.contribution_timeout),
516			Error::<T>::StillValid
517		);
518		let record = InstaPoolHistory::<T>::take(when).ok_or(Error::<T>::NoHistory)?;
519		if let Some(payout) = record.maybe_payout {
520			let _ = Self::charge(&Self::account_id(), payout);
521		}
522		let revenue = record.maybe_payout.unwrap_or_default();
523		Self::deposit_event(Event::HistoryDropped { when, revenue });
524		Ok(())
525	}
526
527	pub(crate) fn do_drop_renewal(core: CoreIndex, when: Timeslice) -> DispatchResult {
528		let status = Status::<T>::get().ok_or(Error::<T>::Uninitialized)?;
529		ensure!(status.last_committed_timeslice >= when, Error::<T>::StillValid);
530		let id = PotentialRenewalId { core, when };
531		ensure!(PotentialRenewals::<T>::contains_key(id), Error::<T>::UnknownRenewal);
532		PotentialRenewals::<T>::remove(id);
533		Self::deposit_event(Event::PotentialRenewalDropped { core, when });
534		Ok(())
535	}
536
537	pub(crate) fn do_notify_revenue(revenue: OnDemandRevenueRecordOf<T>) -> DispatchResult {
538		RevenueInbox::<T>::put(revenue);
539		Ok(())
540	}
541
542	pub(crate) fn do_swap_leases(id: TaskId, other: TaskId) -> DispatchResult {
543		let mut id_leases_count = 0;
544		let mut other_leases_count = 0;
545		Leases::<T>::mutate(|leases| {
546			leases.iter_mut().for_each(|lease| {
547				if lease.task == id {
548					lease.task = other;
549					id_leases_count += 1;
550				} else if lease.task == other {
551					lease.task = id;
552					other_leases_count += 1;
553				}
554			})
555		});
556		Ok(())
557	}
558
559	pub(crate) fn do_enable_auto_renew(
560		sovereign_account: T::AccountId,
561		core: CoreIndex,
562		task: TaskId,
563		workload_end_hint: Option<Timeslice>,
564	) -> DispatchResult {
565		let sale = SaleInfo::<T>::get().ok_or(Error::<T>::NoSales)?;
566		let mut core = core;
567
568		// Check if the core is expiring in the next bulk period; if so, we will renew it now.
569		//
570		// In case we renew it now, we don't need to check the workload end since we know it is
571		// eligible for renewal.
572		if PotentialRenewals::<T>::get(PotentialRenewalId { core, when: sale.region_begin })
573			.is_some()
574		{
575			core = Self::do_renew(sovereign_account.clone(), core)?;
576		} else if let Some(workload_end) = workload_end_hint {
577			ensure!(
578				PotentialRenewals::<T>::get(PotentialRenewalId { core, when: workload_end })
579					.is_some(),
580				Error::<T>::NotAllowed
581			);
582		} else {
583			return Err(Error::<T>::NotAllowed.into());
584		}
585
586		// We are sorting auto renewals by `CoreIndex`.
587		AutoRenewals::<T>::try_mutate(|renewals| {
588			let pos = renewals
589				.binary_search_by(|r: &AutoRenewalRecord| r.core.cmp(&core))
590				.unwrap_or_else(|e| e);
591			renewals.try_insert(
592				pos,
593				AutoRenewalRecord {
594					core,
595					task,
596					next_renewal: workload_end_hint.unwrap_or(sale.region_end),
597				},
598			)
599		})
600		.map_err(|_| Error::<T>::TooManyAutoRenewals)?;
601
602		Self::deposit_event(Event::AutoRenewalEnabled { core, task });
603		Ok(())
604	}
605
606	pub(crate) fn do_disable_auto_renew(core: CoreIndex, task: TaskId) -> DispatchResult {
607		AutoRenewals::<T>::try_mutate(|renewals| -> DispatchResult {
608			let pos = renewals
609				.binary_search_by(|r: &AutoRenewalRecord| r.core.cmp(&core))
610				.map_err(|_| Error::<T>::AutoRenewalNotEnabled)?;
611
612			let renewal_record = renewals.get(pos).ok_or(Error::<T>::AutoRenewalNotEnabled)?;
613
614			ensure!(
615				renewal_record.core == core && renewal_record.task == task,
616				Error::<T>::NoPermission
617			);
618			renewals.remove(pos);
619			Ok(())
620		})?;
621
622		Self::deposit_event(Event::AutoRenewalDisabled { core, task });
623		Ok(())
624	}
625
626	pub(crate) fn do_remove_potential_renewal(core: CoreIndex, when: Timeslice) -> DispatchResult {
627		let renewal_id = PotentialRenewalId { core, when };
628
629		PotentialRenewals::<T>::take(renewal_id).ok_or(Error::<T>::UnknownRenewal)?;
630
631		Self::deposit_event(Event::PotentialRenewalRemoved { core, timeslice: when });
632
633		Ok(())
634	}
635
636	pub(crate) fn ensure_cores_for_sale(
637		status: &StatusRecord,
638		sale: &SaleInfoRecordOf<T>,
639	) -> Result<(), DispatchError> {
640		ensure!(sale.first_core < status.core_count, Error::<T>::Unavailable);
641		ensure!(sale.cores_sold < sale.cores_offered, Error::<T>::SoldOut);
642
643		Ok(())
644	}
645
646	/// If there is an ongoing sale returns the current price of a core.
647	pub fn current_price() -> Result<BalanceOf<T>, DispatchError> {
648		let status = Status::<T>::get().ok_or(Error::<T>::Uninitialized)?;
649		let sale = SaleInfo::<T>::get().ok_or(Error::<T>::NoSales)?;
650
651		Self::ensure_cores_for_sale(&status, &sale)?;
652
653		let now = RCBlockNumberProviderOf::<T::Coretime>::current_block_number();
654		Ok(Self::sale_price(&sale, now))
655	}
656}