init.go 3.3 KB

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