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 43 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
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,13 @@ use frame_support::{
OnUnbalanced,
},
};
use pallet_broker::{CoreAssignment, CoreIndex, CoretimeInterface, PartsOf57600, RCBlockNumberOf};
use pallet_broker::{
CoreAssignment, CoreIndex, CoretimeInterface, PartsOf57600, RCBlockNumberOf, TaskId,
};
use parachains_common::{AccountId, Balance, BlockNumber};
use sp_runtime::traits::MaybeConvert;
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 +221,15 @@ impl CoretimeInterface for CoretimeAllocator {
}
}

pub struct SovereignAccountOf;
impl MaybeConvert<TaskId, AccountId> for SovereignAccountOf {
fn maybe_convert(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 @@ -232,5 +245,7 @@ impl pallet_broker::Config for Runtime {
type WeightInfo = weights::pallet_broker::WeightInfo<Runtime>;
type PalletId = BrokerPalletId;
type AdminOrigin = EnsureRoot<AccountId>;
type SovereignAccountOf = SovereignAccountOf;
type MaxAutoRenewals = ConstU32<100>;
type PriceAdapter = pallet_broker::CenterTargetPrice<Balance>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -523,4 +523,42 @@ impl<T: frame_system::Config> pallet_broker::WeightInfo for WeightInfo<T> {
.saturating_add(T::DbWeight::get().reads(1))
.saturating_add(T::DbWeight::get().writes(1))
}
/// Storage: `Broker::SaleInfo` (r:1 w:1)
/// Proof: `Broker::SaleInfo` (`max_values`: Some(1), `max_size`: Some(57), added: 552, mode: `MaxEncodedLen`)
/// Storage: `Broker::PotentialRenewals` (r:1 w:2)
/// Proof: `Broker::PotentialRenewals` (`max_values`: None, `max_size`: Some(1233), added: 3708, mode: `MaxEncodedLen`)
/// Storage: `Broker::Configuration` (r:1 w:0)
/// Proof: `Broker::Configuration` (`max_values`: Some(1), `max_size`: Some(31), added: 526, mode: `MaxEncodedLen`)
/// Storage: `Broker::Status` (r:1 w:0)
/// Proof: `Broker::Status` (`max_values`: Some(1), `max_size`: Some(18), added: 513, mode: `MaxEncodedLen`)
/// Storage: `System::Account` (r:1 w:1)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
/// Storage: `Authorship::Author` (r:1 w:0)
/// Proof: `Authorship::Author` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`)
/// Storage: `System::Digest` (r:1 w:0)
/// Proof: `System::Digest` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `Broker::AutoRenewals` (r:1 w:1)
/// Proof: `Broker::AutoRenewals` (`max_values`: Some(1), `max_size`: Some(31), added: 526, mode: `MaxEncodedLen`)
/// Storage: `Broker::Workplan` (r:0 w:1)
/// Proof: `Broker::Workplan` (`max_values`: None, `max_size`: Some(1216), added: 3691, mode: `MaxEncodedLen`)
fn enable_auto_renew() -> Weight {
// Proof Size summary in bytes:
// Measured: `914`
// Estimated: `4698`
// Minimum execution time: 51_938_000 picoseconds.
Weight::from_parts(55_025_000, 4698)
.saturating_add(T::DbWeight::get().reads(8_u64))
.saturating_add(T::DbWeight::get().writes(6_u64))
}
/// Storage: `Broker::AutoRenewals` (r:1 w:1)
/// Proof: `Broker::AutoRenewals` (`max_values`: Some(1), `max_size`: Some(31), added: 526, mode: `MaxEncodedLen`)
fn disable_auto_renew() -> Weight {
// Proof Size summary in bytes:
// Measured: `480`
// Estimated: `1516`
// Minimum execution time: 9_628_000 picoseconds.
Weight::from_parts(10_400_000, 1516)
.saturating_add(T::DbWeight::get().reads(1_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
}
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,13 @@ use frame_support::{
OnUnbalanced,
},
};
use pallet_broker::{CoreAssignment, CoreIndex, CoretimeInterface, PartsOf57600, RCBlockNumberOf};
use pallet_broker::{
CoreAssignment, CoreIndex, CoretimeInterface, PartsOf57600, RCBlockNumberOf, TaskId,
};
use parachains_common::{AccountId, Balance, BlockNumber};
use sp_runtime::traits::MaybeConvert;
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 +233,15 @@ impl CoretimeInterface for CoretimeAllocator {
}
}

pub struct SovereignAccountOf;
impl MaybeConvert<TaskId, AccountId> for SovereignAccountOf {
fn maybe_convert(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 @@ -245,5 +258,7 @@ impl pallet_broker::Config for Runtime {
type WeightInfo = weights::pallet_broker::WeightInfo<Runtime>;
type PalletId = BrokerPalletId;
type AdminOrigin = EnsureRoot<AccountId>;
type SovereignAccountOf = SovereignAccountOf;
type MaxAutoRenewals = ConstU32<20>;
type PriceAdapter = pallet_broker::CenterTargetPrice<Balance>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -523,4 +523,42 @@ impl<T: frame_system::Config> pallet_broker::WeightInfo for WeightInfo<T> {
.saturating_add(T::DbWeight::get().reads(1))
.saturating_add(T::DbWeight::get().writes(1))
}
/// Storage: `Broker::SaleInfo` (r:1 w:1)
/// Proof: `Broker::SaleInfo` (`max_values`: Some(1), `max_size`: Some(57), added: 552, mode: `MaxEncodedLen`)
/// Storage: `Broker::PotentialRenewals` (r:1 w:2)
/// Proof: `Broker::PotentialRenewals` (`max_values`: None, `max_size`: Some(1233), added: 3708, mode: `MaxEncodedLen`)
/// Storage: `Broker::Configuration` (r:1 w:0)
/// Proof: `Broker::Configuration` (`max_values`: Some(1), `max_size`: Some(31), added: 526, mode: `MaxEncodedLen`)
/// Storage: `Broker::Status` (r:1 w:0)
/// Proof: `Broker::Status` (`max_values`: Some(1), `max_size`: Some(18), added: 513, mode: `MaxEncodedLen`)
/// Storage: `System::Account` (r:1 w:1)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
/// Storage: `Authorship::Author` (r:1 w:0)
/// Proof: `Authorship::Author` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`)
/// Storage: `System::Digest` (r:1 w:0)
/// Proof: `System::Digest` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `Broker::AutoRenewals` (r:1 w:1)
/// Proof: `Broker::AutoRenewals` (`max_values`: Some(1), `max_size`: Some(31), added: 526, mode: `MaxEncodedLen`)
/// Storage: `Broker::Workplan` (r:0 w:1)
/// Proof: `Broker::Workplan` (`max_values`: None, `max_size`: Some(1216), added: 3691, mode: `MaxEncodedLen`)
fn enable_auto_renew() -> Weight {
// Proof Size summary in bytes:
// Measured: `914`
// Estimated: `4698`
// Minimum execution time: 51_938_000 picoseconds.
Weight::from_parts(55_025_000, 4698)
.saturating_add(T::DbWeight::get().reads(8_u64))
.saturating_add(T::DbWeight::get().writes(6_u64))
}
/// Storage: `Broker::AutoRenewals` (r:1 w:1)
/// Proof: `Broker::AutoRenewals` (`max_values`: Some(1), `max_size`: Some(31), added: 526, mode: `MaxEncodedLen`)
fn disable_auto_renew() -> Weight {
// Proof Size summary in bytes:
// Measured: `480`
// Estimated: `1516`
// Minimum execution time: 9_628_000 picoseconds.
Weight::from_parts(10_400_000, 1516)
.saturating_add(T::DbWeight::get().reads(1_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
}
16 changes: 14 additions & 2 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,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::TaskId;
#[allow(deprecated)]
pub use pallet_transaction_payment::{CurrencyAdapter, Multiplier, TargetedFeeAdjustment};
use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo};
Expand All @@ -93,8 +94,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, BlakeTwo256, Block as BlockT, Bounded, ConvertInto,
MaybeConvert, NumberFor, OpaqueKeys, SaturatedConversion, StaticLookup,
},
transaction_validity::{TransactionPriority, TransactionSource, TransactionValidity},
ApplyExtrinsicResult, FixedPointNumber, FixedU128, Perbill, Percent, Permill, Perquintill,
Expand Down Expand Up @@ -2133,6 +2134,15 @@ impl CoretimeInterface for CoretimeProvider {
}
}

pub struct SovereignAccountOf;
// Dummy implementation which converts `TaskId` to `AccountId`.
impl MaybeConvert<TaskId, AccountId> for SovereignAccountOf {
fn maybe_convert(task: TaskId) -> Option<AccountId> {
let mut account: [u8; 32] = [0; 32];
account[..4].copy_from_slice(&task.to_le_bytes());
Some(account.into())
}
}
impl pallet_broker::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
Expand All @@ -2145,6 +2155,8 @@ impl pallet_broker::Config for Runtime {
type WeightInfo = ();
type PalletId = BrokerPalletId;
type AdminOrigin = EnsureRoot<AccountId>;
type SovereignAccountOf = SovereignAccountOf;
type MaxAutoRenewals = ConstU32<10>;
type PriceAdapter = pallet_broker::CenterTargetPrice<Balance>;
}

Expand Down
123 changes: 121 additions & 2 deletions substrate/frame/broker/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ use frame_support::{
use frame_system::{Pallet as System, RawOrigin};
use sp_arithmetic::{traits::Zero, Perbill};
use sp_core::Get;
use sp_runtime::{traits::BlockNumberProvider, Saturating};
use sp_runtime::{
traits::{BlockNumberProvider, MaybeConvert},
SaturatedConversion, Saturating,
};
use sp_std::{vec, vec::Vec};

const SEED: u32 = 0;
Expand All @@ -41,6 +44,10 @@ fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
frame_system::Pallet::<T>::assert_last_event(generic_event.into());
}

fn assert_has_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
frame_system::Pallet::<T>::assert_has_event(generic_event.into());
}

fn new_config_record<T: Config>() -> ConfigRecordOf<T> {
ConfigRecord {
advance_notice: 2u32.into(),
Expand Down Expand Up @@ -771,7 +778,7 @@ mod benches {
}

#[benchmark]
fn rotate_sale(n: Linear<0, { MAX_CORE_COUNT.into() }>) {
fn rotate_sale(n: Linear<0, { MAX_CORE_COUNT.into() }>) -> Result<(), BenchmarkError> {
let core_count = n.try_into().unwrap();
let config = new_config_record::<T>();

Expand Down Expand Up @@ -805,6 +812,27 @@ mod benches {
// Assume Leases to be filled for worst case
setup_leases::<T>(T::MaxLeasedCores::get(), 1, 10);

// Assume max auto renewals for worst case.
(0..T::MaxAutoRenewals::get()).try_for_each(|indx| -> Result<(), BenchmarkError> {
let task = 1000 + indx;
let caller: T::AccountId = T::SovereignAccountOf::maybe_convert(task)
.expect("Failed to get sovereign account");
T::Currency::set_balance(
&caller.clone(),
T::Currency::minimum_balance().saturating_add(100u32.into()),
);

let region = Broker::<T>::do_purchase(caller.clone(), 10u32.into())
.map_err(|_| BenchmarkError::Weightless)?;

Broker::<T>::do_assign(region, None, task, Final)
.map_err(|_| BenchmarkError::Weightless)?;

Broker::<T>::do_enable_auto_renew(caller, region.core, task, None)?;

Ok(())
})?;

#[block]
{
Broker::<T>::rotate_sale(sale.clone(), &config, &status);
Expand All @@ -828,6 +856,30 @@ mod benches {
}
.into(),
);

// Make sure all cores got renewed:
(0..T::MaxAutoRenewals::get()).for_each(|indx| {
let task = 1000 + indx;
let who = T::SovereignAccountOf::maybe_convert(task)
.expect("Failed to get sovereign account");
assert_has_event::<T>(
Event::Renewed {
who,
old_core: 10 + indx as u16, // first ten cores are allocated to leases.
core: 10 + indx as u16,
price: 10u32.saturated_into(),
begin: 7,
duration: 3,
workload: Schedule::truncate_from(vec![ScheduleItem {
assignment: Task(task),
mask: CoreMask::complete(),
}]),
}
.into(),
);
});

Ok(())
}

#[benchmark]
Expand Down Expand Up @@ -939,6 +991,73 @@ mod benches {
Ok(())
}

#[benchmark]
fn enable_auto_renew() -> Result<(), BenchmarkError> {
let _core = setup_and_start_sale::<T>()?;

advance_to::<T>(2);

let caller: T::AccountId =
T::SovereignAccountOf::maybe_convert(2001).expect("Failed to get sovereign account");
T::Currency::set_balance(
&caller.clone(),
T::Currency::minimum_balance().saturating_add(100u32.into()),
);

let region = Broker::<T>::do_purchase(caller.clone(), 10u32.into())
.map_err(|_| BenchmarkError::Weightless)?;

Broker::<T>::do_assign(region, None, 2001, Final)
.map_err(|_| BenchmarkError::Weightless)?;
// advance to next bulk sale:
advance_to::<T>(6);

// The most 'intensive' path is when we renew the core upon enabling auto-renewal.
Szegoo marked this conversation as resolved.
Show resolved Hide resolved

#[extrinsic_call]
_(RawOrigin::Signed(caller), region.core, 2001, None);

assert_last_event::<T>(Event::AutoRenewalEnabled { core: region.core, task: 2001 }.into());
// Make sure we indeed renewed:
assert!(PotentialRenewals::<T>::get(PotentialRenewalId {
core: region.core,
when: 10 // region end after renewal
})
.is_some());

Ok(())
}

#[benchmark]
fn disable_auto_renew() -> Result<(), BenchmarkError> {
let _core = setup_and_start_sale::<T>()?;

advance_to::<T>(2);

let caller: T::AccountId =
T::SovereignAccountOf::maybe_convert(2001).expect("Failed to get sovereign account");
T::Currency::set_balance(
&caller.clone(),
T::Currency::minimum_balance().saturating_add(100u32.into()),
);

let region = Broker::<T>::do_purchase(caller.clone(), 10u32.into())
.map_err(|_| BenchmarkError::Weightless)?;

Broker::<T>::do_assign(region, None, 2001, Final)
.map_err(|_| BenchmarkError::Weightless)?;

advance_to::<T>(6);
Broker::<T>::do_enable_auto_renew(caller.clone(), region.core, 2001, None)?;

#[extrinsic_call]
_(RawOrigin::Signed(caller), region.core, 2001);

assert_last_event::<T>(Event::AutoRenewalDisabled { core: region.core, task: 2001 }.into());

Ok(())
}

// Implements a test for each benchmark. Execute with:
// `cargo test -p pallet-broker --features runtime-benchmarks`.
impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test);
Expand Down
Loading
Loading