Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo committed May 12, 2024
1 parent e2a4dd5 commit 9c1c7e3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
2 changes: 1 addition & 1 deletion substrate/frame/broker/src/dispatchable_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ impl<T: Config> Pallet<T> {
})
.map_err(|_| Error::<T>::TooManyAutoRenewals)?;

// TODO: event
Self::deposit_event(Event::AutoRenewalEnabled { core, task: task_id });
Ok(())
}

Expand Down
16 changes: 16 additions & 0 deletions substrate/frame/broker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,22 @@ pub mod pallet {
/// The core whose workload is no longer available to be renewed for `when`.
core: CoreIndex,
},
AutoRenewalEnabled {
/// The core for which the renewal was enabled.
core: CoreIndex,
/// The task for which the renewal was enabled.
task: TaskId,
},
/// Failed to auto-renew a core, likely due to the payer account not being sufficiently
/// funded.
AutoRenewalFailed {
/// The core for which the renewal failed.
core: CoreIndex,
/// The task for which the renewal failed.
task: TaskId,
/// The account which was supposed to pay for renewal.
payer: T::AccountId,
},
}

#[pallet::error]
Expand Down
31 changes: 28 additions & 3 deletions substrate/frame/broker/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1414,11 +1414,36 @@ 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));
let region_id = Broker::do_purchase(1, u64::max_value()).unwrap();
let record = Regions::<Test>::get(region_id).unwrap();

// Cannot enable auto renewal with provisional finality:
assert_ok!(Broker::do_assign(region_id, Some(1), 1001, Provisional));
assert_noop!(
Broker::do_enable_auto_renew(1001, region_id.core, None),
Error::<Test>::NotAllowed
);

// Eligible for renewal after final assignment:
assert_ok!(Broker::do_assign(region_id, Some(1), 1001, Final));
assert!(AllowedRenewals::<Test>::get(AllowedRenewalId {
core: region_id.core,
when: record.end
})
.is_some());

// Only the task's sovereign account can enable auto renewal.
assert_noop!(
Broker::do_enable_auto_renew(1, region_id.core, None),
Error::<Test>::NoPermission
);

assert_ok!(Broker::do_enable_auto_renew(1001, region.core, None));
// Works when calling with the sovereign account:
assert_ok!(Broker::do_enable_auto_renew(1001, region_id.core, None));
assert_eq!(AutoRenewals::<Test>::get().to_vec(), vec![(0, 1001)]);
System::assert_has_event(
Event::<Test>::AutoRenewalEnabled { core: region_id.core, task: 1001 }.into(),
);
});
}

Expand Down
8 changes: 4 additions & 4 deletions substrate/frame/broker/src/tick_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,10 @@ impl<T: Config> Pallet<T> {
/// Renews all the cores which have auto-renewal enabled.
pub(crate) fn renew_cores() {
let renewals = AutoRenewals::<T>::get();
renewals.iter().for_each(|(core, task_id)| {
let payer = T::SovereignAccountOf::convert(*task_id);
if let Err(_) = Self::do_renew(payer, *core) {
// TODO: emit event
renewals.into_iter().for_each(|(core, task)| {
let payer = T::SovereignAccountOf::convert(task);
if let Err(_) = Self::do_renew(payer.clone(), core) {
Self::deposit_event(Event::<T>::AutoRenewalFailed { core, task, payer });
}
});
}
Expand Down

0 comments on commit 9c1c7e3

Please sign in to comment.