From ea6831e39815d67a980e8f753b7dc0154c4f999c Mon Sep 17 00:00:00 2001 From: Guillaume Thiolliere Date: Tue, 8 Jun 2021 22:54:06 +0200 Subject: [PATCH] put the validate_unsigned implementation inside the pallet definition (#9044) Co-authored-by: Shawn Tabrizi --- substrate/frame/babe/src/equivocation.rs | 14 +++++++------- substrate/frame/babe/src/lib.rs | 12 ++++++++++++ substrate/frame/grandpa/src/equivocation.rs | 14 +++++++------- substrate/frame/grandpa/src/lib.rs | 12 ++++++++++++ 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/substrate/frame/babe/src/equivocation.rs b/substrate/frame/babe/src/equivocation.rs index 0fd74882c1..e9017205c6 100644 --- a/substrate/frame/babe/src/equivocation.rs +++ b/substrate/frame/babe/src/equivocation.rs @@ -179,12 +179,12 @@ where } } -/// A `ValidateUnsigned` implementation that restricts calls to `report_equivocation_unsigned` -/// to local calls (i.e. extrinsics generated on this node) or that already in a block. This -/// guarantees that only block authors can include unsigned equivocation reports. -impl frame_support::unsigned::ValidateUnsigned for Pallet { - type Call = Call; - fn validate_unsigned(source: TransactionSource, call: &Self::Call) -> TransactionValidity { +/// Methods for the `ValidateUnsigned` implementation: +/// It restricts calls to `report_equivocation_unsigned` to local calls (i.e. extrinsics generated +/// on this node) or that already in a block. This guarantees that only block authors can include +/// unsigned equivocation reports. +impl Pallet { + pub fn validate_unsigned(source: TransactionSource, call: &Call) -> TransactionValidity { if let Call::report_equivocation_unsigned(equivocation_proof, key_owner_proof) = call { // discard equivocation report not coming from the local node match source { @@ -221,7 +221,7 @@ impl frame_support::unsigned::ValidateUnsigned for Pallet { } } - fn pre_dispatch(call: &Self::Call) -> Result<(), TransactionValidityError> { + pub fn pre_dispatch(call: &Call) -> Result<(), TransactionValidityError> { if let Call::report_equivocation_unsigned(equivocation_proof, key_owner_proof) = call { is_known_offence::(equivocation_proof, key_owner_proof) } else { diff --git a/substrate/frame/babe/src/lib.rs b/substrate/frame/babe/src/lib.rs index 6eecf26752..a0a9e01eaa 100644 --- a/substrate/frame/babe/src/lib.rs +++ b/substrate/frame/babe/src/lib.rs @@ -407,6 +407,18 @@ pub mod pallet { Ok(()) } } + + #[pallet::validate_unsigned] + impl ValidateUnsigned for Pallet { + type Call = Call; + fn validate_unsigned(source: TransactionSource, call: &Self::Call) -> TransactionValidity { + Self::validate_unsigned(source, call) + } + + fn pre_dispatch(call: &Self::Call) -> Result<(), TransactionValidityError> { + Self::pre_dispatch(call) + } + } } /// A BABE public key diff --git a/substrate/frame/grandpa/src/equivocation.rs b/substrate/frame/grandpa/src/equivocation.rs index 24f56247d3..0383d2d9a9 100644 --- a/substrate/frame/grandpa/src/equivocation.rs +++ b/substrate/frame/grandpa/src/equivocation.rs @@ -200,12 +200,12 @@ pub struct GrandpaTimeSlot { pub round: RoundNumber, } -/// A `ValidateUnsigned` implementation that restricts calls to `report_equivocation_unsigned` -/// to local calls (i.e. extrinsics generated on this node) or that already in a block. This -/// guarantees that only block authors can include unsigned equivocation reports. -impl frame_support::unsigned::ValidateUnsigned for Pallet { - type Call = Call; - fn validate_unsigned(source: TransactionSource, call: &Self::Call) -> TransactionValidity { +/// Methods for the `ValidateUnsigned` implementation: +/// It restricts calls to `report_equivocation_unsigned` to local calls (i.e. extrinsics generated +/// on this node) or that already in a block. This guarantees that only block authors can include +/// unsigned equivocation reports. +impl Pallet { + pub fn validate_unsigned(source: TransactionSource, call: &Call) -> TransactionValidity { if let Call::report_equivocation_unsigned(equivocation_proof, key_owner_proof) = call { // discard equivocation report not coming from the local node match source { @@ -243,7 +243,7 @@ impl frame_support::unsigned::ValidateUnsigned for Pallet { } } - fn pre_dispatch(call: &Self::Call) -> Result<(), TransactionValidityError> { + pub fn pre_dispatch(call: &Call) -> Result<(), TransactionValidityError> { if let Call::report_equivocation_unsigned(equivocation_proof, key_owner_proof) = call { is_known_offence::(equivocation_proof, key_owner_proof) } else { diff --git a/substrate/frame/grandpa/src/lib.rs b/substrate/frame/grandpa/src/lib.rs index f6edb07ccc..952e0d6461 100644 --- a/substrate/frame/grandpa/src/lib.rs +++ b/substrate/frame/grandpa/src/lib.rs @@ -341,6 +341,18 @@ pub mod pallet { Pallet::::initialize(&self.authorities) } } + + #[pallet::validate_unsigned] + impl ValidateUnsigned for Pallet { + type Call = Call; + fn validate_unsigned(source: TransactionSource, call: &Self::Call) -> TransactionValidity { + Self::validate_unsigned(source, call) + } + + fn pre_dispatch(call: &Self::Call) -> Result<(), TransactionValidityError> { + Self::pre_dispatch(call) + } + } } pub trait WeightInfo {