referrerpolicy=no-referrer-when-downgrade

pallet_broker/
utility_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 super::*;
19use frame_support::{
20	pallet_prelude::*,
21	traits::{
22		fungible::Balanced,
23		tokens::{Fortitude::Polite, Precision::Exact, Preservation::Expendable},
24		OnUnbalanced,
25	},
26};
27use sp_arithmetic::{
28	traits::{SaturatedConversion, Saturating},
29	FixedPointNumber, FixedU64,
30};
31use sp_runtime::traits::{AccountIdConversion, BlockNumberProvider};
32
33impl<T: Config> Pallet<T> {
34	pub fn current_timeslice() -> Timeslice {
35		let latest = RCBlockNumberProviderOf::<T::Coretime>::current_block_number();
36		let timeslice_period = T::TimeslicePeriod::get();
37		(latest / timeslice_period).saturated_into()
38	}
39
40	pub fn latest_timeslice_ready_to_commit(config: &ConfigRecordOf<T>) -> Timeslice {
41		let latest = RCBlockNumberProviderOf::<T::Coretime>::current_block_number();
42		let advanced = latest.saturating_add(config.advance_notice);
43		let timeslice_period = T::TimeslicePeriod::get();
44		(advanced / timeslice_period).saturated_into()
45	}
46
47	pub fn next_timeslice_to_commit(
48		config: &ConfigRecordOf<T>,
49		status: &StatusRecord,
50	) -> Option<Timeslice> {
51		if status.last_committed_timeslice < Self::latest_timeslice_ready_to_commit(config) {
52			Some(status.last_committed_timeslice + 1)
53		} else {
54			None
55		}
56	}
57
58	pub fn account_id() -> T::AccountId {
59		T::PalletId::get().into_account_truncating()
60	}
61
62	pub fn sale_price(sale: &SaleInfoRecordOf<T>, now: RelayBlockNumberOf<T>) -> BalanceOf<T> {
63		let num = now.saturating_sub(sale.sale_start).min(sale.leadin_length).saturated_into();
64		let through = FixedU64::from_rational(num, sale.leadin_length.saturated_into());
65		T::PriceAdapter::leadin_factor_at(through).saturating_mul_int(sale.end_price)
66	}
67
68	pub(crate) fn charge(who: &T::AccountId, amount: BalanceOf<T>) -> DispatchResult {
69		let credit = T::Currency::withdraw(&who, amount, Exact, Expendable, Polite)?;
70		T::OnRevenue::on_unbalanced(credit);
71		Ok(())
72	}
73
74	/// Buy a core at the specified price (price is to be determined by the caller).
75	///
76	/// Note: It is the responsibility of the caller to write back the changed `SaleInfoRecordOf` to
77	/// storage.
78	pub(crate) fn purchase_core(
79		who: &T::AccountId,
80		price: BalanceOf<T>,
81		sale: &mut SaleInfoRecordOf<T>,
82	) -> Result<CoreIndex, DispatchError> {
83		Self::charge(who, price)?;
84		log::debug!("Purchased core at: {:?}", price);
85		let core = sale.first_core.saturating_add(sale.cores_sold);
86		sale.cores_sold.saturating_inc();
87		if sale.cores_sold <= sale.ideal_cores_sold || sale.sellout_price.is_none() {
88			sale.sellout_price = Some(price);
89		}
90		Ok(core)
91	}
92
93	pub fn issue(
94		core: CoreIndex,
95		begin: Timeslice,
96		mask: CoreMask,
97		end: Timeslice,
98		owner: Option<T::AccountId>,
99		paid: Option<BalanceOf<T>>,
100	) -> RegionId {
101		let id = RegionId { begin, core, mask };
102		let record = RegionRecord { end, owner, paid };
103		Regions::<T>::insert(&id, &record);
104		id
105	}
106
107	pub(crate) fn utilize(
108		mut region_id: RegionId,
109		maybe_check_owner: Option<T::AccountId>,
110		finality: Finality,
111	) -> Result<Option<(RegionId, RegionRecordOf<T>)>, Error<T>> {
112		let status = Status::<T>::get().ok_or(Error::<T>::Uninitialized)?;
113		let region = Regions::<T>::get(&region_id).ok_or(Error::<T>::UnknownRegion)?;
114
115		if let Some(check_owner) = maybe_check_owner {
116			ensure!(Some(check_owner) == region.owner, Error::<T>::NotOwner);
117		}
118
119		Regions::<T>::remove(&region_id);
120
121		let last_committed_timeslice = status.last_committed_timeslice;
122		if region_id.begin <= last_committed_timeslice {
123			let duration = region.end.saturating_sub(region_id.begin);
124			region_id.begin = last_committed_timeslice + 1;
125			if region_id.begin >= region.end {
126				Self::deposit_event(Event::RegionDropped { region_id, duration });
127				return Ok(None)
128			}
129		} else {
130			Workplan::<T>::mutate_extant((region_id.begin, region_id.core), |p| {
131				p.retain(|i| (i.mask & region_id.mask).is_void())
132			});
133		}
134		if finality == Finality::Provisional {
135			Regions::<T>::insert(&region_id, &region);
136		}
137
138		Ok(Some((region_id, region)))
139	}
140
141	// Remove a region from on-demand pool contributions. Useful in cases where it was pooled
142	// provisionally and it is being redispatched (partition/interlace/assign).
143	//
144	// Takes both the region_id and (a reference to) the region as arguments to avoid another DB
145	// read. No-op for regions which have not been pooled.
146	pub(crate) fn force_unpool_region(
147		region_id: RegionId,
148		region: &RegionRecordOf<T>,
149		status: &StatusRecord,
150	) {
151		// We don't care if this fails or not, just that it is removed if present. This is to
152		// account for the case where a region is pooled provisionally and redispatched.
153		if InstaPoolContribution::<T>::take(region_id).is_some() {
154			// `InstaPoolHistory` is calculated from the `InstaPoolIo` one timeslice in advance.
155			// Therefore we need to schedule this for the timeslice after that.
156			let end_timeslice = status.last_committed_timeslice + 1;
157
158			// InstaPoolIo has already accounted for regions that have already ended. Regions ending
159			// this timeslice would have region.end == unpooled_at below.
160			if region.end <= end_timeslice {
161				return
162			}
163
164			// Account for the change in `InstaPoolIo` either from the start of the region or from
165			// the current timeslice if we are already part-way through the region.
166			let size = region_id.mask.count_ones() as i32;
167			let unpooled_at = end_timeslice.max(region_id.begin);
168			InstaPoolIo::<T>::mutate(unpooled_at, |a| a.private.saturating_reduce(size));
169			InstaPoolIo::<T>::mutate(region.end, |a| a.private.saturating_accrue(size));
170
171			Self::deposit_event(Event::<T>::RegionUnpooled { region_id, when: unpooled_at });
172		};
173	}
174}