init.go 3.22 KB
Newer Older
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 6 7 8
// 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.
//
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 13 14
// GNU Lesser General Public License for more details.
//
// 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
obscuren's avatar
obscuren committed
19 20 21

import (
	"encoding/json"
Taylor Gerring's avatar
Taylor Gerring committed
22
	"fmt"
obscuren's avatar
obscuren committed
23 24 25 26
	"io"
	"io/ioutil"
	"net/http"
	"os"
Taylor Gerring's avatar
Taylor Gerring committed
27
	"path/filepath"
obscuren's avatar
obscuren committed
28 29
)

Taylor Gerring's avatar
Taylor Gerring committed
30 31
var (
	baseDir            = filepath.Join(".", "files")
32
	blockTestDir       = filepath.Join(baseDir, "BlockchainTests")
Taylor Gerring's avatar
Taylor Gerring committed
33 34 35
	stateTestDir       = filepath.Join(baseDir, "StateTests")
	transactionTestDir = filepath.Join(baseDir, "TransactionTests")
	vmTestDir          = filepath.Join(baseDir, "VMTests")
36
	rlpTestDir         = filepath.Join(baseDir, "RLPTests")
37

38
	BlockSkipTests = []string{
39 40
		// These tests are not valid, as they are out of scope for RLP and
		// the consensus protocol.
41 42 43 44
		"BLOCK__RandomByteAtTheEnd",
		"TRANSCT__RandomByteAtTheEnd",
		"BLOCK__ZeroByteAtTheEnd",
		"TRANSCT__ZeroByteAtTheEnd",
45 46 47

		"ChainAtoChainB_blockorder2",
		"ChainAtoChainB_blockorder1",
48 49 50

		"GasLimitHigherThan2p63m1", // not yet ;)
		"SuicideIssue",             // fails genesis check
51
	}
52

53
	/* Go client does not support transaction (account) nonces above 2^64. This
54 55 56 57
	technically breaks consensus but is regarded as "reasonable
	engineering constraint" as accounts cannot easily reach such high
	nonce values in practice
	*/
58 59
	TransSkipTests = []string{
		"TransactionWithHihghNonce256",
60 61 62
		"Vitalik_15",
		"Vitalik_16",
		"Vitalik_17",
63
	}
64
	StateSkipTests = []string{}
Taylor Gerring's avatar
Taylor Gerring committed
65
	VmSkipTests    = []string{}
Taylor Gerring's avatar
Taylor Gerring committed
66
)
67

Taylor Gerring's avatar
Taylor Gerring committed
68
func readJson(reader io.Reader, value interface{}) error {
obscuren's avatar
obscuren committed
69 70
	data, err := ioutil.ReadAll(reader)
	if err != nil {
Felix Lange's avatar
Felix Lange committed
71
		return fmt.Errorf("error reading JSON file: %v", err)
Taylor Gerring's avatar
Taylor Gerring committed
72 73 74 75 76 77 78
	}
	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)
obscuren's avatar
obscuren committed
79
	}
80
	return nil
obscuren's avatar
obscuren committed
81 82
}

Taylor Gerring's avatar
Taylor Gerring committed
83
func readJsonHttp(uri string, value interface{}) error {
obscuren's avatar
obscuren committed
84 85
	resp, err := http.Get(uri)
	if err != nil {
86
		return err
obscuren's avatar
obscuren committed
87 88 89
	}
	defer resp.Body.Close()

90
	return readJson(resp.Body, value)
obscuren's avatar
obscuren committed
91 92
}

Taylor Gerring's avatar
Taylor Gerring committed
93
func readJsonFile(fn string, value interface{}) error {
obscuren's avatar
obscuren committed
94 95
	file, err := os.Open(fn)
	if err != nil {
96
		return err
obscuren's avatar
obscuren committed
97 98 99
	}
	defer file.Close()

Taylor Gerring's avatar
Taylor Gerring committed
100
	err = readJson(file, value)
101
	if err != nil {
Taylor Gerring's avatar
Taylor Gerring committed
102
		return fmt.Errorf("%s in file %s", err.Error(), fn)
103 104 105
	}
	return nil
}
Taylor Gerring's avatar
Taylor Gerring committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119

// 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
}