aura, babe: don't allow disabled validators to build blocks (#9414)

* frame-support: add trait for checking disabled validators

* pallet-session: implement DisabledValidators trait

* pallet-babe: check for disabled validators

* pallet-babe: add test for disabled validators

* pallet-aura: check for disabled validators

* pallet-aura: add test for disabled validators

* runtime: fix missing DisableValidator

* test-runtime: add missing DisabledValidators

* frame-support: clean up doc

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
André Silva
2021-08-03 11:57:08 +01:00
committed by GitHub
parent c9ffce0f9c
commit 0240efde58
12 changed files with 142 additions and 11 deletions
+16 -1
View File
@@ -39,7 +39,7 @@
use codec::{Decode, Encode};
use frame_support::{
traits::{FindAuthor, Get, OnTimestampSet, OneSessionHandler},
traits::{DisabledValidators, FindAuthor, Get, OnTimestampSet, OneSessionHandler},
ConsensusEngineId, Parameter,
};
use sp_consensus_aura::{AuthorityIndex, ConsensusLog, Slot, AURA_ENGINE_ID};
@@ -70,6 +70,11 @@ pub mod pallet {
+ RuntimeAppPublic
+ Default
+ MaybeSerializeDeserialize;
/// A way to check whether a given validator is disabled and should not be authoring blocks.
/// Blocks authored by a disabled validator will lead to a panic as part of this module's
/// initialization.
type DisabledValidators: DisabledValidators;
}
#[pallet::pallet]
@@ -84,6 +89,16 @@ pub mod pallet {
assert!(current_slot < new_slot, "Slot must increase");
CurrentSlot::<T>::put(new_slot);
if let Some(n_authorities) = <Authorities<T>>::decode_len() {
let authority_index = *new_slot % n_authorities as u64;
if T::DisabledValidators::is_disabled(authority_index as u32) {
panic!(
"Validator with index {:?} is disabled and should not be attempting to author blocks.",
authority_index,
);
}
}
// TODO [#3398] Generate offence report for all authorities that skipped their slots.
T::DbWeight::get().reads_writes(2, 1)
+30 -2
View File
@@ -20,13 +20,17 @@
#![cfg(test)]
use crate as pallet_aura;
use frame_support::{parameter_types, traits::GenesisBuild};
use sp_consensus_aura::ed25519::AuthorityId;
use frame_support::{
parameter_types,
traits::{DisabledValidators, GenesisBuild},
};
use sp_consensus_aura::{ed25519::AuthorityId, AuthorityIndex};
use sp_core::H256;
use sp_runtime::{
testing::{Header, UintAuthorityId},
traits::IdentityLookup,
};
use sp_std::cell::RefCell;
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
@@ -83,8 +87,32 @@ impl pallet_timestamp::Config for Test {
type WeightInfo = ();
}
thread_local! {
static DISABLED_VALIDATORS: RefCell<Vec<AuthorityIndex>> = RefCell::new(Default::default());
}
pub struct MockDisabledValidators;
impl MockDisabledValidators {
pub fn disable_validator(index: AuthorityIndex) {
DISABLED_VALIDATORS.with(|v| {
let mut disabled = v.borrow_mut();
if let Err(i) = disabled.binary_search(&index) {
disabled.insert(i, index);
}
})
}
}
impl DisabledValidators for MockDisabledValidators {
fn is_disabled(index: AuthorityIndex) -> bool {
DISABLED_VALIDATORS.with(|v| v.borrow().binary_search(&index).is_ok())
}
}
impl pallet_aura::Config for Test {
type AuthorityId = AuthorityId;
type DisabledValidators = MockDisabledValidators;
}
pub fn new_test_ext(authorities: Vec<u64>) -> sp_io::TestExternalities {
+27 -1
View File
@@ -19,7 +19,12 @@
#![cfg(test)]
use crate::mock::{new_test_ext, Aura};
use crate::mock::{new_test_ext, Aura, MockDisabledValidators, System};
use codec::Encode;
use frame_support::traits::OnInitialize;
use frame_system::InitKind;
use sp_consensus_aura::{Slot, AURA_ENGINE_ID};
use sp_runtime::{Digest, DigestItem};
#[test]
fn initial_values() {
@@ -28,3 +33,24 @@ fn initial_values() {
assert_eq!(Aura::authorities().len(), 4);
});
}
#[test]
#[should_panic(
expected = "Validator with index 1 is disabled and should not be attempting to author blocks."
)]
fn disabled_validators_cannot_author_blocks() {
new_test_ext(vec![0, 1, 2, 3]).execute_with(|| {
// slot 1 should be authored by validator at index 1
let slot = Slot::from(1);
let pre_digest =
Digest { logs: vec![DigestItem::PreRuntime(AURA_ENGINE_ID, slot.encode())] };
System::initialize(&42, &System::parent_hash(), &pre_digest, InitKind::Full);
// let's disable the validator
MockDisabledValidators::disable_validator(1);
// and we should not be able to initialize the block
Aura::on_initialize(42);
});
}