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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// 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/>.

//! Gear primitive types.
//!
//! Unlike `gstd`, `gcore::general` provides some minimal implementation for
//! `ActorId` and `MessageId` structs with public access to their internals. It
//! is usable provided that you understand how it works and
//! consider security factors.
//!
//! `gstd::primitives` declares its own `ActorId` and `MessageId` structures
//! with more extensive methods for access to their internals (no public
//! access). It is recommended to use for most cases.
//!
//! # Examples
//! ```
//! use gstd::ActorId;
//!
//! let id = ActorId::new([0; 32]);
//! let bytes = id.as_ref();
//! ```

use crate::{
    errors::{Error, IntoResult, Result},
    prelude::{convert::TryFrom, String},
};
use primitive_types::H256;
use scale::MaxEncodedLen;
use scale_info::{
    scale::{self, Decode, Encode},
    TypeInfo,
};

const BS58_MIN_LEN: usize = 35; // Prefix (1) + ID (32) + Checksum (2)

/// Program (actor) identifier.
///
/// Gear allows user and program interactions via messages.
/// Source and target program as well as user are represented by
/// 256-bit identifier `ActorId` struct. The source `ActorId` for a message
/// being processed can be obtained using [`msg::source`](crate::msg::source)
/// function. Also, each send function has a target `ActorId` as one of the
/// arguments.
#[derive(
    Clone,
    Copy,
    Debug,
    Default,
    Hash,
    Ord,
    PartialEq,
    PartialOrd,
    Eq,
    TypeInfo,
    Decode,
    Encode,
    MaxEncodedLen,
)]
#[codec(crate = scale)]
pub struct ActorId([u8; 32]);

impl ActorId {
    /// Create a new `ActorId` from a 32-byte array.
    pub const fn new(arr: [u8; 32]) -> Self {
        Self(arr)
    }

    /// Create a new zero `ActorId`.
    pub const fn zero() -> Self {
        Self::new([0; 32])
    }

    /// Check whether `ActorId` is zero.
    pub fn is_zero(&self) -> bool {
        self == &Self::zero()
    }

    /// Create a new `ActorId` from the Base58 string.
    pub fn from_bs58(address: String) -> Result<Self> {
        let decoded = bs58::decode(address)
            .into_vec()
            .map_err(|_| Error::Convert("Unable to decode bs58 address"))?;

        let len = decoded.len();
        if len < BS58_MIN_LEN {
            Err(Error::Convert("Wrong address len"))
        } else {
            Self::from_slice(&decoded[len - 34..len - 2])
        }
    }

    /// Create a new `ActorId` from a byte slice.
    pub fn from_slice(slice: &[u8]) -> Result<Self> {
        if slice.len() != 32 {
            return Err(Error::Convert("Slice should be 32 length"));
        }

        let mut actor_id: Self = Default::default();
        actor_id.0[..].copy_from_slice(slice);

        Ok(actor_id)
    }
}

impl AsRef<[u8]> for ActorId {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

impl AsMut<[u8]> for ActorId {
    fn as_mut(&mut self) -> &mut [u8] {
        self.0.as_mut()
    }
}

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

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

impl From<ActorId> for [u8; 32] {
    fn from(other: ActorId) -> Self {
        other.0
    }
}

impl From<H256> for ActorId {
    fn from(h256: H256) -> Self {
        Self::new(h256.to_fixed_bytes())
    }
}

impl From<gcore::ActorId> for ActorId {
    fn from(other: gcore::ActorId) -> Self {
        Self(other.0)
    }
}

impl From<ActorId> for gcore::ActorId {
    fn from(other: ActorId) -> Self {
        Self(other.0)
    }
}

impl TryFrom<&[u8]> for ActorId {
    type Error = Error;

    fn try_from(slice: &[u8]) -> Result<Self> {
        Self::from_slice(slice)
    }
}

/// Message identifier.
///
/// Gear allows users and program interactions via messages.
/// Each message has its own unique 256-bit id. This id is represented
/// via the `MessageId` struct. The message identifier can be obtained for the
/// currently processed message using the [`msg::id`](crate::msg::id) function.
/// Also, each send and reply functions return a message identifier.
#[derive(
    Clone, Copy, Debug, Default, Hash, Ord, PartialEq, PartialOrd, Eq, TypeInfo, Decode, Encode,
)]
#[codec(crate = scale)]
pub struct MessageId([u8; 32]);

impl MessageId {
    /// Create a new `MessageId` from a 32-byte array.
    pub const fn new(arr: [u8; 32]) -> Self {
        Self(arr)
    }

    /// Create a new zero `MessageId`.
    pub const fn zero() -> Self {
        Self([0; 32])
    }
}

