github.com/cranelv/ethereum_mpc@v0.0.0-20191031014521-23aeb1415092/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  	"fmt"
    22  	"reflect"
    23  	"testing"
    24  
    25  	"github.com/ethereum/go-ethereum/core/vm"
    26  	"github.com/ethereum/go-ethereum/log"
    27  )
    28  func initLog(t *testing.T) {
    29  	glogger := log.NewTestHandler(t)
    30  	glogger.Verbosity(3)
    31  	//	glogger.Vmodule(*vmodule)
    32  	log.Root().SetHandler(glogger)
    33  }
    34  
    35  func TestState(t *testing.T) {
    36  	initLog(t)
    37  	t.Parallel()
    38  
    39  	st := new(testMatcher)
    40  	// Long tests:
    41  	st.skipShortMode(`^stQuadraticComplexityTest/`)
    42  	// Broken tests:
    43  	st.skipLoad(`^stTransactionTest/OverflowGasRequire\.json`) // gasLimit > 256 bits
    44  	st.skipLoad(`^stTransactionTest/zeroSigTransa[^/]*\.json`) // EIP-86 is not supported yet
    45  	// Expected failures:
    46  	st.fails(`^stRevertTest/RevertPrecompiledTouch\.json/EIP158`, "bug in test")
    47  	st.fails(`^stRevertTest/RevertPrecompiledTouch\.json/Byzantium`, "bug in test")
    48  
    49  	st.walk(t, stateTestDir, func(t *testing.T, name string, test *StateTest) {
    50  		for _, subtest := range test.Subtests() {
    51  			subtest := subtest
    52  			key := fmt.Sprintf("%s/%d", subtest.Fork, subtest.Index)
    53  			name := name + "/" + key
    54  			t.Run(key, func(t *testing.T) {
    55  				if subtest.Fork == "Constantinople" {
    56  					t.Skip("constantinople not supported yet")
    57  				}
    58  				withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
    59  					_, err := test.Run(subtest, vmconfig)
    60  					return st.checkFailure(t, name, err)
    61  				})
    62  			})
    63  		}
    64  	})
    65  }
    66  
    67  // Transactions with gasLimit above this value will not get a VM trace on failure.
    68  const traceErrorLimit = 400000
    69  
    70  func withTrace(t *testing.T, gasLimit uint64, test func(vm.Config) error) {
    71  	err := test(vm.Config{})
    72  	if err == nil {
    73  		return
    74  	}
    75  	t.Error(err)
    76  	if gasLimit > traceErrorLimit {
    77  		t.Log("gas limit too high for EVM trace")
    78  		return
    79  	}
    80  	tracer := vm.NewStructLogger(nil)
    81  	err2 := test(vm.Config{Debug: true, Tracer: tracer})
    82  	if !reflect.DeepEqual(err, err2) {
    83  		t.Errorf("different error for second run: %v", err2)
    84  	}
    85  	buf := new(bytes.Buffer)
    86  	vm.WriteTrace(buf, tracer.StructLogs())
    87  	if buf.Len() == 0 {
    88  		t.Log("no EVM operation logs generated")
    89  	} else {
    90  		t.Log("EVM operation log:\n" + buf.String())
    91  	}
    92  	t.Logf("EVM output: 0x%x", tracer.Output())
    93  	t.Logf("EVM error: %v", tracer.Error())
    94  }