|
| 1 | +use crate::{Call, Config}; |
| 2 | +use codec::{Decode, DecodeWithMemTracking, Encode}; |
| 3 | +use frame_support::dispatch::{DispatchInfo, PostDispatchInfo}; |
| 4 | +use frame_support::pallet_prelude::Weight; |
| 5 | +use frame_support::traits::IsSubType; |
| 6 | +use scale_info::TypeInfo; |
| 7 | +use sp_runtime::traits::{ |
| 8 | + DispatchInfoOf, DispatchOriginOf, Dispatchable, Implication, TransactionExtension, |
| 9 | + ValidateResult, |
| 10 | +}; |
| 11 | +use sp_runtime::transaction_validity::{ |
| 12 | + TransactionPriority, TransactionSource, TransactionValidityError, ValidTransaction, |
| 13 | +}; |
| 14 | +use sp_std::marker::PhantomData; |
| 15 | +use subtensor_macros::freeze_struct; |
| 16 | + |
| 17 | +pub type RuntimeCallFor<T> = <T as frame_system::Config>::RuntimeCall; |
| 18 | + |
| 19 | +#[freeze_struct("d0d094192bd6390e")] |
| 20 | +#[derive(Default, Encode, Decode, DecodeWithMemTracking, Clone, Eq, PartialEq, TypeInfo)] |
| 21 | +pub struct DrandPriority<T: Config + Send + Sync + TypeInfo>(pub PhantomData<T>); |
| 22 | + |
| 23 | +impl<T: Config + Send + Sync + TypeInfo> sp_std::fmt::Debug for DrandPriority<T> { |
| 24 | + fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result { |
| 25 | + write!(f, "DrandPriority") |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +impl<T: Config + Send + Sync + TypeInfo> DrandPriority<T> { |
| 30 | + pub fn new() -> Self { |
| 31 | + Self(PhantomData) |
| 32 | + } |
| 33 | + |
| 34 | + fn get_drand_priority() -> TransactionPriority { |
| 35 | + 10_000u64 |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl<T: Config + Send + Sync + TypeInfo> TransactionExtension<RuntimeCallFor<T>> |
| 40 | + for DrandPriority<T> |
| 41 | +where |
| 42 | + <T as frame_system::Config>::RuntimeCall: |
| 43 | + Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>, |
| 44 | + <T as frame_system::Config>::RuntimeCall: IsSubType<Call<T>>, |
| 45 | +{ |
| 46 | + const IDENTIFIER: &'static str = "DrandPriority"; |
| 47 | + type Implicit = (); |
| 48 | + type Val = (); |
| 49 | + type Pre = (); |
| 50 | + |
| 51 | + fn weight(&self, _call: &RuntimeCallFor<T>) -> Weight { |
| 52 | + // TODO: benchmark transaction extension |
| 53 | + Weight::zero() |
| 54 | + } |
| 55 | + |
| 56 | + fn validate( |
| 57 | + &self, |
| 58 | + origin: DispatchOriginOf<RuntimeCallFor<T>>, |
| 59 | + call: &RuntimeCallFor<T>, |
| 60 | + _info: &DispatchInfoOf<RuntimeCallFor<T>>, |
| 61 | + _len: usize, |
| 62 | + _self_implicit: Self::Implicit, |
| 63 | + _inherited_implication: &impl Implication, |
| 64 | + _source: TransactionSource, |
| 65 | + ) -> ValidateResult<Self::Val, RuntimeCallFor<T>> { |
| 66 | + match call.is_sub_type() { |
| 67 | + Some(Call::write_pulse { .. }) => { |
| 68 | + let validity = ValidTransaction { |
| 69 | + priority: Self::get_drand_priority(), |
| 70 | + ..Default::default() |
| 71 | + }; |
| 72 | + |
| 73 | + Ok((validity, (), origin)) |
| 74 | + } |
| 75 | + _ => Ok((Default::default(), (), origin)), |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + fn prepare( |
| 80 | + self, |
| 81 | + _val: Self::Val, |
| 82 | + _origin: &DispatchOriginOf<RuntimeCallFor<T>>, |
| 83 | + _call: &RuntimeCallFor<T>, |
| 84 | + _info: &DispatchInfoOf<RuntimeCallFor<T>>, |
| 85 | + _len: usize, |
| 86 | + ) -> Result<Self::Pre, TransactionValidityError> { |
| 87 | + Ok(()) |
| 88 | + } |
| 89 | +} |
0 commit comments