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.

130 lines
3.6 KiB

2 years ago
  1. use evm::*;
  2. #[test]
  3. fn stack_simple_push_pop() {
  4. let mut s = Stack::new();
  5. s.push(str_to_u256("1"));
  6. s.push(str_to_u256("2"));
  7. s.push(str_to_u256("3"));
  8. assert_eq!(s.pop(), str_to_u256("3"));
  9. assert_eq!(s.pop(), str_to_u256("2"));
  10. assert_eq!(s.pop(), str_to_u256("1"));
  11. // assert_eq!(s.pop(), error); // TODO expect error as stack is empty
  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);
  20. assert_eq!(s.pop(), 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);
  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);
  41. // assert_eq!(out[0], 0x09);
  42. assert_eq!(s.gas, 9999999991);
  43. assert_eq!(s.pc, 7);
  44. assert_eq!(s.pop(), 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);
  53. assert_eq!(s.gas, 9999999985);
  54. assert_eq!(s.pc, 7);
  55. assert_eq!(s.pop(), 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);
  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);
  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);
  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);
  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("05").unwrap();
  100. //
  101. // let mut s = Stack::new();
  102. // s.execute(&code, &calldata, false);
  103. //
  104. // assert_eq!(s.gas, 9999999788);
  105. // assert_eq!(s.pc, 21);
  106. // assert_eq!(s.stack.len(), 0);
  107. // }