revm-interpreter 35.0.1

Revm Interpreter that executes bytecode.
Documentation
use context_interface::CreateScheme;
use core::cell::OnceCell;
use primitives::{Address, Bytes, U256};

/// Inputs for a create call
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CreateInputs {
    /// Caller address of the EVM
    caller: Address,
    /// The create scheme
    scheme: CreateScheme,
    /// The value to transfer
    value: U256,
    /// The init code of the contract
    init_code: Bytes,
    /// The gas limit of the call
    gas_limit: u64,
    /// State gas reservoir (EIP-8037). Passed from parent frame to child frame.
    reservoir: u64,
    /// Cached created address. This is computed lazily and cached to avoid
    /// redundant keccak computations when inspectors call `created_address`.
    #[cfg_attr(feature = "serde", serde(skip))]
    cached_address: OnceCell<Address>,
}

impl CreateInputs {
    /// Creates a new `CreateInputs` instance.
    pub fn new(
        caller: Address,
        scheme: CreateScheme,
        value: U256,
        init_code: Bytes,
        gas_limit: u64,
        reservoir: u64,
    ) -> Self {
        Self {
            caller,
            scheme,
            value,
            init_code,
            gas_limit,
            reservoir,
            cached_address: OnceCell::new(),
        }
    }

    /// Returns the address that this create call will create.
    ///
    /// The result is cached to avoid redundant keccak computations.
    pub fn created_address(&self, nonce: u64) -> Address {
        *self.cached_address.get_or_init(|| match self.scheme {
            CreateScheme::Create => self.caller.create(nonce),
            CreateScheme::Create2 { salt } => self
                .caller
                .create2_from_code(salt.to_be_bytes(), &self.init_code),
            CreateScheme::Custom { address } => address,
        })
    }

    /// Returns the caller address of the EVM.
    pub fn caller(&self) -> Address {
        self.caller
    }

    /// Returns the create scheme of the EVM.
    pub fn scheme(&self) -> CreateScheme {
        self.scheme
    }

    /// Returns the value to transfer.
    pub fn value(&self) -> U256 {
        self.value
    }

    /// Returns the init code of the contract.
    pub fn init_code(&self) -> &Bytes {
        &self.init_code
    }

    /// Returns the gas limit of the call.
    pub fn gas_limit(&self) -> u64 {
        self.gas_limit
    }

    /// Set call
    pub fn set_call(&mut self, caller: Address) {
        self.caller = caller;
        self.cached_address = OnceCell::new();
    }

    /// Set scheme
    pub fn set_scheme(&mut self, scheme: CreateScheme) {
        self.scheme = scheme;
        self.cached_address = OnceCell::new();
    }

    /// Set value
    pub fn set_value(&mut self, value: U256) {
        self.value = value;
    }

    /// Set init code
    pub fn set_init_code(&mut self, init_code: Bytes) {
        self.init_code = init_code;
        self.cached_address = OnceCell::new();
    }

    /// Set gas limit
    pub fn set_gas_limit(&mut self, gas_limit: u64) {
        self.gas_limit = gas_limit;
    }

    /// Returns the state gas reservoir (EIP-8037).
    pub fn reservoir(&self) -> u64 {
        self.reservoir
    }

    /// Set the state gas reservoir (EIP-8037).
    pub fn set_reservoir(&mut self, reservoir: u64) {
        self.reservoir = reservoir;
    }
}