revm_interpreter/interpreter_action/
call_inputs.rs

1use context_interface::{ContextTr, LocalContextTr};
2use core::ops::Range;
3use primitives::{Address, Bytes, B256, U256};
4use state::Bytecode;
5/// Input enum for a call.
6///
7/// As CallInput uses shared memory buffer it can get overridden if not used directly when call happens.
8#[derive(Clone, Debug, PartialEq, Eq)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10pub enum CallInput {
11    /// The Range points to the SharedMemory buffer. Buffer can be found in [`context_interface::LocalContextTr::shared_memory_buffer_slice`] function.
12    /// And can be accessed with `evm.ctx().local().shared_memory_buffer()`
13    ///
14    /// # Warning
15    ///
16    /// Use it with caution, CallInput shared buffer can be overridden if context from child call is returned so
17    /// recommendation is to fetch buffer at first Inspector call and clone it from [`context_interface::LocalContextTr::shared_memory_buffer_slice`] function.
18    SharedBuffer(Range<usize>),
19    /// Bytes of the call data.
20    Bytes(Bytes),
21}
22
23impl CallInput {
24    /// Returns the length of the call input.
25    pub fn len(&self) -> usize {
26        match self {
27            Self::Bytes(bytes) => bytes.len(),
28            Self::SharedBuffer(range) => range.len(),
29        }
30    }
31
32    /// Returns `true` if the call input is empty.
33    pub fn is_empty(&self) -> bool {
34        self.len() == 0
35    }
36
37    /// Returns the bytes of the call input.
38    ///
39    /// SharedMemory buffer can be shrunked or overwritten if the child call returns the
40    /// shared memory context to its parent, the range in `CallInput::SharedBuffer` can show unexpected data.
41    ///
42    /// # Allocation
43    ///
44    /// If this `CallInput` is a `SharedBuffer`, the slice will be copied
45    /// into a fresh `Bytes` buffer, which can pose a performance penalty.
46    pub fn bytes<CTX>(&self, ctx: &mut CTX) -> Bytes
47    where
48        CTX: ContextTr,
49    {
50        match self {
51            CallInput::Bytes(bytes) => bytes.clone(),
52            CallInput::SharedBuffer(range) => ctx
53                .local()
54                .shared_memory_buffer_slice(range.clone())
55                .map(|b| Bytes::from(b.to_vec()))
56                .unwrap_or_default(),
57        }
58    }
59}
60
61impl Default for CallInput {
62    #[inline]
63    fn default() -> Self {
64        CallInput::SharedBuffer(0..0)
65    }
66}
67
68/// Inputs for a call.
69#[derive(Clone, Debug, PartialEq, Eq)]
70#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
71pub struct CallInputs {
72    /// The call data of the call.
73    pub input: CallInput,
74    /// The return memory offset where the output of the call is written.
75    pub return_memory_offset: Range<usize>,
76    /// The gas limit of the call.
77    pub gas_limit: u64,
78    /// The account address of bytecode that is going to be executed.
79    ///
80    /// Previously `context.code_address`.
81    pub bytecode_address: Address,
82    /// Bytecode that is going to be executed.
83    pub bytecode: Bytecode,
84    /// Bytecode hash,
85    pub bytecode_hash: B256,
86    /// Target address, this account storage is going to be modified.
87    ///
88    /// Previously `context.address`.
89    pub target_address: Address,
90    /// This caller is invoking the call.
91    ///
92    /// Previously `context.caller`.
93    pub caller: Address,
94    /// Call value.
95    ///
96    /// **Note**: This value may not necessarily be transferred from caller to callee, see [`CallValue`].
97    ///
98    /// Previously `transfer.value` or `context.apparent_value`.
99    pub value: CallValue,
100    /// The call scheme.
101    ///
102    /// Previously `context.scheme`.
103    pub scheme: CallScheme,
104    /// Whether the call is a static call, or is initiated inside a static call.
105    pub is_static: bool,
106}
107
108impl CallInputs {
109    /// Returns `true` if the call will transfer a non-zero value.
110    #[inline]
111    pub fn transfers_value(&self) -> bool {
112        self.value.transfer().is_some_and(|x| x > U256::ZERO)
113    }
114
115    /// Returns the transfer value.
116    ///
117    /// This is the value that is transferred from caller to callee, see [`CallValue`].
118    #[inline]
119    pub const fn transfer_value(&self) -> Option<U256> {
120        self.value.transfer()
121    }
122
123    /// Returns the **apparent** call value.
124    ///
125    /// This value is not actually transferred, see [`CallValue`].
126    #[inline]
127    pub const fn apparent_value(&self) -> Option<U256> {
128        self.value.apparent()
129    }
130
131    /// Returns the address of the transfer source account.
132    ///
133    /// This is only meaningful if `transfers_value` is `true`.
134    #[inline]
135    pub const fn transfer_from(&self) -> Address {
136        self.caller
137    }
138
139    /// Returns the address of the transfer target account.
140    ///
141    /// This is only meaningful if `transfers_value` is `true`.
142    #[inline]
143    pub const fn transfer_to(&self) -> Address {
144        self.target_address
145    }
146
147    /// Returns the call value, regardless of the transfer value type.
148    ///
149    /// **Note**: This value may not necessarily be transferred from caller to callee, see [`CallValue`].
150    #[inline]
151    pub const fn call_value(&self) -> U256 {
152        self.value.get()
153    }
154}
155
156/// Call scheme.
157#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
158#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
159pub enum CallScheme {
160    /// `CALL`.
161    Call,
162    /// `CALLCODE`
163    CallCode,
164    /// `DELEGATECALL`
165    DelegateCall,
166    /// `STATICCALL`
167    StaticCall,
168}
169
170impl CallScheme {
171    /// Returns true if it is `CALL`.
172    pub fn is_call(&self) -> bool {
173        matches!(self, Self::Call)
174    }
175
176    /// Returns true if it is `CALLCODE`.
177    pub fn is_call_code(&self) -> bool {
178        matches!(self, Self::CallCode)
179    }
180
181    /// Returns true if it is `DELEGATECALL`.
182    pub fn is_delegate_call(&self) -> bool {
183        matches!(self, Self::DelegateCall)
184    }
185
186    /// Returns true if it is `STATICCALL`.
187    pub fn is_static_call(&self) -> bool {
188        matches!(self, Self::StaticCall)
189    }
190}
191
192/// Call value.
193#[derive(Clone, Debug, PartialEq, Eq, Hash)]
194#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
195pub enum CallValue {
196    /// Concrete value, transferred from caller to callee at the end of the transaction.
197    Transfer(U256),
198    /// Apparent value, that is **not** actually transferred.
199    ///
200    /// Set when in a `DELEGATECALL` call type, and used by the `CALLVALUE` opcode.
201    Apparent(U256),
202}
203
204impl Default for CallValue {
205    #[inline]
206    fn default() -> Self {
207        CallValue::Transfer(U256::ZERO)
208    }
209}
210
211impl CallValue {
212    /// Returns the call value, regardless of the type.
213    #[inline]
214    pub const fn get(&self) -> U256 {
215        match *self {
216            Self::Transfer(value) | Self::Apparent(value) => value,
217        }
218    }
219
220    /// Returns the transferred value, if any.
221    #[inline]
222    pub const fn transfer(&self) -> Option<U256> {
223        match *self {
224            Self::Transfer(transfer) => Some(transfer),
225            Self::Apparent(_) => None,
226        }
227    }
228
229    /// Returns whether the call value will be transferred.
230    #[inline]
231    pub const fn is_transfer(&self) -> bool {
232        matches!(self, Self::Transfer(_))
233    }
234
235    /// Returns the apparent value, if any.
236    #[inline]
237    pub const fn apparent(&self) -> Option<U256> {
238        match *self {
239            Self::Transfer(_) => None,
240            Self::Apparent(apparent) => Some(apparent),
241        }
242    }
243
244    /// Returns whether the call value is apparent, and not actually transferred.
245    #[inline]
246    pub const fn is_apparent(&self) -> bool {
247        matches!(self, Self::Apparent(_))
248    }
249}