dao_test.go 5.6 KB

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