use crate::{
Config, CoreAssignment, CoreIndex, CoreMask, CoretimeInterface, RCBlockNumberOf, TaskId,
CORE_MASK_BITS,
};
use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
use pezframe_support::traits::fungible::Inspect;
use pezframe_system::Config as SConfig;
use pezsp_arithmetic::Perbill;
use pezsp_core::{ConstU32, RuntimeDebug};
use pezsp_runtime::BoundedVec;
use scale_info::TypeInfo;
pub type BalanceOf<T> = <<T as Config>::Currency as Inspect<<T as SConfig>::AccountId>>::Balance;
pub type RelayBalanceOf<T> = <<T as Config>::Coretime as CoretimeInterface>::Balance;
pub type RelayBlockNumberOf<T> = RCBlockNumberOf<<T as Config>::Coretime>;
pub type RelayAccountIdOf<T> = <<T as Config>::Coretime as CoretimeInterface>::AccountId;
pub type Timeslice = u32;
pub type CoreMaskBitCount = u32;
pub type SignedCoreMaskBitCount = i32;
#[derive(
Encode,
Decode,
DecodeWithMemTracking,
Copy,
Clone,
PartialEq,
Eq,
RuntimeDebug,
TypeInfo,
MaxEncodedLen,
)]
pub enum Finality {
Provisional,
Final,
}
#[derive(
Encode,
Decode,
DecodeWithMemTracking,
Copy,
Clone,
PartialEq,
Eq,
RuntimeDebug,
TypeInfo,
MaxEncodedLen,
)]
pub struct RegionId {
pub begin: Timeslice,
pub core: CoreIndex,
pub mask: CoreMask,
}
impl From<u128> for RegionId {
fn from(x: u128) -> Self {
Self { begin: (x >> 96) as u32, core: (x >> 80) as u16, mask: x.into() }
}
}
impl From<RegionId> for u128 {
fn from(x: RegionId) -> Self {
((x.begin as u128) << 96) | ((x.core as u128) << 80) | u128::from(x.mask)
}
}
#[test]
fn region_id_converts_u128() {
let r = RegionId { begin: 0x12345678u32, core: 0xabcdu16, mask: 0xdeadbeefcafef00d0123.into() };
let u = 0x12345678_abcd_deadbeefcafef00d0123u128;
assert_eq!(RegionId::from(u), r);
assert_eq!(u128::from(r), u);
}
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct RegionRecord<AccountId, Balance> {
pub end: Timeslice,
pub owner: Option<AccountId>,
pub paid: Option<Balance>,
}
pub type RegionRecordOf<T> = RegionRecord<<T as SConfig>::AccountId, BalanceOf<T>>;
#[derive(
Encode,
Decode,
DecodeWithMemTracking,
Clone,
PartialEq,
Eq,
RuntimeDebug,
TypeInfo,
MaxEncodedLen,
)]
pub struct ScheduleItem {
pub mask: CoreMask,
pub assignment: CoreAssignment,
}
pub type Schedule = BoundedVec<ScheduleItem, ConstU32<{ CORE_MASK_BITS as u32 }>>;
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct ContributionRecord<AccountId> {
pub length: Timeslice,
pub payee: AccountId,
}
pub type ContributionRecordOf<T> = ContributionRecord<<T as SConfig>::AccountId>;
#[derive(Encode, Decode, Clone, Default, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct InstaPoolHistoryRecord<Balance> {
pub private_contributions: CoreMaskBitCount,
pub system_contributions: CoreMaskBitCount,
pub maybe_payout: Option<Balance>,
}
pub type InstaPoolHistoryRecordOf<T> = InstaPoolHistoryRecord<BalanceOf<T>>;
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub enum CompletionStatus {
Partial(CoreMask),
Complete(Schedule),
}
impl CompletionStatus {
pub fn complete(&self) -> Option<&Schedule> {
match self {
Self::Complete(s) => Some(s),
Self::Partial(_) => None,
}
}
pub fn drain_complete(self) -> Option<Schedule> {
match self {
Self::Complete(s) => Some(s),
Self::Partial(_) => None,
}
}
}
#[derive(Encode, Decode, Copy, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct PotentialRenewalId {
pub core: CoreIndex,
pub when: Timeslice,
}
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct PotentialRenewalRecord<Balance> {
pub price: Balance,
pub completion: CompletionStatus,
}
pub type PotentialRenewalRecordOf<T> = PotentialRenewalRecord<BalanceOf<T>>;
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct StatusRecord {
pub core_count: CoreIndex,
pub private_pool_size: CoreMaskBitCount,
pub system_pool_size: CoreMaskBitCount,
pub last_committed_timeslice: Timeslice,
pub last_timeslice: Timeslice,
}
#[derive(
Encode, Decode, Clone, Copy, Default, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen,
)]
pub struct PoolIoRecord {
pub private: SignedCoreMaskBitCount,
pub system: SignedCoreMaskBitCount,
}
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct SaleInfoRecord<Balance, RelayBlockNumber> {
pub sale_start: RelayBlockNumber,
pub leadin_length: RelayBlockNumber,
pub end_price: Balance,
pub region_begin: Timeslice,
pub region_end: Timeslice,
pub ideal_cores_sold: CoreIndex,
pub cores_offered: CoreIndex,
pub first_core: CoreIndex,
pub sellout_price: Option<Balance>,
pub cores_sold: CoreIndex,
}
pub type SaleInfoRecordOf<T> = SaleInfoRecord<BalanceOf<T>, RelayBlockNumberOf<T>>;
pub type ReservationsRecord<Max> = BoundedVec<Schedule, Max>;
pub type ReservationsRecordOf<T> = ReservationsRecord<<T as Config>::MaxReservedCores>;
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct LeaseRecordItem {
pub until: Timeslice,
pub task: TaskId,
}
pub type LeasesRecord<Max> = BoundedVec<LeaseRecordItem, Max>;
pub type LeasesRecordOf<T> = LeasesRecord<<T as Config>::MaxLeasedCores>;
#[derive(
Encode,
Decode,
DecodeWithMemTracking,
Clone,
PartialEq,
Eq,
RuntimeDebug,
TypeInfo,
MaxEncodedLen,
)]
pub struct OnDemandRevenueRecord<RelayBlockNumber, RelayBalance> {
pub until: RelayBlockNumber,
pub amount: RelayBalance,
}
pub type OnDemandRevenueRecordOf<T> =
OnDemandRevenueRecord<RelayBlockNumberOf<T>, RelayBalanceOf<T>>;
#[derive(
Encode,
Decode,
DecodeWithMemTracking,
Clone,
PartialEq,
Eq,
RuntimeDebug,
TypeInfo,
MaxEncodedLen,
)]
pub struct ConfigRecord<RelayBlockNumber> {
pub advance_notice: RelayBlockNumber,
pub interlude_length: RelayBlockNumber,
pub leadin_length: RelayBlockNumber,
pub region_length: Timeslice,
pub ideal_bulk_proportion: Perbill,
pub limit_cores_offered: Option<CoreIndex>,
pub renewal_bump: Perbill,
pub contribution_timeout: Timeslice,
}
pub type ConfigRecordOf<T> = ConfigRecord<RelayBlockNumberOf<T>>;
impl<RelayBlockNumber> ConfigRecord<RelayBlockNumber>
where
RelayBlockNumber: pezsp_arithmetic::traits::Zero,
{
pub(crate) fn validate(&self) -> Result<(), ()> {
if self.leadin_length.is_zero() {
return Err(());
}
Ok(())
}
}
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct AutoRenewalRecord {
pub core: CoreIndex,
pub task: TaskId,
pub next_renewal: Timeslice,
}