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

Coretime auto-renew #4424

Merged
merged 53 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
afb5e75
start
Szegoo May 4, 2024
2716e0f
setup
Szegoo May 8, 2024
e443c9e
outlining the main logic
Szegoo May 9, 2024
2a8a68b
bounded vec
Szegoo May 10, 2024
8df2f81
resolvinig some todos
Szegoo May 11, 2024
82859ad
auto-renew for legacy leases
Szegoo May 11, 2024
e2a4dd5
init test
Szegoo May 11, 2024
9c1c7e3
more tests
Szegoo May 12, 2024
c7ccf5a
enable_auto_renew_renews
Szegoo May 12, 2024
c3eaa21
different approach
Szegoo May 12, 2024
edffbfc
docs
Szegoo May 13, 2024
68f244c
enable_auto_renewal_with_end_hint_works
Szegoo May 13, 2024
893fa9d
auto_renewal_works
Szegoo May 15, 2024
0b50a58
progress
Szegoo May 15, 2024
9dc9b6a
store account instead of task
Szegoo May 15, 2024
9baa77a
fix builds
Szegoo May 15, 2024
ee7e3c4
disable auto-renewal
Szegoo May 17, 2024
e0f1c71
disable auto renew test
Szegoo May 17, 2024
1a46abc
docs
Szegoo May 17, 2024
2f17544
account for changing core indices
Szegoo May 23, 2024
63b6f66
fix
Szegoo May 23, 2024
aaba50d
bench enable_auto_renew
Szegoo May 23, 2024
7d682e6
bench disable & auto_renew
Szegoo May 23, 2024
af8f109
weights
Szegoo May 23, 2024
f22c451
fix
Szegoo May 23, 2024
a6c0676
dummy weights
Szegoo May 23, 2024
a4f3b6c
don't auto renew when not needed
Szegoo May 24, 2024
4f18949
fix auto renewal
Szegoo May 24, 2024
5d29cd3
more checks
Szegoo May 24, 2024
0d9e91d
test fixes/improvements
Szegoo May 27, 2024
dad2b91
merge
Szegoo May 30, 2024
88c9ce1
merge fix
Szegoo May 30, 2024
17e0ee5
cleanup | benchmarks: TODO
Szegoo May 30, 2024
8be2d02
Update substrate/frame/broker/src/dispatchable_impls.rs
Szegoo May 31, 2024
c8fa5f8
Update substrate/frame/broker/src/types.rs
Szegoo May 31, 2024
7d887ab
Update substrate/frame/broker/src/lib.rs
Szegoo May 31, 2024
78a203f
resolve comments
Szegoo May 31, 2024
5f02e32
cleanup
Szegoo May 31, 2024
b0aa972
rename
Szegoo May 31, 2024
de89f30
comment
Szegoo May 31, 2024
7259f41
fix benchmarks
Szegoo May 31, 2024
9a23b2f
cleanup
Szegoo Jun 3, 2024
0c4e651
simplify logic
Szegoo Jun 3, 2024
ef9c4fb
fix benchmarks
Szegoo Jul 1, 2024
4d35d2b
Merge branch 'master' into auto-renew
Szegoo Jul 1, 2024
15d9e1b
merge fixes
Szegoo Jul 1, 2024
eff4ee7
benchmark fix
Szegoo Jul 1, 2024
61f466d
resolve conflicts
Szegoo Jul 30, 2024
d2041f1
prdoc
Szegoo Jul 30, 2024
7155233
improve docs
Szegoo Jul 30, 2024
9a90fbd
improve comments
Szegoo Aug 1, 2024
0cf0dad
Merge branch 'master' into auto-renew
Szegoo Aug 1, 2024
c1fc125
Merge branch 'master' into auto-renew
Szegoo Aug 3, 2024
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pallet-xcm = { path = "../../../../../polkadot/xcm/pallet-xcm", default-features
pallet-xcm-benchmarks = { path = "../../../../../polkadot/xcm/pallet-xcm-benchmarks", default-features = false, optional = true }
polkadot-parachain-primitives = { path = "../../../../../polkadot/parachain", default-features = false }
polkadot-runtime-common = { path = "../../../../../polkadot/runtime/common", default-features = false }
polkadot-runtime-parachains = { path = "../../../../../polkadot/runtime/parachains", default-features = false }
rococo-runtime-constants = { path = "../../../../../polkadot/runtime/rococo/constants", default-features = false }
xcm = { package = "staging-xcm", path = "../../../../../polkadot/xcm", default-features = false }
xcm-builder = { package = "staging-xcm-builder", path = "../../../../../polkadot/xcm/xcm-builder", default-features = false }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.

use crate::*;
use crate::{xcm_config::LocationToAccountId, *};
use codec::{Decode, Encode};
use cumulus_pallet_parachain_system::RelaychainDataProvider;
use cumulus_primitives_core::relay_chain;
Expand All @@ -25,9 +25,15 @@ use frame_support::{
OnUnbalanced,
},
};
use pallet_broker::{CoreAssignment, CoreIndex, CoretimeInterface, PartsOf57600, RCBlockNumberOf};
use pallet_broker::{
CoreAssignment, CoreIndex, CoretimeInterface, PartsOf57600, RCBlockNumberOf,
TaskAccountInterface, TaskId,
};
use parachains_common::{AccountId, Balance, BlockNumber};
use polkadot_runtime_parachains::{ensure_parachain, origin, Origin as ParaOrigin};
use sp_runtime::traits::BadOrigin;
use xcm::latest::prelude::*;
use xcm_executor::traits::ConvertLocation;

