1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
//! # Authority
//! A module to provide features for governance including dispatch method on
//! behalf of other accounts and schedule dispatchables.
//!
//! - [`Config`](./trait.Config.html)
//! - [`Call`](./enum.Call.html)
//! - [`Module`](./struct.Module.html)
//!
//! ## Overview
//!
//! Two functionalities are provided by this module:
//! - schedule a dispatchable
//! - dispatch method with on behalf of other origins
//!
//! NOTE:
//!
//! In order to derive a feasible max encoded len for `DelayedOrigin`, it is
//! assumed that there are no nested `DelayedOrigin` in `OriginCaller`.
//! In practice, this means there should not be nested `schedule_dispatch`.
//! Otherwise the proof size estimation may not be accurate.

#![cfg_attr(not(feature = "std"), no_std)]
// Disable the following three lints since they originate from an external macro
#![allow(clippy::string_lit_as_bytes)]
#![allow(clippy::boxed_local)]
#![allow(clippy::borrowed_box)]
#![allow(clippy::unused_unit)]

use frame_support::{
	dispatch::PostDispatchInfo,
	dispatch::{DispatchClass, GetDispatchInfo, Pays},
	pallet_prelude::*,
	traits::{
		schedule::{DispatchTime, Priority},
		EitherOfDiverse, EnsureOrigin, Get, IsType, OriginTrait,
	},
};
use frame_system::{pallet_prelude::*, EnsureRoot, EnsureSigned};
use parity_scale_codec::MaxEncodedLen;
use scale_info::TypeInfo;
use sp_core::defer;
use sp_runtime::{
	traits::{CheckedSub, Dispatchable, Hash, Saturating},
	ArithmeticError, DispatchError, DispatchResult, Either, RuntimeDebug,
};
use sp_std::prelude::*;

// Todo: Switch to current v3 api: https://github.com/open-web3-stack/open-runtime-module-library/issues/995
#[allow(deprecated)]
use frame_support::traits::schedule::v1::Named as ScheduleNamed;

mod mock;
mod tests;
mod weights;

pub use weights::WeightInfo;

/// A delayed origin. Can only be dispatched via `dispatch_as` with a delay.
#[derive(PartialEq, Eq, Clone, RuntimeDebug, Encode, Decode, TypeInfo)]
pub struct DelayedOrigin<BlockNumber, PalletsOrigin> {
	/// Number of blocks that this call have been delayed.
	pub(crate) delay: BlockNumber,
	/// The initial origin.
	pub(crate) origin: Box<PalletsOrigin>,
}

#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
impl<BlockNumber, PalletsOrigin> DelayedOrigin<BlockNumber, PalletsOrigin> {
	pub fn new(delay: BlockNumber, origin: Box<PalletsOrigin>) -> Self {
		Self { delay, origin }
	}
}

#[cfg(feature = "std")]
mod helper {
	use std::cell::RefCell;

	thread_local! {
		static NESTED_MAX_ENCODED_LEN: RefCell<bool> = RefCell::new(false);
	}

	pub fn set_nested_max_encoded_len(val: bool) {
		NESTED_MAX_ENCODED_LEN.with(|v| *v.borrow_mut() = val);
	}

	pub fn nested_max_encoded_len() -> bool {
		NESTED_MAX_ENCODED_LEN.with(|v| *v.borrow())
	}
}

#[cfg(not(feature = "std"))]
mod helper {
	static mut NESTED_MAX_ENCODED_LEN: bool = false;

	pub fn set_nested_max_encoded_len(val: bool) {
		unsafe {
			NESTED_MAX_ENCODED_LEN = val;
		}
	}

	pub fn nested_max_encoded_len() -> bool {
		unsafe { NESTED_MAX_ENCODED_LEN }
	}
}

