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
// This file is part of Gear.

// Copyright (C) 2024 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Auxiliary (for tests) task pool implementation for the crate.

use gear_common::{
    auxiliary::{
        task_pool::{AuxiliaryTaskpool, TaskPoolErrorImpl, TaskPoolStorageWrap},
        BlockNumber,
    },
    scheduler::{TaskPool, TaskPoolCallbacks},
    storage::KeyIterableByKeyMap,
    ProgramId,
};
use gear_core::tasks::VaraScheduledTask;

/// Task pool manager which operates under the hood over
/// [`gear_common::auxiliary::task_pool::AuxiliaryTaskpool`].
///
/// Manager is needed mainly to adapt arguments of the task pool methods to the
/// crate.
#[derive(Debug, Default)]
pub(crate) struct TaskPoolManager;

impl TaskPoolManager {
    /// Adapted by argument types version of the task pool `add` method.
    pub(crate) fn add(
        &self,
        block_number: BlockNumber,
        task: VaraScheduledTask<ProgramId>,
    ) -> Result<(), TaskPoolErrorImpl> {
        <AuxiliaryTaskpool<TaskPoolCallbacksImpl> as TaskPool>::add(block_number, task)
    }

    /// Adapted by argument types version of the task pool `clear` method.
    pub(crate) fn clear(&self) {
        <AuxiliaryTaskpool<TaskPoolCallbacksImpl> as TaskPool>::clear();
    }

    /// Adapted by argument types version of the task pool `contains` method.
    #[allow(unused)]
    pub(crate) fn contains(
        &self,
        block_number: &BlockNumber,
        task: &VaraScheduledTask<ProgramId>,
    ) -> bool {
        <AuxiliaryTaskpool<TaskPoolCallbacksImpl> as TaskPool>::contains(block_number, task)
    }

    /// Adapted by argument types version of the task pool `delete` method.
    pub(crate) fn delete(
        &self,
        block_number: BlockNumber,
        task: VaraScheduledTask<ProgramId>,
    ) -> Result<(), TaskPoolErrorImpl> {
        <AuxiliaryTaskpool<TaskPoolCallbacksImpl> as TaskPool>::delete(block_number, task)
    }

    /// Adapted by argument types version of the task pool `drain_prefix_keys`
    /// method.
    pub(crate) fn drain_prefix_keys(
        &self,
        block_number: BlockNumber,
    ) -> <TaskPoolStorageWrap as KeyIterableByKeyMap>::DrainIter {
        AuxiliaryTaskpool::<TaskPoolCallbacksImpl>::drain_prefix_keys(block_number)
    }
}

/// Task pool callbacks implementor.
pub(crate) struct TaskPoolCallbacksImpl;

impl TaskPoolCallbacks for TaskPoolCallbacksImpl {
    type OnAdd = ();
    type OnDelete = ();
}

#[cfg(test)]
mod tests {
    use super::TaskPoolManager;
    use gear_core::{ids::ProgramId, tasks::VaraScheduledTask};

    #[test]
    fn test_taskpool() {
        let manager = TaskPoolManager;

        let block_1_tasks = [
            VaraScheduledTask::<ProgramId>::SendDispatch(42.into()),
            VaraScheduledTask::<ProgramId>::SendUserMessage {
                message_id: 422.into(),
                to_mailbox: true,
            },
        ];
        let block_2_tasks = [
            VaraScheduledTask::<ProgramId>::RemoveGasReservation(922.into(), 1.into()),
            VaraScheduledTask::<ProgramId>::RemoveFromWaitlist(42.into(), 44.into()),
        ];

        block_1_tasks
            .iter()
            .for_each(|task| manager.add(1, task.clone()).unwrap());

        block_2_tasks
            .iter()
            .for_each(|task| manager.add(2, task.clone()).unwrap());

        for task in block_1_tasks.iter() {
            assert!(manager.contains(&1, task));
        }

        for task in block_2_tasks.iter() {
            assert!(manager.contains(&2, task));
        }

        for task in manager.drain_prefix_keys(1) {
            assert!(
                block_1_tasks.contains(&task),
                "task not found in block 1 tasks"
            );
        }

        for task in manager.drain_prefix_keys(2) {
            assert!(
                block_2_tasks.contains(&task),
                "task not found in block 2 tasks"
            );
        }

        for task in block_1_tasks.iter() {
            assert!(!manager.contains(&1, task));
        }

        for task in block_2_tasks.iter() {
            assert!(!manager.contains(&2, task));
        }

        let task = VaraScheduledTask::<ProgramId>::RemoveFromMailbox(422.into(), 16.into());
        manager.add(3, task.clone()).unwrap();
        manager.add(4, task.clone()).unwrap();
        manager.delete(4, task.clone()).unwrap();
        manager.clear();

        assert!(!manager.contains(&3, &task));
        assert!(!manager.contains(&4, &task));
    }
}