good morning!!!!

Skip to content
Snippets Groups Projects
init.go 3.11 KiB
Newer Older
  • Learn to ignore specific revisions
  • // Copyright 2015 The go-ethereum Authors
    
    // This file is part of the go-ethereum library.
    
    // The go-ethereum library is free software: you can redistribute it and/or modify
    
    // it under the terms of the GNU Lesser General Public License as published by
    // the Free Software Foundation, either version 3 of the License, or
    // (at your option) any later version.
    //
    
    // The go-ethereum library is distributed in the hope that it will be useful,
    
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    
    // GNU Lesser General Public License for more details.
    //
    // You should have received a copy of the GNU Lesser General Public License
    
    // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    
    // Package tests implements execution of Ethereum JSON tests.
    
    Taylor Gerring's avatar
    Taylor Gerring committed
    package tests
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    
    import (
    	"encoding/json"
    
    Taylor Gerring's avatar
    Taylor Gerring committed
    	"fmt"
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    	"io"
    	"io/ioutil"
    	"net/http"
    	"os"
    
    Taylor Gerring's avatar
    Taylor Gerring committed
    	"path/filepath"
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    )
    
    
    Taylor Gerring's avatar
    Taylor Gerring committed
    var (
    	baseDir            = filepath.Join(".", "files")
    
    	blockTestDir       = filepath.Join(baseDir, "BlockchainTests")
    
    Taylor Gerring's avatar
    Taylor Gerring committed
    	stateTestDir       = filepath.Join(baseDir, "StateTests")
    	transactionTestDir = filepath.Join(baseDir, "TransactionTests")
    	vmTestDir          = filepath.Join(baseDir, "VMTests")
    
    	rlpTestDir         = filepath.Join(baseDir, "RLPTests")
    
    	BlockSkipTests = []string{
    
    		// These tests are not valid, as they are out of scope for RLP and
    		// the consensus protocol.
    
    		"BLOCK__RandomByteAtTheEnd",
    		"TRANSCT__RandomByteAtTheEnd",
    		"BLOCK__ZeroByteAtTheEnd",
    		"TRANSCT__ZeroByteAtTheEnd",
    
    
    		"ChainAtoChainB_blockorder2",
    		"ChainAtoChainB_blockorder1",
    
    	/* Go client does not support transaction (account) nonces above 2^64. This
    
    	technically breaks consensus but is regarded as "reasonable
    	engineering constraint" as accounts cannot easily reach such high
    	nonce values in practice
    	*/
    
    Taylor Gerring's avatar
    Taylor Gerring committed
    	TransSkipTests = []string{"TransactionWithHihghNonce256"}
    
    	StateSkipTests = []string{}
    
    Taylor Gerring's avatar
    Taylor Gerring committed
    	VmSkipTests    = []string{}
    
    Taylor Gerring's avatar
    Taylor Gerring committed
    )
    
    Taylor Gerring's avatar
    Taylor Gerring committed
    func readJson(reader io.Reader, value interface{}) error {
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    	data, err := ioutil.ReadAll(reader)
    	if err != nil {
    
    Felix Lange's avatar
    Felix Lange committed
    		return fmt.Errorf("error reading JSON file: %v", err)
    
    Taylor Gerring's avatar
    Taylor Gerring committed
    	}
    	if err = json.Unmarshal(data, &value); err != nil {
    		if syntaxerr, ok := err.(*json.SyntaxError); ok {
    			line := findLine(data, syntaxerr.Offset)
    			return fmt.Errorf("JSON syntax error at line %v: %v", line, err)
    		}
    		return fmt.Errorf("JSON unmarshal error: %v", err)
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    	}
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    }
    
    
    Taylor Gerring's avatar
    Taylor Gerring committed
    func readJsonHttp(uri string, value interface{}) error {
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    	resp, err := http.Get(uri)
    	if err != nil {
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    	}
    	defer resp.Body.Close()
    
    
    Taylor Gerring's avatar
    Taylor Gerring committed
    	err = readJson(resp.Body, value)
    
    	if err != nil {
    		return err
    	}
    	return nil
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    }
    
    
    Taylor Gerring's avatar
    Taylor Gerring committed
    func readJsonFile(fn string, value interface{}) error {
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    	file, err := os.Open(fn)
    	if err != nil {
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    	}
    	defer file.Close()
    
    
    Taylor Gerring's avatar
    Taylor Gerring committed
    	err = readJson(file, value)
    
    Taylor Gerring's avatar
    Taylor Gerring committed
    		return fmt.Errorf("%s in file %s", err.Error(), fn)
    
    Taylor Gerring's avatar
    Taylor Gerring committed
    
    // findLine returns the line number for the given offset into data.
    func findLine(data []byte, offset int64) (line int) {
    	line = 1
    	for i, r := range string(data) {
    		if int64(i) >= offset {
    			return
    		}
    		if r == '\n' {
    			line++
    		}
    	}
    	return
    }