init.go 3.2 KB

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