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
use crate::fracpack::{Pack, UnpackOwned};
use crate::services::transact::ServiceMethod;
use crate::{get_result_bytes, native_raw, AccountNumber, Action, MethodNumber};
use serde::de::DeserializeOwned;
use serde::Serialize;
#[cfg(target_family = "wasm")]
static mut SERVICE: AccountNumber = AccountNumber::new(0);
#[cfg(target_family = "wasm")]
static mut SENDER: AccountNumber = AccountNumber::new(0);
/// Get currently-executing service
///
/// If the currently-executing WASM is a service, then this
/// returns the service's account. If the currently-executing
/// code is not WASM, or is not a service, then this returns 0.
#[cfg(target_family = "wasm")]
pub fn get_service() -> AccountNumber {
unsafe { SERVICE }
}
/// Get currently-executing service
///
/// If the currently-executing WASM is a service, then this
/// returns the service's account. If the currently-executing
/// code is not WASM, or is not a service, then this returns 0.
#[cfg(not(target_family = "wasm"))]
pub fn get_service() -> AccountNumber {
AccountNumber::new(0)
}
/// Get sender of currently-executing action
///
/// If the currently-executing WASM is a service, then this
/// returns the current action's sender. If the
/// currently-executing code is not WASM, or is not a service,
/// then this returns 0.
#[cfg(target_family = "wasm")]
pub fn get_sender() -> AccountNumber {
unsafe { SENDER }
}
/// Get sender of currently-executing action
///
/// If the currently-executing WASM is a service, then this
/// returns the current action's sender. If the
/// currently-executing code is not WASM, or is not a service,
/// then this returns 0.
#[cfg(not(target_family = "wasm"))]
pub fn get_sender() -> AccountNumber {
AccountNumber::new(0)
}
// The service's `start` entry point sets this. Nothing else
// should call it.
#[doc(hidden)]
#[cfg(target_family = "wasm")]
pub unsafe fn set_service(_acc: AccountNumber) {
SERVICE = _acc;
}
// The service's `called` entry point sets this. Nothing else
// should call it. The entry point must restore the previous
// version when it returns.
#[doc(hidden)]
#[cfg(target_family = "wasm")]
pub unsafe fn set_sender(_acc: AccountNumber) {
SENDER = _acc;
}
#[doc(hidden)]
#[cfg(target_family = "wasm")]
pub fn service_start() {
std::panic::set_hook(Box::new(|info| {
let payload = info.payload();
let msg: &str = if let Some(s) = payload.downcast_ref::<&str>() {
*s
} else if let Some(s) = payload.downcast_ref::<String>() {
&*s
} else {
""
};
crate::abort_message(msg)
}))
}
pub trait ProcessActionStruct {
type Output;
fn process<
Return: Serialize + DeserializeOwned,
ArgStruct: Pack + Serialize + DeserializeOwned,
>(
self,
) -> Self::Output;
}
pub trait WithActionStruct {
fn with_action_struct<P: ProcessActionStruct>(action: &str, process: P) -> Option<P::Output>;
}
pub trait Caller: Clone {
type ReturnsNothing;
type ReturnType<T: UnpackOwned>;
fn call_returns_nothing<Args: Pack>(
&self,
method: MethodNumber,
args: Args,
) -> Self::ReturnsNothing;
fn call<Ret: UnpackOwned, Args: Pack>(
&self,
method: MethodNumber,
args: Args,
) -> Self::ReturnType<Ret>;
}
#[derive(Clone, Default)]
pub struct JustSchema;
impl Caller for JustSchema {
type ReturnsNothing = ();
type ReturnType<T: UnpackOwned> = ();
fn call_returns_nothing<Args: Pack>(&self, _method: MethodNumber, _args: Args) {}
fn call<Ret: UnpackOwned, Args: Pack>(&self, _method: MethodNumber, _args: Args) {}
}
#[derive(Clone, Default)]
pub struct ServiceCaller {
pub sender: AccountNumber,
pub service: AccountNumber,
pub flags: u64,
}
impl Caller for ServiceCaller {
type ReturnsNothing = ();
type ReturnType<T: UnpackOwned> = T;
fn call_returns_nothing<Args: Pack>(&self, method: MethodNumber, args: Args) {
let act = Action {
sender: self.sender,
service: self.service,
method,
rawData: args.packed().into(),
}
.packed();
unsafe { native_raw::call(act.as_ptr(), act.len() as u32, self.flags) };
}
fn call<Ret: UnpackOwned, Args: Pack>(&self, method: MethodNumber, args: Args) -> Ret {
let act = Action {
sender: self.sender,
service: self.service,
method,
rawData: args.packed().into(),
}
.packed();
let ret = get_result_bytes(unsafe {
native_raw::call(act.as_ptr(), act.len() as u32, self.flags)
});
Ret::unpacked(&ret).unwrap()
}
}
#[derive(Clone, Default)]
pub struct ActionPacker {
pub sender: AccountNumber,
pub service: AccountNumber,
}
impl Caller for ActionPacker {
type ReturnsNothing = Action;
type ReturnType<T: UnpackOwned> = Action;
fn call_returns_nothing<Args: Pack>(&self, method: MethodNumber, args: Args) -> Action {
Action {
sender: self.sender,
service: self.service,
method,
rawData: args.packed().into(),
}
}
fn call<Ret: UnpackOwned, Args: Pack>(&self, method: MethodNumber, args: Args) -> Action {
Action {
sender: self.sender,
service: self.service,
method,
rawData: args.packed().into(),
}
}
}
pub trait ServiceWrapper {
const SERVICE: AccountNumber;
type Actions<T: Caller>;
fn with_caller<T: Caller>(caller: T) -> Self::Actions<T>;
/// Call another service.
///
/// This method returns an object which has methods
/// (one per action) which call another service and return the result from the call.
/// This method is only usable by services.
///
/// This method defaults `sender` to [`psibase::get_sender`] and `service` to `Self::SERVICE`.
fn call() -> Self::Actions<ServiceCaller> {
Self::call_from_to(get_service(), Self::SERVICE)
}
/// Call another service.
///
/// This method returns an object which has methods
/// (one per action) which call another service and return the result from the call.
/// This method is only usable by services.
///
/// This method defaults `sender` to [`psibase::get_sender`].
fn call_to(service: AccountNumber) -> Self::Actions<ServiceCaller> {
Self::call_from_to(get_service(), service)
}
/// Call another service.
///
/// This method returns an object which has methods
/// (one per action) which call another service and return the result from the call.
/// This method is only usable by services.
///
/// This method defaults `service` to `Self::SERVICE`.
fn call_from(sender: AccountNumber) -> Self::Actions<ServiceCaller> {
Self::call_from_to(sender, Self::SERVICE)
}
/// Call another service.
///
/// This method returns an object which has methods
/// (one per action) which call another service and return the result from the call.
/// This method is only usable by services.
fn call_from_to(sender: AccountNumber, service: AccountNumber) -> Self::Actions<ServiceCaller> {
Self::with_caller(ServiceCaller {
sender,
service,
flags: 0,
})
}
/// Call another service using [runAs](psibase::services::transact::Actions::runAs).
///
/// This method returns an object which has methods
/// (one per action) which call another service via `runAs` and return the result from the call.
/// The action will run with `sender` set to the provided account and `service` to `Self::SERVICE`.
///
/// This will fail unless certain conditions are met. See [runAs](psibase::services::transact::Actions::runAs)
/// documentation for more details.
fn call_as(sender: AccountNumber) -> Self::Actions<RunAsCaller> {
Self::call_as_extend(sender, Vec::new())
}
/// Call another service using [runAs](psibase::services::transact::Actions::runAs).
///
/// This method returns an object which has methods
/// (one per action) which call another service via `runAs` and return the result from the call.
/// The action will run with `sender` set to the provided account and `service` to `Self::SERVICE`.
///
/// This will fail unless certain conditions are met. See [runAs](psibase::services::transact::Actions::runAs)
/// documentation for more details.
///
/// This method also accepts `allowedActions` for nested `runAs` calls.
fn call_as_extend(
sender: AccountNumber,
allowed_actions: Vec<ServiceMethod>,
) -> Self::Actions<RunAsCaller> {
Self::with_caller(RunAsCaller {
sender,
service: Self::SERVICE,
allowed_actions,
})
}
/// Call another service in RPC mode.
///
/// This method returns an object which has methods
/// (one per action) which call another service and return the result from the call.
/// This method is only usable by services.
///
/// This method defaults `sender` to [`psibase::get_sender`] and `service` to `Self::SERVICE`.
fn rpc() -> Self::Actions<ServiceCaller> {
Self::with_caller(ServiceCaller {
sender: get_service(),
service: Self::SERVICE,
flags: 1,
})
}
/// Pack actions into [psibase::Action](psibase::Action).
///
/// This method returns an object which has methods
/// (one per action) which pack the action's arguments using [fracpack] and
/// return a [psibase::Action](psibase::Action). The `pack_*` series of
/// functions is mainly useful to applications which push transactions
/// to blockchains.
///
/// This method defaults both `sender` and `service` to `Self::SERVICE`.
fn pack() -> Self::Actions<ActionPacker> {
Self::pack_from_to(Self::SERVICE, Self::SERVICE)
}
/// Pack actions into [psibase::Action](psibase::Action).
///
/// This method returns an object which has methods
/// (one per action) which pack the action's arguments using [fracpack] and
/// return a [psibase::Action](psibase::Action). The `pack_*` series of
/// functions is mainly useful to applications which push transactions
/// to blockchains.
///
/// This method defaults `sender` to `Self::SERVICE`.
fn pack_to(service: AccountNumber) -> Self::Actions<ActionPacker> {
Self::pack_from_to(Self::SERVICE, service)
}
/// Pack actions into [psibase::Action](psibase::Action).
///
/// This method returns an object which has methods
/// (one per action) which pack the action's arguments using [fracpack] and
/// return a [psibase::Action](psibase::Action). The `pack_*` series of
/// functions is mainly useful to applications which push transactions
/// to blockchains.
///
/// This method defaults `service` to `Self::SERVICE`.
fn pack_from(sender: AccountNumber) -> Self::Actions<ActionPacker> {
Self::pack_from_to(sender, Self::SERVICE)
}
/// Pack actions into [psibase::Action](psibase::Action).
///
/// This method returns an object which has methods
/// (one per action) which pack the action's arguments using [fracpack] and
/// return a [psibase::Action](psibase::Action). The `pack_*` series of
/// functions is mainly useful to applications which push transactions
/// to blockchains.
fn pack_from_to(sender: AccountNumber, service: AccountNumber) -> Self::Actions<ActionPacker> {
Self::with_caller(ActionPacker { sender, service })
}
}
#[derive(Clone, Default)]
pub struct RunAsCaller {
pub sender: AccountNumber,
pub service: AccountNumber,
pub allowed_actions: Vec<ServiceMethod>,
}
impl Caller for RunAsCaller {
type ReturnsNothing = ();
type ReturnType<T: UnpackOwned> = T;
fn call_returns_nothing<Args: Pack>(&self, method: MethodNumber, args: Args) {
let action = Action {
sender: self.sender,
service: self.service,
method,
rawData: args.packed().into(),
};
crate::services::transact::Wrapper::call().runAs(action, self.allowed_actions.clone());
}
fn call<Ret: UnpackOwned, Args: Pack>(&self, method: MethodNumber, args: Args) -> Ret {
let action = Action {
sender: self.sender,
service: self.service,
method,
rawData: args.packed().into(),
};
let ret =
crate::services::transact::Wrapper::call().runAs(action, self.allowed_actions.clone());
Ret::unpacked(&ret.0).unwrap()
}
}