You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
7.1 KiB

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