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
+1
View File
@@ -12,6 +12,7 @@ rstd = { package = "sr-std", path = "../../core/sr-std", default-features = fals
sr-primitives = { path = "../../core/sr-primitives", default-features = false }
primitives = { package = "substrate-primitives", path = "../../core/primitives", default-features = false }
app-crypto = { package = "substrate-application-crypto", path = "../../core/application-crypto", default-features = false }
runtime_io = { package = "sr-io", path = "../../core/sr-io", default-features = false, features = [ "wasm-nice-panic-message" ] }
srml-support = { path = "../support", default-features = false }
system = { package = "srml-system", path = "../system", default-features = false }
timestamp = { package = "srml-timestamp", path = "../timestamp", default-features = false }
+28 -1
View File
@@ -165,7 +165,19 @@ decl_storage! {
LastTimestamp get(last) build(|_| 0.into()): T::Moment;
/// The current authorities
pub Authorities get(authorities) config(): Vec<T::AuthorityId>;
pub Authorities get(authorities): Vec<T::AuthorityId>;
}
add_extra_genesis {
config(authorities): Vec<T::AuthorityId>;
build(|
storage: &mut (sr_primitives::StorageOverlay, sr_primitives::ChildrenStorageOverlay),
config: &GenesisConfig<T>
| {
runtime_io::with_storage(
storage,
|| Module::<T>::initialize_authorities(&config.authorities),
);
})
}
}
@@ -183,11 +195,25 @@ impl<T: Trait> Module<T> {
);
<system::Module<T>>::deposit_log(log.into());
}
fn initialize_authorities(authorities: &[T::AuthorityId]) {
if !authorities.is_empty() {
assert!(<Authorities<T>>::get().is_empty(), "Authorities are already initialized!");
<Authorities<T>>::put_ref(authorities);
}
}
}
impl<T: Trait> session::OneSessionHandler<T::AccountId> for Module<T> {
type Key = T::AuthorityId;
fn on_genesis_session<'a, I: 'a>(validators: I)
where I: Iterator<Item=(&'a T::AccountId, T::AuthorityId)>
{
let authorities = validators.map(|(_, k)| k).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, T::AuthorityId)>
{
@@ -200,6 +226,7 @@ impl<T: Trait> session::OneSessionHandler<T::AccountId> for Module<T> {
}
}
}
fn on_disabled(i: usize) {
let log: DigestItem<T::Hash> = DigestItem::Consensus(
AURA_ENGINE_ID,