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
// Copyright (C) Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

use alloc::{
    collections::{BTreeMap, btree_map::Iter},
    vec::Vec,
};
use anyhow::{Result, anyhow};
use core::num::NonZero;
use ethexe_common::{
    MAILBOX_VALIDITY_VERSION_2, ProgramStates, Schedule, ScheduledTask, StateHashWithQueueSize,
    gear::{Message, StateTransition, ValueClaim},
};
use gprimitives::{ActorId, CodeId, H256};

pub(crate) const GEAR_SAILS_EVENT: ActorId = ActorId::new([0; 32]);

/// Must match `gstd`/`gcore` `ETH_EVENT_ADDR`
/// (`0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF` on Ethereum).
pub(crate) const ETH_SAILS_EVENT: ActorId = ActorId::new([
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
]);

pub(crate) fn is_event_destination(destination: ActorId) -> bool {
    matches!(destination, GEAR_SAILS_EVENT | ETH_SAILS_EVENT)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TransitionsConfig {
    pub block_height: u32,
    pub mailbox_validity: NonZero<u32>,
    pub event_destinations_autoreply: bool,
}

impl Default for TransitionsConfig {
    fn default() -> Self {
        Self {
            block_height: 0,
            mailbox_validity: MAILBOX_VALIDITY_VERSION_2,
            event_destinations_autoreply: false,
        }
    }
}

/// In-memory store for the state transitions
/// that are going to be applied in the current block.
///
/// The type is instantiated with states taken from the parent
/// block, as parent block stores latest states to be possibly
/// updated in the current block.
///
/// The type actually stores latest state transitions, which are going to be
/// applied in the current block.
#[derive(Debug, Default)]
pub struct InBlockTransitions {
    cfg: TransitionsConfig,
    states: ProgramStates,
    schedule: Schedule,
    modifications: BTreeMap<ActorId, NonFinalTransition>,
    program_creations: BTreeMap<ActorId, CodeId>,
}

#[derive(Debug, Clone, Default)]
pub struct FinalizedBlockTransitions {
    pub transitions: Vec<StateTransition>,
    pub states: ProgramStates,
    pub schedule: Schedule,
    pub program_creations: Vec<(ActorId, CodeId)>,
}

impl InBlockTransitions {
    pub fn new(cfg: TransitionsConfig, states: ProgramStates, schedule: Schedule) -> Self {
        Self {
            cfg,
            states,
            schedule,
            ..Default::default()
        }
    }

    pub fn is_program(&self, actor_id: &ActorId) -> bool {
        self.states.contains_key(actor_id)
    }

    pub fn state_of(&self, actor_id: &ActorId) -> Option<StateHashWithQueueSize> {
        self.states.get(actor_id).copied()
    }

    pub fn states_amount(&self) -> usize {
        self.states.len()
    }

    pub fn states_iter(&self) -> Iter<'_, ActorId, StateHashWithQueueSize> {
        self.states.iter()
    }

    pub fn known_programs(&self) -> Vec<ActorId> {
        self.states.keys().copied().collect()
    }

    pub fn current_messages(&self) -> Vec<(ActorId, Message)> {
        self.modifications
            .iter()
            .flat_map(|(id, trans)| trans.messages.iter().map(|message| (*id, message.clone())))
            .collect()
    }

    pub fn modifications_len(&self) -> usize {
        self.modifications.len()
    }

    /// Drain every scheduled task whose deadline is at or before
    /// `block_height` and return them in chronological order
    /// (oldest height first; within a height, BTreeSet `Ord`).
    pub fn take_actual_tasks(&mut self) -> Vec<ScheduledTask> {
        let cutoff = self.cfg.block_height.saturating_add(1);
        let kept = self.schedule.split_off(&cutoff);
        let due = core::mem::replace(&mut self.schedule, kept);
        due.into_values().flatten().collect()
    }

    pub fn schedule_task(&mut self, in_blocks: NonZero<u32>, task: ScheduledTask) -> u32 {
        let scheduled_block = self.cfg.block_height + u32::from(in_blocks);

        self.schedule
            .entry(scheduled_block)
            .or_default()
            .insert(task);

        scheduled_block
    }

    pub fn remove_task(&mut self, expiry: u32, task: &ScheduledTask) -> Result<()> {
        let block_tasks = self
            .schedule
            .get_mut(&expiry)
            .ok_or_else(|| anyhow!("No tasks found scheduled for a given block"))?;

        block_tasks
            .remove(task)
            .then_some(())
            .ok_or_else(|| anyhow!("Requested task wasn't found scheduled for a given block"))?;

        if block_tasks.is_empty() {
            self.schedule.remove(&expiry);
        }

        Ok(())
    }

    pub fn register_new(&mut self, actor_id: ActorId, code_id: CodeId) {
        self.states.insert(actor_id, StateHashWithQueueSize::zero());
        self.modifications.insert(actor_id, Default::default());
        self.program_creations.insert(actor_id, code_id);
    }

    pub fn registered_programs(&self) -> &BTreeMap<ActorId, CodeId> {
        &self.program_creations
    }

    pub fn modify_state(
        &mut self,
        actor_id: ActorId,
        new_state_hash: H256,
        canonical_queue_size: u8,
        injected_queue_size: u8,
    ) {
        self.modify(actor_id, |state, _transition| {
            state.hash = new_state_hash;
            state.canonical_queue_size = canonical_queue_size;
            state.injected_queue_size = injected_queue_size;
        })
    }

    pub fn modify_transition<T>(
        &mut self,
        actor_id: ActorId,
        f: impl FnOnce(&mut NonFinalTransition) -> T,
    ) -> T {
        self.modify(actor_id, |_state, transition| f(transition))
    }

    pub fn claim_value(&mut self, actor_id: ActorId, claim: ValueClaim) {
        self.modify(actor_id, |_state, transition| {
            transition.value_to_receive = transition
                .value_to_receive
                .checked_add(
                    i128::try_from(claim.value).expect("claimed_value doesn't fit in i128"),
                )
                .expect("Overflow in transition.value_to_receive += claimed_value");

            transition.claims.push(claim);
        });
    }

    pub fn modify<T>(
        &mut self,
        actor_id: ActorId,
        f: impl FnOnce(&mut StateHashWithQueueSize, &mut NonFinalTransition) -> T,
    ) -> T {
        let initial_state = self
            .states
            .get_mut(&actor_id)
            .expect("couldn't modify transition for unknown actor");

        let transition = self
            .modifications
            .entry(actor_id)
            .or_insert(NonFinalTransition {
                initial_state: initial_state.hash,
                ..Default::default()
            });

        f(initial_state, transition)
    }

    pub fn finalize(self) -> FinalizedBlockTransitions {
        let Self {
            states,
            schedule,
            modifications,
            program_creations,
            ..
        } = self;

        let mut transitions = Vec::with_capacity(modifications.len());

        for (actor_id, modification) in modifications {
            let new_state = states
                .get(&actor_id)
                .cloned()
                .expect("failed to find state record for modified state");

            if !modification.is_noop(new_state.hash) {
                transitions.push(StateTransition {
                    actor_id,
                    new_state_hash: new_state.hash,
                    exited: modification.inheritor.is_some(),
                    inheritor: modification.inheritor.unwrap_or_default(),
                    value_to_receive: modification.value_to_receive.unsigned_abs(),
                    value_to_receive_negative_sign: modification.value_to_receive < 0,
                    value_claims: modification.claims,
                    messages: modification.messages,
                });
            }
        }

        FinalizedBlockTransitions {
            transitions,
            states,
            schedule,
            program_creations: program_creations.into_iter().collect(),
        }
    }

    pub fn cfg(&self) -> &TransitionsConfig {
        &self.cfg
    }

    #[cfg(any(test, feature = "mock"))]
    pub fn from_parts(
        cfg: TransitionsConfig,
        states: ProgramStates,
        schedule: Schedule,
        modifications: BTreeMap<ActorId, NonFinalTransition>,
        program_creations: BTreeMap<ActorId, CodeId>,
    ) -> Self {
        Self {
            cfg,
            states,
            schedule,
            modifications,
            program_creations,
        }
    }

    #[cfg(any(test, feature = "mock"))]
    pub fn modifications_mut(&mut self) -> &mut BTreeMap<ActorId, NonFinalTransition> {
        &mut self.modifications
    }

    #[cfg(any(test, feature = "mock"))]
    pub fn cfg_mut(&mut self) -> &mut TransitionsConfig {
        &mut self.cfg
    }
}

