use crate::{paused_program_storage::SessionId, Gas};
use frame_support::{
codec::{self, Decode, Encode, MaxEncodedLen},
scale_info::{self, TypeInfo},
};
use gear_core::ids::{CodeId, MessageId, ProgramId, ReservationId};
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Encode, Decode, TypeInfo, MaxEncodedLen)]
#[codec(crate = codec)]
#[scale_info(crate = scale_info)]
pub enum ScheduledTask<AccountId> {
#[codec(index = 0)]
PauseProgram(ProgramId),
#[codec(index = 1)]
RemoveCode(CodeId),
#[codec(index = 2)]
RemoveFromMailbox(AccountId, MessageId),
#[codec(index = 3)]
RemoveFromWaitlist(ProgramId, MessageId),
#[codec(index = 4)]
RemovePausedProgram(ProgramId),
#[codec(index = 5)]
WakeMessage(ProgramId, MessageId),
#[codec(index = 6)]
SendDispatch(MessageId),
#[codec(index = 7)]
SendUserMessage {
message_id: MessageId,
to_mailbox: bool,
},
#[codec(index = 8)]
RemoveGasReservation(ProgramId, ReservationId),
#[codec(index = 9)]
RemoveResumeSession(SessionId),
}
impl<AccountId> ScheduledTask<AccountId> {
pub fn process_with(self, handler: &mut impl TaskHandler<AccountId>) -> Gas {
use ScheduledTask::*;
match self {
PauseProgram(program_id) => handler.pause_program(program_id),
RemoveCode(code_id) => handler.remove_code(code_id),
RemoveFromMailbox(user_id, message_id) => {
handler.remove_from_mailbox(user_id, message_id)
}
RemoveFromWaitlist(program_id, message_id) => {
handler.remove_from_waitlist(program_id, message_id)
}
RemovePausedProgram(program_id) => handler.remove_paused_program(program_id),
WakeMessage(program_id, message_id) => handler.wake_message(program_id, message_id),
SendDispatch(message_id) => handler.send_dispatch(message_id),
SendUserMessage {
message_id,
to_mailbox,
} => handler.send_user_message(message_id, to_mailbox),
RemoveGasReservation(program_id, reservation_id) => {
handler.remove_gas_reservation(program_id, reservation_id)
}
RemoveResumeSession(session_id) => handler.remove_resume_session(session_id),
}
}
}
pub trait TaskHandler<AccountId> {
fn pause_program(&mut self, program_id: ProgramId) -> Gas;
fn remove_code(&mut self, code_id: CodeId) -> Gas;
fn remove_from_mailbox(&mut self, user_id: AccountId, message_id: MessageId) -> Gas;
fn remove_from_waitlist(&mut self, program_id: ProgramId, message_id: MessageId) -> Gas;
fn remove_paused_program(&mut self, program_id: ProgramId) -> Gas;
fn wake_message(&mut self, program_id: ProgramId, message_id: MessageId) -> Gas;
fn send_dispatch(&mut self, stashed_message_id: MessageId) -> Gas;
fn send_user_message(&mut self, stashed_message_id: MessageId, to_mailbox: bool) -> Gas;
fn remove_gas_reservation(
&mut self,
program_id: ProgramId,
reservation_id: ReservationId,
) -> Gas;
fn remove_resume_session(&mut self, session_id: SessionId) -> Gas;
}
#[test]
fn task_encoded_size() {
const MAX_SIZE: usize = 256;
type AccountId = ProgramId;
assert!(ScheduledTask::<AccountId>::max_encoded_len() <= MAX_SIZE);
}