state_test.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
  42. _, err := test.Run(subtest, vmconfig)
  43. return st.checkFailure(t, name, err)
  44. })
  45. })
  46. }
  47. })
  48. }
  49. // Transactions with gasLimit above this value will not get a VM trace on failure.
  50. const traceErrorLimit = 400000
  51. func withTrace(t *testing.T, gasLimit uint64, test func(vm.Config) error) {
  52. err := test(vm.Config{})
  53. if err == nil {
  54. return
  55. }
  56. t.Error(err)
  57. if gasLimit > traceErrorLimit {
  58. t.Log("gas limit too high for EVM trace")
  59. return
  60. }
  61. tracer := vm.NewStructLogger(nil)
  62. err2 := test(vm.Config{Debug: true, Tracer: tracer})
  63. if !reflect.DeepEqual(err, err2) {
  64. t.Errorf("different error for second run: %v", err2)
  65. }
  66. buf := new(bytes.Buffer)
  67. vm.WriteTrace(buf, tracer.StructLogs())
  68. if buf.Len() == 0 {
  69. t.Log("no EVM operation logs generated")
  70. } else {
  71. t.Log("EVM operation log:\n" + buf.String())
  72. }
  73. t.Logf("EVM output: 0x%x", tracer.Output())
  74. t.Logf("EVM error: %v", tracer.Error())
  75. }