revm_context/
local.rs

1//! Local context that is filled by execution.
2use context_interface::LocalContextTr;
3use core::cell::RefCell;
4use std::{rc::Rc, string::String, vec::Vec};
5
6/// Local context that is filled by execution.
7#[derive(Clone, Debug)]
8pub struct LocalContext {
9    /// Interpreter shared memory buffer. A reused memory buffer for calls.
10    pub shared_memory_buffer: Rc<RefCell<Vec<u8>>>,
11    /// Optional precompile error message to bubble up.
12    pub precompile_error_message: Option<String>,
13}
14
15impl Default for LocalContext {
16    fn default() -> Self {
17        Self {
18            shared_memory_buffer: Rc::new(RefCell::new(Vec::with_capacity(1024 * 4))),
19            precompile_error_message: None,
20        }
21    }
22}
23
24impl LocalContextTr for LocalContext {
25    fn clear(&mut self) {
26        // Sets len to 0 but it will not shrink to drop the capacity.
27        unsafe { self.shared_memory_buffer.borrow_mut().set_len(0) };
28        self.precompile_error_message = None;
29    }
30
31    fn shared_memory_buffer(&self) -> &Rc<RefCell<Vec<u8>>> {
32        &self.shared_memory_buffer
33    }
34
35    fn set_precompile_error_context(&mut self, output: String) {
36        self.precompile_error_message = Some(output);
37    }
38
39    fn take_precompile_error_context(&mut self) -> Option<String> {
40        self.precompile_error_message.take()
41    }
42}
43
44impl LocalContext {
45    /// Creates a new local context, initcodes are hashes and added to the mapping.
46    pub fn new() -> Self {
47        Self::default()
48    }
49}