evm-rs/tests/execute.rs
tests/execute.rsRaw
1use evm::*;
2
3#[test]
4fn stack_simple_push_pop() {
5 let mut s = Stack::new();
6 s.push(u256::str_to_u256("1"));
7 s.push(u256::str_to_u256("2"));
8 s.push(u256::str_to_u256("3"));
9 assert_eq!(s.pop().unwrap(), u256::str_to_u256("3"));
10 assert_eq!(s.pop().unwrap(), u256::str_to_u256("2"));
11 assert_eq!(s.pop().unwrap(), u256::str_to_u256("1"));
12 assert_eq!(s.pop(), Err(format!("pop err"))); // WIP
13}
14
15// arithmetic
16#[test]
17fn execute_opcodes_0() {
18 let code = hex::decode("6005600c01").unwrap(); // 5+12
19 let calldata = vec![];
20
21 let mut s = Stack::new();
22 s.execute(&code, &calldata, false).unwrap();
23 assert_eq!(s.pop().unwrap(), u256::str_to_u256("17"));
24 assert_eq!(s.gas, 9999999991);
25 assert_eq!(s.pc, 5);
26}
27
28#[test]
29fn execute_opcodes_1() {
30 let code = hex::decode("60056004016000526001601ff3").unwrap();
31 let calldata = vec![];
32
33 let mut s = Stack::new();
34 let out = s.execute(&code, &calldata, false).unwrap();
35
36 assert_eq!(out[0], 0x09);
37 assert_eq!(s.gas, 9999999976);
38 assert_eq!(s.pc, 12);
39 // assert_eq!(s.pop(), err); // TODO expect error as stack is empty
40}
41
42#[test]
43fn execute_opcodes_2() {
44 let code = hex::decode("61010161010201").unwrap();
45 let calldata = vec![];
46
47 let mut s = Stack::new();
48 s.execute(&code, &calldata, false).unwrap();
49
50 // assert_eq!(out[0], 0x09);
51 assert_eq!(s.gas, 9999999991);
52 assert_eq!(s.pc, 7);
53 assert_eq!(s.pop().unwrap(), u256::str_to_u256("515"));
54}
55
56#[test]
57fn execute_opcodes_3() {
58 // contains calldata
59 let code = hex::decode("60003560203501").unwrap();
60 let calldata = hex::decode("00000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000004").unwrap();
61
62 let mut s = Stack::new();
63 s.execute(&code, &calldata, false).unwrap();
64
65 assert_eq!(s.gas, 9999999985);
66 assert_eq!(s.pc, 7);
67 assert_eq!(s.pop().unwrap(), u256::str_to_u256("9"));
68}
69
70// storage and execution
71#[test]
72fn execute_opcodes_4() {
73 // contains loops
74 let code = hex::decode("6000356000525b600160005103600052600051600657").unwrap();
75 let calldata =
76 hex::decode("0000000000000000000000000000000000000000000000000000000000000005").unwrap();
77
78 let mut s = Stack::new();
79 s.execute(&code, &calldata, false).unwrap();
80
81 assert_eq!(s.gas, 9999999795);
82 assert_eq!(s.pc, 22);
83 assert_eq!(s.stack.len(), 0);
84}
85#[test]
86fn execute_opcodes_5() {
87 // contains loops, without using mem
88 let code = hex::decode("6000355b6001900380600357").unwrap();
89 let calldata =
90 hex::decode("0000000000000000000000000000000000000000000000000000000000000001").unwrap();
91
92 let mut s = Stack::new();
93 s.execute(&code, &calldata, false).unwrap();
94
95 assert_eq!(s.gas, 9999999968);
96 assert_eq!(s.pc, 12);
97
98 let code = hex::decode("6000355b6001900380600357").unwrap();
99 let calldata =
100 hex::decode("0000000000000000000000000000000000000000000000000000000000000002").unwrap();
101
102 let mut s = Stack::new();
103 s.execute(&code, &calldata, false).unwrap();
104
105 assert_eq!(s.gas, 9999999942);
106 assert_eq!(s.pc, 12);
107
108 let code = hex::decode("6000355b6001900380600357").unwrap();
109 let calldata =
110 hex::decode("0000000000000000000000000000000000000000000000000000000000000005").unwrap();
111
112 let mut s = Stack::new();
113 s.execute(&code, &calldata, false).unwrap();
114
115 assert_eq!(s.gas, 9999999864);
116 assert_eq!(s.pc, 12);
117}
118#[test]
119fn execute_opcodes_6() {
120 // 0x36: calldata_size
121 let code = hex::decode("366020036101000a600035045b6001900380600c57").unwrap();
122 let calldata = hex::decode("01").unwrap();
123
124 let mut s = Stack::new();
125 s.execute(&code, &calldata, false).unwrap();
126
127 assert_eq!(s.gas, 9999999892);
128 assert_eq!(s.pc, 21);
129 assert_eq!(s.stack.len(), 1);
130
131 let code = hex::decode("366020036101000a600035045b6001900380600c57").unwrap();
132 let calldata = hex::decode("05").unwrap();
133
134 let mut s = Stack::new();
135 s.execute(&code, &calldata, false).unwrap();
136
137 assert_eq!(s.gas, 9999999788);
138 assert_eq!(s.pc, 21);
139 assert_eq!(s.stack.len(), 1);
140
141 let code = hex::decode("366020036101000a600035045b6001900380600c57").unwrap();
142 let calldata = hex::decode("0101").unwrap();
143
144 let mut s = Stack::new();
145 s.execute(&code, &calldata, false).unwrap();
146
147 assert_eq!(s.gas, 9999993236);
148 assert_eq!(s.pc, 21);
149 assert_eq!(s.stack.len(), 1);
150}
151
152#[test]
153fn execute_opcodes_7() {
154 // contract deployment (code_copy)
155 let code = hex::decode("600580600b6000396000f36005600401").unwrap();
156 let calldata = hex::decode("").unwrap();
157
158 let mut s = Stack::new();
159 let out = s.execute(&code, &calldata, true).unwrap();
160
161 assert_eq!(s.gas, 9999999976);
162 assert_eq!(s.pc, 10);
163 assert_eq!(s.stack.len(), 0);
164 assert_eq!(s.mem.len(), 32);
165 assert_eq!(
166 s.mem,
167 hex::decode("6005600401000000000000000000000000000000000000000000000000000000").unwrap()
168 );
169 assert_eq!(out, hex::decode("6005600401").unwrap());
170}
171
172#[test]
173fn execute_exceptions() {
174 let mut s = Stack::new();
175 let calldata = hex::decode("").unwrap();
176
177 let code = hex::decode("5f").unwrap();
178 let out = s.execute(&code, &calldata, false);
179 assert_eq!(out, Err(format!("invalid opcode 5f")));
180
181 let code = hex::decode("56").unwrap();
182 let out = s.execute(&code, &calldata, false);
183 assert_eq!(out, Err(format!("pop err")));
184
185 let code = hex::decode("600056").unwrap();
186 let out = s.execute(&code, &calldata, false);
187 assert_eq!(out, Err(format!("not valid dest: 00")));
188
189 s.gas = 1;
190 let code = hex::decode("6000").unwrap();
191 let out = s.execute(&code, &calldata, false);
192 assert_eq!(out, Err(format!("out of gas")));
193}
194
195#[test]
196fn execute_opcodes_8() {
197 let code = hex::decode("611000805151").unwrap();
198 let calldata = hex::decode("").unwrap();
199
200 let mut s = Stack::new();
201 s.execute(&code, &calldata, false).unwrap();
202
203 assert_eq!(s.gas, 9999999569);
204 assert_eq!(s.pc, 6);
205 assert_eq!(s.stack.len(), 2);
206 assert_eq!(s.mem.len(), 4128);
207}
208
209#[test]
210fn execute_opcodes_9() {
211 // sstore (0x55)
212 let code = hex::decode("60026000556001600055").unwrap();
213 let calldata = hex::decode("").unwrap();
214
215 let mut s = Stack::new();
216 s.execute(&code, &calldata, false).unwrap();
217
218 // assert_eq!(s.gas, 9999977788); // TODO WIP geth reported gas
219 assert_eq!(s.gas, 9999974988);
220 assert_eq!(s.pc, 10);
221 assert_eq!(s.stack.len(), 0);
222 assert_eq!(s.storage.len(), 1);
223}
224
225#[test]
226fn execute_opcodes_10() {
227 let code = hex::decode(
228 "606060405260e060020a6000350463a5f3c23b8114601a575b005b60243560043501600055601856",
229 )
230 .unwrap();
231 let calldata = hex::decode("a5f3c23b00000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000004").unwrap();
232
233 println!("LEN {:?}", calldata.len());
234
235 let mut s = Stack::new();
236 s.execute(&code, &calldata, true).unwrap();
237
238 // assert_eq!(s.gas, 9999977752); // WIP correct sstore gas computation
239 assert_eq!(s.gas, 9999979852);
240 assert_eq!(s.pc, 25);
241 assert_eq!(s.stack.len(), 1);
242 assert_eq!(s.storage.len(), 1);
243}