state_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package tests
  17. import (
  18. "bytes"
  19. "flag"
  20. "fmt"
  21. "reflect"
  22. "testing"
  23. "github.com/ethereum/go-ethereum/cmd/utils"
  24. "github.com/ethereum/go-ethereum/core/vm"
  25. )
  26. func TestState(t *testing.T) {
  27. t.Parallel()
  28. st := new(testMatcher)
  29. // Long tests:
  30. st.slow(`^stAttackTest/ContractCreationSpam`)
  31. st.slow(`^stBadOpcode/badOpcodes`)
  32. st.slow(`^stPreCompiledContracts/modexp`)
  33. st.slow(`^stQuadraticComplexityTest/`)
  34. st.slow(`^stStaticCall/static_Call50000`)
  35. st.slow(`^stStaticCall/static_Return50000`)
  36. st.slow(`^stStaticCall/static_Call1MB`)
  37. st.slow(`^stSystemOperationsTest/CallRecursiveBomb`)
  38. st.slow(`^stTransactionTest/Opcodes_TransactionInit`)
  39. // Broken tests:
  40. st.skipLoad(`^stTransactionTest/OverflowGasRequire\.json`) // gasLimit > 256 bits
  41. st.skipLoad(`^stTransactionTest/zeroSigTransa[^/]*\.json`) // EIP-86 is not supported yet
  42. // Expected failures:
  43. st.fails(`^stRevertTest/RevertPrecompiledTouch\.json/EIP158`, "bug in test")
  44. st.fails(`^stRevertTest/RevertPrecompiledTouch\.json/Byzantium`, "bug in test")
  45. st.fails(`^stRevertTest/RevertPrecompiledTouch.json/Constantinople`, "bug in test")
  46. st.walk(t, stateTestDir, func(t *testing.T, name string, test *StateTest) {
  47. for _, subtest := range test.Subtests() {
  48. subtest := subtest
  49. key := fmt.Sprintf("%s/%d", subtest.Fork, subtest.Index)
  50. name := name + "/" + key
  51. t.Run(key, func(t *testing.T) {
  52. withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
  53. _, err := test.Run(subtest, vmconfig)
  54. return st.checkFailure(t, name, err)
  55. })
  56. })
  57. }
  58. })
  59. }
  60. // Transactions with gasLimit above this value will not get a VM trace on failure.
  61. const traceErrorLimit = 400000
  62. // The VM config for state tests that accepts --vm.* command line arguments.
  63. var testVMConfig = func() vm.Config {
  64. vmconfig := vm.Config{}
  65. flag.StringVar(&vmconfig.EVMInterpreter, utils.EVMInterpreterFlag.Name, utils.EVMInterpreterFlag.Value, utils.EVMInterpreterFlag.Usage)
  66. flag.StringVar(&vmconfig.EWASMInterpreter, utils.EWASMInterpreterFlag.Name, utils.EWASMInterpreterFlag.Value, utils.EWASMInterpreterFlag.Usage)
  67. flag.Parse()
  68. return vmconfig
  69. }()
  70. func withTrace(t *testing.T, gasLimit uint64, test func(vm.Config) error) {
  71. err := test(testVMConfig)
  72. if err == nil {
  73. return
  74. }
  75. t.Error(err)
  76. if gasLimit > traceErrorLimit {
  77. t.Log("gas limit too high for EVM trace")
  78. return
  79. }
  80. tracer := vm.NewStructLogger(nil)
  81. err2 := test(vm.Config{Debug: true, Tracer: tracer})
  82. if !reflect.DeepEqual(err, err2) {
  83. t.Errorf("different error for second run: %v", err2)
  84. }
  85. buf := new(bytes.Buffer)
  86. vm.WriteTrace(buf, tracer.StructLogs())
  87. if buf.Len() == 0 {
  88. t.Log("no EVM operation logs generated")
  89. } else {
  90. t.Log("EVM operation log:\n" + buf.String())
  91. }
  92. t.Logf("EVM output: 0x%x", tracer.Output())
  93. t.Logf("EVM error: %v", tracer.Error())
  94. }