Skip to content

Commit b009cc1

Browse files
committed
Add offence handler test
1 parent 53e0578 commit b009cc1

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

pallets/validator-set/src/mock.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,8 @@ impl pallet_session::Config for Test {
181181
type WeightInfo = ();
182182
type RuntimeEvent = RuntimeEvent;
183183
}
184+
185+
impl pallet_session::historical::Config for Test {
186+
type FullIdentification = Self::ValidatorId;
187+
type FullIdentificationOf = Self::ValidatorIdOf;
188+
}

pallets/validator-set/src/tests.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ use super::mock::{
2222
active_validators, new_test_ext, next_session, AccountId, RuntimeOrigin, Session, System, Test,
2323
ValidatorSet,
2424
};
25-
use frame_support::{assert_noop, assert_ok, traits::ValidatorRegistration};
26-
use sp_runtime::{traits::Zero, DispatchError};
25+
use frame_support::{
26+
assert_noop, assert_ok,
27+
traits::{DisabledValidators, ValidatorRegistration},
28+
};
29+
use sp_runtime::{traits::Zero, DispatchError, Perbill};
30+
use sp_staking::offence::{DisableStrategy, OffenceDetails, OnOffenceHandler};
2731
use std::collections::HashSet;
2832

2933
type Error = super::Error<Test>;
@@ -132,3 +136,31 @@ fn remove_purges_keys_and_decs_providers() {
132136
assert!(System::providers(&3).is_zero());
133137
});
134138
}
139+
140+
#[test]
141+
fn offender_disabled_and_removed() {
142+
new_test_ext().execute_with(|| {
143+
assert_eq!(validators(), HashSet::from([1, 2, 3]));
144+
ValidatorSet::on_offence(
145+
&[OffenceDetails { offender: (3, 3), reporters: vec![] }],
146+
&[Perbill::from_rational(1u32, 2u32)],
147+
0,
148+
DisableStrategy::WhenSlashed,
149+
);
150+
assert_eq!(validators(), HashSet::from([1, 2]));
151+
152+
// The offender should be disabled for the rest of this session and the next session. The
153+
// removal should take effect by the session after next.
154+
assert_eq!(active_validators(), HashSet::from([1, 2, 3]));
155+
assert!(Session::is_disabled(
156+
Session::validators().iter().position(|who| *who == 3).unwrap() as u32
157+
));
158+
next_session();
159+
assert_eq!(active_validators(), HashSet::from([1, 2, 3]));
160+
assert!(Session::is_disabled(
161+
Session::validators().iter().position(|who| *who == 3).unwrap() as u32
162+
));
163+
next_session();
164+
assert_eq!(active_validators(), HashSet::from([1, 2]));
165+
});
166+
}

0 commit comments

Comments
 (0)