Skip to content

Commit

Permalink
Use relay chain block number in the broker pallet instead of block nu…
Browse files Browse the repository at this point in the history
…mber (#5656)

Based on #3331
Related to #3268

Implements migrations with customizable block number to relay height
number translation function.

Adds block to relay height migration code for rococo and westend.

---------

Co-authored-by: DavidK <[email protected]>
Co-authored-by: Kian Paimani <[email protected]>
  • Loading branch information
3 people authored Nov 11, 2024
1 parent dd9514f commit 1b0cbe9
Show file tree
Hide file tree
Showing 10 changed files with 345 additions and 31 deletions.
22 changes: 21 additions & 1 deletion cumulus/parachains/runtimes/coretime/coretime-rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
pub use sp_runtime::BuildStorage;
use sp_runtime::{
generic, impl_opaque_keys,
traits::{BlakeTwo256, Block as BlockT},
traits::{BlakeTwo256, Block as BlockT, BlockNumberProvider},
transaction_validity::{TransactionSource, TransactionValidity},
ApplyExtrinsicResult, DispatchError, MultiAddress, Perbill, RuntimeDebug,
};
Expand Down Expand Up @@ -124,6 +124,7 @@ pub type Migrations = (
pallet_broker::migration::MigrateV0ToV1<Runtime>,
pallet_broker::migration::MigrateV1ToV2<Runtime>,
pallet_broker::migration::MigrateV2ToV3<Runtime>,
pallet_broker::migration::MigrateV3ToV4<Runtime, BrokerMigrationV4BlockConversion>,
// permanent
pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,
);
Expand Down Expand Up @@ -591,6 +592,25 @@ impl pallet_sudo::Config for Runtime {
type WeightInfo = pallet_sudo::weights::SubstrateWeight<Runtime>;
}

pub struct BrokerMigrationV4BlockConversion;

impl pallet_broker::migration::v4::BlockToRelayHeightConversion<Runtime>
for BrokerMigrationV4BlockConversion
{
fn convert_block_number_to_relay_height(input_block_number: u32) -> u32 {
let relay_height = pallet_broker::RCBlockNumberProviderOf::<
<Runtime as pallet_broker::Config>::Coretime,
>::current_block_number();
let parachain_block_number = frame_system::Pallet::<Runtime>::block_number();
let offset = relay_height - parachain_block_number * 2;
offset + input_block_number * 2
}

fn convert_block_length_to_relay_length(input_block_length: u32) -> u32 {
input_block_length * 2
}
}

// Create the runtime by composing the FRAME pallets that were previously configured.
construct_runtime!(
pub enum Runtime
Expand Down
22 changes: 21 additions & 1 deletion cumulus/parachains/runtimes/coretime/coretime-westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
pub use sp_runtime::BuildStorage;
use sp_runtime::{
generic, impl_opaque_keys,
traits::{BlakeTwo256, Block as BlockT},
traits::{BlakeTwo256, Block as BlockT, BlockNumberProvider},
transaction_validity::{TransactionSource, TransactionValidity},
ApplyExtrinsicResult, DispatchError, MultiAddress, Perbill, RuntimeDebug,
};
Expand Down Expand Up @@ -124,6 +124,7 @@ pub type Migrations = (
pallet_broker::migration::MigrateV0ToV1<Runtime>,
pallet_broker::migration::MigrateV1ToV2<Runtime>,
pallet_broker::migration::MigrateV2ToV3<Runtime>,
pallet_broker::migration::MigrateV3ToV4<Runtime, BrokerMigrationV4BlockConversion>,
// permanent
pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,
);
Expand Down Expand Up @@ -586,6 +587,25 @@ impl pallet_utility::Config for Runtime {
type WeightInfo = weights::pallet_utility::WeightInfo<Runtime>;
}

pub struct BrokerMigrationV4BlockConversion;

impl pallet_broker::migration::v4::BlockToRelayHeightConversion<Runtime>
for BrokerMigrationV4BlockConversion
{
fn convert_block_number_to_relay_height(input_block_number: u32) -> u32 {
let relay_height = pallet_broker::RCBlockNumberProviderOf::<
<Runtime as pallet_broker::Config>::Coretime,
>::current_block_number();
let parachain_block_number = frame_system::Pallet::<Runtime>::block_number();
let offset = relay_height - parachain_block_number * 2;
offset + input_block_number * 2
}

fn convert_block_length_to_relay_length(input_block_length: u32) -> u32 {
input_block_length * 2
}
}

// Create the runtime by composing the FRAME pallets that were previously configured.
construct_runtime!(
pub enum Runtime
Expand Down
18 changes: 18 additions & 0 deletions prdoc/pr_5656.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: Use Relay Blocknumber in Pallet Broker

doc:
- audience: Runtime Dev
description: |
Changing `sale_start`, `interlude_length` and `leading_length` in `pallet_broker` to use relay chain block numbers instead of parachain block numbers.
Relay chain block numbers are almost deterministic and more future proof.

crates:
- name: pallet-broker
bump: major
- name: coretime-rococo-runtime
bump: major
- name: coretime-westend-runtime
bump: major
10 changes: 7 additions & 3 deletions substrate/frame/broker/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,11 @@ mod benches {
_(origin as T::RuntimeOrigin, initial_price, extra_cores.try_into().unwrap());

assert!(SaleInfo::<T>::get().is_some());
let sale_start = RCBlockNumberProviderOf::<T::Coretime>::current_block_number() +
config.interlude_length;
assert_last_event::<T>(
Event::SaleInitialized {
sale_start: 2u32.into(),
sale_start,
leadin_length: 1u32.into(),
start_price: 1_000_000_000u32.into(),
end_price: 10_000_000u32.into(),
Expand Down Expand Up @@ -787,7 +789,7 @@ mod benches {
let core_count = n.try_into().unwrap();
let config = new_config_record::<T>();

let now = frame_system::Pallet::<T>::block_number();
let now = RCBlockNumberProviderOf::<T::Coretime>::current_block_number();
let end_price = 10_000_000u32.into();
let commit_timeslice = Broker::<T>::latest_timeslice_ready_to_commit(&config);
let sale = SaleInfoRecordOf::<T> {
Expand Down Expand Up @@ -844,9 +846,11 @@ mod benches {
}

assert!(SaleInfo::<T>::get().is_some());
let sale_start = RCBlockNumberProviderOf::<T::Coretime>::current_block_number() +
config.interlude_length;
assert_last_event::<T>(
Event::SaleInitialized {
sale_start: 2u32.into(),
sale_start,
leadin_length: 1u32.into(),
start_price: 1_000_000_000u32.into(),
end_price: 10_000_000u32.into(),
Expand Down
10 changes: 5 additions & 5 deletions substrate/frame/broker/src/dispatchable_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use frame_support::{
traits::{fungible::Mutate, tokens::Preservation::Expendable, DefensiveResult},
};
use sp_arithmetic::traits::{CheckedDiv, Saturating, Zero};
use sp_runtime::traits::Convert;
use sp_runtime::traits::{BlockNumberProvider, Convert};
use CompletionStatus::{Complete, Partial};

impl<T: Config> Pallet<T> {
Expand Down Expand Up @@ -91,7 +91,7 @@ impl<T: Config> Pallet<T> {
last_committed_timeslice: commit_timeslice.saturating_sub(1),
last_timeslice: Self::current_timeslice(),
};
let now = frame_system::Pallet::<T>::block_number();
let now = RCBlockNumberProviderOf::<T::Coretime>::current_block_number();
// Imaginary old sale for bootstrapping the first actual sale:
let old_sale = SaleInfoRecord {
sale_start: now,
Expand Down Expand Up @@ -119,7 +119,7 @@ impl<T: Config> Pallet<T> {
let mut sale = SaleInfo::<T>::get().ok_or(Error::<T>::NoSales)?;
Self::ensure_cores_for_sale(&status, &sale)?;

let now = frame_system::Pallet::<T>::block_number();
let now = RCBlockNumberProviderOf::<T::Coretime>::current_block_number();
ensure!(now > sale.sale_start, Error::<T>::TooEarly);
let price = Self::sale_price(&sale, now);
ensure!(price_limit >= price, Error::<T>::Overpriced);
Expand Down Expand Up @@ -171,7 +171,7 @@ impl<T: Config> Pallet<T> {

let begin = sale.region_end;
let price_cap = record.price + config.renewal_bump * record.price;
let now = frame_system::Pallet::<T>::block_number();
let now = RCBlockNumberProviderOf::<T::Coretime>::current_block_number();
let price = Self::sale_price(&sale, now).min(price_cap);
log::debug!(
"Renew with: sale price: {:?}, price cap: {:?}, old price: {:?}",
Expand Down Expand Up @@ -569,7 +569,7 @@ impl<T: Config> Pallet<T> {

Self::ensure_cores_for_sale(&status, &sale)?;

let now = frame_system::Pallet::<T>::block_number();
let now = RCBlockNumberProviderOf::<T::Coretime>::current_block_number();
Ok(Self::sale_price(&sale, now))
}
}
11 changes: 6 additions & 5 deletions substrate/frame/broker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub mod pallet {
use frame_system::pallet_prelude::*;
use sp_runtime::traits::{Convert, ConvertBack, MaybeConvert};

const STORAGE_VERSION: StorageVersion = StorageVersion::new(3);
const STORAGE_VERSION: StorageVersion = StorageVersion::new(4);

#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
Expand Down Expand Up @@ -305,10 +305,11 @@ pub mod pallet {
},
/// A new sale has been initialized.
SaleInitialized {
/// The local block number at which the sale will/did start.
sale_start: BlockNumberFor<T>,
/// The length in blocks of the Leadin Period (where the price is decreasing).
leadin_length: BlockNumberFor<T>,
/// The relay block number at which the sale will/did start.
sale_start: RelayBlockNumberOf<T>,
/// The length in relay chain blocks of the Leadin Period (where the price is
/// decreasing).
leadin_length: RelayBlockNumberOf<T>,
/// The price of Bulk Coretime at the beginning of the Leadin Period.
start_price: BalanceOf<T>,
/// The price of Bulk Coretime after the Leadin Period.
Expand Down
Loading

0 comments on commit 1b0cbe9

Please sign in to comment.