github.com/LampardNguyen234/go-ethereum@v1.10.16-0.20220117140830-b6a3b0260724/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  	"bufio"
    21  	"bytes"
    22  	"fmt"
    23  	"reflect"
    24  	"testing"
    25  
    26  	"github.com/LampardNguyen234/go-ethereum/core/vm"
    27  	"github.com/LampardNguyen234/go-ethereum/eth/tracers/logger"
    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(`^stSystemOperationsTest/CallRecursiveBomb`)
    42  	st.slow(`^stTransactionTest/Opcodes_TransactionInit`)
    43  
    44  	// Very time consuming
    45  	st.skipLoad(`^stTimeConsuming/`)
    46  	st.skipLoad(`.*vmPerformance/loop.*`)
    47  
    48  	// Uses 1GB RAM per tested fork
    49  	st.skipLoad(`^stStaticCall/static_Call1MB`)
    50  
    51  	// Broken tests:
    52  	// Expected failures:
    53  	//st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Byzantium/0`, "bug in test")
    54  	//st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Byzantium/3`, "bug in test")
    55  	//st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Constantinople/0`, "bug in test")
    56  	//st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Constantinople/3`, "bug in test")
    57  	//st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/ConstantinopleFix/0`, "bug in test")
    58  	//st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/ConstantinopleFix/3`, "bug in test")
    59  
    60  	// For Istanbul, older tests were moved into LegacyTests
    61  	for _, dir := range []string{
    62  		stateTestDir,
    63  		legacyStateTestDir,
    64  	} {
    65  		st.walk(t, dir, func(t *testing.T, name string, test *StateTest) {
    66  			for _, subtest := range test.Subtests() {
    67  				subtest := subtest
    68  				key := fmt.Sprintf("%s/%d", subtest.Fork, subtest.Index)
    69  
    70  				t.Run(key+"/trie", func(t *testing.T) {
    71  					withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
    72  						_, _, err := test.Run(subtest, vmconfig, false)
    73  						if err != nil && len(test.json.Post[subtest.Fork][subtest.Index].ExpectException) > 0 {
    74  							// Ignore expected errors (TODO MariusVanDerWijden check error string)
    75  							return nil
    76  						}
    77  						return st.checkFailure(t, err)
    78  					})
    79  				})
    80  				t.Run(key+"/snap", func(t *testing.T) {
    81  					withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
    82  						snaps, statedb, err := test.Run(subtest, vmconfig, true)
    83  						if snaps != nil && statedb != nil {
    84  							if _, err := snaps.Journal(statedb.IntermediateRoot(false)); err != nil {
    85  								return err
    86  							}
    87  						}
    88  						if err != nil && len(test.json.Post[subtest.Fork][subtest.Index].ExpectException) > 0 {
    89  							// Ignore expected errors (TODO MariusVanDerWijden check error string)
    90  							return nil
    91  						}
    92  						return st.checkFailure(t, err)
    93  					})
    94  				})
    95  			}
    96  		})
    97  	}
    98  }
    99  
   100  // Transactions with gasLimit above this value will not get a VM trace on failure.
   101  const traceErrorLimit = 400000
   102  
   103  func withTrace(t *testing.T, gasLimit uint64, test func(vm.Config) error) {
   104  	// Use config from command line arguments.
   105  	config := vm.Config{}
   106  	err := test(config)
   107  	if err == nil {
   108  		return
   109  	}
   110  
   111  	// Test failed, re-run with tracing enabled.
   112  	t.Error(err)
   113  	if gasLimit > traceErrorLimit {
   114  		t.Log("gas limit too high for EVM trace")
   115  		return
   116  	}
   117  	buf := new(bytes.Buffer)
   118  	w := bufio.NewWriter(buf)
   119  	tracer := logger.NewJSONLogger(&logger.Config{}, w)
   120  	config.Debug, config.Tracer = true, tracer
   121  	err2 := test(config)
   122  	if !reflect.DeepEqual(err, err2) {
   123  		t.Errorf("different error for second run: %v", err2)
   124  	}
   125  	w.Flush()
   126  	if buf.Len() == 0 {
   127  		t.Log("no EVM operation logs generated")
   128  	} else {
   129  		t.Log("EVM operation log:\n" + buf.String())
   130  	}
   131  	// t.Logf("EVM output: 0x%x", tracer.Output())
   132  	// t.Logf("EVM error: %v", tracer.Error())
   133  }