github.com/aidoskuneen/adk-node@v0.0.0-20220315131952-2e32567cb7f4/tests/state_test.go (about)

     1  // Copyright 2021 The adkgo Authors
     2  // This file is part of the adkgo library (adapted for adkgo from go--ethereum v1.10.8).
     3  //
     4  // the adkgo 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 adkgo 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 adkgo 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/aidoskuneen/adk-node/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  	// Broken tests:
    50  	// Expected failures:
    51  	//st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Byzantium/0`, "bug in test")
    52  	//st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Byzantium/3`, "bug in test")
    53  	//st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Constantinople/0`, "bug in test")
    54  	//st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/Constantinople/3`, "bug in test")
    55  	//st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/ConstantinopleFix/0`, "bug in test")
    56  	//st.fails(`^stRevertTest/RevertPrecompiledTouch(_storage)?\.json/ConstantinopleFix/3`, "bug in test")
    57  
    58  	// For Istanbul, older tests were moved into LegacyTests
    59  	for _, dir := range []string{
    60  		stateTestDir,
    61  		legacyStateTestDir,
    62  	} {
    63  		st.walk(t, dir, func(t *testing.T, name string, test *StateTest) {
    64  			for _, subtest := range test.Subtests() {
    65  				subtest := subtest
    66  				key := fmt.Sprintf("%s/%d", subtest.Fork, subtest.Index)
    67  
    68  				t.Run(key+"/trie", func(t *testing.T) {
    69  					withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
    70  						_, _, err := test.Run(subtest, vmconfig, false)
    71  						if err != nil && len(test.json.Post[subtest.Fork][subtest.Index].ExpectException) > 0 {
    72  							// Ignore expected errors (TODO MariusVanDerWijden check error string)
    73  							return nil
    74  						}
    75  						return st.checkFailure(t, err)
    76  					})
    77  				})
    78  				t.Run(key+"/snap", func(t *testing.T) {
    79  					withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
    80  						snaps, statedb, err := test.Run(subtest, vmconfig, true)
    81  						if snaps != nil && statedb != nil {
    82  							if _, err := snaps.Journal(statedb.IntermediateRoot(false)); err != nil {
    83  								return err
    84  							}
    85  						}
    86  						if err != nil && len(test.json.Post[subtest.Fork][subtest.Index].ExpectException) > 0 {
    87  							// Ignore expected errors (TODO MariusVanDerWijden check error string)
    88  							return nil
    89  						}
    90  						return st.checkFailure(t, err)
    91  					})
    92  				})
    93  			}
    94  		})
    95  	}
    96  }
    97  
    98  // Transactions with gasLimit above this value will not get a VM trace on failure.
    99  const traceErrorLimit = 400000
   100  
   101  func withTrace(t *testing.T, gasLimit uint64, test func(vm.Config) error) {
   102  	// Use config from command line arguments.
   103  	config := vm.Config{}
   104  	err := test(config)
   105  	if err == nil {
   106  		return
   107  	}
   108  
   109  	// Test failed, re-run with tracing enabled.
   110  	t.Error(err)
   111  	if gasLimit > traceErrorLimit {
   112  		t.Log("gas limit too high for EVM trace")
   113  		return
   114  	}
   115  	buf := new(bytes.Buffer)
   116  	w := bufio.NewWriter(buf)
   117  	tracer := vm.NewJSONLogger(&vm.LogConfig{DisableMemory: true}, w)
   118  	config.Debug, config.Tracer = true, tracer
   119  	err2 := test(config)
   120  	if !reflect.DeepEqual(err, err2) {
   121  		t.Errorf("different error for second run: %v", err2)
   122  	}
   123  	w.Flush()
   124  	if buf.Len() == 0 {
   125  		t.Log("no EVM operation logs generated")
   126  	} else {
   127  		t.Log("EVM operation log:\n" + buf.String())
   128  	}
   129  	// t.Logf("EVM output: 0x%x", tracer.Output())
   130  	// t.Logf("EVM error: %v", tracer.Error())
   131  }