pub struct CreditToCollatorPot;
impl OnUnbalanced<Credit<AccountId, Balances>> for CreditToCollatorPot {
Expand Down Expand Up @@ -217,6 +223,28 @@ impl CoretimeInterface for CoretimeAllocator {
}
}

impl origin::Config for Runtime {}

pub struct TaskSovereignAccount;
impl TaskAccountInterface for TaskSovereignAccount {
type AccountId = AccountId;
type OuterOrigin = RuntimeOrigin;
type TaskOrigin = ParaOrigin;

fn ensure_task_sovereign_account(o: RuntimeOrigin) -> Result<TaskId, BadOrigin> {
match ensure_parachain(o) {
Ok(para_id) => Ok(para_id.into()),
Err(e) => Err(e),
}
}

fn sovereign_account(id: TaskId) -> Option<AccountId> {
// Currently all tasks are parachains.
let location = Location::new(1, [Parachain(id)]);
LocationToAccountId::convert_location(&location)
}
}

impl pallet_broker::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
Expand All @@ -233,4 +261,6 @@ impl pallet_broker::Config for Runtime {
type PalletId = BrokerPalletId;
type AdminOrigin = EnsureRoot<AccountId>;
type PriceAdapter = pallet_broker::Linear;
type SovereignAccountOf = TaskSovereignAccount;
type MaxAutoRenewals = ConstU32<50>;
Szegoo marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ use parachains_common::{
AVERAGE_ON_INITIALIZE_RATIO, NORMAL_DISPATCH_RATIO,
};
use polkadot_runtime_common::{BlockHashCount, SlowAdjustingFeeUpdate};
use polkadot_runtime_parachains::origin;
use sp_api::impl_runtime_apis;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
#[cfg(any(feature = "std", test))]
Expand Down Expand Up @@ -462,6 +463,7 @@ construct_runtime!(
// Handy utilities.
Utility: pallet_utility = 40,
Multisig: pallet_multisig = 41,
ParachainsOrigin: origin = 42,

// The main stage.
Broker: pallet_broker = 50,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pallet-xcm = { path = "../../../../../polkadot/xcm/pallet-xcm", default-features
pallet-xcm-benchmarks = { path = "../../../../../polkadot/xcm/pallet-xcm-benchmarks", default-features = false, optional = true }
polkadot-parachain-primitives = { path = "../../../../../polkadot/parachain", default-features = false }
polkadot-runtime-common = { path = "../../../../../polkadot/runtime/common", default-features = false }
polkadot-runtime-parachains = { path = "../../../../../polkadot/runtime/parachains", default-features = false }
westend-runtime-constants = { path = "../../../../../polkadot/runtime/westend/constants", default-features = false }
xcm = { package = "staging-xcm", path = "../../../../../polkadot/xcm", default-features = false }
xcm-builder = { package = "staging-xcm-builder", path = "../../../../../polkadot/xcm/xcm-builder", default-features = false }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.

use crate::*;
use crate::{xcm_config::LocationToAccountId, *};
use codec::{Decode, Encode};
use cumulus_pallet_parachain_system::RelaychainDataProvider;
use cumulus_primitives_core::relay_chain;
Expand All @@ -25,9 +25,15 @@ use frame_support::{
OnUnbalanced,
},
};
use pallet_broker::{CoreAssignment, CoreIndex, CoretimeInterface, PartsOf57600, RCBlockNumberOf};
use pallet_broker::{
CoreAssignment, CoreIndex, CoretimeInterface, PartsOf57600, RCBlockNumberOf,
TaskAccountInterface, TaskId,
};
use parachains_common::{AccountId, Balance, BlockNumber};
use polkadot_runtime_parachains::{ensure_parachain, origin, Origin as ParaOrigin};
use sp_runtime::traits::BadOrigin;
use xcm::latest::prelude::*;
use xcm_executor::traits::ConvertLocation;

pub struct CreditToCollatorPot;
impl OnUnbalanced<Credit<AccountId, Balances>> for CreditToCollatorPot {
Expand Down Expand Up @@ -229,6 +235,28 @@ impl CoretimeInterface for CoretimeAllocator {
}
}

impl origin::Config for Runtime {}

pub struct TaskSovereignAccount;
impl TaskAccountInterface for TaskSovereignAccount {
type AccountId = AccountId;
type OuterOrigin = RuntimeOrigin;
type TaskOrigin = ParaOrigin;

fn ensure_task_sovereign_account(o: RuntimeOrigin) -> Result<TaskId, BadOrigin> {
match ensure_parachain(o) {
Ok(para_id) => Ok(para_id.into()),
Err(e) => Err(e),
}
}

fn sovereign_account(id: TaskId) -> Option<AccountId> {
// Currently all tasks are parachains.
let location = Location::new(1, [Parachain(id)]);
LocationToAccountId::convert_location(&location)
}
}

impl pallet_broker::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
Expand All @@ -246,4 +274,6 @@ impl pallet_broker::Config for Runtime {
type PalletId = BrokerPalletId;
type AdminOrigin = EnsureRoot<AccountId>;
type PriceAdapter = pallet_broker::Linear;
type SovereignAccountOf = TaskSovereignAccount;
type MaxAutoRenewals = ConstU32<10>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ use parachains_common::{
AVERAGE_ON_INITIALIZE_RATIO, NORMAL_DISPATCH_RATIO,
};
use polkadot_runtime_common::{BlockHashCount, SlowAdjustingFeeUpdate};
use polkadot_runtime_parachains::origin;
use sp_api::impl_runtime_apis;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
#[cfg(any(feature = "std", test))]
Expand Down Expand Up @@ -457,6 +458,7 @@ construct_runtime!(
// Handy utilities.
Utility: pallet_utility = 40,
Multisig: pallet_multisig = 41,
ParachainsOrigin: origin = 42,

// The main stage.
Broker: pallet_broker = 50,
Expand Down
22 changes: 20 additions & 2 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ use pallet_nis::WithMaximumOf;
use pallet_session::historical as pallet_session_historical;
// Can't use `FungibleAdapter` here until Treasury pallet migrates to fungibles
// <https://github.com/paritytech/polkadot-sdk/issues/226>
use pallet_broker::{TaskAccountInterface, TaskId};
#[allow(deprecated)]
pub use pallet_transaction_payment::{CurrencyAdapter, Multiplier, TargetedFeeAdjustment};
use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo};
Expand All @@ -91,8 +92,8 @@ use sp_runtime::{
curve::PiecewiseLinear,
generic, impl_opaque_keys,
traits::{
self, AccountIdConversion, BlakeTwo256, Block as BlockT, Bounded, ConvertInto, NumberFor,
OpaqueKeys, SaturatedConversion, StaticLookup,
self, AccountIdConversion, BadOrigin, BlakeTwo256, Block as BlockT, Bounded, ConvertInto,
NumberFor, OpaqueKeys, SaturatedConversion, StaticLookup,
},
transaction_validity::{TransactionPriority, TransactionSource, TransactionValidity},
ApplyExtrinsicResult, FixedPointNumber, FixedU128, Perbill, Percent, Permill, Perquintill,
Expand Down Expand Up @@ -2126,6 +2127,21 @@ impl CoretimeInterface for CoretimeProvider {
}
}

pub struct TaskSovereignAccount;
// Dummy implementation which converts `TaskId` to `AccountId`.
impl TaskAccountInterface for TaskSovereignAccount {
type AccountId = AccountId;
type OuterOrigin = RuntimeOrigin;
type TaskOrigin = frame_system::RawOrigin<AccountId>;

fn ensure_task_sovereign_account(_o: RuntimeOrigin) -> Result<TaskId, BadOrigin> {
Err(BadOrigin)
}

fn sovereign_account(_task: TaskId) -> Option<AccountId> {
None
}
}
impl pallet_broker::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
Expand All @@ -2139,6 +2155,8 @@ impl pallet_broker::Config for Runtime {
type PalletId = BrokerPalletId;
type AdminOrigin = EnsureRoot<AccountId>;
type PriceAdapter = pallet_broker::Linear;
type SovereignAccountOf = TaskSovereignAccount;
type MaxAutoRenewals = ConstU32<5>;
}

parameter_types! {
Expand Down
24 changes: 23 additions & 1 deletion substrate/frame/broker/src/coretime_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use frame_support::Parameter;
use scale_info::TypeInfo;
use sp_arithmetic::traits::AtLeast32BitUnsigned;
use sp_core::RuntimeDebug;
use sp_runtime::traits::BlockNumberProvider;
use sp_runtime::traits::{BadOrigin, BlockNumberProvider};
use sp_std::vec::Vec;

/// Index of a Polkadot Core.
Expand Down Expand Up @@ -146,3 +146,25 @@ impl CoretimeInterface for () {
#[cfg(feature = "runtime-benchmarks")]
fn ensure_notify_revenue_info(_when: RCBlockNumberOf<Self>, _revenue: Self::Balance) {}
}

/// Trait for getting the associated account and origin of a task.
///
/// For parachains, this is the sovereign account, which is controlled by the parachain
/// itself.
pub trait TaskAccountInterface {
/// The type for representing accounts.
type AccountId;

/// The overarching origin type.
type OuterOrigin: Into<Result<Self::TaskOrigin, Self::OuterOrigin>>;

/// The custom task origin. Given that all tasks on Polkadot are parachains this will most
/// likely be the `Parachain` origin.
type TaskOrigin;

/// Ensures that the origin is a task origin and returns the associated `TaskId`.
fn ensure_task_sovereign_account(o: Self::OuterOrigin) -> Result<TaskId, BadOrigin>;

/// Returns the associated account of a task.
fn sovereign_account(task: TaskId) -> Option<Self::AccountId>;
}
seadanda marked this conversation as resolved.
Show resolved Hide resolved
75 changes: 75 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,81 @@ impl<T: Config> Pallet<T> {
Ok(())
}

pub(crate) fn do_enable_auto_renew(
task: TaskId,
core: CoreIndex,
workload_end_hint: Option<Timeslice>,
) -> DispatchResult {
let sale = SaleInfo::<T>::get().ok_or(Error::<T>::NoSales)?;
let Some(sovereign_account) = T::SovereignAccountOf::sovereign_account(task) else {
return Err(Error::<T>::SovereignAccountNotFound.into());
};

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(sovereign_account.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.
Szegoo marked this conversation as resolved.
Show resolved Hide resolved
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())
};

if let CoreAssignment::Task(core_task) = schedule_item.assignment {
// Sovereign account of a task can only enable auto renewal for its own core.
ensure!(task == core_task, Error::<T>::NoPermission);
} else {
return Err(Error::<T>::NonTaskAutoRenewal.into())
};

// We are keeping the auto-renewals sorted by `CoreIndex`.
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))
})
.map_err(|_| Error::<T>::TooManyAutoRenewals)?;

Self::deposit_event(Event::AutoRenewalEnabled { core, task });
Ok(())
}

pub(crate) fn do_disable_auto_renew(task: TaskId, core: CoreIndex) -> DispatchResult {
AutoRenewals::<T>::try_mutate(|renewals| -> DispatchResult {
let pos = renewals
.binary_search_by(|r: &(CoreIndex, TaskId)| r.0.cmp(&core))
.map_err(|_| Error::<T>::AutoRenewalNotEnabled)?;

ensure!(Some(&(core, task)) == renewals.get(pos), Error::<T>::NoPermission);
renewals.remove(pos);
Ok(())
})?;

Self::deposit_event(Event::AutoRenewalDisabled { core, task });
Ok(())
}

pub(crate) fn ensure_cores_for_sale(
status: &StatusRecord,
sale: &SaleInfoRecordOf<T>,
Expand Down
Loading
Loading