diff --git a/pallets/services-payment/src/lib.rs b/pallets/services-payment/src/lib.rs index 0f8031b1b..af92308d4 100644 --- a/pallets/services-payment/src/lib.rs +++ b/pallets/services-payment/src/lib.rs @@ -183,16 +183,7 @@ pub mod pallet { ) -> DispatchResultWithPostInfo { ensure_root(origin)?; - if free_block_credits.is_zero() { - BlockProductionCredits::::remove(para_id); - } else { - BlockProductionCredits::::insert(para_id, free_block_credits); - } - - Self::deposit_event(Event::::BlockProductionCreditsSet { - para_id, - credits: free_block_credits, - }); + Self::set_free_block_production_credits(¶_id, free_block_credits); Ok(().into()) } @@ -228,16 +219,7 @@ pub mod pallet { ) -> DispatchResultWithPostInfo { ensure_root(origin)?; - if free_collator_assignment_credits.is_zero() { - CollatorAssignmentCredits::::remove(para_id); - } else { - CollatorAssignmentCredits::::insert(para_id, free_collator_assignment_credits); - } - - Self::deposit_event(Event::::CollatorAssignmentCreditsSet { - para_id, - credits: free_collator_assignment_credits, - }); + Self::set_free_collator_assignment_credits(¶_id, free_collator_assignment_credits); Ok(().into()) } @@ -291,17 +273,14 @@ pub mod pallet { // This para id has already received free credits return Weight::default(); } + // Set number of credits to MaxCreditsStored let block_production_existing_credits = BlockProductionCredits::::get(para_id).unwrap_or(BlockNumberFor::::zero()); let block_production_updated_credits = T::MaxBlockProductionCreditsStored::get(); // Do not update credits if for some reason this para id had more if block_production_existing_credits < block_production_updated_credits { - BlockProductionCredits::::insert(para_id, block_production_updated_credits); - Self::deposit_event(Event::::BlockProductionCreditsSet { - para_id: *para_id, - credits: block_production_updated_credits, - }); + Self::set_free_block_production_credits(¶_id, block_production_updated_credits); } // Set number of credits to MaxCreditsStored @@ -311,14 +290,10 @@ pub mod pallet { // Do not update credits if for some reason this para id had more if collator_assignment_existing_credits < collator_assignment_updated_credits { - CollatorAssignmentCredits::::insert( - para_id, + Self::set_free_collator_assignment_credits( + ¶_id, collator_assignment_updated_credits, ); - Self::deposit_event(Event::::CollatorAssignmentCreditsSet { - para_id: *para_id, - credits: collator_assignment_updated_credits, - }); } // We only allow to call this function once per para id, even if it didn't actually @@ -327,6 +302,41 @@ pub mod pallet { Weight::default() } + + pub fn set_free_collator_assignment_credits( + para_id: &ParaId, + free_collator_assignment_credits: u32, + ) { + if free_collator_assignment_credits.is_zero() { + CollatorAssignmentCredits::::remove(para_id); + } else { + CollatorAssignmentCredits::::insert(para_id, free_collator_assignment_credits); + } + + Self::deposit_event(Event::::CollatorAssignmentCreditsSet { + para_id: *para_id, + credits: free_collator_assignment_credits, + }); + } + + pub fn set_free_block_production_credits( + para_id: &ParaId, + free_collator_block_production_credits: BlockNumberFor, + ) { + if free_collator_block_production_credits.is_zero() { + BlockProductionCredits::::remove(para_id); + } else { + BlockProductionCredits::::insert( + para_id, + free_collator_block_production_credits, + ); + } + + Self::deposit_event(Event::::BlockProductionCreditsSet { + para_id: *para_id, + credits: free_collator_block_production_credits, + }); + } } #[pallet::genesis_config] diff --git a/runtime/dancebox/src/migrations.rs b/runtime/dancebox/src/migrations.rs index 82dec71fb..924dd386a 100644 --- a/runtime/dancebox/src/migrations.rs +++ b/runtime/dancebox/src/migrations.rs @@ -365,7 +365,7 @@ where pub struct MigrateServicesPaymentAddCredits(pub PhantomData); impl Migration for MigrateServicesPaymentAddCredits where - T: cumulus_pallet_xcmp_queue::Config, + T: frame_system::Config, { fn friendly_name(&self) -> &str { "TM_MigrateServicesPaymentAddCredits" @@ -398,6 +398,44 @@ where } } +pub struct MigrateServicesPaymentAddCollatorAssignmentCredits(pub PhantomData); +impl Migration for MigrateServicesPaymentAddCollatorAssignmentCredits +where + T: pallet_services_payment::Config, +{ + fn friendly_name(&self) -> &str { + "TM_MigrateServicesPaymentAddCollatorAssignmentCredits" + } + + fn migrate(&self, _available_weight: Weight) -> Weight { + // For each parachain in pallet_registrar (active, pending or pending_verification), + // insert `MaxCreditsStored` to pallet_services_payment, + // and mark that parachain as "given_free_credits". + let mut para_ids = BTreeSet::new(); + let active = pallet_registrar::RegisteredParaIds::::get(); + let pending = pallet_registrar::PendingParaIds::::get(); + + let paused = pallet_registrar::Paused::::get(); + para_ids.extend(active); + para_ids.extend(pending.into_iter().flat_map(|(_session, active)| active)); + para_ids.extend(paused); + + let reads = 3 + 2 * para_ids.len() as u64; + let writes = 2 * para_ids.len() as u64; + + for para_id in para_ids { + // 2 reads 2 writes + ServicesPayment::set_free_collator_assignment_credits( + ¶_id, + T::MaxCollatorAssignmentCreditsStored::get(), + ); + } + + let db_weights = T::DbWeight::get(); + db_weights.reads_writes(reads, writes) + } +} + /// A reason for placing a hold on funds. #[derive( Clone, @@ -573,6 +611,7 @@ where Runtime: pallet_configuration::Config, Runtime: pallet_xcm::Config, Runtime: cumulus_pallet_xcmp_queue::Config, + Runtime: pallet_services_payment::Config, ::RuntimeHoldReason: From, { @@ -582,14 +621,17 @@ where //let migrate_config = MigrateConfigurationFullRotationPeriod::(Default::default()); //let migrate_xcm = PolkadotXcmMigration::(Default::default()); // let migrate_xcmp_queue = XcmpQueueMigration::(Default::default()); - let migrate_services_payment = - MigrateServicesPaymentAddCredits::(Default::default()); - let migrate_boot_nodes = MigrateBootNodes::(Default::default()); - let migrate_config_parathread_params = - MigrateConfigurationParathreads::(Default::default()); - - let migrate_hold_reason_runtime_enum = - MigrateHoldReasonRuntimeEnum::(Default::default()); + //let migrate_services_payment = + // MigrateServicesPaymentAddCredits::(Default::default()); + //let migrate_boot_nodes = MigrateBootNodes::(Default::default()); + //let migrate_config_parathread_params = + // MigrateConfigurationParathreads::(Default::default()); + + //let migrate_hold_reason_runtime_enum = + // MigrateHoldReasonRuntimeEnum::(Default::default()); + + let migrate_add_collator_assignment_credits = + MigrateServicesPaymentAddCollatorAssignmentCredits::(Default::default()); vec![ // Applied in runtime 200 //Box::new(migrate_invulnerables), @@ -601,10 +643,15 @@ where //Box::new(migrate_xcm), // Applied in runtime 300 //Box::new(migrate_xcmp_queue), - Box::new(migrate_services_payment), - Box::new(migrate_hold_reason_runtime_enum), - Box::new(migrate_boot_nodes), - Box::new(migrate_config_parathread_params), + // Applied in runtime 400 + //Box::new(migrate_services_payment), + // Applied in runtime 400 + //Box::new(migrate_hold_reason_runtime_enum), + // Applied in runtime 400 + //Box::new(migrate_boot_nodes), + // Applied in runtime 400 + //Box::new(migrate_config_parathread_params), + Box::new(migrate_add_collator_assignment_credits), ] } }