Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 7 additions & 31 deletions frame/offences/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ pub mod migration;
mod mock;
mod tests;

use codec::{Decode, Encode};
use core::marker::PhantomData;

use codec::Encode;
use frame_support::weights::Weight;
use sp_runtime::{traits::Hash, Perbill};
use sp_staking::{
Expand Down Expand Up @@ -85,21 +87,6 @@ pub mod pallet {
ValueQuery,
>;

/// Enumerates all reports of a kind along with the time they happened.
///
/// All reports are sorted by the time of offence.
///
/// Note that the actual type of this mapping is `Vec<u8>`, this is because values of
/// different types are not supported at the moment so we are doing the manual serialization.
#[pallet::storage]
pub type ReportsByKindIndex<T> = StorageMap<
_,
Twox64Concat,
Kind,
Vec<u8>, // (O::TimeSlot, ReportIdOf<T>)
ValueQuery,
>;

/// Events type.
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
Expand Down Expand Up @@ -190,7 +177,7 @@ impl<T: Config> Pallet<T> {
OffenceDetails { offender, reporters: reporters.clone() },
);

storage.insert(time_slot, report_id);
storage.insert(report_id);
}
}

Expand Down Expand Up @@ -225,38 +212,27 @@ struct TriageOutcome<T: Config> {
struct ReportIndexStorage<T: Config, O: Offence<T::IdentificationTuple>> {
opaque_time_slot: OpaqueTimeSlot,
concurrent_reports: Vec<ReportIdOf<T>>,
same_kind_reports: Vec<(O::TimeSlot, ReportIdOf<T>)>,
_offence: PhantomData<O>,
Comment thread
gupnik marked this conversation as resolved.
Outdated
}

impl<T: Config, O: Offence<T::IdentificationTuple>> ReportIndexStorage<T, O> {
/// Preload indexes from the storage for the specific `time_slot` and the kind of the offence.
fn load(time_slot: &O::TimeSlot) -> Self {
let opaque_time_slot = time_slot.encode();

let same_kind_reports = ReportsByKindIndex::<T>::get(&O::ID);
let same_kind_reports =
Vec::<(O::TimeSlot, ReportIdOf<T>)>::decode(&mut &same_kind_reports[..])
.unwrap_or_default();

let concurrent_reports = <ConcurrentReportsIndex<T>>::get(&O::ID, &opaque_time_slot);

Self { opaque_time_slot, concurrent_reports, same_kind_reports }
Self { opaque_time_slot, concurrent_reports, _offence: Default::default() }
}

/// Insert a new report to the index.
fn insert(&mut self, time_slot: &O::TimeSlot, report_id: ReportIdOf<T>) {
// Insert the report id into the list while maintaining the ordering by the time
// slot.
let pos = self.same_kind_reports.partition_point(|(when, _)| when <= time_slot);
self.same_kind_reports.insert(pos, (time_slot.clone(), report_id));

fn insert(&mut self, report_id: ReportIdOf<T>) {
// Update the list of concurrent reports.
self.concurrent_reports.push(report_id);
}

/// Dump the indexes to the storage.
fn save(self) {
ReportsByKindIndex::<T>::insert(&O::ID, self.same_kind_reports.encode());
<ConcurrentReportsIndex<T>>::insert(
&O::ID,
&self.opaque_time_slot,
Expand Down
5 changes: 0 additions & 5 deletions frame/offences/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,3 @@ impl offence::Offence<u64> for Offence {
Perbill::from_percent(5 + offenders_count * 100 / self.validator_set_count)
}
}

/// Create the report id for the given `offender` and `time_slot` combination.
pub fn report_id(time_slot: u128, offender: u64) -> H256 {
Offences::report_id::<Offence>(&time_slot, &offender)
}
49 changes: 2 additions & 47 deletions frame/offences/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

use super::*;
use crate::mock::{
new_test_ext, offence_reports, report_id, with_on_offence_fractions, Offence, Offences,
new_test_ext, offence_reports, with_on_offence_fractions, Offence, Offences,
RuntimeEvent, System, KIND,
};
use frame_system::{EventRecord, Phase};
Expand Down Expand Up @@ -244,49 +244,4 @@ 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)),
]
);
});
}
}