dao_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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, &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, &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, &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. blocks, _ = GenerateChain(&proConf, conBc.CurrentBlock(), ethash.NewFaker(), db, 1, func(i int, gen *BlockGen) {})
  70. if _, err := conBc.InsertChain(blocks); err == nil {
  71. t.Fatalf("contra-fork chain accepted pro-fork block: %v", blocks[0])
  72. }
  73. // Create a proper no-fork block for the contra-forker
  74. blocks, _ = GenerateChain(&conConf, conBc.CurrentBlock(), ethash.NewFaker(), db, 1, func(i int, gen *BlockGen) {})
  75. if _, err := conBc.InsertChain(blocks); err != nil {
  76. t.Fatalf("contra-fork chain didn't accepted no-fork block: %v", err)
  77. }
  78. // Create a no-fork block, and try to feed into the pro-fork chain
  79. db, _ = ethdb.NewMemDatabase()
  80. gspec.MustCommit(db)
  81. bc, _ = NewBlockChain(db, &proConf, ethash.NewFaker(), vm.Config{})
  82. defer bc.Stop()
  83. blocks = proBc.GetBlocksFromHash(proBc.CurrentBlock().Hash(), int(proBc.CurrentBlock().NumberU64()))
  84. for j := 0; j < len(blocks)/2; j++ {
  85. blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
  86. }
  87. if _, err := bc.InsertChain(blocks); err != nil {
  88. t.Fatalf("failed to import pro-fork chain for expansion: %v", err)
  89. }
  90. blocks, _ = GenerateChain(&conConf, proBc.CurrentBlock(), ethash.NewFaker(), db, 1, func(i int, gen *BlockGen) {})
  91. if _, err := proBc.InsertChain(blocks); err == nil {
  92. t.Fatalf("pro-fork chain accepted contra-fork block: %v", blocks[0])
  93. }
  94. // Create a proper pro-fork block for the pro-forker
  95. blocks, _ = GenerateChain(&proConf, proBc.CurrentBlock(), ethash.NewFaker(), db, 1, func(i int, gen *BlockGen) {})
  96. if _, err := proBc.InsertChain(blocks); err != nil {
  97. t.Fatalf("pro-fork chain didn't accepted pro-fork block: %v", err)
  98. }
  99. }
  100. // Verify that contra-forkers accept pro-fork extra-datas after forking finishes
  101. db, _ = ethdb.NewMemDatabase()
  102. gspec.MustCommit(db)
  103. bc, _ := NewBlockChain(db, &conConf, ethash.NewFaker(), vm.Config{})
  104. defer bc.Stop()
  105. blocks := conBc.GetBlocksFromHash(conBc.CurrentBlock().Hash(), int(conBc.CurrentBlock().NumberU64()))
  106. for j := 0; j < len(blocks)/2; j++ {
  107. blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
  108. }
  109. if _, err := bc.InsertChain(blocks); err != nil {
  110. t.Fatalf("failed to import contra-fork chain for expansion: %v", err)
  111. }
  112. blocks, _ = GenerateChain(&proConf, conBc.CurrentBlock(), ethash.NewFaker(), db, 1, func(i int, gen *BlockGen) {})
  113. if _, err := conBc.InsertChain(blocks); err != nil {
  114. t.Fatalf("contra-fork chain didn't accept pro-fork block post-fork: %v", err)
  115. }
  116. // Verify that pro-forkers accept contra-fork extra-datas after forking finishes
  117. db, _ = ethdb.NewMemDatabase()
  118. gspec.MustCommit(db)
  119. bc, _ = NewBlockChain(db, &proConf, ethash.NewFaker(), vm.Config{})
  120. defer bc.Stop()
  121. blocks = proBc.GetBlocksFromHash(proBc.CurrentBlock().Hash(), int(proBc.CurrentBlock().NumberU64()))
  122. for j := 0; j < len(blocks)/2; j++ {
  123. blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
  124. }
  125. if _, err := bc.InsertChain(blocks); err != nil {
  126. t.Fatalf("failed to import pro-fork chain for expansion: %v", err)
  127. }
  128. blocks, _ = GenerateChain(&conConf, proBc.CurrentBlock(), ethash.NewFaker(), db, 1, func(i int, gen *BlockGen) {})
  129. if _, err := proBc.InsertChain(blocks); err != nil {
  130. t.Fatalf("pro-fork chain didn't accept contra-fork block post-fork: %v", err)
  131. }
  132. }