Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

babe: make plan_config_change callable by root #8233

Merged
merged 1 commit into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions frame/babe/src/default_weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ use frame_support::weights::{
};

impl crate::WeightInfo for () {
fn plan_config_change() -> Weight {
DbWeight::get().writes(1)
}

fn report_equivocation(validator_count: u32) -> Weight {
// we take the validator set count from the membership proof to
// calculate the weight but we set a floor of 100 validators.
Expand Down
25 changes: 15 additions & 10 deletions frame/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use frame_support::{
weights::{Pays, Weight},
Parameter,
};
use frame_system::{ensure_none, ensure_signed};
use frame_system::{ensure_none, ensure_root, ensure_signed};
use sp_application_crypto::Public;
use sp_runtime::{
generic::DigestItem,
Expand Down Expand Up @@ -108,6 +108,7 @@ pub trait Config: pallet_timestamp::Config {
}

pub trait WeightInfo {
fn plan_config_change() -> Weight;
fn report_equivocation(validator_count: u32) -> Weight;
}

Expand Down Expand Up @@ -314,6 +315,19 @@ decl_module! {
key_owner_proof,
)
}

/// Plan an epoch config change. The epoch config change is recorded and will be enacted on
/// the next call to `enact_epoch_change`. The config will be activated one epoch after.
/// Multiple calls to this method will replace any existing planned config change that had
/// not been enacted yet.
#[weight = <T as Config>::WeightInfo::plan_config_change()]
fn plan_config_change(
origin,
config: NextConfigDescriptor,
) {
ensure_root(origin)?;
NextEpochConfig::put(config);
}
}
}

Expand Down Expand Up @@ -432,15 +446,6 @@ impl<T: Config> Module<T> {
})
}

/// Plan an epoch config change. The epoch config change is recorded and will be enacted on the
/// next call to `enact_epoch_change`. The config will be activated one epoch after. Multiple calls to this
/// method will replace any existing planned config change that had not been enacted yet.
pub fn plan_config_change(
config: NextConfigDescriptor,
) {
NextEpochConfig::put(config);
}

/// DANGEROUS: Enact an epoch change. Should be done on every block where `should_epoch_change` has returned `true`,
/// and the caller is the only caller of this function.
///
Expand Down
44 changes: 40 additions & 4 deletions frame/babe/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,13 @@ fn can_enact_next_config() {
assert_eq!(Babe::epoch_index(), 0);
go_to_block(2, 7);

Babe::plan_config_change(NextConfigDescriptor::V1 {
c: (1, 4),
allowed_slots: AllowedSlots::PrimarySlots,
});
Babe::plan_config_change(
Origin::root(),
NextConfigDescriptor::V1 {
c: (1, 4),
allowed_slots: AllowedSlots::PrimarySlots,
},
).unwrap();

progress_to_block(4);
Babe::on_finalize(9);
Expand All @@ -252,6 +255,39 @@ fn can_enact_next_config() {
});
}

#[test]
fn only_root_can_enact_config_change() {
use sp_runtime::DispatchError;

new_test_ext(1).execute_with(|| {
let next_config = NextConfigDescriptor::V1 {
c: (1, 4),
allowed_slots: AllowedSlots::PrimarySlots,
};

let res = Babe::plan_config_change(
Origin::none(),
next_config.clone(),
);

assert_eq!(res, Err(DispatchError::BadOrigin));

let res = Babe::plan_config_change(
Origin::signed(1),
next_config.clone(),
);

assert_eq!(res, Err(DispatchError::BadOrigin));

let res = Babe::plan_config_change(
Origin::root(),
next_config,
);

assert!(res.is_ok());
});
}

#[test]
fn can_fetch_current_and_next_epoch_data() {
new_test_ext(5).execute_with(|| {
Expand Down