Skip to content

Commit

Permalink
refactor pallet-service-payment
Browse files Browse the repository at this point in the history
  • Loading branch information
girazoki committed Feb 6, 2024
1 parent 07ffd24 commit 91a2995
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 44 deletions.
72 changes: 41 additions & 31 deletions pallets/services-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,7 @@ pub mod pallet {
) -> DispatchResultWithPostInfo {
ensure_root(origin)?;

if free_block_credits.is_zero() {
BlockProductionCredits::<T>::remove(para_id);
} else {
BlockProductionCredits::<T>::insert(para_id, free_block_credits);
}

Self::deposit_event(Event::<T>::BlockProductionCreditsSet {
para_id,
credits: free_block_credits,
});
Self::set_free_block_production_credits(&para_id, free_block_credits);

Ok(().into())
}
Expand Down Expand Up @@ -228,16 +219,7 @@ pub mod pallet {
) -> DispatchResultWithPostInfo {
ensure_root(origin)?;

if free_collator_assignment_credits.is_zero() {
CollatorAssignmentCredits::<T>::remove(para_id);
} else {
CollatorAssignmentCredits::<T>::insert(para_id, free_collator_assignment_credits);
}

Self::deposit_event(Event::<T>::CollatorAssignmentCreditsSet {
para_id,
credits: free_collator_assignment_credits,
});
Self::set_free_collator_assignment_credits(&para_id, free_collator_assignment_credits);

Ok(().into())
}
Expand Down Expand Up @@ -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::<T>::get(para_id).unwrap_or(BlockNumberFor::<T>::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::<T>::insert(para_id, block_production_updated_credits);
Self::deposit_event(Event::<T>::BlockProductionCreditsSet {
para_id: *para_id,
credits: block_production_updated_credits,
});
Self::set_free_block_production_credits(&para_id, block_production_updated_credits);
}

// Set number of credits to MaxCreditsStored
Expand All @@ -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::<T>::insert(
para_id,
Self::set_free_collator_assignment_credits(
&para_id,
collator_assignment_updated_credits,
);
Self::deposit_event(Event::<T>::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
Expand All @@ -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::<T>::remove(para_id);
} else {
CollatorAssignmentCredits::<T>::insert(para_id, free_collator_assignment_credits);
}

Self::deposit_event(Event::<T>::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<T>,
) {
if free_collator_block_production_credits.is_zero() {
BlockProductionCredits::<T>::remove(para_id);
} else {
BlockProductionCredits::<T>::insert(
para_id,
free_collator_block_production_credits,
);
}

Self::deposit_event(Event::<T>::BlockProductionCreditsSet {
para_id: *para_id,
credits: free_collator_block_production_credits,
});
}
}

#[pallet::genesis_config]
Expand Down
73 changes: 60 additions & 13 deletions runtime/dancebox/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ where
pub struct MigrateServicesPaymentAddCredits<T>(pub PhantomData<T>);
impl<T> Migration for MigrateServicesPaymentAddCredits<T>
where
T: cumulus_pallet_xcmp_queue::Config,
T: frame_system::Config,
{
fn friendly_name(&self) -> &str {
"TM_MigrateServicesPaymentAddCredits"
Expand Down Expand Up @@ -398,6 +398,44 @@ where
}
}

pub struct MigrateServicesPaymentAddCollatorAssignmentCredits<T>(pub PhantomData<T>);
impl<T> Migration for MigrateServicesPaymentAddCollatorAssignmentCredits<T>
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::<Runtime>::get();
let pending = pallet_registrar::PendingParaIds::<Runtime>::get();

let paused = pallet_registrar::Paused::<Runtime>::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(
&para_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,
Expand Down Expand Up @@ -573,6 +611,7 @@ where
Runtime: pallet_configuration::Config,
Runtime: pallet_xcm::Config,
Runtime: cumulus_pallet_xcmp_queue::Config,
Runtime: pallet_services_payment::Config,
<Runtime as pallet_balances::Config>::RuntimeHoldReason:
From<pallet_pooled_staking::HoldReason>,
{
Expand All @@ -582,14 +621,17 @@ where
//let migrate_config = MigrateConfigurationFullRotationPeriod::<Runtime>(Default::default());
//let migrate_xcm = PolkadotXcmMigration::<Runtime>(Default::default());
// let migrate_xcmp_queue = XcmpQueueMigration::<Runtime>(Default::default());
let migrate_services_payment =
MigrateServicesPaymentAddCredits::<Runtime>(Default::default());
let migrate_boot_nodes = MigrateBootNodes::<Runtime>(Default::default());
let migrate_config_parathread_params =
MigrateConfigurationParathreads::<Runtime>(Default::default());

let migrate_hold_reason_runtime_enum =
MigrateHoldReasonRuntimeEnum::<Runtime>(Default::default());
//let migrate_services_payment =
// MigrateServicesPaymentAddCredits::<Runtime>(Default::default());
//let migrate_boot_nodes = MigrateBootNodes::<Runtime>(Default::default());
//let migrate_config_parathread_params =
// MigrateConfigurationParathreads::<Runtime>(Default::default());

//let migrate_hold_reason_runtime_enum =
// MigrateHoldReasonRuntimeEnum::<Runtime>(Default::default());

let migrate_add_collator_assignment_credits =
MigrateServicesPaymentAddCollatorAssignmentCredits::<Runtime>(Default::default());
vec![
// Applied in runtime 200
//Box::new(migrate_invulnerables),
Expand All @@ -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),
]
}
}

0 comments on commit 91a2995

Please sign in to comment.