genesis_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. "strings"
  22. "testing"
  23. "github.com/cespare/cp"
  24. )
  25. var customGenesisTests = []struct {
  26. genesis string
  27. query string
  28. result string
  29. }{
  30. // Genesis file with an empty chain configuration (ensure missing fields work)
  31. {
  32. genesis: `{
  33. "alloc" : {},
  34. "coinbase" : "0x0000000000000000000000000000000000000000",
  35. "difficulty" : "0x20000",
  36. "extraData" : "",
  37. "gasLimit" : "0x2fefd8",
  38. "nonce" : "0x0000000000001338",
  39. "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  40. "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  41. "timestamp" : "0x00",
  42. "config" : { "isQuorum":false }
  43. }`,
  44. query: "eth.getBlock(0).nonce",
  45. result: "0x0000000000001338",
  46. },
  47. // Genesis file with specific chain configurations
  48. {
  49. genesis: `{
  50. "alloc" : {},
  51. "coinbase" : "0x0000000000000000000000000000000000000000",
  52. "difficulty" : "0x20000",
  53. "extraData" : "",
  54. "gasLimit" : "0x2fefd8",
  55. "nonce" : "0x0000000000000042",
  56. "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  57. "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  58. "timestamp" : "0x00",
  59. "config" : {
  60. "homesteadBlock" : 42,
  61. "daoForkBlock" : 141,
  62. "daoForkSupport" : true,
  63. "isQuorum" : false
  64. },
  65. }`,
  66. query: "eth.getBlock(0).nonce",
  67. result: "0x0000000000000042",
  68. },
  69. }
  70. // Tests that initializing Geth with a custom genesis block and chain definitions
  71. // work properly.
  72. func TestCustomGenesis(t *testing.T) {
  73. defer SetResetPrivateConfig("ignore")()
  74. for i, tt := range customGenesisTests {
  75. // Create a temporary data directory to use and inspect later
  76. datadir := tmpdir(t)
  77. defer os.RemoveAll(datadir)
  78. // copy the node key and static-nodes.json so that geth can start with the raft consensus
  79. gethDir := filepath.Join(datadir, "geth")
  80. sourceNodeKey := filepath.Join("testdata", "geth")
  81. if err := cp.CopyAll(gethDir, sourceNodeKey); err != nil {
  82. t.Fatal(err)
  83. }
  84. // Initialize the data directory with the custom genesis block
  85. json := filepath.Join(datadir, "genesis.json")
  86. if err := ioutil.WriteFile(json, []byte(tt.genesis), 0600); err != nil {
  87. t.Fatalf("test %d: failed to write genesis file: %v", i, err)
  88. }
  89. runGeth(t, "--datadir", datadir, "init", json).WaitExit()
  90. // Query the custom genesis block
  91. geth := runGeth(t, "--networkid", "1337", "--syncmode=full",
  92. "--datadir", datadir, "--maxpeers", "0", "--port", "0",
  93. "--nodiscover", "--nat", "none", "--ipcdisable",
  94. "--raft",
  95. "--exec", tt.query, "console")
  96. geth.ExpectRegexp(tt.result)
  97. geth.ExpectExit()
  98. }
  99. }
  100. func TestCustomGenesisUpgradeWithPrivacyEnhancementsBlock(t *testing.T) {
  101. defer SetResetPrivateConfig("ignore")()
  102. // Create a temporary data directory to use and inspect later
  103. datadir := tmpdir(t)
  104. defer os.RemoveAll(datadir)
  105. // copy the node key and static-nodes.json so that geth can start with the raft consensus
  106. gethDir := filepath.Join(datadir, "geth")
  107. sourceNodeKey := filepath.Join("testdata", "geth")
  108. if err := cp.CopyAll(gethDir, sourceNodeKey); err != nil {
  109. t.Fatal(err)
  110. }
  111. genesisContentWithoutPrivacyEnhancements :=
  112. `{
  113. "alloc" : {},
  114. "coinbase" : "0x0000000000000000000000000000000000000000",
  115. "difficulty" : "0x20000",
  116. "extraData" : "",
  117. "gasLimit" : "0x2fefd8",
  118. "nonce" : "0x0000000000000042",
  119. "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  120. "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  121. "timestamp" : "0x00",
  122. "config" : {
  123. "homesteadBlock" : 42,
  124. "daoForkBlock" : 141,
  125. "daoForkSupport" : true,
  126. "isQuorum" : false
  127. }
  128. }`
  129. // Initialize the data directory with the custom genesis block
  130. json := filepath.Join(datadir, "genesis.json")
  131. if err := ioutil.WriteFile(json, []byte(genesisContentWithoutPrivacyEnhancements), 0600); err != nil {
  132. t.Fatalf("failed to write genesis file: %v", err)
  133. }
  134. geth := runGeth(t, "--datadir", datadir, "init", json)
  135. geth.WaitExit()
  136. genesisContentWithPrivacyEnhancements :=
  137. `{
  138. "alloc" : {},
  139. "coinbase" : "0x0000000000000000000000000000000000000000",
  140. "difficulty" : "0x20000",
  141. "extraData" : "",
  142. "gasLimit" : "0x2fefd8",
  143. "nonce" : "0x0000000000000042",
  144. "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  145. "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  146. "timestamp" : "0x00",
  147. "config" : {
  148. "homesteadBlock" : 42,
  149. "daoForkBlock" : 141,
  150. "privacyEnhancementsBlock" : 1000,
  151. "daoForkSupport" : true,
  152. "isQuorum" : false
  153. }
  154. }`
  155. if err := ioutil.WriteFile(json, []byte(genesisContentWithPrivacyEnhancements), 0600); err != nil {
  156. t.Fatalf("failed to write genesis file: %v", err)
  157. }
  158. geth = runGeth(t, "--datadir", datadir, "init", json)
  159. geth.WaitExit()
  160. expectedText := "Privacy enhancements have been enabled from block height 1000. Please ensure your privacy manager is upgraded and supports privacy enhancements"
  161. result := strings.TrimSpace(geth.StderrText())
  162. if !strings.Contains(result, expectedText) {
  163. geth.Fatalf("bad stderr text. want '%s', got '%s'", expectedText, result)
  164. }
  165. // start quorum - it should fail the transaction manager PrivacyEnhancements feature validation
  166. geth = runGeth(t,
  167. "--datadir", datadir, "--maxpeers", "0", "--port", "0",
  168. "--nodiscover", "--nat", "none", "--ipcdisable",
  169. "--raft", "console")
  170. geth.ExpectRegexp("Cannot start quorum with privacy enhancements enabled while the transaction manager does not support it")
  171. geth.ExpectExit()
  172. }