sails-rs 1.0.1

Main abstractions for the Sails framework
Documentation
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
use super::*;
pub use ::gtest::constants::{
    DEFAULT_USER_ALICE, DEFAULT_USER_BOB, DEFAULT_USER_CHARLIE, DEFAULT_USER_EVE,
    DEFAULT_USERS_INITIAL_BALANCE, EPOCH_DURATION_IN_BLOCKS, MAX_USER_GAS_LIMIT,
};
use ::gtest::{BlockRunResult, System, TestError};
use core::{cell::RefCell, task::ready};
use futures::{
    Stream,
    channel::{mpsc, oneshot},
};
pub use gear_core_errors::{ErrorReplyReason, SimpleExecutionError};
use hashbrown::HashMap;
use std::rc::Rc;
use tokio_stream::StreamExt;

const GAS_LIMIT_DEFAULT: ::gtest::constants::Gas = ::gtest::constants::MAX_USER_GAS_LIMIT;
type EventSender = mpsc::UnboundedSender<(ActorId, Vec<u8>)>;
type ReplySender = oneshot::Sender<Result<Vec<u8>, GtestError>>;
type ReplyReceiver = oneshot::Receiver<Result<Vec<u8>, GtestError>>;

#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
pub enum GtestError {
    #[error(transparent)]
    Env(#[from] TestError),
    #[error("reply error: {0}")]
    ReplyHasError(ErrorReplyReason, crate::Vec<u8>),
    #[error("reply is missing")]
    ReplyIsMissing,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum BlockRunMode {
    /// Run blocks until all pending replies are received.
    Auto,
    /// Run up to the given number of blocks. Any pending reply not produced
    /// within that limit resolves with `GtestError::ReplyIsMissing`.
    UpTo(BlockCount),
    /// Run only the next block. Any reply not produced in that block
    /// resolves with `GtestError::ReplyIsMissing`.
    Next,
    /// Sending messages does not advance the chain.
    /// Use `GtestEnv::run_next_block` to advance manually.
    Manual,
}

#[derive(Clone)]
pub struct GtestEnv {
    system: Rc<System>,
    actor_id: ActorId,
    event_senders: Rc<RefCell<Vec<EventSender>>>,
    block_run_mode: BlockRunMode,
    block_reply_senders: Rc<RefCell<HashMap<MessageId, ReplySender>>>,
}

crate::params_struct_impl!(
    GtestEnv,
    GtestParams {
        actor_id: ActorId,
        #[cfg(not(feature = "ethexe"))]
        gas_limit: GasUnit,
        value: ValueUnit,
    }
);

impl GtestEnv {
    /// Create new `GtestEnv` instance from `gtest::System` with specified `actor_id`
    /// and `UpTo(EPOCH_DURATION_IN_BLOCKS)` block run mode.
    pub fn new(system: System, actor_id: ActorId) -> Self {
        let system = Rc::new(system);
        Self {
            system,
            actor_id,
            event_senders: Default::default(),
            block_run_mode: BlockRunMode::UpTo(EPOCH_DURATION_IN_BLOCKS),
            block_reply_senders: Default::default(),
        }
    }

    /// Create `GtestEnv` instance with new `System` and `DEFAULT_USER_ALICE` actor
    pub fn system_default() -> Self {
        let system = System::new();
        system.init_logger_with_default_filter("gwasm=debug,gtest=info,sails_rs=debug");
        system.mint_to(DEFAULT_USER_ALICE, DEFAULT_USERS_INITIAL_BALANCE);

        GtestEnv::new(system, DEFAULT_USER_ALICE.into())
    }

    /// Avoid calling methods of `System` related to block execution.
    /// Use `GtestEnv::run_next_block` instead. This method can be used
    /// for obtaining reference data like balance, timestamp, etc.
    pub fn system(&self) -> &System {
        &self.system
    }

    pub fn with_block_run_mode(self, block_run_mode: BlockRunMode) -> Self {
        Self {
            block_run_mode,
            ..self
        }
    }

    pub fn with_actor_id(self, actor_id: ActorId) -> Self {
        Self { actor_id, ..self }
    }

    pub fn actor_id(&self) -> ActorId {
        self.actor_id
    }

    pub fn run_next_block(&self) {
        _ = self.run_next_block_and_extract();
    }
}

impl GtestEnv {
    fn extract_events_and_replies(&self, run_result: &BlockRunResult) {
        log::debug!(
            "Process block #{} run result, mode {:?}",
            run_result.block_info.height,
            &self.block_run_mode
        );
        let mut event_senders = self.event_senders.borrow_mut();
        let mut reply_senders = self.block_reply_senders.borrow_mut();
        event_senders.retain(|c| !c.is_closed());
        for entry in run_result.log().iter() {
            if entry.destination() == ActorId::zero() {
                log::debug!("Extract event from entry {entry:?}");
                for sender in event_senders.iter() {
                    _ = sender.unbounded_send((entry.source(), entry.payload().to_vec()));
                }
                continue;
            }
            #[cfg(feature = "ethexe")]
            if entry.destination() == crate::solidity::ETH_EVENT_ADDR {
                log::debug!("Extract event from entry {:?}", entry);
                for sender in event_senders.iter() {
                    _ = sender.unbounded_send((entry.source(), entry.payload().to_vec()));
                }
                continue;
            }
            if let Some(message_id) = entry.reply_to()
                && let Some(sender) = reply_senders.remove(&message_id)
            {
                log::debug!("Extract reply from entry {entry:?}");
                let reply: result::Result<Vec<u8>, _> = match entry.reply_code() {
                    Some(ReplyCode::Success(_)) => Ok(entry.payload().to_vec()),
                    Some(ReplyCode::Error(reason)) => {
                        Err(GtestError::ReplyHasError(reason, entry.payload().to_vec()))
                    }
                    _ => Err(GtestError::ReplyIsMissing),
                };
                _ = sender.send(reply);
            }
        }
    }

    pub fn create_program(
        &self,
        code_id: CodeId,
        salt: impl AsRef<[u8]>,
        payload: impl AsRef<[u8]>,
        params: GtestParams,
    ) -> Result<(ActorId, MessageId), GtestError> {
        let value = params.value.unwrap_or(0);
        #[cfg(not(feature = "ethexe"))]
        let gas_limit = params.gas_limit.unwrap_or(GAS_LIMIT_DEFAULT);
        #[cfg(feature = "ethexe")]
        let gas_limit = GAS_LIMIT_DEFAULT;
        let code = self
            .system
            .submitted_code(code_id)
            .ok_or(TestError::Instrumentation)?;
        let program_id = ::gtest::calculate_program_id(code_id, salt.as_ref(), None);
        let program = ::gtest::Program::from_binary_with_id(&self.system, program_id, code);
        let actor_id = params.actor_id.unwrap_or(self.actor_id);
        let message_id = program.send_bytes_with_gas(actor_id, payload.as_ref(), gas_limit, value);
        log::debug!("Send activation id: {message_id}, to program: {program_id}");
        Ok((program_id, message_id))
    }

    pub fn send_one_way(
        &self,
        destination: ActorId,
        payload: impl AsRef<[u8]>,
        params: GtestParams,
    ) -> Result<MessageId, GtestError> {
        let value = params.value.unwrap_or(0);
        #[cfg(not(feature = "ethexe"))]
        let gas_limit = params.gas_limit.unwrap_or(GAS_LIMIT_DEFAULT);
        #[cfg(feature = "ethexe")]
        let gas_limit = GAS_LIMIT_DEFAULT;
        let program = self
            .system
            .get_program(destination)
            .ok_or(TestError::ActorNotFound(destination))?;
        let actor_id = params.actor_id.unwrap_or(self.actor_id);
        let message_id = program.send_bytes_with_gas(actor_id, payload.as_ref(), gas_limit, value);
        log::debug!(
            "Send message id: {message_id}, to: {destination}, payload: {}",
            hex::encode(payload.as_ref())
        );
        Ok(message_id)
    }

    pub async fn send_for_reply(
        &self,
        destination: ActorId,
        payload: impl AsRef<[u8]>,
        params: GtestParams,
    ) -> Result<Vec<u8>, GtestError> {
        let message_id = self.send_one_way(destination, payload, params)?;
        self.message_reply_from_next_blocks(message_id)
            .await
            .unwrap_or(Err(GtestError::ReplyIsMissing))
    }

    pub fn message_reply_from_next_blocks(&self, message_id: MessageId) -> ReplyReceiver {
        let (tx, rx) = oneshot::channel::<Result<Vec<u8>, GtestError>>();
        self.block_reply_senders.borrow_mut().insert(message_id, tx);

        match self.block_run_mode {
            BlockRunMode::Auto => {
                self.run_until_extract_replies();
            }
            BlockRunMode::UpTo(block_limit) => {
                self.run_until_extract_replies_up_to(block_limit);
            }
            BlockRunMode::Next => {
                self.run_next_block_and_extract();
                self.drain_reply_senders();
            }
            BlockRunMode::Manual => (),
        };
        rx
    }

    pub fn query(
        &self,
        destination: ActorId,
        payload: impl AsRef<[u8]>,
        params: GtestParams,
    ) -> Result<Vec<u8>, GtestError> {
        let value = params.value.unwrap_or(0);
        #[cfg(not(feature = "ethexe"))]
        let gas_limit = params.gas_limit.unwrap_or(GAS_LIMIT_DEFAULT);
        #[cfg(feature = "ethexe")]
        let gas_limit = GAS_LIMIT_DEFAULT;

        let actor_id = params.actor_id.unwrap_or(self.actor_id);
        let reply_info = self
            .system
            .calculate_reply_for_handle(actor_id, destination, payload.as_ref(), gas_limit, value)
            .map_err(|_s| GtestError::ReplyIsMissing)?;

        match reply_info.code {
            ReplyCode::Success(_) => Ok(reply_info.payload),
            ReplyCode::Error(err) => Err(GtestError::ReplyHasError(err, reply_info.payload)),
            _ => {
                log::debug!("Unexpected reply code: {:?}", reply_info.code);
                Err(GtestError::ReplyIsMissing)
            }
        }
    }

    fn run_next_block_and_extract(&self) -> BlockRunResult {
        let run_result = self.system.run_next_block();
        self.extract_events_and_replies(&run_result);
        run_result
    }

    fn run_until_extract_replies(&self) {
        while !self.block_reply_senders.borrow().is_empty() {
            self.run_next_block_and_extract();
        }
    }

    fn run_until_extract_replies_up_to(&self, block_limit: BlockCount) {
        for _ in 0..block_limit {
            if self.block_reply_senders.borrow().is_empty() {
                return;
            }
            self.run_next_block_and_extract();
        }
        self.drain_reply_senders();
    }

    fn drain_reply_senders(&self) {
        let mut reply_senders = self.block_reply_senders.borrow_mut();
        for (message_id, sender) in reply_senders.drain() {
            log::debug!("Reply is missing in block for message {message_id}");
            _ = sender.send(Err(GtestError::ReplyIsMissing));
        }
    }
}

impl ReplyError for GtestError {
    fn from_codec_error(err: parity_scale_codec::Error) -> Self {
        TestError::ScaleCodecError(err).into()
    }

    fn userspace_panic_payload(&self) -> Option<&[u8]> {
        match self {
            GtestError::ReplyHasError(
                ErrorReplyReason::Execution(SimpleExecutionError::UserspacePanic),
                payload,
            ) => Some(payload),
            _ => None,
        }
    }
}

impl GearEnv for GtestEnv {
    type Params = GtestParams;
    type Error = GtestError;
    type MessageState = ReplyReceiver;
}

impl<T: ServiceCall> PendingCall<T, GtestEnv> {
    pub fn send_one_way(&mut self) -> Result<MessageId, GtestError> {
        if self.state.is_some() {
            panic!("{PENDING_CALL_INVALID_STATE}");
        }
        let (payload, params) = self.take_encoded_args_and_params();
        let message_id = self.env.send_one_way(self.destination, payload, params)?;
        log::debug!("PendingCall: send message {message_id:?}");
        Ok(message_id)
    }

    pub fn send_for_reply(mut self) -> Result<Self, GtestError> {
        let message_id = self.send_one_way()?;
        self.state = Some(self.env.message_reply_from_next_blocks(message_id));
        Ok(self)
    }

    pub fn query(mut self) -> Result<T::Output, GtestError> {
        let (payload, params) = self.take_encoded_args_and_params();
        let reply = self.env.query(self.destination, payload, params);
        decode_reply_or_throw::<T, _>(&self.route, reply)
    }
}

impl<T: ServiceCall> Future for PendingCall<T, GtestEnv> {
    type Output = Result<T::Output, <GtestEnv as GearEnv>::Error>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        if self.state.is_none() {
            let (payload, params) = self.take_encoded_args_and_params();
            let send_res = self.env.send_one_way(self.destination, payload, params);
            match send_res {
                Ok(message_id) => {
                    log::debug!("PendingCall: send message {message_id:?}");
                    self.state = Some(self.env.message_reply_from_next_blocks(message_id));
                }
                Err(err) => {
                    log::error!("PendingCall: failed to send message: {err}");
                    return Poll::Ready(Err(err));
                }
            }
        }
        let this = self.as_mut().project();
        let reply_receiver = this
            .state
            .as_pin_mut()
            .unwrap_or_else(|| panic!("{PENDING_CALL_INVALID_STATE}"));
        match ready!(reply_receiver.poll(cx)) {
            Ok(res) => Poll::Ready(decode_reply_or_throw::<T, _>(this.route, res)),
            Err(_) => Poll::Ready(Err(GtestError::ReplyIsMissing)),
        }
    }
}

impl<A, T: ServiceCall> PendingCtor<A, T, GtestEnv> {
    pub fn create_program(mut self) -> Result<Self, GtestError> {
        if self.state.is_some() {
            panic!("{PENDING_CTOR_INVALID_STATE}");
        }
        let args = self
            .args
            .take()
            .unwrap_or_else(|| panic!("{PENDING_CTOR_INVALID_STATE}"));
        let payload = T::encode_call(&self.route, &args);
        let params = self.params.take().unwrap_or_default();
        let salt = self.salt.take().unwrap_or_default();
        let send_res = self
            .env
            .create_program(self.code_id, salt, payload.as_slice(), params);
        match send_res {
            Ok((program_id, message_id)) => {
                log::debug!("PendingCtor: send message {message_id:?}");
                self.state = Some(self.env.message_reply_from_next_blocks(message_id));
                self.program_id = Some(program_id);
                Ok(self)
            }
            Err(err) => {
                log::error!("PendingCtor: failed to send message: {err}");
                Err(err)
            }
        }
    }
}

impl<A, T> Future for PendingCtor<A, T, GtestEnv>
where
    T: ServiceCall,
    T::Output: PendingCtorOutput<A, GtestEnv>,
{
    type Output =
        Result<<T::Output as PendingCtorOutput<A, GtestEnv>>::Output, <GtestEnv as GearEnv>::Error>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        if self.state.is_none() {
            let args = self
                .args
                .take()
                .unwrap_or_else(|| panic!("{PENDING_CTOR_INVALID_STATE}"));
            let payload = T::encode_call(&self.route, &args);
            let params = self.params.take().unwrap_or_default();
            let salt = self.salt.take().unwrap_or_default();
            let send_res = self
                .env
                .create_program(self.code_id, salt, payload.as_slice(), params);
            match send_res {
                Ok((program_id, message_id)) => {
                    log::debug!("PendingCtor: send message {message_id:?}");
                    self.state = Some(self.env.message_reply_from_next_blocks(message_id));
                    self.program_id = Some(program_id);
                }
                Err(err) => {
                    log::error!("PendingCtor: failed to send message: {err}");
                    return Poll::Ready(Err(err));
                }
            }
        }
        let route = self.route.clone();
        let this = self.as_mut().project();
        let reply_receiver = this
            .state
            .as_pin_mut()
            .unwrap_or_else(|| panic!("{PENDING_CTOR_INVALID_STATE}"));
        match ready!(reply_receiver.poll(cx)) {
            Ok(res) => match decode_reply_or_throw::<T, _>(&route, res) {
                Ok(output) => {
                    let program_id = this
                        .program_id
                        .take()
                        .unwrap_or_else(|| panic!("{PENDING_CTOR_INVALID_STATE}"));
                    Poll::Ready(Ok(output.map_result(this.env.clone(), program_id)))
                }
                Err(err) => Poll::Ready(Err(err)),
            },
            Err(_) => Poll::Ready(Err(GtestError::ReplyIsMissing)),
        }
    }
}

impl Listener for GtestEnv {
    type Error = <GtestEnv as GearEnv>::Error;

    async fn listen<E, F: FnMut((ActorId, Vec<u8>)) -> Option<(ActorId, E)>>(
        &self,
        f: F,
    ) -> Result<impl Stream<Item = (ActorId, E)> + Unpin + use<E, F>, Self::Error> {
        let (tx, rx) = mpsc::unbounded::<(ActorId, Vec<u8>)>();
        self.event_senders.borrow_mut().push(tx);
        Ok(rx.filter_map(f))
    }
}

impl<T> Actor<T, GtestEnv> {
    pub fn balance(&self) -> ValueUnit {
        self.env.system().balance_of(self.id)
    }
}