From 83f1741d1d73ce2f784b80c721704c51e623c4bd Mon Sep 17 00:00:00 2001 From: Daniel Shiposha Date: Thu, 20 Oct 2022 12:05:10 +0000 Subject: [PATCH] fix: make schedulerv2 take fees --- --- a/pallets/scheduler-v2/src/lib.rs +++ b/pallets/scheduler-v2/src/lib.rs @@ -81,17 +81,18 @@ traits::{ schedule::{self, DispatchTime}, EnsureOrigin, Get, IsType, OriginTrait, PrivilegeCmp, StorageVersion, PreimageRecipient, - ConstU32, + ConstU32, UnfilteredDispatchable, }, - weights::Weight, + weights::{Weight, PostDispatchInfo}, unsigned::TransactionValidityError, }; use frame_system::{self as system}; use scale_info::TypeInfo; use sp_runtime::{ traits::{BadOrigin, One, Saturating, Zero, Hash}, - BoundedVec, RuntimeDebug, + BoundedVec, RuntimeDebug, DispatchErrorWithPostInfo, }; +use sp_core::H160; use sp_std::{borrow::Borrow, cmp::Ordering, marker::PhantomData, prelude::*}; pub use weights::WeightInfo; @@ -206,6 +207,12 @@ } } +pub enum ScheduledEnsureOriginSuccess { + Root, + Signed(AccountId), + Unsigned, +} + pub type TaskName = [u8; 32]; /// Information regarding an item to be executed in the future. @@ -312,6 +319,7 @@ /// The aggregated call type. type Call: Parameter + Dispatchable::Origin, PostInfo = PostDispatchInfo> + + UnfilteredDispatchable::Origin> + GetDispatchInfo + From>; @@ -320,7 +328,10 @@ type MaximumWeight: Get; /// Required origin to schedule or cancel calls. - type ScheduleOrigin: EnsureOrigin<::Origin>; + type ScheduleOrigin: EnsureOrigin< + ::Origin, + Success = ScheduledEnsureOriginSuccess, + >; /// Compare the privileges of origins. /// @@ -340,6 +351,9 @@ /// The preimage provider with which we look up call hashes to get the call. type Preimages: SchedulerPreimages; + + /// The helper type used for custom transaction fee logic. + type CallExecutor: DispatchCall; } #[pallet::storage] @@ -726,6 +740,18 @@ } use ServiceTaskError::*; +/// A Scheduler-Runtime interface for finer payment handling. +pub trait DispatchCall { + /// Resolve the call dispatch, including any post-dispatch operations. + fn dispatch_call( + signer: Option, + function: ::Call, + ) -> Result< + Result>, + TransactionValidityError, + >; +} + impl Pallet { /// Service up to `max` agendas queue starting from earliest incompletely executed agenda. fn service_agendas(weight: &mut WeightCounter, now: T::BlockNumber, max: u32) { @@ -927,12 +953,41 @@ return Err(Overweight); } - let (maybe_actual_call_weight, result) = match call.dispatch(dispatch_origin) { - Ok(post_info) => (post_info.actual_weight, Ok(())), - Err(error_and_info) => ( - error_and_info.post_info.actual_weight, - Err(error_and_info.error), - ), + // let scheduled_origin = + // <::Origin as From>::from(origin.clone()); + let ensured_origin = T::ScheduleOrigin::ensure_origin(dispatch_origin.into()); + + let r = match ensured_origin { + Ok(ScheduledEnsureOriginSuccess::Root) => { + Ok(call.dispatch_bypass_filter(frame_system::RawOrigin::Root.into())) + }, + Ok(ScheduledEnsureOriginSuccess::Signed(sender)) => { + // Execute transaction via chain default pipeline + // That means dispatch will be processed like any user's extrinsic e.g. transaction fees will be taken + T::CallExecutor::dispatch_call(Some(sender), call.clone()) + }, + Ok(ScheduledEnsureOriginSuccess::Unsigned) => { + // Unsigned version of the above + T::CallExecutor::dispatch_call(None, call.clone()) + } + Err(e) => Ok(Err(e.into())), + }; + + let (maybe_actual_call_weight, result) = match r { + Ok(result) => match result { + Ok(post_info) => (post_info.actual_weight, Ok(())), + Err(error_and_info) => ( + error_and_info.post_info.actual_weight, + Err(error_and_info.error), + ), + }, + Err(_) => { + log::error!( + target: "runtime::scheduler", + "Warning: Scheduler has failed to execute a post-dispatch transaction. \ + This block might have become invalid."); + (None, Err(DispatchError::CannotLookup)) + } }; let call_weight = maybe_actual_call_weight.unwrap_or(call_weight); weight.check_accrue(base_weight); --- a/runtime/common/config/pallets/scheduler.rs +++ b/runtime/common/config/pallets/scheduler.rs @@ -27,7 +27,7 @@ runtime_common::{scheduler::SchedulerPaymentExecutor, config::substrate::RuntimeBlockWeights}, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, OriginCaller, Balances, }; -use pallet_unique_scheduler::ScheduledEnsureOriginSuccess; +use pallet_unique_scheduler_v2::ScheduledEnsureOriginSuccess; use up_common::types::AccountId; parameter_types! { @@ -98,4 +98,5 @@ type MaxScheduledPerBlock = MaxScheduledPerBlock; type WeightInfo = (); type Preimages = (); + type CallExecutor = SchedulerPaymentExecutor; } --- a/runtime/common/scheduler.rs +++ b/runtime/common/scheduler.rs @@ -28,11 +28,11 @@ use crate::{Runtime, RuntimeCall, RuntimeOrigin, Balances, maintenance}; use up_common::types::{AccountId, Balance}; use fp_self_contained::SelfContainedCall; -use pallet_unique_scheduler::DispatchCall; +use pallet_unique_scheduler_v2::DispatchCall; use pallet_transaction_payment::ChargeTransactionPayment; -type SponsorshipChargeTransactionPayment = - pallet_charge_transaction::ChargeTransactionPayment; +// type SponsorshipChargeTransactionPayment = +// pallet_charge_transaction::ChargeTransactionPayment; /// The SignedExtension to the basic transaction logic. pub type SignedExtraScheduler = ( @@ -61,7 +61,7 @@ pub struct SchedulerPaymentExecutor; -impl +impl DispatchCall for SchedulerPaymentExecutor where ::RuntimeCall: Member @@ -71,13 +71,13 @@ + From>, SelfContainedSignedInfo: Send + Sync + 'static, RuntimeCall: From<::RuntimeCall> - + From<::RuntimeCall> + + From<::RuntimeCall> + SelfContainedCall, sp_runtime::AccountId32: From<::AccountId>, { fn dispatch_call( signer: Option<::AccountId>, - call: ::RuntimeCall, + call: ::RuntimeCall, ) -> Result< Result>, TransactionValidityError, @@ -105,52 +105,99 @@ extrinsic.apply::(&dispatch_info, len) } +} - fn reserve_balance( - id: [u8; 16], - sponsor: ::AccountId, - call: ::RuntimeCall, - count: u32, - ) -> Result<(), DispatchError> { - let dispatch_info = call.get_dispatch_info(); - let weight: Balance = - SponsorshipChargeTransactionPayment::traditional_fee(0, &dispatch_info, 0) - .saturating_mul(count.into()); - >::reserve_named( - &id, - &(sponsor.into()), - weight, - ) - } +// impl +// DispatchCall for SchedulerPaymentExecutor +// where +// ::Call: Member +// + Dispatchable +// + SelfContainedCall +// + GetDispatchInfo +// + From>, +// SelfContainedSignedInfo: Send + Sync + 'static, +// Call: From<::Call> +// + From<::Call> +// + SelfContainedCall, +// sp_runtime::AccountId32: From<::AccountId>, +// { +// fn dispatch_call( +// signer: Option<::AccountId>, +// call: ::Call, +// ) -> Result< +// Result>, +// TransactionValidityError, +// > { +// let dispatch_info = call.get_dispatch_info(); +// let len = call.encoded_size(); - fn pay_for_call( - id: [u8; 16], - sponsor: ::AccountId, - call: ::RuntimeCall, - ) -> Result { - let dispatch_info = call.get_dispatch_info(); - let weight: Balance = - SponsorshipChargeTransactionPayment::traditional_fee(0, &dispatch_info, 0); - Ok( - >::unreserve_named( - &id, - &(sponsor.into()), - weight, - ), - ) - } +// let signed = match signer { +// Some(signer) => fp_self_contained::CheckedSignature::Signed( +// signer.clone().into(), +// get_signed_extras(signer.into()), +// ), +// None => fp_self_contained::CheckedSignature::Unsigned, +// }; - fn cancel_reserve( - id: [u8; 16], - sponsor: ::AccountId, - ) -> Result { - Ok( - >::unreserve_named( - &id, - &(sponsor.into()), - u128::MAX, - ), - ) - } -} +// let extrinsic = fp_self_contained::CheckedExtrinsic::< +// AccountId, +// Call, +// SignedExtraScheduler, +// SelfContainedSignedInfo, +// > { +// signed, +// function: call.into(), +// }; + +// extrinsic.apply::(&dispatch_info, len) +// } + +// fn reserve_balance( +// id: [u8; 16], +// sponsor: ::AccountId, +// call: ::Call, +// count: u32, +// ) -> Result<(), DispatchError> { +// let dispatch_info = call.get_dispatch_info(); +// let weight: Balance = +// SponsorshipChargeTransactionPayment::traditional_fee(0, &dispatch_info, 0) +// .saturating_mul(count.into()); + +// >::reserve_named( +// &id, +// &(sponsor.into()), +// weight, +// ) +// } + +// fn pay_for_call( +// id: [u8; 16], +// sponsor: ::AccountId, +// call: ::Call, +// ) -> Result { +// let dispatch_info = call.get_dispatch_info(); +// let weight: Balance = +// SponsorshipChargeTransactionPayment::traditional_fee(0, &dispatch_info, 0); +// Ok( +// >::unreserve_named( +// &id, +// &(sponsor.into()), +// weight, +// ), +// ) +// } + +// fn cancel_reserve( +// id: [u8; 16], +// sponsor: ::AccountId, +// ) -> Result { +// Ok( +// >::unreserve_named( +// &id, +// &(sponsor.into()), +// u128::MAX, +// ), +// ) +// } +// } -- gitstuff