github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/tests/state_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:45</date>
    10  //</624450122457026560>
    11  
    12  
    13  package tests
    14  
    15  import (
    16  	"bytes"
    17  	"flag"
    18  	"fmt"
    19  	"reflect"
    20  	"testing"
    21  
    22  	"github.com/ethereum/go-ethereum/cmd/utils"
    23  	"github.com/ethereum/go-ethereum/core/vm"
    24  )
    25  
    26  func TestState(t *testing.T) {
    27  	t.Parallel()
    28  
    29  	st := new(testMatcher)
    30  //长期测试:
    31  	st.slow(`^stAttackTest/ContractCreationSpam`)
    32  	st.slow(`^stBadOpcode/badOpcodes`)
    33  	st.slow(`^stPreCompiledContracts/modexp`)
    34  	st.slow(`^stQuadraticComplexityTest/`)
    35  	st.slow(`^stStaticCall/static_Call50000`)
    36  	st.slow(`^stStaticCall/static_Return50000`)
    37  	st.slow(`^stStaticCall/static_Call1MB`)
    38  	st.slow(`^stSystemOperationsTest/CallRecursiveBomb`)
    39  	st.slow(`^stTransactionTest/Opcodes_TransactionInit`)
    40  //断裂试验:
    41  st.skipLoad(`^stTransactionTest/OverflowGasRequire\.json`) //气体限制>256位
    42  st.skipLoad(`^stTransactionTest/zeroSigTransa[^/]*\.json`) //还不支持EIP-86
    43  //预期故障:
    44  	st.fails(`^stRevertTest/RevertPrecompiledTouch\.json/EIP158`, "bug in test")
    45  	st.fails(`^stRevertTest/RevertPrecompiledTouch\.json/Byzantium`, "bug in test")
    46  	st.fails(`^stRevertTest/RevertPrecompiledTouch.json/Constantinople`, "bug in test")
    47  
    48  	st.walk(t, stateTestDir, func(t *testing.T, name string, test *StateTest) {
    49  		for _, subtest := range test.Subtests() {
    50  			subtest := subtest
    51  			key := fmt.Sprintf("%s/%d", subtest.Fork, subtest.Index)
    52  			name := name + "/" + key
    53  			t.Run(key, func(t *testing.T) {
    54  				withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
    55  					_, err := test.Run(subtest, vmconfig)
    56  					return st.checkFailure(t, name, err)
    57  				})
    58  			})
    59  		}
    60  	})
    61  }
    62  
    63  //GasLimit高于此值的事务在失败时不会获得VM跟踪。
    64  const traceErrorLimit = 400000
    65  
    66  //接受--vm.*命令行参数的状态测试的vm配置。
    67  var testVMConfig = func() vm.Config {
    68  	vmconfig := vm.Config{}
    69  	flag.StringVar(&vmconfig.EVMInterpreter, utils.EVMInterpreterFlag.Name, utils.EVMInterpreterFlag.Value, utils.EVMInterpreterFlag.Usage)
    70  	flag.StringVar(&vmconfig.EWASMInterpreter, utils.EWASMInterpreterFlag.Name, utils.EWASMInterpreterFlag.Value, utils.EWASMInterpreterFlag.Usage)
    71  	flag.Parse()
    72  	return vmconfig
    73  }()
    74  
    75  func withTrace(t *testing.T, gasLimit uint64, test func(vm.Config) error) {
    76  	err := test(testVMConfig)
    77  	if err == nil {
    78  		return
    79  	}
    80  	t.Error(err)
    81  	if gasLimit > traceErrorLimit {
    82  		t.Log("gas limit too high for EVM trace")
    83  		return
    84  	}
    85  	tracer := vm.NewStructLogger(nil)
    86  	err2 := test(vm.Config{Debug: true, Tracer: tracer})
    87  	if !reflect.DeepEqual(err, err2) {
    88  		t.Errorf("different error for second run: %v", err2)
    89  	}
    90  	buf := new(bytes.Buffer)
    91  	vm.WriteTrace(buf, tracer.StructLogs())
    92  	if buf.Len() == 0 {
    93  		t.Log("no EVM operation logs generated")
    94  	} else {
    95  		t.Log("EVM operation log:\n" + buf.String())
    96  	}
    97  	t.Logf("EVM output: 0x%x", tracer.Output())
    98  	t.Logf("EVM error: %v", tracer.Error())
    99  }
   100