ethexe-runtime-common 2.0.0-pre.1

Shared runtime types and storage traits for ethexe
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
// Copyright (C) Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

use crate::{
    TransitionController,
    state::{
        Dispatch, DispatchStash, Expiring, MailboxMessage, ModifiableStorage, PayloadLookup,
        ProgramState, QueryableStorage, Storage, UserMailbox, Waitlist,
    },
    transitions::is_event_destination,
};
use alloc::collections::{BTreeMap, BTreeSet};
use anyhow::Context;
use ethexe_common::{ProgramStates, Rfm, Schedule, ScheduledTask, Sd, Sum, gear::ValueClaim};
use gear_core::tasks::TaskHandler;
use gear_core_errors::SuccessReplyReason;
use gprimitives::{ActorId, H256, MessageId, ReservationId};

pub struct Handler<'a, S: Storage> {
    pub controller: TransitionController<'a, S>,
}

impl<S: Storage> TaskHandler<Rfm, Sd, Sum> for Handler<'_, S> {
    fn remove_from_mailbox(
        &mut self,
        (program_id, user_id): (ActorId, ActorId),
        message_id: MessageId,
    ) -> u64 {
        self.controller
            .update_state(program_id, |state, storage, transitions| {
                let Expiring {
                    value:
                        MailboxMessage {
                            value,
                            message_type: origin,
                            ..
                        },
                    ..
                } = storage.modify(&mut state.mailbox_hash, |mailbox| {
                    mailbox
                        .remove_and_store_user_mailbox(storage, user_id, message_id)
                        .expect("failed to find message in mailbox")
                });

                transitions.modify_transition(program_id, |transition| {
                    transition.claims.push(ValueClaim {
                        message_id,
                        destination: user_id,
                        value,
                    })
                });

                let reply = Dispatch::reply(
                    message_id,
                    user_id,
                    PayloadLookup::empty(),
                    0,
                    SuccessReplyReason::Auto,
                    origin,
                    false,
                );

                let queue = state.queue_from_msg_type(origin);
                queue.modify_queue(storage, |queue| queue.queue(reply));
            });

        0
    }

    fn send_dispatch(&mut self, (program_id, message_id): (ActorId, MessageId)) -> u64 {
        self.controller
            .update_state(program_id, |state, storage, _| {
                let dispatch = storage.modify(&mut state.stash_hash, |stash| {
                    stash.remove_to_program(&message_id)
                });

                let queue = state.queue_from_msg_type(dispatch.message_type);
                queue.modify_queue(storage, |queue| {
                    queue.queue(dispatch);
                });
            });

        0
    }

    fn send_user_message(&mut self, stashed_message_id: MessageId, program_id: ActorId) -> u64 {
        let cfg = self.controller.transitions.cfg();
        let mailbox_validity = cfg.mailbox_validity;
        let event_destinations = cfg.event_destinations_autoreply;

        self.controller
            .update_state(program_id, |state, storage, transitions| {
                let (dispatch, user_id) = storage.modify(&mut state.stash_hash, |stash| {
                    stash.remove_to_user(&stashed_message_id)
                });

                if event_destinations && is_event_destination(user_id) {
                    let value = dispatch.value;
                    let message_type = dispatch.message_type;

                    transitions.modify_transition(program_id, |transition| {
                        transition
                            .messages
                            .push(dispatch.clone().into_message(storage, user_id));
                        transition.claims.push(ValueClaim {
                            message_id: stashed_message_id,
                            destination: user_id,
                            value,
                        });
                    });

                    let reply = Dispatch::reply(
                        stashed_message_id,
                        user_id,
                        PayloadLookup::empty(),
                        0,
                        SuccessReplyReason::Auto,
                        message_type,
                        false,
                    );

                    let queue = state.queue_from_msg_type(message_type);
                    queue.modify_queue(storage, |queue| queue.queue(reply));

                    return;
                }

                let expiry = transitions.schedule_task(
                    mailbox_validity,
                    ScheduledTask::RemoveFromMailbox((program_id, user_id), stashed_message_id),
                );

                storage.modify(&mut state.mailbox_hash, |mailbox| {
                    mailbox.add_and_store_user_mailbox(
                        storage,
                        user_id,
                        stashed_message_id,
                        dispatch.clone().into(),
                        expiry,
                    );
                });

                transitions.modify_transition(program_id, |transition| {
                    transition
                        .messages
                        .push(dispatch.into_message(storage, user_id))
                })
            });

        0
    }

    // TODO (breathx): consider deprecation of delayed wakes + non-concrete waits.
    fn wake_message(&mut self, program_id: ActorId, message_id: MessageId) -> u64 {
        log::trace!("Running scheduled task wake message {message_id} to {program_id}");

        self.controller
            .update_state(program_id, |state, storage, _| {
                let Expiring {
                    value: dispatch, ..
                } = storage.modify(&mut state.waitlist_hash, |waitlist| {
                    waitlist
                        .wake(&message_id)
                        .expect("failed to find message in waitlist")
                });

                let queue = state.queue_from_msg_type(dispatch.message_type);
                queue.modify_queue(storage, |queue| {
                    queue.queue(dispatch);
                })
            });

        0
    }

    /* Deprecated APIs */
    fn remove_from_waitlist(&mut self, _program_id: ActorId, _message_id: MessageId) -> u64 {
        unreachable!("considering deprecation of it; use `wake_message` instead")
    }
    fn remove_gas_reservation(&mut self, _: ActorId, _: ReservationId) -> u64 {
        unreachable!("deprecated")
    }
}