// Manual implementation to break recursive calls of `MaxEncodedLen` as the
// implementation of `PalletsOrigin::max_encoded_len` will also call
// `MaxEncodedLen` on `DelayedOrigin`. This is only safe if there are no nested
// `DelayedOrigin`. It is only possible to construct a `DelayedOrigin` via
// `schedule_dispatch` which is a protected call only accessible via governance.
impl<BlockNumber: MaxEncodedLen, PalletsOrigin: MaxEncodedLen> MaxEncodedLen
	for DelayedOrigin<BlockNumber, PalletsOrigin>
{
	fn max_encoded_len() -> usize {
		if helper::nested_max_encoded_len() {
			return 0;
		}

		helper::set_nested_max_encoded_len(true);
		defer!(helper::set_nested_max_encoded_len(false));

		BlockNumber::max_encoded_len() + PalletsOrigin::max_encoded_len()
	}
}

/// Ensure the origin have a minimum amount of delay.
pub struct EnsureDelayed<Delay, Inner, BlockNumber, PalletsOrigin>(
	sp_std::marker::PhantomData<(Delay, Inner, BlockNumber, PalletsOrigin)>,
);
impl<
		PalletsOrigin: Into<O>,
		O: Into<Result<DelayedOrigin<BlockNumber, PalletsOrigin>, O>> + From<DelayedOrigin<BlockNumber, PalletsOrigin>>,
		Delay: Get<BlockNumber>,
		Inner: EnsureOrigin<O>,
		BlockNumber: PartialOrd,
	> EnsureOrigin<O> for EnsureDelayed<Delay, Inner, BlockNumber, PalletsOrigin>
{
	type Success = Inner::Success;

	fn try_origin(o: O) -> Result<Self::Success, O> {
		o.into().and_then(|delayed_origin| {
			if delayed_origin.delay >= Delay::get() {
				let pallets_origin = *delayed_origin.origin;
				Inner::try_origin(pallets_origin.into())
			} else {
				Err(delayed_origin.into())
			}
		})
	}

	#[cfg(feature = "runtime-benchmarks")]
	fn try_successful_origin() -> Result<O, ()> {
		unimplemented!()
	}
}

/// Config for orml-authority
pub trait AuthorityConfig<Origin, PalletsOrigin, BlockNumber> {
	/// Check if the `origin` is allowed to schedule a dispatchable call
	/// with a given `priority`.
	fn check_schedule_dispatch(origin: Origin, priority: Priority) -> DispatchResult;
	/// Check if the `origin` is allow to fast track a scheduled task that
	/// initially created by `initial_origin`. `new_delay` is number of
	/// blocks this dispatchable will be dispatched from now after fast
	/// track.
	fn check_fast_track_schedule(
		origin: Origin,
		initial_origin: &PalletsOrigin,
		new_delay: BlockNumber,
	) -> DispatchResult;
	/// Check if the `origin` is allow to delay a scheduled task that
	/// initially created by `initial_origin`.
	fn check_delay_schedule(origin: Origin, initial_origin: &PalletsOrigin) -> DispatchResult;
	/// Check if the `origin` is allow to cancel a scheduled task that
	/// initially created by `initial_origin`.
	fn check_cancel_schedule(origin: Origin, initial_origin: &PalletsOrigin) -> DispatchResult;
}

/// Represent an origin that can be dispatched by other origins with
/// permission check.
pub trait AsOriginId<Origin, PalletsOrigin> {
	/// Convert into `PalletsOrigin`
	fn into_origin(self) -> PalletsOrigin;
	/// Check if the `origin` is allow to dispatch call on behalf of this
	/// origin.
	fn check_dispatch_from(&self, origin: Origin) -> DispatchResult;
}

/// The schedule task index type.
pub type ScheduleTaskIndex = u32;

pub use module::*;

#[frame_support::pallet]
pub mod module {
	use super::*;

	/// Origin for the authority module.
	#[pallet::origin]
	pub type Origin<T> = DelayedOrigin<BlockNumberFor<T>, <T as Config>::PalletsOrigin>;
	pub(crate) type CallOf<T> = <T as Config>::RuntimeCall;

	#[pallet::config]
	pub trait Config: frame_system::Config {
		/// The overarching event type.
		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

		/// The outer origin type.
		type RuntimeOrigin: From<DelayedOrigin<BlockNumberFor<Self>, <Self as Config>::PalletsOrigin>>
			+ IsType<<Self as frame_system::Config>::RuntimeOrigin>
			+ OriginTrait<PalletsOrigin = Self::PalletsOrigin>;

