github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/tests/state_test.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar 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-aigar 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-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package tests
    19  
    20  import (
    21  	"bufio"
    22  	"bytes"
    23  	"fmt"
    24  	"reflect"
    25  	"testing"
    26  
    27  	"github.com/AigarNetwork/aigar/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  
    45  	// Very time consuming
    46  	st.skipLoad(`^stTimeConsuming/`)
    47  
    48  	// Broken tests:
    49  	// Expected failures:
    50  	//st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Byzantium/0`, "bug in test")
    51  	//st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Byzantium/3`, "bug in test")
    52  	//st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Constantinople/0`, "bug in test")
    53  	//st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Constantinople/3`, "bug in test")
    54  	//st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/ConstantinopleFix/0`, "bug in test")
    55  	//st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/ConstantinopleFix/3`, "bug in test")
    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  				withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
    64  					_, err := test.Run(subtest, vmconfig)
    65  					return st.checkFailure(t, name, err)
    66  				})
    67  			})
    68  		}
    69  	})
    70  	// For Istanbul, older tests were moved into LegacyTests
    71  	st.walk(t, legacyStateTestDir, func(t *testing.T, name string, test *StateTest) {
    72  		for _, subtest := range test.Subtests() {
    73  			subtest := subtest
    74  			key := fmt.Sprintf("%s/%d", subtest.Fork, subtest.Index)
    75  			name := name + "/" + key
    76  			t.Run(key, func(t *testing.T) {
    77  				withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
    78  					_, err := test.Run(subtest, vmconfig)
    79  					return st.checkFailure(t, name, err)
    80  				})
    81  			})
    82  		}
    83  	})
    84  }
    85  
    86  // Transactions with gasLimit above this value will not get a VM trace on failure.
    87  const traceErrorLimit = 400000
    88  
    89  func withTrace(t *testing.T, gasLimit uint64, test func(vm.Config) error) {
    90  	// Use config from command line arguments.
    91  	config := vm.Config{EVMInterpreter: *testEVM, EWASMInterpreter: *testEWASM}
    92  	err := test(config)
    93  	if err == nil {
    94  		return
    95  	}
    96  
    97  	// Test failed, re-run with tracing enabled.
    98  	t.Error(err)
    99  	if gasLimit > traceErrorLimit {
   100  		t.Log("gas limit too high for EVM trace")
   101  		return
   102  	}
   103  	buf := new(bytes.Buffer)
   104  	w := bufio.NewWriter(buf)
   105  	tracer := vm.NewJSONLogger(&vm.LogConfig{DisableMemory: true}, w)
   106  	config.Debug, config.Tracer = true, tracer
   107  	err2 := test(config)
   108  	if !reflect.DeepEqual(err, err2) {
   109  		t.Errorf("different error for second run: %v", err2)
   110  	}
   111  	w.Flush()
   112  	if buf.Len() == 0 {
   113  		t.Log("no EVM operation logs generated")
   114  	} else {
   115  		t.Log("EVM operation log:\n" + buf.String())
   116  	}
   117  	//t.Logf("EVM output: 0x%x", tracer.Output())
   118  	//t.Logf("EVM error: %v", tracer.Error())
   119  }