Skip to content

Commit

Permalink
frame/beefy: add privileged call to reset BEEFY consensus (paritytech…
Browse files Browse the repository at this point in the history
…#1534)

We want to be able to (re)set BEEFY genesis in order to (re)start BEEFY
consensus on chains which didn't run it since genesis.

This commit adds privileged helper call to (re)set BEEFY genesis to some
block in the future.

Signed-off-by: Adrian Catangiu <adrian@parity.io>
  • Loading branch information
acatangiu committed Sep 14, 2023
1 parent ef41ee9 commit 2742cba
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
4 changes: 4 additions & 0 deletions substrate/frame/beefy/src/default_weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ impl crate::WeightInfo for () {
// fetching set id -> session index mappings
.saturating_add(DbWeight::get().reads(2))
}

fn set_new_genesis() -> Weight {
DbWeight::get().writes(1)
}
}
30 changes: 25 additions & 5 deletions substrate/frame/beefy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use frame_system::{
use log;
use sp_runtime::{
generic::DigestItem,
traits::{IsMember, Member},
traits::{IsMember, Member, One},
RuntimeAppPublic,
};
use sp_session::{GetSessionNumber, GetValidatorCount};
Expand Down Expand Up @@ -62,7 +62,7 @@ const LOG_TARGET: &str = "runtime::beefy";
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_system::pallet_prelude::BlockNumberFor;
use frame_system::{ensure_root, pallet_prelude::BlockNumberFor};

#[pallet::config]
pub trait Config: frame_system::Config {
Expand Down Expand Up @@ -152,8 +152,8 @@ pub mod pallet {
StorageMap<_, Twox64Concat, sp_consensus_beefy::ValidatorSetId, SessionIndex>;

/// Block number where BEEFY consensus is enabled/started.
/// By changing this (through governance or sudo), BEEFY consensus is effectively
/// restarted from the new block number.
/// By changing this (through privileged `set_new_genesis()`), BEEFY consensus is effectively
/// restarted from the newly set block number.
#[pallet::storage]
#[pallet::getter(fn genesis_block)]
pub(super) type GenesisBlock<T: Config> =
Expand All @@ -174,7 +174,7 @@ pub mod pallet {
fn default() -> Self {
// BEEFY genesis will be first BEEFY-MANDATORY block,
// use block number one instead of chain-genesis.
let genesis_block = Some(sp_runtime::traits::One::one());
let genesis_block = Some(One::one());
Self { authorities: Vec::new(), genesis_block }
}
}
Expand All @@ -198,6 +198,8 @@ pub mod pallet {
InvalidEquivocationProof,
/// A given equivocation report is valid but already previously reported.
DuplicateOffenceReport,
/// Submitted configuration is invalid.
InvalidConfiguration,
}

#[pallet::call]
Expand Down Expand Up @@ -265,6 +267,23 @@ pub mod pallet {
)?;
Ok(Pays::No.into())
}

/// Reset BEEFY consensus by setting a new BEEFY genesis at `delay_in_blocks` blocks in the
/// future.
///
/// Note: `delay_in_blocks` has to be at least 1.
#[pallet::call_index(2)]
#[pallet::weight(<T as Config>::WeightInfo::set_new_genesis())]
pub fn set_new_genesis(
origin: OriginFor<T>,
delay_in_blocks: BlockNumberFor<T>,
) -> DispatchResult {
ensure_root(origin)?;
ensure!(delay_in_blocks >= One::one(), Error::<T>::InvalidConfiguration);
let genesis_block = frame_system::Pallet::<T>::block_number() + delay_in_blocks;
GenesisBlock::<T>::put(Some(genesis_block));
Ok(())
}
}

#[pallet::validate_unsigned]
Expand Down Expand Up @@ -452,4 +471,5 @@ impl<T: Config> IsMember<T::BeefyId> for Pallet<T> {

pub trait WeightInfo {
fn report_equivocation(validator_count: u32, max_nominators_per_validator: u32) -> Weight;
fn set_new_genesis() -> Weight;
}
22 changes: 22 additions & 0 deletions substrate/frame/beefy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -791,3 +791,25 @@ fn valid_equivocation_reports_dont_pay_fees() {
assert_eq!(post_info.pays_fee, Pays::Yes);
})
}

#[test]
fn set_new_genesis_works() {
let authorities = test_authorities();

new_test_ext_raw_authorities(authorities).execute_with(|| {
start_era(1);

let new_genesis_delay = 10u64;
// the call for setting new genesis should work
assert_ok!(Beefy::set_new_genesis(RuntimeOrigin::root(), new_genesis_delay,));
let expected = System::block_number() + new_genesis_delay;
// verify new genesis was set
assert_eq!(Beefy::genesis_block(), Some(expected));

// setting delay < 1 should fail
assert_err!(
Beefy::set_new_genesis(RuntimeOrigin::root(), 0u64,),
Error::<Test>::InvalidConfiguration,
);
});
}

0 comments on commit 2742cba

Please sign in to comment.