github.com/klaytn/klaytn@v1.10.2/tests/state_test.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2015 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from tests/state_test.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package tests
    22  
    23  import (
    24  	"bytes"
    25  	"fmt"
    26  	"reflect"
    27  	"testing"
    28  
    29  	"github.com/klaytn/klaytn/blockchain/vm"
    30  )
    31  
    32  func TestState(t *testing.T) {
    33  	t.Parallel()
    34  
    35  	st := new(testMatcher)
    36  	// Long tests:
    37  	st.skipShortMode(`^stQuadraticComplexityTest/`)
    38  	// Broken tests:
    39  	st.skipLoad(`^stTransactionTest/OverflowGasRequire\.json`) // gasLimit > 256 bits
    40  	st.skipLoad(`^stTransactionTest/zeroSigTransa[^/]*\.json`) // EIP-86 is not supported yet
    41  	// Expected failures:
    42  	st.fails(`^stRevertTest/RevertPrecompiledTouch\.json/Byzantium`, "bug in test")
    43  	st.skipLoad(`^stZeroKnowledge2/ecmul_0-3_5616_28000_96\.json`)
    44  
    45  	// Skip since the tests transfer values to precompiled contracts
    46  	st.skipLoad(`^stPreCompiledContracts2/CallSha256_1_nonzeroValue.json`)
    47  	st.skipLoad(`^stPreCompiledContracts2/CallIdentity_1_nonzeroValue.json`)
    48  	st.skipLoad(`^stPreCompiledContracts2/CallEcrecover0_NoGas.json`)
    49  	st.skipLoad(`^stRandom2/randomStatetest644.json`)
    50  	st.skipLoad(`^stRandom2/randomStatetest645.json`)
    51  	st.skipLoad(`^stStaticCall/static_CallIdentity_1_nonzeroValue.json`)
    52  	st.skipLoad(`^stStaticCall/static_CallSha256_1_nonzeroValue.json`)
    53  	st.skipLoad(`^stArgsZeroOneBalance/callNonConst.json`)
    54  	st.skipLoad(`^stPreCompiledContracts2/modexpRandomInput.json`)
    55  	st.skipLoad(`^stRandom2/randomStatetest642.json`)
    56  
    57  	st.walk(t, stateTestDir, func(t *testing.T, name string, test *StateTest) {
    58  		for _, subtest := range test.Subtests() {
    59  			subtest := subtest
    60  			key := fmt.Sprintf("%s/%d", subtest.Fork, subtest.Index)
    61  			name := name + "/" + key
    62  			t.Run(key, func(t *testing.T) {
    63  				if subtest.Fork == "Constantinople" {
    64  					t.Skip("constantinople not supported yet")
    65  				}
    66  				withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
    67  					_, err := test.Run(subtest, vmconfig)
    68  					return st.checkFailure(t, name, err)
    69  				})
    70  			})
    71  		}
    72  	})
    73  }
    74  
    75  // Transactions with gasLimit above this value will not get a VM trace on failure.
    76  const traceErrorLimit = 400000
    77  
    78  func withTrace(t *testing.T, gasLimit uint64, test func(vm.Config) error) {
    79  	err := test(vm.Config{})
    80  	if err == nil {
    81  		return
    82  	}
    83  	t.Error(err)
    84  	if gasLimit > traceErrorLimit {
    85  		t.Log("gas limit too high for EVM trace")
    86  		return
    87  	}
    88  	tracer := vm.NewStructLogger(nil)
    89  	err2 := test(vm.Config{Debug: true, Tracer: tracer})
    90  	if !reflect.DeepEqual(err, err2) {
    91  		t.Errorf("different error for second run: %v", err2)
    92  	}
    93  	buf := new(bytes.Buffer)
    94  	vm.WriteTrace(buf, tracer.StructLogs())
    95  	if buf.Len() == 0 {
    96  		t.Log("no EVM operation logs generated")
    97  	} else {
    98  		t.Log("EVM operation log:\n" + buf.String())
    99  	}
   100  	t.Logf("EVM output: 0x%x", tracer.Output())
   101  	t.Logf("EVM error: %v", tracer.Error())
   102  }