git.delta.rocks / unique-network / refs/commits / ea4aa986829e

difftreelog

fix make schedulerv2 take fees

Daniel Shiposha2022-10-20parent: #94baf85.patch.diff
in: master

3 files changed

modifiedpallets/scheduler-v2/src/lib.rsdiffbeforeafterboth
--- 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<AccountId> {
+	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 = <Self as Config>::Origin, PostInfo = PostDispatchInfo>
+			+ UnfilteredDispatchable<Origin = <Self as system::Config>::Origin>
 			+ GetDispatchInfo
 			+ From<system::Call<Self>>;
 
@@ -320,7 +328,10 @@
 		type MaximumWeight: Get<Weight>;
 
 		/// Required origin to schedule or cancel calls.
-		type ScheduleOrigin: EnsureOrigin<<Self as system::Config>::Origin>;
+		type ScheduleOrigin: EnsureOrigin<
+			<Self as system::Config>::Origin,
+			Success = ScheduledEnsureOriginSuccess<Self::AccountId>,
+		>;
 
 		/// 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<Self>;
+
+		/// The helper type used for custom transaction fee logic.
+		type CallExecutor: DispatchCall<Self, H160>;
 	}
 
 	#[pallet::storage]
@@ -726,6 +740,18 @@
 }
 use ServiceTaskError::*;
 
+/// A Scheduler-Runtime interface for finer payment handling.
+pub trait DispatchCall<T: frame_system::Config + Config, SelfContainedSignedInfo> {
+	/// Resolve the call dispatch, including any post-dispatch operations.
+	fn dispatch_call(
+		signer: Option<T::AccountId>,
+		function: <T as Config>::Call,
+	) -> Result<
+		Result<PostDispatchInfo, DispatchErrorWithPostInfo<PostDispatchInfo>>,
+		TransactionValidityError,
+	>;
+}
+
 impl<T: Config> Pallet<T> {
 	/// 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 =
+		// 	<<T as Config>::Origin as From<T::PalletsOrigin>>::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);
modifiedruntime/common/config/pallets/scheduler.rsdiffbeforeafterboth
--- 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;
 }