#[derive(Debug, Default, Clone)]
pub struct NonFinalTransition {
    initial_state: H256,
    pub inheritor: Option<ActorId>,
    pub value_to_receive: i128,
    pub claims: Vec<ValueClaim>,
    pub messages: Vec<Message>,
}

impl NonFinalTransition {
    pub fn is_noop(&self, current_state: H256) -> bool {
        // check if just created program (always op)
        !self.initial_state.is_zero()
            // check if state hash changed at final (always op)
            && current_state == self.initial_state
            // check if with unchanged state needs commitment (op)
            && (self.inheritor.is_none() && self.value_to_receive == 0 && self.claims.is_empty() && self.messages.is_empty())
    }

    #[cfg(any(test, feature = "mock"))]
    pub fn initial_state(&self) -> H256 {
        self.initial_state
    }

    #[cfg(any(test, feature = "mock"))]
    pub fn new(
        initial_state: H256,
        inheritor: Option<ActorId>,
        value_to_receive: i128,
        claims: Vec<ValueClaim>,
        messages: Vec<Message>,
    ) -> Self {
        Self {
            initial_state,
            inheritor,
            value_to_receive,
            claims,
            messages,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::collections::BTreeSet;
    use ethexe_common::ScheduledTask;
    use gprimitives::MessageId;

    fn wake(actor: u8, msg: u8) -> ScheduledTask {
        ScheduledTask::WakeMessage(ActorId::from([actor; 32]), MessageId::from([msg; 32]))
    }

    fn transitions_with_schedule(block_height: u32, schedule: Schedule) -> InBlockTransitions {
        let cfg = TransitionsConfig {
            block_height,
            ..Default::default()
        };
        InBlockTransitions::new(cfg, ProgramStates::default(), schedule)
    }

    #[test]
    fn take_actual_tasks_single_height() {
        let mut schedule = Schedule::new();
        schedule
            .entry(10)
            .or_default()
            .extend([wake(1, 1), wake(2, 2)]);
        let mut t = transitions_with_schedule(10, schedule);

        let drained = t.take_actual_tasks();
        let drained: BTreeSet<_> = drained.into_iter().collect();
        assert_eq!(drained, BTreeSet::from([wake(1, 1), wake(2, 2)]));
        assert!(t.schedule.is_empty(), "all due heights drained");
    }

    /// Tasks left over from earlier MBs (heights < current) must fire on the
    /// next pass — that's the MB-driven invariant. Future heights stay put.
    #[test]
    fn take_actual_tasks_drains_past_heights_keeps_future() {
        let mut schedule = Schedule::new();
        schedule.entry(5).or_default().insert(wake(1, 1));
        schedule.entry(8).or_default().insert(wake(2, 2));
        schedule.entry(10).or_default().insert(wake(3, 3));
        schedule.entry(15).or_default().insert(wake(4, 4));
        schedule.entry(20).or_default().insert(wake(5, 5));
        let mut t = transitions_with_schedule(10, schedule);

        let drained = t.take_actual_tasks();
        // Past-and-current drained.
        assert_eq!(drained, vec![wake(1, 1), wake(2, 2), wake(3, 3)]);
        // Future preserved.
        assert_eq!(t.schedule.len(), 2);
        assert!(t.schedule.contains_key(&15));
        assert!(t.schedule.contains_key(&20));
    }

    /// Chronological ordering across heights — height-major, BTreeSet `Ord`
    /// within a height. Validators must agree on this order.
    #[test]
    fn take_actual_tasks_ordering_is_height_major() {
        let mut schedule = Schedule::new();
        // Inserted out of height order; insertion order in BTreeSet doesn't matter.
        schedule.entry(20).or_default().insert(wake(0, 9));
        schedule
            .entry(5)
            .or_default()
            .extend([wake(2, 2), wake(1, 1)]);
        schedule.entry(15).or_default().insert(wake(3, 3));
        let mut t = transitions_with_schedule(20, schedule);

        let drained = t.take_actual_tasks();
        // Height 5 first (Ord-sorted within), then 15, then 20.
        assert_eq!(
            drained,
            vec![wake(1, 1), wake(2, 2), wake(3, 3), wake(0, 9)]
        );
        assert!(t.schedule.is_empty());
    }

    /// Empty schedule → no tasks, no panic.
    #[test]
    fn take_actual_tasks_empty() {
        let mut t = transitions_with_schedule(42, Schedule::new());
        assert!(t.take_actual_tasks().is_empty());
    }

    /// `block_height = 0` should still drain height-0 tasks.
    #[test]
    fn take_actual_tasks_at_genesis() {
        let mut schedule = Schedule::new();
        schedule.entry(0).or_default().insert(wake(1, 1));
        schedule.entry(1).or_default().insert(wake(2, 2));
        let mut t = transitions_with_schedule(0, schedule);

        assert_eq!(t.take_actual_tasks(), vec![wake(1, 1)]);
        assert!(t.schedule.contains_key(&1));
    }

    #[test]
    fn ethereum_event_destination_matches_eth_event_addr() {
        use gprimitives::H160;

        let eth_event_addr = H160::repeat_byte(0xff);
        assert_eq!(ETH_SAILS_EVENT, ActorId::from(eth_event_addr));
        assert_ne!(ETH_SAILS_EVENT, ActorId::new([u8::MAX; 32]));
        assert!(is_event_destination(ETH_SAILS_EVENT));
    }
}