dao_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. "math/big"
  20. "os"
  21. "path/filepath"
  22. "testing"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/core/rawdb"
  25. "github.com/ethereum/go-ethereum/params"
  26. )
  27. // Genesis block for nodes which don't care about the DAO fork (i.e. not configured)
  28. var daoOldGenesis = `{
  29. "alloc" : {},
  30. "coinbase" : "0x0000000000000000000000000000000000000000",
  31. "difficulty" : "0x20000",
  32. "extraData" : "",
  33. "gasLimit" : "0x2fefd8",
  34. "nonce" : "0x0000000000000042",
  35. "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  36. "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  37. "timestamp" : "0x00",
  38. "config" : {
  39. "homesteadBlock" : 0
  40. }
  41. }`
  42. // Genesis block for nodes which actively oppose the DAO fork
  43. var daoNoForkGenesis = `{
  44. "alloc" : {},
  45. "coinbase" : "0x0000000000000000000000000000000000000000",
  46. "difficulty" : "0x20000",
  47. "extraData" : "",
  48. "gasLimit" : "0x2fefd8",
  49. "nonce" : "0x0000000000000042",
  50. "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  51. "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  52. "timestamp" : "0x00",
  53. "config" : {
  54. "homesteadBlock" : 0,
  55. "daoForkBlock" : 314,
  56. "daoForkSupport" : false
  57. }
  58. }`
  59. // Genesis block for nodes which actively support the DAO fork
  60. var daoProForkGenesis = `{
  61. "alloc" : {},
  62. "coinbase" : "0x0000000000000000000000000000000000000000",
  63. "difficulty" : "0x20000",
  64. "extraData" : "",
  65. "gasLimit" : "0x2fefd8",
  66. "nonce" : "0x0000000000000042",
  67. "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  68. "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  69. "timestamp" : "0x00",
  70. "config" : {
  71. "homesteadBlock" : 0,
  72. "daoForkBlock" : 314,
  73. "daoForkSupport" : true
  74. }
  75. }`
  76. var daoGenesisHash = common.HexToHash("5e1fc79cb4ffa4739177b5408045cd5d51c6cf766133f23f7cd72ee1f8d790e0")
  77. var daoGenesisForkBlock = big.NewInt(314)
  78. // TestDAOForkBlockNewChain tests that the DAO hard-fork number and the nodes support/opposition is correctly
  79. // set in the database after various initialization procedures and invocations.
  80. func TestDAOForkBlockNewChain(t *testing.T) {
  81. for i, arg := range []struct {
  82. genesis string
  83. expectBlock *big.Int
  84. expectVote bool
  85. }{
  86. // Test DAO Default Mainnet
  87. {"", params.MainnetChainConfig.DAOForkBlock, true},
  88. // test DAO Init Old Privnet
  89. {daoOldGenesis, nil, false},
  90. // test DAO Default No Fork Privnet
  91. {daoNoForkGenesis, daoGenesisForkBlock, false},
  92. // test DAO Default Pro Fork Privnet
  93. {daoProForkGenesis, daoGenesisForkBlock, true},
  94. } {
  95. testDAOForkBlockNewChain(t, i, arg.genesis, arg.expectBlock, arg.expectVote)
  96. }
  97. }
  98. func testDAOForkBlockNewChain(t *testing.T, test int, genesis string, expectBlock *big.Int, expectVote bool) {
  99. defer SetResetPrivateConfig("ignore")()
  100. // Create a temporary data directory to use and inspect later
  101. datadir := tmpdir(t)
  102. defer os.RemoveAll(datadir)
  103. // Start a Geth instance with the requested flags set and immediately terminate
  104. if genesis != "" {
  105. json := filepath.Join(datadir, "genesis.json")
  106. if err := ioutil.WriteFile(json, []byte(genesis), 0600); err != nil {
  107. t.Fatalf("test %d: failed to write genesis file: %v", test, err)
  108. }
  109. runGeth(t, "--datadir", datadir, "--networkid", "1337", "init", json).WaitExit()
  110. } else {
  111. // Force chain initialization
  112. args := []string{"--port", "0", "--networkid", "1337", "--maxpeers", "0", "--nodiscover", "--nat", "none", "--ipcdisable", "--datadir", datadir}
  113. runGeth(t, append(args, []string{"--exec", "2+2", "console"}...)...).WaitExit()
  114. }
  115. // Retrieve the DAO config flag from the database
  116. path := filepath.Join(datadir, "geth", "chaindata")
  117. db, err := rawdb.NewLevelDBDatabase(path, 0, 0, "", false)
  118. if err != nil {
  119. t.Fatalf("test %d: failed to open test database: %v", test, err)
  120. }
  121. defer db.Close()
  122. genesisHash := common.HexToHash("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")
  123. if genesis != "" {
  124. genesisHash = daoGenesisHash
  125. }
  126. config := rawdb.ReadChainConfig(db, genesisHash)
  127. if config == nil {
  128. t.Errorf("test %d: failed to retrieve chain config: %v", test, err)
  129. return // we want to return here, the other checks can't make it past this point (nil panic).
  130. }
  131. // Validate the DAO hard-fork block number against the expected value
  132. if config.DAOForkBlock == nil {
  133. if expectBlock != nil {
  134. t.Errorf("test %d: dao hard-fork block mismatch: have nil, want %v", test, expectBlock)
  135. }
  136. } else if expectBlock == nil {
  137. t.Errorf("test %d: dao hard-fork block mismatch: have %v, want nil", test, config.DAOForkBlock)
  138. } else if config.DAOForkBlock.Cmp(expectBlock) != 0 {
  139. t.Errorf("test %d: dao hard-fork block mismatch: have %v, want %v", test, config.DAOForkBlock, expectBlock)
  140. }
  141. if config.DAOForkSupport != expectVote {
  142. t.Errorf("test %d: dao hard-fork support mismatch: have %v, want %v", test, config.DAOForkSupport, expectVote)
  143. }
  144. }