modifiedruntime/common/scheduler.rsdiffbeforeafterboth
28use crate::{Runtime, RuntimeCall, RuntimeOrigin, Balances};28use crate::{Runtime, RuntimeCall, RuntimeOrigin, Balances};
29use up_common::types::{AccountId, Balance};29use up_common::types::{AccountId, Balance};
30use fp_self_contained::SelfContainedCall;30use fp_self_contained::SelfContainedCall;
31use pallet_unique_scheduler::DispatchCall;31use pallet_unique_scheduler_v2::DispatchCall;
32use pallet_transaction_payment::ChargeTransactionPayment;32use pallet_transaction_payment::ChargeTransactionPayment;
3333
34type SponsorshipChargeTransactionPayment =34// type SponsorshipChargeTransactionPayment =
35 pallet_charge_transaction::ChargeTransactionPayment<Runtime>;35// pallet_charge_transaction::ChargeTransactionPayment<Runtime>;
3636
37/// The SignedExtension to the basic transaction logic.37/// The SignedExtension to the basic transaction logic.
38pub type SignedExtraScheduler = (38pub type SignedExtraScheduler = (
5959
60pub struct SchedulerPaymentExecutor;60pub struct SchedulerPaymentExecutor;
6161
62impl<T: frame_system::Config + pallet_unique_scheduler::Config, SelfContainedSignedInfo>62impl<T: frame_system::Config + pallet_unique_scheduler_v2::Config, SelfContainedSignedInfo>
63 DispatchCall<T, SelfContainedSignedInfo> for SchedulerPaymentExecutor63 DispatchCall<T, SelfContainedSignedInfo> for SchedulerPaymentExecutor
64where64where
65 <T as frame_system::Config>::RuntimeCall: Member65 <T as frame_system::Config>::RuntimeCall: Member
69 + From<frame_system::Call<Runtime>>,69 + From<frame_system::Call<Runtime>>,
70 SelfContainedSignedInfo: Send + Sync + 'static,70 SelfContainedSignedInfo: Send + Sync + 'static,
71 RuntimeCall: From<<T as frame_system::Config>::RuntimeCall>71 RuntimeCall: From<<T as frame_system::Config>::RuntimeCall>
72 + From<<T as pallet_unique_scheduler::Config>::RuntimeCall>72 + From<<T as pallet_unique_scheduler_v2::Config>::RuntimeCall>
73 + SelfContainedCall<SignedInfo = SelfContainedSignedInfo>,73 + SelfContainedCall<SignedInfo = SelfContainedSignedInfo>,
74 sp_runtime::AccountId32: From<<T as frame_system::Config>::AccountId>,74 sp_runtime::AccountId32: From<<T as frame_system::Config>::AccountId>,
75{75{
76 fn dispatch_call(76 fn dispatch_call(
77 signer: Option<<T as frame_system::Config>::AccountId>,77 signer: Option<<T as frame_system::Config>::AccountId>,
78 call: <T as pallet_unique_scheduler::Config>::RuntimeCall,78 call: <T as pallet_unique_scheduler_v2::Config>::RuntimeCall,
79 ) -> Result<79 ) -> Result<
80 Result<PostDispatchInfo, DispatchErrorWithPostInfo<PostDispatchInfo>>,80 Result<PostDispatchInfo, DispatchErrorWithPostInfo<PostDispatchInfo>>,
81 TransactionValidityError,81 TransactionValidityError,
104 extrinsic.apply::<Runtime>(&dispatch_info, len)104 extrinsic.apply::<Runtime>(&dispatch_info, len)
105 }105 }
106
107 fn reserve_balance(
108 id: [u8; 16],
109 sponsor: <T as frame_system::Config>::AccountId,
110 call: <T as pallet_unique_scheduler::Config>::RuntimeCall,
111 count: u32,
112 ) -> Result<(), DispatchError> {
113 let dispatch_info = call.get_dispatch_info();
114 let weight: Balance =
115 SponsorshipChargeTransactionPayment::traditional_fee(0, &dispatch_info, 0)
116 .saturating_mul(count.into());
117
118 <Balances as NamedReservableCurrency<AccountId>>::reserve_named(
119 &id,
120 &(sponsor.into()),
121 weight,
122 )
123 }
124
125 fn pay_for_call(
126 id: [u8; 16],
127 sponsor: <T as frame_system::Config>::AccountId,
128 call: <T as pallet_unique_scheduler::Config>::RuntimeCall,
129 ) -> Result<u128, DispatchError> {
130 let dispatch_info = call.get_dispatch_info();
131 let weight: Balance =
132 SponsorshipChargeTransactionPayment::traditional_fee(0, &dispatch_info, 0);
133 Ok(
134 <Balances as NamedReservableCurrency<AccountId>>::unreserve_named(
135 &id,
136 &(sponsor.into()),
137 weight,
138 ),
139 )
140 }
141
142 fn cancel_reserve(
143 id: [u8; 16],
144 sponsor: <T as frame_system::Config>::AccountId,
145 ) -> Result<u128, DispatchError> {
146 Ok(
147 <Balances as NamedReservableCurrency<AccountId>>::unreserve_named(
148 &id,
149 &(sponsor.into()),
150 u128::MAX,
151 ),
152 )
153 }
154}106}
107
108
109// impl<T: frame_system::Config + pallet_unique_scheduler::Config, SelfContainedSignedInfo>
110// DispatchCall<T, SelfContainedSignedInfo> for SchedulerPaymentExecutor
111// where
112// <T as frame_system::Config>::Call: Member
113// + Dispatchable<Origin = Origin, Info = DispatchInfo>
114// + SelfContainedCall<SignedInfo = SelfContainedSignedInfo>
115// + GetDispatchInfo
116// + From<frame_system::Call<Runtime>>,
117// SelfContainedSignedInfo: Send + Sync + 'static,
118// Call: From<<T as frame_system::Config>::Call>
119// + From<<T as pallet_unique_scheduler::Config>::Call>
120// + SelfContainedCall<SignedInfo = SelfContainedSignedInfo>,
121// sp_runtime::AccountId32: From<<T as frame_system::Config>::AccountId>,
122// {
123// fn dispatch_call(
124// signer: Option<<T as frame_system::Config>::AccountId>,
125// call: <T as pallet_unique_scheduler::Config>::Call,
126// ) -> Result<
127// Result<PostDispatchInfo, DispatchErrorWithPostInfo<PostDispatchInfo>>,
128// TransactionValidityError,
129// > {
130// let dispatch_info = call.get_dispatch_info();
131// let len = call.encoded_size();
132
133// let signed = match signer {
134// Some(signer) => fp_self_contained::CheckedSignature::Signed(
135// signer.clone().into(),
136// get_signed_extras(signer.into()),
137// ),
138// None => fp_self_contained::CheckedSignature::Unsigned,
139// };
140
141// let extrinsic = fp_self_contained::CheckedExtrinsic::<
142// AccountId,
143// Call,
144// SignedExtraScheduler,
145// SelfContainedSignedInfo,
146// > {
147// signed,
148// function: call.into(),
149// };
150
151// extrinsic.apply::<Runtime>(&dispatch_info, len)
152// }
153
154// fn reserve_balance(
155// id: [u8; 16],
156// sponsor: <T as frame_system::Config>::AccountId,
157// call: <T as pallet_unique_scheduler::Config>::Call,
158// count: u32,
159// ) -> Result<(), DispatchError> {
160// let dispatch_info = call.get_dispatch_info();
161// let weight: Balance =
162// SponsorshipChargeTransactionPayment::traditional_fee(0, &dispatch_info, 0)
163// .saturating_mul(count.into());
164
165// <Balances as NamedReservableCurrency<AccountId>>::reserve_named(
166// &id,
167// &(sponsor.into()),
168// weight,
169// )
170// }
171
172// fn pay_for_call(
173// id: [u8; 16],
174// sponsor: <T as frame_system::Config>::AccountId,
175// call: <T as pallet_unique_scheduler::Config>::Call,
176// ) -> Result<u128, DispatchError> {
177// let dispatch_info = call.get_dispatch_info();
178// let weight: Balance =
179// SponsorshipChargeTransactionPayment::traditional_fee(0, &dispatch_info, 0);
180// Ok(
181// <Balances as NamedReservableCurrency<AccountId>>::unreserve_named(
182// &id,
183// &(sponsor.into()),
184// weight,
185// ),
186// )
187// }
188
189// fn cancel_reserve(
190// id: [u8; 16],
191// sponsor: <T as frame_system::Config>::AccountId,
192// ) -> Result<u128, DispatchError> {
193// Ok(
194// <Balances as NamedReservableCurrency<AccountId>>::unreserve_named(
195// &id,
196// &(sponsor.into()),
197// u128::MAX,
198// ),
199// )
200// }
201// }
155202