Skip to content

Commit

Permalink
Add a couple of reporting tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HCastano committed Dec 12, 2024
1 parent 82aa757 commit c3ec986
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
38 changes: 38 additions & 0 deletions pallets/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ frame_support::construct_runtime!(
Historical: pallet_session_historical,
BagsList: pallet_bags_list,
Parameters: pallet_parameters,
Slashing: pallet_slashing,
}
);

Expand Down Expand Up @@ -418,6 +419,43 @@ impl entropy_shared::AttestationHandler<AccountId> for MockAttestationHandler {
fn request_quote(_attestee: &AccountId, _nonce: [u8; 32]) {}
}

type IdentificationTuple = (u64, pallet_staking::Exposure<AccountId, Balance>);
type Offence = pallet_slashing::UnresponsivenessOffence<IdentificationTuple>;

parameter_types! {
pub static Offences: Vec<Offence> = vec![];
}

/// A mock offence report handler.
pub struct OffenceHandler;
impl sp_staking::offence::ReportOffence<AccountId, IdentificationTuple, Offence>
for OffenceHandler
{
fn report_offence(
_reporters: Vec<u64>,
offence: Offence,
) -> Result<(), sp_staking::offence::OffenceError> {
Offences::mutate(|l| l.push(offence));
Ok(())
}

fn is_known_offence(_offenders: &[IdentificationTuple], _time_slot: &SessionIndex) -> bool {
false
}
}

parameter_types! {
pub const ReportThreshold: u32 = 5;
}

impl pallet_slashing::Config for Test {
type RuntimeEvent = RuntimeEvent;
type AuthorityId = UintAuthorityId;
type ReportThreshold = ReportThreshold;
type ValidatorSet = Historical;
type ReportUnresponsiveness = OffenceHandler;
}

impl pallet_staking_extension::Config for Test {
type AttestationHandler = MockAttestationHandler;
type Currency = Balances;
Expand Down
58 changes: 58 additions & 0 deletions pallets/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,3 +844,61 @@ fn it_stops_chill_when_signer_or_next_signer() {
);
});
}

#[test]
fn cannot_report_outside_of_signer_set() {
new_test_ext().execute_with(|| {
// These mappings come from the mock GenesisConfig
let (alice_validator, alice_tss) = (5, 7);
let (_bob_validator, bob_tss) = (6, 8);

let (_not_validator, not_tss) = (33, 33);

// We only want Alice to be part of the signing committee for the test.
Signers::<Test>::put(vec![alice_validator]);

// A TSS which doesn't have a `ValidatorId` cannot report another peer
assert_noop!(
Staking::report_unstable_peer(RuntimeOrigin::signed(not_tss), bob_tss),
Error::<Test>::NoThresholdKey
);

// A validator which isn't part of the signing committee cannot report another peer
assert_noop!(
Staking::report_unstable_peer(RuntimeOrigin::signed(bob_tss), alice_tss),
Error::<Test>::NotSigner
);

// An offender that does not have a `ValidatorId` cannot be reported
assert_noop!(
Staking::report_unstable_peer(RuntimeOrigin::signed(alice_tss), not_tss),
Error::<Test>::NoThresholdKey
);

// An offender which isn't part of the signing committee cannot be reported
assert_noop!(
Staking::report_unstable_peer(RuntimeOrigin::signed(alice_tss), bob_tss),
Error::<Test>::NotSigner
);
})
}

#[test]
fn can_report_unstable_peer() {
new_test_ext().execute_with(|| {
// These mappings come from the mock GenesisConfig
let (alice_validator, alice_tss) = (5, 7);
let (bob_validator, bob_tss) = (6, 8);

Signers::<Test>::put(vec![alice_validator, bob_validator]);

// The TSS accounts are used for reports. We expect the accompanying validator to be
// reported though.
assert_ok!(Staking::report_unstable_peer(
RuntimeOrigin::signed(alice_tss),
bob_tss
));

assert_eq!(<pallet_slashing::Pallet<Test>>::failed_registrations(bob_validator), 1);
})
}

0 comments on commit c3ec986

Please sign in to comment.