		/// The caller origin, overarching type of all pallets origins.
		type PalletsOrigin: Parameter + Into<<Self as frame_system::Config>::RuntimeOrigin>;

		/// The aggregated call type.
		type RuntimeCall: Parameter
			+ Dispatchable<RuntimeOrigin = <Self as frame_system::Config>::RuntimeOrigin, PostInfo = PostDispatchInfo>
			+ GetDispatchInfo;

		/// The Scheduler.
		#[allow(deprecated)]
		type Scheduler: ScheduleNamed<BlockNumberFor<Self>, <Self as Config>::RuntimeCall, Self::PalletsOrigin>;

		/// The type represent origin that can be dispatched by other origins.
		type AsOriginId: Parameter + AsOriginId<<Self as frame_system::Config>::RuntimeOrigin, Self::PalletsOrigin>;

		/// Additional permission config.
		type AuthorityConfig: AuthorityConfig<
			<Self as frame_system::Config>::RuntimeOrigin,
			Self::PalletsOrigin,
			BlockNumberFor<Self>,
		>;

		/// Weight information for extrinsics in this module.
		type WeightInfo: WeightInfo;
	}

	#[pallet::error]
	pub enum Error<T> {
		/// Failed to schedule a task.
		FailedToSchedule,
		/// Failed to cancel a task.
		FailedToCancel,
		/// Failed to fast track a task.
		FailedToFastTrack,
		/// Failed to delay a task.
		FailedToDelay,
		/// Call is not authorized.
		CallNotAuthorized,
		/// Triggering the call is not permitted.
		TriggerCallNotPermitted,
		/// Call weight bound is wrong.
		WrongCallWeightBound,
	}

	#[pallet::event]
	#[pallet::generate_deposit(fn deposit_event)]
	pub enum Event<T: Config> {
		/// A call is dispatched.
		Dispatched { result: DispatchResult },
		/// A call is scheduled.
		Scheduled {
			origin: T::PalletsOrigin,
			index: ScheduleTaskIndex,
		},
		/// A scheduled call is fast tracked.
		FastTracked {
			origin: T::PalletsOrigin,
			index: ScheduleTaskIndex,
			when: BlockNumberFor<T>,
		},
		/// A scheduled call is delayed.
		Delayed {
			origin: T::PalletsOrigin,
			index: ScheduleTaskIndex,
			when: BlockNumberFor<T>,
		},
		/// A scheduled call is cancelled.
		Cancelled {
			origin: T::PalletsOrigin,
			index: ScheduleTaskIndex,
		},
		/// A call is authorized.
		AuthorizedCall {
			hash: T::Hash,
			caller: Option<T::AccountId>,
		},
		/// An authorized call was removed.
		RemovedAuthorizedCall { hash: T::Hash },
		/// An authorized call was triggered.
		TriggeredCallBy { hash: T::Hash, caller: T::AccountId },
	}

	#[pallet::storage]
	#[pallet::getter(fn next_task_index)]
	pub type NextTaskIndex<T: Config> = StorageValue<_, ScheduleTaskIndex, ValueQuery>;

	#[pallet::storage]
	#[pallet::getter(fn saved_calls)]
	pub type SavedCalls<T: Config> = StorageMap<_, Identity, T::Hash, (CallOf<T>, Option<T::AccountId>), OptionQuery>;

	#[pallet::pallet]
	#[pallet::without_storage_info]
	pub struct Pallet<T>(_);

