multiplexer_evm/
opcodes.rs

1use alloy_primitives::{Address, U256};
2
3// Operation opcodes as constants
4pub const OP_EOF: u8 = 0x00;
5pub const OP_CLEARDATA: u8 = 0x01;
6pub const OP_SETDATA: u8 = 0x02;
7pub const OP_SETADDR: u8 = 0x03;
8pub const OP_SETVALUE: u8 = 0x04;
9pub const OP_EXTCODECOPY: u8 = 0x05;
10pub const OP_CALL: u8 = 0x06;
11pub const OP_CREATE: u8 = 0x07;
12pub const OP_DELEGATECALL: u8 = 0x08;
13pub const OP_SETCALLBACK: u8 = 0x09;
14pub const OP_SETFAIL: u8 = 0x0a;
15pub const OP_CLEARFAIL: u8 = 0x0b;
16
17
18// Struct for the CLEARDATA operation
19pub struct ClearData {
20    pub size: u16,
21}
22
23impl ClearData {
24    pub fn new(size: u16) -> Self {
25        ClearData { size }
26    }
27
28    pub fn encode(&self) -> Vec<u8> {
29        let mut encoded = Vec::new();
30        encoded.push(OP_CLEARDATA); // Opcode
31        encoded.extend(&self.size.to_be_bytes()); // Size
32        encoded
33    }
34}
35
36// Struct for the SETDATA operation
37pub struct SetData {
38    pub offset: u16,
39    pub data: Vec<u8>,
40}
41
42impl SetData {
43    pub fn new(offset: u16, data: Vec<u8>) -> Self {
44        SetData { offset, data }
45    }
46
47    pub fn encode(&self) -> Vec<u8> {
48        let data_size = self.data.len() as u16;
49        let mut encoded = Vec::new();
50        encoded.push(OP_SETDATA); // Opcode
51        encoded.extend(&self.offset.to_be_bytes()); // Offset
52        encoded.extend(&data_size.to_be_bytes()); // Data Size
53        encoded.extend(&self.data); // Data
54
55        encoded
56    }
57}
58
59// Struct for the SETADDR operation
60pub struct SetAddr {
61    pub addr: Address, // 20-byte address
62}
63
64impl SetAddr {
65    pub fn new(addr: Address) -> Self {
66        SetAddr { addr }
67    }
68
69    pub fn encode(&self) -> Vec<u8> {
70        let mut encoded = Vec::new();
71        encoded.push(OP_SETADDR); // Opcode
72        encoded.extend(&self.addr); // Address
73        encoded
74    }
75}
76
77// Struct for the SETVALUE operation
78#[derive(Clone, Debug)]
79pub struct SetValue {
80    pub value: U256,
81}
82
83impl SetValue {
84    pub fn new(value: U256) -> Self {
85        SetValue { value }
86    }
87
88    pub fn encode(&self) -> Vec<u8> {
89        let mut encoded = Vec::new();
90        encoded.push(OP_SETVALUE); // Opcode
91        encoded.extend(&self.value.to_be_bytes::<32>()); // Value
92        encoded
93    }
94}
95
96// Struct for the EXTCODECOPY operation
97pub struct ExtCodeCopy {
98    pub source: Address,  // Address of contract to copy code from
99    pub data_offset: u16, // Offset in the data to copy the code to
100    pub code_offset: u16, // Offset in the code to copy from
101    pub size: u16,        // Size of the code to copy
102}
103
104impl ExtCodeCopy {
105    pub fn new(source: Address, data_offset: u16, code_offset: u16, size: u16) -> Self {
106        ExtCodeCopy {
107            source,
108            data_offset,
109            code_offset,
110            size,
111        }
112    }
113
114    pub fn encode(&self) -> Vec<u8> {
115        let mut encoded = Vec::new();
116        encoded.push(OP_EXTCODECOPY); // Opcode
117        encoded.extend(&self.source); // Source address
118        encoded.extend(&self.data_offset.to_be_bytes()); // Offset
119        encoded.extend(&self.code_offset.to_be_bytes()); // Offset
120        encoded.extend(&self.size.to_be_bytes()); // Size
121        encoded
122    }
123}
124
125// Struct for the CALL operation
126#[derive(Default)]
127pub struct Call {}
128
129impl Call {
130    pub fn new() -> Self {
131        Call {}
132    }
133
134    pub fn encode(&self) -> Vec<u8> {
135        vec![OP_CALL] // Opcode
136    }
137}
138
139// Struct for the CREATE operation
140#[derive(Default)]
141pub struct Create {
142    pub created_address: Address,
143}
144
145impl Create {
146    pub fn new(created_address: Address) -> Self {
147        Self { created_address }
148    }
149
150    pub fn encode(&self) -> Vec<u8> {
151        vec![OP_CREATE] // Opcode
152    }
153}
154
155// Struct for the DELEGATECALL operation
156#[derive(Default)]
157pub struct DelegateCall {}
158
159impl DelegateCall {
160    pub fn new() -> Self {
161        DelegateCall {}
162    }
163
164    pub fn encode(&self) -> Vec<u8> {
165        vec![OP_DELEGATECALL] // Opcode
166    }
167}
168
169// Struct for the SETCALLBACK operation
170#[derive(Default)]
171pub struct SetCallback {
172    pub callback_address: Address
173}
174
175impl SetCallback {
176    pub fn new(callback_address: Address) -> Self {
177        SetCallback {callback_address}
178    }
179
180    pub fn encode(&self) -> Vec<u8> {
181        let mut encoded = vec![OP_SETCALLBACK]; // Opcode
182        encoded.extend(&self.callback_address); // Offset
183        encoded
184    }
185}
186
187// Struct for the SETFAIL operation
188#[derive(Default)]
189pub struct SetFail {}
190
191impl SetFail {
192    pub fn new() -> Self {
193        SetFail {}
194    }
195
196    pub fn encode(&self) -> Vec<u8> {
197        vec![OP_SETFAIL] // Opcode
198    }
199}
200
201// Struct for the CLEARFAIL operation
202#[derive(Default)]
203pub struct ClearFail {}
204
205impl ClearFail {
206    pub fn new() -> Self {
207        ClearFail {}
208    }
209
210    pub fn encode(&self) -> Vec<u8> {
211        vec![OP_CLEARFAIL] // Opcode
212    }
213}
214