From 310987ab735e201eaea4896749f191031db45b3d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bastian=20K=C3=B6cher?= <bkchr@users.noreply.github.com>
Date: Sat, 17 Aug 2019 19:41:29 +0200
Subject: [PATCH] Implements `FindAuthor<u32>` for `srml-aura` (#3426)

* Implements `FindAuthor<u32>` for `srml-aura`

* Update lib.rs

* Build
---
 .../core/consensus/aura/primitives/src/lib.rs |  2 +-
 substrate/srml/aura/src/lib.rs                | 30 +++++++++++++++----
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/substrate/core/consensus/aura/primitives/src/lib.rs b/substrate/core/consensus/aura/primitives/src/lib.rs
index 070eb6c6a91..84671e5ae68 100644
--- a/substrate/core/consensus/aura/primitives/src/lib.rs
+++ b/substrate/core/consensus/aura/primitives/src/lib.rs
@@ -61,7 +61,7 @@ pub mod ed25519 {
 pub const AURA_ENGINE_ID: ConsensusEngineId = [b'a', b'u', b'r', b'a'];
 
 /// The index of an authority.
-pub type AuthorityIndex = u64;
+pub type AuthorityIndex = u32;
 
 /// An consensus log item for Aura.
 #[derive(Decode, Encode)]
diff --git a/substrate/srml/aura/src/lib.rs b/substrate/srml/aura/src/lib.rs
index fb131e445f4..e02a2e1f0a0 100644
--- a/substrate/srml/aura/src/lib.rs
+++ b/substrate/srml/aura/src/lib.rs
@@ -48,8 +48,11 @@
 pub use timestamp;
 
 use rstd::{result, prelude::*};
-use codec::Encode;
-use srml_support::{decl_storage, decl_module, Parameter, storage::StorageValue, traits::Get};
+use codec::{Encode, Decode};
+use srml_support::{
+	decl_storage, decl_module, Parameter, storage::StorageValue, traits::{Get, FindAuthor},
+	ConsensusEngineId,
+};
 use app_crypto::AppPublic;
 use sr_primitives::{
 	traits::{SaturatedConversion, Saturating, Zero, Member, IsMember}, generic::DigestItem,
@@ -60,9 +63,7 @@ use timestamp::TimestampInherentData;
 use inherents::{RuntimeString, InherentIdentifier, InherentData, ProvideInherent, MakeFatalError};
 #[cfg(feature = "std")]
 use inherents::{InherentDataProviders, ProvideInherentData};
-use substrate_consensus_aura_primitives::{AURA_ENGINE_ID, ConsensusLog};
-#[cfg(feature = "std")]
-use codec::Decode;
+use substrate_consensus_aura_primitives::{AURA_ENGINE_ID, ConsensusLog, AuthorityIndex};
 
 mod mock;
 mod tests;
@@ -215,13 +216,30 @@ 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,
-			ConsensusLog::<T::AuthorityId>::OnDisabled(i as u64).encode(),
+			ConsensusLog::<T::AuthorityId>::OnDisabled(i as AuthorityIndex).encode(),
 		);
 
 		<system::Module<T>>::deposit_log(log.into());
 	}
 }
 
+impl<T: Trait> FindAuthor<u32> for Module<T> {
+	fn find_author<'a, I>(digests: I) -> Option<u32> where
+		I: 'a + IntoIterator<Item=(ConsensusEngineId, &'a [u8])>
+	{
+		for (id, mut data) in digests.into_iter() {
+			if id == AURA_ENGINE_ID {
+				if let Ok(slot_num) = u64::decode(&mut data) {
+					let author_index = slot_num % Self::authorities().len() as u64;
+					return Some(author_index as u32)
+				}
+			}
+		}
+
+		None
+	}
+}
+
 impl<T: Trait> IsMember<T::AuthorityId> for Module<T> {
 	fn is_member(authority_id: &T::AuthorityId) -> bool {
 		Self::authorities()
-- 
GitLab