init.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package tests
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "net/http"
  8. "os"
  9. "path/filepath"
  10. "github.com/ethereum/go-ethereum/core"
  11. )
  12. var (
  13. baseDir = filepath.Join(".", "files")
  14. blockTestDir = filepath.Join(baseDir, "BlockchainTests")
  15. stateTestDir = filepath.Join(baseDir, "StateTests")
  16. transactionTestDir = filepath.Join(baseDir, "TransactionTests")
  17. vmTestDir = filepath.Join(baseDir, "VMTests")
  18. BlockSkipTests = []string{
  19. // Fails in InsertPreState with: computed state root does not
  20. // match genesis block bba25a96 0d8f85c8 Christoph said it will be
  21. // fixed eventually
  22. "SimpleTx3",
  23. // These tests are not valid, as they are out of scope for RLP and
  24. // the consensus protocol.
  25. "BLOCK__RandomByteAtTheEnd",
  26. "TRANSCT__RandomByteAtTheEnd",
  27. "BLOCK__ZeroByteAtTheEnd",
  28. "TRANSCT__ZeroByteAtTheEnd",
  29. }
  30. /* Go does not support transaction (account) nonces above 2^64. This
  31. technically breaks consensus but is regarded as "reasonable
  32. engineering constraint" as accounts cannot easily reach such high
  33. nonce values in practice
  34. */
  35. TransSkipTests = []string{"TransactionWithHihghNonce256"}
  36. StateSkipTests = []string{}
  37. VmSkipTests = []string{}
  38. )
  39. func readJson(reader io.Reader, value interface{}) error {
  40. data, err := ioutil.ReadAll(reader)
  41. if err != nil {
  42. return fmt.Errorf("Error reading JSON file", err.Error())
  43. }
  44. core.DisableBadBlockReporting = true
  45. if err = json.Unmarshal(data, &value); err != nil {
  46. if syntaxerr, ok := err.(*json.SyntaxError); ok {
  47. line := findLine(data, syntaxerr.Offset)
  48. return fmt.Errorf("JSON syntax error at line %v: %v", line, err)
  49. }
  50. return fmt.Errorf("JSON unmarshal error: %v", err)
  51. }
  52. return nil
  53. }
  54. func readJsonHttp(uri string, value interface{}) error {
  55. resp, err := http.Get(uri)
  56. if err != nil {
  57. return err
  58. }
  59. defer resp.Body.Close()
  60. err = readJson(resp.Body, value)
  61. if err != nil {
  62. return err
  63. }
  64. return nil
  65. }
  66. func readJsonFile(fn string, value interface{}) error {
  67. file, err := os.Open(fn)
  68. if err != nil {
  69. return err
  70. }
  71. defer file.Close()
  72. err = readJson(file, value)
  73. if err != nil {
  74. return fmt.Errorf("%s in file %s", err.Error(), fn)
  75. }
  76. return nil
  77. }
  78. // findLine returns the line number for the given offset into data.
  79. func findLine(data []byte, offset int64) (line int) {
  80. line = 1
  81. for i, r := range string(data) {
  82. if int64(i) >= offset {
  83. return
  84. }
  85. if r == '\n' {
  86. line++
  87. }
  88. }
  89. return
  90. }