revm_context_interface/
local.rs

1//! Local context trait [`LocalContextTr`] and related types.
2use core::{
3    cell::{Ref, RefCell},
4    ops::Range,
5};
6use std::{boxed::Box, rc::Rc, vec::Vec};
7
8/// Non-empty, item-pooling Vec.
9#[derive(Debug, Clone)]
10pub struct FrameStack<T> {
11    stack: Vec<Box<T>>,
12    index: Option<usize>,
13}
14
15impl<T> Default for FrameStack<T> {
16    fn default() -> Self {
17        Self::new()
18    }
19}
20
21impl<T> FrameStack<T> {
22    /// Creates a new, empty stack. It must be initialized with init before use.
23    #[inline]
24    pub fn new() -> Self {
25        Self {
26            stack: Vec::with_capacity(1025),
27            index: None,
28        }
29    }
30
31    /// Initializes the stack with a single item.
32    #[inline]
33    pub fn start_init(&mut self) -> OutFrame<'_, T> {
34        self.index = Some(0);
35        if self.stack.is_empty() {
36            self.stack.reserve(1);
37        }
38        self.out_frame_at(0)
39    }
40
41    /// Finishes initialization.
42    #[inline]
43    pub fn end_init(&mut self, token: FrameToken) {
44        token.assert();
45        if self.stack.is_empty() {
46            unsafe { self.stack.set_len(1) };
47        }
48    }
49
50    /// Returns the current index of the stack.
51    #[inline]
52    pub fn index(&self) -> Option<usize> {
53        self.index
54    }
55
56    /// Increments the index.
57    #[inline]
58    pub fn push(&mut self, token: FrameToken) {
59        token.assert();
60        let index = self.index.as_mut().unwrap();
61        if *index + 1 == self.stack.len() {
62            unsafe { self.stack.set_len(self.stack.len() + 1) };
63            self.stack.reserve(1);
64        }
65        *index += 1;
66    }
67
68    /// Clears the stack by setting the index to 0.
69    /// It does not destroy the stack.
70    #[inline]
71    pub fn clear(&mut self) {
72        self.index = None;
73    }
74
75    /// Decrements the index.
76    #[inline]
77    pub fn pop(&mut self) {
78        self.index = self.index.unwrap_or(0).checked_sub(1);
79    }
80
81    /// Returns the current item.
82    #[inline]
83    pub fn get(&mut self) -> &mut T {
84        debug_assert!(self.stack.capacity() > self.index.unwrap() + 1);
85        unsafe { &mut *self.stack.as_mut_ptr().add(self.index.unwrap()) }
86    }
87
88    /// Get next uninitialized item.
89    #[inline]
90    pub fn get_next(&mut self) -> OutFrame<'_, T> {
91        self.out_frame_at(self.index.unwrap() + 1)
92    }
93
94    fn out_frame_at(&mut self, idx: usize) -> OutFrame<'_, T> {
95        unsafe {
96            OutFrame::new_maybe_uninit(self.stack.as_mut_ptr().add(idx), idx < self.stack.len())
97        }
98    }
99}
100
101/// A potentially initialized frame. Used when initializing a new frame in the main loop.
102#[allow(missing_debug_implementations)]
103pub struct OutFrame<'a, T> {
104    ptr: *mut Box<T>,
105    init: bool,
106    lt: core::marker::PhantomData<&'a mut T>,
107}
108
109impl<'a, T> OutFrame<'a, T> {
110    /// Creates a new initialized `OutFrame` from a mutable reference to a type `T`.
111    pub fn new_init(slot: &'a mut Box<T>) -> Self {
112        unsafe { Self::new_maybe_uninit(slot, true) }
113    }
114
115    /// Creates a new uninitialized `OutFrame` from a mutable reference to a `MaybeUninit<T>`.
116    pub fn new_uninit(slot: &'a mut core::mem::MaybeUninit<Box<T>>) -> Self {
117        unsafe { Self::new_maybe_uninit(slot.as_mut_ptr(), false) }
118    }
119
120    /// Creates a new `OutFrame` from a raw pointer to a type `T`.
121    ///
122    /// # Safety
123    ///
124    /// This method is unsafe because it assumes that the pointer is valid and points to a location
125    /// where a type `T` can be stored. It also assumes that the `init` flag correctly reflects whether
126    /// the type `T` has been initialized or not.
127    pub unsafe fn new_maybe_uninit(ptr: *mut Box<T>, init: bool) -> Self {
128        Self {
129            ptr,
130            init,
131            lt: Default::default(),
132        }
133    }
134
135    /// Returns a mutable reference to the type `T`, initializing it if it hasn't been initialized yet.
136    pub fn get(&mut self, f: impl FnOnce() -> T) -> &mut T {
137        if !self.init {
138            self.do_init(f);
139        }
140        unsafe { &mut *self.ptr }
141    }
142
143    #[cold]
144    fn do_init(&mut self, f: impl FnOnce() -> T) {
145        unsafe {
146            self.init = true;
147            self.ptr.write(Box::new(f()));
148        }
149    }
150
151    /// Returns a mutable reference to the type `T`, without checking if it has been initialized.
152    ///
153    /// # Safety
154    ///
155    /// This method is unsafe because it assumes that the `OutFrame` has been initialized before use.
156    pub unsafe fn get_unchecked(&mut self) -> &mut T {
157        debug_assert!(self.init, "OutFrame must be initialized before use");
158        unsafe { &mut *self.ptr }
159    }
160
161    /// Consumes the `OutFrame`, returning a `FrameToken` that indicates the frame has been initialized.
162    pub fn consume(self) -> FrameToken {
163        FrameToken(self.init)
164    }
165}
166
167/// Used to guarantee that a frame is initialized before use.
168#[allow(missing_debug_implementations)]
169pub struct FrameToken(bool);
170
171impl FrameToken {
172    /// Asserts that the frame token is initialized.
173    #[cfg_attr(debug_assertions, track_caller)]
174    pub fn assert(self) {
175        assert!(self.0, "FrameToken must be initialized before use");
176    }
177}
178
179/// Local context used for caching initcode from Initcode transactions.
180pub trait LocalContextTr {
181    /// Interpreter shared memory buffer. A reused memory buffer for calls.
182    fn shared_memory_buffer(&self) -> &Rc<RefCell<Vec<u8>>>;
183
184    /// Slice of the shared memory buffer returns None if range is not valid or buffer can't be borrowed.
185    fn shared_memory_buffer_slice(&self, range: Range<usize>) -> Option<Ref<'_, [u8]>> {
186        let buffer = self.shared_memory_buffer();
187        buffer.borrow().get(range.clone())?;
188        Some(Ref::map(buffer.borrow(), |b| {
189            b.get(range).unwrap_or_default()
190        }))
191    }
192
193    /// Clear the local context.
194    fn clear(&mut self);
195}
196
197#[cfg(test)]
198mod tests {
199    use super::*;
200
201    #[test]
202    fn frame_stack() {
203        let mut stack = FrameStack::new();
204        let mut frame = stack.start_init();
205        frame.get(|| 1);
206        let token = frame.consume();
207        stack.end_init(token);
208
209        assert_eq!(stack.index(), Some(0));
210        assert_eq!(stack.stack.len(), 1);
211
212        let a = stack.get();
213        assert_eq!(a, &mut 1);
214        let mut b = stack.get_next();
215        assert!(!b.init);
216        assert_eq!(b.get(|| 2), &mut 2);
217        let token = b.consume(); // TODO: remove
218        stack.push(token);
219
220        assert_eq!(stack.index(), Some(1));
221        assert_eq!(stack.stack.len(), 2);
222        let a = stack.get();
223        assert_eq!(a, &mut 2);
224        let b = stack.get_next();
225        assert!(!b.init);
226
227        stack.pop();
228
229        assert_eq!(stack.index(), Some(0));
230        assert_eq!(stack.stack.len(), 2);
231        let a = stack.get();
232        assert_eq!(a, &mut 1);
233        let mut b = stack.get_next();
234        assert!(b.init);
235        assert_eq!(unsafe { b.get_unchecked() }, &mut 2);
236    }
237}