arnaucube
arnaucube/evm-rsPublic Self-hosted repository
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(), u256::str_to_u256("3"));
 10    assert_eq!(s.pop(), u256::str_to_u256("2"));
 11    assert_eq!(s.pop(), u256::str_to_u256("1"));
 12    // assert_eq!(s.pop(), error); // TODO expect error as stack is empty
 13}
 14
 15#[test]
 16fn available_opcodes() {
 17    let s = Stack::new();
 18    println!("available opcodes {}/130", s.opcodes.len());
 19}
 20
 21// arithmetic
 22#[test]
 23fn execute_opcodes_0() {
 24    let code = hex::decode("6005600c01").unwrap(); // 5+12
 25    let calldata = vec![];
 26
 27    let mut s = Stack::new();
 28    s.execute(&code, &calldata, false);
 29    assert_eq!(s.pop(), u256::str_to_u256("17"));
 30    assert_eq!(s.gas, 9999999991);
 31    assert_eq!(s.pc, 5);
 32}
 33
 34#[test]
 35fn execute_opcodes_1() {
 36    let code = hex::decode("60056004016000526001601ff3").unwrap();
 37    let calldata = vec![];
 38
 39    let mut s = Stack::new();
 40    let out = s.execute(&code, &calldata, false);
 41
 42    assert_eq!(out[0], 0x09);
 43    assert_eq!(s.gas, 9999999976);
 44    assert_eq!(s.pc, 12);
 45    // assert_eq!(s.pop(), err); // TODO expect error as stack is empty
 46}
 47
 48#[test]
 49fn execute_opcodes_2() {
 50    let code = hex::decode("61010161010201").unwrap();
 51    let calldata = vec![];
 52
 53    let mut s = Stack::new();
 54    s.execute(&code, &calldata, false);
 55
 56    // assert_eq!(out[0], 0x09);
 57    assert_eq!(s.gas, 9999999991);
 58    assert_eq!(s.pc, 7);
 59    assert_eq!(s.pop(), u256::str_to_u256("515"));
 60}
 61
 62#[test]
 63fn execute_opcodes_3() {
 64    // contains calldata
 65    let code = hex::decode("60003560203501").unwrap();
 66    let calldata = hex::decode("00000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000004").unwrap();
 67
 68    let mut s = Stack::new();
 69    s.execute(&code, &calldata, false);
 70
 71    assert_eq!(s.gas, 9999999985);
 72    assert_eq!(s.pc, 7);
 73    assert_eq!(s.pop(), u256::str_to_u256("9"));
 74}
 75
 76// storage and execution
 77#[test]
 78fn execute_opcodes_4() {
 79    // contains loops
 80    let code = hex::decode("6000356000525b600160005103600052600051600657").unwrap();
 81    let calldata =
 82        hex::decode("0000000000000000000000000000000000000000000000000000000000000005").unwrap();
 83
 84    let mut s = Stack::new();
 85    s.execute(&code, &calldata, false);
 86
 87    assert_eq!(s.gas, 9999999795);
 88    assert_eq!(s.pc, 22);
 89    assert_eq!(s.stack.len(), 0);
 90}
 91#[test]
 92fn execute_opcodes_5() {
 93    // contains loops, without using mem
 94    let code = hex::decode("6000355b6001900380600357").unwrap();
 95    let calldata =
 96        hex::decode("0000000000000000000000000000000000000000000000000000000000000001").unwrap();
 97
 98    let mut s = Stack::new();
 99    s.execute(&code, &calldata, false);
100
101    assert_eq!(s.gas, 9999999968);
102    assert_eq!(s.pc, 12);
103
104    let code = hex::decode("6000355b6001900380600357").unwrap();
105    let calldata =
106        hex::decode("0000000000000000000000000000000000000000000000000000000000000002").unwrap();
107
108    let mut s = Stack::new();
109    s.execute(&code, &calldata, false);
110
111    assert_eq!(s.gas, 9999999942);
112    assert_eq!(s.pc, 12);
113
114    let code = hex::decode("6000355b6001900380600357").unwrap();
115    let calldata =
116        hex::decode("0000000000000000000000000000000000000000000000000000000000000005").unwrap();
117
118    let mut s = Stack::new();
119    s.execute(&code, &calldata, false);
120
121    assert_eq!(s.gas, 9999999864);
122    assert_eq!(s.pc, 12);
123}
124#[test]
125fn execute_opcodes_6() {
126    // 0x36: calldata_size
127    let code = hex::decode("366020036101000a600035045b6001900380600c57").unwrap();
128    let calldata = hex::decode("01").unwrap();
129
130    let mut s = Stack::new();
131    s.execute(&code, &calldata, false);
132
133    assert_eq!(s.gas, 9999999892);
134    assert_eq!(s.pc, 21);
135    assert_eq!(s.stack.len(), 1);
136
137    let code = hex::decode("366020036101000a600035045b6001900380600c57").unwrap();
138    let calldata = hex::decode("05").unwrap();
139
140    let mut s = Stack::new();
141    s.execute(&code, &calldata, false);
142
143    assert_eq!(s.gas, 9999999788);
144    assert_eq!(s.pc, 21);
145    assert_eq!(s.stack.len(), 1);
146
147    let code = hex::decode("366020036101000a600035045b6001900380600c57").unwrap();
148    let calldata = hex::decode("0101").unwrap();
149
150    let mut s = Stack::new();
151    s.execute(&code, &calldata, false);
152
153    assert_eq!(s.gas, 9999993236);
154    assert_eq!(s.pc, 21);
155    assert_eq!(s.stack.len(), 1);
156}
157
158#[test]
159fn execute_opcodes_7() {
160    // contract deployment (code_copy)
161    let code = hex::decode("600580600b6000396000f36005600401").unwrap();
162    let calldata = hex::decode("").unwrap();
163
164    let mut s = Stack::new();
165    let out = s.execute(&code, &calldata, true);
166
167    assert_eq!(s.gas, 9999999976);
168    assert_eq!(s.pc, 10);
169    assert_eq!(s.stack.len(), 0);
170    assert_eq!(s.mem.len(), 32);
171    assert_eq!(
172        s.mem,
173        hex::decode("6005600401000000000000000000000000000000000000000000000000000000").unwrap()
174    );
175    assert_eq!(out, hex::decode("6005600401").unwrap());
176}
177
178#[test]
179fn execute_opcodes_8() {
180    let code = hex::decode("611000805151").unwrap();
181    let calldata = hex::decode("").unwrap();
182
183    let mut s = Stack::new();
184    s.execute(&code, &calldata, false);
185
186    assert_eq!(s.gas, 9999999569);
187    assert_eq!(s.pc, 6);
188    assert_eq!(s.stack.len(), 2);
189    assert_eq!(s.mem.len(), 4128);
190}
191
192#[test]
193fn execute_opcodes_9() {
194    // sstore (0x55)
195    let code = hex::decode("60026000556001600055").unwrap();
196    let calldata = hex::decode("").unwrap();
197
198    let mut s = Stack::new();
199    s.execute(&code, &calldata, true);
200
201    assert_eq!(s.gas, 9999974988);
202    // assert_eq!(s.gas, 9999977788); // TODO WIP geth reported gas
203    assert_eq!(s.pc, 10);
204    assert_eq!(s.stack.len(), 0);
205    assert_eq!(s.storage.len(), 1);
206}