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