log authorities change (#726)

This commit is contained in:
Svyatoslav Nikolsky
2018-09-19 18:21:46 +03:00
committed by Gav Wood
parent 04e22cd795
commit 1e31b8d8ea
3 changed files with 143 additions and 4 deletions
+15 -4
View File
@@ -51,6 +51,9 @@ use primitives::bft::MisbehaviorReport;
use substrate_primitives::storage::well_known_keys;
use system::{ensure_signed, ensure_inherent};
mod mock;
mod tests;
struct AuthorityStorageVec<S: codec::Codec + Default>(rstd::marker::PhantomData<S>);
impl<S: codec::Codec + Default> StorageVec for AuthorityStorageVec<S> {
type Item = S;
@@ -90,13 +93,13 @@ impl<SessionKey: Member> RawLog<SessionKey> {
// Implementation for tests outside of this crate.
#[cfg(any(feature = "std", test))]
impl<N> From<RawLog<N>> for primitives::testing::DigestItem {
impl<N> From<RawLog<N>> for primitives::testing::DigestItem where N: Into<u64> {
fn from(log: RawLog<N>) -> primitives::testing::DigestItem {
match log {
RawLog::AuthoritiesChange(authorities) =>
primitives::generic::DigestItem::AuthoritiesChange
::<substrate_primitives::H256, u64>(authorities.into_iter()
.enumerate().map(|(i, _)| i as u64).collect()),
.map(Into::into).collect()),
}
}
}
@@ -211,13 +214,21 @@ impl<T: Trait> Module<T> {
<OriginalAuthorities<T>>::put(current_authorities.unwrap_or_else(||
AuthorityStorageVec::<T::SessionKey>::items()));
}
/// Deposit one of this module's logs.
fn deposit_log(log: Log<T>) {
<system::Module<T>>::deposit_log(<T as Trait>::Log::from(log).into());
}
}
/// Finalization hook for the consensus module.
impl<T: Trait> OnFinalise<T::BlockNumber> for Module<T> {
fn on_finalise(_n: T::BlockNumber) {
if let Some(_) = <OriginalAuthorities<T>>::take() {
// TODO: call Self::deposit_log
if let Some(original_authorities) = <OriginalAuthorities<T>>::take() {
let current_authorities = AuthorityStorageVec::<T::SessionKey>::items();
if current_authorities != original_authorities {
Self::deposit_log(RawLog::AuthoritiesChange(current_authorities));
}
}
}
}