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 191 192 193 194 195 196 197 198 199 200 201
// 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/>.
use crate::{
ids::{MessageId, ProgramId},
message::{
common::MessageDetails, ContextStore, DispatchKind, GasLimit, Payload, StoredDispatch,
StoredMessage, Value,
},
};
use core::ops::Deref;
use scale_info::{
scale::{Decode, Encode},
TypeInfo,
};
/// Incoming message.
///
/// Used for program execution.
#[derive(Clone, Default, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Decode, Encode, TypeInfo)]
pub struct IncomingMessage {
/// Message id.
id: MessageId,
/// Message source.
source: ProgramId,
/// Message payload.
payload: Payload,
/// Message gas limit. Required here.
gas_limit: GasLimit,
/// Message value.
value: Value,
/// Message details like reply message ID, status code, etc.
details: Option<MessageDetails>,
}
impl IncomingMessage {
/// Create new IncomingMessage.
pub fn new(
id: MessageId,
source: ProgramId,
payload: Payload,
gas_limit: GasLimit,
value: Value,
details: Option<MessageDetails>,
) -> Self {
Self {
id,
source,
payload,
gas_limit,
value,
details,
}
}
/// Convert IncomingMessage into gasless StoredMessage.
pub fn into_stored(self, destination: ProgramId) -> StoredMessage {
StoredMessage::new(
self.id,
self.source,
destination,
self.payload,
self.value,
self.details,
)
}
/// Message id.
pub fn id(&self) -> MessageId {
self.id
}
/// Message source.
pub fn source(&self) -> ProgramId {
self.source
}
/// Message payload bytes.
pub fn payload_bytes(&self) -> &[u8] {
self.payload.inner()
}
/// Mutable reference to message payload.
pub fn payload_mut(&mut self) -> &mut Payload {
&mut self.payload
}
/// Message gas limit.
pub fn gas_limit(&self) -> GasLimit {
self.gas_limit
}
/// Message value.
pub fn value(&self) -> Value {
self.value
}
/// Message details.
pub fn details(&self) -> Option<MessageDetails> {
self.details
}
/// Returns bool defining if message is error reply.
pub fn is_error_reply(&self) -> bool {
self.details.map(|d| d.is_error_reply()).unwrap_or(false)
}
/// Returns bool defining if message is reply.
pub fn is_reply(&self) -> bool {
self.details.map(|d| d.is_reply_details()).unwrap_or(false)
}
}
/// Incoming message with entry point and previous execution context, if exists.
#[derive(Clone, Default, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Decode, Encode, TypeInfo)]
pub struct IncomingDispatch {
/// Entry point.
kind: DispatchKind,
/// Incoming message.
message: IncomingMessage,
/// Previous execution context, if exists.
context: Option<ContextStore>,
}
impl From<IncomingDispatch> for (DispatchKind, IncomingMessage, Option<ContextStore>) {
fn from(dispatch: IncomingDispatch) -> (DispatchKind, IncomingMessage, Option<ContextStore>) {
(dispatch.kind, dispatch.message, dispatch.context)
}
}
impl IncomingDispatch {
/// Create new IncomingDispatch.
pub fn new(
kind: DispatchKind,
message: IncomingMessage,
context: Option<ContextStore>,
) -> Self {
Self {
kind,
message,
context,
}
}
/// Convert IncomingDispatch into gasless StoredDispatch with updated (or recently set) context.
pub fn into_stored(self, destination: ProgramId, context: ContextStore) -> StoredDispatch {
StoredDispatch::new(
self.kind,
self.message.into_stored(destination),
Some(context),
)
}
/// Decompose IncomingDispatch for it's components: DispatchKind, IncomingMessage and `Option<ContextStore>`.
pub fn into_parts(self) -> (DispatchKind, IncomingMessage, Option<ContextStore>) {
self.into()
}
/// Entry point for the message.
pub fn kind(&self) -> DispatchKind {
self.kind
}
/// Dispatch message reference.
pub fn message(&self) -> &IncomingMessage {
&self.message
}
/// Previous execution context reference, if exists.
pub fn context(&self) -> &Option<ContextStore> {
&self.context
}
/// Previous execution context mutable reference, if exists.
pub fn context_mut(&mut self) -> &mut Option<ContextStore> {
&mut self.context
}
}
impl Deref for IncomingDispatch {
type Target = IncomingMessage;
fn deref(&self) -> &Self::Target {
self.message()
}
}