From a973e281d9c261b160bcc4c47b898baf2409fc3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=B3nal=20Murray?= Date: Thu, 18 Apr 2024 16:18:44 +0100 Subject: [PATCH 01/16] Bump broker version to get renewals fix --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- system-parachains/coretime/coretime-kusama/src/lib.rs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fddf46d540..94dfbca72d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7282,9 +7282,9 @@ dependencies = [ [[package]] name = "pallet-broker" -version = "0.7.0" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd8cfe04e8c3f9ca8342ac785f2b1aee6140e1809546fc6f3a99fad20a8dfbf9" +checksum = "3626d7e8e49b153b84c74594e1fb4b6d64720b5a9588297d3ba3c049c3b3b9e3" dependencies = [ "bitvec", "frame-benchmarking", diff --git a/Cargo.toml b/Cargo.toml index 8c9d50680a..52d3fce132 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -68,7 +68,7 @@ pallet-bridge-grandpa = { version = "0.8.0", default-features = false } pallet-bridge-messages = { version = "0.8.0", default-features = false } pallet-bridge-parachains = { version = "0.8.0", default-features = false } pallet-bridge-relayers = { version = "0.8.0", default-features = false } -pallet-broker = { version = "0.7.0", default-features = false } +pallet-broker = { version = "0.7.1", default-features = false } pallet-child-bounties = { version = "28.0.0", default-features = false } pallet-collator-selection = { version = "10.0.0", default-features = false } pallet-collective = { version = "29.0.0", default-features = false } diff --git a/system-parachains/coretime/coretime-kusama/src/lib.rs b/system-parachains/coretime/coretime-kusama/src/lib.rs index 7500be16bb..a8c88e0451 100644 --- a/system-parachains/coretime/coretime-kusama/src/lib.rs +++ b/system-parachains/coretime/coretime-kusama/src/lib.rs @@ -129,7 +129,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("coretime-kusama"), impl_name: create_runtime_str!("coretime-kusama"), authoring_version: 1, - spec_version: 1_002_000, + spec_version: 1_002_001, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 0, From 96b9511d577ade257d9eb39419bdc9833ad53628 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=B3nal=20Murray?= Date: Fri, 19 Apr 2024 12:19:25 +0100 Subject: [PATCH 02/16] Add migration to fix leases --- .../coretime/coretime-kusama/src/lib.rs | 6 +- .../coretime-kusama/src/migrations.rs | 204 ++++++++++++++++++ 2 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 system-parachains/coretime/coretime-kusama/src/migrations.rs diff --git a/system-parachains/coretime/coretime-kusama/src/lib.rs b/system-parachains/coretime/coretime-kusama/src/lib.rs index a8c88e0451..9d98ceea97 100644 --- a/system-parachains/coretime/coretime-kusama/src/lib.rs +++ b/system-parachains/coretime/coretime-kusama/src/lib.rs @@ -23,6 +23,7 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); mod coretime; +mod migrations; #[cfg(test)] mod tests; mod weights; @@ -106,7 +107,10 @@ pub type UncheckedExtrinsic = generic::UncheckedExtrinsic; /// Migrations to apply on runtime upgrade. -pub type Migrations = (pallet_xcm::migration::MigrateToLatestXcmVersion,); +pub type Migrations = ( + pallet_xcm::migration::MigrateToLatestXcmVersion, + migrations::bootstrapping::ImportLeases, +); /// Executive: handles dispatch to the various modules. pub type Executive = frame_executive::Executive< diff --git a/system-parachains/coretime/coretime-kusama/src/migrations.rs b/system-parachains/coretime/coretime-kusama/src/migrations.rs new file mode 100644 index 0000000000..f901294f27 --- /dev/null +++ b/system-parachains/coretime/coretime-kusama/src/migrations.rs @@ -0,0 +1,204 @@ +// Copyright (C) Parity Technologies and the various Polkadot contributors, see Contributions.md +// for a list of specific contributors. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/// The XCM Transact which was meant to set the leases as part of the Kusama relay runtime upgrade +/// did not have enough weight. Therefore the leases were not migrated. +/// +/// This migration populates the leases and restarts the sale from whichever timeslice it runs. +/// +/// This does not affect storage structure, only values. +pub mod bootstrapping { + use crate::{weights, Runtime, RuntimeOrigin}; + use frame_support::{pallet_prelude::*, traits::OnRuntimeUpgrade}; + use pallet_broker::WeightInfo; + #[cfg(feature = "try-runtime")] + use pallet_broker::{ + CoreAssignment::{Pool, Task}, + CoreMask, LeaseRecordItem, Leases, SaleInfo, SaleInfoRecordOf, Schedule, ScheduleItem, + Workplan, + }; + use sp_runtime::create_runtime_str; + #[cfg(feature = "try-runtime")] + use sp_runtime::TryRuntimeError; + #[cfg(feature = "try-runtime")] + use sp_std::vec::Vec; + + /// The log target. + const TARGET: &'static str = "runtime::bootstrapping::import-leases"; + + // Alias into the broker weights for this runtime. + type BrokerWeights = weights::pallet_broker::WeightInfo; + + pub struct ImportLeases; + + impl OnRuntimeUpgrade for ImportLeases { + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, sp_runtime::TryRuntimeError> { + let sale_info = SaleInfo::::get().unwrap(); + Ok(sale_info.encode()) + } + + fn on_runtime_upgrade() -> Weight { + let version = ::Version::get(); + + // This migration contains hardcoded values only relevant to Kusama Coretime + // 1002000. + if version.spec_name != create_runtime_str!("coretime_kusama") || + version.spec_version != 1002000 + { + // Bail - this should never run again. + return ::DbWeight::get().reads(1); + } + + for (para_id, end) in LEASES { + match pallet_broker::Pallet::::set_lease( + RuntimeOrigin::root(), + para_id, + end, + ) { + Ok(_) => + log::info!(target: TARGET, "Importing lease for parachain {}", ¶_id), + Err(_) => + log::error!(target: TARGET, "Importing lease for parachain {} failed!", ¶_id), + } + } + + // The values used in referendum 375 included 52 cores. Replaying this here shifts the + // start of the sale, while crucially populating the workplan with the leases and + // recalculating the number of cores to be offered. However, there are 4 system + // parachains + 1 pool core + 47 leases + 3 cores for the open market, therefore we need + // to start sales with 55 cores. + match pallet_broker::Pallet::::request_core_count(RuntimeOrigin::root(), 55) { + Ok(_) => log::info!(target: TARGET, "Request for 55 cores sent."), + Err(_) => log::error!(target: TARGET, "Request for 55 cores failed to send."), + } + match pallet_broker::Pallet::::start_sales( + RuntimeOrigin::root(), + 5_000_000_000_0000, + 55, + ) { + Ok(_) => log::info!(target: TARGET, "Sales started"), + Err(_) => log::error!(target: TARGET, "Start sales failed!"), + } + + // Weight for setting every lease and starting the sales, plus one read for the + // version check. + BrokerWeights::set_lease() + .saturating_mul(LEASES.len() as u64) + .saturating_add(BrokerWeights::start_sales(55)) + .saturating_add(::DbWeight::get().reads(1)) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(state: Vec) -> Result<(), TryRuntimeError> { + let prev_sale_info = >::decode(&mut &state[..]).unwrap(); + let sale_info = SaleInfo::::get().unwrap(); + let leases = Leases::::get(); + assert_eq!(leases.len(), LEASES.len()); + + // Check the sale start has changed and the cores_offered updated. + assert!(sale_info.sale_start > prev_sale_info.sale_start); + assert!(sale_info.region_begin > prev_sale_info.region_begin); + assert_eq!(sale_info.cores_offered, 3); + + // The workplan entries start from the region begin reported by the new SaleInfo. + let workplan_start = sale_info.region_begin; + + // Check the reservations are still in the workplan out of an abundance of caution. + for (core_id, task) in + [Task(1000), Task(1001), Task(1002), Task(1005), Pool].into_iter().enumerate() + { + assert_eq!( + Workplan::::get((workplan_start, core_id as u16)), + Some(Schedule::truncate_from(Vec::from([ScheduleItem { + mask: CoreMask::complete(), + assignment: task, + }]))) + ); + } + + // Iterate through hardcoded leases and check they're all correctly in state and + // scheduled in the workplan. + for (i, (para_id, until)) in LEASES.iter().enumerate() { + // Add the system parachains and pool core as an offset - these should come before + // the leases. + let core_id = i as u16 + 5; + assert!(leases.contains(&LeaseRecordItem { until: *until, task: *para_id })); + assert_eq!( + Workplan::::get((workplan_start, core_id)), + Some(Schedule::truncate_from(Vec::from([ScheduleItem { + mask: CoreMask::complete(), + assignment: Task(*para_id), + }]))) + ); + } + + Ok(()) + } + } + + // Hardcoded para ids and their end timeslice. + // Calculated using https://github.com/seadanda/coretime-scripts/blob/main/get_leases.py + const LEASES: [(u32, u32); 47] = [ + (2000, 340200), + (2001, 302400), + (2004, 332640), + (2007, 317520), + (2011, 325080), + (2012, 309960), + (2015, 287280), + (2023, 309960), + (2024, 309960), + (2048, 302400), + (2084, 340200), + (2085, 294840), + (2087, 340200), + (2088, 287280), + (2090, 340200), + (2092, 287280), + (2095, 332640), + (2096, 332640), + (2105, 325080), + (2106, 325080), + (2110, 317520), + (2113, 332640), + (2114, 317520), + (2119, 340200), + (2121, 332640), + (2123, 294840), + (2124, 287280), + (2125, 294840), + (2222, 302400), + (2233, 294840), + (2236, 317520), + (2239, 332640), + (2241, 325080), + (2274, 294840), + (2275, 294840), + (2281, 302400), + (3334, 309960), + (3336, 317520), + (3338, 317520), + (3339, 325080), + (3340, 325080), + (3343, 317520), + (3344, 340200), + (3345, 325080), + (3347, 287280), + (3348, 287280), + (3350, 340200), + ]; +} From 975454bb1520cd8adfd3a87ba78a0476d09f3136 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=B3nal=20Murray?= Date: Fri, 19 Apr 2024 12:19:43 +0100 Subject: [PATCH 03/16] Add coretime-kusama uri to matrix --- .github/workflows/runtimes-matrix.json | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/runtimes-matrix.json b/.github/workflows/runtimes-matrix.json index f599883850..265924dc10 100644 --- a/.github/workflows/runtimes-matrix.json +++ b/.github/workflows/runtimes-matrix.json @@ -58,6 +58,7 @@ "name": "coretime-kusama", "package": "coretime-kusama-runtime", "path": "system-parachains/coretime/coretime-kusama", + "uri": "wss://kusama-coretime-rpc.polkadot.io:443", "is_relay": false }, { From c890e22997816e77c4eb4f787375e3e78543cd8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=B3nal=20Murray?= Date: Fri, 19 Apr 2024 13:59:22 +0100 Subject: [PATCH 04/16] Improve how migration runs with try-runtime and fix version check --- .../coretime-kusama/src/migrations.rs | 69 ++++++++++++------- 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/system-parachains/coretime/coretime-kusama/src/migrations.rs b/system-parachains/coretime/coretime-kusama/src/migrations.rs index f901294f27..694a55c97e 100644 --- a/system-parachains/coretime/coretime-kusama/src/migrations.rs +++ b/system-parachains/coretime/coretime-kusama/src/migrations.rs @@ -30,7 +30,6 @@ pub mod bootstrapping { CoreMask, LeaseRecordItem, Leases, SaleInfo, SaleInfoRecordOf, Schedule, ScheduleItem, Workplan, }; - use sp_runtime::create_runtime_str; #[cfg(feature = "try-runtime")] use sp_runtime::TryRuntimeError; #[cfg(feature = "try-runtime")] @@ -45,21 +44,12 @@ pub mod bootstrapping { pub struct ImportLeases; impl OnRuntimeUpgrade for ImportLeases { - #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, sp_runtime::TryRuntimeError> { - let sale_info = SaleInfo::::get().unwrap(); - Ok(sale_info.encode()) - } - fn on_runtime_upgrade() -> Weight { - let version = ::Version::get(); - // This migration contains hardcoded values only relevant to Kusama Coretime - // 1002000. - if version.spec_name != create_runtime_str!("coretime_kusama") || - version.spec_version != 1002000 - { - // Bail - this should never run again. + // 1002000 before it has any leases. These checks could be tightened. + if Leases::::get().len() > 0 { + // Already has leases, bail + log::error!(target: TARGET, "This migration includes hardcoded values not relevant to this runtime. Bailing."); return ::DbWeight::get().reads(1); } @@ -94,23 +84,44 @@ pub mod bootstrapping { Err(_) => log::error!(target: TARGET, "Start sales failed!"), } - // Weight for setting every lease and starting the sales, plus one read for the - // version check. + // Weight for setting every lease and starting the sales, plus one read for leases + // check. BrokerWeights::set_lease() .saturating_mul(LEASES.len() as u64) + .saturating_add(BrokerWeights::request_core_count(55)) .saturating_add(BrokerWeights::start_sales(55)) .saturating_add(::DbWeight::get().reads(1)) } + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, sp_runtime::TryRuntimeError> { + let sale_info = SaleInfo::::get().unwrap(); + Ok(sale_info.encode()) + } + #[cfg(feature = "try-runtime")] fn post_upgrade(state: Vec) -> Result<(), TryRuntimeError> { + use pallet_broker::{AllowedRenewalId, AllowedRenewals, Configuration}; + let prev_sale_info = >::decode(&mut &state[..]).unwrap(); + + // Idempotency hack - sorry. This just checks that the migration has run in the correct + // sale period, because the following checks only matter in that case. A bit of a "trust + // me bro", but this is the first sale for coretime-kusama. + if prev_sale_info.sale_start != 104193 { + log::info!(target: TARGET, "Idempotency hack has filtered checks for this run."); + return Ok(()); + } + + log::info!(target: TARGET, "Idempotency hack has not affected us for this run."); + let sale_info = SaleInfo::::get().unwrap(); - let leases = Leases::::get(); - assert_eq!(leases.len(), LEASES.len()); + let now = frame_system::Pallet::::block_number(); + let config = Configuration::::get().unwrap(); - // Check the sale start has changed and the cores_offered updated. - assert!(sale_info.sale_start > prev_sale_info.sale_start); + // Check the sale start has changed as expected and the cores_offered is the correct + // number. + assert_eq!(sale_info.sale_start, now + config.interlude_length); assert!(sale_info.region_begin > prev_sale_info.region_begin); assert_eq!(sale_info.cores_offered, 3); @@ -130,13 +141,25 @@ pub mod bootstrapping { ); } - // Iterate through hardcoded leases and check they're all correctly in state and - // scheduled in the workplan. + // Because we also run start_sales, 12 expiring leases are removed from the original 47, + // leaving 35. + let leases = Leases::::get(); + assert_eq!(leases.len(), 35); + + // Iterate through hardcoded leases and check they're all correctly in state (leases or + // allowedrenewals) and scheduled in the workplan. for (i, (para_id, until)) in LEASES.iter().enumerate() { // Add the system parachains and pool core as an offset - these should come before // the leases. let core_id = i as u16 + 5; - assert!(leases.contains(&LeaseRecordItem { until: *until, task: *para_id })); + if !leases.contains(&LeaseRecordItem { until: *until, task: *para_id }) { + // Check that the 12 who no longer have a lease can renew. + AllowedRenewals::::get(AllowedRenewalId { + core: core_id, + when: sale_info.region_end, + }); + } + // They should all be in the workplan for next sale. assert_eq!( Workplan::::get((workplan_start, core_id)), Some(Schedule::truncate_from(Vec::from([ScheduleItem { From 407d7ef77810168ebd59dba8b45343b493838dde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=B3nal=20Murray?= Date: Fri, 19 Apr 2024 14:21:58 +0100 Subject: [PATCH 05/16] Add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 27012bd33c..8eb7ce241a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## Unreleased - Add `pallet-vesting` to Asset Hubs ([polkadot-fellows/runtimes#269](https://github.com/polkadot-fellows/runtimes/pull/269)) +- Fix Kusama Coretime launch issues: import leases and fix renewals for short leases ([polkadot-fellows/runtimes#276](https://github.com/polkadot-fellows/runtimes/pull/276)) ## [1.2.1] 09.04.2024 From 18c0fef60440ed583703e3e38592baa1735f645b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=B3nal=20Murray?= Date: Fri, 19 Apr 2024 14:52:45 +0100 Subject: [PATCH 06/16] Actually check the AllowedRenewals --- .../coretime-kusama/src/migrations.rs | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/system-parachains/coretime/coretime-kusama/src/migrations.rs b/system-parachains/coretime/coretime-kusama/src/migrations.rs index 694a55c97e..28066bd2fe 100644 --- a/system-parachains/coretime/coretime-kusama/src/migrations.rs +++ b/system-parachains/coretime/coretime-kusama/src/migrations.rs @@ -26,6 +26,7 @@ pub mod bootstrapping { use pallet_broker::WeightInfo; #[cfg(feature = "try-runtime")] use pallet_broker::{ + AllowedRenewalId, AllowedRenewalRecord, AllowedRenewals, Configuration, CoreAssignment::{Pool, Task}, CoreMask, LeaseRecordItem, Leases, SaleInfo, SaleInfoRecordOf, Schedule, ScheduleItem, Workplan, @@ -101,8 +102,6 @@ pub mod bootstrapping { #[cfg(feature = "try-runtime")] fn post_upgrade(state: Vec) -> Result<(), TryRuntimeError> { - use pallet_broker::{AllowedRenewalId, AllowedRenewals, Configuration}; - let prev_sale_info = >::decode(&mut &state[..]).unwrap(); // Idempotency hack - sorry. This just checks that the migration has run in the correct @@ -152,21 +151,27 @@ pub mod bootstrapping { // Add the system parachains and pool core as an offset - these should come before // the leases. let core_id = i as u16 + 5; + // This is the entry found in Workplan and AllowedRenewal + let workload = Schedule::truncate_from(Vec::from([ScheduleItem { + mask: CoreMask::complete(), + assignment: Task(*para_id), + }])); + + // Check that the 12 who no longer have a lease can renew. if !leases.contains(&LeaseRecordItem { until: *until, task: *para_id }) { - // Check that the 12 who no longer have a lease can renew. - AllowedRenewals::::get(AllowedRenewalId { - core: core_id, - when: sale_info.region_end, - }); + assert_eq!( + AllowedRenewals::::get(AllowedRenewalId { + core: core_id, + when: sale_info.region_end, + }), + Some(AllowedRenewalRecord { + price: 5_000_000_000_0000, + completion: pallet_broker::CompletionStatus::Complete(workload.clone()) + }) + ); } // They should all be in the workplan for next sale. - assert_eq!( - Workplan::::get((workplan_start, core_id)), - Some(Schedule::truncate_from(Vec::from([ScheduleItem { - mask: CoreMask::complete(), - assignment: Task(*para_id), - }]))) - ); + assert_eq!(Workplan::::get((workplan_start, core_id)), Some(workload)); } Ok(()) From 6a60492c921cafb6e7c393e1e031f44d6b8707f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=B3nal=20Murray?= Date: Fri, 19 Apr 2024 16:35:53 +0100 Subject: [PATCH 07/16] Fix Leases import --- system-parachains/coretime/coretime-kusama/src/migrations.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/system-parachains/coretime/coretime-kusama/src/migrations.rs b/system-parachains/coretime/coretime-kusama/src/migrations.rs index 28066bd2fe..f39aee066e 100644 --- a/system-parachains/coretime/coretime-kusama/src/migrations.rs +++ b/system-parachains/coretime/coretime-kusama/src/migrations.rs @@ -23,14 +23,13 @@ pub mod bootstrapping { use crate::{weights, Runtime, RuntimeOrigin}; use frame_support::{pallet_prelude::*, traits::OnRuntimeUpgrade}; - use pallet_broker::WeightInfo; #[cfg(feature = "try-runtime")] use pallet_broker::{ AllowedRenewalId, AllowedRenewalRecord, AllowedRenewals, Configuration, CoreAssignment::{Pool, Task}, - CoreMask, LeaseRecordItem, Leases, SaleInfo, SaleInfoRecordOf, Schedule, ScheduleItem, - Workplan, + CoreMask, LeaseRecordItem, SaleInfo, SaleInfoRecordOf, Schedule, ScheduleItem, Workplan, }; + use pallet_broker::{Leases, WeightInfo}; #[cfg(feature = "try-runtime")] use sp_runtime::TryRuntimeError; #[cfg(feature = "try-runtime")] From 1550e01b4a34deb21cd58943a44335a41b88f253 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=B3nal=20Murray?= Date: Fri, 19 Apr 2024 16:45:40 +0100 Subject: [PATCH 08/16] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- .../coretime/coretime-kusama/src/migrations.rs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/system-parachains/coretime/coretime-kusama/src/migrations.rs b/system-parachains/coretime/coretime-kusama/src/migrations.rs index f39aee066e..0b4049ac58 100644 --- a/system-parachains/coretime/coretime-kusama/src/migrations.rs +++ b/system-parachains/coretime/coretime-kusama/src/migrations.rs @@ -47,7 +47,7 @@ pub mod bootstrapping { fn on_runtime_upgrade() -> Weight { // This migration contains hardcoded values only relevant to Kusama Coretime // 1002000 before it has any leases. These checks could be tightened. - if Leases::::get().len() > 0 { + if Leases::::get().decode_len().unwrap_or(0) > 0 { // Already has leases, bail log::error!(target: TARGET, "This migration includes hardcoded values not relevant to this runtime. Bailing."); return ::DbWeight::get().reads(1); @@ -95,22 +95,16 @@ pub mod bootstrapping { #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, sp_runtime::TryRuntimeError> { + if Leases::::get().decode_len().unwrap_or(0) > 0 { return Ok(Vec::new()) } let sale_info = SaleInfo::::get().unwrap(); Ok(sale_info.encode()) } #[cfg(feature = "try-runtime")] fn post_upgrade(state: Vec) -> Result<(), TryRuntimeError> { + if state.is_empty() { return Ok(()) } let prev_sale_info = >::decode(&mut &state[..]).unwrap(); - // Idempotency hack - sorry. This just checks that the migration has run in the correct - // sale period, because the following checks only matter in that case. A bit of a "trust - // me bro", but this is the first sale for coretime-kusama. - if prev_sale_info.sale_start != 104193 { - log::info!(target: TARGET, "Idempotency hack has filtered checks for this run."); - return Ok(()); - } - log::info!(target: TARGET, "Idempotency hack has not affected us for this run."); let sale_info = SaleInfo::::get().unwrap(); From 43852c07d5d38814809392eaaba4c4f3af9f387a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=B3nal=20Murray?= Date: Fri, 19 Apr 2024 18:13:01 +0100 Subject: [PATCH 09/16] Update lib.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- system-parachains/coretime/coretime-kusama/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system-parachains/coretime/coretime-kusama/src/lib.rs b/system-parachains/coretime/coretime-kusama/src/lib.rs index 9d98ceea97..0da38ad843 100644 --- a/system-parachains/coretime/coretime-kusama/src/lib.rs +++ b/system-parachains/coretime/coretime-kusama/src/lib.rs @@ -133,7 +133,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("coretime-kusama"), impl_name: create_runtime_str!("coretime-kusama"), authoring_version: 1, - spec_version: 1_002_001, + spec_version: 1_002_002, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 0, From 93952995450e7b389bd72dc27f36b503690d91e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Fri, 19 Apr 2024 20:30:25 +0200 Subject: [PATCH 10/16] Fixes --- relay/kusama/Cargo.toml | 2 +- system-parachains/coretime/coretime-kusama/src/migrations.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/relay/kusama/Cargo.toml b/relay/kusama/Cargo.toml index 44f6a79367..9c73470343 100644 --- a/relay/kusama/Cargo.toml +++ b/relay/kusama/Cargo.toml @@ -21,7 +21,7 @@ kusama-runtime-constants = { package = "kusama-runtime-constants", path = "const sp-api = { workspace = true } inherents = { package = "sp-inherents", default-features = false , version = "27.0.0" } offchain-primitives = { package = "sp-offchain", default-features = false , version = "27.0.0" } -sp-std = { package = "sp-std", workspace = true } +sp-std = { workspace = true } sp-application-crypto = { workspace = true } sp-arithmetic = { workspace = true } sp-genesis-builder = { workspace = true } diff --git a/system-parachains/coretime/coretime-kusama/src/migrations.rs b/system-parachains/coretime/coretime-kusama/src/migrations.rs index 0b4049ac58..a96eaae9ec 100644 --- a/system-parachains/coretime/coretime-kusama/src/migrations.rs +++ b/system-parachains/coretime/coretime-kusama/src/migrations.rs @@ -47,7 +47,7 @@ pub mod bootstrapping { fn on_runtime_upgrade() -> Weight { // This migration contains hardcoded values only relevant to Kusama Coretime // 1002000 before it has any leases. These checks could be tightened. - if Leases::::get().decode_len().unwrap_or(0) > 0 { + if Leases::::decode_len().unwrap_or(0) > 0 { // Already has leases, bail log::error!(target: TARGET, "This migration includes hardcoded values not relevant to this runtime. Bailing."); return ::DbWeight::get().reads(1); @@ -136,7 +136,7 @@ pub mod bootstrapping { // Because we also run start_sales, 12 expiring leases are removed from the original 47, // leaving 35. let leases = Leases::::get(); - assert_eq!(leases.len(), 35); + assert_eq!(leases.len(), LEASES.iter().filter(|(_, l)| sale_info.region_end * 80 <= l).count()); // Iterate through hardcoded leases and check they're all correctly in state (leases or // allowedrenewals) and scheduled in the workplan. From 663a9bd6252dc201f4414b371a2881c116d063aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=B3nal=20Murray?= Date: Fri, 19 Apr 2024 19:49:03 +0100 Subject: [PATCH 11/16] Fix typo in start sales amount and some tidying --- .../coretime-kusama/src/migrations.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/system-parachains/coretime/coretime-kusama/src/migrations.rs b/system-parachains/coretime/coretime-kusama/src/migrations.rs index a96eaae9ec..9f88bad3be 100644 --- a/system-parachains/coretime/coretime-kusama/src/migrations.rs +++ b/system-parachains/coretime/coretime-kusama/src/migrations.rs @@ -77,7 +77,7 @@ pub mod bootstrapping { } match pallet_broker::Pallet::::start_sales( RuntimeOrigin::root(), - 5_000_000_000_0000, + 5_000_000_000_000, 55, ) { Ok(_) => log::info!(target: TARGET, "Sales started"), @@ -95,17 +95,21 @@ pub mod bootstrapping { #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, sp_runtime::TryRuntimeError> { - if Leases::::get().decode_len().unwrap_or(0) > 0 { return Ok(Vec::new()) } + if Leases::::decode_len().unwrap_or(0) > 0 { + return Ok(Vec::new()) + } let sale_info = SaleInfo::::get().unwrap(); Ok(sale_info.encode()) } #[cfg(feature = "try-runtime")] fn post_upgrade(state: Vec) -> Result<(), TryRuntimeError> { - if state.is_empty() { return Ok(()) } + if state.is_empty() { + return Ok(()) + } let prev_sale_info = >::decode(&mut &state[..]).unwrap(); - log::info!(target: TARGET, "Idempotency hack has not affected us for this run."); + log::info!(target: TARGET, "Checking migration."); let sale_info = SaleInfo::::get().unwrap(); let now = frame_system::Pallet::::block_number(); @@ -136,7 +140,10 @@ pub mod bootstrapping { // Because we also run start_sales, 12 expiring leases are removed from the original 47, // leaving 35. let leases = Leases::::get(); - assert_eq!(leases.len(), LEASES.iter().filter(|(_, l)| sale_info.region_end * 80 <= l).count()); + assert_eq!( + leases.len(), + LEASES.iter().filter(|(_, l)| sale_info.region_end <= *l).count() + ); // Iterate through hardcoded leases and check they're all correctly in state (leases or // allowedrenewals) and scheduled in the workplan. @@ -158,7 +165,7 @@ pub mod bootstrapping { when: sale_info.region_end, }), Some(AllowedRenewalRecord { - price: 5_000_000_000_0000, + price: 5_000_000_000_000, completion: pallet_broker::CompletionStatus::Complete(workload.clone()) }) ); From 6fc3dfb8f1badaaa4bffb1cee163599e78afeef6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=B3nal=20Murray?= Date: Fri, 19 Apr 2024 19:52:41 +0100 Subject: [PATCH 12/16] Make clippy happier --- system-parachains/coretime/coretime-kusama/src/migrations.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system-parachains/coretime/coretime-kusama/src/migrations.rs b/system-parachains/coretime/coretime-kusama/src/migrations.rs index 9f88bad3be..f710a5725c 100644 --- a/system-parachains/coretime/coretime-kusama/src/migrations.rs +++ b/system-parachains/coretime/coretime-kusama/src/migrations.rs @@ -36,7 +36,7 @@ pub mod bootstrapping { use sp_std::vec::Vec; /// The log target. - const TARGET: &'static str = "runtime::bootstrapping::import-leases"; + const TARGET: &str = "runtime::bootstrapping::import-leases"; // Alias into the broker weights for this runtime. type BrokerWeights = weights::pallet_broker::WeightInfo; From 95aab79eed95d279db014752384c06ad5a5d8bb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Fri, 19 Apr 2024 20:54:45 +0200 Subject: [PATCH 13/16] Determine core count programmatically --- .../coretime/coretime-kusama/src/migrations.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/system-parachains/coretime/coretime-kusama/src/migrations.rs b/system-parachains/coretime/coretime-kusama/src/migrations.rs index f710a5725c..a975c9adb3 100644 --- a/system-parachains/coretime/coretime-kusama/src/migrations.rs +++ b/system-parachains/coretime/coretime-kusama/src/migrations.rs @@ -71,7 +71,12 @@ pub mod bootstrapping { // recalculating the number of cores to be offered. However, there are 4 system // parachains + 1 pool core + 47 leases + 3 cores for the open market, therefore we need // to start sales with 55 cores. - match pallet_broker::Pallet::::request_core_count(RuntimeOrigin::root(), 55) { + match pallet_broker::Pallet::::request_core_count( + RuntimeOrigin::root(), + pallet_broker::Reservations::::decode_len().unwrap_or(0) as u16 + + pallet_broker::Leases::::decode_len().unwrap_or(0) as u16 + + 1 + 3, + ) { Ok(_) => log::info!(target: TARGET, "Request for 55 cores sent."), Err(_) => log::error!(target: TARGET, "Request for 55 cores failed to send."), } @@ -142,7 +147,7 @@ pub mod bootstrapping { let leases = Leases::::get(); assert_eq!( leases.len(), - LEASES.iter().filter(|(_, l)| sale_info.region_end <= *l).count() + LEASES.iter().filter(|(_, l)| sale_info.region_end * 80 <= l).count() ); // Iterate through hardcoded leases and check they're all correctly in state (leases or @@ -174,6 +179,10 @@ pub mod bootstrapping { assert_eq!(Workplan::::get((workplan_start, core_id)), Some(workload)); } + // Ensure we have requested the correct number of events. + assert!(frame_system::Pallet::::read_events_no_consensus() + .any(|e| pallet_broker::Event::CoreCountRequested { core_count: 55 }.into() == e)); + Ok(()) } } From 679eb8a135571db3ea63645a0b9b4b5beeeadd60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Fri, 19 Apr 2024 21:01:30 +0200 Subject: [PATCH 14/16] Fix compilation --- .../coretime/coretime-kusama/src/migrations.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/system-parachains/coretime/coretime-kusama/src/migrations.rs b/system-parachains/coretime/coretime-kusama/src/migrations.rs index a975c9adb3..1ebcf5fa5f 100644 --- a/system-parachains/coretime/coretime-kusama/src/migrations.rs +++ b/system-parachains/coretime/coretime-kusama/src/migrations.rs @@ -147,7 +147,7 @@ pub mod bootstrapping { let leases = Leases::::get(); assert_eq!( leases.len(), - LEASES.iter().filter(|(_, l)| sale_info.region_end * 80 <= l).count() + LEASES.iter().filter(|(_, l)| sale_info.region_end * 80 <= *l).count() ); // Iterate through hardcoded leases and check they're all correctly in state (leases or @@ -180,8 +180,11 @@ pub mod bootstrapping { } // Ensure we have requested the correct number of events. - assert!(frame_system::Pallet::::read_events_no_consensus() - .any(|e| pallet_broker::Event::CoreCountRequested { core_count: 55 }.into() == e)); + assert!(frame_system::Pallet::::read_events_no_consensus().any(|e| { + crate::RuntimeEvent::from(pallet_broker::Event::::CoreCountRequested { + core_count: 55, + }) == e.event + })); Ok(()) } From e69b0fec5bcbf3e3762731ce874715764497a97f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Fri, 19 Apr 2024 21:10:05 +0200 Subject: [PATCH 15/16] My bad --- system-parachains/coretime/coretime-kusama/src/migrations.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system-parachains/coretime/coretime-kusama/src/migrations.rs b/system-parachains/coretime/coretime-kusama/src/migrations.rs index 1ebcf5fa5f..520bb4d7a0 100644 --- a/system-parachains/coretime/coretime-kusama/src/migrations.rs +++ b/system-parachains/coretime/coretime-kusama/src/migrations.rs @@ -147,7 +147,7 @@ pub mod bootstrapping { let leases = Leases::::get(); assert_eq!( leases.len(), - LEASES.iter().filter(|(_, l)| sale_info.region_end * 80 <= *l).count() + LEASES.iter().filter(|(_, l)| sale_info.region_end <= *l).count() ); // Iterate through hardcoded leases and check they're all correctly in state (leases or From 71b139305e246cb8e6f53bafe7f37c0f74f167a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Fri, 19 Apr 2024 21:40:14 +0200 Subject: [PATCH 16/16] Fix it :D --- .../coretime-kusama/src/migrations.rs | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/system-parachains/coretime/coretime-kusama/src/migrations.rs b/system-parachains/coretime/coretime-kusama/src/migrations.rs index 520bb4d7a0..94e8903b09 100644 --- a/system-parachains/coretime/coretime-kusama/src/migrations.rs +++ b/system-parachains/coretime/coretime-kusama/src/migrations.rs @@ -71,11 +71,13 @@ pub mod bootstrapping { // recalculating the number of cores to be offered. However, there are 4 system // parachains + 1 pool core + 47 leases + 3 cores for the open market, therefore we need // to start sales with 55 cores. + let core_count = pallet_broker::Reservations::::decode_len().unwrap_or(0) + as u16 + pallet_broker::Leases::::decode_len().unwrap_or(0) + as u16 + 3; + match pallet_broker::Pallet::::request_core_count( RuntimeOrigin::root(), - pallet_broker::Reservations::::decode_len().unwrap_or(0) as u16 + - pallet_broker::Leases::::decode_len().unwrap_or(0) as u16 + - 1 + 3, + core_count, ) { Ok(_) => log::info!(target: TARGET, "Request for 55 cores sent."), Err(_) => log::error!(target: TARGET, "Request for 55 cores failed to send."), @@ -83,7 +85,7 @@ pub mod bootstrapping { match pallet_broker::Pallet::::start_sales( RuntimeOrigin::root(), 5_000_000_000_000, - 55, + core_count, ) { Ok(_) => log::info!(target: TARGET, "Sales started"), Err(_) => log::error!(target: TARGET, "Start sales failed!"), @@ -181,9 +183,16 @@ pub mod bootstrapping { // Ensure we have requested the correct number of events. assert!(frame_system::Pallet::::read_events_no_consensus().any(|e| { - crate::RuntimeEvent::from(pallet_broker::Event::::CoreCountRequested { - core_count: 55, - }) == e.event + match e.event { + crate::RuntimeEvent::Broker( + pallet_broker::Event::::CoreCountRequested { core_count }, + ) => { + log::info!("{core_count:?}"); + + core_count == 55 + }, + _ => false, + } })); Ok(())