session: add handler for genesis session (#3413)

* session: add handler for genesis session

* node: bump spec version

* aura: handle on_genesis_session

* srml: make sure we don't re-initialize genesis authorities

* session: fix mock

* node: remove genesis authorities from chain spec

* staking: fix mock

* srml: don't initialize genesis authorities twice

* aura: don't reinitialize genesis authorities

* aura: fix runtime_io dependency

* Bump runtime
This commit is contained in:
André Silva
2019-08-16 10:24:06 +02:00
committed by Svyatoslav Nikolsky
parent 3590c9c33f
commit d1dde7e087
12 changed files with 146 additions and 14 deletions
+27 -1
View File
@@ -123,7 +123,7 @@ decl_storage! {
pub EpochIndex get(epoch_index): u64;
/// Current epoch authorities.
pub Authorities get(authorities) config(): Vec<(AuthorityId, BabeWeight)>;
pub Authorities get(authorities): Vec<(AuthorityId, BabeWeight)>;
/// Slot at which the current epoch started. It is possible that no
/// block was authored at the given slot and the epoch change was
@@ -163,6 +163,18 @@ decl_storage! {
SegmentIndex build(|_| 0): u32;
UnderConstruction: map u32 => Vec<[u8; 32 /* VRF_OUTPUT_LENGTH */]>;
}
add_extra_genesis {
config(authorities): Vec<(AuthorityId, BabeWeight)>;
build(|
storage: &mut (sr_primitives::StorageOverlay, sr_primitives::ChildrenStorageOverlay),
config: &GenesisConfig
| {
runtime_io::with_storage(
storage,
|| Module::<T>::initialize_authorities(&config.authorities),
);
})
}
}
decl_module! {
@@ -292,6 +304,12 @@ impl<T: Trait> Module<T> {
this_randomness
}
fn initialize_authorities(authorities: &[(AuthorityId, BabeWeight)]) {
if !authorities.is_empty() {
assert!(Authorities::get().is_empty(), "Authorities are already initialized!");
Authorities::put_ref(authorities);
}
}
}
impl<T: Trait> OnTimestampSet<T::Moment> for Module<T> {
@@ -300,6 +318,14 @@ impl<T: Trait> OnTimestampSet<T::Moment> for Module<T> {
impl<T: Trait> session::OneSessionHandler<T::AccountId> for Module<T> {
type Key = AuthorityId;
fn on_genesis_session<'a, I: 'a>(validators: I)
where I: Iterator<Item=(&'a T::AccountId, AuthorityId)>
{
let authorities = validators.map(|(_, k)| (k, 1)).collect::<Vec<_>>();
Self::initialize_authorities(&authorities);
}
fn on_new_session<'a, I: 'a>(_changed: bool, validators: I, queued_validators: I)
where I: Iterator<Item=(&'a T::AccountId, AuthorityId)>
{