github.com/felberj/go-ethereum@v1.8.23/tests/state_test.go (about)

     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  
    17  package tests
    18  
    19  import (
    20  	"bufio"
    21  	"bytes"
    22  	"flag"
    23  	"fmt"
    24  	"reflect"
    25  	"testing"
    26  
    27  	"github.com/ethereum/go-ethereum/cmd/utils"
    28  	"github.com/ethereum/go-ethereum/core/vm"
    29  )
    30  
    31  func TestState(t *testing.T) {
    32  	t.Parallel()
    33  
    34  	st := new(testMatcher)
    35  	// Long tests:
    36  	st.slow(`^stAttackTest/ContractCreationSpam`)
    37  	st.slow(`^stBadOpcode/badOpcodes`)
    38  	st.slow(`^stPreCompiledContracts/modexp`)
    39  	st.slow(`^stQuadraticComplexityTest/`)
    40  	st.slow(`^stStaticCall/static_Call50000`)
    41  	st.slow(`^stStaticCall/static_Return50000`)
    42  	st.slow(`^stStaticCall/static_Call1MB`)
    43  	st.slow(`^stSystemOperationsTest/CallRecursiveBomb`)
    44  	st.slow(`^stTransactionTest/Opcodes_TransactionInit`)
    45  	// Broken tests:
    46  	st.skipLoad(`^stTransactionTest/OverflowGasRequire\.json`) // gasLimit > 256 bits
    47  	st.skipLoad(`^stTransactionTest/zeroSigTransa[^/]*\.json`) // EIP-86 is not supported yet
    48  	// Expected failures:
    49  	st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Byzantium/0`, "bug in test")
    50  	st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Byzantium/3`, "bug in test")
    51  	st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Constantinople/0`, "bug in test")
    52  	st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Constantinople/3`, "bug in test")
    53  	st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/ConstantinopleFix/0`, "bug in test")
    54  	st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/ConstantinopleFix/3`, "bug in test")
    55  
    56  	st.walk(t, stateTestDir, func(t *testing.T, name string, test *StateTest) {
    57  		for _, subtest := range test.Subtests() {
    58  			subtest := subtest
    59  			key := fmt.Sprintf("%s/%d", subtest.Fork, subtest.Index)
    60  			name := name + "/" + key
    61  			t.Run(key, func(t *testing.T) {
    62  				withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
    63  					_, err := test.Run(subtest, vmconfig)
    64  					return st.checkFailure(t, name, err)
    65  				})
    66  			})
    67  		}
    68  	})
    69  }
    70  
    71  // Transactions with gasLimit above this value will not get a VM trace on failure.
    72  const traceErrorLimit = 400000
    73  
    74  // The VM config for state tests that accepts --vm.* command line arguments.
    75  var testVMConfig = func() vm.Config {
    76  	vmconfig := vm.Config{}
    77  	flag.StringVar(&vmconfig.EVMInterpreter, utils.EVMInterpreterFlag.Name, utils.EVMInterpreterFlag.Value, utils.EVMInterpreterFlag.Usage)
    78  	flag.StringVar(&vmconfig.EWASMInterpreter, utils.EWASMInterpreterFlag.Name, utils.EWASMInterpreterFlag.Value, utils.EWASMInterpreterFlag.Usage)
    79  	flag.Parse()
    80  	return vmconfig
    81  }()
    82  
    83  func withTrace(t *testing.T, gasLimit uint64, test func(vm.Config) error) {
    84  	err := test(testVMConfig)
    85  	if err == nil {
    86  		return
    87  	}
    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  	err2 := test(vm.Config{Debug: true, Tracer: tracer})
    97  	if !reflect.DeepEqual(err, err2) {
    98  		t.Errorf("different error for second run: %v", err2)
    99  	}
   100  	w.Flush()
   101  	if buf.Len() == 0 {
   102  		t.Log("no EVM operation logs generated")
   103  	} else {
   104  		t.Log("EVM operation log:\n" + buf.String())
   105  	}
   106  	//t.Logf("EVM output: 0x%x", tracer.Output())
   107  	//t.Logf("EVM error: %v", tracer.Error())
   108  }