From e6155b29ea1190462fa9ca548288dda2b86d9f59 Mon Sep 17 00:00:00 2001 From: talhadaar Date: Wed, 4 Dec 2024 22:31:27 +0500 Subject: [PATCH] no collator shall be left unpaid --- pallets/parachain-staking/src/lib.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/pallets/parachain-staking/src/lib.rs b/pallets/parachain-staking/src/lib.rs index 99c36aa3..98397816 100644 --- a/pallets/parachain-staking/src/lib.rs +++ b/pallets/parachain-staking/src/lib.rs @@ -2917,7 +2917,25 @@ pub mod pallet { let old_round = round - 1; // Get total collator staking number of round that is ending - let (in_reads, total_stake) = Self::get_total_collator_staking_num(old_round); + let (in_reads, mut total_stake) = Self::get_total_collator_staking_num(old_round); + + // Total stake cannot be zero + // We expect this is the case when runtime upgrade for token-economy-v2 is done. + // TODO this case can be removed in later upgrades. + if total_stake.is_zero() { + // there will be only 1 unfortunate author, as we force new round in runtime upgrade + CollatorBlocks::::iter_prefix(old_round).for_each(|(collator, num)| { + if let Some(state) = CandidatePool::::get(collator.clone()) { + let collator_total = T::CurrencyBalance::from(num) + .checked_mul(&state.total) + .unwrap_or_else(Zero::zero); + total_stake = total_stake.saturating_add(collator_total); + reads = reads.saturating_add(Weight::from_parts(1_u64, 0)); + }; + reads = reads.saturating_add(Weight::from_parts(1_u64, 0)); + }); + } + // Get total issuance of round that is ending let (issuance_weight, total_issuance) = Self::pot_issuance(); reads = reads.saturating_add(in_reads).saturating_add(issuance_weight);