1#[allow(deprecated)]
10use super::PreimageProvider;
11use alloc::vec::Vec;
12use codec::{Codec, Decode, DecodeWithMemTracking, Encode, EncodeLike, MaxEncodedLen};
13use core::{fmt::Debug, result::Result};
14use scale_info::TypeInfo;
15use subsoil::runtime::{traits::Saturating, DispatchError};
16
17pub type Period<BlockNumber> = (BlockNumber, u32);
21
22pub type Priority = u8;
25
26#[derive(
28 Encode,
29 Decode,
30 DecodeWithMemTracking,
31 Copy,
32 Clone,
33 PartialEq,
34 Eq,
35 Debug,
36 TypeInfo,
37 MaxEncodedLen,
38)]
39pub enum DispatchTime<BlockNumber> {
40 At(BlockNumber),
42 After(BlockNumber),
44}
45
46impl<BlockNumber: Saturating + Copy> DispatchTime<BlockNumber> {
47 pub fn evaluate(&self, since: BlockNumber) -> BlockNumber {
48 match &self {
49 Self::At(m) => *m,
50 Self::After(m) => m.saturating_add(since),
51 }
52 }
53}
54
55pub const HIGHEST_PRIORITY: Priority = 0;
58pub const HARD_DEADLINE: Priority = 63;
61pub const LOWEST_PRIORITY: Priority = 255;
63
64#[derive(Clone, Eq, PartialEq, Encode, Decode, Debug, TypeInfo, MaxEncodedLen)]
66pub enum MaybeHashed<T, Hash> {
67 Value(T),
69 Hash(Hash),
71}
72
73impl<T, H> From<T> for MaybeHashed<T, H> {
74 fn from(t: T) -> Self {
75 MaybeHashed::Value(t)
76 }
77}
78
79#[derive(Clone, Eq, PartialEq, Encode, Decode, Debug, TypeInfo, MaxEncodedLen)]
81pub enum LookupError {
82 Unknown,
84 BadFormat,
86}
87
88impl<T: Decode, H> MaybeHashed<T, H> {
89 pub fn as_value(&self) -> Option<&T> {
90 match &self {
91 Self::Value(c) => Some(c),
92 Self::Hash(_) => None,
93 }
94 }
95
96 pub fn as_hash(&self) -> Option<&H> {
97 match &self {
98 Self::Value(_) => None,
99 Self::Hash(h) => Some(h),
100 }
101 }
102
103 pub fn ensure_requested<P: PreimageProvider<H>>(&self) {
104 match &self {
105 Self::Value(_) => (),
106 Self::Hash(hash) => P::request_preimage(hash),
107 }
108 }
109
110 pub fn ensure_unrequested<P: PreimageProvider<H>>(&self) {
111 match &self {
112 Self::Value(_) => (),
113 Self::Hash(hash) => P::unrequest_preimage(hash),
114 }
115 }
116
117 pub fn resolved<P: PreimageProvider<H>>(self) -> (Self, Option<H>) {
118 match self {
119 Self::Value(c) => (Self::Value(c), None),
120 Self::Hash(h) => {
121 let data = match P::get_preimage(&h) {
122 Some(p) => p,
123 None => return (Self::Hash(h), None),
124 };
125 match T::decode(&mut &data[..]) {
126 Ok(c) => (Self::Value(c), Some(h)),
127 Err(_) => (Self::Hash(h), None),
128 }
129 },
130 }
131 }
132}
133
134#[deprecated(note = "Use `v3` instead. Will be removed after September 2024.")]
135pub mod v1 {
136 use super::*;
137
138 pub trait Anon<BlockNumber, Call, RuntimeOrigin> {
140 type Address: Codec + Clone + Eq + EncodeLike + Debug + TypeInfo + MaxEncodedLen;
142
143 fn schedule(
147 when: DispatchTime<BlockNumber>,
148 maybe_periodic: Option<Period<BlockNumber>>,
149 priority: Priority,
150 origin: RuntimeOrigin,
151 call: Call,
152 ) -> Result<Self::Address, DispatchError>;
153
154 fn cancel(address: Self::Address) -> Result<(), ()>;
165
166 fn reschedule(
173 address: Self::Address,
174 when: DispatchTime<BlockNumber>,
175 ) -> Result<Self::Address, DispatchError>;
176
177 fn next_dispatch_time(address: Self::Address) -> Result<BlockNumber, ()>;
181 }
182
183 pub trait Named<BlockNumber, Call, RuntimeOrigin> {
185 type Address: Codec + Clone + Eq + EncodeLike + core::fmt::Debug + MaxEncodedLen;
187
188 fn schedule_named(
192 id: Vec<u8>,
193 when: DispatchTime<BlockNumber>,
194 maybe_periodic: Option<Period<BlockNumber>>,
195 priority: Priority,
196 origin: RuntimeOrigin,
197 call: Call,
198 ) -> Result<Self::Address, ()>;
199
200 fn cancel_named(id: Vec<u8>) -> Result<(), ()>;
208
209 fn reschedule_named(
212 id: Vec<u8>,
213 when: DispatchTime<BlockNumber>,
214 ) -> Result<Self::Address, DispatchError>;
215
216 fn next_dispatch_time(id: Vec<u8>) -> Result<BlockNumber, ()>;
220 }
221
222 #[allow(deprecated)]
223 impl<T, BlockNumber, Call, RuntimeOrigin> Anon<BlockNumber, Call, RuntimeOrigin> for T
224 where
225 T: v2::Anon<BlockNumber, Call, RuntimeOrigin>,
226 {
227 #[allow(deprecated)]
228 type Address = T::Address;
229
230 fn schedule(
231 when: DispatchTime<BlockNumber>,
232 maybe_periodic: Option<Period<BlockNumber>>,
233 priority: Priority,
234 origin: RuntimeOrigin,
235 call: Call,
236 ) -> Result<Self::Address, DispatchError> {
237 let c = MaybeHashed::<Call, T::Hash>::Value(call);
238
239 #[allow(deprecated)]
240 T::schedule(when, maybe_periodic, priority, origin, c)
241 }
242
243 fn cancel(address: Self::Address) -> Result<(), ()> {
244 #[allow(deprecated)]
245 T::cancel(address)
246 }
247
248 fn reschedule(
249 address: Self::Address,
250 when: DispatchTime<BlockNumber>,
251 ) -> Result<Self::Address, DispatchError> {
252 #[allow(deprecated)]
253 T::reschedule(address, when)
254 }
255
256 fn next_dispatch_time(address: Self::Address) -> Result<BlockNumber, ()> {
257 #[allow(deprecated)]
258 T::next_dispatch_time(address)
259 }
260 }
261
262 #[allow(deprecated)]
263 impl<T, BlockNumber, Call, RuntimeOrigin> Named<BlockNumber, Call, RuntimeOrigin> for T
264 where
265 T: v2::Named<BlockNumber, Call, RuntimeOrigin>,
266 {
267 #[allow(deprecated)]
268 type Address = T::Address;
269
270 fn schedule_named(
271 id: Vec<u8>,
272 when: DispatchTime<BlockNumber>,
273 maybe_periodic: Option<Period<BlockNumber>>,
274 priority: Priority,
275 origin: RuntimeOrigin,
276 call: Call,
277 ) -> Result<Self::Address, ()> {
278 let c = MaybeHashed::<Call, T::Hash>::Value(call);
279 #[allow(deprecated)]
280 T::schedule_named(id, when, maybe_periodic, priority, origin, c)
281 }
282
283 fn cancel_named(id: Vec<u8>) -> Result<(), ()> {
284 #[allow(deprecated)]
285 T::cancel_named(id)
286 }
287
288 fn reschedule_named(
289 id: Vec<u8>,
290 when: DispatchTime<BlockNumber>,
291 ) -> Result<Self::Address, DispatchError> {
292 #[allow(deprecated)]
293 T::reschedule_named(id, when)
294 }
295
296 fn next_dispatch_time(id: Vec<u8>) -> Result<BlockNumber, ()> {
297 #[allow(deprecated)]
298 T::next_dispatch_time(id)
299 }
300 }
301}
302
303#[deprecated(note = "Use `v3` instead. Will be removed after September 2024.")]
304pub mod v2 {
305 use super::*;
306
307 pub trait Anon<BlockNumber, Call, RuntimeOrigin> {
309 type Address: Codec + Clone + Eq + EncodeLike + Debug + TypeInfo + MaxEncodedLen;
311 type Hash;
313
314 fn schedule(
318 when: DispatchTime<BlockNumber>,
319 maybe_periodic: Option<Period<BlockNumber>>,
320 priority: Priority,
321 origin: RuntimeOrigin,
322 call: MaybeHashed<Call, Self::Hash>,
323 ) -> Result<Self::Address, DispatchError>;
324
325 fn cancel(address: Self::Address) -> Result<(), ()>;
336
337 fn reschedule(
344 address: Self::Address,
345 when: DispatchTime<BlockNumber>,
346 ) -> Result<Self::Address, DispatchError>;
347
348 fn next_dispatch_time(address: Self::Address) -> Result<BlockNumber, ()>;
352 }
353
354 pub trait Named<BlockNumber, Call, RuntimeOrigin> {
356 type Address: Codec + Clone + Eq + EncodeLike + core::fmt::Debug + MaxEncodedLen;
358 type Hash;
360
361 fn schedule_named(
365 id: Vec<u8>,
366 when: DispatchTime<BlockNumber>,
367 maybe_periodic: Option<Period<BlockNumber>>,
368 priority: Priority,
369 origin: RuntimeOrigin,
370 call: MaybeHashed<Call, Self::Hash>,
371 ) -> Result<Self::Address, ()>;
372
373 fn cancel_named(id: Vec<u8>) -> Result<(), ()>;
381
382 fn reschedule_named(
385 id: Vec<u8>,
386 when: DispatchTime<BlockNumber>,
387 ) -> Result<Self::Address, DispatchError>;
388
389 fn next_dispatch_time(id: Vec<u8>) -> Result<BlockNumber, ()>;
393 }
394}
395
396pub mod v3 {
397 use super::*;
398 use crate::traits::Bounded;
399
400 pub trait Anon<BlockNumber, Call, Origin> {
402 type Address: Codec + MaxEncodedLen + Clone + Eq + EncodeLike + Debug + TypeInfo;
404 type Hasher: subsoil::runtime::traits::Hash;
406
407 fn schedule(
411 when: DispatchTime<BlockNumber>,
412 maybe_periodic: Option<Period<BlockNumber>>,
413 priority: Priority,
414 origin: Origin,
415 call: Bounded<Call, Self::Hasher>,
416 ) -> Result<Self::Address, DispatchError>;
417
418 fn cancel(address: Self::Address) -> Result<(), DispatchError>;
429
430 fn reschedule(
437 address: Self::Address,
438 when: DispatchTime<BlockNumber>,
439 ) -> Result<Self::Address, DispatchError>;
440
441 fn next_dispatch_time(address: Self::Address) -> Result<BlockNumber, DispatchError>;
445 }
446
447 pub type TaskName = [u8; 32];
448
449 pub trait Named<BlockNumber, Call, Origin> {
451 type Address: Codec + MaxEncodedLen + Clone + Eq + EncodeLike + core::fmt::Debug;
453 type Hasher: subsoil::runtime::traits::Hash;
455
456 fn schedule_named(
462 id: TaskName,
463 when: DispatchTime<BlockNumber>,
464 maybe_periodic: Option<Period<BlockNumber>>,
465 priority: Priority,
466 origin: Origin,
467 call: Bounded<Call, Self::Hasher>,
468 ) -> Result<Self::Address, DispatchError>;
469
470 fn cancel_named(id: TaskName) -> Result<(), DispatchError>;
478
479 fn reschedule_named(
484 id: TaskName,
485 when: DispatchTime<BlockNumber>,
486 ) -> Result<Self::Address, DispatchError>;
487
488 fn next_dispatch_time(id: TaskName) -> Result<BlockNumber, DispatchError>;
492 }
493}
494
495#[allow(deprecated)]
496pub use v1::*;