init.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. "Vitalik_15",
  54. "Vitalik_16",
  55. "Vitalik_17",
  56. }
  57. StateSkipTests = []string{}
  58. VmSkipTests = []string{}
  59. )
  60. func readJson(reader io.Reader, value interface{}) error {
  61. data, err := ioutil.ReadAll(reader)
  62. if err != nil {
  63. return fmt.Errorf("error reading JSON file: %v", err)
  64. }
  65. if err = json.Unmarshal(data, &value); err != nil {
  66. if syntaxerr, ok := err.(*json.SyntaxError); ok {
  67. line := findLine(data, syntaxerr.Offset)
  68. return fmt.Errorf("JSON syntax error at line %v: %v", line, err)
  69. }
  70. return fmt.Errorf("JSON unmarshal error: %v", err)
  71. }
  72. return nil
  73. }
  74. func readJsonHttp(uri string, value interface{}) error {
  75. resp, err := http.Get(uri)
  76. if err != nil {
  77. return err
  78. }
  79. defer resp.Body.Close()
  80. return readJson(resp.Body, value)
  81. }
  82. func readJsonFile(fn string, value interface{}) error {
  83. file, err := os.Open(fn)
  84. if err != nil {
  85. return err
  86. }
  87. defer file.Close()
  88. err = readJson(file, value)
  89. if err != nil {
  90. return fmt.Errorf("%s in file %s", err.Error(), fn)
  91. }
  92. return nil
  93. }
  94. // findLine returns the line number for the given offset into data.
  95. func findLine(data []byte, offset int64) (line int) {
  96. line = 1
  97. for i, r := range string(data) {
  98. if int64(i) >= offset {
  99. return
  100. }
  101. if r == '\n' {
  102. line++
  103. }
  104. }
  105. return
  106. }