	#[pallet::hooks]
	impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}

	#[pallet::call]
	impl<T: Config> Pallet<T> {
		/// Dispatch a dispatchable on behalf of other origin
		#[pallet::call_index(0)]
		#[pallet::weight({
			let info = call.get_dispatch_info();
			(T::WeightInfo::dispatch_as().saturating_add(info.weight), info.class)
		})]
		pub fn dispatch_as(origin: OriginFor<T>, as_origin: T::AsOriginId, call: Box<CallOf<T>>) -> DispatchResult {
			as_origin.check_dispatch_from(origin)?;

			let e = call.dispatch(as_origin.into_origin().into());

			Self::deposit_event(Event::Dispatched {
				result: e.map(|_| ()).map_err(|e| e.error),
			});
			Ok(())
		}

		/// Schedule a dispatchable to be dispatched at later block.
		/// This is the only way to dispatch a call with `DelayedOrigin`.
		#[pallet::call_index(1)]
		#[pallet::weight(T::WeightInfo::schedule_dispatch_without_delay())]
		pub fn schedule_dispatch(
			origin: OriginFor<T>,
			when: DispatchTime<BlockNumberFor<T>>,
			priority: Priority,
			with_delayed_origin: bool,
			call: Box<CallOf<T>>,
		) -> DispatchResult {
			T::AuthorityConfig::check_schedule_dispatch(origin.clone(), priority)?;

			let id = NextTaskIndex::<T>::mutate(|id| -> sp_std::result::Result<ScheduleTaskIndex, DispatchError> {
				let current_id = *id;
				*id = id.checked_add(1).ok_or(ArithmeticError::Overflow)?;
				Ok(current_id)
			})?;
			let now = frame_system::Pallet::<T>::block_number();
			let delay = match when {
				DispatchTime::At(x) => x.checked_sub(&now).ok_or(ArithmeticError::Overflow)?,
				DispatchTime::After(x) => x,
			};
			let schedule_origin = if with_delayed_origin {
				let origin: <T as Config>::RuntimeOrigin = From::from(origin);
				let origin: <T as Config>::RuntimeOrigin =
					From::from(DelayedOrigin::<BlockNumberFor<T>, T::PalletsOrigin> {
						delay,
						origin: Box::new(origin.caller().clone()),
					});
				origin
			} else {
				<T as Config>::RuntimeOrigin::from(origin)
			};
			let pallets_origin = schedule_origin.caller().clone();

			#[allow(deprecated)]
			T::Scheduler::schedule_named(
				Encode::encode(&(&pallets_origin, id)),
				when,
				None,
				priority,
				pallets_origin.clone(),
				*call,
			)
			.map_err(|_| Error::<T>::FailedToSchedule)?;

			Self::deposit_event(Event::Scheduled {
				origin: pallets_origin,
				index: id,
			});
			Ok(())
		}

		/// Fast track a scheduled dispatchable.
		#[pallet::call_index(2)]
		#[pallet::weight(T::WeightInfo::fast_track_scheduled_dispatch())]
		pub fn fast_track_scheduled_dispatch(
			origin: OriginFor<T>,
			initial_origin: Box<T::PalletsOrigin>,
			task_id: ScheduleTaskIndex,
			when: DispatchTime<BlockNumberFor<T>>,
		) -> DispatchResult {
			let now = frame_system::Pallet::<T>::block_number();
			let new_delay = match when {
				DispatchTime::At(x) => x.checked_sub(&now).ok_or(ArithmeticError::Overflow)?,
				DispatchTime::After(x) => x,
			};
			let dispatch_at = match when {
				DispatchTime::At(x) => x,
				DispatchTime::After(x) => now.saturating_add(x),
			};

			T::AuthorityConfig::check_fast_track_schedule(origin, &initial_origin, new_delay)?;
			#[allow(deprecated)]
			T::Scheduler::reschedule_named((&initial_origin, task_id).encode(), when)
				.map_err(|_| Error::<T>::FailedToFastTrack)?;

			Self::deposit_event(Event::FastTracked {
				origin: *initial_origin,
				index: task_id,
				when: dispatch_at,
			});
			Ok(())
		}

		/// Delay a scheduled dispatchable.
		#[pallet::call_index(3)]
		#[pallet::weight(T::WeightInfo::delay_scheduled_dispatch())]
		pub fn delay_scheduled_dispatch(
			origin: OriginFor<T>,
			initial_origin: Box<T::PalletsOrigin>,
			task_id: ScheduleTaskIndex,
			additional_delay: BlockNumberFor<T>,
		) -> DispatchResult {
			T::AuthorityConfig::check_delay_schedule(origin, &initial_origin)?;

			#[allow(deprecated)]
			T::Scheduler::reschedule_named(
				(&initial_origin, task_id).encode(),
				DispatchTime::After(additional_delay),
			)
			.map_err(|_| Error::<T>::FailedToDelay)?;

			let now = frame_system::Pallet::<T>::block_number();
			let dispatch_at = now.saturating_add(additional_delay);

			Self::deposit_event(Event::Delayed {
				origin: *initial_origin,
				index: task_id,
				when: dispatch_at,
			});
			Ok(())
		}

		/// Cancel a scheduled dispatchable.
		#[pallet::call_index(4)]
		#[pallet::weight(T::WeightInfo::cancel_scheduled_dispatch())]
		pub fn cancel_scheduled_dispatch(
			origin: OriginFor<T>,
			initial_origin: Box<T::PalletsOrigin>,
			task_id: ScheduleTaskIndex,
		) -> DispatchResult {
			T::AuthorityConfig::check_cancel_schedule(origin, &initial_origin)?;
			#[allow(deprecated)]
			T::Scheduler::cancel_named((&initial_origin, task_id).encode()).map_err(|_| Error::<T>::FailedToCancel)?;

			Self::deposit_event(Event::Cancelled {
				origin: *initial_origin,
				index: task_id,
			});
			Ok(())
		}

		#[pallet::call_index(5)]
		#[pallet::weight(T::WeightInfo::authorize_call())]
		pub fn authorize_call(
			origin: OriginFor<T>,
			call: Box<CallOf<T>>,
			caller: Option<T::AccountId>,
		) -> DispatchResult {
			ensure_root(origin)?;
			let hash = T::Hashing::hash_of(&call);
			SavedCalls::<T>::insert(hash, (call, caller.clone()));
			Self::deposit_event(Event::AuthorizedCall { hash, caller });
			Ok(())
		}

		#[pallet::call_index(6)]
		#[pallet::weight(T::WeightInfo::remove_authorized_call())]
		pub fn remove_authorized_call(origin: OriginFor<T>, hash: T::Hash) -> DispatchResult {
			let root_or_signed =
				EitherOfDiverse::<EnsureRoot<T::AccountId>, EnsureSigned<T::AccountId>>::ensure_origin(origin)?;

			SavedCalls::<T>::try_mutate_exists(hash, |maybe_call| {
				let (_, maybe_caller) = maybe_call.take().ok_or(Error::<T>::CallNotAuthorized)?;
				match root_or_signed {
					Either::Left(_) => {} // root, do nothing
					Either::Right(who) => {
						// signed, ensure it's the caller
						let caller = maybe_caller.ok_or(Error::<T>::CallNotAuthorized)?;
						ensure!(who == caller, Error::<T>::CallNotAuthorized);
					}
				}
				Self::deposit_event(Event::RemovedAuthorizedCall { hash });
				Ok(())
			})
		}

		#[pallet::call_index(8)]
		#[pallet::weight((
			T::WeightInfo::trigger_call().saturating_add(*call_weight_bound),
			DispatchClass::Operational,
		))]
		pub fn trigger_call(
			origin: OriginFor<T>,
			hash: T::Hash,
			call_weight_bound: Weight,
		) -> DispatchResultWithPostInfo {
			let who = ensure_signed(origin)?;
			SavedCalls::<T>::try_mutate_exists(hash, |maybe_call| {
				let (call, maybe_caller) = maybe_call.take().ok_or(Error::<T>::CallNotAuthorized)?;
				if let Some(caller) = maybe_caller {
					ensure!(who == caller, Error::<T>::TriggerCallNotPermitted);
				}
				ensure!(
					call_weight_bound.all_gte(call.get_dispatch_info().weight),
					Error::<T>::WrongCallWeightBound
				);
				let result = call.dispatch(OriginFor::<T>::root());
				Self::deposit_event(Event::TriggeredCallBy { hash, caller: who });
				Self::deposit_event(Event::Dispatched {
					result: result.map(|_| ()).map_err(|e| e.error),
				});
				Ok(Pays::No.into())
			})
		}
	}
}