#[allow(deprecated)]
use super::PreimageProvider;
use codec::{Codec, Decode, DecodeWithMemTracking, Encode, EncodeLike, MaxEncodedLen};
use core::{fmt::Debug, result::Result};
use scale_info::TypeInfo;
use sp_runtime::{traits::Saturating, DispatchError};
pub type Period<BlockNumber> = (BlockNumber, u32);
pub type Priority = u8;
#[derive(
Encode,
Decode,
DecodeWithMemTracking,
Copy,
Clone,
PartialEq,
Eq,
Debug,
TypeInfo,
MaxEncodedLen,
)]
pub enum DispatchTime<BlockNumber> {
At(BlockNumber),
After(BlockNumber),
}
impl<BlockNumber: Saturating + Copy> DispatchTime<BlockNumber> {
pub fn evaluate(&self, since: BlockNumber) -> BlockNumber {
match &self {
Self::At(m) => *m,
Self::After(m) => m.saturating_add(since),
}
}
}
pub const HIGHEST_PRIORITY: Priority = 0;
pub const HARD_DEADLINE: Priority = 63;
pub const LOWEST_PRIORITY: Priority = 255;
#[derive(Clone, Eq, PartialEq, Encode, Decode, Debug, TypeInfo, MaxEncodedLen)]
pub enum MaybeHashed<T, Hash> {
Value(T),
Hash(Hash),
}
impl<T, H> From<T> for MaybeHashed<T, H> {
fn from(t: T) -> Self {
MaybeHashed::Value(t)
}
}
#[derive(Clone, Eq, PartialEq, Encode, Decode, Debug, TypeInfo, MaxEncodedLen)]
pub enum LookupError {
Unknown,
BadFormat,
}
impl<T: Decode, H> MaybeHashed<T, H> {
pub fn as_value(&self) -> Option<&T> {
match &self {
Self::Value(c) => Some(c),
Self::Hash(_) => None,
}
}
pub fn as_hash(&self) -> Option<&H> {
match &self {
Self::Value(_) => None,
Self::Hash(h) => Some(h),
}
}
pub fn ensure_requested<P: PreimageProvider<H>>(&self) {
match &self {
Self::Value(_) => (),
Self::Hash(hash) => P::request_preimage(hash),
}
}
pub fn ensure_unrequested<P: PreimageProvider<H>>(&self) {
match &self {
Self::Value(_) => (),
Self::Hash(hash) => P::unrequest_preimage(hash),
}
}
pub fn resolved<P: PreimageProvider<H>>(self) -> (Self, Option<H>) {
match self {
Self::Value(c) => (Self::Value(c), None),
Self::Hash(h) => {
let data = match P::get_preimage(&h) {
Some(p) => p,
None => return (Self::Hash(h), None),
};
match T::decode(&mut &data[..]) {
Ok(c) => (Self::Value(c), Some(h)),
Err(_) => (Self::Hash(h), None),
}
},
}
}
}
pub mod v3 {
use super::*;
use crate::traits::Bounded;
pub trait Anon<BlockNumber, Call, Origin> {
type Address: Codec + MaxEncodedLen + Clone + Eq + EncodeLike + Debug + TypeInfo;
type Hasher: sp_runtime::traits::Hash;
fn schedule(
when: DispatchTime<BlockNumber>,
maybe_periodic: Option<Period<BlockNumber>>,
priority: Priority,
origin: Origin,
call: Bounded<Call, Self::Hasher>,
) -> Result<Self::Address, DispatchError>;
fn cancel(address: Self::Address) -> Result<(), DispatchError>;
fn reschedule(
address: Self::Address,
when: DispatchTime<BlockNumber>,
) -> Result<Self::Address, DispatchError>;
fn next_dispatch_time(address: Self::Address) -> Result<BlockNumber, DispatchError>;
}
pub type TaskName = [u8; 32];
pub trait Named<BlockNumber, Call, Origin> {
type Address: Codec + MaxEncodedLen + Clone + Eq + EncodeLike + core::fmt::Debug;
type Hasher: sp_runtime::traits::Hash;
fn schedule_named(
id: TaskName,
when: DispatchTime<BlockNumber>,
maybe_periodic: Option<Period<BlockNumber>>,
priority: Priority,
origin: Origin,
call: Bounded<Call, Self::Hasher>,
) -> Result<Self::Address, DispatchError>;
fn cancel_named(id: TaskName) -> Result<(), DispatchError>;
fn reschedule_named(
id: TaskName,
when: DispatchTime<BlockNumber>,
) -> Result<Self::Address, DispatchError>;
fn next_dispatch_time(id: TaskName) -> Result<BlockNumber, DispatchError>;
}
}