genesis_test.go 3.0 KB

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