Skip to content

Commit 6ad7329

Browse files
Adam ReifAdam Reif
authored andcommitted
Rename Threshold -> Tolerance & Percentile -> Baseline
1 parent dcd1a6a commit 6ad7329

File tree

6 files changed

+106
-106
lines changed

6 files changed

+106
-106
lines changed

pallets/collator-selection/src/benchmarking.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,28 +149,28 @@ benchmarks! {
149149
assert_last_event::<T>(Event::NewCandidacyBond(bond).into());
150150
}
151151

152-
set_eviction_percentile {
152+
set_eviction_baseline {
153153
let percentile = 80u8;
154154
let origin = T::UpdateOrigin::successful_origin();
155155
}: {
156156
assert_ok!(
157-
<CollatorSelection<T>>::set_eviction_percentile(origin, percentile)
157+
<CollatorSelection<T>>::set_eviction_baseline(origin, percentile)
158158
);
159159
}
160160
verify {
161-
assert_last_event::<T>(Event::NewEvictionPercentile(percentile).into());
161+
assert_last_event::<T>(Event::NewEvictionBaseline(percentile).into());
162162
}
163163

164-
set_eviction_threshold {
164+
set_eviction_tolerance {
165165
let percentage = 10u8;
166166
let origin = T::UpdateOrigin::successful_origin();
167167
}: {
168168
assert_ok!(
169-
<CollatorSelection<T>>::set_eviction_threshold(origin, percentage)
169+
<CollatorSelection<T>>::set_eviction_tolerance(origin, percentage)
170170
);
171171
}
172172
verify {
173-
assert_last_event::<T>(Event::NewEvictionThreshold(percentage).into());
173+
assert_last_event::<T>(Event::NewEvictionTolerance(percentage).into());
174174
}
175175

176176
// worse case is when we have all the max-candidate slots filled except one, and we fill that
@@ -291,12 +291,12 @@ benchmarks! {
291291
let c in 1 .. T::MaxCandidates::get();
292292

293293
<CandidacyBond<T>>::put(T::Currency::minimum_balance());
294-
<EvictionPercentile<T>>::put(Percent::from_percent(100)); // Consider all collators
295-
<EvictionThreshold<T>>::put(Percent::from_percent(0)); // Kick anyone not at perfect performance
294+
<EvictionBaseline<T>>::put(Percent::from_percent(100)); // Consider all collators
295+
<EvictionTolerance<T>>::put(Percent::from_percent(0)); // Kick anyone not at perfect performance
296296
<DesiredCandidates<T>>::put(c);
297297
frame_system::Pallet::<T>::set_block_number(0u32.into());
298298

299-
let p = <CollatorSelection<T>>::eviction_percentile();
299+
let p = <CollatorSelection<T>>::eviction_baseline();
300300
register_validators::<T>(c);
301301
register_candidates::<T>(c);
302302

pallets/collator-selection/src/lib.rs

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,15 @@ pub mod pallet {
196196

197197
/// Performance percentile to use as baseline for collator eviction
198198
#[pallet::storage]
199-
#[pallet::getter(fn eviction_percentile)]
200-
pub type EvictionPercentile<T: Config> = StorageValue<_, Percent, ValueQuery>;
199+
#[pallet::getter(fn eviction_baseline)]
200+
pub type EvictionBaseline<T: Config> = StorageValue<_, Percent, ValueQuery>;
201201

202202
/// Percentage of underperformance to _tolerate_ before evicting a collator
203203
///
204-
/// i.e. A collator gets evicted if it produced _less_ than x% fewer blocks than the collator at EvictionPercentile
204+
/// i.e. A collator gets evicted if it produced _less_ than x% fewer blocks than the collator at EvictionBaseline
205205
#[pallet::storage]
206-
#[pallet::getter(fn eviction_threshold)]
207-
pub type EvictionThreshold<T: Config> = StorageValue<_, Percent, ValueQuery>;
206+
#[pallet::getter(fn eviction_tolerance)]
207+
pub type EvictionTolerance<T: Config> = StorageValue<_, Percent, ValueQuery>;
208208

209209
/// Desired number of candidates.
210210
///
@@ -222,8 +222,8 @@ pub mod pallet {
222222
pub struct GenesisConfig<T: Config> {
223223
pub invulnerables: Vec<T::AccountId>,
224224
pub candidacy_bond: BalanceOf<T>,
225-
pub eviction_percentile: Percent,
226-
pub eviction_threshold: Percent,
225+
pub eviction_baseline: Percent,
226+
pub eviction_tolerance: Percent,
227227
pub desired_candidates: u32,
228228
}
229229

@@ -233,8 +233,8 @@ pub mod pallet {
233233
Self {
234234
invulnerables: Default::default(),
235235
candidacy_bond: Default::default(),
236-
eviction_percentile: Percent::zero(), // Note: eviction disabled by default
237-
eviction_threshold: Percent::one(), // Note: eviction disabled by default
236+
eviction_baseline: Percent::zero(), // Note: eviction disabled by default
237+
eviction_tolerance: Percent::one(), // Note: eviction disabled by default
238238
desired_candidates: Default::default(),
239239
}
240240
}
@@ -261,17 +261,17 @@ pub mod pallet {
261261
"genesis desired_candidates are more than T::MaxCandidates",
262262
);
263263
assert!(
264-
self.eviction_percentile <= Percent::one(),
265-
"Percentile must be given as number between 0 and 100",
264+
self.eviction_baseline <= Percent::one(),
265+
"Eviction baseline must be given as a percentile - number between 0 and 100",
266266
);
267267
assert!(
268-
self.eviction_threshold <= Percent::one(),
269-
"Kicking threshold must be given as number between 0 and 100",
268+
self.eviction_tolerance <= Percent::one(),
269+
"Eviction tolerance must be given as a percentage - number between 0 and 100",
270270
);
271271
<DesiredCandidates<T>>::put(&self.desired_candidates);
272272
<CandidacyBond<T>>::put(&self.candidacy_bond);
273-
<EvictionPercentile<T>>::put(&self.eviction_percentile);
274-
<EvictionThreshold<T>>::put(&self.eviction_threshold);
273+
<EvictionBaseline<T>>::put(&self.eviction_baseline);
274+
<EvictionTolerance<T>>::put(&self.eviction_tolerance);
275275
<Invulnerables<T>>::put(&self.invulnerables);
276276
}
277277
}
@@ -282,8 +282,8 @@ pub mod pallet {
282282
NewInvulnerables(Vec<T::AccountId>),
283283
NewDesiredCandidates(u32),
284284
NewCandidacyBond(BalanceOf<T>),
285-
NewEvictionPercentile(u8),
286-
NewEvictionThreshold(u8),
285+
NewEvictionBaseline(u8),
286+
NewEvictionTolerance(u8),
287287
CandidateAdded(T::AccountId, BalanceOf<T>),
288288
CandidateRemoved(T::AccountId),
289289
}
@@ -371,28 +371,28 @@ pub mod pallet {
371371
/// Set the collator performance percentile used as baseline for eviction
372372
///
373373
/// `percentile`: x-th percentile of collator performance to use as eviction baseline
374-
#[pallet::weight(T::WeightInfo::set_eviction_percentile())]
375-
pub fn set_eviction_percentile(
374+
#[pallet::weight(T::WeightInfo::set_eviction_baseline())]
375+
pub fn set_eviction_baseline(
376376
origin: OriginFor<T>,
377377
percentile: u8,
378378
) -> DispatchResultWithPostInfo {
379379
T::UpdateOrigin::ensure_origin(origin)?;
380-
<EvictionPercentile<T>>::put(Percent::from_percent(percentile)); // NOTE: from_percent saturates at 100
381-
Self::deposit_event(Event::NewEvictionPercentile(percentile));
380+
<EvictionBaseline<T>>::put(Percent::from_percent(percentile)); // NOTE: from_percent saturates at 100
381+
Self::deposit_event(Event::NewEvictionBaseline(percentile));
382382
Ok(().into())
383383
}
384384

385385
/// Set the tolerated underperformance percentage before evicting
386386
///
387-
/// `percentage`: x% of missed blocks under eviction_percentile to tolerate
388-
#[pallet::weight(T::WeightInfo::set_eviction_threshold())]
389-
pub fn set_eviction_threshold(
387+
/// `percentage`: x% of missed blocks under eviction_baseline to tolerate
388+
#[pallet::weight(T::WeightInfo::set_eviction_tolerance())]
389+
pub fn set_eviction_tolerance(
390390
origin: OriginFor<T>,
391391
percentage: u8,
392392
) -> DispatchResultWithPostInfo {
393393
T::UpdateOrigin::ensure_origin(origin)?;
394-
<EvictionThreshold<T>>::put(Percent::from_percent(percentage)); // NOTE: from_percent saturates at 100
395-
Self::deposit_event(Event::NewEvictionThreshold(percentage));
394+
<EvictionTolerance<T>>::put(Percent::from_percent(percentage)); // NOTE: from_percent saturates at 100
395+
Self::deposit_event(Event::NewEvictionTolerance(percentage));
396396
Ok(().into())
397397
}
398398

@@ -567,12 +567,12 @@ pub mod pallet {
567567
if candidates.is_empty() {
568568
return Vec::new(); // No candidates means we're running invulnerables only
569569
}
570-
let percentile_for_kick = Self::eviction_percentile();
570+
let percentile_for_kick = Self::eviction_baseline();
571571
if percentile_for_kick == Percent::zero() {
572572
return Vec::new(); // Selecting 0-th percentile disables kicking. Upper bound check in fn build()
573573
}
574-
let underperformance_threshold_percent = Self::eviction_threshold();
575-
if underperformance_threshold_percent == Percent::one() {
574+
let underperformance_tolerated = Self::eviction_tolerance();
575+
if underperformance_tolerated == Percent::one() {
576576
return Vec::new(); // tolerating 100% underperformance disables kicking
577577
}
578578
let mut collator_perf_this_session =
@@ -590,32 +590,32 @@ pub mod pallet {
590590
let index_at_ordinal_rank = ordinal_rank.saturating_sub(1); // -1 to accomodate 0-index counting, should not saturate due to precondition check and round up multiplication
591591

592592
// 3. Block number at rank is the percentile and our kick performance benchmark
593-
let blocks_created_at_percentile: BlockCount =
593+
let blocks_created_at_baseline: BlockCount =
594594
collator_perf_this_session[index_at_ordinal_rank].1;
595595

596-
// 4. We kick if a collator produced fewer than (EvictionThreshold * EvictionPercentile rounded up) blocks than the percentile
597-
let threshold_factor = underperformance_threshold_percent.left_from_one(); // bounded to [0,1] due to checks on underperformance_threshold_percent
598-
let kick_threshold =
599-
(threshold_factor.mul_ceil(blocks_created_at_percentile)) as BlockCount;
596+
// 4. We kick if a collator produced fewer than (EvictionTolerance * EvictionBaseline rounded up) blocks than the percentile
597+
let evict_below_blocks = (underperformance_tolerated
598+
.left_from_one()
599+
.mul_ceil(blocks_created_at_baseline)) as BlockCount;
600600
log::trace!(
601601
"Session Performance stats: {}-th percentile: {:?} blocks. Evicting collators who produced less than {} blocks",
602602
percentile_for_kick.mul_ceil(100u8),
603-
blocks_created_at_percentile,
604-
kick_threshold
603+
blocks_created_at_baseline,
604+
evict_below_blocks
605605
);
606606

607607
// 5. Walk the percentile slice, call try_remove_candidate if a collator is under threshold
608608
let mut removed_account_ids: Vec<T::AccountId> = Vec::new();
609609
let kick_candidates = &collator_perf_this_session[..index_at_ordinal_rank]; // ordinal-rank exclusive, the collator at percentile is safe
610610
kick_candidates.iter().for_each(|(acc_id,my_blocks_this_session)| {
611-
if *my_blocks_this_session < kick_threshold {
611+
if *my_blocks_this_session < evict_below_blocks {
612612
// If our validator is not also a candidate we're invulnerable or already kicked
613613
if let Some(_) = candidates.iter().find(|&x|{x.who == *acc_id})
614614
{
615615
Self::try_remove_candidate(&acc_id)
616616
.and_then(|_| {
617617
removed_account_ids.push(acc_id.clone());
618-
log::info!("Removed collator of account {:?} as it only produced {} blocks this session which is below acceptable threshold of {}", &acc_id, my_blocks_this_session,kick_threshold);
618+
log::info!("Removed collator of account {:?} as it only produced {} blocks this session which is below acceptable threshold of {}", &acc_id, my_blocks_this_session,evict_below_blocks);
619619
Ok(())
620620
})
621621
.unwrap_or_else(|why| -> () {

pallets/collator-selection/src/mock.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
239239
let collator_selection = collator_selection::GenesisConfig::<Test> {
240240
desired_candidates: 2,
241241
candidacy_bond: 10,
242-
eviction_percentile: Percent::from_percent(80),
243-
eviction_threshold: Percent::from_percent(10),
242+
eviction_baseline: Percent::from_percent(80),
243+
eviction_tolerance: Percent::from_percent(10),
244244
invulnerables,
245245
};
246246
let session = pallet_session::GenesisConfig::<Test> { keys };

0 commit comments

Comments
 (0)