genesis_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU 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. // go-ethereum 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 General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "io/ioutil"
  19. "os"
  20. "path/filepath"
  21. "testing"
  22. )
  23. var customGenesisTests = []struct {
  24. genesis string
  25. query string
  26. result string
  27. }{
  28. // Plain genesis file without anything extra
  29. {
  30. genesis: `{
  31. "alloc" : {},
  32. "coinbase" : "0x0000000000000000000000000000000000000000",
  33. "difficulty" : "0x20000",
  34. "extraData" : "",
  35. "gasLimit" : "0x2fefd8",
  36. "nonce" : "0x0000000000000042",
  37. "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  38. "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  39. "timestamp" : "0x00"
  40. }`,
  41. query: "eth.getBlock(0).nonce",
  42. result: "0x0000000000000042",
  43. },
  44. // Genesis file with an empty chain configuration (ensure missing fields work)
  45. {
  46. genesis: `{
  47. "alloc" : {},
  48. "coinbase" : "0x0000000000000000000000000000000000000000",
  49. "difficulty" : "0x20000",
  50. "extraData" : "",
  51. "gasLimit" : "0x2fefd8",
  52. "nonce" : "0x0000000000000042",
  53. "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  54. "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  55. "timestamp" : "0x00",
  56. "config" : {}
  57. }`,
  58. query: "eth.getBlock(0).nonce",
  59. result: "0x0000000000000042",
  60. },
  61. // Genesis file with specific chain configurations
  62. {
  63. genesis: `{
  64. "alloc" : {},
  65. "coinbase" : "0x0000000000000000000000000000000000000000",
  66. "difficulty" : "0x20000",
  67. "extraData" : "",
  68. "gasLimit" : "0x2fefd8",
  69. "nonce" : "0x0000000000000042",
  70. "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  71. "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  72. "timestamp" : "0x00",
  73. "config" : {
  74. "homesteadBlock" : 314,
  75. "daoForkBlock" : 141,
  76. "daoForkSupport" : true
  77. },
  78. }`,
  79. query: "eth.getBlock(0).nonce",
  80. result: "0x0000000000000042",
  81. },
  82. }
  83. // Tests that initializing Geth with a custom genesis block and chain definitions
  84. // work properly.
  85. func TestCustomGenesis(t *testing.T) {
  86. for i, tt := range customGenesisTests {
  87. // Create a temporary data directory to use and inspect later
  88. datadir := tmpdir(t)
  89. defer os.RemoveAll(datadir)
  90. // Initialize the data directory with the custom genesis block
  91. json := filepath.Join(datadir, "genesis.json")
  92. if err := ioutil.WriteFile(json, []byte(tt.genesis), 0600); err != nil {
  93. t.Fatalf("test %d: failed to write genesis file: %v", i, err)
  94. }
  95. runGeth(t, "--datadir", datadir, "init", json).WaitExit()
  96. // Query the custom genesis block
  97. geth := runGeth(t,
  98. "--datadir", datadir, "--maxpeers", "0", "--port", "0",
  99. "--nodiscover", "--nat", "none", "--ipcdisable",
  100. "--exec", tt.query, "console")
  101. geth.ExpectRegexp(tt.result)
  102. geth.ExpectExit()
  103. }
  104. }