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
// This file is part of Gear.
// Copyright (C) 2022-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/>.
//! Module for messenger implementation.
//!
//! Messenger provides API for all available gear message storing.
use crate::storage::{
Counted, CountedByKey, Counter, DequeueError, Interval, IterableByKeyMap, IterableMap, Mailbox,
MailboxError, MapStorage, Queue, Toggler, Waitlist, WaitlistError,
};
use core::fmt::Debug;
/// Represents messenger's logic of centralized message processing behavior.
pub trait Messenger {
/// Block number type of the messenger.
type BlockNumber;
/// Capacity of the messenger.
///
/// This type defines length type of the queue, sent and
/// dequeued messages within same block amount type.
type Capacity;
/// Inner error type generated by gear's storage types.
type Error: Debug + DequeueError + MailboxError + WaitlistError;
/// Output error of each storage algorithm.
///
/// Implements `From<Self::Error>` to be able to return
/// any required error type.
type OutputError: From<Self::Error> + Debug;
/// First key of the mailbox storage.
///
/// Present to clarify compiler behavior over associated types.
type MailboxFirstKey;
/// Second key of the mailbox storage.
///
/// Present to clarify compiler behavior over associated types.
type MailboxSecondKey;
/// Stored values type for `Self::Mailbox`.
///
/// Present to clarify compiler behavior over associated types.
type MailboxedMessage;
/// Stored values type for `Self::Queue`.
///
/// Present to clarify compiler behavior over associated types.
type QueuedDispatch;
/// Stored values type for `Self::DispatchStash`.
///
/// Present to clarify compiler behavior over associated types.
type DelayedDispatch;
/// First key of the waitlist storage.
///
/// Present to clarify compiler behavior over associated types.
type WaitlistFirstKey;
/// Second key of the waitlist storage.
///
/// Present to clarify compiler behavior over associated types.
type WaitlistSecondKey;
/// Stored values type for `Self::Waitlist`.
///
/// Present to clarify compiler behavior over associated types.
type WaitlistedMessage;
/// Key for value types for `Self::DispatchStash`.
///
/// Present to clarify compiler behavior over associated types.
type DispatchStashKey;
/// Amount of messages sent from outside (from users)
/// within the current block.
///
/// Used as local counter for `MessageId` generation.
type Sent: Counter<Value = Self::Capacity>;
/// Amount of messages dequeued with the current block.
///
/// Used for depositing informational event about how much messages
/// were took from queue in `process_queue` execution.
type Dequeued: Counter<Value = Self::Capacity>;
/// Allowance of queue processing.
///
/// Used for checking could `process_queue` continue it's execution.
/// Execution finishes, once message requeued at the end of the queue,
/// because it alerts, that this execution exceed gas allowance of the
/// current block by gear's processing algorithm.
type QueueProcessing: Toggler;
/// Gear message queue.
///
/// Message queue contains only messages addressed to programs.
/// Messages from queue process on idle of each block in `process_queue`,
/// function, except case of runtime upgrade - then processing skipped.
type Queue: Queue<Value = Self::QueuedDispatch, Error = Self::Error, OutputError = Self::OutputError>
+ Counted<Length = Self::Capacity>
+ IterableMap<Result<Self::QueuedDispatch, Self::OutputError>>;
/// Gear mailbox.
///
/// Mailbox contains only messages addressed to user accounts.
/// Any address meant as user account if it's not program id.
///
/// Only mailbox owner (user with message's destination address)
/// can claim value from the message, removing it afterward, or claim
/// and send reply on received message, if it still present (#642).
type Mailbox: Mailbox<
Key1 = Self::MailboxFirstKey,
Key2 = Self::MailboxSecondKey,
Value = Self::MailboxedMessage,
BlockNumber = Self::BlockNumber,
Error = Self::Error,
OutputError = Self::OutputError,
> + CountedByKey<Key = Self::MailboxFirstKey, Length = usize>
+ IterableMap<(Self::MailboxedMessage, Interval<Self::BlockNumber>)>
+ IterableByKeyMap<
(Self::MailboxedMessage, Interval<Self::BlockNumber>),
Key = Self::MailboxFirstKey,
>;
/// Gear waitlist.
///
/// Waitlist contains messages, which execution should be
/// delayed for some logic.
///
/// Message can be inserted into waitlist only in these cases:
/// 1. Destination program called `gr_wait` while was executing
/// this message, so only this program can remove and
/// requeue it by `gr_wake` call in any execution.
/// 2. The message sent to program, that hadn't finished its
/// initialization, and will be automatically removed once
/// result of initialization would be available.
/// 3. Restored after resuming paused programs. On pause we
/// collect waitlist content addressed to the program,
/// removing it afterwards. On resume, user should provide
/// the same content to be able to unpause program, which
/// gonna be added into waitlist again.
///
/// More cases may be considered in future.
///
/// Gear runtime also charges rent for holding in waitlist.
/// Note, that system can remove message from waitlist,
/// if it couldn't pay rent for holding there further.
/// For details, see `pallet-gear-scheduler`.
type Waitlist: Waitlist<
Key1 = Self::WaitlistFirstKey,
Key2 = Self::WaitlistSecondKey,
Value = Self::WaitlistedMessage,
BlockNumber = Self::BlockNumber,
Error = Self::Error,
OutputError = Self::OutputError,
> + CountedByKey<Key = Self::WaitlistFirstKey, Length = usize>
+ IterableMap<(Self::WaitlistedMessage, Interval<Self::BlockNumber>)>
+ IterableByKeyMap<
(Self::WaitlistedMessage, Interval<Self::BlockNumber>),
Key = Self::WaitlistFirstKey,
>;
type DispatchStash: MapStorage<
Key = Self::DispatchStashKey,
Value = (Self::DelayedDispatch, Interval<Self::BlockNumber>),
>;
/// Resets all related to messenger storages.
///
/// It's a temporary production solution to avoid DB migrations
/// and would be available for test purposes only in the future.
fn reset() {
Self::Sent::reset();
Self::Dequeued::reset();
Self::QueueProcessing::allow();
Self::Queue::clear();
Self::Mailbox::clear();
Self::Waitlist::clear();
Self::DispatchStash::clear();
}
}