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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// This file is part of Gear.

// Copyright (C) 2021-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/>.

//! Common structures used in Gear programs.
//!
//! This module contains definitions of common structures that are used to work
//! with Gear API.

/// Message handle.
///
/// Gear allows users and programs to interact with other users and programs via
/// messages. Message creation consists of the following parts: message
/// initialization, filling the message with payload (can be gradual), and
/// message sending.
///
/// Here are the functions that make up the parts of forming and sending
/// messages:
///
/// - [`msg::send_init`](crate::msg::send_init) initializes the message
/// - [`msg::send_push`](crate::msg::send_push) adds a payload to a
/// message
/// - [`msg::send_commit`](crate::msg::send_commit) sends a message
///
/// To identify a message that is being built from parts of a program, you
/// should use `MessageHandle` obtained via
/// [`msg::send_init`](crate::msg::send_init).
///
/// # Examples
///
/// ```
/// use gcore::msg;
///
/// #[no_mangle]
/// extern "C" fn handle() {
///     let msg_handle = msg::send_init().expect("Unable to init");
///     msg::send_push(msg_handle, b"Hello,").expect("Unable to push");
///     msg::send_push(msg_handle, b" world!").expect("Unable to push");
///     msg::send_commit(msg_handle, msg::source(), 0).expect("Unable to send");
/// }
/// ```
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct MessageHandle(pub(crate) u32);

/// Message identifier.
///
/// Gear allows users and programs to interact with other users and programs via
/// messages. Each message has its unique 256-bit identifier. The `MessageId`
/// struct represents this identifier. One can get the message identifier for
/// the currently processing message using the [`msg::id`](crate::msg::id)
/// function. Also, each send and reply functions return a message identifier.
///
/// # Examples
///
/// ```
/// use gcore::msg;
///
/// #[no_mangle]
/// extern "C" fn handle() {
///     let current_message_id = msg::id();
/// }
/// ```
#[derive(Clone, Copy, Debug, Default, Hash, Ord, PartialEq, PartialOrd, Eq)]
pub struct MessageId(pub [u8; 32]);

impl MessageId {
    /// Create an empty `MessageId`.
    pub const fn zero() -> Self {
        Self([0u8; 32])
    }
    /// Create a new `MessageId` from the 32-byte `slice`.
    ///
    /// # Panics
    ///
    /// Panics if the supplied slice length is other than 32.
    pub fn from_slice(slice: &[u8]) -> Self {
        if slice.len() != 32 {
            panic!("The slice must contain 32 u8 to be casted to MessageId");
        }
        let mut id = Self([0u8; 32]);
        id.0.copy_from_slice(slice);
        id
    }

    /// Get `MessageId` represented as a slice of `u8`.
    pub const fn as_slice(&self) -> &[u8] {
        &self.0
    }

    pub(crate) const fn as_ptr(&self) -> *const [u8; 32] {
        self.0.as_ptr() as *const [u8; 32]
    }

    pub(crate) fn as_mut_ptr(&mut self) -> *mut [u8; 32] {
        self.0.as_mut_ptr() as *mut [u8; 32]
    }
}

/// Program identifier.
///
/// Gear allows users and programs to interact with other users and programs via
/// messages. `ActorId` struct represents the 256-bit identifier of the
/// source/target program or user. For example, the source `ActorId` for a
/// processing message can be obtained using the
/// [`msg::source`](crate::msg::source) function. Also, each send function has
/// an `ActorId` target as one of the arguments.
#[derive(Clone, Copy, Debug, Default, Hash, Ord, PartialEq, PartialOrd, Eq)]
pub struct ActorId(pub [u8; 32]);

impl From<u64> for ActorId {
    fn from(value: u64) -> Self {
        let mut id = ActorId::zero();
        id.0[0..8].copy_from_slice(&value.to_le_bytes());
        id
    }
}

impl ActorId {
    /// Create an empty `ActorId`.
    pub const fn zero() -> Self {
        Self([0; 32])
    }
    /// Create a new ActorId from 32-byte `slice`.
    ///
    /// # Panics
    ///
    /// Panics if the supplied slice length is other than 32.
    pub fn from_slice(slice: &[u8]) -> Self {
        if slice.len() != 32 {
            panic!("The slice must contain 32 u8 to be casted to ActorId");
        }
        let mut id = ActorId::zero();
        id.0.copy_from_slice(slice);
        id
    }

    /// Get `ActorId` represented as a slice of `u8`.
    pub const fn as_slice(&self) -> &[u8] {
        &self.0
    }

    pub(crate) const fn as_ptr(&self) -> *const [u8; 32] {
        self.0.as_ptr() as *const [u8; 32]
    }

    pub(crate) fn as_mut_ptr(&mut self) -> *mut [u8; 32] {
        self.0.as_mut_ptr() as *mut [u8; 32]
    }
}

/// Reservation identifier.
///
/// This identifier is used to get reserved gas or unreserve it.
///
/// See [`exec::reserve_gas`](crate::exec::reserve_gas).
#[derive(Clone, Copy, Debug, Default, Hash, Ord, PartialEq, PartialOrd, Eq)]
pub struct ReservationId(pub [u8; 32]);

impl ReservationId {
    /// Create an empty `ReservationId`.
    pub const fn zero() -> Self {
        Self([0; 32])
    }
    /// Get `ReservationId` represented as a slice of `u8`.
    pub const fn as_slice(&self) -> &[u8] {
        self.0.as_slice()
    }

    pub(crate) const fn as_ptr(&self) -> *const [u8; 32] {
        self.0.as_ptr() as *const [u8; 32]
    }
}

impl From<[u8; 32]> for ReservationId {
    fn from(value: [u8; 32]) -> Self {
        Self(value)
    }
}

// TODO: More info
/// Code identifier.
#[derive(Clone, Copy, Debug, Default, Hash, Ord, PartialEq, PartialOrd, Eq)]
pub struct CodeId(pub [u8; 32]);

impl From<[u8; 32]> for CodeId {
    fn from(value: [u8; 32]) -> Self {
        Self(value)
    }
}

impl CodeId {
    /// Create a new `CodeId` from 32-byte `slice`.
    ///
    /// Panics if the supplied slice length is other than 32.
    pub fn from_slice(slice: &[u8]) -> Self {
        if slice.len() != 32 {
            panic!("The slice must contain 32 u8 to be casted to CodeId");
        }
        let mut id = CodeId([0u8; 32]);
        id.0.copy_from_slice(slice);
        id
    }

    /// Get `CodeId` represented as a slice of `u8`.
    pub const fn as_slice(&self) -> &[u8] {
        &self.0
    }
}