Skip to content

Commit 2ce4c8e

Browse files
authored
Deauthenticate offline validators (#354)
* Expose helper functions API from the bioauth pallet * Shash the bioauth when node is detected to be offline
1 parent 0cd8fc5 commit 2ce4c8e

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/humanode-runtime/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ sp-inherents = { default-features = false, git = "https://github.com/humanode-ne
6464
sp-offchain = { default-features = false, git = "https://github.com/humanode-network/substrate", branch = "master" }
6565
sp-runtime = { default-features = false, git = "https://github.com/humanode-network/substrate", branch = "master" }
6666
sp-session = { default-features = false, git = "https://github.com/humanode-network/substrate", branch = "master" }
67+
sp-staking = { default-features = false, git = "https://github.com/humanode-network/substrate", branch = "master" }
6768
sp-std = { default-features = false, git = "https://github.com/humanode-network/substrate", branch = "master" }
6869
sp-transaction-pool = { default-features = false, git = "https://github.com/humanode-network/substrate", branch = "master" }
6970
sp-version = { default-features = false, git = "https://github.com/humanode-network/substrate", branch = "master" }
@@ -125,6 +126,7 @@ std = [
125126
"sp-offchain/std",
126127
"sp-runtime/std",
127128
"sp-session/std",
129+
"sp-staking/std",
128130
"sp-std/std",
129131
"sp-transaction-pool/std",
130132
"sp-version/std",

crates/humanode-runtime/src/lib.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,12 +584,45 @@ parameter_types! {
584584
pub const MaxPeerDataEncodingSize: u32 = MAX_PEER_DATA_ENCODING_SIZE;
585585
}
586586

587+
pub struct ImOnlineSlasher;
588+
589+
impl
590+
sp_staking::offence::ReportOffence<
591+
AccountId,
592+
pallet_im_online::IdentificationTuple<Runtime>,
593+
pallet_im_online::UnresponsivenessOffence<pallet_im_online::IdentificationTuple<Runtime>>,
594+
> for ImOnlineSlasher
595+
{
596+
fn report_offence(
597+
_reporters: Vec<AccountId>,
598+
offence: pallet_im_online::UnresponsivenessOffence<
599+
pallet_im_online::IdentificationTuple<Runtime>,
600+
>,
601+
) -> Result<(), sp_staking::offence::OffenceError> {
602+
for offender in offence.offenders {
603+
Bioauth::deauthenticate(&offender.0);
604+
}
605+
Ok(())
606+
}
607+
608+
fn is_known_offence(
609+
_offenders: &[pallet_im_online::IdentificationTuple<Runtime>],
610+
_time_slot: &<pallet_im_online::UnresponsivenessOffence<
611+
pallet_im_online::IdentificationTuple<Runtime>,
612+
> as sp_staking::offence::Offence<
613+
pallet_im_online::IdentificationTuple<Runtime>,
614+
>>::TimeSlot,
615+
) -> bool {
616+
unreachable!("ImOnline will never call `is_known_offence`")
617+
}
618+
}
619+
587620
impl pallet_im_online::Config for Runtime {
588621
type AuthorityId = ImOnlineId;
589622
type Event = Event;
590623
type NextSessionRotation = Babe;
591624
type ValidatorSet = Historical;
592-
type ReportUnresponsiveness = ();
625+
type ReportUnresponsiveness = ImOnlineSlasher;
593626
type UnsignedPriority = ImOnlineUnsignedPriority;
594627
type WeightInfo = pallet_im_online::weights::SubstrateWeight<Runtime>;
595628
type MaxKeys = MaxKeys;

crates/pallet-bioauth/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,29 @@ pub mod pallet {
396396
Ok(())
397397
}
398398

399+
/// Public API the pallet exposes to the runtime.
400+
impl<T: Config> Pallet<T> {
401+
pub fn is_authenticated(public_key: &<T as Config>::ValidatorPublicKey) -> bool {
402+
ActiveAuthentications::<T>::get()
403+
.iter()
404+
.any(|authentication| &authentication.public_key == public_key)
405+
}
406+
407+
pub fn deauthenticate(public_key: &<T as Config>::ValidatorPublicKey) -> bool {
408+
ActiveAuthentications::<T>::mutate(|active_authentications| {
409+
let mut removed = false;
410+
active_authentications.retain(|authentication| {
411+
if &authentication.public_key == public_key {
412+
removed = true;
413+
return false;
414+
}
415+
true
416+
});
417+
removed
418+
})
419+
}
420+
}
421+
399422
/// Dispatchable functions allow users to interact with the pallet and invoke state changes.
400423
/// These functions materialize as "extrinsics", which are often compared to transactions.
401424
/// Dispatchable functions must be annotated with a weight and must return

0 commit comments

Comments
 (0)