From 58ec388f36ff76471d491ebf7553b37768b93e32 Mon Sep 17 00:00:00 2001 From: str-mv Date: Thu, 10 Feb 2022 17:22:07 +0000 Subject: [PATCH] Scheduler uses default runtime logic when applying call instead dispatch method --- --- a/pallets/scheduler/src/lib.rs +++ b/pallets/scheduler/src/lib.rs @@ -55,11 +55,11 @@ mod benchmarking; pub mod weights; -use sp_std::{prelude::*, marker::PhantomData, borrow::Borrow}; +use sp_std::{prelude::*, marker::PhantomData, borrow::Borrow, cmp}; use codec::{Encode, Decode, Codec}; use sp_runtime::{ RuntimeDebug, - traits::{One, BadOrigin, Saturating}, + traits::{Zero, One, BadOrigin, Saturating}, }; use frame_support::{ decl_module, decl_storage, decl_event, decl_error, @@ -74,19 +74,20 @@ use frame_system::{self as system, ensure_signed}; pub use weights::WeightInfo; use scale_info::TypeInfo; -use sp_runtime::ApplyExtrinsicResult; -use sp_runtime::traits::{IdentifyAccount, Verify}; -use sp_runtime::{MultiSignature}; use sp_core::{H160}; +use sp_runtime::transaction_validity::TransactionValidityError; +use frame_support::weights::PostDispatchInfo; +use sp_runtime::DispatchErrorWithPostInfo; pub trait ApplyCall { - fn apply_call(signer: T::AccountId, function: ::Call); // + fn apply_call( + signer: T::AccountId, + function: ::Call, + ) -> Result< + Result>, + TransactionValidityError, + >; } - -/// The address format for describing accounts. -//pub type Signature = MultiSignature; -// pub type Address = sp_runtime::MultiAddress; -//pub type AccountId = <::Signer as IdentifyAccount>::AccountId; /// Our pallet's configuration trait. All our types and constants go in here. If the /// pallet is dependent on specific other pallets, then their configuration traits @@ -437,7 +438,7 @@ let sender = ensure_signed(origin).unwrap_or_default(); - T::Executor::apply_call(sender.clone(), s.call.clone()); + let apply_result = T::Executor::apply_call(sender.clone(), s.call.clone()); let maybe_id = s.maybe_id.clone(); if let Some((period, count)) = s.maybe_periodic { @@ -509,18 +510,20 @@ <::Origin as From>::from(origin.clone()).into(), ) .unwrap_or_default(); - let who_will_pay = sender; // T::SponsorshipHandler::get_sponsor(&sender, &call).unwrap_or(sender); - let sponsor = T::PalletsOrigin::from(system::RawOrigin::Signed(who_will_pay)); - let r = call.clone().dispatch(sponsor.into()); + //let who_will_pay = sender; // T::SponsorshipHandler::get_sponsor(&sender, &call).unwrap_or(sender); + //let sponsor = T::PalletsOrigin::from(system::RawOrigin::Signed(who_will_pay)); + //let r = call.clone().dispatch(sponsor.into()); + + //let sender = ensure_signed(origin).unwrap_or_default(); + let apply_result = T::Executor::apply_call(sender.clone(), call.clone()); //T::PaymentHandler::validate(sponsor.into(), call.clone(), ); todo dispatch call // sanitize maybe_periodic - let maybe_periodic = None; - // let maybe_periodic = maybe_periodic - // .filter(|p| p.1 > 1 && !p.0.is_zero()) - // // Limit the repetitions to 100 calls and remove one from the number of repetitions since we will schedule one now. - // .map(|(p, c)| (p, std::cmp::min(c, PERIODIC_CALLS_LIMIT) - 1)); + let maybe_periodic = maybe_periodic + .filter(|p| p.1 > 1 && !p.0.is_zero()) + // Limit the repetitions to 100 calls and remove one from the number of repetitions since we will schedule one now. + .map(|(p, c)| (p, cmp::min(c, PERIODIC_CALLS_LIMIT) - 1)); let s = Some(Scheduled { maybe_id, @@ -578,6 +581,7 @@ if let Some(s) = scheduled { if let Some(id) = s.maybe_id { Lookup::::remove(id); + // todo add back money / displace to another function for consistency } Self::deposit_event(RawEvent::Canceled(when, index)); Ok(()) --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -74,7 +74,9 @@ use sp_core::crypto::Public; use sp_runtime::{ traits::{BlockNumberProvider, Dispatchable, PostDispatchInfoOf}, + generic::Era, transaction_validity::TransactionValidityError, + DispatchErrorWithPostInfo, }; // pub use pallet_timestamp::Call as TimestampCall; @@ -826,11 +828,25 @@ type ScheduleOrigin = EnsureSigned; type MaxScheduledPerBlock = MaxScheduledPerBlock; type WeightInfo = (); - type Executor = Executor; + type Executor = SchedulerPaymentExecutor; } -pub struct Executor; -impl ApplyCall for Executor +fn get_signed_extras(from: ::AccountId) -> SignedExtra { + ( + frame_system::CheckSpecVersion::::new(), + frame_system::CheckGenesis::::new(), + frame_system::CheckEra::::from(Era::Immortal), + frame_system::CheckNonce::::from(frame_system::Pallet::::account_nonce( + from, + )), + frame_system::CheckWeight::::new(), + pallet_charge_transaction::ChargeTransactionPayment::::new(0), + ) +} + +pub struct SchedulerPaymentExecutor; +impl + ApplyCall for SchedulerPaymentExecutor where ::Call: Member + Dispatchable @@ -838,9 +854,18 @@ + GetDispatchInfo + From>, SelfContainedSignedInfo: Send + Sync + 'static, - Call: From<::Call> + From<::Call> + SelfContainedCall + Call: From<::Call> + + From<::Call> + + SelfContainedCall, + sp_runtime::AccountId32: From<::AccountId>, { - fn apply_call(signer: ::AccountId, call: ::Call) { + fn apply_call( + signer: ::AccountId, + call: ::Call, + ) -> Result< + Result>, + TransactionValidityError, + > { let dispatch_info = call.get_dispatch_info(); let extrinsic = fp_self_contained::CheckedExtrinsic::< AccountId, @@ -848,11 +873,14 @@ SignedExtra, SelfContainedSignedInfo, > { - signed: CheckedSignature::::Unsigned, // change to signer + signed: CheckedSignature::::Signed( + signer.clone().into(), + get_signed_extras(signer.into()), + ), function: call.into(), }; - extrinsic.apply::(&dispatch_info, 0); + extrinsic.apply::(&dispatch_info, 0) } } -- gitstuff