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
27 runtime_common::{scheduler::SchedulerPaymentExecutor, config::substrate::RuntimeBlockWeights},27 runtime_common::{scheduler::SchedulerPaymentExecutor, config::substrate::RuntimeBlockWeights},
28 Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, OriginCaller, Balances,28 Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, OriginCaller, Balances,
29};29};
30use pallet_unique_scheduler::ScheduledEnsureOriginSuccess;30use pallet_unique_scheduler_v2::ScheduledEnsureOriginSuccess;
31use up_common::types::AccountId;31use up_common::types::AccountId;
3232
33parameter_types! {33parameter_types! {
98 type MaxScheduledPerBlock = MaxScheduledPerBlock;98 type MaxScheduledPerBlock = MaxScheduledPerBlock;
99 type WeightInfo = ();99 type WeightInfo = ();
100 type Preimages = ();100 type Preimages = ();
101 type CallExecutor = SchedulerPaymentExecutor;
101}102}
102103
modifiedruntime/common/scheduler.rsdiffbeforeafterboth
--- 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<Runtime>;
+// type SponsorshipChargeTransactionPayment =
+// 	pallet_charge_transaction::ChargeTransactionPayment<Runtime>;
 
 /// The SignedExtension to the basic transaction logic.
 pub type SignedExtraScheduler = (
@@ -61,7 +61,7 @@
 
 pub struct SchedulerPaymentExecutor;
 
-impl<T: frame_system::Config + pallet_unique_scheduler::Config, SelfContainedSignedInfo>
+impl<T: frame_system::Config + pallet_unique_scheduler_v2::Config, SelfContainedSignedInfo>
 	DispatchCall<T, SelfContainedSignedInfo> for SchedulerPaymentExecutor
 where
 	<T as frame_system::Config>::RuntimeCall: Member
@@ -71,13 +71,13 @@
 		+ From<frame_system::Call<Runtime>>,
 	SelfContainedSignedInfo: Send + Sync + 'static,
 	RuntimeCall: From<<T as frame_system::Config>::RuntimeCall>
-		+ From<<T as pallet_unique_scheduler::Config>::RuntimeCall>
+		+ From<<T as pallet_unique_scheduler_v2::Config>::RuntimeCall>
 		+ SelfContainedCall<SignedInfo = SelfContainedSignedInfo>,
 	sp_runtime::AccountId32: From<<T as frame_system::Config>::AccountId>,
 {
 	fn dispatch_call(
 		signer: Option<<T as frame_system::Config>::AccountId>,
-		call: <T as pallet_unique_scheduler::Config>::RuntimeCall,
+		call: <T as pallet_unique_scheduler_v2::Config>::RuntimeCall,
 	) -> Result<
 		Result<PostDispatchInfo, DispatchErrorWithPostInfo<PostDispatchInfo>>,
 		TransactionValidityError,
@@ -105,52 +105,99 @@
 
 		extrinsic.apply::<Runtime>(&dispatch_info, len)
 	}
+}
 
-	fn reserve_balance(
-		id: [u8; 16],
-		sponsor: <T as frame_system::Config>::AccountId,
-		call: <T as pallet_unique_scheduler::Config>::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());
 
-		<Balances as NamedReservableCurrency<AccountId>>::reserve_named(
-			&id,
-			&(sponsor.into()),
-			weight,
-		)
-	}
+// impl<T: frame_system::Config + pallet_unique_scheduler::Config, SelfContainedSignedInfo>
+// 	DispatchCall<T, SelfContainedSignedInfo> for SchedulerPaymentExecutor
+// where
+// 	<T as frame_system::Config>::Call: Member
+// 		+ Dispatchable<Origin = Origin, Info = DispatchInfo>
+// 		+ SelfContainedCall<SignedInfo = SelfContainedSignedInfo>
+// 		+ GetDispatchInfo
+// 		+ From<frame_system::Call<Runtime>>,
+// 	SelfContainedSignedInfo: Send + Sync + 'static,
+// 	Call: From<<T as frame_system::Config>::Call>
+// 		+ From<<T as pallet_unique_scheduler::Config>::Call>
+// 		+ SelfContainedCall<SignedInfo = SelfContainedSignedInfo>,
+// 	sp_runtime::AccountId32: From<<T as frame_system::Config>::AccountId>,
+// {
+// 	fn dispatch_call(
+// 		signer: Option<<T as frame_system::Config>::AccountId>,
+// 		call: <T as pallet_unique_scheduler::Config>::Call,
+// 	) -> Result<
+// 		Result<PostDispatchInfo, DispatchErrorWithPostInfo<PostDispatchInfo>>,
+// 		TransactionValidityError,
+// 	> {
+// 		let dispatch_info = call.get_dispatch_info();
+// 		let len = call.encoded_size();
 
-	fn pay_for_call(
-		id: [u8; 16],
-		sponsor: <T as frame_system::Config>::AccountId,
-		call: <T as pallet_unique_scheduler::Config>::RuntimeCall,
-	) -> Result<u128, DispatchError> {
-		let dispatch_info = call.get_dispatch_info();
-		let weight: Balance =
-			SponsorshipChargeTransactionPayment::traditional_fee(0, &dispatch_info, 0);
-		Ok(
-			<Balances as NamedReservableCurrency<AccountId>>::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: <T as frame_system::Config>::AccountId,
-	) -> Result<u128, DispatchError> {
-		Ok(
-			<Balances as NamedReservableCurrency<AccountId>>::unreserve_named(
-				&id,
-				&(sponsor.into()),
-				u128::MAX,
-			),
-		)
-	}
-}
+// 		let extrinsic = fp_self_contained::CheckedExtrinsic::<
+// 			AccountId,
+// 			Call,
+// 			SignedExtraScheduler,
+// 			SelfContainedSignedInfo,
+// 		> {
+// 			signed,
+// 			function: call.into(),
+// 		};
+
+// 		extrinsic.apply::<Runtime>(&dispatch_info, len)
+// 	}
+
+// 	fn reserve_balance(
+// 		id: [u8; 16],
+// 		sponsor: <T as frame_system::Config>::AccountId,
+// 		call: <T as pallet_unique_scheduler::Config>::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());
+
+// 		<Balances as NamedReservableCurrency<AccountId>>::reserve_named(
+// 			&id,
+// 			&(sponsor.into()),
+// 			weight,
+// 		)
+// 	}
+
+// 	fn pay_for_call(
+// 		id: [u8; 16],
+// 		sponsor: <T as frame_system::Config>::AccountId,
+// 		call: <T as pallet_unique_scheduler::Config>::Call,
+// 	) -> Result<u128, DispatchError> {
+// 		let dispatch_info = call.get_dispatch_info();
+// 		let weight: Balance =
+// 			SponsorshipChargeTransactionPayment::traditional_fee(0, &dispatch_info, 0);
+// 		Ok(
+// 			<Balances as NamedReservableCurrency<AccountId>>::unreserve_named(
+// 				&id,
+// 				&(sponsor.into()),
+// 				weight,
+// 			),
+// 		)
+// 	}
+
+// 	fn cancel_reserve(
+// 		id: [u8; 16],
+// 		sponsor: <T as frame_system::Config>::AccountId,
+// 	) -> Result<u128, DispatchError> {
+// 		Ok(
+// 			<Balances as NamedReservableCurrency<AccountId>>::unreserve_named(
+// 				&id,
+// 				&(sponsor.into()),
+// 				u128::MAX,
+// 			),
+// 		)
+// 	}
+// }