-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathexports.rs
328 lines (295 loc) · 10.5 KB
/
exports.rs
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
//! exports exposes the public wasm API
//!
//! interface_version_6, allocate and deallocate turn into Wasm exports
//! as soon as cosmwasm_std is `use`d in the contract, even privately.
//!
//! `do_execute`, `do_instantiate`, `do_migrate`, `do_query`, `do_reply`
//! and `do_sudo` should be wrapped with a extern "C" entry point including
//! the contract-specific function pointer. This is done via the `#[entry_point]`
//! macro attribute from cosmwasm-derive.
use std::fmt;
use std::vec::Vec;
use schemars::JsonSchema;
use serde::{de::DeserializeOwned, Serialize};
use crate::deps::OwnedDeps;
use crate::imports::{ExternalApi, ExternalQuerier, ExternalStorage};
use crate::memory::{alloc, consume_region, release_buffer, Region};
use crate::results::{ContractResult, QueryResponse, Reply, Response};
use crate::serde::{from_slice, to_vec};
use crate::types::Env;
use crate::{Deps, DepsMut, MessageInfo};
#[cfg(feature = "staking")]
#[no_mangle]
extern "C" fn requires_staking() -> () {}
#[cfg(feature = "stargate")]
#[no_mangle]
extern "C" fn requires_stargate() -> () {}
/// interface_version_* exports mark which Wasm VM interface level this contract is compiled for.
/// They can be checked by cosmwasm_vm.
/// Update this whenever the Wasm VM interface breaks.
#[no_mangle]
extern "C" fn interface_version_6() -> () {}
/// allocate reserves the given number of bytes in wasm memory and returns a pointer
/// to a Region defining this data. This space is managed by the calling process
/// and should be accompanied by a corresponding deallocate
#[no_mangle]
extern "C" fn allocate(size: usize) -> u32 {
alloc(size) as u32
}
/// deallocate expects a pointer to a Region created with allocate.
/// It will free both the Region and the memory referenced by the Region.
#[no_mangle]
extern "C" fn deallocate(pointer: u32) {
// auto-drop Region on function end
let _ = unsafe { consume_region(pointer as *mut Region) };
}
// TODO: replace with https://doc.rust-lang.org/std/ops/trait.Try.html once stabilized
macro_rules! r#try_into_contract_result {
($expr:expr) => {
match $expr {
Ok(val) => val,
Err(err) => {
return ContractResult::Err(err.to_string());
}
}
};
($expr:expr,) => {
$crate::try_into_contract_result!($expr)
};
}
/// This should be wrapped in an external "C" export, containing a contract-specific function as an argument.
///
/// - `M`: message type for request
/// - `C`: custom response message type (see CosmosMsg)
/// - `E`: error type for responses
pub fn do_instantiate<M, C, E>(
instantiate_fn: &dyn Fn(DepsMut, Env, MessageInfo, M) -> Result<Response<C>, E>,
env_ptr: u32,
info_ptr: u32,
msg_ptr: u32,
) -> u32
where
M: DeserializeOwned + JsonSchema,
C: Serialize + Clone + fmt::Debug + PartialEq + JsonSchema,
E: ToString,
{
let res = _do_instantiate(
instantiate_fn,
env_ptr as *mut Region,
info_ptr as *mut Region,
msg_ptr as *mut Region,
);
let v = to_vec(&res).unwrap();
release_buffer(v) as u32
}
/// do_execute should be wrapped in an external "C" export, containing a contract-specific function as arg
///
/// - `M`: message type for request
/// - `C`: custom response message type (see CosmosMsg)
/// - `E`: error type for responses
pub fn do_execute<M, C, E>(
execute_fn: &dyn Fn(DepsMut, Env, MessageInfo, M) -> Result<Response<C>, E>,
env_ptr: u32,
info_ptr: u32,
msg_ptr: u32,
) -> u32
where
M: DeserializeOwned + JsonSchema,
C: Serialize + Clone + fmt::Debug + PartialEq + JsonSchema,
E: ToString,
{
let res = _do_execute(
execute_fn,
env_ptr as *mut Region,
info_ptr as *mut Region,
msg_ptr as *mut Region,
);
let v = to_vec(&res).unwrap();
release_buffer(v) as u32
}
/// do_migrate should be wrapped in an external "C" export, containing a contract-specific function as arg
///
/// - `M`: message type for request
/// - `C`: custom response message type (see CosmosMsg)
/// - `E`: error type for responses
pub fn do_migrate<M, C, E>(
migrate_fn: &dyn Fn(DepsMut, Env, M) -> Result<Response<C>, E>,
env_ptr: u32,
msg_ptr: u32,
) -> u32
where
M: DeserializeOwned + JsonSchema,
C: Serialize + Clone + fmt::Debug + PartialEq + JsonSchema,
E: ToString,
{
let res = _do_migrate(migrate_fn, env_ptr as *mut Region, msg_ptr as *mut Region);
let v = to_vec(&res).unwrap();
release_buffer(v) as u32
}
/// do_sudo should be wrapped in an external "C" export, containing a contract-specific function as arg
///
/// - `M`: message type for request
/// - `C`: custom response message type (see CosmosMsg)
/// - `E`: error type for responses
pub fn do_sudo<M, C, E>(
sudo_fn: &dyn Fn(DepsMut, Env, M) -> Result<Response<C>, E>,
env_ptr: u32,
msg_ptr: u32,
) -> u32
where
M: DeserializeOwned + JsonSchema,
C: Serialize + Clone + fmt::Debug + PartialEq + JsonSchema,
E: ToString,
{
let res = _do_sudo(sudo_fn, env_ptr as *mut Region, msg_ptr as *mut Region);
let v = to_vec(&res).unwrap();
release_buffer(v) as u32
}
/// do_reply should be wrapped in an external "C" export, containing a contract-specific function as arg
/// message body is always `SubcallResult`
/// - `C`: custom response message type (see CosmosMsg)
/// - `E`: error type for responses
pub fn do_reply<C, E>(
reply_fn: &dyn Fn(DepsMut, Env, Reply) -> Result<Response<C>, E>,
env_ptr: u32,
msg_ptr: u32,
) -> u32
where
C: Serialize + Clone + fmt::Debug + PartialEq + JsonSchema,
E: ToString,
{
let res = _do_reply(reply_fn, env_ptr as *mut Region, msg_ptr as *mut Region);
let v = to_vec(&res).unwrap();
release_buffer(v) as u32
}
/// do_query should be wrapped in an external "C" export, containing a contract-specific function as arg
///
/// - `M`: message type for request
/// - `E`: error type for responses
pub fn do_query<M, E>(
query_fn: &dyn Fn(Deps, Env, M) -> Result<QueryResponse, E>,
env_ptr: u32,
msg_ptr: u32,
) -> u32
where
M: DeserializeOwned + JsonSchema,
E: ToString,
{
let res = _do_query(query_fn, env_ptr as *mut Region, msg_ptr as *mut Region);
let v = to_vec(&res).unwrap();
release_buffer(v) as u32
}
fn _do_instantiate<M, C, E>(
instantiate_fn: &dyn Fn(DepsMut, Env, MessageInfo, M) -> Result<Response<C>, E>,
env_ptr: *mut Region,
info_ptr: *mut Region,
msg_ptr: *mut Region,
) -> ContractResult<Response<C>>
where
M: DeserializeOwned + JsonSchema,
C: Serialize + Clone + fmt::Debug + PartialEq + JsonSchema,
E: ToString,
{
let env: Vec<u8> = unsafe { consume_region(env_ptr) };
let info: Vec<u8> = unsafe { consume_region(info_ptr) };
let msg: Vec<u8> = unsafe { consume_region(msg_ptr) };
let env: Env = try_into_contract_result!(from_slice(&env));
let info: MessageInfo = try_into_contract_result!(from_slice(&info));
let msg: M = try_into_contract_result!(from_slice(&msg));
let mut deps = make_dependencies();
instantiate_fn(deps.as_mut(), env, info, msg).into()
}
fn _do_execute<M, C, E>(
execute_fn: &dyn Fn(DepsMut, Env, MessageInfo, M) -> Result<Response<C>, E>,
env_ptr: *mut Region,
info_ptr: *mut Region,
msg_ptr: *mut Region,
) -> ContractResult<Response<C>>
where
M: DeserializeOwned + JsonSchema,
C: Serialize + Clone + fmt::Debug + PartialEq + JsonSchema,
E: ToString,
{
let env: Vec<u8> = unsafe { consume_region(env_ptr) };
let info: Vec<u8> = unsafe { consume_region(info_ptr) };
let msg: Vec<u8> = unsafe { consume_region(msg_ptr) };
let env: Env = try_into_contract_result!(from_slice(&env));
let info: MessageInfo = try_into_contract_result!(from_slice(&info));
let msg: M = try_into_contract_result!(from_slice(&msg));
let mut deps = make_dependencies();
execute_fn(deps.as_mut(), env, info, msg).into()
}
fn _do_migrate<M, C, E>(
migrate_fn: &dyn Fn(DepsMut, Env, M) -> Result<Response<C>, E>,
env_ptr: *mut Region,
msg_ptr: *mut Region,
) -> ContractResult<Response<C>>
where
M: DeserializeOwned + JsonSchema,
C: Serialize + Clone + fmt::Debug + PartialEq + JsonSchema,
E: ToString,
{
let env: Vec<u8> = unsafe { consume_region(env_ptr) };
let msg: Vec<u8> = unsafe { consume_region(msg_ptr) };
let env: Env = try_into_contract_result!(from_slice(&env));
let msg: M = try_into_contract_result!(from_slice(&msg));
let mut deps = make_dependencies();
migrate_fn(deps.as_mut(), env, msg).into()
}
fn _do_sudo<M, C, E>(
sudo_fn: &dyn Fn(DepsMut, Env, M) -> Result<Response<C>, E>,
env_ptr: *mut Region,
msg_ptr: *mut Region,
) -> ContractResult<Response<C>>
where
M: DeserializeOwned + JsonSchema,
C: Serialize + Clone + fmt::Debug + PartialEq + JsonSchema,
E: ToString,
{
let env: Vec<u8> = unsafe { consume_region(env_ptr) };
let msg: Vec<u8> = unsafe { consume_region(msg_ptr) };
let env: Env = try_into_contract_result!(from_slice(&env));
let msg: M = try_into_contract_result!(from_slice(&msg));
let mut deps = make_dependencies();
sudo_fn(deps.as_mut(), env, msg).into()
}
fn _do_reply<C, E>(
reply_fn: &dyn Fn(DepsMut, Env, Reply) -> Result<Response<C>, E>,
env_ptr: *mut Region,
msg_ptr: *mut Region,
) -> ContractResult<Response<C>>
where
C: Serialize + Clone + fmt::Debug + PartialEq + JsonSchema,
E: ToString,
{
let env: Vec<u8> = unsafe { consume_region(env_ptr) };
let msg: Vec<u8> = unsafe { consume_region(msg_ptr) };
let env: Env = try_into_contract_result!(from_slice(&env));
let msg: Reply = try_into_contract_result!(from_slice(&msg));
let mut deps = make_dependencies();
reply_fn(deps.as_mut(), env, msg).into()
}
fn _do_query<M, E>(
query_fn: &dyn Fn(Deps, Env, M) -> Result<QueryResponse, E>,
env_ptr: *mut Region,
msg_ptr: *mut Region,
) -> ContractResult<QueryResponse>
where
M: DeserializeOwned + JsonSchema,
E: ToString,
{
let env: Vec<u8> = unsafe { consume_region(env_ptr) };
let msg: Vec<u8> = unsafe { consume_region(msg_ptr) };
let env: Env = try_into_contract_result!(from_slice(&env));
let msg: M = try_into_contract_result!(from_slice(&msg));
let deps = make_dependencies();
query_fn(deps.as_ref(), env, msg).into()
}
/// Makes all bridges to external dependencies (i.e. Wasm imports) that are injected by the VM
pub(crate) fn make_dependencies() -> OwnedDeps<ExternalStorage, ExternalApi, ExternalQuerier> {
OwnedDeps {
storage: ExternalStorage::new(),
api: ExternalApi::new(),
querier: ExternalQuerier::new(),
}
}