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
// 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/>.

//! API for creating programs from Gear programs.
//!
//! Once a Wasm code has been uploaded to the chain, the Gear program can create
//! a new program from the code blob using its identifier.
//!
//! To create a new program, you are to provide the code identifier obtained
//! after running the `upload_code` extrinsic, unique salt (arbitrary data
//! needed to instantiate several programs from one code blob), and the init
//! message that consists at least from a payload and value.

use crate::{
    errors::{Error, Result, SyscallError},
    ActorId, CodeId, MessageId,
};
use gsys::{ErrorWithTwoHashes, HashWithValue};

/// Create a new program and returns its address.
///
/// This function creates a program initialization message.
///
/// Parameters:
/// - `code_id` is the code identifier of newly creating program that is
///   represented as blake2b hash;
/// - `salt` is the arbitrary data needed to generate an address for a new
///   program (control of salt uniqueness is entirely on the program developer's
///   side);
/// - `payload` that can be used in the `init` function of the newly deployed
///   "child" program;
/// - `value` sent with the init message.
///
/// # Examples
///
/// Basically we can use "automatic" salt generation ("nonce"):
///
/// ```
/// use gcore::{prog, CodeId};
///
/// static mut NONCE: i32 = 0;
///
/// fn increase() {
///     unsafe { NONCE += 1 };
/// }
///
/// fn get() -> i32 {
///     unsafe { NONCE }
/// }
///
/// #[no_mangle]
/// extern "C" fn handle() {
///     // We assume we already have a code identifier
///     let submitted_code: CodeId =
///         hex_literal::hex!("abf3746e72a6e8740bd9e12b879fbdd59e052cb390f116454e9116c22021ae4a")
///             .into();
///     let (message_id, new_program_id) =
///         prog::create_program(submitted_code, &get().to_le_bytes(), b"", 0)
///             .expect("Unable to create a program");
///     increase();
/// }
/// ```
///
/// Another case for salt is to receive it as input:
///
/// ```
/// use gcore::{msg, prog};
/// # use gcore::CodeId;
///
/// #[no_mangle]
/// extern "C" fn handle() {
///     # let submitted_code: CodeId = hex_literal::hex!("abf3746e72a6e8740bd9e12b879fbdd59e052cb390f116454e9116c22021ae4a").into();
///     // ...
///     let mut salt = vec![0u8; msg::size()];
///     msg::read(&mut salt).expect("Unable to read");
///     let (message_id, new_program_id) = prog::create_program(submitted_code, &salt, b"", 0)
///         .expect("Unable to create a program");
/// }
/// ```
///
/// Moreover, messages can be sent to a new program:
///
/// ```
/// use gcore::{msg, prog};
/// # use gcore::CodeId;
///
/// #[no_mangle]
/// extern "C" fn handle() {
///     # let submitted_code: CodeId = hex_literal::hex!("abf3746e72a6e8740bd9e12b879fbdd59e052cb390f116454e9116c22021ae4a").into();
///     # let mut salt = vec![0u8; msg::size()];
///     # msg::read(&mut salt).unwrap();
///     // ...
///     let (_, new_program_id) = prog::create_program(submitted_code, &salt, b"", 0)
///         .expect("Unable to create a program");
///     msg::send(new_program_id, b"payload for a new program", 0)
///         .expect("Unable to send");
/// }
/// ```
pub fn create_program(
    code_id: CodeId,
    salt: &[u8],
    payload: &[u8],
    value: u128,
) -> Result<(MessageId, ActorId)> {
    create_program_delayed(code_id, salt, payload, value, 0)
}

/// Same as [`create_program`], but with an explicit gas limit.
#[cfg(not(feature = "ethexe"))]
pub fn create_program_with_gas(
    code_id: CodeId,
    salt: &[u8],
    payload: &[u8],
    gas_limit: u64,
    value: u128,
) -> Result<(MessageId, ActorId)> {
    create_program_with_gas_delayed(code_id, salt, payload, gas_limit, value, 0)
}

/// Same as [`create_program`], but creates a new program after the `delay`
/// expressed in block count.
pub fn create_program_delayed(
    code_id: CodeId,
    salt: &[u8],
    payload: &[u8],
    value: u128,
    delay: u32,
) -> Result<(MessageId, ActorId)> {
    let cid_value = HashWithValue {
        hash: code_id.into(),
        value,
    };

    let mut res: ErrorWithTwoHashes = Default::default();

    let salt_len = salt.len().try_into().map_err(|_| Error::SyscallUsage)?;

    let payload_len = payload.len().try_into().map_err(|_| Error::SyscallUsage)?;

    unsafe {
        gsys::gr_create_program(
            cid_value.as_ptr(),
            salt.as_ptr(),
            salt_len,
            payload.as_ptr(),
            payload_len,
            delay,
            res.as_mut_ptr(),
        )
    };
    SyscallError(res.error_code).into_result()?;

    Ok((res.hash1.into(), res.hash2.into()))
}

/// Same as [`create_program_with_gas`], but creates a new program after the
/// `delay` expressed in block count.
#[cfg(not(feature = "ethexe"))]
pub fn create_program_with_gas_delayed(
    code_id: CodeId,
    salt: &[u8],
    payload: &[u8],
    gas_limit: u64,
    value: u128,
    delay: u32,
) -> Result<(MessageId, ActorId)> {
    let cid_value = HashWithValue {
        hash: code_id.into(),
        value,
    };

    let mut res: ErrorWithTwoHashes = Default::default();

    let salt_len = salt.len().try_into().map_err(|_| Error::SyscallUsage)?;

    let payload_len = payload.len().try_into().map_err(|_| Error::SyscallUsage)?;

    unsafe {
        gsys::gr_create_program_wgas(
            cid_value.as_ptr(),
            salt.as_ptr(),
            salt_len,
            payload.as_ptr(),
            payload_len,
            gas_limit,
            delay,
            res.as_mut_ptr(),
        )
    };
    SyscallError(res.error_code).into_result()?;

    Ok((res.hash1.into(), res.hash2.into()))
}