impl AsRef<[u8]> for MessageId {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

impl From<MessageId> for gcore::MessageId {
    fn from(other: MessageId) -> Self {
        Self(other.0)
    }
}

impl From<gcore::MessageId> for MessageId {
    fn from(other: gcore::MessageId) -> Self {
        Self(other.0)
    }
}

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

impl From<MessageId> for [u8; 32] {
    fn from(other: MessageId) -> Self {
        other.0
    }
}

impl From<H256> for MessageId {
    fn from(h256: H256) -> Self {
        MessageId(h256.to_fixed_bytes())
    }
}

/// Code identifier.
///
/// This identifier can be obtained as a result of executing the
/// `gear.uploadCode` extrinsic. Actually, the code identifier is the Blake2
/// hash of the Wasm binary code blob.
///
/// Code identifier is required when creating programs from programs (see
/// [`prog`](crate::prog) module for details).
#[derive(
    Clone, Copy, Debug, Default, Hash, Ord, PartialEq, PartialOrd, Eq, TypeInfo, Decode, Encode,
)]
#[codec(crate = scale)]
pub struct CodeId([u8; 32]);

impl CodeId {
    /// Create a new `CodeId` from a 32-byte array.
    pub const fn new(arr: [u8; 32]) -> Self {
        Self(arr)
    }

    /// Create a new `CodeId` from a byte slice.
    pub fn from_slice(slice: &[u8]) -> Result<Self> {
        if slice.len() != 32 {
            return Err(Error::Convert("Slice should be 32 length"));
        }

        let mut ret: Self = Default::default();
        ret.0.as_mut().copy_from_slice(slice);

        Ok(ret)
    }
}

impl AsRef<[u8]> for CodeId {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

impl AsMut<[u8]> for CodeId {
    fn as_mut(&mut self) -> &mut [u8] {
        self.0.as_mut()
    }
}

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

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

impl From<H256> for CodeId {
    fn from(h256: H256) -> Self {
        Self::new(h256.to_fixed_bytes())
    }
}

impl From<gcore::CodeId> for CodeId {
    fn from(other: gcore::CodeId) -> Self {
        Self(other.0)
    }
}

impl From<CodeId> for gcore::CodeId {
    fn from(other: CodeId) -> Self {
        Self(other.0)
    }
}

impl TryFrom<&[u8]> for CodeId {
    type Error = Error;

    fn try_from(slice: &[u8]) -> Result<Self> {
        Self::from_slice(slice)
    }
}

/// Reservation identifier.
///
/// The identifier is used to reserve and unreserve gas amount
/// for program execution later.
///
/// # Examples
///
/// ```
/// use gstd::ReservationId;
///
/// static mut RESERVED: Option<ReservationId> = None;
///
/// #[no_mangle]
/// extern "C" fn init() {
///     let reservation_id = ReservationId::reserve(50_000_000, 7).expect("Unable to reserve");
///     unsafe { RESERVED = Some(reservation_id) };
/// }
///
/// #[no_mangle]
/// extern "C" fn handle() {
///     let reservation_id = unsafe { RESERVED.take().expect("Empty `RESERVED`") };
///     reservation_id.unreserve().expect("Unable to unreserve");
/// }
/// ```
#[derive(Clone, Copy, Debug, Hash, Ord, PartialEq, PartialOrd, Eq, TypeInfo, Decode, Encode)]
#[codec(crate = scale)]
pub struct ReservationId([u8; 32]);

impl ReservationId {
    /// Reserve the `amount` of gas for further usage.
    ///
    /// `duration` is the block count within which the reserve must be used.
    ///
    /// This function returns [`ReservationId`], which one can use for gas
    /// unreserving.
    ///
    /// # Examples
    ///
    /// Reserve 50 million of gas for one block, send a reply, then unreserve
    /// gas back:
    ///
    /// ```
    /// use gstd::{msg, ReservationId};
    ///
    /// #[no_mangle]
    /// extern "C" fn handle() {
    ///     let reservation_id = ReservationId::reserve(50_000_000, 1).expect("Unable to reserve");
    ///     msg::reply_bytes_from_reservation(reservation_id.clone(), b"PONG", 0)
    ///         .expect("Unable to reply");
    ///     let reservation_left = reservation_id.unreserve().expect("Unable to unreserve");
    /// }
    /// ```
    pub fn reserve(amount: u64, duration: u32) -> Result<Self> {
        gcore::exec::reserve_gas(amount, duration).into_result()
    }

    /// Unreserve unused gas from the reservation.
    ///
    /// If successful, it returns the reserved amount of gas.
    pub fn unreserve(self) -> Result<u64> {
        gcore::exec::unreserve_gas(self.into()).into_result()
    }
}

impl AsRef<[u8]> for ReservationId {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

impl From<gcore::ReservationId> for ReservationId {
    fn from(id: gcore::ReservationId) -> Self {
        Self(id.0)
    }
}

impl From<ReservationId> for gcore::ReservationId {
    fn from(id: ReservationId) -> Self {
        gcore::ReservationId(id.0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn zero_id() {
        let id = ActorId::zero();
        assert_eq!(id.0, [0; 32]);
        assert!(id.is_zero());
    }
}