github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/tests/state_test.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package tests
    13  
    14  import (
    15  	"bytes"
    16  	"fmt"
    17  	"reflect"
    18  	"testing"
    19  
    20  	"github.com/Sberex/go-sberex/core/vm"
    21  )
    22  
    23  func TestState(t *testing.T) {
    24  	t.Parallel()
    25  
    26  	st := new(testMatcher)
    27  	// Long tests:
    28  	st.skipShortMode(`^stQuadraticComplexityTest/`)
    29  	// Broken tests:
    30  	st.skipLoad(`^stTransactionTest/OverflowGasRequire\.json`) // gasLimit > 256 bits
    31  	st.skipLoad(`^stTransactionTest/zeroSigTransa[^/]*\.json`) // EIP-86 is not supported yet
    32  	// Expected failures:
    33  	st.fails(`^stRevertTest/RevertPrecompiledTouch\.json/EIP158`, "bug in test")
    34  	st.fails(`^stRevertTest/RevertPrefoundEmptyOOG\.json/EIP158`, "bug in test")
    35  	st.fails(`^stRevertTest/RevertPrecompiledTouch\.json/Byzantium`, "bug in test")
    36  	st.fails(`^stRevertTest/RevertPrefoundEmptyOOG\.json/Byzantium`, "bug in test")
    37  	st.fails(`^stRandom2/randomStatetest64[45]\.json/(EIP150|Frontier|Homestead)/.*`, "known bug #15119")
    38  	st.fails(`^stCreateTest/TransactionCollisionToEmpty\.json/EIP158/2`, "known bug ")
    39  	st.fails(`^stCreateTest/TransactionCollisionToEmpty\.json/EIP158/3`, "known bug ")
    40  	st.fails(`^stCreateTest/TransactionCollisionToEmpty\.json/Byzantium/2`, "known bug ")
    41  	st.fails(`^stCreateTest/TransactionCollisionToEmpty\.json/Byzantium/3`, "known bug ")
    42  
    43  	st.walk(t, stateTestDir, func(t *testing.T, name string, test *StateTest) {
    44  		for _, subtest := range test.Subtests() {
    45  			subtest := subtest
    46  			key := fmt.Sprintf("%s/%d", subtest.Fork, subtest.Index)
    47  			name := name + "/" + key
    48  			t.Run(key, func(t *testing.T) {
    49  				if subtest.Fork == "Constantinople" {
    50  					t.Skip("constantinople not supported yet")
    51  				}
    52  				withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
    53  					_, err := test.Run(subtest, vmconfig)
    54  					return st.checkFailure(t, name, err)
    55  				})
    56  			})
    57  		}
    58  	})
    59  }
    60  
    61  // Transactions with gasLimit above this value will not get a VM trace on failure.
    62  const traceErrorLimit = 400000
    63  
    64  func withTrace(t *testing.T, gasLimit uint64, test func(vm.Config) error) {
    65  	err := test(vm.Config{})
    66  	if err == nil {
    67  		return
    68  	}
    69  	t.Error(err)
    70  	if gasLimit > traceErrorLimit {
    71  		t.Log("gas limit too high for EVM trace")
    72  		return
    73  	}
    74  	tracer := vm.NewStructLogger(nil)
    75  	err2 := test(vm.Config{Debug: true, Tracer: tracer})
    76  	if !reflect.DeepEqual(err, err2) {
    77  		t.Errorf("different error for second run: %v", err2)
    78  	}
    79  	buf := new(bytes.Buffer)
    80  	vm.WriteTrace(buf, tracer.StructLogs())
    81  	if buf.Len() == 0 {
    82  		t.Log("no EVM operation logs generated")
    83  	} else {
    84  		t.Log("EVM operation log:\n" + buf.String())
    85  	}
    86  	t.Logf("EVM output: 0x%x", tracer.Output())
    87  	t.Logf("EVM error: %v", tracer.Error())
    88  }