revm_interpreter/interpreter_action/
eof_create_inputs.rs

1use crate::primitives::{Address, Bytes, Eof, TxEnv, U256};
2
3/// EOF create can be called from two places:
4/// * EOFCREATE opcode
5/// * Creation transaction.
6///
7/// Creation transaction uses initdata and packs EOF and initdata inside it.
8/// This eof bytecode needs to be validated.
9///
10/// Opcode creation uses already validated EOF bytecode, and input from Interpreter memory.
11/// Address is already known and is passed as an argument.
12#[derive(Debug, Clone, PartialEq, Eq)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
14pub enum EOFCreateKind {
15    Tx {
16        initdata: Bytes,
17    },
18    Opcode {
19        initcode: Eof,
20        input: Bytes,
21        created_address: Address,
22    },
23}
24
25impl EOFCreateKind {
26    /// Returns created address
27    pub fn created_address(&self) -> Option<&Address> {
28        match self {
29            EOFCreateKind::Opcode {
30                created_address, ..
31            } => Some(created_address),
32            EOFCreateKind::Tx { .. } => None,
33        }
34    }
35}
36
37impl Default for EOFCreateKind {
38    fn default() -> Self {
39        EOFCreateKind::Opcode {
40            initcode: Eof::default(),
41            input: Bytes::default(),
42            created_address: Address::default(),
43        }
44    }
45}
46
47/// Inputs for EOF create call.
48#[derive(Debug, Default, Clone, PartialEq, Eq)]
49#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
50pub struct EOFCreateInputs {
51    /// Caller of Eof Craate
52    pub caller: Address,
53    /// Values of ether transferred
54    pub value: U256,
55    /// Gas limit for the create call.
56    pub gas_limit: u64,
57    /// EOF Create kind
58    pub kind: EOFCreateKind,
59}
60
61impl EOFCreateInputs {
62    /// Create new EOF crate input from transaction that has concatenated eof init code and calldata.
63    ///
64    /// Legacy transaction still have optional nonce so we need to obtain it.
65    pub fn new(caller: Address, value: U256, gas_limit: u64, kind: EOFCreateKind) -> Self {
66        //let (eof_init_code, input) = Eof::decode_dangling(tx.data.clone())?;
67        EOFCreateInputs {
68            caller,
69            value,
70            gas_limit,
71            kind,
72        }
73    }
74
75    /// Creates new EOFCreateInputs from transaction.
76    pub fn new_tx(tx: &TxEnv, gas_limit: u64) -> Self {
77        EOFCreateInputs::new(
78            tx.caller,
79            tx.value,
80            gas_limit,
81            EOFCreateKind::Tx {
82                initdata: tx.data.clone(),
83            },
84        )
85    }
86
87    /// Returns a new instance of EOFCreateInput.
88    pub fn new_opcode(
89        caller: Address,
90        created_address: Address,
91        value: U256,
92        eof_init_code: Eof,
93        gas_limit: u64,
94        input: Bytes,
95    ) -> EOFCreateInputs {
96        EOFCreateInputs::new(
97            caller,
98            value,
99            gas_limit,
100            EOFCreateKind::Opcode {
101                initcode: eof_init_code,
102                input,
103                created_address,
104            },
105        )
106    }
107}