Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pallet_broker: Let start_sales calculate and request the correct core count #4221

Merged
merged 4 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion substrate/frame/broker/src/dispatchable_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,16 @@ impl<T: Config> Pallet<T> {
Ok(())
}

pub(crate) fn do_start_sales(price: BalanceOf<T>, core_count: CoreIndex) -> DispatchResult {
pub(crate) fn do_start_sales(price: BalanceOf<T>, extra_cores: CoreIndex) -> DispatchResult {
let config = Configuration::<T>::get().ok_or(Error::<T>::Uninitialized)?;

// Determine the core count
let core_count = Leases::<T>::decode_len().unwrap_or(0) as CoreIndex +
Reservations::<T>::decode_len().unwrap_or(0) as CoreIndex +
extra_cores;

Self::do_request_core_count(core_count)?;

let commit_timeslice = Self::latest_timeslice_ready_to_commit(&config);
let status = StatusRecord {
core_count,
Expand Down
21 changes: 7 additions & 14 deletions substrate/frame/broker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,27 +559,20 @@ pub mod pallet {
///
/// - `origin`: Must be Root or pass `AdminOrigin`.
/// - `initial_price`: The price of Bulk Coretime in the first sale.
/// - `total_core_count`: This is the total number of cores the relay chain should have
/// after the sale concludes.
/// - `extra_cores`: Number of extra cores that should be requested on top of the cores
/// required for `Reservations` and `Leases`.
///
/// NOTE: This function does not actually request that new core count from the relay chain.
/// You need to make sure to call `request_core_count` afterwards to bring the relay chain
/// in sync.
///
/// When to call the function depends on the new core count. If it is larger than what it
/// was before, you can call it immediately or even before `start_sales` as non allocated
/// cores will just be `Idle`. If you are actually reducing the number of cores, you should
/// call `request_core_count`, right before the next sale, to avoid shutting down tasks too
/// early.
/// This will call [`Self::request_core_count`] internally to set the correct core count on
/// the relay chain.
#[pallet::call_index(4)]
#[pallet::weight(T::WeightInfo::start_sales((*total_core_count).into()))]
#[pallet::weight(T::WeightInfo::start_sales(T::MaxLeasedCores::get() + T::MaxReservedCores::get()))]
pub fn start_sales(
origin: OriginFor<T>,
initial_price: BalanceOf<T>,
total_core_count: CoreIndex,
extra_cores: CoreIndex,
) -> DispatchResultWithPostInfo {
T::AdminOrigin::ensure_origin_or_root(origin)?;
Self::do_start_sales(initial_price, total_core_count)?;
Self::do_start_sales(initial_price, extra_cores)?;
Ok(Pays::No.into())
}

Expand Down
20 changes: 20 additions & 0 deletions substrate/frame/broker/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1408,3 +1408,23 @@ fn renewal_works_leases_ended_before_start_sales() {
);
});
}

#[test]
fn start_sales_sets_correct_core_count() {
TestExt::new().endow(1, 1000).execute_with(|| {
advance_to(1);

Broker::do_set_lease(1, 100).unwrap();
Broker::do_set_lease(2, 100).unwrap();
Broker::do_set_lease(3, 100).unwrap();
Broker::do_reserve(Schedule::truncate_from(vec![ScheduleItem {
assignment: Pool,
mask: CoreMask::complete(),
}]))
.unwrap();

Broker::do_start_sales(5, 5).unwrap();

System::assert_has_event(Event::<Test>::CoreCountRequested { core_count: 9 }.into());
})
}
Loading