dao_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser 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. // The go-ethereum library 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 Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package core
  17. import (
  18. "math/big"
  19. "testing"
  20. "github.com/ethereum/go-ethereum/consensus/ethash"
  21. "github.com/ethereum/go-ethereum/core/vm"
  22. "github.com/ethereum/go-ethereum/ethdb"
  23. "github.com/ethereum/go-ethereum/params"
  24. )
  25. // Tests that DAO-fork enabled clients can properly filter out fork-commencing
  26. // blocks based on their extradata fields.
  27. func TestDAOForkRangeExtradata(t *testing.T) {
  28. forkBlock := big.NewInt(32)
  29. // Generate a common prefix for both pro-forkers and non-forkers
  30. db := ethdb.NewMemDatabase()
  31. gspec := new(Genesis)
  32. genesis := gspec.MustCommit(db)
  33. prefix, _ := GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), db, int(forkBlock.Int64()-1), func(i int, gen *BlockGen) {})
  34. // Create the concurrent, conflicting two nodes
  35. proDb := ethdb.NewMemDatabase()
  36. gspec.MustCommit(proDb)
  37. proConf := *params.TestChainConfig
  38. proConf.DAOForkBlock = forkBlock
  39. proConf.DAOForkSupport = true
  40. proBc, _ := NewBlockChain(proDb, nil, &proConf, ethash.NewFaker(), vm.Config{})
  41. defer proBc.Stop()
  42. conDb := ethdb.NewMemDatabase()
  43. gspec.MustCommit(conDb)
  44. conConf := *params.TestChainConfig
  45. conConf.DAOForkBlock = forkBlock
  46. conConf.DAOForkSupport = false
  47. conBc, _ := NewBlockChain(conDb, nil, &conConf, ethash.NewFaker(), vm.Config{})
  48. defer conBc.Stop()
  49. if _, err := proBc.InsertChain(prefix); err != nil {
  50. t.Fatalf("pro-fork: failed to import chain prefix: %v", err)
  51. }
  52. if _, err := conBc.InsertChain(prefix); err != nil {
  53. t.Fatalf("con-fork: failed to import chain prefix: %v", err)
  54. }
  55. // Try to expand both pro-fork and non-fork chains iteratively with other camp's blocks
  56. for i := int64(0); i < params.DAOForkExtraRange.Int64(); i++ {
  57. // Create a pro-fork block, and try to feed into the no-fork chain
  58. db = ethdb.NewMemDatabase()
  59. gspec.MustCommit(db)
  60. bc, _ := NewBlockChain(db, nil, &conConf, ethash.NewFaker(), vm.Config{})
  61. defer bc.Stop()
  62. blocks := conBc.GetBlocksFromHash(conBc.CurrentBlock().Hash(), int(conBc.CurrentBlock().NumberU64()))
  63. for j := 0; j < len(blocks)/2; j++ {
  64. blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
  65. }
  66. if _, err := bc.InsertChain(blocks); err != nil {
  67. t.Fatalf("failed to import contra-fork chain for expansion: %v", err)
  68. }
  69. if err := bc.stateCache.TrieDB().Commit(bc.CurrentHeader().Root, true); err != nil {
  70. t.Fatalf("failed to commit contra-fork head for expansion: %v", err)
  71. }
  72. blocks, _ = GenerateChain(&proConf, conBc.CurrentBlock(), ethash.NewFaker(), db, 1, func(i int, gen *BlockGen) {})
  73. if _, err := conBc.InsertChain(blocks); err == nil {
  74. t.Fatalf("contra-fork chain accepted pro-fork block: %v", blocks[0])
  75. }
  76. // Create a proper no-fork block for the contra-forker
  77. blocks, _ = GenerateChain(&conConf, conBc.CurrentBlock(), ethash.NewFaker(), db, 1, func(i int, gen *BlockGen) {})
  78. if _, err := conBc.InsertChain(blocks); err != nil {
  79. t.Fatalf("contra-fork chain didn't accepted no-fork block: %v", err)
  80. }
  81. // Create a no-fork block, and try to feed into the pro-fork chain
  82. db = ethdb.NewMemDatabase()
  83. gspec.MustCommit(db)
  84. bc, _ = NewBlockChain(db, nil, &proConf, ethash.NewFaker(), vm.Config{})
  85. defer bc.Stop()
  86. blocks = proBc.GetBlocksFromHash(proBc.CurrentBlock().Hash(), int(proBc.CurrentBlock().NumberU64()))
  87. for j := 0; j < len(blocks)/2; j++ {
  88. blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
  89. }
  90. if _, err := bc.InsertChain(blocks); err != nil {
  91. t.Fatalf("failed to import pro-fork chain for expansion: %v", err)
  92. }
  93. if err := bc.stateCache.TrieDB().Commit(bc.CurrentHeader().Root, true); err != nil {
  94. t.Fatalf("failed to commit pro-fork head for expansion: %v", err)
  95. }
  96. blocks, _ = GenerateChain(&conConf, proBc.CurrentBlock(), ethash.NewFaker(), db, 1, func(i int, gen *BlockGen) {})
  97. if _, err := proBc.InsertChain(blocks); err == nil {
  98. t.Fatalf("pro-fork chain accepted contra-fork block: %v", blocks[0])
  99. }
  100. // Create a proper pro-fork block for the pro-forker
  101. blocks, _ = GenerateChain(&proConf, proBc.CurrentBlock(), ethash.NewFaker(), db, 1, func(i int, gen *BlockGen) {})
  102. if _, err := proBc.InsertChain(blocks); err != nil {
  103. t.Fatalf("pro-fork chain didn't accepted pro-fork block: %v", err)
  104. }
  105. }
  106. // Verify that contra-forkers accept pro-fork extra-datas after forking finishes
  107. db = ethdb.NewMemDatabase()
  108. gspec.MustCommit(db)
  109. bc, _ := NewBlockChain(db, nil, &conConf, ethash.NewFaker(), vm.Config{})
  110. defer bc.Stop()
  111. blocks := conBc.GetBlocksFromHash(conBc.CurrentBlock().Hash(), int(conBc.CurrentBlock().NumberU64()))
  112. for j := 0; j < len(blocks)/2; j++ {
  113. blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
  114. }
  115. if _, err := bc.InsertChain(blocks); err != nil {
  116. t.Fatalf("failed to import contra-fork chain for expansion: %v", err)
  117. }
  118. if err := bc.stateCache.TrieDB().Commit(bc.CurrentHeader().Root, true); err != nil {
  119. t.Fatalf("failed to commit contra-fork head for expansion: %v", err)
  120. }
  121. blocks, _ = GenerateChain(&proConf, conBc.CurrentBlock(), ethash.NewFaker(), db, 1, func(i int, gen *BlockGen) {})
  122. if _, err := conBc.InsertChain(blocks); err != nil {
  123. t.Fatalf("contra-fork chain didn't accept pro-fork block post-fork: %v", err)
  124. }
  125. // Verify that pro-forkers accept contra-fork extra-datas after forking finishes
  126. db = ethdb.NewMemDatabase()
  127. gspec.MustCommit(db)
  128. bc, _ = NewBlockChain(db, nil, &proConf, ethash.NewFaker(), vm.Config{})
  129. defer bc.Stop()
  130. blocks = proBc.GetBlocksFromHash(proBc.CurrentBlock().Hash(), int(proBc.CurrentBlock().NumberU64()))
  131. for j := 0; j < len(blocks)/2; j++ {
  132. blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
  133. }
  134. if _, err := bc.InsertChain(blocks); err != nil {
  135. t.Fatalf("failed to import pro-fork chain for expansion: %v", err)
  136. }
  137. if err := bc.stateCache.TrieDB().Commit(bc.CurrentHeader().Root, true); err != nil {
  138. t.Fatalf("failed to commit pro-fork head for expansion: %v", err)
  139. }
  140. blocks, _ = GenerateChain(&conConf, proBc.CurrentBlock(), ethash.NewFaker(), db, 1, func(i int, gen *BlockGen) {})
  141. if _, err := proBc.InsertChain(blocks); err != nil {
  142. t.Fatalf("pro-fork chain didn't accept contra-fork block post-fork: %v", err)
  143. }
  144. }