/// A [`Schedule`] restorer.
///
/// Used primary for fast sync and tests.
///
/// No expiry filtering is applied: every scheduled task found in the dumped
/// states is restored. Committed states never hold a task already expired at
/// the dumped block, and the executor drains the full backlog with no lower
/// bound, so any restored task fires at the first computed block regardless of
/// its expiry.
#[derive(Default)]
pub struct Restorer {
    schedule: Schedule,
}

impl Restorer {
    /// Creates an empty restorer.
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates a restorer from storage.
    ///
    /// Tries to fully restore schedule
    pub fn from_storage<T: Storage>(
        storage: &T,
        program_states: &ProgramStates,
    ) -> anyhow::Result<Self> {
        let program_states: BTreeMap<H256, BTreeSet<ActorId>> =
            program_states
                .iter()
                .fold(BTreeMap::new(), |mut acc, (&program_id, state)| {
                    acc.entry(state.hash).or_default().insert(program_id);
                    acc
                });

        let mut restorer = Self::new();

        for (hash, program_ids) in program_states {
            let program_state = storage
                .program_state(hash)
                .context("failed to read ['Waitlist'] from storage by hash")?;
            let ProgramState {
                waitlist_hash,
                stash_hash,
                mailbox_hash,
                ..
            } = program_state;

            if let Ok(waitlist) = storage.query(&waitlist_hash) {
                for &program_id in &program_ids {
                    restorer.waitlist(program_id, &waitlist);
                }
            }

            if let Ok(stash) = storage.query(&stash_hash) {
                for &program_id in &program_ids {
                    restorer.stash(program_id, &stash);
                }
            }

            if let Ok(mailbox) = storage.query(&mailbox_hash) {
                for (&user_id, &user_mailbox) in mailbox.as_ref() {
                    let user_mailbox = storage
                        .user_mailbox(user_mailbox)
                        .context("failed to read ['UserMailbox'] from storage by hash")?;

                    for &program_id in &program_ids {
                        restorer.user_mailbox(program_id, user_id, &user_mailbox)
                    }
                }
            }
        }

        Ok(restorer)
    }

    pub fn waitlist(&mut self, program_id: ActorId, waitlist: &Waitlist) {
        for (
            &message_id,
            &Expiring {
                value: ref dispatch,
                expiry,
            },
        ) in waitlist.as_ref()
        {
            debug_assert_eq!(message_id, dispatch.id);

            self.schedule
                .entry(expiry)
                .or_default()
                .insert(ScheduledTask::WakeMessage(program_id, message_id));
        }
    }

    pub fn user_mailbox(
        &mut self,
        program_id: ActorId,
        user_id: ActorId,
        user_mailbox: &UserMailbox,
    ) {
        for (&message_id, &Expiring { value: _, expiry }) in user_mailbox.as_ref() {
            self.schedule
                .entry(expiry)
                .or_default()
                .insert(ScheduledTask::RemoveFromMailbox(
                    (program_id, user_id),
                    message_id,
                ));
        }
    }

