From 615a24855c55e778622ca6a5ad15b427157cfc4c Mon Sep 17 00:00:00 2001 From: akildemir Date: Tue, 25 Jun 2024 16:18:58 +0300 Subject: [PATCH 1/3] use FindAuthor in babe & grandpa directly --- frame/babe/src/equivocation.rs | 8 ++++++-- frame/babe/src/lib.rs | 2 ++ frame/grandpa/src/equivocation.rs | 8 ++++++-- frame/grandpa/src/lib.rs | 4 +++- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/frame/babe/src/equivocation.rs b/frame/babe/src/equivocation.rs index d9a097da76b24..bafec08d98d90 100644 --- a/frame/babe/src/equivocation.rs +++ b/frame/babe/src/equivocation.rs @@ -34,7 +34,7 @@ //! that the `ValidateUnsigned` for the BABE pallet is used in the runtime //! definition. -use frame_support::traits::{Get, KeyOwnerProofSystem}; +use frame_support::traits::{Get, KeyOwnerProofSystem, FindAuthor}; use frame_system::pallet_prelude::HeaderFor; use log::{error, info}; @@ -164,7 +164,11 @@ where evidence: (EquivocationProof>, T::KeyOwnerProof), ) -> Result<(), DispatchError> { let (equivocation_proof, key_owner_proof) = evidence; - let reporter = reporter.or_else(|| >::author()); + let reporter = reporter.or_else(|| { + let digest = >::digest(); + let pre_runtime_digests = digest.logs.iter().filter_map(|d| d.as_pre_runtime()); + ::FindAuthor::find_author(pre_runtime_digests) + }); let offender = equivocation_proof.offender.clone(); let slot = equivocation_proof.slot; diff --git a/frame/babe/src/lib.rs b/frame/babe/src/lib.rs index 793718146462b..c2915a4172476 100644 --- a/frame/babe/src/lib.rs +++ b/frame/babe/src/lib.rs @@ -166,6 +166,8 @@ pub mod pallet { Option, (EquivocationProof>, Self::KeyOwnerProof), >; + + type FindAuthor: FindAuthor; } #[pallet::error] diff --git a/frame/grandpa/src/equivocation.rs b/frame/grandpa/src/equivocation.rs index e32886603e8f5..1da836c8e927d 100644 --- a/frame/grandpa/src/equivocation.rs +++ b/frame/grandpa/src/equivocation.rs @@ -37,7 +37,7 @@ //! definition. use codec::{self as codec, Decode, Encode}; -use frame_support::traits::{Get, KeyOwnerProofSystem}; +use frame_support::traits::{Get, KeyOwnerProofSystem, FindAuthor}; use frame_system::pallet_prelude::BlockNumberFor; use log::{error, info}; use sp_consensus_grandpa::{AuthorityId, EquivocationProof, RoundNumber, SetId, KEY_TYPE}; @@ -177,7 +177,11 @@ where evidence: (EquivocationProof>, T::KeyOwnerProof), ) -> Result<(), DispatchError> { let (equivocation_proof, key_owner_proof) = evidence; - let reporter = reporter.or_else(|| >::author()); + let reporter = reporter.or_else(|| { + let digest = >::digest(); + let pre_runtime_digests = digest.logs.iter().filter_map(|d| d.as_pre_runtime()); + ::FindAuthor::find_author(pre_runtime_digests) + }); let offender = equivocation_proof.offender().clone(); // We check the equivocation within the context of its set id (and diff --git a/frame/grandpa/src/lib.rs b/frame/grandpa/src/lib.rs index a9f0ccfeb7482..475b2d6d8d90a 100644 --- a/frame/grandpa/src/lib.rs +++ b/frame/grandpa/src/lib.rs @@ -39,7 +39,7 @@ use frame_support::{ dispatch::{DispatchResultWithPostInfo, Pays}, pallet_prelude::Get, storage, - traits::OneSessionHandler, + traits::{OneSessionHandler, FindAuthor}, weights::Weight, WeakBoundedVec, }; @@ -117,6 +117,8 @@ pub mod pallet { Option, (EquivocationProof>, Self::KeyOwnerProof), >; + + type FindAuthor: FindAuthor; } #[pallet::hooks] From f6883ed7c5bcd35202de7053f01a421402f8f817 Mon Sep 17 00:00:00 2001 From: akildemir Date: Wed, 26 Jun 2024 11:50:12 +0300 Subject: [PATCH 2/3] use babe FindAuthor implementation --- frame/babe/src/equivocation.rs | 4 ++-- frame/babe/src/lib.rs | 9 ++++----- frame/grandpa/src/equivocation.rs | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/frame/babe/src/equivocation.rs b/frame/babe/src/equivocation.rs index bafec08d98d90..3cb76280e50fa 100644 --- a/frame/babe/src/equivocation.rs +++ b/frame/babe/src/equivocation.rs @@ -111,7 +111,7 @@ impl OffenceReportSystem, (EquivocationProof>, T::KeyOwnerProof)> for EquivocationReportSystem where - T: Config + pallet_authorship::Config + frame_system::offchain::SendTransactionTypes>, + T: Config + frame_system::offchain::SendTransactionTypes>, R: ReportOffence< T::AccountId, P::IdentificationTuple, @@ -167,7 +167,7 @@ where let reporter = reporter.or_else(|| { let digest = >::digest(); let pre_runtime_digests = digest.logs.iter().filter_map(|d| d.as_pre_runtime()); - ::FindAuthor::find_author(pre_runtime_digests) + Pallet::find_author(pre_runtime_digests) }); let offender = equivocation_proof.offender.clone(); let slot = equivocation_proof.slot; diff --git a/frame/babe/src/lib.rs b/frame/babe/src/lib.rs index c2915a4172476..08c5e61b36aee 100644 --- a/frame/babe/src/lib.rs +++ b/frame/babe/src/lib.rs @@ -166,8 +166,6 @@ pub mod pallet { Option, (EquivocationProof>, Self::KeyOwnerProof), >; - - type FindAuthor: FindAuthor; } #[pallet::error] @@ -487,15 +485,16 @@ pub mod pallet { } } -impl FindAuthor for Pallet { - fn find_author<'a, I>(digests: I) -> Option +impl FindAuthor for Pallet { + fn find_author<'a, I>(digests: I) -> Option where I: 'a + IntoIterator, { for (id, mut data) in digests.into_iter() { if id == BABE_ENGINE_ID { let pre_digest: PreDigest = PreDigest::decode(&mut data).ok()?; - return Some(pre_digest.authority_index()) + let index = pre_digest.authority_index(); + return Some(Self::authorities()[index as usize].0.clone().into()) } } diff --git a/frame/grandpa/src/equivocation.rs b/frame/grandpa/src/equivocation.rs index 1da836c8e927d..b81d62d3ebc9b 100644 --- a/frame/grandpa/src/equivocation.rs +++ b/frame/grandpa/src/equivocation.rs @@ -123,7 +123,7 @@ impl (EquivocationProof>, T::KeyOwnerProof), > for EquivocationReportSystem where - T: Config + pallet_authorship::Config + frame_system::offchain::SendTransactionTypes>, + T: Config + frame_system::offchain::SendTransactionTypes>, R: ReportOffence< T::AccountId, P::IdentificationTuple, From 80eb8501e10906de3a1c097b833ecba641430ee7 Mon Sep 17 00:00:00 2001 From: akildemir Date: Wed, 26 Jun 2024 12:03:10 +0300 Subject: [PATCH 3/3] bug fixes --- frame/babe/src/equivocation.rs | 2 +- frame/babe/src/lib.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/frame/babe/src/equivocation.rs b/frame/babe/src/equivocation.rs index 3cb76280e50fa..0c8f41b4d6981 100644 --- a/frame/babe/src/equivocation.rs +++ b/frame/babe/src/equivocation.rs @@ -167,7 +167,7 @@ where let reporter = reporter.or_else(|| { let digest = >::digest(); let pre_runtime_digests = digest.logs.iter().filter_map(|d| d.as_pre_runtime()); - Pallet::find_author(pre_runtime_digests) + Pallet::::find_author(pre_runtime_digests) }); let offender = equivocation_proof.offender.clone(); let slot = equivocation_proof.slot; diff --git a/frame/babe/src/lib.rs b/frame/babe/src/lib.rs index 08c5e61b36aee..e8909bcae4ad6 100644 --- a/frame/babe/src/lib.rs +++ b/frame/babe/src/lib.rs @@ -37,7 +37,7 @@ use sp_consensus_babe::{ EquivocationProof, Randomness as BabeRandomness, Slot, BABE_ENGINE_ID, RANDOMNESS_LENGTH, RANDOMNESS_VRF_CONTEXT, }; -use sp_core::crypto::Wraps; +use sp_core::{crypto::Wraps, sr25519::Public}; use sp_runtime::{ generic::DigestItem, traits::{IsMember, One, SaturatedConversion, Saturating, Zero}, @@ -120,7 +120,7 @@ pub mod pallet { #[pallet::config] #[pallet::disable_frame_system_supertrait_check] - pub trait Config: pallet_timestamp::Config { + pub trait Config: pallet_timestamp::Config { /// The amount of time, in slots, that each epoch should last. /// NOTE: Currently it is not possible to change the epoch duration after /// the chain has started. Attempting to do so will brick block production.