github.com/codingfuture/orig-energi3@v0.8.4/tests/state_test.go (about)

     1  // Copyright 2018 The Energi Core Authors
     2  // Copyright 2015 The go-ethereum Authors
     3  // This file is part of the Energi Core library.
     4  //
     5  // The Energi Core library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The Energi Core library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package tests
    19  
    20  import (
    21  	"bufio"
    22  	"bytes"
    23  	"flag"
    24  	"fmt"
    25  	"os"
    26  	"reflect"
    27  	"testing"
    28  
    29  	"github.com/ethereum/go-ethereum/cmd/utils"
    30  	"github.com/ethereum/go-ethereum/core/vm"
    31  )
    32  
    33  func TestState(t *testing.T) {
    34  	if val, ok := os.LookupEnv("SKIP_KNOWN_FAIL"); ok && val == "1" {
    35  		t.Skip("unit test is broken: conditional test skipping activated")
    36  	}
    37  	t.Parallel()
    38  
    39  	st := new(testMatcher)
    40  	// Long tests:
    41  	st.slow(`^stAttackTest/ContractCreationSpam`)
    42  	st.slow(`^stBadOpcode/badOpcodes`)
    43  	st.slow(`^stPreCompiledContracts/modexp`)
    44  	st.slow(`^stQuadraticComplexityTest/`)
    45  	st.slow(`^stStaticCall/static_Call50000`)
    46  	st.slow(`^stStaticCall/static_Return50000`)
    47  	st.slow(`^stStaticCall/static_Call1MB`)
    48  	st.slow(`^stSystemOperationsTest/CallRecursiveBomb`)
    49  	st.slow(`^stTransactionTest/Opcodes_TransactionInit`)
    50  	// Broken tests:
    51  	st.skipLoad(`^stTransactionTest/OverflowGasRequire\.json`) // gasLimit > 256 bits
    52  	st.skipLoad(`^stTransactionTest/zeroSigTransa[^/]*\.json`) // EIP-86 is not supported yet
    53  	// Expected failures:
    54  	st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Byzantium/0`, "bug in test")
    55  	st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Byzantium/3`, "bug in test")
    56  	st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Constantinople/0`, "bug in test")
    57  	st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Constantinople/3`, "bug in test")
    58  	st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/ConstantinopleFix/0`, "bug in test")
    59  	st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/ConstantinopleFix/3`, "bug in test")
    60  
    61  	st.walk(t, stateTestDir, func(t *testing.T, name string, test *StateTest) {
    62  		for _, subtest := range test.Subtests() {
    63  			subtest := subtest
    64  			key := fmt.Sprintf("%s/%d", subtest.Fork, subtest.Index)
    65  			name := name + "/" + key
    66  			t.Run(key, func(t *testing.T) {
    67  				withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
    68  					_, err := test.Run(subtest, vmconfig)
    69  					return st.checkFailure(t, name, err)
    70  				})
    71  			})
    72  		}
    73  	})
    74  }
    75  
    76  // Transactions with gasLimit above this value will not get a VM trace on failure.
    77  const traceErrorLimit = 400000
    78  
    79  // The VM config for state tests that accepts --vm.* command line arguments.
    80  var testVMConfig = func() vm.Config {
    81  	testing.Init()
    82  	vmconfig := vm.Config{}
    83  	flag.StringVar(&vmconfig.EVMInterpreter, utils.EVMInterpreterFlag.Name, utils.EVMInterpreterFlag.Value, utils.EVMInterpreterFlag.Usage)
    84  	flag.StringVar(&vmconfig.EWASMInterpreter, utils.EWASMInterpreterFlag.Name, utils.EWASMInterpreterFlag.Value, utils.EWASMInterpreterFlag.Usage)
    85  	flag.Parse()
    86  	return vmconfig
    87  }()
    88  
    89  func withTrace(t *testing.T, gasLimit uint64, test func(vm.Config) error) {
    90  	err := test(testVMConfig)
    91  	if err == nil {
    92  		return
    93  	}
    94  	t.Error(err)
    95  	if gasLimit > traceErrorLimit {
    96  		t.Log("gas limit too high for EVM trace")
    97  		return
    98  	}
    99  	buf := new(bytes.Buffer)
   100  	w := bufio.NewWriter(buf)
   101  	tracer := vm.NewJSONLogger(&vm.LogConfig{DisableMemory: true}, w)
   102  	err2 := test(vm.Config{Debug: true, Tracer: tracer})
   103  	if !reflect.DeepEqual(err, err2) {
   104  		t.Errorf("different error for second run: %v", err2)
   105  	}
   106  	w.Flush()
   107  	if buf.Len() == 0 {
   108  		t.Log("no EVM operation logs generated")
   109  	} else {
   110  		t.Log("EVM operation log:\n" + buf.String())
   111  	}
   112  	//t.Logf("EVM output: 0x%x", tracer.Output())
   113  	//t.Logf("EVM error: %v", tracer.Error())
   114  }