Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove authorship pallet dependency from babe & grandpa #6

Open
wants to merge 3 commits into
base: serai
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions frame/babe/src/equivocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -111,7 +111,7 @@ impl<T, R, P, L>
OffenceReportSystem<Option<T::AccountId>, (EquivocationProof<HeaderFor<T>>, T::KeyOwnerProof)>
for EquivocationReportSystem<T, R, P, L>
where
T: Config + pallet_authorship::Config + frame_system::offchain::SendTransactionTypes<Call<T>>,
T: Config + frame_system::offchain::SendTransactionTypes<Call<T>>,
R: ReportOffence<
T::AccountId,
P::IdentificationTuple,
Expand Down Expand Up @@ -164,7 +164,11 @@ where
evidence: (EquivocationProof<HeaderFor<T>>, T::KeyOwnerProof),
) -> Result<(), DispatchError> {
let (equivocation_proof, key_owner_proof) = evidence;
let reporter = reporter.or_else(|| <pallet_authorship::Pallet<T>>::author());
let reporter = reporter.or_else(|| {
let digest = <frame_system::Pallet<T>>::digest();
let pre_runtime_digests = digest.logs.iter().filter_map(|d| d.as_pre_runtime());
Pallet::<T>::find_author(pre_runtime_digests)
});
let offender = equivocation_proof.offender.clone();
let slot = equivocation_proof.slot;

Expand Down
11 changes: 6 additions & 5 deletions frame/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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<AccountId = Public> {
/// 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.
Expand Down Expand Up @@ -485,15 +485,16 @@ pub mod pallet {
}
}

impl<T: Config> FindAuthor<u32> for Pallet<T> {
fn find_author<'a, I>(digests: I) -> Option<u32>
impl<T: Config> FindAuthor<T::AccountId> for Pallet<T> {
fn find_author<'a, I>(digests: I) -> Option<T::AccountId>
where
I: 'a + IntoIterator<Item = (ConsensusEngineId, &'a [u8])>,
{
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())
}
}

Expand Down
10 changes: 7 additions & 3 deletions frame/grandpa/src/equivocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -123,7 +123,7 @@ impl<T, R, P, L>
(EquivocationProof<T::Hash, BlockNumberFor<T>>, T::KeyOwnerProof),
> for EquivocationReportSystem<T, R, P, L>
where
T: Config + pallet_authorship::Config + frame_system::offchain::SendTransactionTypes<Call<T>>,
T: Config + frame_system::offchain::SendTransactionTypes<Call<T>>,
R: ReportOffence<
T::AccountId,
P::IdentificationTuple,
Expand Down Expand Up @@ -177,7 +177,11 @@ where
evidence: (EquivocationProof<T::Hash, BlockNumberFor<T>>, T::KeyOwnerProof),
) -> Result<(), DispatchError> {
let (equivocation_proof, key_owner_proof) = evidence;
let reporter = reporter.or_else(|| <pallet_authorship::Pallet<T>>::author());
let reporter = reporter.or_else(|| {
let digest = <frame_system::Pallet<T>>::digest();
let pre_runtime_digests = digest.logs.iter().filter_map(|d| d.as_pre_runtime());
<T as Config>::FindAuthor::find_author(pre_runtime_digests)
});
let offender = equivocation_proof.offender().clone();

// We check the equivocation within the context of its set id (and
Expand Down
4 changes: 3 additions & 1 deletion frame/grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use frame_support::{
dispatch::{DispatchResultWithPostInfo, Pays},
pallet_prelude::Get,
storage,
traits::OneSessionHandler,
traits::{OneSessionHandler, FindAuthor},
weights::Weight,
WeakBoundedVec,
};
Expand Down Expand Up @@ -117,6 +117,8 @@ pub mod pallet {
Option<Self::AccountId>,
(EquivocationProof<Self::Hash, BlockNumberFor<Self>>, Self::KeyOwnerProof),
>;

type FindAuthor: FindAuthor<Self::AccountId>;
}

#[pallet::hooks]
Expand Down
Loading