Return babe configuration information in the babe api epoch functions (#8072)

* Make changes

* Add serialize/deserialize, copy babe epoch config defaults from node runtime

* Fix line widths and turn default features off for serde

* Remove ser/deser from Epoch, fix node-cli

* Apply suggestions

* Add comment to BABE_GENESIS_EPOCH_CONFIG in bin

* Apply suggestions

* Add a sketchy migration function

* Add a migration test

* Check for PendingEpochConfigChange as well

* Make epoch_config in node-cli

* Move updating EpochConfig out of the if

* Fix executor tests

* Calculate weight for add_epoch_configurations

* Fix babe test

* Apply suggestions from code review

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

* Add more asserts to tests, remove unused changes to primitives/slots

* Allow setting the migration pallet prefix

* Rename to BabePalletPrefix

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>
This commit is contained in:
Ashley
2021-03-10 09:40:22 +01:00
committed by GitHub
parent c33235ac5b
commit 7ace5e21aa
9 changed files with 187 additions and 17 deletions
+85 -6
View File
@@ -25,7 +25,7 @@ use frame_support::{
};
use mock::*;
use pallet_session::ShouldEndSession;
use sp_consensus_babe::{AllowedSlots, Slot};
use sp_consensus_babe::{AllowedSlots, Slot, BabeEpochConfiguration};
use sp_core::crypto::Pair;
const EMPTY_RANDOMNESS: [u8; 32] = [
@@ -231,11 +231,31 @@ fn can_enact_next_config() {
assert_eq!(Babe::epoch_index(), 0);
go_to_block(2, 7);
let current_config = BabeEpochConfiguration {
c: (0, 4),
allowed_slots: sp_consensus_babe::AllowedSlots::PrimarySlots,
};
let next_config = BabeEpochConfiguration {
c: (1, 4),
allowed_slots: sp_consensus_babe::AllowedSlots::PrimarySlots,
};
let next_next_config = BabeEpochConfiguration {
c: (2, 4),
allowed_slots: sp_consensus_babe::AllowedSlots::PrimarySlots,
};
EpochConfig::put(current_config);
NextEpochConfig::put(next_config.clone());
assert_eq!(NextEpochConfig::get(), Some(next_config.clone()));
Babe::plan_config_change(
Origin::root(),
NextConfigDescriptor::V1 {
c: (1, 4),
allowed_slots: AllowedSlots::PrimarySlots,
c: next_next_config.c,
allowed_slots: next_next_config.allowed_slots,
},
).unwrap();
@@ -243,10 +263,13 @@ fn can_enact_next_config() {
Babe::on_finalize(9);
let header = System::finalize();
assert_eq!(EpochConfig::get(), Some(next_config));
assert_eq!(NextEpochConfig::get(), Some(next_next_config.clone()));
let consensus_log = sp_consensus_babe::ConsensusLog::NextConfigData(
sp_consensus_babe::digests::NextConfigDescriptor::V1 {
c: (1, 4),
allowed_slots: AllowedSlots::PrimarySlots,
NextConfigDescriptor::V1 {
c: next_next_config.c,
allowed_slots: next_next_config.allowed_slots,
}
);
let consensus_digest = DigestItem::Consensus(BABE_ENGINE_ID, consensus_log.encode());
@@ -291,6 +314,11 @@ fn only_root_can_enact_config_change() {
#[test]
fn can_fetch_current_and_next_epoch_data() {
new_test_ext(5).execute_with(|| {
EpochConfig::put(BabeEpochConfiguration {
c: (1, 4),
allowed_slots: sp_consensus_babe::AllowedSlots::PrimarySlots,
});
// genesis authorities should be used for the first and second epoch
assert_eq!(
Babe::current_epoch().authorities,
@@ -809,3 +837,54 @@ fn valid_equivocation_reports_dont_pay_fees() {
assert_eq!(post_info.pays_fee, Pays::Yes);
})
}
#[test]
fn add_epoch_configurations_migration_works() {
use frame_support::storage::migration::{
put_storage_value, get_storage_value,
};
impl crate::migrations::BabePalletPrefix for Test {
fn pallet_prefix() -> &'static str {
"Babe"
}
}
new_test_ext(1).execute_with(|| {
let next_config_descriptor = NextConfigDescriptor::V1 {
c: (3, 4),
allowed_slots: AllowedSlots::PrimarySlots
};
put_storage_value(
b"Babe",
b"NextEpochConfig",
&[],
Some(next_config_descriptor.clone())
);
assert!(get_storage_value::<Option<NextConfigDescriptor>>(
b"Babe",
b"NextEpochConfig",
&[],
).is_some());
let current_epoch = BabeEpochConfiguration {
c: (1, 4),
allowed_slots: sp_consensus_babe::AllowedSlots::PrimarySlots,
};
crate::migrations::add_epoch_configuration::<Test>(
current_epoch.clone()
);
assert!(get_storage_value::<Option<NextConfigDescriptor>>(
b"Babe",
b"NextEpochConfig",
&[],
).is_none());
assert_eq!(EpochConfig::get(), Some(current_epoch));
assert_eq!(PendingEpochConfigChange::get(), Some(next_config_descriptor));
});
}