state_test.go 2.7 KB

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