From f785964dbb67cd0b5520f6dad04f65f0c1556f69 Mon Sep 17 00:00:00 2001 From: Xiliang Chen <xlchen1291@gmail.com> Date: Fri, 22 May 2020 11:15:26 +1200 Subject: [PATCH] replace SignedExtension with ValidateUnsigned (#179) * replace SignedExtension with ValidateUnsigned * need to provide tag --- oracle/src/lib.rs | 58 +++++++++------------------------------------ oracle/src/mock.rs | 1 - oracle/src/tests.rs | 20 ++++++---------- 3 files changed, 18 insertions(+), 61 deletions(-) diff --git a/oracle/src/lib.rs b/oracle/src/lib.rs index 9d19523..5ce2aaa 100644 --- a/oracle/src/lib.rs +++ b/oracle/src/lib.rs @@ -7,23 +7,21 @@ mod mock; mod tests; mod timestamped_value; -use codec::{Decode, Encode}; +use codec::Encode; use frame_support::{ - decl_error, decl_event, decl_module, decl_storage, - dispatch::Dispatchable, - ensure, + decl_error, decl_event, decl_module, decl_storage, ensure, traits::{ChangeMembers, Get, InitializeMembers, Time}, weights::{DispatchClass, FunctionOf, Pays}, - IsSubType, IterableStorageMap, Parameter, + IterableStorageMap, Parameter, }; use sp_runtime::{ - traits::{DispatchInfoOf, Member, SignedExtension}, + traits::Member, transaction_validity::{ - InvalidTransaction, TransactionPriority, TransactionValidity, TransactionValidityError, ValidTransaction, + InvalidTransaction, TransactionPriority, TransactionSource, TransactionValidity, ValidTransaction, }, DispatchResult, }; -use sp_std::{fmt, prelude::*, result, vec}; +use sp_std::{prelude::*, vec}; // FIXME: `pallet/frame-` prefix should be used for all pallet modules, but currently `frame_system` // would cause compiling error in `decl_module!` and `construct_runtime!` // #3295 https://github.com/paritytech/substrate/issues/3295 @@ -57,7 +55,6 @@ pub type TimestampedValueOf<T> = TimestampedValue<<T as Trait>::OracleValue, Mom pub trait Trait: frame_system::Trait { type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>; - type Call: Parameter + Dispatchable<Origin = <Self as frame_system::Trait>::Origin> + IsSubType<Module<Self>, Self>; type OnNewData: OnNewData<Self::AccountId, Self::OracleKey, Self::OracleValue>; type CombineData: CombineData<Self::OracleKey, TimestampedValueOf<Self>>; type Time: Time; @@ -265,44 +262,10 @@ impl<T: Trait> DataProviderExtended<T::OracleKey, T::OracleValue, T::AccountId> } } -#[derive(Encode, Decode, Clone, Eq, PartialEq, Default)] -pub struct CheckOperator<T: Trait + Send + Sync>(sp_std::marker::PhantomData<T>); - -impl<T: Trait + Send + Sync> CheckOperator<T> { - pub fn new() -> Self { - Self(sp_std::marker::PhantomData) - } -} - -impl<T: Trait + Send + Sync> fmt::Debug for CheckOperator<T> { - #[cfg(feature = "std")] - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "CheckOperator") - } - - #[cfg(not(feature = "std"))] - fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result { - Ok(()) - } -} - -impl<T: Trait + Send + Sync> SignedExtension for CheckOperator<T> { - const IDENTIFIER: &'static str = "CheckOperator"; - type AccountId = T::AccountId; - type Call = <T as Trait>::Call; - type AdditionalSigned = (); - type Pre = (); - - fn additional_signed(&self) -> result::Result<(), TransactionValidityError> { - Ok(()) - } - - fn validate_unsigned(call: &Self::Call, _info: &DispatchInfoOf<Self::Call>, _len: usize) -> TransactionValidity { - let call = match call.is_sub_type() { - Some(call) => call, - None => return Ok(ValidTransaction::default()), - }; +impl<T: Trait> frame_support::unsigned::ValidateUnsigned for Module<T> { + type Call = Call<T>; + fn validate_unsigned(_source: TransactionSource, call: &Self::Call) -> TransactionValidity { if let Call::feed_values(value, index, signature) = call { let members = Module::<T>::members(); let who = members.0.get(*index as usize); @@ -326,8 +289,9 @@ impl<T: Trait + Send + Sync> SignedExtension for CheckOperator<T> { Nonces::<T>::insert(who, nonce + 1); - ValidTransaction::with_tag_prefix("Oracle") + ValidTransaction::with_tag_prefix("orml-oracle") .priority(T::UnsignedPriority::get()) + .and_provides((who, nonce)) .longevity(256) .propagate(true) .build() diff --git a/oracle/src/mock.rs b/oracle/src/mock.rs index 38d6e56..30bce7d 100644 --- a/oracle/src/mock.rs +++ b/oracle/src/mock.rs @@ -89,7 +89,6 @@ parameter_types! { impl Trait for Test { type Event = (); - type Call = Call; type OnNewData = (); type CombineData = DefaultCombineData<Self, MinimumCount, ExpiresIn>; type Time = Timestamp; diff --git a/oracle/src/tests.rs b/oracle/src/tests.rs index 5c76de1..9d6c5e6 100644 --- a/oracle/src/tests.rs +++ b/oracle/src/tests.rs @@ -1,19 +1,18 @@ #![cfg(test)] use crate::{ - mock::{new_test_ext, Call, ModuleOracle, OracleCall, Origin, Test, Timestamp}, - {CheckOperator, TimestampedValue}, + mock::{new_test_ext, ModuleOracle, OracleCall, Origin, Timestamp}, + TimestampedValue, }; use codec::Encode; use frame_support::{ assert_noop, assert_ok, dispatch, traits::{ChangeMembers, OnFinalize}, - weights::{DispatchClass, DispatchInfo, Pays}, + unsigned::ValidateUnsigned, }; use sp_runtime::{ testing::UintAuthorityId, - traits::SignedExtension, - transaction_validity::{InvalidTransaction, TransactionValidityError}, + transaction_validity::{InvalidTransaction, TransactionSource, TransactionValidityError}, RuntimeAppPublic, }; @@ -25,14 +24,9 @@ fn feed_values_from_session_key( ) -> Result<dispatch::DispatchResult, TransactionValidityError> { let sig = id.sign(&(nonce, &values).encode()).unwrap(); - CheckOperator::<Test>::validate_unsigned( - &Call::ModuleOracle(OracleCall::feed_values(values.clone(), index, sig.clone())), - &DispatchInfo { - weight: 0, - class: DispatchClass::Normal, - pays_fee: Pays::Yes, - }, - 0, + <ModuleOracle as ValidateUnsigned>::validate_unsigned( + TransactionSource::External, + &OracleCall::feed_values(values.clone(), index, sig.clone()), )?; Ok(ModuleOracle::feed_values(Origin::NONE, values, index, sig)) -- GitLab