Skip to content

Commit

Permalink
init test
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo committed May 11, 2024
1 parent 82859ad commit e2a4dd5
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 57 deletions.
56 changes: 56 additions & 0 deletions substrate/frame/broker/src/dispatchable_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,62 @@ impl<T: Config> Pallet<T> {
Ok(())
}

pub(crate) fn do_enable_auto_renew(
who: T::AccountId,
core: CoreIndex,
workload_end_hint: Option<Timeslice>,
) -> DispatchResult {
let sale = SaleInfo::<T>::get().ok_or(Error::<T>::NoSales)?;

let record = if let Some(workload_end) = workload_end_hint {
AllowedRenewals::<T>::get(AllowedRenewalId { core, when: workload_end })
.ok_or(Error::<T>::NotAllowed)?
} else {
// If the core hasn't been renewed yet we will renew it now.
if let Some(record) =
AllowedRenewals::<T>::get(AllowedRenewalId { core, when: sale.region_begin })
{
Self::do_renew(who.clone(), core)?;
record
} else {
// If we couldn't find the renewal record for the current bulk period we should
// be able to find it for the upcoming bulk period.
//
// If not the core is not eligable for renewal.
AllowedRenewals::<T>::get(AllowedRenewalId { core, when: sale.region_end })
.ok_or(Error::<T>::NotAllowed)?
}
};

let workload =
record.completion.drain_complete().ok_or(Error::<T>::IncompleteAssignment)?;

// Given that only non-interlaced cores can be renewed, there should be only one
// assignment in the core's workload.
ensure!(workload.len() == 1, Error::<T>::IncompleteAssignment);
let Some(schedule_item) = workload.get(0) else {
return Err(Error::<T>::NotAllowed.into())
};

let CoreAssignment::Task(task_id) = schedule_item.assignment else {
return Err(Error::<T>::NonTaskAutoRenewal.into())
};

// Only the sovereign account of the task can enable auto-renewal.
ensure!(who == T::SovereignAccountOf::convert(task_id), Error::<T>::NoPermission);

AutoRenewals::<T>::try_mutate(|renewals| {
let pos = renewals
.binary_search_by(|r: &(CoreIndex, TaskId)| r.0.cmp(&core))
.unwrap_or_else(|e| e);
renewals.try_insert(pos, (core, task_id))
})
.map_err(|_| Error::<T>::TooManyAutoRenewals)?;

// TODO: event
Ok(())
}

pub(crate) fn ensure_cores_for_sale(
status: &StatusRecord,
sale: &SaleInfoRecordOf<T>,
Expand Down
50 changes: 1 addition & 49 deletions substrate/frame/broker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ pub mod pallet {

/// Type used for getting the associated account of a task. This account is controlled by
/// the task itself.
//
// TODO: maybe rename this?
type SovereignAccountOf: Convert<TaskId, Self::AccountId>;

/// Identifier from which the internal Pot is generated.
Expand Down Expand Up @@ -838,53 +836,7 @@ pub mod pallet {
workload_end_hint: Option<Timeslice>,
) -> DispatchResult {
let who = ensure_signed(origin)?;

let sale = SaleInfo::<T>::get().ok_or(Error::<T>::NoSales)?;

let record = if let Some(workload_end) = workload_end_hint {
AllowedRenewals::<T>::get(AllowedRenewalId { core, when: workload_end })
.ok_or(Error::<T>::NotAllowed)?
} else {
// If the core hasn't been renewed yet we will renew it now.
if let Some(record) =
AllowedRenewals::<T>::get(AllowedRenewalId { core, when: sale.region_begin })
{
Self::do_renew(who.clone(), core)?;
record
} else {
// If we couldn't find the renewal record for the current bulk period we should
// be able to find it for the upcoming bulk period.
//
// If not the core is not eligable for renewal.
AllowedRenewals::<T>::get(AllowedRenewalId { core, when: sale.region_end })
.ok_or(Error::<T>::NotAllowed)?
}
};

let workload =
record.completion.drain_complete().ok_or(Error::<T>::IncompleteAssignment)?;

// Given that only non-interlaced cores can be renewed, there should be only one
// assignment in the core's workload.
ensure!(workload.len() == 1, Error::<T>::IncompleteAssignment);
let Some(schedule_item) = workload.get(0) else {
return Err(Error::<T>::NotAllowed.into())
};

let CoreAssignment::Task(task_id) = schedule_item.assignment else {
return Err(Error::<T>::NonTaskAutoRenewal.into())
};

// Only the sovereign account of the task can enable auto-renewal.
ensure!(who == T::SovereignAccountOf::convert(task_id), Error::<T>::NoPermission);

AutoRenewals::<T>::try_mutate(|renewals| {
let pos = renewals
.binary_search_by(|r: &(CoreIndex, TaskId)| r.0.cmp(&core))
.unwrap_or_else(|e| e);
renewals.try_insert(pos, (core, task_id))
})
.map_err(|_| Error::<T>::TooManyAutoRenewals)?;
Self::do_enable_auto_renew(who, core, workload_end_hint)?;

// The caller must pay for the transaction otherwise spamming would be possible by
// turning auto-renewal on and off.
Expand Down
7 changes: 4 additions & 3 deletions substrate/frame/broker/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@ ord_parameter_types! {
type EnsureOneOrRoot = EitherOfDiverse<EnsureRoot<u64>, EnsureSignedBy<One, u64>>;

pub struct TaskToAccountId;
// TODO
// Dummy implementation which converts `TaskId` to `AccountId`.
impl Convert<TaskId, u64> for TaskToAccountId {
fn convert(_task: TaskId) -> u64 {
0
fn convert(task: TaskId) -> u64 {
task.into()
}
}

Expand All @@ -209,6 +209,7 @@ impl crate::Config for Test {
type AdminOrigin = EnsureOneOrRoot;
type PriceAdapter = Linear;
type SovereignAccountOf = TaskToAccountId;
type MaxAutoRenewals = ConstU32<5>;
}

pub fn advance_to(b: u64) {
Expand Down
18 changes: 13 additions & 5 deletions substrate/frame/broker/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,19 @@ fn renewal_works_leases_ended_before_start_sales() {
});
}

#[test]
fn enable_auto_renew_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, Final));

assert_ok!(Broker::do_enable_auto_renew(1001, region.core, None));
assert_eq!(AutoRenewals::<Test>::get().to_vec(), vec![(0, 1001)]);
});
}

#[test]
fn start_sales_sets_correct_core_count() {
TestExt::new().endow(1, 1000).execute_with(|| {
Expand All @@ -1428,8 +1441,3 @@ fn start_sales_sets_correct_core_count() {
System::assert_has_event(Event::<Test>::CoreCountRequested { core_count: 9 }.into());
})
}

#[test]
fn enable_auto_renew_works() {
TestExt::new().endow(1, 1000).execute_with(|| {})
}

0 comments on commit e2a4dd5

Please sign in to comment.