dao_test.go 6.0 KB

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