genesis_test.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // Genesis file with an empty chain configuration (ensure missing fields work)
  29. {
  30. genesis: `{
  31. "alloc" : {},
  32. "coinbase" : "0x0000000000000000000000000000000000000000",
  33. "difficulty" : "0x20000",
  34. "extraData" : "",
  35. "gasLimit" : "0x2fefd8",
  36. "nonce" : "0x0000000000001338",
  37. "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  38. "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  39. "timestamp" : "0x00",
  40. "config" : {}
  41. }`,
  42. query: "eth.getBlock(0).nonce",
  43. result: "0x0000000000001338",
  44. },
  45. // Genesis file with specific chain configurations
  46. {
  47. genesis: `{
  48. "alloc" : {},
  49. "coinbase" : "0x0000000000000000000000000000000000000000",
  50. "difficulty" : "0x20000",
  51. "extraData" : "",
  52. "gasLimit" : "0x2fefd8",
  53. "nonce" : "0x0000000000001339",
  54. "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  55. "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  56. "timestamp" : "0x00",
  57. "config" : {
  58. "homesteadBlock" : 42,
  59. "daoForkBlock" : 141,
  60. "daoForkSupport" : true
  61. }
  62. }`,
  63. query: "eth.getBlock(0).nonce",
  64. result: "0x0000000000001339",
  65. },
  66. }
  67. // Tests that initializing Geth with a custom genesis block and chain definitions
  68. // work properly.
  69. func TestCustomGenesis(t *testing.T) {
  70. for i, tt := range customGenesisTests {
  71. // Create a temporary data directory to use and inspect later
  72. datadir := tmpdir(t)
  73. defer os.RemoveAll(datadir)
  74. // Initialize the data directory with the custom genesis block
  75. json := filepath.Join(datadir, "genesis.json")
  76. if err := ioutil.WriteFile(json, []byte(tt.genesis), 0600); err != nil {
  77. t.Fatalf("test %d: failed to write genesis file: %v", i, err)
  78. }
  79. runGeth(t, "--datadir", datadir, "init", json).WaitExit()
  80. // Query the custom genesis block
  81. geth := runGeth(t, "--networkid", "1337", "--syncmode=full",
  82. "--datadir", datadir, "--maxpeers", "0", "--port", "0",
  83. "--nodiscover", "--nat", "none", "--ipcdisable",
  84. "--exec", tt.query, "console")
  85. geth.ExpectRegexp(tt.result)
  86. geth.ExpectExit()
  87. }
  88. }