mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-28 08:37:56 +00:00
pallet-offences: Switch to partition_point (#9049)
This changes the code to use `partition_point` instead of `binary_search_by_key`, because this was very likely the problematic pallet 2 weeks ago on polkadot.
This commit is contained in:
@@ -22,7 +22,7 @@
|
||||
use super::*;
|
||||
use crate::mock::{
|
||||
Offences, System, Offence, Event, KIND, new_test_ext, with_on_offence_fractions,
|
||||
offence_reports,
|
||||
offence_reports, report_id,
|
||||
};
|
||||
use sp_runtime::Perbill;
|
||||
use frame_system::{EventRecord, Phase};
|
||||
@@ -284,3 +284,62 @@ fn should_properly_count_offences() {
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/// We insert offences in sorted order using the time slot in the `same_kind_reports`.
|
||||
/// This test ensures that it works as expected.
|
||||
#[test]
|
||||
fn should_properly_sort_offences() {
|
||||
new_test_ext().execute_with(|| {
|
||||
// given
|
||||
let time_slot = 42;
|
||||
assert_eq!(offence_reports(KIND, time_slot), vec![]);
|
||||
|
||||
let offence1 = Offence {
|
||||
validator_set_count: 5,
|
||||
time_slot,
|
||||
offenders: vec![5],
|
||||
};
|
||||
let offence2 = Offence {
|
||||
validator_set_count: 5,
|
||||
time_slot,
|
||||
offenders: vec![4],
|
||||
};
|
||||
let offence3 = Offence {
|
||||
validator_set_count: 5,
|
||||
time_slot: time_slot + 1,
|
||||
offenders: vec![6, 7],
|
||||
};
|
||||
let offence4 = Offence {
|
||||
validator_set_count: 5,
|
||||
time_slot: time_slot - 1,
|
||||
offenders: vec![3],
|
||||
};
|
||||
Offences::report_offence(vec![], offence1).unwrap();
|
||||
with_on_offence_fractions(|f| {
|
||||
assert_eq!(f.clone(), vec![Perbill::from_percent(25)]);
|
||||
f.clear();
|
||||
});
|
||||
|
||||
// when
|
||||
// report for the second time
|
||||
Offences::report_offence(vec![], offence2).unwrap();
|
||||
Offences::report_offence(vec![], offence3).unwrap();
|
||||
Offences::report_offence(vec![], offence4).unwrap();
|
||||
|
||||
// then
|
||||
let same_kind_reports =
|
||||
Vec::<(u128, sp_core::H256)>::decode(
|
||||
&mut &crate::ReportsByKindIndex::<crate::mock::Runtime>::get(KIND)[..],
|
||||
).unwrap();
|
||||
assert_eq!(
|
||||
same_kind_reports,
|
||||
vec![
|
||||
(time_slot - 1, report_id(time_slot - 1, 3)),
|
||||
(time_slot, report_id(time_slot, 5)),
|
||||
(time_slot, report_id(time_slot, 4)),
|
||||
(time_slot + 1, report_id(time_slot + 1, 6)),
|
||||
(time_slot + 1, report_id(time_slot + 1, 7)),
|
||||
]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user