From 86228fa45e4c64642f7210cf44c40cc84ae17537 Mon Sep 17 00:00:00 2001
From: yjh <yjh465402634@gmail.com>
Date: Fri, 27 Oct 2023 03:55:49 +0800
Subject: [PATCH] add `authorities_len` for aura (#2040)

---
 substrate/frame/aura/src/lib.rs   | 7 ++++++-
 substrate/frame/aura/src/tests.rs | 3 ++-
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/substrate/frame/aura/src/lib.rs b/substrate/frame/aura/src/lib.rs
index 3017120e4e6..f7506db05d1 100644
--- a/substrate/frame/aura/src/lib.rs
+++ b/substrate/frame/aura/src/lib.rs
@@ -230,6 +230,11 @@ impl<T: Config> Pallet<T> {
 		}
 	}
 
+	/// Return current authorities length.
+	pub fn authorities_len() -> usize {
+		Authorities::<T>::decode_len().unwrap_or(0)
+	}
+
 	/// Get the current slot from the pre-runtime digests.
 	fn current_slot_from_digests() -> Option<Slot> {
 		let digest = frame_system::Pallet::<T>::digest();
@@ -363,7 +368,7 @@ impl<T: Config> FindAuthor<u32> for Pallet<T> {
 		for (id, mut data) in digests.into_iter() {
 			if id == AURA_ENGINE_ID {
 				let slot = Slot::decode(&mut data).ok()?;
-				let author_index = *slot % Self::authorities().len() as u64;
+				let author_index = *slot % Self::authorities_len() as u64;
 				return Some(author_index as u32)
 			}
 		}
diff --git a/substrate/frame/aura/src/tests.rs b/substrate/frame/aura/src/tests.rs
index d3ce877d3e6..b3a5e144fad 100644
--- a/substrate/frame/aura/src/tests.rs
+++ b/substrate/frame/aura/src/tests.rs
@@ -29,7 +29,8 @@ use sp_runtime::{Digest, DigestItem};
 fn initial_values() {
 	build_ext_and_execute_test(vec![0, 1, 2, 3], || {
 		assert_eq!(Aura::current_slot(), 0u64);
-		assert_eq!(Aura::authorities().len(), 4);
+		assert_eq!(Aura::authorities().len(), Aura::authorities_len());
+		assert_eq!(Aura::authorities_len(), 4);
 	});
 }
 
-- 
GitLab