state_test.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. "fmt"
  20. "reflect"
  21. "testing"
  22. "github.com/ethereum/go-ethereum/core/vm"
  23. )
  24. func TestState(t *testing.T) {
  25. t.Parallel()
  26. st := new(testMatcher)
  27. // Long tests:
  28. st.slow(`^stAttackTest/ContractCreationSpam`)
  29. st.slow(`^stBadOpcode/badOpcodes`)
  30. st.slow(`^stPreCompiledContracts/modexp`)
  31. st.slow(`^stQuadraticComplexityTest/`)
  32. st.slow(`^stStaticCall/static_Call50000`)
  33. st.slow(`^stStaticCall/static_Return50000`)
  34. st.slow(`^stStaticCall/static_Call1MB`)
  35. st.slow(`^stSystemOperationsTest/CallRecursiveBomb`)
  36. st.slow(`^stTransactionTest/Opcodes_TransactionInit`)
  37. // Broken tests:
  38. st.skipLoad(`^stTransactionTest/OverflowGasRequire\.json`) // gasLimit > 256 bits
  39. st.skipLoad(`^stTransactionTest/zeroSigTransa[^/]*\.json`) // EIP-86 is not supported yet
  40. // Expected failures:
  41. st.fails(`^stRevertTest/RevertPrecompiledTouch\.json/EIP158`, "bug in test")
  42. st.fails(`^stRevertTest/RevertPrecompiledTouch\.json/Byzantium`, "bug in test")
  43. st.fails(`^stRevertTest/RevertPrecompiledTouch.json/Constantinople`, "bug in test")
  44. st.walk(t, stateTestDir, func(t *testing.T, name string, test *StateTest) {
  45. for _, subtest := range test.Subtests() {
  46. subtest := subtest
  47. key := fmt.Sprintf("%s/%d", subtest.Fork, subtest.Index)
  48. name := name + "/" + key
  49. t.Run(key, func(t *testing.T) {
  50. withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
  51. _, err := test.Run(subtest, vmconfig)
  52. return st.checkFailure(t, name, err)
  53. })
  54. })
  55. }
  56. })
  57. }
  58. // Transactions with gasLimit above this value will not get a VM trace on failure.
  59. const traceErrorLimit = 400000
  60. func withTrace(t *testing.T, gasLimit uint64, test func(vm.Config) error) {
  61. err := test(vm.Config{})
  62. if err == nil {
  63. return
  64. }
  65. t.Error(err)
  66. if gasLimit > traceErrorLimit {
  67. t.Log("gas limit too high for EVM trace")
  68. return
  69. }
  70. tracer := vm.NewStructLogger(nil)
  71. err2 := test(vm.Config{Debug: true, Tracer: tracer})
  72. if !reflect.DeepEqual(err, err2) {
  73. t.Errorf("different error for second run: %v", err2)
  74. }
  75. buf := new(bytes.Buffer)
  76. vm.WriteTrace(buf, tracer.StructLogs())
  77. if buf.Len() == 0 {
  78. t.Log("no EVM operation logs generated")
  79. } else {
  80. t.Log("EVM operation log:\n" + buf.String())
  81. }
  82. t.Logf("EVM output: 0x%x", tracer.Output())
  83. t.Logf("EVM error: %v", tracer.Error())
  84. }