github.com/core-coin/go-core/v2@v2.1.9/tests/state_test.go (about)

     1  // Copyright 2015 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package tests
    18  
    19  import (
    20  	"bufio"
    21  	"bytes"
    22  	"fmt"
    23  	"reflect"
    24  	"testing"
    25  
    26  	"github.com/core-coin/go-core/v2/core/vm"
    27  )
    28  
    29  func TestState(t *testing.T) {
    30  	t.Parallel()
    31  
    32  	st := new(testMatcher)
    33  	// Long tests:
    34  	st.slow(`^stAttackTest/ContractCreationSpam`)
    35  	st.slow(`^stBadOpcode/badOpcodes`)
    36  	st.slow(`^stPreCompiledContracts/modexp`)
    37  	st.slow(`^stQuadraticComplexityTest/`)
    38  	st.slow(`^stStaticCall/static_Call50000`)
    39  	st.slow(`^stStaticCall/static_Return50000`)
    40  	st.slow(`^stSystemOperationsTest/CallRecursiveBomb`)
    41  	st.slow(`^stTransactionTest/Opcodes_TransactionInit`)
    42  
    43  	// Very time consuming
    44  	st.skipLoad(`^stTimeConsuming/`)
    45  
    46  	// Uses 1GB RAM per tested fork
    47  	st.skipLoad(`^stStaticCall/static_Call1MB`)
    48  
    49  	// For Istanbul, older tests were moved into LegacyTests
    50  	for _, dir := range []string{
    51  		stateTestDir,
    52  		legacyStateTestDir,
    53  	} {
    54  		st.walk(t, dir, func(t *testing.T, name string, test *StateTest) {
    55  			for _, subtest := range test.Subtests() {
    56  				subtest := subtest
    57  				key := fmt.Sprintf("%s/%d", subtest.Fork, subtest.Index)
    58  				name := name + "/" + key
    59  
    60  				t.Run(key+"/trie", func(t *testing.T) {
    61  					withTrace(t, test.energyLimit(subtest), func(vmconfig vm.Config) error {
    62  						_, _, err := test.Run(subtest, vmconfig, false)
    63  						return st.checkFailure(t, name+"/trie", err)
    64  					})
    65  				})
    66  				t.Run(key+"/snap", func(t *testing.T) {
    67  					withTrace(t, test.energyLimit(subtest), func(vmconfig vm.Config) error {
    68  						snaps, statedb, err := test.Run(subtest, vmconfig, true)
    69  						if _, err := snaps.Journal(statedb.IntermediateRoot(false)); err != nil {
    70  							return err
    71  						}
    72  						return st.checkFailure(t, name+"/snap", err)
    73  					})
    74  				})
    75  			}
    76  		})
    77  	}
    78  }
    79  
    80  // Transactions with energyLimit above this value will not get a VM trace on failure.
    81  const traceErrorLimit = 400000
    82  
    83  func withTrace(t *testing.T, energyLimit uint64, test func(vm.Config) error) {
    84  	// Use config from command line arguments.
    85  	config := vm.Config{CVMInterpreter: *testCVM, EWASMInterpreter: *testEWASM}
    86  	err := test(config)
    87  	if err == nil {
    88  		return
    89  	}
    90  
    91  	// Test failed, re-run with tracing enabled.
    92  	t.Error(err)
    93  	if energyLimit > traceErrorLimit {
    94  		t.Log("energy limit too high for CVM trace")
    95  		return
    96  	}
    97  	buf := new(bytes.Buffer)
    98  	w := bufio.NewWriter(buf)
    99  	tracer := vm.NewJSONLogger(&vm.LogConfig{DisableMemory: true}, w)
   100  	config.Debug, config.Tracer = true, tracer
   101  	err2 := test(config)
   102  	if !reflect.DeepEqual(err, err2) {
   103  		t.Errorf("different error for second run: %v", err2)
   104  	}
   105  	w.Flush()
   106  	if buf.Len() == 0 {
   107  		t.Log("no CVM operation logs generated")
   108  	} else {
   109  		t.Log("CVM operation log:\n" + buf.String())
   110  	}
   111  	//t.Logf("CVM output: 0x%x", tracer.Output())
   112  	//t.Logf("CVM error: %v", tracer.Error())
   113  }