github.com/ylsGit/go-ethereum@v1.6.5/tests/init.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 implements execution of Ethereum JSON tests.
    18  package tests
    19  
    20  import (
    21  	"encoding/json"
    22  	"fmt"
    23  	"io"
    24  	"io/ioutil"
    25  	"net/http"
    26  	"os"
    27  	"path/filepath"
    28  )
    29  
    30  var (
    31  	baseDir            = filepath.Join(".", "files")
    32  	blockTestDir       = filepath.Join(baseDir, "BlockchainTests")
    33  	stateTestDir       = filepath.Join(baseDir, "StateTests")
    34  	transactionTestDir = filepath.Join(baseDir, "TransactionTests")
    35  	vmTestDir          = filepath.Join(baseDir, "VMTests")
    36  	rlpTestDir         = filepath.Join(baseDir, "RLPTests")
    37  
    38  	BlockSkipTests = []string{
    39  		// These tests are not valid, as they are out of scope for RLP and
    40  		// the consensus protocol.
    41  		"BLOCK__RandomByteAtTheEnd",
    42  		"TRANSCT__RandomByteAtTheEnd",
    43  		"BLOCK__ZeroByteAtTheEnd",
    44  		"TRANSCT__ZeroByteAtTheEnd",
    45  
    46  		"ChainAtoChainB_blockorder2",
    47  		"ChainAtoChainB_blockorder1",
    48  
    49  		"GasLimitHigherThan2p63m1", // not yet ;)
    50  		"SuicideIssue",             // fails genesis check
    51  	}
    52  
    53  	/* Go client does not support transaction (account) nonces above 2^64. This
    54  	technically breaks consensus but is regarded as "reasonable
    55  	engineering constraint" as accounts cannot easily reach such high
    56  	nonce values in practice
    57  	*/
    58  	TransSkipTests = []string{
    59  		"TransactionWithHihghNonce256",
    60  		"Vitalik_15",
    61  		"Vitalik_16",
    62  		"Vitalik_17",
    63  	}
    64  	StateSkipTests = []string{}
    65  	VmSkipTests    = []string{}
    66  )
    67  
    68  func readJson(reader io.Reader, value interface{}) error {
    69  	data, err := ioutil.ReadAll(reader)
    70  	if err != nil {
    71  		return fmt.Errorf("error reading JSON file: %v", err)
    72  	}
    73  	if err = json.Unmarshal(data, &value); err != nil {
    74  		if syntaxerr, ok := err.(*json.SyntaxError); ok {
    75  			line := findLine(data, syntaxerr.Offset)
    76  			return fmt.Errorf("JSON syntax error at line %v: %v", line, err)
    77  		}
    78  		return fmt.Errorf("JSON unmarshal error: %v", err)
    79  	}
    80  	return nil
    81  }
    82  
    83  func readJsonHttp(uri string, value interface{}) error {
    84  	resp, err := http.Get(uri)
    85  	if err != nil {
    86  		return err
    87  	}
    88  	defer resp.Body.Close()
    89  
    90  	return readJson(resp.Body, value)
    91  }
    92  
    93  func readJsonFile(fn string, value interface{}) error {
    94  	file, err := os.Open(fn)
    95  	if err != nil {
    96  		return err
    97  	}
    98  	defer file.Close()
    99  
   100  	err = readJson(file, value)
   101  	if err != nil {
   102  		return fmt.Errorf("%s in file %s", err.Error(), fn)
   103  	}
   104  	return nil
   105  }
   106  
   107  // findLine returns the line number for the given offset into data.
   108  func findLine(data []byte, offset int64) (line int) {
   109  	line = 1
   110  	for i, r := range string(data) {
   111  		if int64(i) >= offset {
   112  			return
   113  		}
   114  		if r == '\n' {
   115  			line++
   116  		}
   117  	}
   118  	return
   119  }