state_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. "bufio"
  19. "bytes"
  20. "fmt"
  21. "reflect"
  22. "testing"
  23. "github.com/ethereum/go-ethereum/core/vm"
  24. )
  25. func TestState(t *testing.T) {
  26. t.Parallel()
  27. st := new(testMatcher)
  28. // Long tests:
  29. st.slow(`^stAttackTest/ContractCreationSpam`)
  30. st.slow(`^stBadOpcode/badOpcodes`)
  31. st.slow(`^stPreCompiledContracts/modexp`)
  32. st.slow(`^stQuadraticComplexityTest/`)
  33. st.slow(`^stStaticCall/static_Call50000`)
  34. st.slow(`^stStaticCall/static_Return50000`)
  35. st.slow(`^stSystemOperationsTest/CallRecursiveBomb`)
  36. st.slow(`^stTransactionTest/Opcodes_TransactionInit`)
  37. // Very time consuming
  38. st.skipLoad(`^stTimeConsuming/`)
  39. // Uses 1GB RAM per tested fork
  40. st.skipLoad(`^stStaticCall/static_Call1MB`)
  41. // Broken tests:
  42. // Expected failures:
  43. //st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Byzantium/0`, "bug in test")
  44. //st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Byzantium/3`, "bug in test")
  45. //st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Constantinople/0`, "bug in test")
  46. //st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Constantinople/3`, "bug in test")
  47. //st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/ConstantinopleFix/0`, "bug in test")
  48. //st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/ConstantinopleFix/3`, "bug in test")
  49. // For Istanbul, older tests were moved into LegacyTests
  50. for _, dir := range []string{
  51. stateTestDir,
  52. legacyStateTestDir,
  53. } {
  54. st.walk(t, dir, func(t *testing.T, name string, test *StateTest) {
  55. for _, subtest := range test.Subtests() {
  56. subtest := subtest
  57. key := fmt.Sprintf("%s/%d", subtest.Fork, subtest.Index)
  58. name := name + "/" + key
  59. t.Run(key+"/trie", func(t *testing.T) {
  60. withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
  61. _, _, err := test.Run(subtest, vmconfig, false)
  62. return st.checkFailure(t, name+"/trie", err)
  63. })
  64. })
  65. t.Run(key+"/snap", func(t *testing.T) {
  66. withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
  67. snaps, statedb, err := test.Run(subtest, vmconfig, true)
  68. if _, err := snaps.Journal(statedb.IntermediateRoot(false)); err != nil {
  69. return err
  70. }
  71. return st.checkFailure(t, name+"/snap", err)
  72. })
  73. })
  74. }
  75. })
  76. }
  77. }
  78. // Transactions with gasLimit above this value will not get a VM trace on failure.
  79. const traceErrorLimit = 400000
  80. func withTrace(t *testing.T, gasLimit uint64, test func(vm.Config) error) {
  81. // Use config from command line arguments.
  82. config := vm.Config{EVMInterpreter: *testEVM, EWASMInterpreter: *testEWASM}
  83. err := test(config)
  84. if err == nil {
  85. return
  86. }
  87. // Test failed, re-run with tracing enabled.
  88. t.Error(err)
  89. if gasLimit > traceErrorLimit {
  90. t.Log("gas limit too high for EVM trace")
  91. return
  92. }
  93. buf := new(bytes.Buffer)
  94. w := bufio.NewWriter(buf)
  95. tracer := vm.NewJSONLogger(&vm.LogConfig{DisableMemory: true}, w)
  96. config.Debug, config.Tracer = true, tracer
  97. err2 := test(config)
  98. if !reflect.DeepEqual(err, err2) {
  99. t.Errorf("different error for second run: %v", err2)
  100. }
  101. w.Flush()
  102. if buf.Len() == 0 {
  103. t.Log("no EVM operation logs generated")
  104. } else {
  105. t.Log("EVM operation log:\n" + buf.String())
  106. }
  107. //t.Logf("EVM output: 0x%x", tracer.Output())
  108. //t.Logf("EVM error: %v", tracer.Error())
  109. }