dao_test.go 5.5 KB

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