    pub fn stash(&mut self, program_id: ActorId, stash: &DispatchStash) {
        for (
            &message_id,
            &Expiring {
                value: (ref dispatch, user_id),
                expiry,
            },
        ) in stash.as_ref()
        {
            debug_assert_eq!(message_id, dispatch.id);

            let task = if user_id.is_some() {
                ScheduledTask::SendUserMessage {
                    message_id,
                    to_mailbox: program_id,
                }
            } else {
                ScheduledTask::SendDispatch((program_id, message_id))
            };

            self.schedule.entry(expiry).or_default().insert(task);
        }
    }

    pub fn restore(self) -> Schedule {
        self.schedule
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::state::{Mailbox, MemStorage};
    use ethexe_common::gear::MessageType;
    use gear_core::buffer::Payload;
    use std::collections::{BTreeMap, BTreeSet};

    #[test]
    fn restorer_waitlist() {
        let program_id = ActorId::from(1);

        let dispatch = Dispatch::reply(
            MessageId::from(456),
            ActorId::from(789),
            PayloadLookup::Direct(Payload::repeat(0xfe)),
            0xffffff,
            SuccessReplyReason::Auto,
            MessageType::Canonical,
            false,
        );

        let mut waitlist = Waitlist::default();
        waitlist.wait(dispatch.clone(), 1000);

        let mut restorer = Restorer::new();
        restorer.waitlist(program_id, &waitlist);
        assert_eq!(
            restorer.restore(),
            BTreeMap::from([(
                1000,
                BTreeSet::from([ScheduledTask::WakeMessage(program_id, dispatch.id)])
            )])
        );
    }

    #[test]
    fn restorer_mailbox() {
        let storage = MemStorage::default();

        let program_id = ActorId::from(1);
        let user_id = ActorId::from(2);
        let message_id = MessageId::from(3);
        let message = MailboxMessage::new(
            PayloadLookup::Direct(Payload::repeat(0xfe)),
            0xffffff,
            MessageType::Canonical,
        );

        let mut mailbox = Mailbox::default();
        mailbox.add_and_store_user_mailbox(&storage, user_id, message_id, message, 1000);
        let user_mailbox = mailbox
            .as_ref()
            .iter()
            .next()
            .map(|(&mailbox_user_id, &user_mailbox)| {
                assert_eq!(user_id, mailbox_user_id);
                storage.user_mailbox(user_mailbox).unwrap()
            })
            .unwrap();

        let mut restorer = Restorer::new();
        restorer.user_mailbox(program_id, user_id, &user_mailbox);
        assert_eq!(
            restorer.restore(),
            BTreeMap::from([(
                1000,
                BTreeSet::from([ScheduledTask::RemoveFromMailbox(
                    (program_id, user_id),
                    message_id
                )])
            )]),
        );
    }

    #[test]
    fn restorer_stash() {
        let program_id = ActorId::from(1);
        let program_dispatch = Dispatch::reply(
            MessageId::from(456),
            ActorId::from(789),
            PayloadLookup::Direct(Payload::repeat(0xfe)),
            0xffffff,
            SuccessReplyReason::Auto,
            MessageType::Canonical,
            false,
        );

        let user_id = ActorId::from(2);
        let user_dispatch = Dispatch::reply(
            MessageId::from(789),
            ActorId::from(999),
            PayloadLookup::Direct(Payload::repeat(0xaa)),
            0xbbbbbb,
            SuccessReplyReason::Auto,
            MessageType::Canonical,
            false,
        );

        let mut stash = DispatchStash::default();
        stash.add_to_program(program_dispatch.clone(), 1000);
        stash.add_to_user(user_dispatch.clone(), 1000, user_id);

        let mut restorer = Restorer::new();
        restorer.stash(program_id, &stash);
        assert_eq!(
            restorer.restore(),
            BTreeMap::from([(
                1000,
                BTreeSet::from([
                    ScheduledTask::SendDispatch((program_id, program_dispatch.id)),
                    ScheduledTask::SendUserMessage {
                        message_id: user_dispatch.id,
                        to_mailbox: program_id
                    }
                ])
            )]),
        );
    }

    // Regression for #5574: the genesis `from_storage` path must restore every
    // scheduled task in the dumped states regardless of its expiry. The restorer
    // used to drop tasks with `expiry <= genesis_block_height`; during re-genesis
    // that height can sit far above a task's expiry, so still-pending tasks were
    // silently lost. Here all expiries are tiny (1..=4) — exactly the entries the
    // old cutoff would have discarded — and all four must survive restoration.
    #[test]
    fn from_storage_restores_tasks_below_genesis_height() {
        use ethexe_common::StateHashWithQueueSize;

        let storage = MemStorage::default();
        let program_id = ActorId::from(1);
        let user_id = ActorId::from(2);

        let reply = |id: u64, fill: u8| {
            Dispatch::reply(
                MessageId::from(id),
                ActorId::from(id + 1000),
                PayloadLookup::Direct(Payload::repeat(fill)),
                0xffffff,
                SuccessReplyReason::Auto,
                MessageType::Canonical,
                false,
            )
        };

        let waitlisted = reply(10, 0x11);
        let stashed_to_program = reply(11, 0x22);
        let stashed_to_user = reply(12, 0x33);
        let mailbox_message_id = MessageId::from(13);

        let mut waitlist = Waitlist::default();
        waitlist.wait(waitlisted.clone(), 1);

        let mut stash = DispatchStash::default();
        stash.add_to_program(stashed_to_program.clone(), 2);
        stash.add_to_user(stashed_to_user.clone(), 3, user_id);

        let mut mailbox = Mailbox::default();
        mailbox.add_and_store_user_mailbox(
            &storage,
            user_id,
            mailbox_message_id,
            MailboxMessage::new(
                PayloadLookup::Direct(Payload::repeat(0x44)),
                0xffffff,
                MessageType::Canonical,
            ),
            4,
        );

        let mut state = ProgramState::zero();
        state.waitlist_hash = waitlist.store(&storage).expect("waitlist changed");
        state.stash_hash = stash.store(&storage);
        state.mailbox_hash = mailbox.store(&storage).expect("mailbox changed");
        let state_hash = storage.write_program_state(state);

        let program_states = ProgramStates::from([(
            program_id,
            StateHashWithQueueSize {
                hash: state_hash,
                canonical_queue_size: 0,
                injected_queue_size: 0,
            },
        )]);

        let schedule = Restorer::from_storage(&storage, &program_states)
            .expect("restore must succeed")
            .restore();

        assert_eq!(
            schedule,
            BTreeMap::from([
                (
                    1,
                    BTreeSet::from([ScheduledTask::WakeMessage(program_id, waitlisted.id)])
                ),
                (
                    2,
                    BTreeSet::from([ScheduledTask::SendDispatch((
                        program_id,
                        stashed_to_program.id
                    ))])
                ),
                (
                    3,
                    BTreeSet::from([ScheduledTask::SendUserMessage {
                        message_id: stashed_to_user.id,
                        to_mailbox: program_id,
                    }])
                ),
                (
                    4,
                    BTreeSet::from([ScheduledTask::RemoveFromMailbox(
                        (program_id, user_id),
                        mailbox_message_id
                    )])
                ),
            ]),
        );
    }

    #[test]
    fn send_user_message_to_event_destination_skips_mailbox() {
        use crate::{
            InBlockTransitions, TransitionController, TransitionsConfig,
            transitions::{ETH_SAILS_EVENT, GEAR_SAILS_EVENT},
        };
        use ethexe_common::{ProgramStates, StateHashWithQueueSize};
        use gear_core::{
            ids::prelude::MessageIdExt,
            message::{DispatchKind, ReplyCode},
        };

        for destination in [GEAR_SAILS_EVENT, ETH_SAILS_EVENT] {
            let storage = MemStorage::default();
            let program_id = ActorId::from(7);
            let message_id = MessageId::from(10);

            let dispatch = Dispatch::new(
                &storage,
                message_id,
                program_id,
                vec![1, 2, 3],
                11,
                false,
                MessageType::Canonical,
                false,
            )
            .expect("dispatch");

            let mut stash = DispatchStash::default();
            stash.add_to_user(dispatch, 1000, destination);

            let mut state = ProgramState::zero();
            state.stash_hash = stash.store(&storage);
            let state_hash = storage.write_program_state(state);
            let states = ProgramStates::from_iter([(
                program_id,
                StateHashWithQueueSize {
                    hash: state_hash,
                    canonical_queue_size: 0,
                    injected_queue_size: 0,
                },
            )]);

            let cfg = TransitionsConfig {
                event_destinations_autoreply: true,
                ..Default::default()
            };
            let mut transitions = InBlockTransitions::new(cfg, states, Default::default());

            {
                let mut handler = Handler {
                    controller: TransitionController {
                        storage: &storage,
                        transitions: &mut transitions,
                    },
                };
                handler.send_user_message(message_id, program_id);
            }

            let transition = transitions.modifications_mut().remove(&program_id).unwrap();
            assert_eq!(transition.messages.len(), 1);
            assert_eq!(transition.messages[0].destination, destination);
            assert_eq!(transition.messages[0].value, 11);
            assert_eq!(transition.claims.len(), 1);
            assert_eq!(transition.claims[0].message_id, message_id);
            assert_eq!(transition.claims[0].destination, destination);
            assert_eq!(transition.claims[0].value, 11);

            let state_hash = transitions.state_of(&program_id).unwrap().hash;
            let state = storage.program_state(state_hash).unwrap();
            assert!(state.mailbox_hash.is_empty());

            let mut queue = state.canonical_queue.query(&storage).unwrap();
            let reply = queue.dequeue().expect("auto reply must be queued");
            assert_eq!(reply.id, MessageId::generate_reply(message_id));
            assert_eq!(reply.kind, DispatchKind::Reply);
            assert_eq!(reply.source, destination);
            assert_eq!(
                reply
                    .details
                    .unwrap()
                    .to_reply_details()
                    .unwrap()
                    .to_reply_code(),
                ReplyCode::Success(SuccessReplyReason::Auto)
            );
            assert!(queue.is_empty());
        }
    }

    #[test]
    fn send_user_message_to_regular_user_uses_mailbox() {
        use crate::{InBlockTransitions, TransitionController, TransitionsConfig};
        use ethexe_common::{ProgramStates, StateHashWithQueueSize};

        let storage = MemStorage::default();
        let program_id = ActorId::from(7);
        let user_id = ActorId::from(2);
        let message_id = MessageId::from(10);

        let dispatch = Dispatch::new(
            &storage,
            message_id,
            program_id,
            vec![1, 2, 3],
            11,
            false,
            MessageType::Canonical,
            false,
        )
        .expect("dispatch");

        let mut stash = DispatchStash::default();
        stash.add_to_user(dispatch, 1000, user_id);

        let mut state = ProgramState::zero();
        state.stash_hash = stash.store(&storage);
        let state_hash = storage.write_program_state(state);
        let states = ProgramStates::from_iter([(
            program_id,
            StateHashWithQueueSize {
                hash: state_hash,
                canonical_queue_size: 0,
                injected_queue_size: 0,
            },
        )]);

        let cfg = TransitionsConfig {
            event_destinations_autoreply: true,
            ..Default::default()
        };
        let mut transitions = InBlockTransitions::new(cfg, states, Default::default());

        {
            let mut handler = Handler {
                controller: TransitionController {
                    storage: &storage,
                    transitions: &mut transitions,
                },
            };
            handler.send_user_message(message_id, program_id);
        }

        let transition = transitions.modifications_mut().remove(&program_id).unwrap();
        assert_eq!(transition.messages.len(), 1);
        assert_eq!(transition.messages[0].destination, user_id);
        assert_eq!(transition.messages[0].value, 11);
        assert!(transition.claims.is_empty());

        let state_hash = transitions.state_of(&program_id).unwrap().hash;
        let state = storage.program_state(state_hash).unwrap();
        assert!(!state.mailbox_hash.is_empty());
        assert!(state.stash_hash.is_empty());
        assert!(state.canonical_queue.is_empty());
    }
}