#![cfg(test)]
use super::*;
use crate::mock::{
new_test_ext, offence_reports, report_id, with_on_offence_fractions, Offence, Offences,
RuntimeEvent, System, KIND,
};
use frame_system::{EventRecord, Phase};
use sp_runtime::Perbill;
#[test]
fn should_report_an_authority_and_trigger_on_offence() {
new_test_ext().execute_with(|| {
let time_slot = 42;
assert_eq!(offence_reports(KIND, time_slot), vec![]);
let offence = Offence { validator_set_count: 5, time_slot, offenders: vec![5] };
Offences::report_offence(vec![], offence).unwrap();
with_on_offence_fractions(|f| {
assert_eq!(f.clone(), vec![Perbill::from_percent(25)]);
});
});
}
#[test]
fn should_not_report_the_same_authority_twice_in_the_same_slot() {
new_test_ext().execute_with(|| {
let time_slot = 42;
assert_eq!(offence_reports(KIND, time_slot), vec![]);
let offence = Offence { validator_set_count: 5, time_slot, offenders: vec![5] };
Offences::report_offence(vec![], offence.clone()).unwrap();
with_on_offence_fractions(|f| {
assert_eq!(f.clone(), vec![Perbill::from_percent(25)]);
f.clear();
});
assert_eq!(Offences::report_offence(vec![], offence), Err(OffenceError::DuplicateReport));
with_on_offence_fractions(|f| {
assert_eq!(f.clone(), vec![]);
});
});
}
#[test]
fn should_report_in_different_time_slot() {
new_test_ext().execute_with(|| {
let time_slot = 42;
assert_eq!(offence_reports(KIND, time_slot), vec![]);
let mut offence = Offence { validator_set_count: 5, time_slot, offenders: vec![5] };
Offences::report_offence(vec![], offence.clone()).unwrap();
with_on_offence_fractions(|f| {
assert_eq!(f.clone(), vec![Perbill::from_percent(25)]);
f.clear();
});
offence.time_slot += 1;
Offences::report_offence(vec![], offence).unwrap();
with_on_offence_fractions(|f| {
assert_eq!(f.clone(), vec![Perbill::from_percent(25)]);
});
});
}
#[test]
fn should_deposit_event() {
new_test_ext().execute_with(|| {
let time_slot = 42;
assert_eq!(offence_reports(KIND, time_slot), vec![]);
let offence = Offence { validator_set_count: 5, time_slot, offenders: vec![5] };
Offences::report_offence(vec![], offence).unwrap();
assert_eq!(
System::events(),
vec![EventRecord {
phase: Phase::Initialization,
event: RuntimeEvent::Offences(crate::Event::Offence {
kind: KIND,
timeslot: time_slot.encode()
}),
topics: vec![],
}]
);
});
}
#[test]
fn doesnt_deposit_event_for_dups() {
new_test_ext().execute_with(|| {
let time_slot = 42;
assert_eq!(offence_reports(KIND, time_slot), vec![]);
let offence = Offence { validator_set_count: 5, time_slot, offenders: vec![5] };
Offences::report_offence(vec![], offence.clone()).unwrap();
with_on_offence_fractions(|f| {
assert_eq!(f.clone(), vec![Perbill::from_percent(25)]);
f.clear();
});
assert_eq!(Offences::report_offence(vec![], offence), Err(OffenceError::DuplicateReport));
assert_eq!(
System::events(),
vec![EventRecord {
phase: Phase::Initialization,
event: RuntimeEvent::Offences(crate::Event::Offence {
kind: KIND,
timeslot: time_slot.encode()
}),
topics: vec![],
}]
);
});
}
#[test]
fn reports_if_an_offence_is_dup() {
type TestOffence = Offence<u64>;
new_test_ext().execute_with(|| {
let time_slot = 42;
assert_eq!(offence_reports(KIND, time_slot), vec![]);
let offence =
|time_slot, offenders| TestOffence { validator_set_count: 5, time_slot, offenders };
let mut test_offence = offence(time_slot, vec![0]);
assert!(!<Offences as ReportOffence<_, _, TestOffence>>::is_known_offence(
&test_offence.offenders,
&test_offence.time_slot
));
Offences::report_offence(vec![], test_offence.clone()).unwrap();
assert!(<Offences as ReportOffence<_, _, TestOffence>>::is_known_offence(
&test_offence.offenders,
&test_offence.time_slot
));
assert_eq!(
Offences::report_offence(vec![], test_offence.clone()),
Err(OffenceError::DuplicateReport)
);
test_offence.offenders.push(1);
assert!(!<Offences as ReportOffence<_, _, TestOffence>>::is_known_offence(
&test_offence.offenders,
&test_offence.time_slot
));
assert_eq!(Offences::report_offence(vec![], test_offence.clone()), Ok(()));
let test_offence_next_slot = offence(time_slot + 1, vec![0, 1]);
assert!(!<Offences as ReportOffence<_, _, TestOffence>>::is_known_offence(
&test_offence_next_slot.offenders,
&test_offence_next_slot.time_slot
));
});
}
#[test]
fn should_properly_count_offences() {
new_test_ext().execute_with(|| {
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] };
Offences::report_offence(vec![], offence1).unwrap();
with_on_offence_fractions(|f| {
assert_eq!(f.clone(), vec![Perbill::from_percent(25)]);
f.clear();
});
Offences::report_offence(vec![], offence2).unwrap();
assert_eq!(
offence_reports(KIND, time_slot),
vec![
OffenceDetails { offender: 5, reporters: vec![] },
OffenceDetails { offender: 4, reporters: vec![] },
]
);
});
}
#[test]
fn should_properly_sort_offences() {
new_test_ext().execute_with(|| {
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();
});
Offences::report_offence(vec![], offence2).unwrap();
Offences::report_offence(vec![], offence3).unwrap();
Offences::report_offence(vec![], offence4).unwrap();
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)),
]
);
});
}