git.delta.rocks / unique-network / refs/commits / 83f1741d1d73

difftreelog

fix make schedulerv2 take fees

Daniel Shiposha2022-10-20parent: #c6878e8.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, maintenance};28use crate::{Runtime, RuntimeCall, RuntimeOrigin, Balances, maintenance};
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 = (
6161
62pub struct SchedulerPaymentExecutor;62pub struct SchedulerPaymentExecutor;
6363
64impl<T: frame_system::Config + pallet_unique_scheduler::Config, SelfContainedSignedInfo>64impl<T: frame_system::Config + pallet_unique_scheduler_v2::Config, SelfContainedSignedInfo>
65 DispatchCall<T, SelfContainedSignedInfo> for SchedulerPaymentExecutor65 DispatchCall<T, SelfContainedSignedInfo> for SchedulerPaymentExecutor
66where66where
67 <T as frame_system::Config>::RuntimeCall: Member67 <T as frame_system::Config>::RuntimeCall: Member
71 + From<frame_system::Call<Runtime>>,71 + From<frame_system::Call<Runtime>>,
72 SelfContainedSignedInfo: Send + Sync + 'static,72 SelfContainedSignedInfo: Send + Sync + 'static,
73 RuntimeCall: From<<T as frame_system::Config>::RuntimeCall>73 RuntimeCall: From<<T as frame_system::Config>::RuntimeCall>
74 + From<<T as pallet_unique_scheduler::Config>::RuntimeCall>74 + From<<T as pallet_unique_scheduler_v2::Config>::RuntimeCall>
75 + SelfContainedCall<SignedInfo = SelfContainedSignedInfo>,75 + SelfContainedCall<SignedInfo = SelfContainedSignedInfo>,
76 sp_runtime::AccountId32: From<<T as frame_system::Config>::AccountId>,76 sp_runtime::AccountId32: From<<T as frame_system::Config>::AccountId>,
77{77{
78 fn dispatch_call(78 fn dispatch_call(
79 signer: Option<<T as frame_system::Config>::AccountId>,79 signer: Option<<T as frame_system::Config>::AccountId>,
80 call: <T as pallet_unique_scheduler::Config>::RuntimeCall,80 call: <T as pallet_unique_scheduler_v2::Config>::RuntimeCall,
81 ) -> Result<81 ) -> Result<
82 Result<PostDispatchInfo, DispatchErrorWithPostInfo<PostDispatchInfo>>,82 Result<PostDispatchInfo, DispatchErrorWithPostInfo<PostDispatchInfo>>,
83 TransactionValidityError,83 TransactionValidityError,
106 extrinsic.apply::<Runtime>(&dispatch_info, len)106 extrinsic.apply::<Runtime>(&dispatch_info, len)
107 }107 }
108
109 fn reserve_balance(
110 id: [u8; 16],
111 sponsor: <T as frame_system::Config>::AccountId,
112 call: <T as pallet_unique_scheduler::Config>::RuntimeCall,
113 count: u32,
114 ) -> Result<(), DispatchError> {
115 let dispatch_info = call.get_dispatch_info();
116 let weight: Balance =
117 SponsorshipChargeTransactionPayment::traditional_fee(0, &dispatch_info, 0)
118 .saturating_mul(count.into());
119
120 <Balances as NamedReservableCurrency<AccountId>>::reserve_named(
121 &id,
122 &(sponsor.into()),
123 weight,
124 )
125 }
126
127 fn pay_for_call(
128 id: [u8; 16],
129 sponsor: <T as frame_system::Config>::AccountId,
130 call: <T as pallet_unique_scheduler::Config>::RuntimeCall,
131 ) -> Result<u128, DispatchError> {
132 let dispatch_info = call.get_dispatch_info();
133 let weight: Balance =
134 SponsorshipChargeTransactionPayment::traditional_fee(0, &dispatch_info, 0);
135 Ok(
136 <Balances as NamedReservableCurrency<AccountId>>::unreserve_named(
137 &id,
138 &(sponsor.into()),
139 weight,
140 ),
141 )
142 }
143
144 fn cancel_reserve(
145 id: [u8; 16],
146 sponsor: <T as frame_system::Config>::AccountId,
147 ) -> Result<u128, DispatchError> {
148 Ok(
149 <Balances as NamedReservableCurrency<AccountId>>::unreserve_named(
150 &id,
151 &(sponsor.into()),
152 u128::MAX,
153 ),
154 )
155 }
156}108}
109
110
111// impl<T: frame_system::Config + pallet_unique_scheduler::Config, SelfContainedSignedInfo>
112// DispatchCall<T, SelfContainedSignedInfo> for SchedulerPaymentExecutor
113// where
114// <T as frame_system::Config>::Call: Member
115// + Dispatchable<Origin = Origin, Info = DispatchInfo>
116// + SelfContainedCall<SignedInfo = SelfContainedSignedInfo>
117// + GetDispatchInfo
118// + From<frame_system::Call<Runtime>>,
119// SelfContainedSignedInfo: Send + Sync + 'static,
120// Call: From<<T as frame_system::Config>::Call>
121// + From<<T as pallet_unique_scheduler::Config>::Call>
122// + SelfContainedCall<SignedInfo = SelfContainedSignedInfo>,
123// sp_runtime::AccountId32: From<<T as frame_system::Config>::AccountId>,
124// {
125// fn dispatch_call(
126// signer: Option<<T as frame_system::Config>::AccountId>,
127// call: <T as pallet_unique_scheduler::Config>::Call,
128// ) -> Result<
129// Result<PostDispatchInfo, DispatchErrorWithPostInfo<PostDispatchInfo>>,
130// TransactionValidityError,
131// > {
132// let dispatch_info = call.get_dispatch_info();
133// let len = call.encoded_size();
134
135// let signed = match signer {
136// Some(signer) => fp_self_contained::CheckedSignature::Signed(
137// signer.clone().into(),
138// get_signed_extras(signer.into()),
139// ),
140// None => fp_self_contained::CheckedSignature::Unsigned,
141// };
142
143// let extrinsic = fp_self_contained::CheckedExtrinsic::<
144// AccountId,
145// Call,
146// SignedExtraScheduler,
147// SelfContainedSignedInfo,
148// > {
149// signed,
150// function: call.into(),
151// };
152
153// extrinsic.apply::<Runtime>(&dispatch_info, len)
154// }
155
156// fn reserve_balance(
157// id: [u8; 16],
158// sponsor: <T as frame_system::Config>::AccountId,
159// call: <T as pallet_unique_scheduler::Config>::Call,
160// count: u32,
161// ) -> Result<(), DispatchError> {
162// let dispatch_info = call.get_dispatch_info();
163// let weight: Balance =
164// SponsorshipChargeTransactionPayment::traditional_fee(0, &dispatch_info, 0)
165// .saturating_mul(count.into());
166
167// <Balances as NamedReservableCurrency<AccountId>>::reserve_named(
168// &id,
169// &(sponsor.into()),
170// weight,
171// )
172// }
173
174// fn pay_for_call(
175// id: [u8; 16],
176// sponsor: <T as frame_system::Config>::AccountId,
177// call: <T as pallet_unique_scheduler::Config>::Call,
178// ) -> Result<u128, DispatchError> {
179// let dispatch_info = call.get_dispatch_info();
180// let weight: Balance =
181// SponsorshipChargeTransactionPayment::traditional_fee(0, &dispatch_info, 0);
182// Ok(
183// <Balances as NamedReservableCurrency<AccountId>>::unreserve_named(
184// &id,
185// &(sponsor.into()),
186// weight,
187// ),
188// )
189// }
190
191// fn cancel_reserve(
192// id: [u8; 16],
193// sponsor: <T as frame_system::Config>::AccountId,
194// ) -> Result<u128, DispatchError> {
195// Ok(
196// <Balances as NamedReservableCurrency<AccountId>>::unreserve_named(
197// &id,
198// &(sponsor.into()),
199// u128::MAX,
200// ),
201// )
202// }
203// }
157204